From a7f90aae08ae76d1cd68d7a1082b76c918526e15 Mon Sep 17 00:00:00 2001 From: Edward Capriolo Date: Fri, 29 Apr 2016 13:35:51 -0400 Subject: [PATCH] Whew --- eclipse_template.xml | 291 ++++++++++++++++++ .../com/google/code/gossip/GossipMember.java | 284 +++++++++-------- .../com/google/code/gossip/GossipRunner.java | 3 - .../com/google/code/gossip/GossipService.java | 30 +- .../google/code/gossip/GossipSettings.java | 16 +- .../code/gossip/GossipTimeoutTimer.java | 19 +- .../google/code/gossip/LocalGossipMember.java | 18 +- .../code/gossip/RemoteGossipMember.java | 6 +- .../google/code/gossip/StartupSettings.java | 80 ++--- .../google/code/gossip/event/GossipState.java | 2 +- .../code/gossip/examples/GossipExample.java | 2 +- .../gossip/manager/ActiveGossipThread.java | 20 +- .../code/gossip/manager/GossipManager.java | 97 +++--- .../gossip/manager/PassiveGossipThread.java | 60 ++-- ...nlyProcessReceivedPassiveGossipThread.java | 73 +++-- .../impl/SendMembersActiveGossipThread.java | 13 +- .../random/RandomActiveGossipThread.java | 8 +- .../manager/random/RandomGossipManager.java | 8 +- 18 files changed, 674 insertions(+), 356 deletions(-) create mode 100644 eclipse_template.xml diff --git a/eclipse_template.xml b/eclipse_template.xml new file mode 100644 index 0000000..3d6e91a --- /dev/null +++ b/eclipse_template.xml @@ -0,0 +1,291 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/com/google/code/gossip/GossipMember.java b/src/main/java/com/google/code/gossip/GossipMember.java index 0160dea..56a5f44 100644 --- a/src/main/java/com/google/code/gossip/GossipMember.java +++ b/src/main/java/com/google/code/gossip/GossipMember.java @@ -24,158 +24,176 @@ import org.json.JSONObject; /** * A abstract class representing a gossip member. - * + * * @author joshclemm, harmenw */ -public abstract class GossipMember implements Comparable{ +public abstract class GossipMember implements Comparable { - public static final String JSON_HOST = "host"; - public static final String JSON_PORT = "port"; - public static final String JSON_HEARTBEAT = "heartbeat"; - public static final String JSON_ID = "id"; - public static final String JSON_CLUSTER = "cluster"; - protected final String _host; - protected final int _port; - protected volatile long _heartbeat; - protected final String _clusterName; - /** - * The purpose of the id field is to be able for nodes to identify themselves beyond there host/port. For example - * an application might generate a persistent id so if they rejoin the cluster at a different host and port we - * are aware it is the same node. - */ - protected String _id; + public static final String JSON_HOST = "host"; - /** - * Constructor. - * @param host The hostname or IP address. - * @param port The port number. - * @param heartbeat The current heartbeat. - * @param id an id that may be replaced after contact - */ - public GossipMember(String clusterName, String host, int port, String id, long heartbeat) { - _clusterName = clusterName; - _host = host; - _port = port; - _id = id; - _heartbeat = heartbeat; - } + public static final String JSON_PORT = "port"; - /** - * Get the name of the cluster the member belongs to. - * - * @return The cluster name - */ - public String getClusterName(){ - return _clusterName; - } + public static final String JSON_HEARTBEAT = "heartbeat"; - /** - * Get the hostname or IP address of the remote gossip member. - * @return The hostname or IP address. - */ - public String getHost() { - return _host; - } + public static final String JSON_ID = "id"; - /** - * Get the port number of the remote gossip member. - * @return The port number. - */ - public int getPort() { - return _port; - } + public static final String JSON_CLUSTER = "cluster"; - /** - * The member address in the form IP/host:port - * Similar to the toString in {@link InetSocketAddress} - */ - public String getAddress() { - return _host+":"+_port; - } + protected final String host; - /** - * Get the heartbeat of this gossip member. - * @return The current heartbeat. - */ - public long getHeartbeat() { - return _heartbeat; - } + protected final int port; - /** - * Set the heartbeat of this gossip member. - * @param heartbeat The new heartbeat. - */ - public void setHeartbeat(long heartbeat) { - this._heartbeat = heartbeat; - } + protected volatile long heartbeat; + protected final String clusterName; - public String getId() { - return _id; + /** + * The purpose of the id field is to be able for nodes to identify themselves beyond there + * host/port. For example an application might generate a persistent id so if they rejoin the + * cluster at a different host and port we are aware it is the same node. + */ + protected String id; + + /** + * Constructor. + * + * @param host + * The hostname or IP address. + * @param port + * The port number. + * @param heartbeat + * The current heartbeat. + * @param id + * an id that may be replaced after contact + */ + public GossipMember(String clusterName, String host, int port, String id, long heartbeat) { + this.clusterName = clusterName; + this.host = host; + this.port = port; + this.id = id; + this.heartbeat = heartbeat; + } + + /** + * Get the name of the cluster the member belongs to. + * + * @return The cluster name + */ + public String getClusterName() { + return clusterName; + } + + /** + * Get the hostname or IP address of the remote gossip member. + * + * @return The hostname or IP address. + */ + public String getHost() { + return host; + } + + /** + * Get the port number of the remote gossip member. + * + * @return The port number. + */ + public int getPort() { + return port; + } + + /** + * The member address in the form IP/host:port Similar to the toString in + * {@link InetSocketAddress} + */ + public String getAddress() { + return host + ":" + port; + } + + /** + * Get the heartbeat of this gossip member. + * + * @return The current heartbeat. + */ + public long getHeartbeat() { + return heartbeat; + } + + /** + * Set the heartbeat of this gossip member. + * + * @param heartbeat + * The new heartbeat. + */ + public void setHeartbeat(long heartbeat) { + this.heartbeat = heartbeat; + } + + public String getId() { + return id; } public void setId(String _id) { - this._id = _id; + this.id = _id; } - public String toString() { - return "Member [address=" + getAddress() + ", id=" + _id + ", heartbeat=" + _heartbeat + "]"; - } + public String toString() { + return "Member [address=" + getAddress() + ", id=" + id + ", heartbeat=" + heartbeat + "]"; + } - /** - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - String address = getAddress(); - result = prime * result - + ((address == null) ? 0 : address.hashCode()) - + _clusterName == null ? 0 : _clusterName.hashCode(); - return result; - } + /** + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + String address = getAddress(); + result = prime * result + ((address == null) ? 0 : address.hashCode()) + clusterName == null ? 0 + : clusterName.hashCode(); + return result; + } - /** - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - System.err.println("equals(): obj is null."); - return false; - } - if (! (obj instanceof GossipMember) ) { - System.err.println("equals(): obj is not of type GossipMember."); - return false; - } - // The object is the same of they both have the same address (hostname and port). - return getAddress().equals(((LocalGossipMember) obj).getAddress()) && - getClusterName().equals(((LocalGossipMember) obj).getClusterName()); - } + /** + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + System.err.println("equals(): obj is null."); + return false; + } + if (!(obj instanceof GossipMember)) { + System.err.println("equals(): obj is not of type GossipMember."); + return false; + } + // The object is the same of they both have the same address (hostname and port). + return getAddress().equals(((LocalGossipMember) obj).getAddress()) + && getClusterName().equals(((LocalGossipMember) obj).getClusterName()); + } - /** - * Get the JSONObject which is the JSON representation of this GossipMember. - * @return The JSONObject of this GossipMember. - */ - public JSONObject toJSONObject() { - try { - JSONObject jsonObject = new JSONObject(); - jsonObject.put(JSON_CLUSTER, _clusterName); - jsonObject.put(JSON_HOST, _host); - jsonObject.put(JSON_PORT, _port); - jsonObject.put(JSON_ID, _id); - jsonObject.put(JSON_HEARTBEAT, _heartbeat); - return jsonObject; - } catch (JSONException e) { - throw new RuntimeException(e); - } - } + /** + * Get the JSONObject which is the JSON representation of this GossipMember. + * + * @return The JSONObject of this GossipMember. + */ + public JSONObject toJSONObject() { + try { + JSONObject jsonObject = new JSONObject(); + jsonObject.put(JSON_CLUSTER, clusterName); + jsonObject.put(JSON_HOST, host); + jsonObject.put(JSON_PORT, port); + jsonObject.put(JSON_ID, id); + jsonObject.put(JSON_HEARTBEAT, heartbeat); + return jsonObject; + } catch (JSONException e) { + throw new RuntimeException(e); + } + } - public int compareTo(GossipMember other){ - return this.getAddress().compareTo(other.getAddress()); - } + public int compareTo(GossipMember other) { + return this.getAddress().compareTo(other.getAddress()); + } } diff --git a/src/main/java/com/google/code/gossip/GossipRunner.java b/src/main/java/com/google/code/gossip/GossipRunner.java index 4694974..7530fd9 100644 --- a/src/main/java/com/google/code/gossip/GossipRunner.java +++ b/src/main/java/com/google/code/gossip/GossipRunner.java @@ -27,18 +27,15 @@ public class GossipRunner { public static void main(String[] args) { File configFile; - if (args.length == 1) { configFile = new File("./" + args[0]); } else { configFile = new File("gossip.conf"); } - new GossipRunner(configFile); } public GossipRunner(File configFile) { - if (configFile != null && configFile.exists()) { try { System.out.println("Parsing the configuration file..."); diff --git a/src/main/java/com/google/code/gossip/GossipService.java b/src/main/java/com/google/code/gossip/GossipService.java index 3d578a4..2226a48 100644 --- a/src/main/java/com/google/code/gossip/GossipService.java +++ b/src/main/java/com/google/code/gossip/GossipService.java @@ -29,57 +29,59 @@ import com.google.code.gossip.manager.random.RandomGossipManager; /** * This object represents the service which is responsible for gossiping with other gossip members. - * + * * @author joshclemm, harmenw */ public class GossipService { public static final Logger LOGGER = Logger.getLogger(GossipService.class); - private GossipManager _gossipManager; + private GossipManager gossipManager; /** * Constructor with the default settings. - * + * * @throws InterruptedException * @throws UnknownHostException */ public GossipService(StartupSettings startupSettings) throws InterruptedException, UnknownHostException { - this(startupSettings.getCluster(), InetAddress.getLocalHost().getHostAddress(), startupSettings.getPort(), startupSettings.getId(), - startupSettings.getGossipMembers(), startupSettings - .getGossipSettings(), null); + this(startupSettings.getCluster(), InetAddress.getLocalHost().getHostAddress(), startupSettings + .getPort(), startupSettings.getId(), startupSettings.getGossipMembers(), + startupSettings.getGossipSettings(), null); } /** * Setup the client's lists, gossiping parameters, and parse the startup config file. - * + * * @throws InterruptedException * @throws UnknownHostException */ public GossipService(String cluster, String ipAddress, int port, String id, List gossipMembers, GossipSettings settings, GossipListener listener) throws InterruptedException, UnknownHostException { - _gossipManager = new RandomGossipManager(cluster, ipAddress, port, id, settings, gossipMembers, listener); + gossipManager = new RandomGossipManager(cluster, ipAddress, port, id, settings, gossipMembers, + listener); } public void start() { - String address = get_gossipManager().getMyself().getHost() + ":" + get_gossipManager().getMyself().getPort(); - LOGGER.debug( "Starting: " + _gossipManager.getName() + " - " + address ); + String address = get_gossipManager().getMyself().getHost() + ":" + + get_gossipManager().getMyself().getPort(); + LOGGER.debug("Starting: " + gossipManager.getName() + " - " + address); - _gossipManager.start(); + gossipManager.start(); } public void shutdown() { - _gossipManager.shutdown(); + gossipManager.shutdown(); } public GossipManager get_gossipManager() { - return _gossipManager; + return gossipManager; } public void set_gossipManager(GossipManager _gossipManager) { - this._gossipManager = _gossipManager; + this.gossipManager = _gossipManager; } } diff --git a/src/main/java/com/google/code/gossip/GossipSettings.java b/src/main/java/com/google/code/gossip/GossipSettings.java index 82558dd..ec9aae1 100644 --- a/src/main/java/com/google/code/gossip/GossipSettings.java +++ b/src/main/java/com/google/code/gossip/GossipSettings.java @@ -25,10 +25,10 @@ package com.google.code.gossip; public class GossipSettings { /** Time between gossip'ing in ms. Default is 1 second. */ - private int _gossipInterval = 1000; + private int gossipInterval = 1000; /** Time between cleanups in ms. Default is 10 seconds. */ - private int _cleanupInterval = 10000; + private int cleanupInterval = 10000; /** * Construct GossipSettings with default settings. @@ -45,8 +45,8 @@ public class GossipSettings { * The cleanup interval in ms. */ public GossipSettings(int gossipInterval, int cleanupInterval) { - _gossipInterval = gossipInterval; - _cleanupInterval = cleanupInterval; + this.gossipInterval = gossipInterval; + this.cleanupInterval = cleanupInterval; } /** @@ -56,7 +56,7 @@ public class GossipSettings { * The gossip interval in ms. */ public void setGossipTimeout(int gossipInterval) { - _gossipInterval = gossipInterval; + this.gossipInterval = gossipInterval; } /** @@ -67,7 +67,7 @@ public class GossipSettings { * The cleanup interval in ms. */ public void setCleanupInterval(int cleanupInterval) { - _cleanupInterval = cleanupInterval; + this.cleanupInterval = cleanupInterval; } /** @@ -76,7 +76,7 @@ public class GossipSettings { * @return The gossip interval in ms. */ public int getGossipInterval() { - return _gossipInterval; + return gossipInterval; } /** @@ -85,6 +85,6 @@ public class GossipSettings { * @return The cleanup interval. */ public int getCleanupInterval() { - return _cleanupInterval; + return cleanupInterval; } } diff --git a/src/main/java/com/google/code/gossip/GossipTimeoutTimer.java b/src/main/java/com/google/code/gossip/GossipTimeoutTimer.java index cd2ee9f..a1bf130 100644 --- a/src/main/java/com/google/code/gossip/GossipTimeoutTimer.java +++ b/src/main/java/com/google/code/gossip/GossipTimeoutTimer.java @@ -26,17 +26,18 @@ import javax.management.timer.Timer; * This object represents a timer for a gossip member. When the timer has elapsed without being * reset in the meantime, it will inform the GossipService about this who in turn will put the * gossip member on the dead list, because it is apparantly not alive anymore. - * + * * @author joshclemm, harmenw */ public class GossipTimeoutTimer extends Timer { - private final long _sleepTime; - private final LocalGossipMember _source; + private final long sleepTime; + + private final LocalGossipMember source; /** * Constructor. Creates a reset-able timer that wakes up after millisecondsSleepTime. - * + * * @param millisecondsSleepTime * The time for this timer to wait before an event. * @param notificationListener @@ -45,8 +46,8 @@ public class GossipTimeoutTimer extends Timer { public GossipTimeoutTimer(long millisecondsSleepTime, NotificationListener notificationListener, LocalGossipMember member) { super(); - _sleepTime = millisecondsSleepTime; - _source = member; + sleepTime = millisecondsSleepTime; + source = member; addNotificationListener(notificationListener, null, null); } @@ -63,15 +64,15 @@ public class GossipTimeoutTimer extends Timer { */ public void reset() { removeAllNotifications(); - setWakeupTime(_sleepTime); + setWakeupTime(sleepTime); } /** * Adds a new wake-up time for this timer. - * + * * @param milliseconds */ private void setWakeupTime(long milliseconds) { - addNotification("type", "message", _source, new Date(System.currentTimeMillis() + milliseconds)); + addNotification("type", "message", source, new Date(System.currentTimeMillis() + milliseconds)); } } diff --git a/src/main/java/com/google/code/gossip/LocalGossipMember.java b/src/main/java/com/google/code/gossip/LocalGossipMember.java index 978e822..216da96 100644 --- a/src/main/java/com/google/code/gossip/LocalGossipMember.java +++ b/src/main/java/com/google/code/gossip/LocalGossipMember.java @@ -22,7 +22,7 @@ import javax.management.NotificationListener; /** * This object represent a gossip member with the properties known locally. These objects are stored * in the local list of gossip member.s - * + * * @author harmenw */ public class LocalGossipMember extends GossipMember { @@ -31,7 +31,7 @@ public class LocalGossipMember extends GossipMember { /** * Constructor. - * + * * @param hostname * The hostname or IP address. * @param port @@ -43,28 +43,28 @@ public class LocalGossipMember extends GossipMember { * @param cleanupTimeout * The cleanup timeout for this gossip member. */ - public LocalGossipMember(String clusterName, String hostname, int port, String id, long heartbeat, - NotificationListener notificationListener, int cleanupTimeout) { + public LocalGossipMember(String clusterName, String hostname, int port, String id, + long heartbeat, NotificationListener notificationListener, int cleanupTimeout) { super(clusterName, hostname, port, id, heartbeat); - this.timeoutTimer = new GossipTimeoutTimer(cleanupTimeout, notificationListener, this); + timeoutTimer = new GossipTimeoutTimer(cleanupTimeout, notificationListener, this); } /** * Start the timeout timer. */ public void startTimeoutTimer() { - this.timeoutTimer.start(); + timeoutTimer.start(); } /** * Reset the timeout timer. */ public void resetTimeoutTimer() { - this.timeoutTimer.reset(); + timeoutTimer.reset(); } - + public void disableTimer() { - this.timeoutTimer.removeAllNotifications(); + timeoutTimer.removeAllNotifications(); } } diff --git a/src/main/java/com/google/code/gossip/RemoteGossipMember.java b/src/main/java/com/google/code/gossip/RemoteGossipMember.java index d3848fd..a7c3a1f 100644 --- a/src/main/java/com/google/code/gossip/RemoteGossipMember.java +++ b/src/main/java/com/google/code/gossip/RemoteGossipMember.java @@ -20,14 +20,14 @@ package com.google.code.gossip; /** * The object represents a gossip member with the properties as received from a remote gossip * member. - * + * * @author harmenw */ public class RemoteGossipMember extends GossipMember { /** * Constructor. - * + * * @param hostname * The hostname or IP address. * @param port @@ -41,7 +41,7 @@ public class RemoteGossipMember extends GossipMember { /** * Construct a RemoteGossipMember with a heartbeat of 0. - * + * * @param hostname * The hostname or IP address. * @param port diff --git a/src/main/java/com/google/code/gossip/StartupSettings.java b/src/main/java/com/google/code/gossip/StartupSettings.java index 5e0a674..6d9d43c 100644 --- a/src/main/java/com/google/code/gossip/StartupSettings.java +++ b/src/main/java/com/google/code/gossip/StartupSettings.java @@ -32,29 +32,29 @@ import org.json.JSONObject; /** * This object represents the settings used when starting the gossip service. - * + * * @author harmenw */ public class StartupSettings { private static final Logger log = Logger.getLogger(StartupSettings.class); /** The id to use fo the service */ - private String _id; + private String id; /** The port to start the gossip service on. */ - private int _port; + private int port; private String cluster; /** The gossip settings used at startup. */ - private final GossipSettings _gossipSettings; + private final GossipSettings gossipSettings; /** The list with gossip members to start with. */ - private final List _gossipMembers; + private final List gossipMembers; /** * Constructor. - * + * * @param id * The id to be used for this service * @param port @@ -68,96 +68,96 @@ public class StartupSettings { /** * Constructor. - * + * * @param id * The id to be used for this service * @param port * The port to start the service on. */ public StartupSettings(String id, int port, GossipSettings gossipSettings) { - _id = id; - _port = port; - _gossipSettings = gossipSettings; - _gossipMembers = new ArrayList<>(); + this.id = id; + this.port = port; + this.gossipSettings = gossipSettings; + gossipMembers = new ArrayList<>(); } - public void setCluster(String cluster){ + public void setCluster(String cluster) { this.cluster = cluster; } - public String getCluster(){ - return this.cluster; + public String getCluster() { + return cluster; } /** * Set the id to be used for this service. - * + * * @param id * The id for this service. */ - public void setId( String id ) { - _id = id; + public void setId(String id) { + this.id = id; } /** * Get the id for this service. - * + * * @return the service's id. */ public String getId() { - return _id; + return id; } /** * Set the port of the gossip service. - * + * * @param port * The port for the gossip service. */ public void setPort(int port) { - _port = port; + this.port = port; } /** * Get the port for the gossip service. - * + * * @return The port of the gossip service. */ public int getPort() { - return _port; + return port; } /** * Get the GossipSettings. - * + * * @return The GossipSettings object. */ public GossipSettings getGossipSettings() { - return _gossipSettings; + return gossipSettings; } /** * Add a gossip member to the list of members to start with. - * + * * @param member * The member to add. */ public void addGossipMember(GossipMember member) { - _gossipMembers.add(member); + gossipMembers.add(member); } /** * Get the list with gossip members. - * + * * @return The gossip members. */ public List getGossipMembers() { - return _gossipMembers; + return gossipMembers; } /** * Parse the settings for the gossip service from a JSON file. - * + * * @param jsonFile * The file object which refers to the JSON config file. * @return The StartupSettings object with the settings from the config file. @@ -171,12 +171,14 @@ public class StartupSettings { public static StartupSettings fromJSONFile(File jsonFile) throws JSONException, FileNotFoundException, IOException { // Read the file to a String. - BufferedReader br = new BufferedReader(new FileReader(jsonFile)); StringBuffer buffer = new StringBuffer(); - String line; - while ((line = br.readLine()) != null) { - buffer.append(line.trim()); + try (BufferedReader br = new BufferedReader(new FileReader(jsonFile)) ){ + String line; + while ((line = br.readLine()) != null) { + buffer.append(line.trim()); + } } + // Lets parse the String as JSON. JSONObject jsonObject = new JSONArray(buffer.toString()).getJSONObject(0); @@ -194,22 +196,22 @@ public class StartupSettings { int cleanupInterval = jsonObject.getInt("cleanup_interval"); // Initiate the settings with the port number. - StartupSettings settings = new StartupSettings(id, port, new GossipSettings( - gossipInterval, cleanupInterval)); + StartupSettings settings = new StartupSettings(id, port, new GossipSettings(gossipInterval, + cleanupInterval)); // Now iterate over the members from the config file and add them to the settings. String configMembersDetails = "Config-members ["; JSONArray membersJSON = jsonObject.getJSONArray("members"); for (int i = 0; i < membersJSON.length(); i++) { JSONObject memberJSON = membersJSON.getJSONObject(i); - RemoteGossipMember member = new RemoteGossipMember(memberJSON.getString("cluster"), memberJSON.getString("host"), - memberJSON.getInt("port"), ""); + RemoteGossipMember member = new RemoteGossipMember(memberJSON.getString("cluster"), + memberJSON.getString("host"), memberJSON.getInt("port"), ""); settings.addGossipMember(member); configMembersDetails += member.getAddress(); if (i < (membersJSON.length() - 1)) configMembersDetails += ", "; } - log.info( configMembersDetails + "]" ); + log.info(configMembersDetails + "]"); // Return the created settings object. return settings; diff --git a/src/main/java/com/google/code/gossip/event/GossipState.java b/src/main/java/com/google/code/gossip/event/GossipState.java index 9b19c66..c0bc565 100644 --- a/src/main/java/com/google/code/gossip/event/GossipState.java +++ b/src/main/java/com/google/code/gossip/event/GossipState.java @@ -21,7 +21,7 @@ public enum GossipState { UP("up"), DOWN("down"); private final String state; - private GossipState(String state){ + private GossipState(String state) { this.state = state; } } diff --git a/src/main/java/com/google/code/gossip/examples/GossipExample.java b/src/main/java/com/google/code/gossip/examples/GossipExample.java index bb5c884..b82bb40 100644 --- a/src/main/java/com/google/code/gossip/examples/GossipExample.java +++ b/src/main/java/com/google/code/gossip/examples/GossipExample.java @@ -30,7 +30,7 @@ import com.google.code.gossip.RemoteGossipMember; /** * This class is an example of how one could use the gossip service. Here we start multiple gossip * clients on this host as specified in the config file. - * + * * @author harmenw */ public class GossipExample extends Thread { diff --git a/src/main/java/com/google/code/gossip/manager/ActiveGossipThread.java b/src/main/java/com/google/code/gossip/manager/ActiveGossipThread.java index 89659a2..9b0bd5c 100644 --- a/src/main/java/com/google/code/gossip/manager/ActiveGossipThread.java +++ b/src/main/java/com/google/code/gossip/manager/ActiveGossipThread.java @@ -31,31 +31,31 @@ import com.google.code.gossip.LocalGossipMember; */ abstract public class ActiveGossipThread implements Runnable { - protected final GossipManager _gossipManager; + protected final GossipManager gossipManager; - private final AtomicBoolean _keepRunning; + private final AtomicBoolean keepRunning; public ActiveGossipThread(GossipManager gossipManager) { - _gossipManager = gossipManager; - _keepRunning = new AtomicBoolean(true); + this.gossipManager = gossipManager; + this.keepRunning = new AtomicBoolean(true); } @Override public void run() { - while (_keepRunning.get()) { + while (keepRunning.get()) { try { - TimeUnit.MILLISECONDS.sleep(_gossipManager.getSettings().getGossipInterval()); - sendMembershipList(_gossipManager.getMyself(), _gossipManager.getMemberList()); + TimeUnit.MILLISECONDS.sleep(gossipManager.getSettings().getGossipInterval()); + sendMembershipList(gossipManager.getMyself(), gossipManager.getMemberList()); } catch (InterruptedException e) { GossipService.LOGGER.error(e); - _keepRunning.set(false); + keepRunning.set(false); } } shutdown(); } public void shutdown() { - _keepRunning.set(false); + keepRunning.set(false); } /** @@ -67,7 +67,7 @@ abstract public class ActiveGossipThread implements Runnable { /** * Abstract method which should be implemented by a subclass. This method should return a member * of the list to gossip with. - * + * * @param memberList * The list of members which are stored in the local list of members. * @return The chosen LocalGossipMember to gossip with. diff --git a/src/main/java/com/google/code/gossip/manager/GossipManager.java b/src/main/java/com/google/code/gossip/manager/GossipManager.java index b695fcc..42354b6 100644 --- a/src/main/java/com/google/code/gossip/manager/GossipManager.java +++ b/src/main/java/com/google/code/gossip/manager/GossipManager.java @@ -43,39 +43,50 @@ import com.google.code.gossip.event.GossipState; public abstract class GossipManager extends Thread implements NotificationListener { public static final Logger LOGGER = Logger.getLogger(GossipManager.class); + public static final int MAX_PACKET_SIZE = 102400; - private final ConcurrentSkipListMap members; - private final LocalGossipMember _me; - private final GossipSettings _settings; - private final AtomicBoolean _gossipServiceRunning; - private final Class _passiveGossipThreadClass; - private final Class _activeGossipThreadClass; + private final ConcurrentSkipListMap members; + + private final LocalGossipMember me; + + private final GossipSettings settings; + + private final AtomicBoolean gossipServiceRunning; + + private final Class passiveGossipThreadClass; + + private final Class activeGossipThreadClass; + private final GossipListener listener; + private ActiveGossipThread activeGossipThread; + private PassiveGossipThread passiveGossipThread; - private ExecutorService _gossipThreadExecutor; + + private ExecutorService gossipThreadExecutor; public GossipManager(Class passiveGossipThreadClass, - Class activeGossipThreadClass, String cluster, String address, int port, - String id, GossipSettings settings, List gossipMembers, - GossipListener listener) { - _passiveGossipThreadClass = passiveGossipThreadClass; - _activeGossipThreadClass = activeGossipThreadClass; - _settings = settings; - _me = new LocalGossipMember(cluster, address, port, id, System.currentTimeMillis(), this, settings.getCleanupInterval()); + Class activeGossipThreadClass, String cluster, + String address, int port, String id, GossipSettings settings, + List gossipMembers, GossipListener listener) { + this.passiveGossipThreadClass = passiveGossipThreadClass; + this.activeGossipThreadClass = activeGossipThreadClass; + this.settings = settings; + me = new LocalGossipMember(cluster, address, port, id, System.currentTimeMillis(), this, + settings.getCleanupInterval()); members = new ConcurrentSkipListMap<>(); for (GossipMember startupMember : gossipMembers) { - if (!startupMember.equals(_me)) { - LocalGossipMember member = new LocalGossipMember(startupMember.getClusterName(), startupMember.getHost(), - startupMember.getPort(), startupMember.getId(), System.currentTimeMillis(), this, - settings.getCleanupInterval()); + if (!startupMember.equals(me)) { + LocalGossipMember member = new LocalGossipMember(startupMember.getClusterName(), + startupMember.getHost(), startupMember.getPort(), startupMember.getId(), + System.currentTimeMillis(), this, settings.getCleanupInterval()); members.put(member, GossipState.UP); GossipService.LOGGER.debug(member); } } - _gossipThreadExecutor = Executors.newCachedThreadPool(); - _gossipServiceRunning = new AtomicBoolean(true); + gossipThreadExecutor = Executors.newCachedThreadPool(); + gossipServiceRunning = new AtomicBoolean(true); this.listener = listener; Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { @@ -98,9 +109,9 @@ public abstract class GossipManager extends Thread implements NotificationListen } } - public void revivieMember(LocalGossipMember m){ - for ( Entry it : this.members.entrySet()){ - if (it.getKey().getId().equals(m.getId())){ + public void revivieMember(LocalGossipMember m) { + for (Entry it : this.members.entrySet()) { + if (it.getKey().getId().equals(m.getId())) { it.getKey().disableTimer(); } } @@ -110,8 +121,8 @@ public abstract class GossipManager extends Thread implements NotificationListen listener.gossipEvent(m, GossipState.UP); } } - - public void createOrRevivieMember(LocalGossipMember m){ + + public void createOrRevivieMember(LocalGossipMember m) { members.put(m, GossipState.UP); if (listener != null) { listener.gossipEvent(m, GossipState.UP); @@ -119,7 +130,7 @@ public abstract class GossipManager extends Thread implements NotificationListen } public GossipSettings getSettings() { - return _settings; + return settings; } /** @@ -128,8 +139,8 @@ public abstract class GossipManager extends Thread implements NotificationListen */ public List getMemberList() { List up = new ArrayList<>(); - for (Entry entry : members.entrySet()){ - if (GossipState.UP.equals(entry.getValue())){ + for (Entry entry : members.entrySet()) { + if (GossipState.UP.equals(entry.getValue())) { up.add(entry.getKey()); } } @@ -137,13 +148,13 @@ public abstract class GossipManager extends Thread implements NotificationListen } public LocalGossipMember getMyself() { - return _me; + return me; } public List getDeadList() { List up = new ArrayList<>(); - for (Entry entry : members.entrySet()){ - if (GossipState.DOWN.equals(entry.getValue())){ + for (Entry entry : members.entrySet()) { + if (GossipState.DOWN.equals(entry.getValue())) { up.add(entry.getKey()); } } @@ -156,23 +167,23 @@ public abstract class GossipManager extends Thread implements NotificationListen */ public void run() { for (LocalGossipMember member : members.keySet()) { - if (member != _me) { + if (member != me) { member.startTimeoutTimer(); } } try { - passiveGossipThread = _passiveGossipThreadClass.getConstructor(GossipManager.class) + passiveGossipThread = passiveGossipThreadClass.getConstructor(GossipManager.class) .newInstance(this); - _gossipThreadExecutor.execute(passiveGossipThread); - activeGossipThread = _activeGossipThreadClass.getConstructor(GossipManager.class) + gossipThreadExecutor.execute(passiveGossipThread); + activeGossipThread = activeGossipThreadClass.getConstructor(GossipManager.class) .newInstance(this); - _gossipThreadExecutor.execute(activeGossipThread); + gossipThreadExecutor.execute(activeGossipThread); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e1) { throw new RuntimeException(e1); } GossipService.LOGGER.debug("The GossipService is started."); - while (_gossipServiceRunning.get()) { + while (gossipServiceRunning.get()) { try { // TODO TimeUnit.MILLISECONDS.sleep(1); @@ -186,17 +197,17 @@ public abstract class GossipManager extends Thread implements NotificationListen * Shutdown the gossip service. */ public void shutdown() { - _gossipServiceRunning.set(false); - _gossipThreadExecutor.shutdown(); - if (passiveGossipThread != null){ + gossipServiceRunning.set(false); + gossipThreadExecutor.shutdown(); + if (passiveGossipThread != null) { passiveGossipThread.shutdown(); } - if (activeGossipThread != null){ + if (activeGossipThread != null) { activeGossipThread.shutdown(); } try { - boolean result = _gossipThreadExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS); - if (!result){ + boolean result = gossipThreadExecutor.awaitTermination(1000, TimeUnit.MILLISECONDS); + if (!result) { LOGGER.error("executor shutdown timed out"); } } catch (InterruptedException e) { diff --git a/src/main/java/com/google/code/gossip/manager/PassiveGossipThread.java b/src/main/java/com/google/code/gossip/manager/PassiveGossipThread.java index 5dcea09..e8eb034 100644 --- a/src/main/java/com/google/code/gossip/manager/PassiveGossipThread.java +++ b/src/main/java/com/google/code/gossip/manager/PassiveGossipThread.java @@ -47,38 +47,38 @@ abstract public class PassiveGossipThread implements Runnable { public static final Logger LOGGER = Logger.getLogger(PassiveGossipThread.class); /** The socket used for the passive thread of the gossip service. */ - private DatagramSocket _server; + private DatagramSocket server; - private final GossipManager _gossipManager; + private final GossipManager gossipManager; - private AtomicBoolean _keepRunning; + private AtomicBoolean keepRunning; - private final String _cluster; + private final String cluster; public PassiveGossipThread(GossipManager gossipManager) { - _gossipManager = gossipManager; + this.gossipManager = gossipManager; try { - SocketAddress socketAddress = new InetSocketAddress(_gossipManager.getMyself().getHost(), - _gossipManager.getMyself().getPort()); - _server = new DatagramSocket(socketAddress); + SocketAddress socketAddress = new InetSocketAddress(gossipManager.getMyself().getHost(), + gossipManager.getMyself().getPort()); + server = new DatagramSocket(socketAddress); GossipService.LOGGER.debug("Gossip service successfully initialized on port " - + _gossipManager.getMyself().getPort()); - GossipService.LOGGER.debug("I am " + _gossipManager.getMyself()); - _cluster = _gossipManager.getMyself().getClusterName(); + + gossipManager.getMyself().getPort()); + GossipService.LOGGER.debug("I am " + gossipManager.getMyself()); + cluster = gossipManager.getMyself().getClusterName(); } catch (SocketException ex) { GossipService.LOGGER.warn(ex); throw new RuntimeException(ex); } - _keepRunning = new AtomicBoolean(true); + keepRunning = new AtomicBoolean(true); } @Override public void run() { - while (_keepRunning.get()) { + while (keepRunning.get()) { try { - byte[] buf = new byte[_server.getReceiveBufferSize()]; + byte[] buf = new byte[server.getReceiveBufferSize()]; DatagramPacket p = new DatagramPacket(buf, buf.length); - _server.receive(p); + server.receive(p); int packet_length = 0; for (int i = 0; i < 4; i++) { int shift = (4 - 1 - i) * 8; @@ -98,7 +98,8 @@ abstract public class PassiveGossipThread implements Runnable { JSONArray jsonArray = new JSONArray(receivedMessage); for (int i = 0; i < jsonArray.length(); i++) { JSONObject memberJSONObject = jsonArray.getJSONObject(i); - if (memberJSONObject.length() == 5 && _cluster.equals(memberJSONObject.get(GossipMember.JSON_CLUSTER))) { + if (memberJSONObject.length() == 5 + && cluster.equals(memberJSONObject.get(GossipMember.JSON_CLUSTER))) { RemoteGossipMember member = new RemoteGossipMember( memberJSONObject.getString(GossipMember.JSON_CLUSTER), memberJSONObject.getString(GossipMember.JSON_HOST), @@ -112,7 +113,7 @@ abstract public class PassiveGossipThread implements Runnable { senderMember = member; } remoteGossipMembers.add(member); - } else if(memberJSONObject.length() == 5) { + } else if (memberJSONObject.length() == 5) { GossipService.LOGGER.warn("The member object does not belong to this cluster."); } else { GossipService.LOGGER @@ -121,7 +122,7 @@ abstract public class PassiveGossipThread implements Runnable { } } - mergeLists(_gossipManager, senderMember, remoteGossipMembers); + mergeLists(gossipManager, senderMember, remoteGossipMembers); } catch (JSONException e) { GossipService.LOGGER .error("The received message is not well-formed JSON. The following message has been dropped:\n" @@ -137,7 +138,7 @@ abstract public class PassiveGossipThread implements Runnable { } catch (IOException e) { GossipService.LOGGER.error(e); System.out.println(e); - _keepRunning.set(false); + keepRunning.set(false); } } shutdown(); @@ -145,13 +146,14 @@ abstract public class PassiveGossipThread implements Runnable { public void shutdown() { try { - _server.close(); - } catch (RuntimeException ex){ } + server.close(); + } catch (RuntimeException ex) { + } } /** * Abstract method for merging the local and remote list. - * + * * @param gossipManager * The GossipManager for retrieving the local members and dead members list. * @param senderMember @@ -165,11 +167,9 @@ abstract public class PassiveGossipThread implements Runnable { } /* - * random comments - * // Check whether the package is smaller than the maximal packet length. - // A package larger than this would not be possible to be send from a GossipService, - // since this is check before sending the message. - // This could normally only occur when the list of members is very big, - // or when the packet is malformed, and the first 4 bytes is not the right in anymore. - // For this reason we regards the message. - * */ + * random comments // Check whether the package is smaller than the maximal packet length. // A + * package larger than this would not be possible to be send from a GossipService, // since this is + * check before sending the message. // This could normally only occur when the list of members is + * very big, // or when the packet is malformed, and the first 4 bytes is not the right in anymore. + * // For this reason we regards the message. + */ diff --git a/src/main/java/com/google/code/gossip/manager/impl/OnlyProcessReceivedPassiveGossipThread.java b/src/main/java/com/google/code/gossip/manager/impl/OnlyProcessReceivedPassiveGossipThread.java index 16584f9..08d573a 100644 --- a/src/main/java/com/google/code/gossip/manager/impl/OnlyProcessReceivedPassiveGossipThread.java +++ b/src/main/java/com/google/code/gossip/manager/impl/OnlyProcessReceivedPassiveGossipThread.java @@ -36,7 +36,7 @@ public class OnlyProcessReceivedPassiveGossipThread extends PassiveGossipThread * Merge remote list (received from peer), and our local member list. Simply, we must update the * heartbeats that the remote list has with our list. Also, some additional logic is needed to * make sure we have not timed out a member and then immediately received a list with that member. - * + * * @param gossipManager * @param senderMember * @param remoteList @@ -44,13 +44,14 @@ public class OnlyProcessReceivedPassiveGossipThread extends PassiveGossipThread protected void mergeLists(GossipManager gossipManager, RemoteGossipMember senderMember, List remoteList) { - //if the person sending to us is in the dead list consider them up - for (LocalGossipMember i : gossipManager.getDeadList()){ - if (i.getId().equals(senderMember.getId())){ - System.out.println(gossipManager.getMyself() +" caught a live one!"); - LocalGossipMember newLocalMember = new LocalGossipMember(senderMember.getClusterName(), senderMember.getHost(), - senderMember.getPort(), senderMember.getId(), senderMember.getHeartbeat(), - gossipManager, gossipManager.getSettings().getCleanupInterval()); + // if the person sending to us is in the dead list consider them up + for (LocalGossipMember i : gossipManager.getDeadList()) { + if (i.getId().equals(senderMember.getId())) { + System.out.println(gossipManager.getMyself() + " caught a live one!"); + LocalGossipMember newLocalMember = new LocalGossipMember(senderMember.getClusterName(), + senderMember.getHost(), senderMember.getPort(), senderMember.getId(), + senderMember.getHeartbeat(), gossipManager, gossipManager.getSettings() + .getCleanupInterval()); gossipManager.revivieMember(newLocalMember); newLocalMember.startTimeoutTimer(); } @@ -58,7 +59,7 @@ public class OnlyProcessReceivedPassiveGossipThread extends PassiveGossipThread for (GossipMember remoteMember : remoteList) { if (remoteMember.getId().equals(gossipManager.getMyself().getId())) { continue; - } + } if (gossipManager.getMemberList().contains(remoteMember)) { LocalGossipMember localMember = gossipManager.getMemberList().get( gossipManager.getMemberList().indexOf(remoteMember)); @@ -66,11 +67,12 @@ public class OnlyProcessReceivedPassiveGossipThread extends PassiveGossipThread localMember.setHeartbeat(remoteMember.getHeartbeat()); localMember.resetTimeoutTimer(); } - } else if (!gossipManager.getMemberList().contains(remoteMember) - && !gossipManager.getDeadList().contains(remoteMember) ){ - LocalGossipMember newLocalMember = new LocalGossipMember(remoteMember.getClusterName(), remoteMember.getHost(), - remoteMember.getPort(), remoteMember.getId(), remoteMember.getHeartbeat(), - gossipManager, gossipManager.getSettings().getCleanupInterval()); + } else if (!gossipManager.getMemberList().contains(remoteMember) + && !gossipManager.getDeadList().contains(remoteMember)) { + LocalGossipMember newLocalMember = new LocalGossipMember(remoteMember.getClusterName(), + remoteMember.getHost(), remoteMember.getPort(), remoteMember.getId(), + remoteMember.getHeartbeat(), gossipManager, gossipManager.getSettings() + .getCleanupInterval()); gossipManager.createOrRevivieMember(newLocalMember); newLocalMember.startTimeoutTimer(); } else { @@ -78,9 +80,10 @@ public class OnlyProcessReceivedPassiveGossipThread extends PassiveGossipThread LocalGossipMember localDeadMember = gossipManager.getDeadList().get( gossipManager.getDeadList().indexOf(remoteMember)); if (remoteMember.getHeartbeat() > localDeadMember.getHeartbeat()) { - LocalGossipMember newLocalMember = new LocalGossipMember(remoteMember.getClusterName(), remoteMember.getHost(), - remoteMember.getPort(), remoteMember.getId(), remoteMember.getHeartbeat(), - gossipManager, gossipManager.getSettings().getCleanupInterval()); + LocalGossipMember newLocalMember = new LocalGossipMember(remoteMember.getClusterName(), + remoteMember.getHost(), remoteMember.getPort(), remoteMember.getId(), + remoteMember.getHeartbeat(), gossipManager, gossipManager.getSettings() + .getCleanupInterval()); gossipManager.revivieMember(newLocalMember); newLocalMember.startTimeoutTimer(); GossipService.LOGGER.debug("Removed remote member " + remoteMember.getAddress() @@ -98,7 +101,7 @@ public class OnlyProcessReceivedPassiveGossipThread extends PassiveGossipThread GossipService.LOGGER.debug("remote " + remoteList); GossipService.LOGGER.debug("live " + gossipManager.getMemberList()); GossipService.LOGGER.debug("dead " + gossipManager.getDeadList()); - //throw new IllegalArgumentException("wtf"); + // throw new IllegalArgumentException("wtf"); } } } @@ -107,25 +110,19 @@ public class OnlyProcessReceivedPassiveGossipThread extends PassiveGossipThread } /** -old comment section: -// If a member is restarted the heartbeat will restart from 1, so we should check -// that here. -// So a member can become from the dead when it is either larger than a previous -// heartbeat (due to network failure) -// or when the heartbeat is 1 (after a restart of the service). -// TODO: What if the first message of a gossip service is sent to a dead node? The -// second member will receive a heartbeat of two. -// TODO: The above does happen. Maybe a special message for a revived member? -// TODO: Or maybe when a member is declared dead for more than -// _settings.getCleanupInterval() ms, reset the heartbeat to 0. -// It will then accept a revived member. -// The above is now handle by checking whether the heartbeat differs -// _settings.getCleanupInterval(), it must be restarted. -*/ + * old comment section: // If a member is restarted the heartbeat will restart from 1, so we should + * check // that here. // So a member can become from the dead when it is either larger than a + * previous // heartbeat (due to network failure) // or when the heartbeat is 1 (after a restart of + * the service). // TODO: What if the first message of a gossip service is sent to a dead node? The + * // second member will receive a heartbeat of two. // TODO: The above does happen. Maybe a special + * message for a revived member? // TODO: Or maybe when a member is declared dead for more than // + * _settings.getCleanupInterval() ms, reset the heartbeat to 0. // It will then accept a revived + * member. // The above is now handle by checking whether the heartbeat differs // + * _settings.getCleanupInterval(), it must be restarted. + */ /* -// The remote member is back from the dead. -// Remove it from the dead list. -// gossipManager.getDeadList().remove(localDeadMember); -// Add it as a new member and add it to the member list. -*/ \ No newline at end of file + * // The remote member is back from the dead. // Remove it from the dead list. // + * gossipManager.getDeadList().remove(localDeadMember); // Add it as a new member and add it to the + * member list. + */ \ No newline at end of file diff --git a/src/main/java/com/google/code/gossip/manager/impl/SendMembersActiveGossipThread.java b/src/main/java/com/google/code/gossip/manager/impl/SendMembersActiveGossipThread.java index ea3d0e0..4e5f855 100644 --- a/src/main/java/com/google/code/gossip/manager/impl/SendMembersActiveGossipThread.java +++ b/src/main/java/com/google/code/gossip/manager/impl/SendMembersActiveGossipThread.java @@ -47,8 +47,8 @@ abstract public class SendMembersActiveGossipThread extends ActiveGossipThread { if (member == null) { return; } - try (DatagramSocket socket = new DatagramSocket()){ - socket.setSoTimeout(_gossipManager.getSettings().getGossipInterval()); + try (DatagramSocket socket = new DatagramSocket()) { + socket.setSoTimeout(gossipManager.getSettings().getGossipInterval()); InetAddress dest = InetAddress.getByName(member.getHost()); JSONArray jsonArray = new JSONArray(); jsonArray.put(me.toJSONObject()); @@ -60,8 +60,7 @@ abstract public class SendMembersActiveGossipThread extends ActiveGossipThread { int packet_length = json_bytes.length; if (packet_length < GossipManager.MAX_PACKET_SIZE) { byte[] buf = createBuffer(packet_length, json_bytes); - DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length, dest, - member.getPort()); + DatagramPacket datagramPacket = new DatagramPacket(buf, buf.length, dest, member.getPort()); socket.send(datagramPacket); } else { GossipService.LOGGER.error("The length of the to be send message is too large (" @@ -71,8 +70,8 @@ abstract public class SendMembersActiveGossipThread extends ActiveGossipThread { GossipService.LOGGER.warn(e1); } } - - private byte[] createBuffer(int packetLength, byte [] jsonBytes){ + + private byte[] createBuffer(int packetLength, byte[] jsonBytes) { byte[] lengthBytes = new byte[4]; lengthBytes[0] = (byte) (packetLength >> 24); lengthBytes[1] = (byte) ((packetLength << 8) >> 24); @@ -84,5 +83,5 @@ abstract public class SendMembersActiveGossipThread extends ActiveGossipThread { byte[] buf = byteBuffer.array(); return buf; } - + } diff --git a/src/main/java/com/google/code/gossip/manager/random/RandomActiveGossipThread.java b/src/main/java/com/google/code/gossip/manager/random/RandomActiveGossipThread.java index 558a9e4..914f5ca 100644 --- a/src/main/java/com/google/code/gossip/manager/random/RandomActiveGossipThread.java +++ b/src/main/java/com/google/code/gossip/manager/random/RandomActiveGossipThread.java @@ -28,23 +28,23 @@ import com.google.code.gossip.manager.impl.SendMembersActiveGossipThread; public class RandomActiveGossipThread extends SendMembersActiveGossipThread { /** The Random used for choosing a member to gossip with. */ - private final Random _random; + private final Random random; public RandomActiveGossipThread(GossipManager gossipManager) { super(gossipManager); - _random = new Random(); + random = new Random(); } /** * [The selectToSend() function.] Find a random peer from the local membership list. In the case * where this client is the only member in the list, this method will return null. - * + * * @return Member random member if list is greater than 1, null otherwise */ protected LocalGossipMember selectPartner(List memberList) { LocalGossipMember member = null; if (memberList.size() > 0) { - int randomNeighborIndex = _random.nextInt(memberList.size()); + int randomNeighborIndex = random.nextInt(memberList.size()); member = memberList.get(randomNeighborIndex); } else { GossipService.LOGGER.debug("I am alone in this world."); diff --git a/src/main/java/com/google/code/gossip/manager/random/RandomGossipManager.java b/src/main/java/com/google/code/gossip/manager/random/RandomGossipManager.java index e256ec2..c1e69d6 100644 --- a/src/main/java/com/google/code/gossip/manager/random/RandomGossipManager.java +++ b/src/main/java/com/google/code/gossip/manager/random/RandomGossipManager.java @@ -26,9 +26,9 @@ import com.google.code.gossip.manager.impl.OnlyProcessReceivedPassiveGossipThrea import java.util.List; public class RandomGossipManager extends GossipManager { - public RandomGossipManager(String cluster, String address, int port, String id, GossipSettings settings, - List gossipMembers, GossipListener listener) { - super(OnlyProcessReceivedPassiveGossipThread.class, RandomActiveGossipThread.class, cluster, address, - port, id, settings, gossipMembers, listener); + public RandomGossipManager(String cluster, String address, int port, String id, + GossipSettings settings, List gossipMembers, GossipListener listener) { + super(OnlyProcessReceivedPassiveGossipThread.class, RandomActiveGossipThread.class, cluster, + address, port, id, settings, gossipMembers, listener); } }