Search in sources :

Example 16 with Quote

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

Example 17 with Quote

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

the class IntegrationTest method testSingleJournalSync.

@Test
public void testSingleJournalSync() throws Exception {
    int size = 100000;
    try (JournalWriter<Quote> remote = getFactory().writer(Quote.class, "remote", 2 * size)) {
        server.publish(remote);
        server.start();
        try {
            final CountDownLatch latch = new CountDownLatch(1);
            client.subscribe(Quote.class, "remote", "local", 2 * size, new JournalListener() {

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

                @Override
                public void onEvent(int event) {
                }
            });
            client.start();
            TestUtils.generateQuoteData(remote, size);
            latch.await();
            client.halt();
        } finally {
            server.halt();
        }
        try (Journal<Quote> local = getFactory().reader(Quote.class, "local")) {
            TestUtils.assertDataEquals(remote, local);
        }
    }
}
Also used : Quote(com.questdb.model.Quote) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 18 with Quote

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

the class IntegrationTest method testOutOfSyncServerSide.

@Test
@Ignore
public // this is failing intermittently, replication is up for rewrite, cant be bothered fixing badly designed code
void testOutOfSyncServerSide() throws Exception {
    int size = 10000;
    try (JournalWriter<Quote> remote = getFactory().writer(Quote.class, "remote", 2 * size)) {
        server.publish(remote);
        server.start();
        try {
            final AtomicInteger serverErrors = new AtomicInteger();
            final AtomicInteger commits = new AtomicInteger();
            client = new JournalClient(new ClientConfig("localhost"), getFactory(), null, evt -> {
                if (evt == JournalClientEvents.EVT_SERVER_DIED) {
                    serverErrors.incrementAndGet();
                }
            });
            client.subscribe(Quote.class, "remote", "local", 2 * size, new JournalListener() {

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

                @Override
                public void onEvent(int event) {
                }
            });
            client.start();
            TestUtils.generateQuoteData(remote, size);
            TestUtils.assertCounter(commits, 1, 1, TimeUnit.SECONDS);
            client.halt();
            try (Journal<Quote> local = getFactory().reader(Quote.class, "local")) {
                TestUtils.assertDataEquals(remote, local);
            }
            // -------------------------------
            TestUtils.generateQuoteData(remote, 10000, remote.getMaxTimestamp());
            remote.commit();
            TestUtils.generateQuoteData(remote, 10000, remote.getMaxTimestamp());
            remote.commit();
            TestUtils.generateQuoteData(remote, 10000, remote.getMaxTimestamp());
            remote.commit();
            try (JournalWriter<Quote> localW = getFactory().writer(Quote.class, "local")) {
                TestUtils.generateQuoteData(localW, 10000, localW.getMaxTimestamp());
                localW.commit();
                TestUtils.generateQuoteData(localW, 10000, localW.getMaxTimestamp());
                localW.commit();
            }
            final AtomicInteger errorCounter = new AtomicInteger();
            client = new JournalClient(new ClientConfig("localhost"), getFactory(), null, evt -> {
                if (evt == JournalClientEvents.EVT_SERVER_DIED) {
                    serverErrors.incrementAndGet();
                }
            });
            client.subscribe(Quote.class, "remote", "local", 2 * size, new JournalListener() {

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

                @Override
                public void onEvent(int event) {
                    errorCounter.incrementAndGet();
                    System.out.println("EV: " + event);
                }
            });
            client.start();
            TestUtils.assertCounter(commits, 1, 1, TimeUnit.SECONDS);
            TestUtils.assertCounter(errorCounter, 1, 1, TimeUnit.SECONDS);
            client.halt();
            Assert.assertEquals(0, serverErrors.get());
        } finally {
            server.halt();
        }
    }
}
Also used : Quote(com.questdb.model.Quote) 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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ClientConfig(com.questdb.net.ha.config.ClientConfig) Ignore(org.junit.Ignore) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 19 with Quote

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

the class IntegrationTest method testSubscribeIncompatible.

@Test
public void testSubscribeIncompatible() 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")) {
            server.publish(remote);
            server.start();
            try {
                remote.append(origin.query().all().asResultSet().subset(0, 1000));
                remote.commit();
                getFactory().writer(new JournalConfigurationBuilder().$("local").$int("x").$()).close();
                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 CountDownLatch incompatible = new CountDownLatch(1);
                try {
                    client.subscribe(Quote.class, "remote", "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));
                    remote.append(origin.query().all().asResultSet().subset(1000, 2000));
                    remote.commit();
                } finally {
                    client.halt();
                }
                Assert.assertTrue(terminated.await(5, TimeUnit.SECONDS));
            } finally {
                server.halt();
            }
        }
    }
}
Also used : Quote(com.questdb.model.Quote) 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 20 with Quote

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

the class IntegrationTest method testTwoJournalsSync.

@Test
public void testTwoJournalsSync() throws Exception {
    int size = 10000;
    try (JournalWriter<Quote> remote1 = getFactory().writer(Quote.class, "remote1", 2 * size)) {
        try (JournalWriter<TestEntity> remote2 = getFactory().writer(TestEntity.class, "remote2", 2 * size)) {
            server.publish(remote1);
            server.publish(remote2);
            server.start();
            final CountDownLatch latch = new CountDownLatch(2);
            client.subscribe(Quote.class, "remote1", "local1", 2 * size, new JournalListener() {

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

                @Override
                public void onEvent(int event) {
                }
            });
            client.subscribe(TestEntity.class, "remote2", "local2", 2 * size, new JournalListener() {

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

                @Override
                public void onEvent(int event) {
                }
            });
            client.start();
            TestUtils.generateQuoteData(remote1, size);
            TestUtils.generateTestEntityData(remote2, size);
            latch.await();
            client.halt();
            server.halt();
            try (Journal<Quote> local1 = getFactory().reader(Quote.class, "local1")) {
                Assert.assertEquals("Local1 has wrong size", size, local1.size());
            }
            try (Journal<TestEntity> local2 = getFactory().reader(TestEntity.class, "local2")) {
                Assert.assertEquals("Remote2 has wrong size", size, remote2.size());
                Assert.assertEquals("Local2 has wrong size", size, local2.size());
            }
        }
    }
}
Also used : Quote(com.questdb.model.Quote) TestEntity(com.questdb.model.TestEntity) CountDownLatch(java.util.concurrent.CountDownLatch) 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