Search in sources :

Example 1 with PortHelper

use of org.apache.qpid.test.utils.PortHelper in project qpid-broker-j by apache.

the class MultiNodeTest method testClusterCannotStartWithIntruder.

@Test
public void testClusterCannotStartWithIntruder() throws Exception {
    int intruderPort = new PortHelper().getNextAvailable(Arrays.stream(getBrokerAdmin().getBdbPorts()).max().getAsInt() + 1);
    String nodeName = "intruder";
    String nodeHostPort = getBrokerAdmin().getHost() + ":" + intruderPort;
    File environmentPathFile = Files.createTempDirectory("qpid-work-intruder").toFile();
    try {
        environmentPathFile.mkdirs();
        ReplicationConfig replicationConfig = new ReplicationConfig("test", nodeName, nodeHostPort);
        replicationConfig.setHelperHosts(getBrokerAdmin().getHelperHostPort());
        EnvironmentConfig envConfig = new EnvironmentConfig();
        envConfig.setAllowCreate(true);
        envConfig.setTransactional(true);
        envConfig.setDurability(new Durability(Durability.SyncPolicy.SYNC, Durability.SyncPolicy.WRITE_NO_SYNC, Durability.ReplicaAckPolicy.SIMPLE_MAJORITY));
        final String currentThreadName = Thread.currentThread().getName();
        try (ReplicatedEnvironment intruder = new ReplicatedEnvironment(environmentPathFile, replicationConfig, envConfig)) {
            LOGGER.debug("Intruder started");
        } finally {
            Thread.currentThread().setName(currentThreadName);
        }
        Set<Integer> ports = Arrays.stream(getBrokerAdmin().getGroupAmqpPorts()).boxed().collect(Collectors.toSet());
        for (int port : ports) {
            getBrokerAdmin().awaitNodeToAttainAttributeValue(port, BDBHAVirtualHostNode.STATE, State.ERRORED.name());
        }
        getBrokerAdmin().stop();
        try {
            getBrokerAdmin().start();
            fail("Cluster cannot start with an intruder node");
        } catch (Exception e) {
        // pass
        }
    } finally {
        FileUtils.delete(environmentPathFile, true);
    }
}
Also used : ReplicationConfig(com.sleepycat.je.rep.ReplicationConfig) EnvironmentConfig(com.sleepycat.je.EnvironmentConfig) PortHelper(org.apache.qpid.test.utils.PortHelper) Durability(com.sleepycat.je.Durability) File(java.io.File) ReplicatedEnvironment(com.sleepycat.je.rep.ReplicatedEnvironment) JMSException(javax.jms.JMSException) TransactionRolledBackException(javax.jms.TransactionRolledBackException) Test(org.junit.Test)

Example 2 with PortHelper

use of org.apache.qpid.test.utils.PortHelper in project qpid-broker-j by apache.

the class GroupBrokerAdmin method initializeGroupData.

private GroupMember[] initializeGroupData(final String groupName, final SpawnBrokerAdmin[] admins) {
    PortHelper helper = new PortHelper();
    int[] ports = new int[admins.length];
    String[] addresses = new String[admins.length];
    int port = -1;
    for (int i = 0; i < admins.length; i++) {
        port = i == 0 ? helper.getNextAvailable() : helper.getNextAvailable(port + 1);
        addresses[i] = HOST + ":" + port;
        ports[i] = port;
    }
    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, "{\"type\":\"BDB_HA\"}");
    String permitted = objectToJson(addresses);
    String contextAsString = objectToJson(context);
    GroupMember[] members = new GroupMember[admins.length];
    for (int i = 0; i < admins.length; i++) {
        String nodeName = "node-" + ports[i];
        Map<String, Object> nodeAttributes = new HashMap<>();
        nodeAttributes.put(BDBHAVirtualHostNode.GROUP_NAME, groupName);
        nodeAttributes.put(BDBHAVirtualHostNode.NAME, nodeName);
        nodeAttributes.put(BDBHAVirtualHostNode.ADDRESS, addresses[i]);
        nodeAttributes.put(BDBHAVirtualHostNode.TYPE, BDBHAVirtualHostNodeImpl.VIRTUAL_HOST_NODE_TYPE);
        nodeAttributes.put(BDBHAVirtualHostNode.DEFAULT_VIRTUAL_HOST_NODE, true);
        nodeAttributes.put(BDBHAVirtualHostNode.HELPER_ADDRESS, addresses[0]);
        if (i > 0) {
            nodeAttributes.put(BDBHAVirtualHostNode.HELPER_NODE_NAME, "node-" + ports[0]);
        }
        nodeAttributes.put(BDBHAVirtualHostNode.PERMITTED_NODES, permitted);
        nodeAttributes.put(BDBHAVirtualHostNode.CONTEXT, contextAsString);
        members[i] = new GroupMember(nodeName, admins[i].getBrokerAddress(BrokerAdmin.PortType.AMQP).getPort(), port, admins[i], nodeAttributes);
    }
    return members;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PortHelper(org.apache.qpid.test.utils.PortHelper)

Aggregations

PortHelper (org.apache.qpid.test.utils.PortHelper)2 Durability (com.sleepycat.je.Durability)1 EnvironmentConfig (com.sleepycat.je.EnvironmentConfig)1 ReplicatedEnvironment (com.sleepycat.je.rep.ReplicatedEnvironment)1 ReplicationConfig (com.sleepycat.je.rep.ReplicationConfig)1 File (java.io.File)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 JMSException (javax.jms.JMSException)1 TransactionRolledBackException (javax.jms.TransactionRolledBackException)1 Test (org.junit.Test)1