Search in sources :

Example 1 with JournalException

use of com.questdb.std.ex.JournalException 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 JournalException

use of com.questdb.std.ex.JournalException in project questdb by bluestreak01.

the class ScenarioTest method testLagTrickle.

@Test
public void testLagTrickle() throws Exception {
    // prepare test data
    try (JournalWriter<Quote> origin = getFactory().writer(Quote.class, "origin")) {
        TestData.appendQuoteData2(origin);
        try (final JournalWriter<Quote> randomOrigin = getFactory().writer(new JournalKey<>(Quote.class, "origin-rnd", PartitionBy.NONE, Constants.NULL_RECORD_HINT, false))) {
            randomOrigin.append(origin.query().all().asResultSet().shuffle(new Rnd()));
            try (final JournalWriter<Quote> remote = getFactory().writer(Quote.class, "remote")) {
                try (final Journal<Quote> remoteReader = getFactory().reader(Quote.class, "remote")) {
                    // create empty journal
                    getFactory().writer(Quote.class, "local").close();
                    // setup local where data should be trickling from client
                    try (final Journal<Quote> local = getFactory().reader(Quote.class, "local")) {
                        Assert.assertEquals(0, local.size());
                        JournalServer server = new JournalServer(serverConfig, getFactory());
                        JournalClient client = new JournalClient(clientConfig, getFactory());
                        server.publish(remote);
                        server.start();
                        final AtomicInteger errors = new AtomicInteger();
                        final CountDownLatch ready = new CountDownLatch(1);
                        client.subscribe(Quote.class, "remote", "local", new JournalListener() {

                            @Override
                            public void onCommit() {
                                try {
                                    if (local.refresh() && local.size() == 33) {
                                        ready.countDown();
                                    }
                                } catch (JournalException e) {
                                    errors.incrementAndGet();
                                    e.printStackTrace();
                                }
                            }

                            @Override
                            public void onEvent(int event) {
                                if (event != JournalEvents.EVT_JNL_SUBSCRIBED) {
                                    errors.incrementAndGet();
                                }
                            }
                        });
                        client.start();
                        int n = 0;
                        while (n < 400) {
                            lagIteration(randomOrigin, remote, n, n + 10);
                            n += 10;
                        }
                        Assert.assertTrue(ready.await(10, TimeUnit.SECONDS));
                        server.halt();
                        client.halt();
                        local.refresh();
                        remoteReader.refresh();
                        TestUtils.assertEquals(remoteReader, local);
                        Assert.assertEquals(0, errors.get());
                    }
                }
            }
        }
    }
}
Also used : JournalException(com.questdb.std.ex.JournalException) Rnd(com.questdb.std.Rnd) CountDownLatch(java.util.concurrent.CountDownLatch) Quote(com.questdb.model.Quote) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractTest(com.questdb.test.tools.AbstractTest) Test(org.junit.Test)

Example 3 with JournalException

use of com.questdb.std.ex.JournalException in project questdb by bluestreak01.

the class AbstractQueryContext method executeQuery.

private RecordSource executeQuery(ChunkedResponse r, Factory factory) throws ParserException, DisconnectedChannelException, SlowWritableChannelException {
    QueryCompiler compiler = COMPILER.get();
    ParsedModel model = compiler.parse(query);
    switch(model.getModelType()) {
        case ParsedModel.QUERY:
            return compiler.compile(factory, model);
        default:
            try {
                compiler.execute(factory, model);
            } catch (JournalException e) {
                error().$("Server error executing statement ").$(query).$(e).$();
                sendException(r, 0, e.getMessage(), 500);
            }
            return null;
    }
}
Also used : JournalException(com.questdb.std.ex.JournalException) QueryCompiler(com.questdb.parser.sql.QueryCompiler) ParsedModel(com.questdb.parser.sql.model.ParsedModel)

Example 4 with JournalException

use of com.questdb.std.ex.JournalException in project questdb by bluestreak01.

the class PlainTextStoringParser method onMetadata.

@Override
public void onMetadata(ObjList<ImportedColumnMetadata> metadata, boolean header) {
    if (writer == null) {
        this.metadata = metadata;
        this.header = header;
        try {
            switch(factory.getConfiguration().exists(name)) {
                case DOES_NOT_EXIST:
                    writer = factory.writer(createStructure());
                    break;
                case EXISTS:
                    if (overwrite) {
                        factory.delete(name);
                        writer = factory.writer(createStructure());
                    } else {
                        writer = mapColumnsAndOpenWriter();
                    }
                    break;
                default:
                    throw ImportNameException.INSTANCE;
            }
            writer.setSequentialAccess(true);
            _size = writer.size();
            errors.seed(writer.getMetadata().getColumnCount(), 0);
        } catch (JournalException e) {
            throw new JournalRuntimeException(e);
        }
    }
}
Also used : JournalException(com.questdb.std.ex.JournalException) JournalRuntimeException(com.questdb.common.JournalRuntimeException)

Example 5 with JournalException

use of com.questdb.std.ex.JournalException in project questdb by bluestreak01.

the class PlainTextStoringParser method onFields.

@Override
public void onFields(int line, ObjList<DirectByteCharSequence> values, int hi) {
    boolean append = true;
    try {
        JournalEntryWriter w = writer.entryWriter();
        for (int i = 0; i < hi; i++) {
            if (values.getQuick(i).length() == 0) {
                continue;
            }
            try {
                DirectByteCharSequence charField;
                ImportedColumnMetadata m = metadata.getQuick(i);
                switch(m.importedColumnType) {
                    case ColumnType.STRING:
                        utf8Sink.clear();
                        charField = values.getQuick(i);
                        Chars.utf8Decode(charField.getLo(), charField.getHi(), utf8Sink);
                        w.putStr(i, (DirectBytes) utf8Sink);
                        break;
                    case ColumnType.DOUBLE:
                        w.putDouble(i, Numbers.parseDouble(values.getQuick(i)));
                        break;
                    case ColumnType.INT:
                        w.putInt(i, Numbers.parseInt(values.getQuick(i)));
                        break;
                    case ColumnType.FLOAT:
                        w.putFloat(i, Numbers.parseFloat(values.getQuick(i)));
                        break;
                    case ColumnType.DATE:
                        if (m.dateFormat != null && m.dateLocale != null) {
                            w.putDate(i, m.dateFormat.parse(values.getQuick(i), m.dateLocale));
                        } else {
                            throw NumericException.INSTANCE;
                        }
                        break;
                    case ColumnType.SYMBOL:
                        utf8Sink.clear();
                        charField = values.getQuick(i);
                        Chars.utf8Decode(charField.getLo(), charField.getHi(), utf8Sink);
                        w.putSym(i, utf8Sink);
                        break;
                    case ColumnType.LONG:
                        w.putLong(i, Numbers.parseLong(values.getQuick(i)));
                        break;
                    case ColumnType.BOOLEAN:
                        w.putBool(i, Chars.equalsIgnoreCase(values.getQuick(i), "true"));
                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                switch(atomicity) {
                    case ATOMICITY_STRICT:
                        LOG.info().$("Error at (").$(line).$(',').$(i).$(')').$();
                        throw new JournalRuntimeException("Error on line: " + line + ", col: " + i);
                    default:
                        errors.increment(i);
                        LOG.debug().$("Error at (").$(line).$(',').$(i).$(") as ").$(metadata.getQuick(i).importedColumnType).$(": ").$(e.getMessage()).$();
                        append = false;
                }
                break;
            }
        }
        if (append) {
            w.append();
        }
    } catch (JournalException e) {
        throw new JournalRuntimeException(e);
    }
}
Also used : ImportedColumnMetadata(com.questdb.parser.ImportedColumnMetadata) JournalException(com.questdb.std.ex.JournalException) DirectByteCharSequence(com.questdb.std.str.DirectByteCharSequence) JournalRuntimeException(com.questdb.common.JournalRuntimeException) JournalEntryWriter(com.questdb.store.JournalEntryWriter) ImportColumnCountException(com.questdb.ex.ImportColumnCountException) JournalException(com.questdb.std.ex.JournalException) NumericException(com.questdb.common.NumericException) JournalRuntimeException(com.questdb.common.JournalRuntimeException) ImportNameException(com.questdb.ex.ImportNameException)

Aggregations

JournalException (com.questdb.std.ex.JournalException)63 JournalRuntimeException (com.questdb.common.JournalRuntimeException)29 AbstractTest (com.questdb.test.tools.AbstractTest)14 Test (org.junit.Test)13 KVIndex (com.questdb.store.KVIndex)12 Partition (com.questdb.store.Partition)9 Quote (com.questdb.model.Quote)8 IndexCursor (com.questdb.store.IndexCursor)8 File (java.io.File)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 IncompatibleJournalException (com.questdb.ex.IncompatibleJournalException)5 JournalStructure (com.questdb.store.factory.configuration.JournalStructure)5 ObjList (com.questdb.std.ObjList)4 JournalWriter (com.questdb.store.JournalWriter)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ClientConfig (com.questdb.net.ha.config.ClientConfig)3 FixedColumn (com.questdb.store.FixedColumn)3 Factory (com.questdb.store.factory.Factory)3 ColumnMetadata (com.questdb.store.factory.configuration.ColumnMetadata)3 JournalLockedException (com.questdb.ex.JournalLockedException)2