Search in sources :

Example 11 with ClientConfig

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

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

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

the class IntegrationTest method testClientConnect.

@Test
public void testClientConnect() throws Exception {
    final CountDownLatch error = new CountDownLatch(1);
    client = new JournalClient(new ClientConfig("localhost"), getFactory(), null, evt -> {
        if (evt == JournalClientEvents.EVT_SERVER_ERROR) {
            error.countDown();
        }
    });
    client.start();
    Assert.assertTrue(error.await(30, TimeUnit.SECONDS));
}
Also used : JournalException(com.questdb.std.ex.JournalException) CyclicBarrier(java.util.concurrent.CyclicBarrier) DateFormatUtils(com.questdb.std.time.DateFormatUtils) JournalConfigurationBuilder(com.questdb.store.factory.configuration.JournalConfigurationBuilder) AbstractTest(com.questdb.test.tools.AbstractTest) ClientConfig(com.questdb.net.ha.config.ClientConfig) Test(org.junit.Test) LogFactory(com.questdb.log.LogFactory) TestEntity(com.questdb.model.TestEntity) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Ignore(org.junit.Ignore) Log(com.questdb.log.Log) ServerConfig(com.questdb.net.ha.config.ServerConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Quote(com.questdb.model.Quote) Assert(org.junit.Assert) com.questdb.store(com.questdb.store) TestUtils(com.questdb.test.tools.TestUtils) Before(org.junit.Before) 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 ClientConfig

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

Example 15 with ClientConfig

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

the class IntegrationTest method testBadSubscriptionOnTheFlyFollowedByReconnect.

@Test
public void testBadSubscriptionOnTheFlyFollowedByReconnect() throws Exception {
    try (final JournalWriter<Quote> origin = getFactory().writer(Quote.class, "origin")) {
        final int batchSize = 1000;
        final int batchCount = 100;
        server.publish(origin);
        server.start();
        try {
            final CountDownLatch terminated = new CountDownLatch(1);
            JournalClient client = new JournalClient(new ClientConfig("localhost"), getFactory(), null, evt -> {
                if (evt == JournalClientEvents.EVT_TERMINATED) {
                    terminated.countDown();
                }
            });
            client.start();
            final AtomicInteger commits = new AtomicInteger();
            final AtomicInteger errors = new AtomicInteger();
            final CountDownLatch localSubscribed = new CountDownLatch(1);
            final CountDownLatch dataReceived = new CountDownLatch(1);
            try {
                // create empty journal
                getFactory().writer(Quote.class, "local").close();
                try (final Journal local = getFactory().reader("local")) {
                    client.subscribe(Quote.class, "origin", "local", new JournalListener() {

                        @Override
                        public void onCommit() {
                            commits.incrementAndGet();
                            try {
                                local.refresh();
                                if (local.size() == batchCount * batchSize) {
                                    dataReceived.countDown();
                                }
                            } catch (JournalException e) {
                                e.printStackTrace();
                                errors.incrementAndGet();
                            }
                        }

                        @Override
                        public void onEvent(int event) {
                            switch(event) {
                                case JournalEvents.EVT_JNL_SUBSCRIBED:
                                    localSubscribed.countDown();
                                    break;
                                default:
                                    errors.incrementAndGet();
                                    break;
                            }
                        }
                    });
                    final CountDownLatch published = new CountDownLatch(1);
                    final CyclicBarrier barrier = new CyclicBarrier(2);
                    final AtomicInteger publisherErrors = new AtomicInteger();
                    new Thread(() -> {
                        try {
                            barrier.await();
                            long timestamp = DateFormatUtils.parseDateTime("2013-09-04T10:00:00.000Z");
                            long increment = 1000L;
                            for (int i = 0; i < batchCount; i++) {
                                TestUtils.generateQuoteData(origin, batchSize, timestamp, increment);
                                timestamp += increment * (batchSize);
                                origin.commit();
                            }
                        } catch (Throwable e) {
                            e.printStackTrace();
                            publisherErrors.incrementAndGet();
                        }
                        published.countDown();
                    }).start();
                    Assert.assertTrue(localSubscribed.await(10, TimeUnit.SECONDS));
                    barrier.await();
                    // after publishing stream is setup we attempt to subscribe bad journal
                    // todo: this part breaks server, fix server and continue
                    // readerFactory.writer(new JournalConfigurationBuilder().$("x").$int("x").$()).close();
                    // 
                    // client.subscribe(Quote.class, "origin", "x", new JournalListener() {
                    // @Override
                    // public void onCommit() {
                    // 
                    // }
                    // 
                    // @Override
                    // public void onEvent(int event) {
                    // System.out.println("bad event: " + event);
                    // }
                    // });
                    Assert.assertTrue(published.await(60, TimeUnit.SECONDS));
                    Assert.assertTrue(dataReceived.await(60, TimeUnit.SECONDS));
                    Assert.assertEquals(0, publisherErrors.get());
                    Assert.assertEquals(0, errors.get());
                    Assert.assertTrue(commits.get() > 0);
                    local.refresh();
                    Assert.assertEquals(batchSize * batchCount, local.size());
                }
            } catch (Throwable e) {
                e.printStackTrace();
                Assert.fail();
            } finally {
                client.halt();
            }
            Assert.assertTrue(terminated.await(5, TimeUnit.SECONDS));
        } finally {
            server.halt();
        }
    }
// Assert.fail();
}
Also used : JournalException(com.questdb.std.ex.JournalException) CountDownLatch(java.util.concurrent.CountDownLatch) CyclicBarrier(java.util.concurrent.CyclicBarrier) 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

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