use of com.sleepycat.je.rep.NodeState in project qpid-broker-j by apache.
the class ReplicatedEnvironmentFacadeTest method testSetPermittedNodes.
@Test
public void testSetPermittedNodes() throws Exception {
ReplicatedEnvironmentFacade firstNode = createMaster();
Set<String> permittedNodes = new HashSet<String>();
permittedNodes.add("localhost:" + TEST_NODE_PORT);
permittedNodes.add("localhost:" + _portHelper.getNextAvailable());
firstNode.setPermittedNodes(permittedNodes);
ReplicationNodeImpl replicationNode = new ReplicationNodeImpl(TEST_NODE_NAME, TEST_NODE_HOST_PORT);
NodeState nodeState = getRemoteNodeState(TEST_GROUP_NAME, replicationNode, 5000);
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> settings = objectMapper.readValue(nodeState.getAppState(), Map.class);
Collection<String> appStatePermittedNodes = (Collection<String>) settings.get(PERMITTED_NODE_LIST);
assertEquals("Unexpected permitted nodes", permittedNodes, new HashSet<String>(appStatePermittedNodes));
}
use of com.sleepycat.je.rep.NodeState in project qpid-broker-j by apache.
the class ReplicatedEnvironmentFacadeTest method testEnvironmentFacadeDetectsRemovalOfRemoteNode.
@Test
public void testEnvironmentFacadeDetectsRemovalOfRemoteNode() throws Exception {
final String replicaName = TEST_NODE_NAME + "_1";
final CountDownLatch nodeRemovedLatch = new CountDownLatch(1);
final CountDownLatch nodeAddedLatch = new CountDownLatch(1);
final AtomicReference<ReplicationNode> addedNodeRef = new AtomicReference<ReplicationNode>();
final AtomicReference<ReplicationNode> removedNodeRef = new AtomicReference<ReplicationNode>();
final CountDownLatch stateLatch = new CountDownLatch(1);
final AtomicReference<NodeState> stateRef = new AtomicReference<NodeState>();
ReplicationGroupListener listener = new NoopReplicationGroupListener() {
@Override
public void onReplicationNodeAddedToGroup(ReplicationNode node) {
if (addedNodeRef.compareAndSet(null, node)) {
nodeAddedLatch.countDown();
}
}
@Override
public void onReplicationNodeRemovedFromGroup(ReplicationNode node) {
removedNodeRef.set(node);
nodeRemovedLatch.countDown();
}
@Override
public void onNodeState(ReplicationNode node, NodeState nodeState) {
if (replicaName.equals(node.getName())) {
stateRef.set(nodeState);
stateLatch.countDown();
}
}
};
TestStateChangeListener stateChangeListener = new TestStateChangeListener();
final ReplicatedEnvironmentFacade masterEnvironment = addNode(stateChangeListener, listener);
assertTrue("Master was not started", stateChangeListener.awaitForStateChange(State.MASTER, _timeout, TimeUnit.SECONDS));
masterEnvironment.reapplyDesignatedPrimary();
int replica1Port = _portHelper.getNextAvailable();
String node1NodeHostPort = "localhost:" + replica1Port;
masterEnvironment.setPermittedNodes(Arrays.asList(masterEnvironment.getHostPort(), node1NodeHostPort));
ReplicatedEnvironmentFacade replica = createReplica(replicaName, node1NodeHostPort, new NoopReplicationGroupListener());
assertTrue("Node should be added", nodeAddedLatch.await(_timeout, TimeUnit.SECONDS));
ReplicationNode node = addedNodeRef.get();
assertEquals("Unexpected node name", replicaName, node.getName());
assertTrue("Node state was not heard", stateLatch.await(_timeout, TimeUnit.SECONDS));
assertEquals("Unexpected node role", State.REPLICA, stateRef.get().getNodeState());
assertEquals("Unexpected node name", replicaName, stateRef.get().getNodeName());
replica.close();
masterEnvironment.removeNodeFromGroup(node.getName());
assertTrue("Node deleting is undetected by the environment facade", nodeRemovedLatch.await(_timeout, TimeUnit.SECONDS));
assertEquals("Unexpected node is deleted", node, removedNodeRef.get());
}
use of com.sleepycat.je.rep.NodeState in project qpid-broker-j by apache.
the class ReplicatedEnvironmentFacadeTest method testMasterHearsRemoteNodeRoles.
@Test
public void testMasterHearsRemoteNodeRoles() throws Exception {
final String node2Name = TEST_NODE_NAME + "_2";
final CountDownLatch nodeAddedLatch = new CountDownLatch(1);
final AtomicReference<ReplicationNode> nodeRef = new AtomicReference<ReplicationNode>();
final CountDownLatch stateLatch = new CountDownLatch(1);
final AtomicReference<NodeState> stateRef = new AtomicReference<NodeState>();
ReplicationGroupListener listener = new NoopReplicationGroupListener() {
@Override
public void onReplicationNodeAddedToGroup(ReplicationNode node) {
nodeRef.set(node);
nodeAddedLatch.countDown();
}
@Override
public void onNodeState(ReplicationNode node, NodeState nodeState) {
if (node2Name.equals(node.getName())) {
stateRef.set(nodeState);
stateLatch.countDown();
}
}
};
TestStateChangeListener stateChangeListener = new TestStateChangeListener();
ReplicatedEnvironmentFacade replicatedEnvironmentFacade = addNode(stateChangeListener, listener);
assertTrue("Master was not started", stateChangeListener.awaitForStateChange(State.MASTER, _timeout, TimeUnit.SECONDS));
String node2NodeHostPort = "localhost" + ":" + _portHelper.getNextAvailable();
replicatedEnvironmentFacade.setPermittedNodes(Arrays.asList(replicatedEnvironmentFacade.getHostPort(), node2NodeHostPort));
createReplica(node2Name, node2NodeHostPort, new NoopReplicationGroupListener());
assertEquals("Unexpected number of nodes at start of test", (long) 2, (long) replicatedEnvironmentFacade.getNumberOfElectableGroupMembers());
assertTrue("Node add not fired within timeout", nodeAddedLatch.await(_timeout, TimeUnit.SECONDS));
ReplicationNode remoteNode = (ReplicationNode) nodeRef.get();
assertEquals("Unexpected node name", node2Name, remoteNode.getName());
assertTrue("Node state not fired within timeout", stateLatch.await(_timeout, TimeUnit.SECONDS));
assertEquals("Unexpected node state", State.REPLICA, stateRef.get().getNodeState());
}
use of com.sleepycat.je.rep.NodeState in project qpid-broker-j by apache.
the class ReplicatedEnvironmentFacade method getPermittedHostsFromHelper.
private static Collection<String> getPermittedHostsFromHelper(final String nodeName, final String groupName, final String helperNodeName, final String helperHostPort, final int dbPingSocketTimeout) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Requesting state of the node '%s' at '%s'", helperNodeName, helperHostPort));
}
if (helperNodeName == null || "".equals(helperNodeName)) {
throw new IllegalConfigurationException(String.format("A helper node is not specified for node '%s'" + " joining the group '%s'", nodeName, groupName));
}
Collection<String> permittedNodes = null;
try {
ReplicationNodeImpl node = new ReplicationNodeImpl(helperNodeName, helperHostPort);
NodeState state = getRemoteNodeState(groupName, node, dbPingSocketTimeout);
byte[] applicationState = state.getAppState();
return convertApplicationStateBytesToPermittedNodeList(applicationState);
} catch (SocketTimeoutException ste) {
throw new ExternalServiceTimeoutException(String.format("Timed out trying to connect to existing node '%s' at '%s'", helperNodeName, helperHostPort), ste);
} catch (IOException | ServiceConnectFailedException e) {
throw new ExternalServiceException(String.format("Cannot connect to existing node '%s' at '%s'", helperNodeName, helperHostPort), e);
} catch (BinaryProtocol.ProtocolException e) {
String message = String.format("Unexpected protocol exception '%s' encountered while retrieving state for node '%s' (%s) from group '%s'", e.getUnexpectedMessage(), helperNodeName, helperHostPort, groupName);
LOGGER.warn(message, e);
throw new ExternalServiceException(message, e);
} catch (RuntimeException e) {
throw new ExternalServiceException(String.format("Cannot retrieve state for node '%s' (%s) from group '%s'", helperNodeName, helperHostPort, groupName), e);
}
}
Aggregations