use of net.openhft.chronicle.wire.ValueIn in project Chronicle-Queue by OpenHFT.
the class EofMarkerOnEmptyQueueTest method shouldRecoverFromEmptyQueueOnRoll.
@Test
public void shouldRecoverFromEmptyQueueOnRoll() throws Exception {
final AtomicLong clock = new AtomicLong(System.currentTimeMillis());
try (final SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(tmpFolder.newFolder()).rollCycle(RollCycles.TEST_SECONDLY).timeProvider(clock::get).timeoutMS(1_000).testBlockSize().build()) {
final ExcerptAppender appender = queue.acquireAppender();
final DocumentContext context = appender.writingDocument();
// start to write a message, but don't close the context - simulates crashed writer
final long expectedEofMarkerPosition = context.wire().bytes().writePosition() - Wires.SPB_HEADER_SIZE;
context.wire().writeEventName("foo").int32(1);
final int startCycle = queue.cycle();
clock.addAndGet(TimeUnit.SECONDS.toMillis(1L));
final int nextCycle = queue.cycle();
// ensure that the cycle file will roll
assertThat(startCycle, is(not(nextCycle)));
Executors.newSingleThreadExecutor().submit(() -> {
try (final DocumentContext nextCtx = queue.acquireAppender().writingDocument()) {
nextCtx.wire().writeEventName("bar").int32(7);
}
}).get(3, TimeUnit.SECONDS);
final WireStore firstCycleStore = queue.storeForCycle(startCycle, 0, false);
final long firstCycleWritePosition = firstCycleStore.writePosition();
// assert that no write was completed
assertThat(firstCycleWritePosition, is(0L));
final ExcerptTailer tailer = queue.createTailer();
int recordCount = 0;
int lastItem = -1;
while (true) {
try (final DocumentContext readCtx = tailer.readingDocument()) {
if (!readCtx.isPresent()) {
break;
}
final StringBuilder name = new StringBuilder();
final ValueIn field = readCtx.wire().readEventName(name);
recordCount++;
lastItem = field.int32();
}
}
assertThat(firstCycleStore.bytes().readVolatileInt(expectedEofMarkerPosition), is(Wires.END_OF_DATA));
assertThat(recordCount, is(1));
assertThat(lastItem, is(7));
}
}
use of net.openhft.chronicle.wire.ValueIn in project Chronicle-Queue by OpenHFT.
the class ExcerptsSkippedWhenTailerDirectionNoneTest method shouldNotSkipMessageAtStartOfQueue.
@Test
public void shouldNotSkipMessageAtStartOfQueue() throws Exception {
final File tmpDir = DirectoryUtils.tempDir(ExcerptsSkippedWhenTailerDirectionNoneTest.class.getSimpleName());
try (final ChronicleQueue writeQueue = SingleChronicleQueueBuilder.binary(tmpDir).testBlockSize().rollCycle(TEST_DAILY).build()) {
final ExcerptAppender excerptAppender = writeQueue.acquireAppender();
try (final DocumentContext ctx = excerptAppender.writingDocument()) {
ctx.wire().getValueOut().object("first");
}
try (final DocumentContext ctx = excerptAppender.writingDocument()) {
ctx.wire().getValueOut().object("second");
}
}
try (final SingleChronicleQueue readQueue = SingleChronicleQueueBuilder.binary(tmpDir).testBlockSize().rollCycle(TEST_DAILY).build()) {
final ExcerptTailer tailer = readQueue.createTailer();
final RollCycle rollCycle = readQueue.rollCycle();
assertThat(rollCycle.toSequenceNumber(tailer.index()), is(0L));
try (final DocumentContext ctx = tailer.direction(TailerDirection.NONE).readingDocument()) {
// access the first document without incrementing sequence number
}
assertThat(rollCycle.toSequenceNumber(tailer.index()), is(0L));
String value;
try (DocumentContext dc = tailer.direction(TailerDirection.FORWARD).readingDocument()) {
ValueIn valueIn = dc.wire().getValueIn();
value = (String) valueIn.object();
}
assertThat(rollCycle.toSequenceNumber(tailer.index()), is(1L));
assertThat(value, is("first"));
try (DocumentContext dc = tailer.direction(TailerDirection.NONE).readingDocument()) {
ValueIn valueIn = dc.wire().getValueIn();
value = (String) valueIn.object();
}
assertThat(rollCycle.toSequenceNumber(tailer.index()), is(1L));
assertThat(value, is("second"));
try (DocumentContext dc = tailer.direction(TailerDirection.NONE).readingDocument()) {
ValueIn valueIn = dc.wire().getValueIn();
value = (String) valueIn.object();
}
assertThat(rollCycle.toSequenceNumber(tailer.index()), is(1L));
assertThat(value, is("second"));
}
}
use of net.openhft.chronicle.wire.ValueIn in project Chronicle-Queue by OpenHFT.
the class SingleChronicleQueueStore method loadWritePosition.
private LongValue loadWritePosition(@NotNull WireIn wire) {
final ValueIn read = wire.read(MetaDataField.writePosition);
final int code;
final long start = wire.bytes().readPosition();
try {
wire.consumePadding();
code = wire.bytes().uncheckedReadUnsignedByte();
} finally {
wire.bytes().readPosition(start);
}
if (code == BinaryWireCode.I64_ARRAY) {
TwoLongValue result = wire.newTwoLongReference();
// when the write position is and array it also encodes the sequence number in the write position as the second long value
read.int128(result);
return result;
}
final LongValue result = wire.newLongReference();
read.int64(result);
return result;
}
use of net.openhft.chronicle.wire.ValueIn in project Chronicle-Queue by OpenHFT.
the class SingleTableBuilder method build.
// *************************************************************************
//
// *************************************************************************
@NotNull
public TableStore build() {
if (readOnly && !file.exists()) {
throw new IORuntimeException("File not found in readOnly mode");
}
try {
MappedBytes bytes = MappedBytes.mappedBytes(file, 64 << 10, 0, readOnly);
Wire wire = wireType.apply(bytes);
StoreRecovery recovery = recoverySupplier.apply(wireType);
try {
TableStore tableStore;
if ((!readOnly) && wire.writeFirstHeader()) {
tableStore = writeTableStore(bytes, wire, recovery);
} else {
wire.readFirstHeader(timeoutMS, TimeUnit.MILLISECONDS);
StringBuilder name = Wires.acquireStringBuilder();
ValueIn valueIn = wire.readEventName(name);
if (StringUtils.isEqual(name, MetaDataKeys.header.name())) {
tableStore = valueIn.typedMarshallable();
} else {
// noinspection unchecked
throw new StreamCorruptedException("The first message should be the header, was " + name);
}
}
return tableStore;
} catch (TimeoutException e) {
recovery.recoverAndWriteHeader(wire, 10_000, null, null);
return writeTableStore(bytes, wire, recovery);
}
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
Aggregations