use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class RollCycleTest method testWriteToCorruptedFile.
@Test
public void testWriteToCorruptedFile() {
File dir = DirectoryUtils.tempDir("testWriteToCorruptedFile");
try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(dir).testBlockSize().rollCycle(RollCycles.TEST_DAILY).build()) {
ExcerptAppender appender = queue.acquireAppender();
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().write().text("hello world");
}
Bytes bytes;
long pos;
try (DocumentContext dc = appender.writingDocument()) {
bytes = dc.wire().bytes();
pos = bytes.writePosition() - 4;
}
// write as not complete.
bytes.writeInt(pos, Wires.NOT_COMPLETE_UNKNOWN_LENGTH);
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().write().text("hello world 2");
}
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().write().text("hello world 3");
}
}
}
use of net.openhft.chronicle.wire.DocumentContext 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.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class CycleNotFoundTest method tailerCycleNotFoundTest.
// reduced so that it runs quicker for the continuous integration (CI)
@Test(timeout = 50_000L)
@Ignore("TODO FIX")
public void tailerCycleNotFoundTest() throws IOException, InterruptedException, ExecutionException {
// added nano time just to make
File path = DirectoryUtils.tempDir("tailerCycleNotFoundTest");
ExecutorService executorService = Executors.newFixedThreadPool((int) NUMBER_OF_MSG);
AtomicLong counter = new AtomicLong();
Runnable reader = () -> {
long count = 0;
try (RollingChronicleQueue rqueue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(RollCycles.TEST_SECONDLY).build()) {
final ExcerptTailer tailer = rqueue.createTailer();
long last = -1;
while (count < NUMBER_OF_MSG) {
try (DocumentContext dc = tailer.readingDocument()) {
if (!dc.isPresent())
continue;
Assert.assertTrue(dc.isData());
Assert.assertEquals(last + 1, last = dc.wire().read().int64());
count++;
counter.incrementAndGet();
}
if (executorService.isShutdown())
Assert.fail();
}
// check nothing after the NUMBER_OF_MSG
try (DocumentContext dc = tailer.readingDocument()) {
Assert.assertFalse(dc.isPresent());
}
} finally {
System.out.printf("Read %,d messages, thread=" + Thread.currentThread().getName() + "\n", count);
}
};
List<Future> tailers = new ArrayList<>();
for (int i = 0; i < NUMBER_OF_TAILERS; i++) {
tailers.add(executorService.submit(reader));
}
// Appender run in a different thread
ExecutorService executorService1 = Executors.newSingleThreadExecutor();
Future<?> submit = executorService1.submit(() -> {
ChronicleQueue wqueue = SingleChronicleQueueBuilder.fieldlessBinary(path).testBlockSize().rollCycle(TEST_SECONDLY).build();
final ExcerptAppender appender = wqueue.acquireAppender();
long next = System.nanoTime() + INTERVAL_US * 1000;
for (int i = 0; i < NUMBER_OF_MSG; i++) {
while (System.nanoTime() < next) /* busy wait*/
;
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().write().int64(i);
}
next += INTERVAL_US * 1000;
if (executorService1.isShutdown())
return;
}
wqueue.close();
});
submit.get();
System.out.println("appender is done.");
// wait for all the tailer to finish
for (Future f : tailers) {
f.get();
}
assertEquals(NUMBER_OF_MSG * NUMBER_OF_TAILERS, counter.get());
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class LastIndexAppendedTest method testLastIndexAppendedAcrossRestarts.
@Test
public void testLastIndexAppendedAcrossRestarts() throws Exception {
String path = OS.TARGET + "/" + getClass().getSimpleName() + "-" + System.nanoTime();
for (int i = 0; i < 5; i++) {
try (SingleChronicleQueue queue = ChronicleQueueBuilder.single(path).testBlockSize().rollCycle(TEST_DAILY).build()) {
ExcerptAppender appender = queue.acquireAppender();
try (DocumentContext documentContext = appender.writingDocument()) {
int index = (int) documentContext.index();
assertEquals(i, index);
documentContext.wire().write().text("hello world");
}
assertEquals(i, (int) appender.lastIndexAppended());
}
}
try {
IOTools.deleteDirWithFiles(path, 2);
} catch (Exception index) {
}
}
use of net.openhft.chronicle.wire.DocumentContext in project Chronicle-Queue by OpenHFT.
the class TailerIndexingQueueTest method tailerShouldBeAbleToMoveBackwardFromEndOfCycle.
@Test
public void tailerShouldBeAbleToMoveBackwardFromEndOfCycle() throws Exception {
assumeFalse(OS.isWindows());
try (final SingleChronicleQueue queue = createQueue(path, clock::get)) {
final ExcerptAppender appender = queue.acquireAppender();
// generate some cycle files
range(0, 5).forEach(i -> {
try (final DocumentContext ctx = appender.writingDocument()) {
ctx.wire().write().int32(i);
clock.addAndGet(TimeUnit.SECONDS.toMillis(10L));
}
});
}
// remove all but the first file
final Path firstFile = Files.list(this.path.toPath()).sorted(Comparator.comparing(Path::toString)).findFirst().orElseThrow(AssertionError::new);
Files.list(this.path.toPath()).filter(p -> !p.equals(firstFile)).forEach(TailerIndexingQueueTest::deleteFile);
try (final SingleChronicleQueue queue = createQueue(path, SystemTimeProvider.INSTANCE)) {
final ExcerptTailer tailer = queue.createTailer().toEnd();
// move to END_OF_CYCLE
try (final DocumentContext readCtx = tailer.readingDocument()) {
assertThat(readCtx.isPresent(), is(false));
}
assertThat(tailer.state(), is(TailerState.END_OF_CYCLE));
tailer.direction(TailerDirection.BACKWARD);
tailer.toEnd();
assertThat(tailer.readingDocument().isPresent(), is(true));
}
}
Aggregations