use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.
the class RollAtEndOfCycleTest method shouldAppendToExistingQueueFile.
@Test
public void shouldAppendToExistingQueueFile() throws Exception {
try (final SingleChronicleQueue queue = createQueue()) {
final ExcerptAppender appender = queue.acquireAppender();
appender.writeDocument(1, (w, i) -> {
w.int32(i);
});
final ExcerptTailer tailer = queue.createTailer();
try (final DocumentContext context = tailer.readingDocument()) {
assertTrue(context.isPresent());
}
assertQueueFileCount(queue.path.toPath(), 1);
assertFalse(tailer.readingDocument().isPresent());
appender.writeDocument(2, (w, i) -> {
w.int32(i);
});
assertQueueFileCount(queue.path.toPath(), 1);
try (final DocumentContext context = tailer.readingDocument()) {
assertTrue(context.isPresent());
}
}
}
use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.
the class RollAtEndOfCycleTest method shouldRollAndAppendToNewFile.
@Test
public void shouldRollAndAppendToNewFile() throws Exception {
try (final SingleChronicleQueue queue = createQueue()) {
final ExcerptAppender appender = queue.acquireAppender();
appender.writeDocument(1, (w, i) -> {
w.int32(i);
});
final ExcerptTailer tailer = queue.createTailer();
try (final DocumentContext context = tailer.readingDocument()) {
assertTrue(context.isPresent());
}
assertQueueFileCount(queue.path.toPath(), 1);
clock.addAndGet(TimeUnit.SECONDS.toMillis(2));
assertFalse(tailer.readingDocument().isPresent());
appender.writeDocument(2, (w, i) -> {
w.int32(i);
});
assertQueueFileCount(queue.path.toPath(), 2);
try (final DocumentContext context = tailer.readingDocument()) {
assertTrue(context.isPresent());
}
final ExcerptTailer newTailer = queue.createTailer();
int totalCount = 0;
while (true) {
final DocumentContext context = newTailer.readingDocument();
if (context.isPresent() && context.isData()) {
assertTrue(context.wire().read().int32() != 0);
totalCount++;
} else if (!context.isPresent()) {
break;
}
}
assertThat(totalCount, is(2));
}
}
use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.
the class ChronicleHistoryReader method readChronicle.
public Map<String, Histogram> readChronicle() {
final SingleChronicleQueue q = createQueue();
final ExcerptTailer tailer = q.createTailer();
final WireParselet parselet = parselet();
final MethodReader mr = new VanillaMethodReader(tailer, true, parselet, null, parselet);
MessageHistory.set(new VanillaMessageHistory());
while (!Thread.currentThread().isInterrupted() && mr.readOne()) {
++counter;
if (this.progress && counter % 1_000_000L == 0) {
System.out.println("Progress: " + counter);
}
}
return histos;
}
use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.
the class ChronicleReader method execute.
public void execute() {
try {
long lastObservedTailIndex = Long.MAX_VALUE;
long highestReachedIndex = 0L;
boolean isFirstIteration = true;
boolean retryLastOperation = false;
boolean queueHasBeenModified = false;
do {
try (final SingleChronicleQueue queue = createQueue();
final QueueEntryHandler messageConverter = entryHandlerFactory.get()) {
final ExcerptTailer tailer = queue.createTailer();
queueHasBeenModified = false;
if (highestReachedIndex != 0L) {
tailer.moveToIndex(highestReachedIndex);
}
final Bytes textConversionTarget = Bytes.elasticByteBuffer();
try {
moveToSpecifiedPosition(queue, tailer, isFirstIteration);
lastObservedTailIndex = tailer.index();
while (!Thread.currentThread().isInterrupted()) {
try (DocumentContext dc = pollMethod.apply(tailer)) {
if (!dc.isPresent()) {
if (tailInputSource) {
pauser.pause();
}
break;
}
pauser.reset();
if (customPlugin == null) {
messageConverter.accept(dc.wire(), text -> {
applyFiltersAndLog(text, tailer.index());
});
} else {
customPlugin.onReadDocument(dc);
}
}
}
} finally {
textConversionTarget.release();
highestReachedIndex = tailer.index();
isFirstIteration = false;
}
queueHasBeenModified = queueHasBeenModifiedSinceLastCheck(lastObservedTailIndex);
} catch (final RuntimeException e) {
if (e.getCause() != null && e.getCause() instanceof DateTimeParseException) {
// ignore this error - due to a race condition between
// the reader creating a Queue (with default roll-cycle due to no files on disk)
// and the writer appending to the Queue with a non-default roll-cycle
retryLastOperation = true;
} else {
throw e;
}
}
} while (tailInputSource || retryLastOperation || queueHasBeenModified);
} catch (Throwable t) {
t.printStackTrace();
throw t;
}
}
use of net.openhft.chronicle.queue.ExcerptTailer in project Chronicle-Queue by OpenHFT.
the class TailerPollingEmptyQueueTest method shouldNotGenerateExcessGarbage.
@Test
public void shouldNotGenerateExcessGarbage() throws Exception {
try (final SingleChronicleQueue queue = createQueue()) {
queue.path.mkdirs();
assertThat(queue.path.list((d, n) -> n.endsWith(SingleChronicleQueue.SUFFIX)).length, is(0));
final ExcerptTailer tailer = queue.createTailer();
for (int i = 0; i < 50; i++) {
assertFalse(tailer.readingDocument().isPresent());
}
final long startCollectionCount = GcControls.getGcCount();
for (int i = 0; i < 1_000_000; i++) {
assertFalse(tailer.readingDocument().isPresent());
}
assertThat(GcControls.getGcCount() - startCollectionCount, is(0L));
}
}
Aggregations