Search in sources :

Example 1 with JournalListener

use of com.questdb.store.JournalListener in project questdb by bluestreak01.

the class ReconnectTest method testServerRestart.

@Test
@Ignore
public void testServerRestart() throws Exception {
    final int size = 10000;
    try (JournalWriter<Quote> remote = getFactory().writer(Quote.class, "remote", 2 * size)) {
        // start server #1
        JournalServer server = newServer();
        server.publish(remote);
        server.start();
        final CountDownLatch connectedLatch = new CountDownLatch(1);
        JournalClient client = new JournalClient(new ClientConfig("localhost") {

            {
                getReconnectPolicy().setLoginRetryCount(3);
                getReconnectPolicy().setRetryCount(5);
                getReconnectPolicy().setSleepBetweenRetriesMillis(TimeUnit.SECONDS.toMillis(1));
            }
        }, getFactory(), null, evt -> {
            if (evt == JournalClientEvents.EVT_CONNECTED) {
                connectedLatch.countDown();
            }
        });
        // subscribe client, waiting for complete set of data
        // when data arrives client triggers latch
        final CountDownLatch latch = new CountDownLatch(1);
        // create empty "local"
        getFactory().writer(Quote.class, "local").close();
        try (final Journal<Quote> local = getFactory().reader(Quote.class, "local")) {
            client.subscribe(Quote.class, "remote", "local", 2 * size, new JournalListener() {

                @Override
                public void onCommit() {
                    try {
                        if (local.refresh() && local.size() == 2 * size) {
                            latch.countDown();
                        }
                    } catch (JournalException e) {
                        throw new JournalRuntimeException(e);
                    }
                }

                @Override
                public void onEvent(int event) {
                }
            });
            client.start();
            Assert.assertTrue(connectedLatch.await(5, TimeUnit.SECONDS));
            // generate first batch
            TestUtils.generateQuoteData(remote, size, System.currentTimeMillis(), 1);
            remote.commit();
            // stop server
            server.halt();
            // start server #2
            server = newServer();
            server.publish(remote);
            server.start();
            // generate second batch
            TestUtils.generateQuoteData(remote, size, System.currentTimeMillis() + 2 * size, 1);
            remote.commit();
            // wait for client to get full set
            latch.await();
            // stop client and server
            client.halt();
            server.halt();
            // assert client state
            TestUtils.assertDataEquals(remote, local);
        }
    }
}
Also used : Quote(com.questdb.model.Quote) JournalException(com.questdb.std.ex.JournalException) JournalListener(com.questdb.store.JournalListener) JournalRuntimeException(com.questdb.common.JournalRuntimeException) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConfig(com.questdb.net.ha.config.ClientConfig) Ignore(org.junit.Ignore) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 2 with JournalListener

use of com.questdb.store.JournalListener in project questdb by bluestreak01.

the class AuthReplicationClientMain method main.

public static void main(String[] args) throws Exception {
    JournalConfiguration configuration = new JournalConfigurationBuilder().build(args[0]);
    Factory factory = new Factory(configuration);
    final JournalClient client = new JournalClient(factory, () -> "MY SECRET".getBytes("UTF8"));
    final Journal<Price> reader = factory.reader(Price.class, "price-copy");
    reader.setSequentialAccess(true);
    client.subscribe(Price.class, null, "price-copy", new JournalListener() {

        @Override
        public void onCommit() {
            int count = 0;
            long t = 0;
            for (Price p : JournalIterators.incrementBufferedIterator(reader)) {
                if (count == 0) {
                    t = p.getNanos();
                }
                count++;
            }
            System.out.println("took: " + (System.nanoTime() - t) + ", count=" + count);
        }

        @Override
        public void onEvent(int event) {
            System.out.println("There was an error");
        }
    });
    client.start();
    System.out.println("Client started");
}
Also used : JournalClient(com.questdb.net.ha.JournalClient) JournalConfiguration(com.questdb.store.factory.configuration.JournalConfiguration) Price(org.questdb.examples.support.Price) JournalListener(com.questdb.store.JournalListener) Factory(com.questdb.store.factory.Factory) JournalConfigurationBuilder(com.questdb.store.factory.configuration.JournalConfigurationBuilder)

Example 3 with JournalListener

use of com.questdb.store.JournalListener in project questdb by bluestreak01.

the class SimpleReplicationClientMain method main.

public static void main(String[] args) throws Exception {
    JournalConfiguration configuration = new JournalConfigurationBuilder().build(args[0]);
    Factory factory = new Factory(configuration, 1000, 1, 0);
    final JournalClient client = new JournalClient(factory);
    final Journal<Price> reader = factory.reader(Price.class, "price-copy");
    reader.setSequentialAccess(true);
    client.subscribe(Price.class, null, "price-copy", new JournalListener() {

        @Override
        public void onCommit() {
            int count = 0;
            long t = 0;
            for (Price p : JournalIterators.incrementBufferedIterator(reader)) {
                if (count == 0) {
                    t = p.getNanos();
                }
                count++;
            }
            System.out.println("took: " + (System.nanoTime() - t) + ", count=" + count);
        }

        @Override
        public void onEvent(int event) {
            System.out.println("There was an error");
        }
    });
    client.start();
    System.out.println("Client started");
}
Also used : JournalClient(com.questdb.net.ha.JournalClient) JournalConfiguration(com.questdb.store.factory.configuration.JournalConfiguration) Price(org.questdb.examples.support.Price) JournalListener(com.questdb.store.JournalListener) Factory(com.questdb.store.factory.Factory) JournalConfigurationBuilder(com.questdb.store.factory.configuration.JournalConfigurationBuilder)

Example 4 with JournalListener

use of com.questdb.store.JournalListener in project questdb by bluestreak01.

the class SslReplicationClientMain method main.

public static void main(String[] args) throws Exception {
    JournalConfiguration configuration = new JournalConfigurationBuilder().build(args[0]);
    Factory factory = new Factory(configuration, 1000, 1, 0);
    final JournalClient client = new JournalClient(new ClientConfig() {

        {
            getSslConfig().setSecure(true);
            try (InputStream is = this.getClass().getResourceAsStream("/keystore/singlekey.ks")) {
                getSslConfig().setTrustStore(is, "changeit");
            }
        }
    }, factory);
    final Journal<Price> reader = factory.reader(Price.class, "price-copy");
    reader.setSequentialAccess(true);
    client.subscribe(Price.class, null, "price-copy", new JournalListener() {

        @Override
        public void onCommit() {
            int count = 0;
            long t = 0;
            for (Price p : JournalIterators.incrementBufferedIterator(reader)) {
                if (count == 0) {
                    t = p.getNanos();
                }
                count++;
            }
            System.out.println("took: " + (System.nanoTime() - t) + ", count=" + count);
        }

        @Override
        public void onEvent(int event) {
            System.out.println("There was an error");
        }
    });
    client.start();
    System.out.println("Client started");
}
Also used : JournalClient(com.questdb.net.ha.JournalClient) JournalConfiguration(com.questdb.store.factory.configuration.JournalConfiguration) Price(org.questdb.examples.support.Price) InputStream(java.io.InputStream) JournalListener(com.questdb.store.JournalListener) Factory(com.questdb.store.factory.Factory) JournalConfigurationBuilder(com.questdb.store.factory.configuration.JournalConfigurationBuilder) ClientConfig(com.questdb.net.ha.config.ClientConfig)

Example 5 with JournalListener

use of com.questdb.store.JournalListener in project questdb by bluestreak01.

the class DataLossTest method testDiscardFile.

@Test
@Ignore
public void testDiscardFile() throws Exception {
    // create master journal
    try (JournalWriter<Quote> master = getFactory().writer(Quote.class, "master")) {
        TestUtils.generateQuoteData(master, 300, master.getMaxTimestamp());
        master.commit();
        // publish master out
        JournalServer server = new JournalServer(new ServerConfig() {

            {
                addNode(new ServerNode(0, "localhost"));
                setEnableMultiCast(false);
                setHeartbeatFrequency(50);
            }
        }, getFactory());
        server.publish(master);
        server.start();
        final AtomicInteger counter = new AtomicInteger();
        final AtomicInteger doNotExpect = new AtomicInteger();
        // equalize slave
        JournalClient client = new JournalClient(new ClientConfig("localhost") {

            {
                setEnableMultiCast(false);
            }
        }, getFactory());
        client.subscribe(Quote.class, "master", "slave", new JournalListener() {

            @Override
            public void onCommit() {
                counter.incrementAndGet();
            }

            @Override
            public void onEvent(int event) {
            }
        });
        client.start();
        TestUtils.assertCounter(counter, 1, 10, TimeUnit.SECONDS);
        // stop client to be able to add to slave manually
        client.halt();
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        // add more data to slave
        try (JournalWriter<Quote> slave = getFactory().writer(Quote.class, "slave")) {
            TestUtils.generateQuoteData(slave, 200, slave.getMaxTimestamp());
            slave.commit();
        }
        // synchronise slave again
        client = new JournalClient(new ClientConfig("localhost"), getFactory());
        client.subscribe(Quote.class, "master", "slave", new JournalListener() {

            @Override
            public void onCommit() {
                doNotExpect.incrementAndGet();
            }

            @Override
            public void onEvent(int event) {
                counter.incrementAndGet();
            }
        });
        client.start();
        TestUtils.assertCounter(counter, 2, 180, TimeUnit.SECONDS);
        client.halt();
        Assert.assertEquals(0, doNotExpect.get());
        try (JournalWriter w = getFactory().writer(Quote.class, "slave")) {
            Assert.assertNotNull(w);
        }
        server.halt();
    }
}
Also used : Quote(com.questdb.model.Quote) ServerConfig(com.questdb.net.ha.config.ServerConfig) JournalWriter(com.questdb.store.JournalWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JournalListener(com.questdb.store.JournalListener) ServerNode(com.questdb.net.ha.config.ServerNode) ClientConfig(com.questdb.net.ha.config.ClientConfig) Ignore(org.junit.Ignore) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Aggregations

JournalListener (com.questdb.store.JournalListener)9 ClientConfig (com.questdb.net.ha.config.ClientConfig)6 Quote (com.questdb.model.Quote)4 JournalClient (com.questdb.net.ha.JournalClient)4 Factory (com.questdb.store.factory.Factory)4 JournalConfiguration (com.questdb.store.factory.configuration.JournalConfiguration)4 JournalConfigurationBuilder (com.questdb.store.factory.configuration.JournalConfigurationBuilder)4 AbstractTest (com.questdb.test.tools.AbstractTest)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Test (org.junit.Test)4 Price (org.questdb.examples.support.Price)4 ServerConfig (com.questdb.net.ha.config.ServerConfig)3 ServerNode (com.questdb.net.ha.config.ServerNode)3 RecordSource (com.questdb.ql.RecordSource)2 RecordSourcePrinter (com.questdb.ql.RecordSourcePrinter)2 StringSink (com.questdb.std.str.StringSink)2 JournalKey (com.questdb.store.JournalKey)2 JournalWriter (com.questdb.store.JournalWriter)2 Ignore (org.junit.Ignore)2 JournalRuntimeException (com.questdb.common.JournalRuntimeException)1