Java Code Examples for io.debezium.util.Stopwatch

Following code examples demonstrate how to use io.debezium.util.Stopwatchfrom debezium. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use io.debezium.util.Stopwatchand various code implementation of this class.

    public void shouldStartClusterAndAllowAsynchronousProductionAndAutomaticConsumersToUseIt() throws Exception {
        Testing.Debug.enable();
        final String topicName = "topicA";
        final CountDownLatch completion = new CountDownLatch(2);
        final int numMessages = 3;
        final AtomicLong messagesRead = new AtomicLong(0);

        cluster.addBrokers(1).startup();
        cluster.createTopics(topicName);

        Stopwatch sw = Stopwatch.reusable().start();
        cluster.useTo().consumeIntegers(topicName, numMessages, 10, TimeUnit.SECONDS, completion::countDown, (key, value) -> {
            messagesRead.incrementAndGet();
            return true;
        });

        cluster.useTo().produce("manual", new StringSerializer(), new IntegerSerializer(), produer -> {
            produer.write(topicName, "key1", 1);
            produer.write(topicName, "key2", 2);
            produer.write(topicName, "key3", 3);
            completion.countDown();
        });

        if (completion.await(10, TimeUnit.SECONDS)) {
            sw.stop();
            Testing.debug("The consumer completed normally in " + sw.durations());
        } else {
            Testing.debug("Consumer did not completed normally");
        }
        assertThat(messagesRead.get()).isEqualTo(numMessages);
    } 


    public void shouldStartClusterAndAllowInteractiveProductionAndAutomaticConsumersToUseIt() throws Exception {
        Testing.Debug.enable();
        final String topicName = "topicA";
        final CountDownLatch completion = new CountDownLatch(1);
        final int numMessages = 3;
        final AtomicLong messagesRead = new AtomicLong(0);

        cluster.addBrokers(1).startup();
        cluster.createTopics(topicName);

        Stopwatch sw = Stopwatch.reusable().start();
        cluster.useTo().consumeIntegers(topicName, numMessages, 10, TimeUnit.SECONDS, completion::countDown, (key, value) -> {
            messagesRead.incrementAndGet();
            return true;
        });

        cluster.useTo()
               .createProducer("manual", new StringSerializer(), new IntegerSerializer())
               .write(topicName, "key1", 1)
               .write(topicName, "key2", 2)
               .write(topicName, "key3", 3)
               .close();

        if (completion.await(10, TimeUnit.SECONDS)) {
            sw.stop();
            Testing.debug("The consumer completed normally in " + sw.durations());
        } else {
            Testing.debug("Consumer did not completed normally");
        }

        assertThat(messagesRead.get()).isEqualTo(numMessages);
    } 

    public void shouldStartClusterAndAllowProducersAndConsumersToUseIt() throws Exception {
        Testing.Debug.enable();
        final String topicName = "topicA";
        final CountDownLatch completion = new CountDownLatch(2);
        final int numMessages = 100;
        final AtomicLong messagesRead = new AtomicLong(0);

        cluster.addBrokers(1).startup();
        cluster.createTopics(topicName);

        Stopwatch sw = Stopwatch.reusable().start();
        cluster.useTo().consumeIntegers(topicName, numMessages, 10, TimeUnit.SECONDS, completion::countDown, (key, value) -> {
            messagesRead.incrementAndGet();
            return true;
        });

        cluster.useTo().produceIntegers(topicName, numMessages, 1, completion::countDown);

        if (completion.await(10, TimeUnit.SECONDS)) {
            sw.stop();
            Testing.debug("Both consumer and producer completed normally in " + sw.durations());
        } else {
            Testing.debug("Consumer and/or producer did not completed normally");
        }

        assertThat(messagesRead.get()).isEqualTo(numMessages);
    } 

    protected static void waitForGtidSetsToMatch(Configuration master, Configuration replica, long timeout, TimeUnit unit) {
        GtidSet masterGtidSet = readAvailableGtidSet(master);
        if (masterGtidSet == null) {
            return;
        }
        Stopwatch sw = Stopwatch.reusable().start();
        CountDownLatch latch = new CountDownLatch(1);
        Runnable runner = () -> {
            try {
                GtidSet replicaGtidSet = null;
                while (true) {
                    Testing.debug("Checking replica's GTIDs and comparing to master's...");
                    replicaGtidSet = readAvailableGtidSet(replica);
                    if (masterGtidSet.isContainedWithin(replicaGtidSet)) {
                        Testing.debug("Replica's GTIDs are caught up to the master's.");
                        sw.stop();
                        return;
                    }
                    Testing.debug("Waiting for replica's GTIDs to catch up to master's...");
                    Thread.sleep(100);
                }
            } catch (InterruptedException e) {
                Thread.interrupted();
            } finally {
                latch.countDown();
            }
        };
        Thread checker = new Thread(runner, "mysql-replica-watcher");
        checker.start();
        try {
            if (!latch.await(timeout, unit)) {
                checker.interrupt();
            }
            Testing.print("Waited a total of " + sw.durations().statistics().getTotalAsString() + " for the replica to catch up to the master.");
        } catch (InterruptedException e) {
            Thread.interrupted();
        }
    } 

Advertisement
Javadoc
A stopwatch for measuring durations. All NewStopwatch implementations are threadsafe, although using

a single stopwatch object across threads requires caution and care. @author Randall Hauch

Read More
Advertisement