@Override public boolean equals(Object o) { if (!(o instanceof Node)) return false; Node n = (Node) o; return (type.equals(n.type) && index == n.index); }
public Node(String serialized) { int dot = serialized.lastIndexOf('.'); if (dot < 0) throw new IllegalArgumentException("Not a legal node string '" + serialized + "'."); type = NodeType.get(serialized.substring(0, dot)); index = Integer.valueOf(serialized.substring(dot + 1)); }
@Override public int hashCode() { return type.hashCode() ^ index; }
/** * Take the distributor nodes as an example. Let X be the highest index of * the distributor nodes added through setNodeState(). Let Y be the number * of suffix nodes for which the state is down and without description. * E.g. if node X is down and without description, but nodex X-1 is up, then Y is 1. * The node count for distributors is then X + 1 - Y. */ public int getNodeCount(NodeType type) { return nodeCount.get(type.ordinal()); }
private void removeLastNodesDownWithoutReason() { for (NodeType nodeType : NodeType.values()) { for (int index = nodeCount.get(nodeType.ordinal()) - 1; index >= 0; --index) { Node node = new Node(nodeType, index); NodeState nodeState = nodeStates.get(node); if (nodeState == null) break; // Node not existing is up if ( ! nodeState.getState().equals(State.DOWN)) break; // Node not down can not be removed if (nodeState.hasDescription()) break; // Node have reason to be down. Don't remove node as we will forget reason nodeStates.remove(node); nodeCount.set(nodeType.ordinal(), node.getIndex()); } } }
public String toString() { return type.toString() + "." + index; }
public static NodeType get(String serialized) { for(NodeType type : values()) { if (type.serializeAs.equals(serialized)) return type; } throw new IllegalArgumentException("Unknown node type '" + serialized + "'. Legal values are 'storage' and 'distributor'."); }
public Diff getDiff(ClusterState other) { Diff diff = new Diff(); if (version != other.version) { diff.add(new Diff.Entry("version", version, other.version)); } if (!state.equals(other.state)) { diff.add(new Diff.Entry("cluster", state, other.state)); } if (distributionBits != other.distributionBits) { diff.add(new Diff.Entry("bits", distributionBits, other.distributionBits)); } if (official != other.official) { diff.add(new Diff.Entry("official", official, other.official)); } for (NodeType type : NodeType.getTypes()) { Diff typeDiff = new Diff(); int maxCount = Math.max(getNodeCount(type), other.getNodeCount(type)); for (int i = 0; i < maxCount; i++) { Node n = new Node(type, i); Diff d = getNodeState(n).getDiff(other.getNodeState(n)); if (d.differs()) { typeDiff.add(new Diff.Entry(i, d)); } } if (typeDiff.differs()) { diff.add(new Diff.Entry(type, typeDiff).splitLine()); } } return diff; }
public boolean validWantedNodeState(NodeType type) { return validWantedNodeState.get(type.ordinal()); } public boolean validCurrentNodeState(NodeType type) { return validCurrentNodeState.get(type.ordinal()); }
public boolean validReportedNodeState(NodeType type) { return validReportedNodeState.get(type.ordinal()); } public boolean validWantedNodeState(NodeType type) { return validWantedNodeState.get(type.ordinal()); }
private int getOrdering() { return (type.equals(NodeType.STORAGE) ? 65536 : 0) + index; }
public boolean validCurrentNodeState(NodeType type) { return validCurrentNodeState.get(type.ordinal()); }
public void verifyValidInSystemState(NodeType type) { if (!state.validCurrentNodeState(type)) { throw new IllegalArgumentException("State " + state + " cannot fit in system state for node of type: " + type); } if (type.equals(NodeType.DISTRIBUTOR) && Math.abs(capacity - 1.0) > 0.000000001) { throw new IllegalArgumentException("Capacity should not be set for a distributor node"); } if (type.equals(NodeType.DISTRIBUTOR) && Math.abs(reliability - 1.0) > 0.000000001) { throw new IllegalArgumentException("Reliability should not be set for a distributor node"); } if (type.equals(NodeType.DISTRIBUTOR) && !diskStates.isEmpty()) { throw new IllegalArgumentException("Disk states should not be set for a distributor node"); } }
private State(String serialized, boolean validDisk, boolean validDistReported, boolean validStorReported, boolean validDistWanted, boolean validStorWanted, boolean validCluster, boolean validDistCurrent, boolean validStorCurrent) { validDiskState = validDisk; validClusterState = validCluster; assert(NodeType.STORAGE.ordinal() == 0); assert(NodeType.DISTRIBUTOR.ordinal() == 1); validReportedNodeState.add(validStorReported); validReportedNodeState.add(validDistReported); validWantedNodeState.add(validStorWanted); validWantedNodeState.add(validDistWanted); validCurrentNodeState.add(validStorCurrent); validCurrentNodeState.add(validDistCurrent); this.serializedAs = serialized; }
&& type.equals(NodeType.STORAGE) && (initProgress < getListingBucketsInitProgressLimit() ^ other.initProgress < getListingBucketsInitProgressLimit()))
public void addNodeState() throws ParseException { if (!empty) { NodeState ns = NodeState.deserialize(node.getType(), sb.toString()); if (!ns.equals(defaultUpNodeState(node.getType()))) { nodeStates.put(node, ns); } if (nodeCount.get(node.getType().ordinal()) <= node.getIndex()) { nodeCount.set(node.getType().ordinal(), node.getIndex() + 1); } } empty = true; sb = new StringBuilder(); } }
if (type.equals(NodeType.STORAGE)) { if (initProgress < getListingBucketsInitProgressLimit()) { sb.append(compact ? " (ls)" : " (listing files)");
/** * Returns the state of a node. * If the node is not known this returns a node in the state UP (never null) if it has lower index than the max * and DOWN otherwise. */ public NodeState getNodeState(Node node) { if (node.getIndex() >= nodeCount.get(node.getType().ordinal())) return new NodeState(node.getType(), State.DOWN); return nodeStates.getOrDefault(node, new NodeState(node.getType(), State.UP)); }
sb.append(" distributor:").append(distributorNodeCount); for (Map.Entry<Node, NodeState> entry : nodeStates.entrySet()) { if (entry.getKey().getType().equals(NodeType.DISTRIBUTOR) && entry.getKey().getIndex() < distributorNodeCount) { String nodeState = entry.getValue().serialize(entry.getKey().getIndex(), verbose); if (!nodeState.isEmpty()) { sb.append(" storage:").append(storageNodeCount); for (Map.Entry<Node, NodeState> entry : nodeStates.entrySet()) { if (entry.getKey().getType().equals(NodeType.STORAGE) && entry.getKey().getIndex() < storageNodeCount) { String nodeState = entry.getValue().serialize(entry.getKey().getIndex(), verbose); if (!nodeState.isEmpty()) {
throw new ParseException("Illegal node count '" + value + "' in state: " + serialized, 0); if (nodeCount > this.nodeCount.get(nodeType.ordinal())) { this.nodeCount.set(nodeType.ordinal(), nodeCount); node = new Node(nodeType, Integer.valueOf(key.substring(dot + 1, dot2))); if (node.getIndex() >= this.nodeCount.get(nodeType.ordinal())) { throw new ParseException("Cannot index " + nodeType + " node " + node.getIndex() + " of " + this.nodeCount.get(nodeType.ordinal()) + " in state: " + serialized, 0);