Search in sources :

Example 1 with ReusableLatch

use of org.apache.activemq.artemis.utils.ReusableLatch in project activemq-artemis by apache.

the class SyncCalculation method syncTest.

/**
 * It will perform {@code tries} write tests of {@code blockSize * blocks} bytes and returning the lowest elapsed time to perform a try.
 *
 * <p>
 * Please configure {@code blocks >= -XX:CompileThreshold} (ie by default on most JVMs is 10000) to favour the best JIT/OSR compilation (ie: Just In Time/On Stack Replacement)
 * if the test is running on a temporary file-system (eg: tmpfs on Linux) or without {@code fsync}.
 * <p>
 * NOTE: The write latencies are provided only if {@code verbose && !(journalType == JournalType.ASYNCIO && !syncWrites)} (ie are used effective synchronous writes).
 *
 * @param datafolder  the folder where the journal files will be stored
 * @param blockSize   the size in bytes of each write on the journal
 * @param blocks      the number of {@code blockSize} writes performed on each try
 * @param tries       the number of tests
 * @param verbose     {@code true} to make the output verbose, {@code false} otherwise
 * @param fsync       if {@code true} the test is performing full durable writes, {@code false} otherwise
 * @param syncWrites  if {@code true} each write is performed only if the previous one is completed, {@code false} otherwise (ie each try will wait only the last write)
 * @param fileName    the name of the journal file used for the test
 * @param maxAIO      the max number of in-flight IO requests (if {@code journalType} will support it)
 * @param journalType the {@link JournalType} used for the tests
 * @return the lowest elapsed time (in {@link TimeUnit#MILLISECONDS}) to perform a try
 * @throws Exception
 */
public static long syncTest(File datafolder, int blockSize, int blocks, int tries, boolean verbose, boolean fsync, boolean syncWrites, String fileName, int maxAIO, JournalType journalType) throws Exception {
    SequentialFileFactory factory = newFactory(datafolder, fsync, journalType, blockSize * blocks, maxAIO);
    final boolean asyncWrites = journalType == JournalType.ASYNCIO && !syncWrites;
    // the write latencies could be taken only when writes are effectively synchronous
    final Histogram writeLatencies = (verbose && !asyncWrites) ? new Histogram(MAX_FLUSH_NANOS, 2) : null;
    if (journalType == JournalType.ASYNCIO && syncWrites) {
        System.out.println();
        System.out.println("*******************************************************************************************");
        System.out.println("*** Notice: The recommendation for AsyncIO journal is to not use --sync-writes          ***");
        System.out.println("***         The measures here will be useful to understand your device                  ***");
        System.out.println("***         however the result here won't represent the best configuration option       ***");
        System.out.println("*******************************************************************************************");
        System.out.println();
    }
    if (verbose) {
        System.out.println("Using " + factory.getClass().getName() + " to calculate sync times, alignment=" + factory.getAlignment());
        if (writeLatencies == null) {
            System.out.println("*** Use --sync-writes if you want to see a histogram for each write performed ***");
        }
    }
    SequentialFile file = factory.createSequentialFile(fileName);
    // to be sure that a process/thread crash won't leave the dataFolder with garbage files
    file.getJavaFile().deleteOnExit();
    try {
        final ByteBuffer bufferBlock = allocateAlignedBlock(blockSize, factory);
        // making sure the blockSize matches the device
        blockSize = bufferBlock.remaining();
        file.delete();
        file.open();
        file.fill(blockSize * blocks);
        file.close();
        long[] result = new long[tries];
        final ReusableLatch latch = new ReusableLatch(0);
        IOCallback callback = new IOCallback() {

            @Override
            public void done() {
                latch.countDown();
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
            }
        };
        DecimalFormat dcformat = new DecimalFormat("###.##");
        for (int ntry = 0; ntry < tries; ntry++) {
            if (verbose) {
                System.out.println("**************************************************");
                System.out.println(ntry + " of " + tries + " calculation");
            }
            file.open();
            file.position(0);
            long start = System.currentTimeMillis();
            for (int i = 0; i < blocks; i++) {
                bufferBlock.position(0);
                latch.countUp();
                long startWrite = 0;
                if (writeLatencies != null) {
                    startWrite = System.nanoTime();
                }
                file.writeDirect(bufferBlock, true, callback);
                if (syncWrites) {
                    flushLatch(latch);
                }
                if (writeLatencies != null) {
                    final long elapsedWriteNanos = System.nanoTime() - startWrite;
                    writeLatencies.recordValue(elapsedWriteNanos);
                }
            }
            if (!syncWrites)
                flushLatch(latch);
            long end = System.currentTimeMillis();
            result[ntry] = (end - start);
            if (verbose) {
                double writesPerMillisecond = (double) blocks / (double) result[ntry];
                System.out.println("Time = " + result[ntry] + " milliseconds");
                System.out.println("Writes / millisecond = " + dcformat.format(writesPerMillisecond));
                System.out.println("bufferTimeout = " + toNanos(result[ntry], blocks, verbose));
                System.out.println("**************************************************");
            }
            file.close();
            if (ntry == 0 && writeLatencies != null) {
                // discarding the first one.. some warmup time
                writeLatencies.reset();
            }
        }
        factory.releaseDirectBuffer(bufferBlock);
        if (writeLatencies != null) {
            System.out.println("Write Latencies Percentile Distribution in microseconds");
            // print latencies in us -> (ns * 1000d)
            System.out.println("*****************************************************************");
            writeLatencies.outputPercentileDistribution(System.out, 1000d);
            System.out.println();
            System.out.println("*****************************************************************");
            System.out.println("*** this may be useful to generate charts if you like charts: ***");
            System.out.println("*** http://hdrhistogram.github.io/HdrHistogram/plotFiles.html ***");
            System.out.println("*****************************************************************");
            System.out.println();
            writeLatencies.reset();
        }
        long totalTime = Long.MAX_VALUE;
        for (int i = 0; i < tries; i++) {
            if (result[i] < totalTime) {
                totalTime = result[i];
            }
        }
        return totalTime;
    } finally {
        try {
            file.close();
        } catch (Exception e) {
        }
        try {
            file.delete();
        } catch (Exception e) {
        }
        try {
            factory.stop();
        } catch (Exception e) {
        }
    }
}
Also used : SequentialFile(org.apache.activemq.artemis.core.io.SequentialFile) Histogram(org.HdrHistogram.Histogram) DecimalFormat(java.text.DecimalFormat) ByteBuffer(java.nio.ByteBuffer) IOCallback(org.apache.activemq.artemis.core.io.IOCallback) NIOSequentialFileFactory(org.apache.activemq.artemis.core.io.nio.NIOSequentialFileFactory) MappedSequentialFileFactory(org.apache.activemq.artemis.core.io.mapped.MappedSequentialFileFactory) AIOSequentialFileFactory(org.apache.activemq.artemis.core.io.aio.AIOSequentialFileFactory) SequentialFileFactory(org.apache.activemq.artemis.core.io.SequentialFileFactory) IOException(java.io.IOException) ReusableLatch(org.apache.activemq.artemis.utils.ReusableLatch)

Example 2 with ReusableLatch

use of org.apache.activemq.artemis.utils.ReusableLatch in project activemq-artemis by apache.

the class ReusableLatchTest method testLatchWithParameterizedDown.

@Test
public void testLatchWithParameterizedDown() throws Exception {
    ReusableLatch latch = new ReusableLatch(1000);
    latch.countDown(5000);
    assertTrue(latch.await(1000));
    assertEquals(0, latch.getCount());
}
Also used : ReusableLatch(org.apache.activemq.artemis.utils.ReusableLatch) Test(org.junit.Test)

Example 3 with ReusableLatch

use of org.apache.activemq.artemis.utils.ReusableLatch in project activemq-artemis by apache.

the class ReusableLatchTest method testLatchOnSingleThread.

@Test
public void testLatchOnSingleThread() throws Exception {
    ReusableLatch latch = new ReusableLatch();
    for (int i = 1; i <= 100; i++) {
        latch.countUp();
        Assert.assertEquals(i, latch.getCount());
    }
    for (int i = 100; i > 0; i--) {
        Assert.assertEquals(i, latch.getCount());
        latch.countDown();
        Assert.assertEquals(i - 1, latch.getCount());
    }
    latch.await();
}
Also used : ReusableLatch(org.apache.activemq.artemis.utils.ReusableLatch) Test(org.junit.Test)

Example 4 with ReusableLatch

use of org.apache.activemq.artemis.utils.ReusableLatch in project activemq-artemis by apache.

the class RedeployTest method testRedeploy.

@Test
public void testRedeploy() throws Exception {
    Path brokerXML = getTestDirfile().toPath().resolve("broker.xml");
    URL url1 = RedeployTest.class.getClassLoader().getResource("reload-test-jms.xml");
    URL url2 = RedeployTest.class.getClassLoader().getResource("reload-test-updated-jms.xml");
    Files.copy(url1.openStream(), brokerXML);
    EmbeddedJMS embeddedJMS = new EmbeddedJMS();
    embeddedJMS.setConfigResourcePath(brokerXML.toUri().toString());
    embeddedJMS.start();
    final ReusableLatch latch = new ReusableLatch(1);
    Runnable tick = new Runnable() {

        @Override
        public void run() {
            latch.countDown();
        }
    };
    embeddedJMS.getActiveMQServer().getReloadManager().setTick(tick);
    try {
        latch.await(10, TimeUnit.SECONDS);
        Assert.assertEquals("DLQ", embeddedJMS.getActiveMQServer().getAddressSettingsRepository().getMatch("jms").getDeadLetterAddress().toString());
        Assert.assertEquals("ExpiryQueue", embeddedJMS.getActiveMQServer().getAddressSettingsRepository().getMatch("jms").getExpiryAddress().toString());
        Assert.assertFalse(tryConsume());
        Files.copy(url2.openStream(), brokerXML, StandardCopyOption.REPLACE_EXISTING);
        brokerXML.toFile().setLastModified(System.currentTimeMillis() + 1000);
        latch.setCount(1);
        embeddedJMS.getActiveMQServer().getReloadManager().setTick(tick);
        latch.await(10, TimeUnit.SECONDS);
        Assert.assertTrue(tryConsume());
        Assert.assertEquals("NewQueue", embeddedJMS.getActiveMQServer().getAddressSettingsRepository().getMatch("jms").getDeadLetterAddress().toString());
        Assert.assertEquals("NewQueue", embeddedJMS.getActiveMQServer().getAddressSettingsRepository().getMatch("jms").getExpiryAddress().toString());
        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
        try (Connection connection = factory.createConnection()) {
            Session session = connection.createSession();
            Queue queue = session.createQueue("DivertQueue");
            MessageProducer producer = session.createProducer(queue);
            producer.send(session.createTextMessage("text"));
            connection.start();
            MessageConsumer consumer = session.createConsumer(session.createQueue("NewQueue"));
            Assert.assertNotNull("Divert wasn't redeployed accordingly", consumer.receive(5000));
        }
    } finally {
        embeddedJMS.stop();
    }
}
Also used : Path(java.nio.file.Path) ActiveMQConnectionFactory(org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory) EmbeddedJMS(org.apache.activemq.artemis.jms.server.embedded.EmbeddedJMS) MessageConsumer(javax.jms.MessageConsumer) ReusableLatch(org.apache.activemq.artemis.utils.ReusableLatch) Connection(javax.jms.Connection) MessageProducer(javax.jms.MessageProducer) Queue(javax.jms.Queue) URL(java.net.URL) Session(javax.jms.Session) Test(org.junit.Test)

Example 5 with ReusableLatch

use of org.apache.activemq.artemis.utils.ReusableLatch in project activemq-artemis by apache.

the class RedeployTest method testRedeployStopAndRestart.

/**
 * Simulates Stop and Start that occurs when network health checker stops the server when network is detected unhealthy
 * and re-starts the broker once detected that it is healthy again.
 *
 * @throws Exception for anything un-expected, test will fail.
 */
@Test
public void testRedeployStopAndRestart() throws Exception {
    Path brokerXML = getTestDirfile().toPath().resolve("broker.xml");
    URL url1 = RedeployTest.class.getClassLoader().getResource("reload-original.xml");
    URL url2 = RedeployTest.class.getClassLoader().getResource("reload-changed.xml");
    Files.copy(url1.openStream(), brokerXML);
    EmbeddedJMS embeddedJMS = new EmbeddedJMS();
    embeddedJMS.setConfigResourcePath(brokerXML.toUri().toString());
    embeddedJMS.start();
    final ReusableLatch latch = new ReusableLatch(1);
    Runnable tick = latch::countDown;
    embeddedJMS.getActiveMQServer().getReloadManager().setTick(tick);
    try {
        latch.await(10, TimeUnit.SECONDS);
        Assert.assertEquals(getSecurityRoles(embeddedJMS, "security_address").size(), 1);
        Assert.assertEquals(getSecurityRoles(embeddedJMS, "security_address").iterator().next().getName(), "b");
        Assert.assertEquals(getAddressSettings(embeddedJMS, "address_settings_address").getDeadLetterAddress(), SimpleString.toSimpleString("OriginalDLQ"));
        Assert.assertEquals(getAddressSettings(embeddedJMS, "address_settings_address").getExpiryAddress(), SimpleString.toSimpleString("OriginalExpiryQueue"));
        Assert.assertNotNull(getAddressInfo(embeddedJMS, "config_test_address_removal_no_queue"));
        Assert.assertNotNull(getAddressInfo(embeddedJMS, "config_test_address_removal"));
        Assert.assertNotNull(getAddressInfo(embeddedJMS, "config_test_queue_removal"));
        Assert.assertTrue(listQueuesNamesForAddress(embeddedJMS, "config_test_queue_removal").contains("config_test_queue_removal_queue_1"));
        Assert.assertTrue(listQueuesNamesForAddress(embeddedJMS, "config_test_queue_removal").contains("config_test_queue_removal_queue_2"));
        Assert.assertNotNull(getAddressInfo(embeddedJMS, "config_test_queue_change"));
        Assert.assertTrue(listQueuesNamesForAddress(embeddedJMS, "config_test_queue_change").contains("config_test_queue_change_queue"));
        Assert.assertEquals(10, getQueue(embeddedJMS, "config_test_queue_change_queue").getMaxConsumers());
        Assert.assertEquals(false, getQueue(embeddedJMS, "config_test_queue_change_queue").isPurgeOnNoConsumers());
        Files.copy(url2.openStream(), brokerXML, StandardCopyOption.REPLACE_EXISTING);
        brokerXML.toFile().setLastModified(System.currentTimeMillis() + 1000);
        latch.setCount(1);
        embeddedJMS.getActiveMQServer().getReloadManager().setTick(tick);
        latch.await(10, TimeUnit.SECONDS);
        // Assert that the security settings change applied
        Assert.assertEquals(getSecurityRoles(embeddedJMS, "security_address").size(), 1);
        Assert.assertEquals(getSecurityRoles(embeddedJMS, "security_address").iterator().next().getName(), "c");
        // Assert that the address settings change applied
        Assert.assertEquals(getAddressSettings(embeddedJMS, "address_settings_address").getDeadLetterAddress(), SimpleString.toSimpleString("NewDLQ"));
        Assert.assertEquals(getAddressSettings(embeddedJMS, "address_settings_address").getExpiryAddress(), SimpleString.toSimpleString("NewExpiryQueue"));
        // Assert the address and queue changes applied
        Assert.assertNull(getAddressInfo(embeddedJMS, "config_test_address_removal_no_queue"));
        Assert.assertNull(getAddressInfo(embeddedJMS, "config_test_address_removal"));
        Assert.assertNotNull(getAddressInfo(embeddedJMS, "config_test_queue_removal"));
        Assert.assertTrue(listQueuesNamesForAddress(embeddedJMS, "config_test_queue_removal").contains("config_test_queue_removal_queue_1"));
        Assert.assertFalse(listQueuesNamesForAddress(embeddedJMS, "config_test_queue_removal").contains("config_test_queue_removal_queue_2"));
        Assert.assertNotNull(getAddressInfo(embeddedJMS, "config_test_queue_change"));
        Assert.assertTrue(listQueuesNamesForAddress(embeddedJMS, "config_test_queue_change").contains("config_test_queue_change_queue"));
        Assert.assertEquals(1, getQueue(embeddedJMS, "config_test_queue_change_queue").getMaxConsumers());
        Assert.assertEquals(true, getQueue(embeddedJMS, "config_test_queue_change_queue").isPurgeOnNoConsumers());
    } finally {
        embeddedJMS.stop();
    }
    try {
        embeddedJMS.start();
        // Assert that the security settings changes persist a stop and start server (e.g. like what occurs if network health check stops the node), but JVM remains up.
        Assert.assertEquals(getSecurityRoles(embeddedJMS, "security_address").size(), 1);
        Assert.assertEquals(getSecurityRoles(embeddedJMS, "security_address").iterator().next().getName(), "c");
        // Assert that the address settings changes persist a stop and start server (e.g. like what occurs if network health check stops the node), but JVM remains up.
        Assert.assertEquals(getAddressSettings(embeddedJMS, "address_settings_address").getDeadLetterAddress(), SimpleString.toSimpleString("NewDLQ"));
        Assert.assertEquals(getAddressSettings(embeddedJMS, "address_settings_address").getExpiryAddress(), SimpleString.toSimpleString("NewExpiryQueue"));
        // Assert that the address and queue changes persist a stop and start server (e.g. like what occurs if network health check stops the node), but JVM remains up.
        Assert.assertNull(getAddressInfo(embeddedJMS, "config_test_address_removal_no_queue"));
        Assert.assertNull(getAddressInfo(embeddedJMS, "config_test_address_removal"));
        Assert.assertNotNull(getAddressInfo(embeddedJMS, "config_test_queue_removal"));
        Assert.assertTrue(listQueuesNamesForAddress(embeddedJMS, "config_test_queue_removal").contains("config_test_queue_removal_queue_1"));
        Assert.assertFalse(listQueuesNamesForAddress(embeddedJMS, "config_test_queue_removal").contains("config_test_queue_removal_queue_2"));
        Assert.assertNotNull(getAddressInfo(embeddedJMS, "config_test_queue_change"));
        Assert.assertTrue(listQueuesNamesForAddress(embeddedJMS, "config_test_queue_change").contains("config_test_queue_change_queue"));
        Assert.assertEquals(1, getQueue(embeddedJMS, "config_test_queue_change_queue").getMaxConsumers());
        Assert.assertEquals(true, getQueue(embeddedJMS, "config_test_queue_change_queue").isPurgeOnNoConsumers());
    } finally {
        embeddedJMS.stop();
    }
}
Also used : Path(java.nio.file.Path) EmbeddedJMS(org.apache.activemq.artemis.jms.server.embedded.EmbeddedJMS) ReusableLatch(org.apache.activemq.artemis.utils.ReusableLatch) URL(java.net.URL) Test(org.junit.Test)

Aggregations

ReusableLatch (org.apache.activemq.artemis.utils.ReusableLatch)15 Test (org.junit.Test)12 URL (java.net.URL)4 Path (java.nio.file.Path)3 EmbeddedJMS (org.apache.activemq.artemis.jms.server.embedded.EmbeddedJMS)3 ByteBuffer (java.nio.ByteBuffer)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Connection (javax.jms.Connection)2 MessageConsumer (javax.jms.MessageConsumer)2 MessageProducer (javax.jms.MessageProducer)2 Session (javax.jms.Session)2 SimpleString (org.apache.activemq.artemis.api.core.SimpleString)2 IOCallback (org.apache.activemq.artemis.core.io.IOCallback)2 IOException (java.io.IOException)1 FileStore (java.nio.file.FileStore)1 DecimalFormat (java.text.DecimalFormat)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1