Skip to content

Commit

Permalink
Combined node and status in a composite map in AbstractFailureDetector
Browse files Browse the repository at this point in the history
  • Loading branch information
Chinmay Soman committed Mar 23, 2012
1 parent 78ee7c1 commit d2fae73
Showing 1 changed file with 40 additions and 19 deletions.
Expand Up @@ -43,10 +43,9 @@ public abstract class AbstractFailureDetector implements FailureDetector {
// simply a wrapper around a ConcurrentHashMap anyway :(
protected final ConcurrentHashMap<FailureDetectorListener, Object> listeners;

protected final Map<Node, NodeStatus> nodeStatusMap;

// Also maintain the list of nodes by IDs (in order to handle host swaps)
protected final Map<Integer, Node> nodeMap;
// Maintain the list of nodes and their status by IDs (in order to handle
// host swaps)
protected final Map<Integer, CompositeNodeStatus> idNodeStatusMap;

protected final Logger logger = Logger.getLogger(getClass().getName());

Expand All @@ -56,13 +55,13 @@ protected AbstractFailureDetector(FailureDetectorConfig failureDetectorConfig) {

this.failureDetectorConfig = failureDetectorConfig;
listeners = new ConcurrentHashMap<FailureDetectorListener, Object>();
nodeStatusMap = new ConcurrentHashMap<Node, NodeStatus>();
nodeMap = new ConcurrentHashMap<Integer, Node>();
idNodeStatusMap = new ConcurrentHashMap<Integer, CompositeNodeStatus>();

for(Node node: failureDetectorConfig.getNodes()) {
nodeStatusMap.put(node, createNodeStatus(failureDetectorConfig.getTime()
.getMilliseconds()));
nodeMap.put(node.getId(), node);
idNodeStatusMap.put(node.getId(),
new CompositeNodeStatus(node,
createNodeStatus(failureDetectorConfig.getTime()
.getMilliseconds())));
}
}

Expand Down Expand Up @@ -216,29 +215,28 @@ protected void setUnavailable(Node node, UnreachableStoreException e) {
}

protected NodeStatus getNodeStatus(Node node) {
NodeStatus nodeStatus = nodeStatusMap.get(node);
Node currentTrackedNode = nodeMap.get(node.getId());
NodeStatus nodeStatus = null;
CompositeNodeStatus currentNodeStatus = idNodeStatusMap.get(node.getId());

if(nodeStatus == null || !currentTrackedNode.isEqualState(node)) {
if(currentNodeStatus == null || !currentNodeStatus.getNode().isEqualState(node)) {
if(logger.isEnabledFor(Level.WARN))
logger.warn("creating new node status for node " + node.getId()
+ " for failure detector");

// If the host is being replaced, remove old tracking information
if(nodeStatus != null) {
nodeStatusMap.remove(currentTrackedNode);
nodeMap.remove(currentTrackedNode);
failureDetectorConfig.removeNode(currentTrackedNode);
if(currentNodeStatus != null) {
idNodeStatusMap.remove(currentNodeStatus);
failureDetectorConfig.removeNode(currentNodeStatus.getNode());
}

nodeStatus = createNodeStatus(failureDetectorConfig.getTime().getMilliseconds());
nodeStatusMap.put(node, nodeStatus);
nodeMap.put(node.getId(), node);
idNodeStatusMap.put(node.getId(), new CompositeNodeStatus(node, nodeStatus));

if(!failureDetectorConfig.getNodes().contains(node)) {
failureDetectorConfig.addNode(node);
}
}
} else
nodeStatus = currentNodeStatus.getStatus();

return nodeStatus;
}
Expand Down Expand Up @@ -276,4 +274,27 @@ private boolean setAvailable(NodeStatus nodeStatus, boolean isAvailable) {
}
}

private class CompositeNodeStatus {

private Node node;
private NodeStatus status;

CompositeNodeStatus(Node node, NodeStatus status) {
this.node = node;
this.status = status;
}

public void setValues(Node node, NodeStatus status) {
this.node = node;
this.status = status;
}

public Node getNode() {
return this.node;
}

public NodeStatus getStatus() {
return this.status;
}
}
}

0 comments on commit d2fae73

Please sign in to comment.