@ -19,9 +19,6 @@ package com.google.code.gossip;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
/**
|
||||
* A abstract class representing a gossip member.
|
||||
*
|
||||
@ -29,15 +26,6 @@ import org.json.JSONObject;
|
||||
*/
|
||||
public abstract class GossipMember implements Comparable<GossipMember> {
|
||||
|
||||
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;
|
||||
|
||||
@ -174,25 +162,6 @@ public abstract class GossipMember implements Comparable<GossipMember> {
|
||||
&& 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);
|
||||
}
|
||||
}
|
||||
|
||||
public int compareTo(GossipMember other) {
|
||||
return this.getAddress().compareTo(other.getAddress());
|
||||
}
|
||||
|
@ -19,6 +19,7 @@ package com.google.code.gossip.event;
|
||||
|
||||
public enum GossipState {
|
||||
UP("up"), DOWN("down");
|
||||
@SuppressWarnings("unused")
|
||||
private final String state;
|
||||
|
||||
private GossipState(String state) {
|
||||
|
@ -28,13 +28,11 @@ import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import com.google.code.gossip.GossipMember;
|
||||
import com.google.code.gossip.GossipService;
|
||||
import com.google.code.gossip.RemoteGossipMember;
|
||||
import com.google.code.gossip.model.ActiveGossipMessage;
|
||||
|
||||
/**
|
||||
* [The passive thread: reply to incoming gossip request.] This class handles the passive cycle,
|
||||
@ -47,14 +45,16 @@ 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 final DatagramSocket server;
|
||||
|
||||
private final GossipManager gossipManager;
|
||||
|
||||
private AtomicBoolean keepRunning;
|
||||
private final AtomicBoolean keepRunning;
|
||||
|
||||
private final String cluster;
|
||||
|
||||
private final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
public PassiveGossipThread(GossipManager gossipManager) {
|
||||
this.gossipManager = gossipManager;
|
||||
try {
|
||||
@ -92,47 +92,38 @@ abstract public class PassiveGossipThread implements Runnable {
|
||||
for (int i = 0; i < packet_length; i++) {
|
||||
json_bytes[i] = buf[i + 4];
|
||||
}
|
||||
if (GossipService.LOGGER.isDebugEnabled()){
|
||||
String receivedMessage = new String(json_bytes);
|
||||
GossipService.LOGGER.debug("Received message (" + packet_length + " bytes): "
|
||||
+ receivedMessage);
|
||||
}
|
||||
try {
|
||||
List<GossipMember> remoteGossipMembers = new ArrayList<>();
|
||||
RemoteGossipMember senderMember = null;
|
||||
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))) {
|
||||
ActiveGossipMessage activeGossipMessage = MAPPER.readValue(json_bytes,
|
||||
ActiveGossipMessage.class);
|
||||
for (int i = 0; i < activeGossipMessage.getMembers().size(); i++) {
|
||||
RemoteGossipMember member = new RemoteGossipMember(
|
||||
memberJSONObject.getString(GossipMember.JSON_CLUSTER),
|
||||
memberJSONObject.getString(GossipMember.JSON_HOST),
|
||||
memberJSONObject.getInt(GossipMember.JSON_PORT),
|
||||
memberJSONObject.getString(GossipMember.JSON_ID),
|
||||
memberJSONObject.getLong(GossipMember.JSON_HEARTBEAT));
|
||||
GossipService.LOGGER.debug(member.toString());
|
||||
activeGossipMessage.getMembers().get(i).getCluster(),
|
||||
activeGossipMessage.getMembers().get(i).getHost(),
|
||||
activeGossipMessage.getMembers().get(i).getPort(),
|
||||
activeGossipMessage.getMembers().get(i).getId(),
|
||||
activeGossipMessage.getMembers().get(i).getHeartbeat());
|
||||
if (!(member.getClusterName().equals(cluster))){
|
||||
GossipService.LOGGER.warn("Note a member of this cluster " + i);
|
||||
continue;
|
||||
}
|
||||
// This is the first member found, so this should be the member who is communicating
|
||||
// with me.
|
||||
if (i == 0) {
|
||||
senderMember = member;
|
||||
}
|
||||
remoteGossipMembers.add(member);
|
||||
} else if (memberJSONObject.length() == 5) {
|
||||
GossipService.LOGGER.warn("The member object does not belong to this cluster.");
|
||||
} else {
|
||||
GossipService.LOGGER
|
||||
.error("The received member object does not contain 5 objects:\n"
|
||||
+ memberJSONObject.toString());
|
||||
}
|
||||
|
||||
}
|
||||
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"
|
||||
+ receivedMessage);
|
||||
System.out.println(e);
|
||||
} catch (RuntimeException ex) {
|
||||
GossipService.LOGGER.error("Unable to process message", ex);
|
||||
}
|
||||
|
||||
} else {
|
||||
GossipService.LOGGER
|
||||
.error("The received message is not of the expected size, it has been dropped.");
|
||||
|
@ -24,19 +24,33 @@ import java.net.InetAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.List;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
|
||||
import com.google.code.gossip.GossipService;
|
||||
import com.google.code.gossip.LocalGossipMember;
|
||||
import com.google.code.gossip.manager.ActiveGossipThread;
|
||||
import com.google.code.gossip.manager.GossipManager;
|
||||
import com.google.code.gossip.model.ActiveGossipMessage;
|
||||
import com.google.code.gossip.model.GossipMember;
|
||||
|
||||
abstract public class SendMembersActiveGossipThread extends ActiveGossipThread {
|
||||
|
||||
protected ObjectMapper om = new ObjectMapper();
|
||||
|
||||
public SendMembersActiveGossipThread(GossipManager gossipManager) {
|
||||
super(gossipManager);
|
||||
}
|
||||
|
||||
private GossipMember convert(LocalGossipMember member){
|
||||
GossipMember gm = new GossipMember();
|
||||
gm.setCluster(member.getClusterName());
|
||||
gm.setHeartbeat(member.getHeartbeat());
|
||||
gm.setHost(member.getHost());
|
||||
gm.setId(member.getId());
|
||||
gm.setPort(member.getPort());
|
||||
return gm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the sending of the membership list, after we have incremented our own heartbeat.
|
||||
*/
|
||||
@ -50,13 +64,12 @@ abstract public class SendMembersActiveGossipThread extends ActiveGossipThread {
|
||||
try (DatagramSocket socket = new DatagramSocket()) {
|
||||
socket.setSoTimeout(gossipManager.getSettings().getGossipInterval());
|
||||
InetAddress dest = InetAddress.getByName(member.getHost());
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
jsonArray.put(me.toJSONObject());
|
||||
ActiveGossipMessage message = new ActiveGossipMessage();
|
||||
message.getMembers().add(convert(me));
|
||||
for (LocalGossipMember other : memberList) {
|
||||
jsonArray.put(other.toJSONObject());
|
||||
GossipService.LOGGER.debug(other);
|
||||
message.getMembers().add(convert(other));
|
||||
}
|
||||
byte[] json_bytes = jsonArray.toString().getBytes();
|
||||
byte[] json_bytes = om.writeValueAsString(message).getBytes();
|
||||
int packet_length = json_bytes.length;
|
||||
if (packet_length < GossipManager.MAX_PACKET_SIZE) {
|
||||
byte[] buf = createBuffer(packet_length, json_bytes);
|
||||
|
@ -0,0 +1,22 @@
|
||||
package com.google.code.gossip.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ActiveGossipMessage {
|
||||
|
||||
private List<GossipMember> members = new ArrayList<>();
|
||||
|
||||
public ActiveGossipMessage(){
|
||||
|
||||
}
|
||||
|
||||
public List<GossipMember> getMembers() {
|
||||
return members;
|
||||
}
|
||||
|
||||
public void setMembers(List<GossipMember> members) {
|
||||
this.members = members;
|
||||
}
|
||||
|
||||
}
|
63
src/main/java/com/google/code/gossip/model/GossipMember.java
Normal file
63
src/main/java/com/google/code/gossip/model/GossipMember.java
Normal file
@ -0,0 +1,63 @@
|
||||
package com.google.code.gossip.model;
|
||||
|
||||
public class GossipMember {
|
||||
|
||||
private String cluster;
|
||||
private String host;
|
||||
private Integer port;
|
||||
private String id;
|
||||
private Long heartbeat;
|
||||
|
||||
public GossipMember(){
|
||||
|
||||
}
|
||||
|
||||
public GossipMember(String cluster, String host, Integer port, String id, Long heartbeat){
|
||||
this.cluster=cluster;
|
||||
this.host= host;
|
||||
this.port = port;
|
||||
this.id = id;
|
||||
|
||||
}
|
||||
|
||||
public String getCluster() {
|
||||
return cluster;
|
||||
}
|
||||
|
||||
public void setCluster(String cluster) {
|
||||
this.cluster = cluster;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public Integer getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(Integer port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getHeartbeat() {
|
||||
return heartbeat;
|
||||
}
|
||||
|
||||
public void setHeartbeat(Long heartbeat) {
|
||||
this.heartbeat = heartbeat;
|
||||
}
|
||||
|
||||
}
|
@ -35,9 +35,6 @@ import java.util.UUID;
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests support of using {@code StartupSettings} and thereby reading
|
||||
* setup config from file.
|
||||
|
@ -28,7 +28,6 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.code.gossip.GossipMember;
|
||||
|
Reference in New Issue
Block a user