Search in sources :

Example 6 with CountDownSessionFailureListener

use of org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener in project activemq-artemis by apache.

the class FailBackAutoTest method testAutoFailback.

@Test
public void testAutoFailback() throws Exception {
    ((SharedStoreSlavePolicy) backupServer.getServer().getHAPolicy()).setRestartBackup(false);
    createSessionFactory();
    final CountDownLatch latch = new CountDownLatch(1);
    ClientSession session = sendAndConsume(sf, true);
    CountDownSessionFailureListener listener = new CountDownSessionFailureListener(latch, session);
    session.addFailureListener(listener);
    liveServer.crash();
    assertTrue(latch.await(5, TimeUnit.SECONDS));
    log.info("backup (nowLive) topology = " + backupServer.getServer().getClusterManager().getDefaultConnection(null).getTopology().describe());
    log.info("Server Crash!!!");
    ClientProducer producer = session.createProducer(ADDRESS);
    ClientMessage message = session.createMessage(true);
    setBody(0, message);
    producer.send(message);
    verifyMessageOnServer(1, 1);
    session.removeFailureListener(listener);
    final CountDownLatch latch2 = new CountDownLatch(1);
    listener = new CountDownSessionFailureListener(latch2, session);
    session.addFailureListener(listener);
    log.info("******* starting live server back");
    liveServer.start();
    Thread.sleep(1000);
    System.out.println("After failback: " + locator.getTopology().describe());
    assertTrue(latch2.await(5, TimeUnit.SECONDS));
    message = session.createMessage(true);
    setBody(1, message);
    producer.send(message);
    session.close();
    verifyMessageOnServer(0, 1);
    wrapUpSessionFactory();
}
Also used : SharedStoreSlavePolicy(org.apache.activemq.artemis.core.server.cluster.ha.SharedStoreSlavePolicy) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownLatch(java.util.concurrent.CountDownLatch) CountDownSessionFailureListener(org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) Test(org.junit.Test)

Example 7 with CountDownSessionFailureListener

use of org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener in project activemq-artemis by apache.

the class FailBackAutoTest method testFailBack.

/**
 * Basic fail-back test.
 *
 * @throws Exception
 */
@Test
public void testFailBack() throws Exception {
    ((SharedStoreSlavePolicy) backupServer.getServer().getHAPolicy()).setRestartBackup(false);
    createSessionFactory();
    ClientSession session = sendAndConsume(sf, true);
    ClientProducer producer = session.createProducer(ADDRESS);
    sendMessages(session, producer, NUM_MESSAGES);
    session.commit();
    crash(session);
    session.start();
    ClientConsumer consumer = session.createConsumer(ADDRESS);
    receiveMessages(consumer, 0, NUM_MESSAGES, true);
    producer = session.createProducer(ADDRESS);
    sendMessages(session, producer, 2 * NUM_MESSAGES);
    session.commit();
    assertFalse("must NOT be a backup", liveServer.getServer().getHAPolicy().isBackup());
    adaptLiveConfigForReplicatedFailBack(liveServer);
    CountDownSessionFailureListener listener = new CountDownSessionFailureListener(session);
    session.addFailureListener(listener);
    liveServer.start();
    assertTrue(listener.getLatch().await(5, TimeUnit.SECONDS));
    assertTrue("live initialized after restart", liveServer.getServer().waitForActivation(15, TimeUnit.SECONDS));
    session.start();
    receiveMessages(consumer, 0, NUM_MESSAGES, true);
}
Also used : SharedStoreSlavePolicy(org.apache.activemq.artemis.core.server.cluster.ha.SharedStoreSlavePolicy) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) CountDownSessionFailureListener(org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener) Test(org.junit.Test)

Example 8 with CountDownSessionFailureListener

use of org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener in project activemq-artemis by apache.

the class AsynchronousFailoverTest method doTestTransactional.

private void doTestTransactional(final TestRunner runner) throws Throwable {
    // For duplication detection
    int executionId = 0;
    while (!runner.isFailed()) {
        ClientSession session = null;
        executionId++;
        log.info("#test doTestTransactional starting now. Execution " + executionId);
        try {
            boolean retry = false;
            final int numMessages = 1000;
            session = sf.createSession(false, false);
            listener = new CountDownSessionFailureListener(session);
            session.addFailureListener(listener);
            do {
                try {
                    ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);
                    for (int i = 0; i < numMessages; i++) {
                        ClientMessage message = session.createMessage(true);
                        message.getBodyBuffer().writeString("message" + i);
                        message.putIntProperty("counter", i);
                        message.putStringProperty(Message.HDR_DUPLICATE_DETECTION_ID, new SimpleString("id:" + i + ",exec:" + executionId));
                        addPayload(message);
                        if (log.isDebugEnabled()) {
                            log.debug("Sending message " + message);
                        }
                        producer.send(message);
                    }
                    log.debug("Sending commit");
                    session.commit();
                    retry = false;
                } catch (ActiveMQDuplicateIdException die) {
                    logAndSystemOut("#test duplicate id rejected on sending");
                    break;
                } catch (ActiveMQTransactionRolledBackException trbe) {
                    log.info("#test transaction rollback retrying on sending");
                    // OK
                    retry = true;
                } catch (ActiveMQUnBlockedException ube) {
                    log.info("#test transaction rollback retrying on sending");
                    // OK
                    retry = true;
                } catch (ActiveMQTransactionOutcomeUnknownException toue) {
                    log.info("#test transaction rollback retrying on sending");
                    // OK
                    retry = true;
                } catch (ActiveMQException e) {
                    log.info("#test Exception " + e, e);
                    throw e;
                }
            } while (retry);
            logAndSystemOut("#test Finished sending, starting consumption now");
            boolean blocked = false;
            retry = false;
            ClientConsumer consumer = null;
            do {
                ArrayList<Integer> msgs = new ArrayList<>();
                try {
                    if (consumer == null) {
                        consumer = session.createConsumer(FailoverTestBase.ADDRESS);
                        session.start();
                    }
                    for (int i = 0; i < numMessages; i++) {
                        if (log.isDebugEnabled()) {
                            log.debug("Consumer receiving message " + i);
                        }
                        ClientMessage message = consumer.receive(10000);
                        if (message == null) {
                            break;
                        }
                        if (log.isDebugEnabled()) {
                            log.debug("Received message " + message);
                        }
                        int count = message.getIntProperty("counter");
                        if (count != i) {
                            log.warn("count was received out of order, " + count + "!=" + i);
                        }
                        msgs.add(count);
                        message.acknowledge();
                    }
                    log.info("#test commit");
                    try {
                        session.commit();
                    } catch (ActiveMQTransactionRolledBackException trbe) {
                        // we know the tx has been rolled back so we just consume again
                        retry = true;
                        continue;
                    } catch (ActiveMQException e) {
                        // This could eventually happen
                        // We will get rid of this when we implement 2 phase commit on failover
                        log.warn("exception during commit, it will be ignored for now" + e.getMessage(), e);
                    }
                    try {
                        if (blocked) {
                            assertTrue("msgs.size is expected to be 0 or " + numMessages + " but it was " + msgs.size(), msgs.size() == 0 || msgs.size() == numMessages);
                        } else {
                            assertTrue("msgs.size is expected to be " + numMessages + " but it was " + msgs.size(), msgs.size() == numMessages);
                        }
                    } catch (Throwable e) {
                        log.info(threadDump("Thread dump, messagesReceived = " + msgs.size()));
                        logAndSystemOut(e.getMessage() + " messages received");
                        for (Integer msg : msgs) {
                            logAndSystemOut(msg.toString());
                        }
                        throw e;
                    }
                    int i = 0;
                    for (Integer msg : msgs) {
                        assertEquals(i++, (int) msg);
                    }
                    retry = false;
                    blocked = false;
                } catch (ActiveMQTransactionRolledBackException trbe) {
                    logAndSystemOut("Transaction rolled back with " + msgs.size(), trbe);
                    // TODO: https://jira.jboss.org/jira/browse/HORNETQ-369
                    // ATM RolledBack exception is being called with the transaction is committed.
                    // the test will fail if you remove this next line
                    blocked = true;
                    retry = true;
                } catch (ActiveMQTransactionOutcomeUnknownException tou) {
                    logAndSystemOut("Transaction rolled back with " + msgs.size(), tou);
                    // TODO: https://jira.jboss.org/jira/browse/HORNETQ-369
                    // ATM RolledBack exception is being called with the transaction is committed.
                    // the test will fail if you remove this next line
                    blocked = true;
                    retry = true;
                } catch (ActiveMQUnBlockedException ube) {
                    logAndSystemOut("Unblocked with " + msgs.size(), ube);
                    // TODO: https://jira.jboss.org/jira/browse/HORNETQ-369
                    // This part of the test is never being called.
                    blocked = true;
                    retry = true;
                } catch (ActiveMQException e) {
                    logAndSystemOut(e.getMessage(), e);
                    throw e;
                }
            } while (retry);
        } finally {
            if (session != null) {
                session.close();
            }
        }
        listener = null;
    }
}
Also used : ActiveMQTransactionOutcomeUnknownException(org.apache.activemq.artemis.api.core.ActiveMQTransactionOutcomeUnknownException) ArrayList(java.util.ArrayList) SimpleString(org.apache.activemq.artemis.api.core.SimpleString) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) CountDownSessionFailureListener(org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener) ActiveMQUnBlockedException(org.apache.activemq.artemis.api.core.ActiveMQUnBlockedException) ActiveMQException(org.apache.activemq.artemis.api.core.ActiveMQException) ActiveMQTransactionRolledBackException(org.apache.activemq.artemis.api.core.ActiveMQTransactionRolledBackException) ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ActiveMQDuplicateIdException(org.apache.activemq.artemis.api.core.ActiveMQDuplicateIdException) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer)

Example 9 with CountDownSessionFailureListener

use of org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener in project activemq-artemis by apache.

the class ReplicatedDistributionTest method fail.

/**
 * @param session
 * @throws InterruptedException
 */
private void fail(final ClientSession session) throws InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    session.addFailureListener(new CountDownSessionFailureListener(latch, session));
    RemotingConnection conn = ((ClientSessionInternal) session).getConnection();
    // Simulate failure on connection
    conn.fail(new ActiveMQNotConnectedException());
    // Wait to be informed of failure
    boolean ok = latch.await(1000, TimeUnit.MILLISECONDS);
    Assert.assertTrue(ok);
}
Also used : ClientSessionInternal(org.apache.activemq.artemis.core.client.impl.ClientSessionInternal) RemotingConnection(org.apache.activemq.artemis.spi.core.protocol.RemotingConnection) ActiveMQNotConnectedException(org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException) CountDownLatch(java.util.concurrent.CountDownLatch) CountDownSessionFailureListener(org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener)

Example 10 with CountDownSessionFailureListener

use of org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener in project activemq-artemis by apache.

the class FailoverTest method testLiveAndBackupBackupComesBackNewFactory.

@Test(timeout = 120000)
public void testLiveAndBackupBackupComesBackNewFactory() throws Exception {
    locator.setBlockOnNonDurableSend(true).setBlockOnDurableSend(true).setFailoverOnInitialConnection(true).setReconnectAttempts(300).setRetryInterval(100);
    sf = createSessionFactoryAndWaitForTopology(locator, 2);
    ClientSession session = sendAndConsume(sf, true);
    CountDownSessionFailureListener listener = new CountDownSessionFailureListener(session);
    session.addFailureListener(listener);
    backupServer.stop();
    liveServer.crash();
    // To reload security or other settings that are read during startup
    beforeRestart(backupServer);
    if (!backupServer.getServer().getHAPolicy().isSharedStore()) {
        // XXX
        // this test would not make sense in the remote replication use case, without the following
        backupServer.getServer().setHAPolicy(new SharedStoreMasterPolicy());
    }
    backupServer.start();
    Assert.assertTrue("session failure listener", listener.getLatch().await(5, TimeUnit.SECONDS));
    ClientProducer producer = session.createProducer(FailoverTestBase.ADDRESS);
    ClientMessage message = session.createMessage(true);
    setBody(0, message);
    producer.send(message);
    session.close();
    sf.close();
    createClientSessionFactory();
    session = createSession(sf);
    ClientConsumer cc = session.createConsumer(FailoverTestBase.ADDRESS);
    session.start();
    ClientMessage cm = cc.receive(5000);
    Assert.assertNotNull(cm);
    Assert.assertEquals("message0", cm.getBodyBuffer().readString());
}
Also used : ClientSession(org.apache.activemq.artemis.api.core.client.ClientSession) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ClientConsumer(org.apache.activemq.artemis.api.core.client.ClientConsumer) CountDownSessionFailureListener(org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener) ClientProducer(org.apache.activemq.artemis.api.core.client.ClientProducer) SharedStoreMasterPolicy(org.apache.activemq.artemis.core.server.cluster.ha.SharedStoreMasterPolicy) Test(org.junit.Test)

Aggregations

CountDownSessionFailureListener (org.apache.activemq.artemis.tests.util.CountDownSessionFailureListener)13 ClientSession (org.apache.activemq.artemis.api.core.client.ClientSession)11 ClientProducer (org.apache.activemq.artemis.api.core.client.ClientProducer)10 ClientMessage (org.apache.activemq.artemis.api.core.client.ClientMessage)9 Test (org.junit.Test)9 CountDownLatch (java.util.concurrent.CountDownLatch)5 ClientConsumer (org.apache.activemq.artemis.api.core.client.ClientConsumer)5 SharedStoreSlavePolicy (org.apache.activemq.artemis.core.server.cluster.ha.SharedStoreSlavePolicy)3 ArrayList (java.util.ArrayList)2 ActiveMQException (org.apache.activemq.artemis.api.core.ActiveMQException)2 ActiveMQUnBlockedException (org.apache.activemq.artemis.api.core.ActiveMQUnBlockedException)2 ActiveMQDuplicateIdException (org.apache.activemq.artemis.api.core.ActiveMQDuplicateIdException)1 ActiveMQNotConnectedException (org.apache.activemq.artemis.api.core.ActiveMQNotConnectedException)1 ActiveMQTransactionOutcomeUnknownException (org.apache.activemq.artemis.api.core.ActiveMQTransactionOutcomeUnknownException)1 ActiveMQTransactionRolledBackException (org.apache.activemq.artemis.api.core.ActiveMQTransactionRolledBackException)1 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)1 ClientSessionFactoryInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryInternal)1 ClientSessionInternal (org.apache.activemq.artemis.core.client.impl.ClientSessionInternal)1 ClusterManager (org.apache.activemq.artemis.core.server.cluster.ClusterManager)1 SharedStoreMasterPolicy (org.apache.activemq.artemis.core.server.cluster.ha.SharedStoreMasterPolicy)1