Search in sources :

Example 1 with NodeState

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));
}
Also used : NodeState(com.sleepycat.je.rep.NodeState) ReplicatedEnvironmentFacade(org.apache.qpid.server.store.berkeleydb.replication.ReplicatedEnvironmentFacade) Collection(java.util.Collection) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with NodeState

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());
}
Also used : NodeState(com.sleepycat.je.rep.NodeState) ReplicatedEnvironmentFacade(org.apache.qpid.server.store.berkeleydb.replication.ReplicatedEnvironmentFacade) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) ReplicationNode(com.sleepycat.je.rep.ReplicationNode) Test(org.junit.Test)

Example 3 with NodeState

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());
}
Also used : NodeState(com.sleepycat.je.rep.NodeState) ReplicatedEnvironmentFacade(org.apache.qpid.server.store.berkeleydb.replication.ReplicatedEnvironmentFacade) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) ReplicationNode(com.sleepycat.je.rep.ReplicationNode) Test(org.junit.Test)

Example 4 with NodeState

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);
    }
}
Also used : ExternalServiceTimeoutException(org.apache.qpid.server.util.ExternalServiceTimeoutException) NodeState(com.sleepycat.je.rep.NodeState) BinaryProtocol(com.sleepycat.je.rep.utilint.BinaryProtocol) ExternalServiceException(org.apache.qpid.server.util.ExternalServiceException) IllegalConfigurationException(org.apache.qpid.server.configuration.IllegalConfigurationException) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException) ServiceConnectFailedException(com.sleepycat.je.rep.utilint.ServiceDispatcher.ServiceConnectFailedException) ConnectionScopedRuntimeException(org.apache.qpid.server.util.ConnectionScopedRuntimeException) ServerScopedRuntimeException(org.apache.qpid.server.util.ServerScopedRuntimeException)

Aggregations

NodeState (com.sleepycat.je.rep.NodeState)4 ReplicatedEnvironmentFacade (org.apache.qpid.server.store.berkeleydb.replication.ReplicatedEnvironmentFacade)3 Test (org.junit.Test)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 ReplicationNode (com.sleepycat.je.rep.ReplicationNode)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 BinaryProtocol (com.sleepycat.je.rep.utilint.BinaryProtocol)1 ServiceConnectFailedException (com.sleepycat.je.rep.utilint.ServiceDispatcher.ServiceConnectFailedException)1 IOException (java.io.IOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 IllegalConfigurationException (org.apache.qpid.server.configuration.IllegalConfigurationException)1 ConnectionScopedRuntimeException (org.apache.qpid.server.util.ConnectionScopedRuntimeException)1 ExternalServiceException (org.apache.qpid.server.util.ExternalServiceException)1 ExternalServiceTimeoutException (org.apache.qpid.server.util.ExternalServiceTimeoutException)1 ServerScopedRuntimeException (org.apache.qpid.server.util.ServerScopedRuntimeException)1