diff options
| author | Alex Rudyy <orudyy@apache.org> | 2014-08-06 12:21:59 +0000 |
|---|---|---|
| committer | Alex Rudyy <orudyy@apache.org> | 2014-08-06 12:21:59 +0000 |
| commit | 1f2d3541f070cedbda87e3df6ee692edbcdf9381 (patch) | |
| tree | 0142e6a7babc1f00b87c805f567d6338a653986f /qpid/java/bdbstore | |
| parent | 90c8a29045f18554fd4c2da5ad01dd00af11cae7 (diff) | |
| download | qpid-python-1f2d3541f070cedbda87e3df6ee692edbcdf9381.tar.gz | |
QPID-5967: Intruder node detection must be mandatory and should validate all necessary arguments
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1616186 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java/bdbstore')
11 files changed, 349 insertions, 407 deletions
diff --git a/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/replication/ReplicatedEnvironmentFacade.java b/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/replication/ReplicatedEnvironmentFacade.java index 4c98f9fb26..92115dd39f 100644 --- a/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/replication/ReplicatedEnvironmentFacade.java +++ b/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/store/berkeleydb/replication/ReplicatedEnvironmentFacade.java @@ -105,7 +105,7 @@ public class ReplicatedEnvironmentFacade implements EnvironmentFacade, StateChan private static final int DEFAULT_REMOTE_NODE_MONITOR_INTERVAL = 1000; private static final int MASTER_TRANSFER_TIMEOUT = Integer.getInteger(MASTER_TRANSFER_TIMEOUT_PROPERTY_NAME, DEFAULT_MASTER_TRANSFER_TIMEOUT); - private static final int DB_PING_SOCKET_TIMEOUT = Integer.getInteger(DB_PING_SOCKET_TIMEOUT_PROPERTY_NAME, DEFAULT_DB_PING_SOCKET_TIMEOUT); + public static final int DB_PING_SOCKET_TIMEOUT = Integer.getInteger(DB_PING_SOCKET_TIMEOUT_PROPERTY_NAME, DEFAULT_DB_PING_SOCKET_TIMEOUT); private static final int REMOTE_NODE_MONITOR_INTERVAL = Integer.getInteger(REMOTE_NODE_MONITOR_INTERVAL_PROPERTY_NAME, DEFAULT_REMOTE_NODE_MONITOR_INTERVAL); private static final int RESTART_TRY_LIMIT = 3; @@ -149,7 +149,7 @@ public class ReplicatedEnvironmentFacade implements EnvironmentFacade, StateChan put(ReplicationConfig.LOG_FLUSH_TASK_INTERVAL, "1 min"); }}); - private static final String PERMITTED_NODE_LIST = "permittedNodes"; + public static final String PERMITTED_NODE_LIST = "permittedNodes"; private final ReplicatedEnvironmentConfiguration _configuration; private final String _prettyGroupNodeName; @@ -978,7 +978,7 @@ public class ReplicatedEnvironmentFacade implements EnvironmentFacade, StateChan LOGGER.info("Node name " + nodeName); LOGGER.info("Node host port " + hostPort); LOGGER.info("Helper host port " + helperHostPort); - LOGGER.info("Helper host name " + helperNodeName); + LOGGER.info("Helper node name " + helperNodeName); LOGGER.info("Durability " + _defaultDurability); LOGGER.info("Designated primary (applicable to 2 node case only) " + designatedPrimary); LOGGER.info("Node priority " + priority); @@ -986,10 +986,6 @@ public class ReplicatedEnvironmentFacade implements EnvironmentFacade, StateChan LOGGER.info("Permitted node list " + _permittedNodes); } - if (helperNodeName != null && _permittedNodes.isEmpty() && !helperHostPort.equals(hostPort)) - { - connectToHelperNodeAndCheckPermittedHosts(helperNodeName, helperHostPort, hostPort); - } Map<String, String> replicationEnvironmentParameters = new HashMap<>(ReplicatedEnvironmentFacade.REPCONFIG_DEFAULTS); replicationEnvironmentParameters.putAll(_configuration.getReplicationParameters()); @@ -1241,64 +1237,6 @@ public class ReplicatedEnvironmentFacade implements EnvironmentFacade, StateChan return baos.toByteArray(); } - Collection<String> bytesToPermittedNodeList(byte[] applicationState) - { - if (applicationState == null || applicationState.length == 0) - { - return Collections.emptySet(); - } - - ObjectMapper objectMapper = new ObjectMapper(); - try - { - Map<String, Object> settings = objectMapper.readValue(applicationState, Map.class); - return (Collection<String>)settings.get(PERMITTED_NODE_LIST); - } - catch (Exception e) - { - throw new RuntimeException("Unexpected exception on de-serializing of application state", e); - } - } - - private void connectToHelperNodeAndCheckPermittedHosts(String helperNodeName, String helperHostPort, String hostPort) - { - if (LOGGER.isDebugEnabled()) - { - LOGGER.debug(String.format("Requesting state of the node '%s' at '%s'", helperNodeName, helperHostPort)); - } - - Collection<String> permittedNodes = null; - try - { - NodeState state = getRemoteNodeState(new ReplicationNodeImpl(helperNodeName, helperHostPort)); - byte[] applicationState = state.getAppState(); - permittedNodes = bytesToPermittedNodeList(applicationState); - } - catch (IOException e) - { - throw new IllegalConfigurationException(String.format("Cannot connect to '%s'", helperHostPort), e); - } - catch (ServiceConnectFailedException e) - { - throw new IllegalConfigurationException(String.format("Failure to connect to '%s'", helperHostPort), e); - } - catch (Exception e) - { - throw new RuntimeException(String.format("Unexpected exception on attempt to retrieve state from '%s' at '%s'", - helperNodeName, helperHostPort), e); - } - - if (LOGGER.isDebugEnabled()) - { - LOGGER.debug(String.format("Attribute 'permittedNodes' on node '%s' is set to '%s'", helperNodeName, String.valueOf(permittedNodes))); - } - - if (permittedNodes != null && !permittedNodes.isEmpty() && !permittedNodes.contains(hostPort)) - { - throw new IllegalConfigurationException(String.format("Node from '%s' is not permitted!", hostPort)); - } - } - private void populateExistingRemoteReplicationNodes() { ReplicationGroup group = _environment.getGroup(); @@ -1542,7 +1480,7 @@ public class ReplicatedEnvironmentFacade implements EnvironmentFacade, StateChan } } - static class ReplicationNodeImpl implements ReplicationNode + public static class ReplicationNodeImpl implements ReplicationNode { private final InetSocketAddress _address; diff --git a/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/virtualhost/berkeleydb/BDBHAVirtualHost.java b/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/virtualhost/berkeleydb/BDBHAVirtualHost.java index 477ffb5a64..e2503a8b51 100644 --- a/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/virtualhost/berkeleydb/BDBHAVirtualHost.java +++ b/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/virtualhost/berkeleydb/BDBHAVirtualHost.java @@ -56,6 +56,6 @@ public interface BDBHAVirtualHost<X extends BDBHAVirtualHost<X>> extends Virtual @ManagedAttribute(mandatory = true, defaultValue = "0") Long getStoreOverfullSize(); - @ManagedAttribute + @ManagedAttribute(mandatory = true) List<String> getPermittedNodes(); } diff --git a/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/virtualhost/berkeleydb/BDBHAVirtualHostImpl.java b/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/virtualhost/berkeleydb/BDBHAVirtualHostImpl.java index aaa5f6aca4..a005bca194 100644 --- a/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/virtualhost/berkeleydb/BDBHAVirtualHostImpl.java +++ b/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/virtualhost/berkeleydb/BDBHAVirtualHostImpl.java @@ -134,26 +134,30 @@ public class BDBHAVirtualHostImpl extends AbstractVirtualHost<BDBHAVirtualHostIm if(changedAttributes.contains(PERMITTED_NODES)) { + validatePermittedNodes(((BDBHAVirtualHost<?>)proxyForValidation).getPermittedNodes()); + } + } - List<String> permittedNodes = ((BDBHAVirtualHost<?>)proxyForValidation).getPermittedNodes(); - if (permittedNodes != null) + private void validatePermittedNodes(List<String> permittedNodes) + { + if (permittedNodes == null || permittedNodes.isEmpty()) + { + throw new IllegalArgumentException(String.format("Attribute '%s' is mandatory and must be set", PERMITTED_NODES)); + } + for (String permittedNode: permittedNodes) + { + String[] tokens = permittedNode.split(":"); + if (tokens.length != 2) + { + throw new IllegalArgumentException(String.format("Invalid permitted node specified '%s'. ", permittedNode)); + } + try { - for (String permittedNode: permittedNodes) - { - String[] tokens = permittedNode.split(":"); - if (tokens.length != 2) - { - throw new IllegalArgumentException(String.format("Invalid permitted node specified '%s'. ", permittedNode)); - } - try - { - Integer.parseInt(tokens[1]); - } - catch(Exception e) - { - throw new IllegalArgumentException(String.format("Invalid port is specified in permitted node '%s'. ", permittedNode)); - } - } + Integer.parseInt(tokens[1]); + } + catch(Exception e) + { + throw new IllegalArgumentException(String.format("Invalid port is specified in permitted node '%s'. ", permittedNode)); } } } @@ -193,6 +197,16 @@ public class BDBHAVirtualHostImpl extends AbstractVirtualHost<BDBHAVirtualHostIm return _permittedNodes; } + @Override + public void onValidate() + { + super.onValidate(); + + validatePermittedNodes(this.getPermittedNodes()); + validateTransactionSynchronizationPolicy(this.getLocalTransactionSynchronizationPolicy()); + validateTransactionSynchronizationPolicy(this.getRemoteTransactionSynchronizationPolicy()); + } + protected void applyPermittedNodes() { ReplicatedEnvironmentFacade facade = getReplicatedEnvironmentFacade(); diff --git a/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/virtualhostnode/berkeleydb/BDBHAVirtualHostNodeImpl.java b/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/virtualhostnode/berkeleydb/BDBHAVirtualHostNodeImpl.java index 2cdb6ec635..83a2054793 100644 --- a/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/virtualhostnode/berkeleydb/BDBHAVirtualHostNodeImpl.java +++ b/qpid/java/bdbstore/src/main/java/org/apache/qpid/server/virtualhostnode/berkeleydb/BDBHAVirtualHostNodeImpl.java @@ -20,9 +20,11 @@ */ package org.apache.qpid.server.virtualhostnode.berkeleydb; +import java.io.IOException; import java.net.InetSocketAddress; import java.security.PrivilegedAction; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -41,10 +43,13 @@ import com.sleepycat.je.rep.ReplicatedEnvironment; import com.sleepycat.je.rep.ReplicationNode; import com.sleepycat.je.rep.StateChangeEvent; import com.sleepycat.je.rep.StateChangeListener; +import com.sleepycat.je.rep.util.DbPing; import com.sleepycat.je.rep.util.ReplicationGroupAdmin; import com.sleepycat.je.rep.utilint.HostPortPair; +import com.sleepycat.je.rep.utilint.ServiceDispatcher; import org.apache.log4j.Logger; +import org.apache.qpid.server.configuration.IllegalConfigurationException; import org.apache.qpid.server.configuration.updater.Task; import org.apache.qpid.server.logging.messages.ConfigStoreMessages; import org.apache.qpid.server.logging.messages.HighAvailabilityMessages; @@ -66,8 +71,10 @@ import org.apache.qpid.server.store.berkeleydb.replication.ReplicatedEnvironment import org.apache.qpid.server.store.berkeleydb.replication.ReplicatedEnvironmentFacadeFactory; import org.apache.qpid.server.store.berkeleydb.replication.ReplicationGroupListener; import org.apache.qpid.server.util.ServerScopedRuntimeException; +import org.apache.qpid.server.virtualhost.berkeleydb.BDBHAVirtualHost; import org.apache.qpid.server.virtualhost.berkeleydb.BDBHAVirtualHostImpl; import org.apache.qpid.server.virtualhostnode.AbstractVirtualHostNode; +import org.codehaus.jackson.map.ObjectMapper; @ManagedObject( category = false, type = BDBHAVirtualHostNodeImpl.VIRTUAL_HOST_NODE_TYPE ) public class BDBHAVirtualHostNodeImpl extends AbstractVirtualHostNode<BDBHAVirtualHostNodeImpl> implements @@ -253,6 +260,19 @@ public class BDBHAVirtualHostNodeImpl extends AbstractVirtualHostNode<BDBHAVirtu public void onCreate() { super.onCreate(); + if (!isFirstNodeInAGroup()) + { + try + { + connectToHelperNodeAndCheckPermittedHosts(getHelperNodeName(), getHelperAddress(), getAddress()); + } + catch(IllegalConfigurationException e) + { + deleted(); + throw e; + } + } + getEventLogger().message(getVirtualHostNodeLogSubject(), HighAvailabilityMessages.ADDED(getName(), getGroupName())); } @@ -383,6 +403,7 @@ public class BDBHAVirtualHostNodeImpl extends AbstractVirtualHostNode<BDBHAVirtu return helpers; } + @Override protected void onClose() { try @@ -675,6 +696,76 @@ public class BDBHAVirtualHostNodeImpl extends AbstractVirtualHostNode<BDBHAVirtu } } + private boolean isFirstNodeInAGroup() + { + return getAddress().equals(getHelperAddress()); + } + + private void connectToHelperNodeAndCheckPermittedHosts(String helperNodeName, String helperHostPort, String hostPort) + { + 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("An attribute '%s' is not set in node '%s'" + + " on joining the group '%s'", HELPER_NODE_NAME, getName(), getGroupName())); + } + + Collection<String> permittedNodes = null; + try + { + ReplicatedEnvironmentFacade.ReplicationNodeImpl node = new ReplicatedEnvironmentFacade.ReplicationNodeImpl(helperNodeName, helperHostPort); + NodeState state = new DbPing(node, getGroupName(), ReplicatedEnvironmentFacade.DB_PING_SOCKET_TIMEOUT).getNodeState(); + byte[] applicationState = state.getAppState(); + permittedNodes = bytesToPermittedNodeList(applicationState); + } + catch (IOException e) + { + throw new IllegalConfigurationException(String.format("Cannot connect to '%s'", helperHostPort), e); + } + catch (ServiceDispatcher.ServiceConnectFailedException e) + { + throw new IllegalConfigurationException(String.format("Failure to connect to '%s'", helperHostPort), e); + } + catch (Exception e) + { + throw new RuntimeException(String.format("Unexpected exception on attempt to retrieve state from '%s' at '%s'", + helperNodeName, helperHostPort), e); + } + + if (LOGGER.isDebugEnabled()) + { + LOGGER.debug(String.format("Attribute 'permittedNodes' on node '%s' is set to '%s'", helperNodeName, String.valueOf(permittedNodes))); + } + + if (permittedNodes != null && !permittedNodes.isEmpty() && !permittedNodes.contains(hostPort)) + { + throw new IllegalConfigurationException(String.format("Node from '%s' is not permitted!", hostPort)); + } + } + + private Collection<String> bytesToPermittedNodeList(byte[] applicationState) + { + if (applicationState == null || applicationState.length == 0) + { + return Collections.emptySet(); + } + + ObjectMapper objectMapper = new ObjectMapper(); + try + { + Map<String, Object> settings = objectMapper.readValue(applicationState, Map.class); + return (Collection<String>)settings.get(ReplicatedEnvironmentFacade.PERMITTED_NODE_LIST); + } + catch (Exception e) + { + throw new RuntimeException("Unexpected exception on de-serializing of application state", e); + } + } + private class RemoteNodesDiscoverer implements ReplicationGroupListener { @Override diff --git a/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/BDBHAVirtualHostNodeTest.java b/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/BDBHAVirtualHostNodeTest.java index 66b252a246..c667e7f7bb 100644 --- a/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/BDBHAVirtualHostNodeTest.java +++ b/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/BDBHAVirtualHostNodeTest.java @@ -34,6 +34,8 @@ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; +import com.sleepycat.je.Durability; +import com.sleepycat.je.EnvironmentConfig; import com.sleepycat.je.rep.ReplicatedEnvironment; import com.sleepycat.je.rep.ReplicationConfig; @@ -80,25 +82,16 @@ public class BDBHAVirtualHostNodeTest extends QpidTestCase public void testCreateAndActivateVirtualHostNode() throws Exception { - String messageStorePath = _helper.getMessageStorePath(); - String repStreamTimeout = "2 h"; - String nodeName = "node"; + int node1PortNumber = findFreePort(); + String helperAddress = "localhost:" + node1PortNumber; String groupName = "group"; - String nodeHostPort = "localhost:" + findFreePort(); - String helperHostPort = nodeHostPort; - UUID id = UUID.randomUUID(); - - Map<String, Object> attributes = new HashMap<String, Object>(); - attributes.put(BDBHAVirtualHostNode.TYPE, BDBHAVirtualHostNodeImpl.VIRTUAL_HOST_NODE_TYPE); - attributes.put(BDBHAVirtualHostNode.ID, id); - attributes.put(BDBHAVirtualHostNode.NAME, nodeName); - attributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - attributes.put(BDBHAVirtualHostNode.ADDRESS, nodeHostPort); - attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperHostPort); - attributes.put(BDBHAVirtualHostNode.STORE_PATH, messageStorePath); - attributes.put(BDBHAVirtualHostNode.CONTEXT, - singletonMap(ReplicationConfig.REP_STREAM_TIMEOUT, repStreamTimeout)); + String nodeName = "node1"; + Map<String, Object> attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber); + String messageStorePath = (String)attributes.get(BDBHAVirtualHostNode.STORE_PATH); + String repStreamTimeout = "2 h"; + Map<String,String> context = (Map<String,String>)attributes.get(BDBHAVirtualHostNode.CONTEXT); + context.put(ReplicationConfig.REP_STREAM_TIMEOUT, repStreamTimeout); BDBHAVirtualHostNode<?> node = _helper.createHaVHN(attributes); final CountDownLatch virtualHostAddedLatch = new CountDownLatch(1); @@ -117,6 +110,7 @@ public class BDBHAVirtualHostNodeTest extends QpidTestCase node.start(); _helper.assertNodeRole(node, "MASTER", "REPLICA"); + assertEquals("Unexpected node state", State.ACTIVE, node.getState()); DurableConfigurationStore store = node.getConfigurationStore(); @@ -128,8 +122,8 @@ public class BDBHAVirtualHostNodeTest extends QpidTestCase assertEquals(nodeName, environment.getNodeName()); assertEquals(groupName, environment.getGroup().getName()); - assertEquals(nodeHostPort, replicationConfig.getNodeHostPort()); - assertEquals(helperHostPort, replicationConfig.getHelperHosts()); + assertEquals(helperAddress, replicationConfig.getNodeHostPort()); + assertEquals(helperAddress, replicationConfig.getHelperHosts()); assertEquals("SYNC,NO_SYNC,SIMPLE_MAJORITY", environment.getConfig().getDurability().toString()); assertEquals("Unexpected JE replication stream timeout", repStreamTimeout, replicationConfig.getConfigParam(ReplicationConfig.REP_STREAM_TIMEOUT)); @@ -155,18 +149,12 @@ public class BDBHAVirtualHostNodeTest extends QpidTestCase public void testMutableAttributes() throws Exception { - UUID id = UUID.randomUUID(); - String address = "localhost:" + findFreePort(); - - Map<String, Object> attributes = new HashMap<String, Object>(); - attributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - attributes.put(BDBHAVirtualHostNode.ID, id); - attributes.put(BDBHAVirtualHostNode.NAME, "node"); - attributes.put(BDBHAVirtualHostNode.GROUP_NAME, "group"); - attributes.put(BDBHAVirtualHostNode.ADDRESS, address); - attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, address); - attributes.put(BDBHAVirtualHostNode.STORE_PATH, _helper.getMessageStorePath()); + int node1PortNumber = findFreePort(); + String helperAddress = "localhost:" + node1PortNumber; + String groupName = "group"; + String nodeName = "node1"; + Map<String, Object> attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber); BDBHAVirtualHostNode<?> node = _helper.createAndStartHaVHN(attributes); BDBConfigurationStore bdbConfigurationStore = (BDBConfigurationStore) node.getConfigurationStore(); @@ -190,42 +178,21 @@ public class BDBHAVirtualHostNodeTest extends QpidTestCase public void testTransferMasterToSelf() throws Exception { - String messageStorePath = _helper.getMessageStorePath(); int node1PortNumber = findFreePort(); + int node2PortNumber = getNextAvailable(node1PortNumber + 1); + int node3PortNumber = getNextAvailable(node2PortNumber + 1); + String helperAddress = "localhost:" + node1PortNumber; String groupName = "group"; + String nodeName = "node1"; - Map<String, Object> node1Attributes = new HashMap<String, Object>(); - node1Attributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); - node1Attributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - node1Attributes.put(BDBHAVirtualHostNode.NAME, "node1"); - node1Attributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - node1Attributes.put(BDBHAVirtualHostNode.ADDRESS, helperAddress); - node1Attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); - node1Attributes.put(BDBHAVirtualHostNode.STORE_PATH, messageStorePath + File.separator + "1"); + Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber, node2PortNumber, node3PortNumber); _helper.createAndStartHaVHN(node1Attributes); - int node2PortNumber = getNextAvailable(node1PortNumber+1); - - Map<String, Object> node2Attributes = new HashMap<String, Object>(); - node2Attributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); - node2Attributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - node2Attributes.put(BDBHAVirtualHostNode.NAME, "node2"); - node2Attributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - node2Attributes.put(BDBHAVirtualHostNode.ADDRESS, "localhost:" + node2PortNumber); - node2Attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); - node2Attributes.put(BDBHAVirtualHostNode.STORE_PATH, messageStorePath + File.separator + "2"); + Map<String, Object> node2Attributes = _helper.createNodeAttributes("node2", groupName, "localhost:" + node2PortNumber, helperAddress, nodeName); _helper.createAndStartHaVHN(node2Attributes); - int node3PortNumber = getNextAvailable(node2PortNumber+1); - Map<String, Object> node3Attributes = new HashMap<String, Object>(); - node3Attributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); - node3Attributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - node3Attributes.put(BDBHAVirtualHostNode.NAME, "node3"); - node3Attributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - node3Attributes.put(BDBHAVirtualHostNode.ADDRESS, "localhost:" + node3PortNumber); - node3Attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); - node3Attributes.put(BDBHAVirtualHostNode.STORE_PATH, messageStorePath + File.separator + "3"); + Map<String, Object> node3Attributes = _helper.createNodeAttributes("node3", groupName, "localhost:" + node3PortNumber, helperAddress, nodeName); _helper.createAndStartHaVHN(node3Attributes); BDBHAVirtualHostNode<?> replica = _helper.awaitAndFindNodeInRole("REPLICA"); @@ -238,19 +205,15 @@ public class BDBHAVirtualHostNodeTest extends QpidTestCase public void testTransferMasterToRemoteReplica() throws Exception { int node1PortNumber = findFreePort(); + int node2PortNumber = getNextAvailable(node1PortNumber + 1); + int node3PortNumber = getNextAvailable(node2PortNumber + 1); + String helperAddress = "localhost:" + node1PortNumber; String groupName = "group"; - String messageStorePath = _helper.getMessageStorePath(); - - Map<String, Object> node1Attributes = new HashMap<String, Object>(); - node1Attributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); - node1Attributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - node1Attributes.put(BDBHAVirtualHostNode.NAME, "node1"); - node1Attributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - node1Attributes.put(BDBHAVirtualHostNode.ADDRESS, helperAddress); - node1Attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); - node1Attributes.put(BDBHAVirtualHostNode.STORE_PATH, messageStorePath + File.separator + "1"); + String nodeName = "node1"; + Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, + helperAddress, nodeName, node1PortNumber, node2PortNumber, node3PortNumber); BDBHAVirtualHostNode<?> node1 = _helper.createAndStartHaVHN(node1Attributes); final AtomicReference<RemoteReplicationNode<?>> lastSeenReplica = new AtomicReference<>(); @@ -268,28 +231,10 @@ public class BDBHAVirtualHostNodeTest extends QpidTestCase } }); - int node2PortNumber = getNextAvailable(node1PortNumber+1); - - Map<String, Object> node2Attributes = new HashMap<String, Object>(); - node2Attributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); - node2Attributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - node2Attributes.put(BDBHAVirtualHostNode.NAME, "node2"); - node2Attributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - node2Attributes.put(BDBHAVirtualHostNode.ADDRESS, "localhost:" + node2PortNumber); - node2Attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); - node2Attributes.put(BDBHAVirtualHostNode.STORE_PATH, messageStorePath + File.separator + "2"); - + Map<String, Object> node2Attributes = _helper.createNodeAttributes("node2", groupName, "localhost:" + node2PortNumber, helperAddress, nodeName); BDBHAVirtualHostNode<?> node2 = _helper.createAndStartHaVHN(node2Attributes); - int node3PortNumber = getNextAvailable(node2PortNumber+1); - Map<String, Object> node3Attributes = new HashMap<String, Object>(); - node3Attributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); - node3Attributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - node3Attributes.put(BDBHAVirtualHostNode.NAME, "node3"); - node3Attributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - node3Attributes.put(BDBHAVirtualHostNode.ADDRESS, "localhost:" + node3PortNumber); - node3Attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); - node3Attributes.put(BDBHAVirtualHostNode.STORE_PATH, messageStorePath + File.separator + "3"); + Map<String, Object> node3Attributes = _helper.createNodeAttributes("node3", groupName, "localhost:" + node3PortNumber, helperAddress, nodeName); BDBHAVirtualHostNode<?> node3 = _helper.createAndStartHaVHN(node3Attributes); assertTrue("Replication nodes have not been seen during 5s", remoteNodeLatch.await(5, TimeUnit.SECONDS)); @@ -308,17 +253,9 @@ public class BDBHAVirtualHostNodeTest extends QpidTestCase int nodePortNumber = findFreePort(); String helperAddress = "localhost:" + nodePortNumber; String groupName = "group"; - String messageStorePath = _helper.getMessageStorePath(); - - Map<String, Object> node1Attributes = new HashMap<String, Object>(); - node1Attributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); - node1Attributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - node1Attributes.put(BDBHAVirtualHostNode.NAME, "node1"); - node1Attributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - node1Attributes.put(BDBHAVirtualHostNode.ADDRESS, helperAddress); - node1Attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); - node1Attributes.put(BDBHAVirtualHostNode.STORE_PATH, messageStorePath + File.separator + "1"); + String nodeName = "node1"; + Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, nodePortNumber); BDBHAVirtualHostNode<?> node = _helper.createAndStartHaVHN(node1Attributes); _helper.assertNodeRole(node, "MASTER"); @@ -336,42 +273,21 @@ public class BDBHAVirtualHostNodeTest extends QpidTestCase public void testRemoveReplicaNode() throws Exception { - String messageStorePath = _helper.getMessageStorePath(); int node1PortNumber = findFreePort(); + int node2PortNumber = getNextAvailable(node1PortNumber + 1); + int node3PortNumber = getNextAvailable(node2PortNumber + 1); + String helperAddress = "localhost:" + node1PortNumber; String groupName = "group"; + String nodeName = "node1"; - Map<String, Object> node1Attributes = new HashMap<String, Object>(); - node1Attributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); - node1Attributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - node1Attributes.put(BDBHAVirtualHostNode.NAME, "node1"); - node1Attributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - node1Attributes.put(BDBHAVirtualHostNode.ADDRESS, helperAddress); - node1Attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); - node1Attributes.put(BDBHAVirtualHostNode.STORE_PATH, messageStorePath + File.separator + "1"); + Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber, node2PortNumber, node3PortNumber); _helper.createAndStartHaVHN(node1Attributes); - int node2PortNumber = getNextAvailable(node1PortNumber+1); - - Map<String, Object> node2Attributes = new HashMap<String, Object>(); - node2Attributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); - node2Attributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - node2Attributes.put(BDBHAVirtualHostNode.NAME, "node2"); - node2Attributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - node2Attributes.put(BDBHAVirtualHostNode.ADDRESS, "localhost:" + node2PortNumber); - node2Attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); - node2Attributes.put(BDBHAVirtualHostNode.STORE_PATH, messageStorePath + File.separator + "2"); + Map<String, Object> node2Attributes = _helper.createNodeAttributes("node2", groupName, "localhost:" + node2PortNumber, helperAddress, nodeName); _helper.createAndStartHaVHN(node2Attributes); - int node3PortNumber = getNextAvailable(node2PortNumber+1); - Map<String, Object> node3Attributes = new HashMap<String, Object>(); - node3Attributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); - node3Attributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - node3Attributes.put(BDBHAVirtualHostNode.NAME, "node3"); - node3Attributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - node3Attributes.put(BDBHAVirtualHostNode.ADDRESS, "localhost:" + node3PortNumber); - node3Attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); - node3Attributes.put(BDBHAVirtualHostNode.STORE_PATH, messageStorePath + File.separator + "3"); + Map<String, Object> node3Attributes = _helper.createNodeAttributes("node3", groupName, "localhost:" + node3PortNumber, helperAddress, nodeName); _helper.createAndStartHaVHN(node3Attributes); BDBHAVirtualHostNode<?> master = _helper.awaitAndFindNodeInRole("MASTER"); @@ -393,15 +309,9 @@ public class BDBHAVirtualHostNodeTest extends QpidTestCase int node1PortNumber = findFreePort(); String helperAddress = "localhost:" + node1PortNumber; String groupName = "group"; + String nodeName = "node1"; - Map<String, Object> nodeAttributes = new HashMap<String, Object>(); - nodeAttributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); - nodeAttributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - nodeAttributes.put(BDBHAVirtualHostNode.NAME, "node1"); - nodeAttributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - nodeAttributes.put(BDBHAVirtualHostNode.ADDRESS, helperAddress); - nodeAttributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); - nodeAttributes.put(BDBHAVirtualHostNode.STORE_PATH, _helper.getMessageStorePath() + File.separator + "1"); + Map<String, Object> nodeAttributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber); BDBHAVirtualHostNode<?> node = _helper.createHaVHN(nodeAttributes); final CountDownLatch virtualHostAddedLatch = new CountDownLatch(1); @@ -465,59 +375,23 @@ public class BDBHAVirtualHostNodeTest extends QpidTestCase } - public void testIntruderProtection() throws Exception + public void testNotPermittedNodeIsNotAllowedToConnect() throws Exception { - String messageStorePath = _helper.getMessageStorePath(); int node1PortNumber = findFreePort(); int node2PortNumber = getNextAvailable(node1PortNumber+1); + int node3PortNumber = getNextAvailable(node2PortNumber+1); + String helperAddress = "localhost:" + node1PortNumber; String groupName = "group"; + String nodeName = "node1"; - List<String> permittedNodes = new ArrayList<>(); - permittedNodes.add(helperAddress); - String node2Address = "localhost:" + node2PortNumber; - permittedNodes.add(node2Address); - - String blueprint = String.format("{ \"%s\" : [ \"%s\", \"%s\" ] } ", BDBHAVirtualHost.PERMITTED_NODES, helperAddress, node2Address); - - Map<String, Object> node1Attributes = new HashMap<>(); - node1Attributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); - node1Attributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - node1Attributes.put(BDBHAVirtualHostNode.NAME, "node1"); - node1Attributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - node1Attributes.put(BDBHAVirtualHostNode.ADDRESS, helperAddress); - node1Attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); - node1Attributes.put(BDBHAVirtualHostNode.STORE_PATH, messageStorePath + File.separator + "1"); - Map<String, String> contextMap = singletonMap(AbstractVirtualHostNode.VIRTUALHOST_BLUEPRINT_CONTEXT_VAR, blueprint); - node1Attributes.put(BDBHAVirtualHostNode.CONTEXT, contextMap); - + Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber, node2PortNumber); BDBHAVirtualHostNode<?> node1 = _helper.createAndStartHaVHN(node1Attributes); - Map<String, Object> node2Attributes = new HashMap<String, Object>(); - node2Attributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); - node2Attributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - node2Attributes.put(BDBHAVirtualHostNode.NAME, "node2"); - node2Attributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - node2Attributes.put(BDBHAVirtualHostNode.ADDRESS, node2Address); - node2Attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); - node2Attributes.put(BDBHAVirtualHostNode.STORE_PATH, messageStorePath + File.separator + "2"); - node2Attributes.put(BDBHAVirtualHostNode.HELPER_NODE_NAME, "node1"); - node2Attributes.put(BDBHAVirtualHostNode.PRIORITY, 0); - + Map<String, Object> node2Attributes = _helper.createNodeAttributes("node2", groupName, "localhost:" + node2PortNumber, helperAddress, nodeName); BDBHAVirtualHostNode<?> node2 = _helper.createAndStartHaVHN(node2Attributes); - int node3PortNumber = getNextAvailable(node2PortNumber+1); - Map<String, Object> node3Attributes = new HashMap<String, Object>(); - node3Attributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); - node3Attributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - node3Attributes.put(BDBHAVirtualHostNode.NAME, "node3"); - node3Attributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - node3Attributes.put(BDBHAVirtualHostNode.ADDRESS, "localhost:" + node3PortNumber); - node3Attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); - node3Attributes.put(BDBHAVirtualHostNode.STORE_PATH, messageStorePath + File.separator + "3"); - node3Attributes.put(BDBHAVirtualHostNode.HELPER_NODE_NAME, "node1"); - node3Attributes.put(BDBHAVirtualHostNode.PRIORITY, 0); - + Map<String, Object> node3Attributes = _helper.createNodeAttributes("node3", groupName, "localhost:" + node3PortNumber, helperAddress, nodeName); try { _helper.createHaVHN(node3Attributes); @@ -527,59 +401,22 @@ public class BDBHAVirtualHostNodeTest extends QpidTestCase { assertEquals("Unexpected exception message", String.format("Node from '%s' is not permitted!", "localhost:" + node3PortNumber), e.getMessage()); } - - // join node by skipping a step retrieving node state and checking the permitted hosts - node3Attributes.remove(BDBHAVirtualHostNode.HELPER_NODE_NAME); - - final CountDownLatch stopLatch = new CountDownLatch(1); - ConfigurationChangeListener listener = new NoopConfigurationChangeListener() - { - @Override - public void stateChanged(ConfiguredObject<?> object, State oldState, State newState) - { - if (newState == State.ERRORED) - { - stopLatch.countDown(); - } - } - }; - node1.addChangeListener(listener); - - _helper.createHaVHN(node3Attributes); - - assertTrue("Intruder protection was not triggered during expected timeout", stopLatch.await(10, TimeUnit.SECONDS)); } public void testIntruderProtectionInManagementMode() throws Exception { - String messageStorePath = _helper.getMessageStorePath(); int node1PortNumber = findFreePort(); + int node2PortNumber = getNextAvailable(node1PortNumber + 1); + String helperAddress = "localhost:" + node1PortNumber; String groupName = "group"; + String nodeName = "node1"; - Map<String, Object> node1Attributes = new HashMap<String, Object>(); - node1Attributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); - node1Attributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - node1Attributes.put(BDBHAVirtualHostNode.NAME, "node1"); - node1Attributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - node1Attributes.put(BDBHAVirtualHostNode.ADDRESS, helperAddress); - node1Attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); - node1Attributes.put(BDBHAVirtualHostNode.STORE_PATH, messageStorePath + File.separator + "1"); - + Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber, node2PortNumber); BDBHAVirtualHostNode<?> node1 = _helper.createAndStartHaVHN(node1Attributes); - int node2PortNumber = getNextAvailable(node1PortNumber+1); - Map<String, Object> node2Attributes = new HashMap<String, Object>(); - node2Attributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); - node2Attributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - node2Attributes.put(BDBHAVirtualHostNode.NAME, "node2"); - node2Attributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - node2Attributes.put(BDBHAVirtualHostNode.ADDRESS, "localhost:" + node2PortNumber); - node2Attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); - node2Attributes.put(BDBHAVirtualHostNode.STORE_PATH, messageStorePath + File.separator + "2"); - node2Attributes.put(BDBHAVirtualHostNode.HELPER_NODE_NAME, "node1"); + Map<String, Object> node2Attributes = _helper.createNodeAttributes("node2", groupName, "localhost:" + node2PortNumber, helperAddress, nodeName); node2Attributes.put(BDBHAVirtualHostNode.PRIORITY, 0); - BDBHAVirtualHostNode<?> node2 = _helper.createAndStartHaVHN(node2Attributes); final CountDownLatch stopLatch = new CountDownLatch(1); @@ -613,37 +450,18 @@ public class BDBHAVirtualHostNodeTest extends QpidTestCase remote.delete(); } - public void testIntruderConnectedBeforePermittedNodesAreSet() throws Exception + public void testIntruderConnected() throws Exception { - String messageStorePath = _helper.getMessageStorePath(); int node1PortNumber = findFreePort(); + int node2PortNumber = getNextAvailable(node1PortNumber + 1); + String helperAddress = "localhost:" + node1PortNumber; String groupName = "group"; + String nodeName = "node1"; - Map<String, Object> node1Attributes = new HashMap<String, Object>(); - node1Attributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); - node1Attributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - node1Attributes.put(BDBHAVirtualHostNode.NAME, "node1"); - node1Attributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - node1Attributes.put(BDBHAVirtualHostNode.ADDRESS, helperAddress); - node1Attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); - node1Attributes.put(BDBHAVirtualHostNode.STORE_PATH, messageStorePath + File.separator + "1"); - + Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber); BDBHAVirtualHostNode<?> node1 = _helper.createAndStartHaVHN(node1Attributes); - int node2PortNumber = getNextAvailable(node1PortNumber+1); - Map<String, Object> node2Attributes = new HashMap<String, Object>(); - node2Attributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); - node2Attributes.put(BDBHAVirtualHostNode.TYPE, "BDB_HA"); - node2Attributes.put(BDBHAVirtualHostNode.NAME, "node2"); - node2Attributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName); - node2Attributes.put(BDBHAVirtualHostNode.ADDRESS, "localhost:" + node2PortNumber); - node2Attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); - node2Attributes.put(BDBHAVirtualHostNode.STORE_PATH, messageStorePath + File.separator + "2"); - node2Attributes.put(BDBHAVirtualHostNode.HELPER_NODE_NAME, "node1"); - - _helper.createAndStartHaVHN(node2Attributes); - final CountDownLatch stopLatch = new CountDownLatch(1); ConfigurationChangeListener listener = new NoopConfigurationChangeListener() { @@ -658,14 +476,28 @@ public class BDBHAVirtualHostNodeTest extends QpidTestCase }; node1.addChangeListener(listener); - BDBHAVirtualHost<?> host = (BDBHAVirtualHost<?>)node1.getVirtualHost(); - List<String> permittedNodes = new ArrayList<String>(); - permittedNodes.add(helperAddress); - host.setAttributes(Collections.<String, Object>singletonMap(BDBHAVirtualHost.PERMITTED_NODES, permittedNodes)); + String node2Name = "node2"; + File environmentPathFile = new File(_helper.getMessageStorePath() + File.separator + node2Name); + environmentPathFile.mkdirs(); - assertTrue("Intruder protection was not triggered during expected timeout", stopLatch.await(20, TimeUnit.SECONDS)); - } + ReplicationConfig replicationConfig = new ReplicationConfig(groupName, node2Name, "localhost:" + node2PortNumber ); + replicationConfig.setHelperHosts(helperAddress); + EnvironmentConfig envConfig = new EnvironmentConfig(); + envConfig.setAllowCreate(true); + envConfig.setTransactional(true); + envConfig.setDurability(Durability.parse((String) node1Attributes.get(BDBHAVirtualHostNode.DURABILITY))); -} + ReplicatedEnvironment intruder = null; + try + { + intruder = new ReplicatedEnvironment(environmentPathFile, replicationConfig, envConfig); + } + finally + { + intruder.close(); + } + assertTrue("Intruder protection was not triggered during expected timeout", stopLatch.await(20, TimeUnit.SECONDS)); + } +}
\ No newline at end of file diff --git a/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/replication/ReplicatedEnvironmentFacadeTest.java b/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/replication/ReplicatedEnvironmentFacadeTest.java index 229c2c3f27..44fc19e14f 100644 --- a/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/replication/ReplicatedEnvironmentFacadeTest.java +++ b/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/store/berkeleydb/replication/ReplicatedEnvironmentFacadeTest.java @@ -24,11 +24,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.File; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -55,6 +51,7 @@ import com.sleepycat.je.rep.ReplicationConfig; import com.sleepycat.je.rep.ReplicationNode; import com.sleepycat.je.rep.StateChangeEvent; import com.sleepycat.je.rep.StateChangeListener; +import org.codehaus.jackson.map.ObjectMapper; public class ReplicatedEnvironmentFacadeTest extends QpidTestCase { @@ -267,6 +264,7 @@ public class ReplicatedEnvironmentFacadeTest extends QpidTestCase String node2Name = TEST_NODE_NAME + "_2"; String node2NodeHostPort = "localhost" + ":" + getNextAvailable(TEST_NODE_PORT + 1); + replicatedEnvironmentFacade.setPermittedNodes(Arrays.asList(replicatedEnvironmentFacade.getHostPort(), node2NodeHostPort)); createReplica(node2Name, node2NodeHostPort, new NoopReplicationGroupListener()); assertTrue("Listener not fired within timeout", nodeAddedLatch.await(LISTENER_TIMEOUT, TimeUnit.SECONDS)); @@ -309,6 +307,7 @@ public class ReplicatedEnvironmentFacadeTest extends QpidTestCase String node2Name = TEST_NODE_NAME + "_2"; String node2NodeHostPort = "localhost" + ":" + getNextAvailable(TEST_NODE_PORT + 1); + replicatedEnvironmentFacade.setPermittedNodes(Arrays.asList(replicatedEnvironmentFacade.getHostPort(), node2NodeHostPort)); createReplica(node2Name, node2NodeHostPort, new NoopReplicationGroupListener()); assertEquals("Unexpected number of nodes at start of test", 2, replicatedEnvironmentFacade.getNumberOfElectableGroupMembers()); @@ -358,6 +357,7 @@ public class ReplicatedEnvironmentFacadeTest extends QpidTestCase assertTrue("Master was not started", stateChangeListener.awaitForStateChange(LISTENER_TIMEOUT, TimeUnit.SECONDS)); String node2NodeHostPort = "localhost" + ":" + getNextAvailable(TEST_NODE_PORT + 1); + replicatedEnvironmentFacade.setPermittedNodes(Arrays.asList(replicatedEnvironmentFacade.getHostPort(), node2NodeHostPort)); createReplica(node2Name, node2NodeHostPort, new NoopReplicationGroupListener()); assertEquals("Unexpected number of nodes at start of test", 2, replicatedEnvironmentFacade.getNumberOfElectableGroupMembers()); @@ -434,7 +434,7 @@ public class ReplicatedEnvironmentFacadeTest extends QpidTestCase int replica1Port = getNextAvailable(TEST_NODE_PORT + 1); 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(WAIT_STATE_CHANGE_TIMEOUT, TimeUnit.SECONDS)); @@ -658,7 +658,10 @@ public class ReplicatedEnvironmentFacadeTest extends QpidTestCase NodeState nodeState = firstNode.getRemoteNodeState(new ReplicatedEnvironmentFacade.ReplicationNodeImpl(TEST_NODE_NAME, TEST_NODE_HOST_PORT)); - Collection<String> appStatePermittedNodes = firstNode.bytesToPermittedNodeList(nodeState.getAppState()); + ObjectMapper objectMapper = new ObjectMapper(); + + Map<String, Object> settings = objectMapper.readValue(nodeState.getAppState(), Map.class); + Collection<String> appStatePermittedNodes = (Collection<String>)settings.get(ReplicatedEnvironmentFacade.PERMITTED_NODE_LIST); assertEquals("Unexpected permitted nodes", permittedNodes, new HashSet<String>(appStatePermittedNodes)); } @@ -684,33 +687,6 @@ public class ReplicatedEnvironmentFacadeTest extends QpidTestCase assertEquals("Unexpected state", State.REPLICA.name(), secondNode.getNodeState()); } - public void testNotPermittedNodeIsNotAllowedToConnect() throws Exception - { - ReplicatedEnvironmentFacade firstNode = createMaster(); - - int replica1Port = getNextAvailable(TEST_NODE_PORT + 1); - String node1NodeHostPort = "localhost:" + replica1Port; - - Set<String> permittedNodes = new HashSet<String>(); - permittedNodes.add("localhost:" + TEST_NODE_PORT); - - firstNode.setPermittedNodes(permittedNodes); - - ReplicatedEnvironmentConfiguration configuration = createReplicatedEnvironmentConfiguration(TEST_NODE_NAME + "_1", node1NodeHostPort, false); - when(configuration.getHelperNodeName()).thenReturn(TEST_NODE_NAME); - - try - { - createReplicatedEnvironmentFacade(TEST_NODE_NAME + "_1", new TestStateChangeListener(State.REPLICA), new NoopReplicationGroupListener(), configuration); - fail("Node is not allowed to connect from " + node1NodeHostPort + " but environment was successfully created"); - } - catch (IllegalConfigurationException e) - { - assertEquals("Unexpected exception message", String.format("Node from '%s' is not permitted!", - node1NodeHostPort), e.getMessage()); - } - } - public void testIntruderNodeIsDetected() throws Exception { final CountDownLatch intruderLatch = new CountDownLatch(1); @@ -820,6 +796,7 @@ public class ReplicatedEnvironmentFacadeTest extends QpidTestCase when(node.getPriority()).thenReturn(TEST_PRIORITY); when(node.getGroupName()).thenReturn(TEST_GROUP_NAME); when(node.getHelperHostPort()).thenReturn(TEST_NODE_HELPER_HOST_PORT); + when(node.getHelperNodeName()).thenReturn(TEST_NODE_NAME); Map<String, String> repConfig = new HashMap<String, String>(); repConfig.put(ReplicationConfig.REPLICA_ACK_TIMEOUT, "2 s"); diff --git a/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/virtualhostnode/berkeleydb/BDBHAVirtualHostNodeOperationalLoggingTest.java b/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/virtualhostnode/berkeleydb/BDBHAVirtualHostNodeOperationalLoggingTest.java index 665a778dd8..45527313e6 100644 --- a/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/virtualhostnode/berkeleydb/BDBHAVirtualHostNodeOperationalLoggingTest.java +++ b/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/virtualhostnode/berkeleydb/BDBHAVirtualHostNodeOperationalLoggingTest.java @@ -75,7 +75,7 @@ public class BDBHAVirtualHostNodeOperationalLoggingTest extends QpidTestCase String groupName = "group"; String nodeName = "node1"; - Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress); + Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber); BDBHAVirtualHostNodeImpl node1 = (BDBHAVirtualHostNodeImpl)_helper.createHaVHN(node1Attributes); _helper.assertNodeRole(node1, "MASTER"); @@ -105,7 +105,7 @@ public class BDBHAVirtualHostNodeOperationalLoggingTest extends QpidTestCase String groupName = "group"; String nodeName = "node1"; - Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress); + Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber); BDBHAVirtualHostNodeImpl node1 = (BDBHAVirtualHostNodeImpl)_helper.createHaVHN(node1Attributes); _helper.assertNodeRole(node1, "MASTER"); reset(_eventLogger); @@ -128,7 +128,7 @@ public class BDBHAVirtualHostNodeOperationalLoggingTest extends QpidTestCase String groupName = "group"; String nodeName = "node1"; - Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress); + Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber); BDBHAVirtualHostNodeImpl node1 = (BDBHAVirtualHostNodeImpl)_helper.createHaVHN(node1Attributes); _helper.assertNodeRole(node1, "MASTER"); @@ -148,7 +148,7 @@ public class BDBHAVirtualHostNodeOperationalLoggingTest extends QpidTestCase String groupName = "group"; String nodeName = "node1"; - Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress); + Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber); BDBHAVirtualHostNodeImpl node1 = (BDBHAVirtualHostNodeImpl)_helper.createHaVHN(node1Attributes); _helper.assertNodeRole(node1, "MASTER"); @@ -172,7 +172,7 @@ public class BDBHAVirtualHostNodeOperationalLoggingTest extends QpidTestCase String groupName = "group"; String nodeName = "node1"; - Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress); + Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber); BDBHAVirtualHostNodeImpl node1 = (BDBHAVirtualHostNodeImpl)_helper.createHaVHN(node1Attributes); _helper.assertNodeRole(node1, "MASTER"); @@ -195,7 +195,7 @@ public class BDBHAVirtualHostNodeOperationalLoggingTest extends QpidTestCase String groupName = "group"; String nodeName = "node1"; - Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress); + Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber); BDBHAVirtualHostNodeImpl node1 = (BDBHAVirtualHostNodeImpl)_helper.createHaVHN(node1Attributes); _helper.assertNodeRole(node1, "MASTER"); @@ -218,7 +218,7 @@ public class BDBHAVirtualHostNodeOperationalLoggingTest extends QpidTestCase String groupName = "group"; String nodeName = "node1"; - Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress); + Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber); BDBHAVirtualHostNodeImpl node1 = (BDBHAVirtualHostNodeImpl)_helper.createHaVHN(node1Attributes); _helper.assertNodeRole(node1, "MASTER"); @@ -237,18 +237,19 @@ public class BDBHAVirtualHostNodeOperationalLoggingTest extends QpidTestCase public void testRemoteNodeAdded() throws Exception { int node1PortNumber = findFreePort(); + int node2PortNumber = getNextAvailable(node1PortNumber + 1); String helperAddress = "localhost:" + node1PortNumber; String groupName = "group"; String nodeName = "node1"; - Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress); + Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber, node2PortNumber); BDBHAVirtualHostNodeImpl node1 = (BDBHAVirtualHostNodeImpl)_helper.createHaVHN(node1Attributes); _helper.assertNodeRole(node1, "MASTER"); reset(_eventLogger); - int node2PortNumber = getNextAvailable(node1PortNumber + 1); - Map<String, Object> node2Attributes = _helper.createNodeAttributes("node2", groupName, "localhost:" + node2PortNumber, helperAddress); + + Map<String, Object> node2Attributes = _helper.createNodeAttributes("node2", groupName, "localhost:" + node2PortNumber, helperAddress, nodeName); BDBHAVirtualHostNodeImpl node2 = (BDBHAVirtualHostNodeImpl)_helper.createHaVHN(node2Attributes); _helper.awaitRemoteNodes(node1, 1); @@ -269,19 +270,19 @@ public class BDBHAVirtualHostNodeOperationalLoggingTest extends QpidTestCase public void testRemoteNodeRemoved() throws Exception { int node1PortNumber = findFreePort(); + int node2PortNumber = getNextAvailable(node1PortNumber + 1); String helperAddress = "localhost:" + node1PortNumber; String groupName = "group"; String nodeName = "node1"; - Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress); + Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber, node2PortNumber); node1Attributes.put(BDBHAVirtualHostNode.DESIGNATED_PRIMARY, true); BDBHAVirtualHostNodeImpl node1 = (BDBHAVirtualHostNodeImpl)_helper.createHaVHN(node1Attributes); _helper.assertNodeRole(node1, "MASTER"); resetEventLogger(); - int node2PortNumber = getNextAvailable(node1PortNumber + 1); - Map<String, Object> node2Attributes = _helper.createNodeAttributes("node2", groupName, "localhost:" + node2PortNumber, helperAddress); + Map<String, Object> node2Attributes = _helper.createNodeAttributes("node2", groupName, "localhost:" + node2PortNumber, helperAddress, nodeName); BDBHAVirtualHostNodeImpl node2 = (BDBHAVirtualHostNodeImpl)_helper.createHaVHN(node2Attributes); _helper.awaitRemoteNodes(node1, 1); @@ -301,17 +302,17 @@ public class BDBHAVirtualHostNodeOperationalLoggingTest extends QpidTestCase public void testRemoteNodeDetached() throws Exception { int node1PortNumber = findFreePort(); + int node2PortNumber = getNextAvailable(node1PortNumber + 1); String helperAddress = "localhost:" + node1PortNumber; String groupName = "group"; String nodeName = "node1"; - Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress); + Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber, node2PortNumber); node1Attributes.put(BDBHAVirtualHostNode.DESIGNATED_PRIMARY, true); BDBHAVirtualHostNodeImpl node1 = (BDBHAVirtualHostNodeImpl)_helper.createHaVHN(node1Attributes); _helper.assertNodeRole(node1, "MASTER"); - int node2PortNumber = getNextAvailable(node1PortNumber + 1); - Map<String, Object> node2Attributes = _helper.createNodeAttributes("node2", groupName, "localhost:" + node2PortNumber, helperAddress); + Map<String, Object> node2Attributes = _helper.createNodeAttributes("node2", groupName, "localhost:" + node2PortNumber, helperAddress, nodeName); BDBHAVirtualHostNodeImpl node2 = (BDBHAVirtualHostNodeImpl)_helper.createHaVHN(node2Attributes); _helper.awaitRemoteNodes(node1, 1); @@ -334,18 +335,19 @@ public class BDBHAVirtualHostNodeOperationalLoggingTest extends QpidTestCase public void testRemoteNodeReAttached() throws Exception { int node1PortNumber = findFreePort(); + int node2PortNumber = getNextAvailable(node1PortNumber + 1); String helperAddress = "localhost:" + node1PortNumber; String groupName = "group"; String nodeName = "node1"; - Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress); + Map<String, Object> node1Attributes = _helper.createNodeAttributes(nodeName, groupName, helperAddress, helperAddress, nodeName, node1PortNumber, node2PortNumber); node1Attributes.put(BDBHAVirtualHostNode.DESIGNATED_PRIMARY, true); BDBHAVirtualHostNodeImpl node1 = (BDBHAVirtualHostNodeImpl)_helper.createHaVHN(node1Attributes); _helper.assertNodeRole(node1, "MASTER"); resetEventLogger(); - int node2PortNumber = getNextAvailable(node1PortNumber + 1); - Map<String, Object> node2Attributes = _helper.createNodeAttributes("node2", groupName, "localhost:" + node2PortNumber, helperAddress); + + Map<String, Object> node2Attributes = _helper.createNodeAttributes("node2", groupName, "localhost:" + node2PortNumber, helperAddress, nodeName); BDBHAVirtualHostNodeImpl node2 = (BDBHAVirtualHostNodeImpl)_helper.createHaVHN(node2Attributes); _helper.awaitRemoteNodes(node1, 1); diff --git a/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/virtualhostnode/berkeleydb/BDBHAVirtualHostNodeTestHelper.java b/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/virtualhostnode/berkeleydb/BDBHAVirtualHostNodeTestHelper.java index 754d8d1d09..07b5a8a21c 100644 --- a/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/virtualhostnode/berkeleydb/BDBHAVirtualHostNodeTestHelper.java +++ b/qpid/java/bdbstore/src/test/java/org/apache/qpid/server/virtualhostnode/berkeleydb/BDBHAVirtualHostNodeTestHelper.java @@ -26,10 +26,13 @@ import static org.junit.Assert.fail; import static org.mockito.Mockito.when; import java.io.File; +import java.io.StringWriter; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; @@ -38,18 +41,17 @@ import com.sleepycat.je.rep.ReplicationConfig; import org.apache.qpid.server.configuration.updater.TaskExecutor; import org.apache.qpid.server.configuration.updater.TaskExecutorImpl; -import org.apache.qpid.server.model.Broker; -import org.apache.qpid.server.model.BrokerModel; -import org.apache.qpid.server.model.ConfiguredObject; -import org.apache.qpid.server.model.ConfiguredObjectFactory; -import org.apache.qpid.server.model.RemoteReplicationNode; -import org.apache.qpid.server.model.State; -import org.apache.qpid.server.model.VirtualHostNode; +import org.apache.qpid.server.model.*; import org.apache.qpid.server.store.ConfiguredObjectRecordImpl; import org.apache.qpid.server.store.UnresolvedConfiguredObject; import org.apache.qpid.server.util.BrokerTestHelper; +import org.apache.qpid.server.virtualhost.berkeleydb.BDBHAVirtualHost; +import org.apache.qpid.server.virtualhost.berkeleydb.BDBHAVirtualHostImpl; +import org.apache.qpid.server.virtualhostnode.AbstractVirtualHostNode; import org.apache.qpid.test.utils.QpidTestCase; import org.apache.qpid.util.FileUtils; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.SerializationConfig; /** * Helper class to make the tests of BDB HA Virtual Host Nodes simpler and more concise. @@ -258,7 +260,9 @@ public class BDBHAVirtualHostNodeTestHelper return _broker; } - Map<String, Object> createNodeAttributes(String nodeName, String groupName, String address, String helperAddress) + public Map<String, Object> createNodeAttributes(String nodeName, String groupName, String address, + String helperAddress, String helperNodeNode, int... ports) + throws Exception { Map<String, Object> node1Attributes = new HashMap<String, Object>(); node1Attributes.put(BDBHAVirtualHostNode.ID, UUID.randomUUID()); @@ -268,13 +272,39 @@ public class BDBHAVirtualHostNodeTestHelper node1Attributes.put(BDBHAVirtualHostNode.ADDRESS, address); node1Attributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, helperAddress); node1Attributes.put(BDBHAVirtualHostNode.STORE_PATH, getMessageStorePath() + File.separator + nodeName); + node1Attributes.put(BDBHAVirtualHostNode.HELPER_NODE_NAME, helperNodeNode); - Map<String, String> repConfig = new HashMap<String, String>(); - repConfig.put(ReplicationConfig.REPLICA_ACK_TIMEOUT, "2 s"); - repConfig.put(ReplicationConfig.INSUFFICIENT_REPLICAS_TIMEOUT, "2 s"); + Map<String, String> context = new HashMap<String, String>(); + context.put(ReplicationConfig.REPLICA_ACK_TIMEOUT, "2 s"); + context.put(ReplicationConfig.INSUFFICIENT_REPLICAS_TIMEOUT, "2 s"); - node1Attributes.put(BDBHAVirtualHostNode.CONTEXT, repConfig); + if (ports != null) + { + String bluePrint = getBlueprint(ports); + context.put(AbstractVirtualHostNode.VIRTUALHOST_BLUEPRINT_CONTEXT_VAR, bluePrint); + } + + node1Attributes.put(BDBHAVirtualHostNode.CONTEXT, context); return node1Attributes; } + + public static String getBlueprint(int... ports) throws Exception + { + List<String> permittedNodes = new ArrayList<String>(); + for (int port:ports) + { + permittedNodes.add("localhost:" + port); + } + Map<String,Object> bluePrint = new HashMap<>(); + bluePrint.put(VirtualHost.TYPE, BDBHAVirtualHostImpl.VIRTUAL_HOST_TYPE); + bluePrint.put(BDBHAVirtualHost.PERMITTED_NODES, permittedNodes); + + StringWriter writer = new StringWriter(); + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); + mapper.writeValue(writer, bluePrint); + return writer.toString(); + } + } diff --git a/qpid/java/bdbstore/systests/src/main/java/org/apache/qpid/server/store/berkeleydb/BDBHAVirtualHostNodeRestTest.java b/qpid/java/bdbstore/systests/src/main/java/org/apache/qpid/server/store/berkeleydb/BDBHAVirtualHostNodeRestTest.java index cfa9557977..1e7c79f7da 100644 --- a/qpid/java/bdbstore/systests/src/main/java/org/apache/qpid/server/store/berkeleydb/BDBHAVirtualHostNodeRestTest.java +++ b/qpid/java/bdbstore/systests/src/main/java/org/apache/qpid/server/store/berkeleydb/BDBHAVirtualHostNodeRestTest.java @@ -41,6 +41,7 @@ import org.apache.qpid.server.model.VirtualHost; import org.apache.qpid.server.model.VirtualHostNode; import org.apache.qpid.server.store.berkeleydb.replication.ReplicatedEnvironmentFacade; import org.apache.qpid.server.virtualhost.berkeleydb.BDBHAVirtualHost; +import org.apache.qpid.server.virtualhostnode.AbstractVirtualHostNode; import org.apache.qpid.server.virtualhostnode.berkeleydb.BDBHARemoteReplicationNode; import org.apache.qpid.server.virtualhostnode.berkeleydb.BDBHAVirtualHostNode; import org.apache.qpid.systest.rest.Asserts; @@ -77,6 +78,7 @@ public class BDBHAVirtualHostNodeRestTest extends QpidRestTestCase _node2HaPort = getNextAvailable(_node1HaPort + 1); _node3HaPort = getNextAvailable(_node2HaPort + 1); + } @Override @@ -302,7 +304,7 @@ public class BDBHAVirtualHostNodeRestTest extends QpidRestTestCase assertEquals("Unexpected response code for virtual host node " + nodeName + " creation request", 201, responseCode); } - private Map<String, Object> createNodeAttributeMap(String nodeName, int nodePort, int helperPort) + private Map<String, Object> createNodeAttributeMap(String nodeName, int nodePort, int helperPort) throws Exception { Map<String, Object> nodeData = new HashMap<String, Object>(); nodeData.put(BDBHAVirtualHostNode.NAME, nodeName); @@ -311,6 +313,11 @@ public class BDBHAVirtualHostNodeRestTest extends QpidRestTestCase nodeData.put(BDBHAVirtualHostNode.GROUP_NAME, _hostName); nodeData.put(BDBHAVirtualHostNode.ADDRESS, "localhost:" + nodePort); nodeData.put(BDBHAVirtualHostNode.HELPER_ADDRESS, "localhost:" + helperPort); + nodeData.put(BDBHAVirtualHostNode.HELPER_NODE_NAME, NODE1); + Map<String,String> context = new HashMap<>(); + nodeData.put(BDBHAVirtualHostNode.CONTEXT, context); + String bluePrint = HATestClusterCreator.getBlueprint("localhost", _node1HaPort, _node2HaPort, _node3HaPort); + context.put(AbstractVirtualHostNode.VIRTUALHOST_BLUEPRINT_CONTEXT_VAR, bluePrint); return nodeData; } @@ -431,4 +438,6 @@ public class BDBHAVirtualHostNodeRestTest extends QpidRestTestCase assertNotNull("Could not find new master", newMasterData); return newMasterData; } + + } diff --git a/qpid/java/bdbstore/systests/src/main/java/org/apache/qpid/server/store/berkeleydb/BDBHAVirtualHostRestTest.java b/qpid/java/bdbstore/systests/src/main/java/org/apache/qpid/server/store/berkeleydb/BDBHAVirtualHostRestTest.java index e5d631b452..334544e334 100644 --- a/qpid/java/bdbstore/systests/src/main/java/org/apache/qpid/server/store/berkeleydb/BDBHAVirtualHostRestTest.java +++ b/qpid/java/bdbstore/systests/src/main/java/org/apache/qpid/server/store/berkeleydb/BDBHAVirtualHostRestTest.java @@ -35,6 +35,7 @@ import org.apache.qpid.server.model.State; import org.apache.qpid.server.model.VirtualHost; import org.apache.qpid.server.model.VirtualHostNode; import org.apache.qpid.server.store.berkeleydb.replication.ReplicatedEnvironmentFacade; +import org.apache.qpid.server.virtualhostnode.AbstractVirtualHostNode; import org.apache.qpid.server.virtualhostnode.berkeleydb.BDBHAVirtualHostNode; import org.apache.qpid.systest.rest.Asserts; import org.apache.qpid.systest.rest.QpidRestTestCase; @@ -48,6 +49,7 @@ public class BDBHAVirtualHostRestTest extends QpidRestTestCase private int _nodeHaPort; private Object _nodeName; private String _virtualhostUrl; + private String _bluePrint; @Override public void setUp() throws Exception @@ -58,6 +60,7 @@ public class BDBHAVirtualHostRestTest extends QpidRestTestCase _storeBaseDir = new File(TMP_FOLDER, "store-" + _hostName + "-" + System.currentTimeMillis()); _nodeHaPort = getNextAvailable(getRestTestHelper().getHttpPort() + 1); _virtualhostUrl = "virtualhost/" + _nodeName + "/" + _hostName; + _bluePrint = HATestClusterCreator.getBlueprint("localhost", _nodeHaPort); super.setUp(); } @@ -93,6 +96,11 @@ public class BDBHAVirtualHostRestTest extends QpidRestTestCase nodeAttributes.put(BDBHAVirtualHostNode.GROUP_NAME, _hostName); nodeAttributes.put(BDBHAVirtualHostNode.ADDRESS, "localhost:" + _nodeHaPort); nodeAttributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, "localhost:" + _nodeHaPort); + nodeAttributes.put(BDBHAVirtualHostNode.HELPER_NODE_NAME, _nodeName); + Map<String, String> context = new HashMap<String,String>(); + context.put(AbstractVirtualHostNode.VIRTUALHOST_BLUEPRINT_CONTEXT_VAR, _bluePrint); + + nodeAttributes.put(BDBHAVirtualHostNode.CONTEXT, context); config.addObjectConfiguration(VirtualHostNode.class, nodeAttributes); } diff --git a/qpid/java/bdbstore/systests/src/main/java/org/apache/qpid/server/store/berkeleydb/HATestClusterCreator.java b/qpid/java/bdbstore/systests/src/main/java/org/apache/qpid/server/store/berkeleydb/HATestClusterCreator.java index 209a5145e2..ebc32b482a 100644 --- a/qpid/java/bdbstore/systests/src/main/java/org/apache/qpid/server/store/berkeleydb/HATestClusterCreator.java +++ b/qpid/java/bdbstore/systests/src/main/java/org/apache/qpid/server/store/berkeleydb/HATestClusterCreator.java @@ -21,8 +21,10 @@ package org.apache.qpid.server.store.berkeleydb; import java.io.File; import java.io.IOException; +import java.io.StringWriter; import java.net.InetAddress; import java.net.UnknownHostException; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -47,8 +49,11 @@ import org.apache.qpid.client.AMQConnectionURL; import org.apache.qpid.server.management.plugin.HttpManagement; import org.apache.qpid.server.model.Plugin; import org.apache.qpid.server.model.Port; +import org.apache.qpid.server.model.VirtualHost; import org.apache.qpid.server.model.VirtualHostNode; -import org.apache.qpid.server.virtualhostnode.JsonVirtualHostNode; +import org.apache.qpid.server.virtualhost.berkeleydb.BDBHAVirtualHost; +import org.apache.qpid.server.virtualhost.berkeleydb.BDBHAVirtualHostImpl; +import org.apache.qpid.server.virtualhostnode.AbstractVirtualHostNode; import org.apache.qpid.server.virtualhostnode.berkeleydb.BDBHARemoteReplicationNode; import org.apache.qpid.server.virtualhostnode.berkeleydb.BDBHAVirtualHostNode; import org.apache.qpid.server.virtualhostnode.berkeleydb.BDBHAVirtualHostNodeImpl; @@ -56,6 +61,8 @@ import org.apache.qpid.systest.rest.RestTestHelper; import org.apache.qpid.test.utils.QpidBrokerTestCase; import org.apache.qpid.test.utils.TestBrokerConfiguration; import org.apache.qpid.url.URLSyntaxException; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.SerializationConfig; import org.junit.Assert; import com.sleepycat.je.rep.ReplicationConfig; @@ -101,11 +108,22 @@ public class HATestClusterCreator { int brokerPort = _testcase.findFreePort(); + int[] bdbPorts = new int[_numberOfNodes]; for (int i = 0; i < _numberOfNodes; i++) { int bdbPort = _testcase.getNextAvailable(brokerPort + 1); + bdbPorts[i] = bdbPort; _brokerPortToBdbPortMap.put(brokerPort, bdbPort); + brokerPort = _testcase.getNextAvailable(bdbPort + 1); + } + + String bluePrintJson = getBlueprint(_ipAddressOfBroker, bdbPorts); + String helperName = null; + for (Map.Entry<Integer,Integer> entry: _brokerPortToBdbPortMap.entrySet()) + { + brokerPort = entry.getKey(); + int bdbPort = entry.getValue(); LOGGER.debug("Cluster broker port " + brokerPort + ", bdb replication port " + bdbPort); if (_bdbHelperPort == 0) { @@ -113,6 +131,10 @@ public class HATestClusterCreator } String nodeName = getNodeNameForNodeAt(bdbPort); + if (helperName == null) + { + helperName = nodeName; + } Map<String, Object> virtualHostNodeAttributes = new HashMap<String, Object>(); virtualHostNodeAttributes.put(BDBHAVirtualHostNode.STORE_PATH, System.getProperty("QPID_WORK") + File.separator + brokerPort); @@ -121,11 +143,13 @@ public class HATestClusterCreator virtualHostNodeAttributes.put(BDBHAVirtualHostNode.ADDRESS, getNodeHostPortForNodeAt(bdbPort)); virtualHostNodeAttributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, getHelperHostPort()); virtualHostNodeAttributes.put(BDBHAVirtualHostNode.TYPE, BDBHAVirtualHostNodeImpl.VIRTUAL_HOST_NODE_TYPE); + virtualHostNodeAttributes.put(BDBHAVirtualHostNode.HELPER_NODE_NAME, helperName); - Map<String, String> repSettings = new HashMap<>(); - repSettings.put(ReplicationConfig.INSUFFICIENT_REPLICAS_TIMEOUT, "2 s"); - repSettings.put(ReplicationConfig.ELECTIONS_PRIMARY_RETRIES, "0"); - virtualHostNodeAttributes.put(BDBHAVirtualHostNode.CONTEXT, repSettings); + Map<String, String> context = new HashMap<>(); + context.put(ReplicationConfig.INSUFFICIENT_REPLICAS_TIMEOUT, "2 s"); + context.put(ReplicationConfig.ELECTIONS_PRIMARY_RETRIES, "0"); + context.put(AbstractVirtualHostNode.VIRTUALHOST_BLUEPRINT_CONTEXT_VAR, bluePrintJson); + virtualHostNodeAttributes.put(BDBHAVirtualHostNode.CONTEXT, context); TestBrokerConfiguration brokerConfiguration = _testcase.getBrokerConfiguration(brokerPort); brokerConfiguration.addJmxManagementConfiguration(); @@ -134,7 +158,6 @@ public class HATestClusterCreator brokerConfiguration.setObjectAttribute(Port.class, TestBrokerConfiguration.ENTRY_NAME_HTTP_PORT, Port.PORT, _testcase.getHttpManagementPort(brokerPort)); brokerConfiguration.setObjectAttributes(VirtualHostNode.class, _virtualHostName, virtualHostNodeAttributes); - brokerPort = _testcase.getNextAvailable(bdbPort + 1); } _primaryBrokerPort = getPrimaryBrokerPort(); } @@ -485,4 +508,22 @@ public class HATestClusterCreator helper.setUsernameAndPassword("webadmin", "webadmin"); return helper; } + + public static String getBlueprint(String hostName, int... ports) throws Exception + { + List<String> permittedNodes = new ArrayList<String>(); + for (int port:ports) + { + permittedNodes.add(hostName + ":" + port); + } + Map<String,Object> bluePrint = new HashMap<>(); + bluePrint.put(VirtualHost.TYPE, BDBHAVirtualHostImpl.VIRTUAL_HOST_TYPE); + bluePrint.put(BDBHAVirtualHost.PERMITTED_NODES, permittedNodes); + + StringWriter writer = new StringWriter(); + ObjectMapper mapper = new ObjectMapper(); + mapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true); + mapper.writeValue(writer, bluePrint); + return writer.toString(); + } } |
