Search in sources :

Example 11 with Quote

use of com.questdb.model.Quote 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 12 with Quote

use of com.questdb.model.Quote 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)

Example 13 with Quote

use of com.questdb.model.Quote in project questdb by bluestreak01.

the class IntegrationTest method testResubscribeAfterBadSubscription.

@Test
public void testResubscribeAfterBadSubscription() throws Exception {
    // check that bad subscription doesn't cause dupe check to go haywire
    int size = 1000;
    try (JournalWriter<Quote> origin = getFactory().writer(Quote.class, "origin")) {
        TestUtils.generateQuoteData(origin, size);
        server.publish(origin);
        server.start();
        try {
            getFactory().writer(new JournalConfigurationBuilder().$("local").$int("x").$()).close();
            final CountDownLatch terminated = new CountDownLatch(1);
            final AtomicInteger serverDied = new AtomicInteger();
            JournalClient client = new JournalClient(new ClientConfig("localhost"), getFactory(), null, evt -> {
                switch(evt) {
                    case JournalClientEvents.EVT_TERMINATED:
                        terminated.countDown();
                        break;
                    case JournalClientEvents.EVT_SERVER_DIED:
                        serverDied.incrementAndGet();
                        break;
                    default:
                        break;
                }
            });
            client.start();
            try {
                final CountDownLatch incompatible = new CountDownLatch(1);
                client.subscribe(Quote.class, "origin", "local", new JournalListener() {

                    @Override
                    public void onCommit() {
                    }

                    @Override
                    public void onEvent(int event) {
                        if (event == JournalEvents.EVT_JNL_INCOMPATIBLE) {
                            incompatible.countDown();
                        }
                    }
                });
                Assert.assertTrue(incompatible.await(500, TimeUnit.SECONDS));
                // delete incompatible journal
                getFactory().delete("local");
                // subscribe again and have client create compatible journal from server's metadata
                final AtomicInteger errorCount = new AtomicInteger();
                final CountDownLatch commit = new CountDownLatch(1);
                client.subscribe(Quote.class, "origin", "local", new JournalListener() {

                    @Override
                    public void onCommit() {
                        commit.countDown();
                    }

                    @Override
                    public void onEvent(int event) {
                        if (event != JournalEvents.EVT_JNL_SUBSCRIBED) {
                            errorCount.incrementAndGet();
                        }
                    }
                });
                Assert.assertTrue(commit.await(30, TimeUnit.SECONDS));
                Assert.assertEquals(0, errorCount.get());
            } finally {
                client.halt();
            }
            Assert.assertTrue(terminated.await(5, TimeUnit.SECONDS));
            Assert.assertEquals(0, serverDied.get());
            try (Journal r = getFactory().reader("local")) {
                Assert.assertEquals(size, r.size());
            }
        } finally {
            server.halt();
        }
    }
}
Also used : Quote(com.questdb.model.Quote) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) JournalConfigurationBuilder(com.questdb.store.factory.configuration.JournalConfigurationBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConfig(com.questdb.net.ha.config.ClientConfig) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 14 with Quote

use of com.questdb.model.Quote in project questdb by bluestreak01.

the class IntegrationTest method testTwoClientSync.

@Test
public void testTwoClientSync() throws Exception {
    int size = 10000;
    try (JournalWriter<Quote> origin = getFactory().writer(Quote.class, "origin")) {
        TestUtils.generateQuoteData(origin, size);
        try (JournalWriter<Quote> remote = getFactory().writer(Quote.class, "remote")) {
            remote.append(origin.query().all().asResultSet().subset(0, 1000));
            remote.commit();
            server.publish(remote);
            server.start();
            final AtomicInteger counter = new AtomicInteger();
            JournalClient client1 = new JournalClient(new ClientConfig("localhost"), getFactory());
            client1.subscribe(Quote.class, "remote", "local1", new JournalListener() {

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

                @Override
                public void onEvent(int event) {
                }
            });
            client1.start();
            JournalClient client2 = new JournalClient(new ClientConfig("localhost"), getFactory());
            client2.subscribe(Quote.class, "remote", "local2", new JournalListener() {

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

                @Override
                public void onEvent(int event) {
                }
            });
            client2.start();
            TestUtils.assertCounter(counter, 2, 2, TimeUnit.SECONDS);
            client1.halt();
            remote.append(origin.query().all().asResultSet().subset(1000, 1500));
            remote.commit();
            TestUtils.assertCounter(counter, 3, 2, TimeUnit.SECONDS);
            LOG.info().$("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~").$();
            // this client should receive an update that gets it up to speed
            // wait until this happens before adding more rows to remote
            final CountDownLatch waitForUpdate = new CountDownLatch(1);
            client1 = new JournalClient(new ClientConfig("localhost"), getFactory());
            client1.subscribe(Quote.class, "remote", "local1", new JournalListener() {

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

                @Override
                public void onEvent(int event) {
                }
            });
            client1.start();
            waitForUpdate.await(2, TimeUnit.SECONDS);
            remote.append(origin.query().all().asResultSet().subset(1500, size));
            remote.commit();
            TestUtils.assertCounter(counter, 6, 2, TimeUnit.SECONDS);
            try (Journal<Quote> local1r = getFactory().reader(Quote.class, "local1")) {
                Assert.assertEquals(size, local1r.size());
            }
            try (Journal<Quote> local2r = getFactory().reader(Quote.class, "local2")) {
                Assert.assertEquals(size, local2r.size());
            }
            client1.halt();
            client2.halt();
            server.halt();
        }
    }
}
Also used : Quote(com.questdb.model.Quote) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientConfig(com.questdb.net.ha.config.ClientConfig) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 15 with Quote

use of com.questdb.model.Quote in project questdb by bluestreak01.

the class IntegrationTest method testSubscribeOnTheFly.

@Test
public void testSubscribeOnTheFly() throws Exception {
    int size = 5000;
    try (JournalWriter<Quote> origin = getFactory().writer(Quote.class, "origin")) {
        TestUtils.generateQuoteData(origin, size);
        try (JournalWriter<Quote> remote1 = getFactory().writer(Quote.class, "remote1")) {
            try (JournalWriter<Quote> remote2 = getFactory().writer(Quote.class, "remote2")) {
                server.publish(remote1);
                server.publish(remote2);
                server.start();
                try {
                    remote1.append(origin.query().all().asResultSet().subset(0, 1000));
                    remote1.commit();
                    remote2.append(origin.query().all().asResultSet().subset(0, 1000));
                    remote2.commit();
                    final AtomicInteger counter = new AtomicInteger();
                    JournalClient client = new JournalClient(new ClientConfig("localhost"), getFactory());
                    client.start();
                    try {
                        client.subscribe(Quote.class, "remote1", "local1", new JournalListener() {

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

                            @Override
                            public void onEvent(int event) {
                            }
                        });
                        TestUtils.assertCounter(counter, 1, 2, TimeUnit.SECONDS);
                        try (Journal r = getFactory().reader("local1")) {
                            Assert.assertEquals(1000, r.size());
                        }
                        client.subscribe(Quote.class, "remote2", "local2", new JournalListener() {

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

                            @Override
                            public void onEvent(int event) {
                            }
                        });
                        TestUtils.assertCounter(counter, 2, 2, TimeUnit.SECONDS);
                        try (Journal r = getFactory().reader("local2")) {
                            Assert.assertEquals(1000, r.size());
                        }
                    } finally {
                        client.halt();
                    }
                } finally {
                    server.halt();
                }
            }
        }
    }
}
Also used : Quote(com.questdb.model.Quote) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientConfig(com.questdb.net.ha.config.ClientConfig) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Aggregations

Quote (com.questdb.model.Quote)80 Test (org.junit.Test)64 AbstractTest (com.questdb.test.tools.AbstractTest)63 ClientConfig (com.questdb.net.ha.config.ClientConfig)20 CountDownLatch (java.util.concurrent.CountDownLatch)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 JournalException (com.questdb.std.ex.JournalException)11 ServerConfig (com.questdb.net.ha.config.ServerConfig)10 ArrayList (java.util.ArrayList)9 Interval (com.questdb.std.time.Interval)8 InputStream (java.io.InputStream)6 JournalConfigurationBuilder (com.questdb.store.factory.configuration.JournalConfigurationBuilder)5 File (java.io.File)5 JournalListener (com.questdb.store.JournalListener)4 ServerNode (com.questdb.net.ha.config.ServerNode)3 RecordSource (com.questdb.ql.RecordSource)3 Rnd (com.questdb.std.Rnd)3 StringSink (com.questdb.std.str.StringSink)3 Ignore (org.junit.Ignore)3 NumericException (com.questdb.common.NumericException)2