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);
}
}
}
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").$();
}
}
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;
}
}
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;
}
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);
}
}
Aggregations