Search in sources :

Example 6 with ClientConfig

use of com.questdb.net.ha.config.ClientConfig in project questdb by bluestreak01.

the class ClusteredProducerMain method main.

public static void main(String[] args) throws JournalException, IOException, JournalNetworkException, NumericException {
    final String pathToDatabase = args[0];
    final int instance = Numbers.parseInt(args[1]);
    final JournalConfiguration configuration = new JournalConfigurationBuilder() {

        {
            $(Price.class).$ts();
        }
    }.build(pathToDatabase);
    final Factory factory = new Factory(configuration, 1000, 1, 0);
    final JournalWriter<Price> writer = factory.writer(new JournalKey<>(Price.class, null, PartitionBy.DEFAULT, 1000000000));
    final WorkerController wc = new WorkerController(writer);
    final ClusterController cc = new ClusterController(new ServerConfig() {

        {
            addNode(new ServerNode(1, "127.0.0.1:7080"));
            addNode(new ServerNode(2, "127.0.0.1:7090"));
        }
    }, new ClientConfig(), factory, instance, new ArrayList<JournalWriter>() {

        {
            add(writer);
        }
    }, wc);
    cc.start();
    Runtime.getRuntime().addShutdownHook(new Thread(cc::halt));
}
Also used : JournalWriter(com.questdb.store.JournalWriter) Factory(com.questdb.store.factory.Factory) JournalConfigurationBuilder(com.questdb.store.factory.configuration.JournalConfigurationBuilder) ServerConfig(com.questdb.net.ha.config.ServerConfig) JournalConfiguration(com.questdb.store.factory.configuration.JournalConfiguration) ClusterController(com.questdb.net.ha.ClusterController) Price(org.questdb.examples.support.Price) ServerNode(com.questdb.net.ha.config.ServerNode) ClientConfig(com.questdb.net.ha.config.ClientConfig)

Example 7 with ClientConfig

use of com.questdb.net.ha.config.ClientConfig 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 8 with ClientConfig

use of com.questdb.net.ha.config.ClientConfig in project questdb by bluestreak01.

the class ClientRecoveryTest method testClientWriterRelease.

@Test
public void testClientWriterRelease() throws Exception {
    final CountDownLatch serverError = new CountDownLatch(1);
    JournalClient client = new JournalClient(new ClientConfig("localhost"), getFactory(), null, evt -> {
        if (evt == JournalClientEvents.EVT_TERMINATED) {
            serverError.countDown();
        }
    });
    client.subscribe(Quote.class);
    client.start();
    Assert.assertTrue(serverError.await(5, TimeUnit.SECONDS));
    Assert.assertFalse(client.isRunning());
    // should be able to get writer after client failure.
    try (JournalWriter<Quote> w = getFactory().writer(Quote.class)) {
        Assert.assertNotNull(w);
    }
}
Also used : Quote(com.questdb.model.Quote) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConfig(com.questdb.net.ha.config.ClientConfig) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 9 with ClientConfig

use of com.questdb.net.ha.config.ClientConfig in project questdb by bluestreak01.

the class ClusterControllerTest method testBusyFailOver.

@Test
@Ignore
public void testBusyFailOver() throws Exception {
    try (JournalWriter<Quote> writer1 = getFactory().writer(Quote.class)) {
        try (final JournalWriter<Quote> writer2 = tf.getFactory().writer(Quote.class)) {
            final CountDownLatch active1 = new CountDownLatch(1);
            final CountDownLatch active2 = new CountDownLatch(1);
            final CountDownLatch standby2 = new CountDownLatch(1);
            final AtomicLong expected = new AtomicLong();
            final AtomicLong actual = new AtomicLong();
            ClusterController controller1 = new ClusterController(new ServerConfig() {

                {
                    addNode(new ServerNode(0, "localhost:7080"));
                    addNode(new ServerNode(1, "localhost:7090"));
                    setEnableMultiCast(false);
                    setHeartbeatFrequency(50);
                }
            }, new ClientConfig() {

                {
                    setEnableMultiCast(false);
                }
            }, getFactory(), 0, new ArrayList<JournalWriter>() {

                {
                    add(writer1);
                }
            }, new ClusterStatusListener() {

                @Override
                public void goActive() {
                    try {
                        TestUtils.generateQuoteData(writer1, 100000);
                        TestUtils.generateQuoteData(writer1, 100000, writer1.getMaxTimestamp());
                        writer1.commit();
                        TestUtils.generateQuoteData(writer1, 100000, writer1.getMaxTimestamp());
                        writer1.commit();
                        TestUtils.generateQuoteData(writer1, 100000, writer1.getMaxTimestamp());
                        writer1.commit();
                        TestUtils.generateQuoteData(writer1, 100000, writer1.getMaxTimestamp());
                        writer1.commit();
                        expected.set(writer1.size());
                        active1.countDown();
                    } catch (JournalException | NumericException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void goPassive(ServerNode activeNode) {
                }

                @Override
                public void onShutdown() {
                }
            });
            ClusterController controller2 = new ClusterController(new ServerConfig() {

                {
                    addNode(new ServerNode(0, "localhost:7080"));
                    addNode(new ServerNode(1, "localhost:7090"));
                    setEnableMultiCast(false);
                    setHeartbeatFrequency(50);
                }
            }, new ClientConfig() {

                {
                    setEnableMultiCast(false);
                }
            }, tf.getFactory(), 1, new ArrayList<JournalWriter>() {

                {
                    add(writer2);
                }
            }, new ClusterStatusListener() {

                @Override
                public void goActive() {
                    try {
                        actual.set(writer2.size());
                        active2.countDown();
                    } catch (JournalException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void goPassive(ServerNode activeNode) {
                    standby2.countDown();
                }

                @Override
                public void onShutdown() {
                }
            });
            controller1.start();
            Assert.assertTrue(active1.await(30, TimeUnit.SECONDS));
            Assert.assertEquals(0, active1.getCount());
            controller2.start();
            standby2.await(60, TimeUnit.SECONDS);
            Assert.assertEquals(0, standby2.getCount());
            controller1.halt();
            active2.await(10, TimeUnit.SECONDS);
            Assert.assertEquals(0, active2.getCount());
            controller2.halt();
            Assert.assertTrue(expected.get() > 0);
            Assert.assertEquals(expected.get(), actual.get());
        }
    }
}
Also used : JournalWriter(com.questdb.store.JournalWriter) JournalException(com.questdb.std.ex.JournalException) CountDownLatch(java.util.concurrent.CountDownLatch) Quote(com.questdb.model.Quote) ServerConfig(com.questdb.net.ha.config.ServerConfig) AtomicLong(java.util.concurrent.atomic.AtomicLong) ServerNode(com.questdb.net.ha.config.ServerNode) ClientConfig(com.questdb.net.ha.config.ClientConfig) AbstractTest(com.questdb.test.tools.AbstractTest)

Example 10 with ClientConfig

use of com.questdb.net.ha.config.ClientConfig 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

ClientConfig (com.questdb.net.ha.config.ClientConfig)27 Quote (com.questdb.model.Quote)21 AbstractTest (com.questdb.test.tools.AbstractTest)17 CountDownLatch (java.util.concurrent.CountDownLatch)17 Test (org.junit.Test)16 ServerConfig (com.questdb.net.ha.config.ServerConfig)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 JournalConfigurationBuilder (com.questdb.store.factory.configuration.JournalConfigurationBuilder)8 ServerNode (com.questdb.net.ha.config.ServerNode)7 InputStream (java.io.InputStream)7 JournalListener (com.questdb.store.JournalListener)6 JournalException (com.questdb.std.ex.JournalException)5 JournalWriter (com.questdb.store.JournalWriter)4 Ignore (org.junit.Ignore)4 Factory (com.questdb.store.factory.Factory)3 JournalConfiguration (com.questdb.store.factory.configuration.JournalConfiguration)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)3 Price (org.questdb.examples.support.Price)3 Log (com.questdb.log.Log)2 LogFactory (com.questdb.log.LogFactory)2