/** * Helper method to build local {@link NodeState} based on pings * * @param allServers all servers in the cluster * @param allConnectedNodes all connected nodes in the cluster * @param epoch current epoch * @param sequencerMetrics metrics * @return local node state */ private NodeState getLocalNodeState(Set<String> allServers, Set<String> allConnectedNodes, long epoch, SequencerMetrics sequencerMetrics) { //Get connectivity status for a local node Map<String, ConnectionStatus> connectivity = new HashMap<>(); allServers.forEach(server -> { connectivity.put(server, ConnectionStatus.fromBool(allConnectedNodes.contains(server))); }); NodeConnectivity localConnectivity = NodeConnectivity.connectivity( localEndpoint, ImmutableMap.copyOf(connectivity) ); return NodeState.builder() .connectivity(localConnectivity) .heartbeat(new HeartbeatTimestamp(epoch, heartbeatCounter.incrementHeartbeat())) .sequencerMetrics(sequencerMetrics) .build(); }
private NodeState nodeState(String endpoint, ConnectionStatus... connectionStates) { Map<String, ConnectionStatus> connectivity = new HashMap<>(); for (int i = 0; i < connectionStates.length; i++) { connectivity.put(NODE_NAMES.get(i), connectionStates[i]); } NodeConnectivity nodeConnectivity = NodeConnectivity.builder() .endpoint(endpoint) .type(NodeConnectivityType.CONNECTED) .connectivity(ImmutableMap.copyOf(connectivity)) .build(); return NodeState.builder() .sequencerMetrics(SequencerMetrics.READY) .heartbeat(new HeartbeatTimestamp(0, 0)) .connectivity(nodeConnectivity) .build(); }
private NodeState unavailable(String endpoint) { NodeConnectivity connectivity = NodeConnectivity.builder() .endpoint(endpoint) .type(NodeConnectivityType.UNAVAILABLE) .connectivity(ImmutableMap.of()) .build(); return new NodeState( connectivity, new HeartbeatTimestamp(Layout.INVALID_EPOCH, 0), SequencerMetrics.UNKNOWN ); } }
@Test public void testTransformation() { NodeState a = NodeState.builder() .sequencerMetrics(SequencerMetrics.READY) .heartbeat(new HeartbeatTimestamp(0, 0)) .connectivity(connectivity("a", ImmutableMap.of("a", OK, "b", OK, "c", FAILED))) .build(); NodeState b = NodeState.builder() .sequencerMetrics(SequencerMetrics.READY) .heartbeat(new HeartbeatTimestamp(0, 0)) .connectivity(connectivity("b", ImmutableMap.of("a", OK, "b", OK, "c", FAILED))) .build(); NodeState c = unavailableNodeState("c"); ImmutableMap<String, NodeState> nodes = ImmutableMap.of("a", a, "b", b, "c", c); ClusterState clusterState = ClusterState.builder() .nodes(nodes) .build(); ClusterGraph graph = ClusterGraph.toClusterGraph(clusterState, "a"); assertEquals(graph.size(), nodes.size()); }
@Test public void testSerializeDeserialize() { NodeConnectivity co = NodeConnectivity.builder() .type(NodeConnectivityType.CONNECTED) .endpoint("localhost:9000") .connectivity(ImmutableMap.of()) .build(); NodeState nodeState = NodeState.builder() .sequencerMetrics(SequencerMetrics.UNKNOWN) .heartbeat(new HeartbeatTimestamp(0, 0)) .connectivity(co) .build(); ByteBuf buf = Unpooled.buffer(); CorfuPayloadMsg<NodeState> msg = CorfuMsgType.NODE_STATE_RESPONSE.payloadMsg(nodeState); msg.serialize(buf); CorfuMsg deserializedMsg = CorfuMsg.deserialize(buf); assertEquals(CorfuMsgType.NODE_STATE_RESPONSE, deserializedMsg.msgType); assertNotNull(deserializedMsg.getBuf()); }
public static NodeState getNotReadyNodeState(String endpoint, long epoch, long counter){ return new NodeState( NodeConnectivity.notReady(endpoint), //Layout.INVALID_EPOCH, INVALID_HEARTBEAT_COUNTER - default values for heartbeat new HeartbeatTimestamp(epoch, counter), SequencerMetrics.UNKNOWN ); }
public static NodeState getUnavailableNodeState(String endpoint){ return new NodeState( NodeConnectivity.unavailable(endpoint), new HeartbeatTimestamp(Layout.INVALID_EPOCH, INVALID_HEARTBEAT_COUNTER), SequencerMetrics.UNKNOWN ); }
private NodeState unavailableNodeState(String endpoint) { return new NodeState( unavailable(endpoint), new HeartbeatTimestamp(Layout.INVALID_EPOCH, 0), SequencerMetrics.UNKNOWN ); } }