Search in sources :

Example 6 with JournalException

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

the class JournalServerAgent method addClientKey.

@SuppressWarnings("unchecked")
private void addClientKey(ByteChannel channel) throws JournalNetworkException {
    setKeyRequestConsumer.read(channel);
    IndexedJournalKey indexedKey = setKeyRequestConsumer.getValue();
    LOG.debug().$(socketAddress).$(" AddKey command received. Index: ").$(indexedKey.getIndex()).$();
    JournalKey<?> readerKey = indexedKey.getKey();
    int index = indexedKey.getIndex();
    IndexedJournalKey augmentedReaderKey = server.getWriterIndex0(readerKey);
    if (augmentedReaderKey == null) {
        error(channel, "Requested key not exported: " + readerKey);
    } else {
        writerToReaderMap.put(augmentedReaderKey.getIndex(), index);
        readerToWriterMap.extendAndSet(index, augmentedReaderKey.getIndex());
        try {
            createReader(index, augmentedReaderKey.getKey());
            ok(channel);
            sendMetadata(channel, index);
        } catch (JournalException e) {
            error(channel, "Could not created reader for key: " + readerKey, e);
        }
    }
}
Also used : JournalException(com.questdb.std.ex.JournalException) IndexedJournalKey(com.questdb.net.ha.model.IndexedJournalKey)

Example 7 with JournalException

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

the class AppendObjectConcurrent method main.

/**
 * Multiple threads append to same journal concurrently.
 * <p>
 * Single append thread receives data from multiple publishers via circular queue using QuestDB's thread messaging mechanism.
 * Please refer to my blog post: http://blog.questdb.org/2016/08/the-art-of-thread-messaging.html for more details.
 */
public static void main(String[] args) throws JournalException, InterruptedException {
    if (args.length != 1) {
        System.out.println("Usage: " + AppendObjectConcurrent.class.getName() + " <path>");
        System.exit(1);
    }
    // 
    RingQueue<Quote> queue = new RingQueue<>(Quote::new, 4096);
    // publisher sequence
    final MPSequence pubSequence = new MPSequence(queue.getCapacity());
    final SCSequence subSequence = new SCSequence();
    // create circular dependency between sequences
    pubSequence.then(subSequence).then(pubSequence);
    // run configuration
    int nThreads = 2;
    int nMessages = 1000000;
    JournalConfiguration configuration = ModelConfiguration.CONFIG.build(args[0]);
    try (Factory factory = new Factory(configuration, 1000, 1, 0)) {
        // start publishing threads
        for (int i = 0; i < nThreads; i++) {
            new Thread(new Publisher(queue, pubSequence, nMessages)).start();
        }
        // consume messages in main thread
        int count = 0;
        int deadline = nMessages * nThreads;
        try (JournalWriter<Quote> writer = factory.writer(Quote.class)) {
            while (count < deadline) {
                long cursor = subSequence.next();
                if (cursor < 0) {
                    LockSupport.parkNanos(1);
                    continue;
                }
                long available = subSequence.available();
                while (cursor < available) {
                    Quote q = queue.get(cursor++);
                    q.setTimestamp(System.currentTimeMillis());
                    try {
                        writer.append(q);
                    } catch (JournalException e) {
                        // append may fail, log and continue
                        // N.B. this logging uses builder pattern to construct message. Building finishes with $() call.
                        LOG.error().$("Sequence failed: ").$(cursor - 1).$(e).$();
                    }
                    count++;
                }
                subSequence.done(available - 1);
                try {
                    writer.commit();
                } catch (JournalException e) {
                    // something serious, attempt to rollback
                    LOG.error().$("Batch commit() failed [").$(available - 1).$(']').$(e).$();
                }
            }
        }
        LOG.info().$("Writer done").$();
    }
}
Also used : MPSequence(com.questdb.mp.MPSequence) RingQueue(com.questdb.mp.RingQueue) JournalException(com.questdb.std.ex.JournalException) Factory(com.questdb.store.factory.Factory) LogFactory(com.questdb.log.LogFactory) Quote(org.questdb.examples.support.Quote) SCSequence(com.questdb.mp.SCSequence) JournalConfiguration(com.questdb.store.factory.configuration.JournalConfiguration)

Example 8 with JournalException

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

the class FactoryEventLogger method runSerially.

@Override
protected boolean runSerially() {
    long cursor = subSeq.next();
    try {
        if (cursor < 0) {
            if (lastEventTimestamp > -1 && clock.getTicks() - lastEventTimestamp > commitInterval) {
                lastEventTimestamp = -1;
                writer.commit();
            }
            return false;
        }
        long available = subSeq.available();
        try {
            long count = available - cursor;
            while (cursor < available) {
                FactoryEvent ev = eventQueue.get(cursor++);
                JournalEntryWriter ew = writer.entryWriter(clock.getTicks());
                ew.putInt(0, PID);
                ew.put(1, ev.factoryType);
                ew.putLong(2, ev.thread);
                ew.putSym(3, ev.name);
                ew.putShort(4, ev.event);
                ew.putShort(5, ev.segment);
                ew.putShort(6, ev.position);
                ew.append();
            }
            if (count > commitBatchSize) {
                writer.commit();
            }
            lastEventTimestamp = clock.getTicks();
        } finally {
            subSeq.done(available - 1);
        }
        return true;
    } catch (JournalException e) {
        LOG.error().$("Failed to log factory event: ").$(e).$();
        return false;
    }
}
Also used : JournalException(com.questdb.std.ex.JournalException) JournalEntryWriter(com.questdb.store.JournalEntryWriter)

Example 9 with JournalException

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

the class JournalIterators method createRanges.

private static <T> ObjList<JournalIteratorRange> createRanges(Journal<T> journal) {
    final int partitionCount = journal.getPartitionCount();
    ObjList<JournalIteratorRange> ranges = new ObjList<>(partitionCount);
    try {
        for (int i = 0; i < partitionCount; i++) {
            Partition<T> p = journal.getPartition(i, true);
            long size = p.size();
            if (size > 0) {
                ranges.add(new JournalIteratorRange(p.getPartitionIndex(), 0, size - 1));
            }
        }
    } catch (JournalException e) {
        throw new JournalRuntimeException(e);
    }
    return ranges;
}
Also used : ObjList(com.questdb.std.ObjList) JournalException(com.questdb.std.ex.JournalException) JournalRuntimeException(com.questdb.common.JournalRuntimeException)

Example 10 with JournalException

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

the class JournalIterators method incrementBufferedIterator.

public static <T> JournalPeekingIterator<T> incrementBufferedIterator(Journal<T> journal) {
    try {
        long lo = journal.getMaxRowID();
        journal.refresh();
        return new JournalBufferedIterator<>(journal, createRanges(journal, journal.incrementRowID(lo)));
    } catch (JournalException e) {
        throw new JournalRuntimeException(e);
    }
}
Also used : JournalException(com.questdb.std.ex.JournalException) JournalRuntimeException(com.questdb.common.JournalRuntimeException)

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