use of net.openhft.chronicle.core.io.IORuntimeException 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);
}
}
use of net.openhft.chronicle.core.io.IORuntimeException in project Chronicle-Queue by OpenHFT.
the class SingleTableStore method acquireValueFor.
/**
* {@inheritDoc}
*/
@Override
public synchronized LongValue acquireValueFor(CharSequence key) {
// TODO Change to ThreadLocal values if performance is a problem.
StringBuilder sb = Wires.acquireStringBuilder();
mappedBytes.reserve();
try {
mappedBytes.readPosition(0);
mappedBytes.readLimit(mappedBytes.realCapacity());
while (mappedWire.readDataHeader()) {
int header = mappedBytes.readInt();
if (Wires.isNotComplete(header))
break;
long readPosition = mappedBytes.readPosition();
int length = Wires.lengthOf(header);
ValueIn valueIn = mappedWire.readEventName(sb);
if (StringUtils.equalsCaseIgnore(key, sb)) {
return valueIn.int64ForBinding(null);
}
mappedBytes.readPosition(readPosition + length);
}
// not found
int safeLength = Maths.toUInt31(mappedBytes.realCapacity() - mappedBytes.readPosition());
mappedBytes.writeLimit(mappedBytes.realCapacity());
mappedBytes.writePosition(mappedBytes.readPosition());
long pos = recovery.writeHeader(mappedWire, safeLength, timeoutMS, null, null);
LongValue longValue = wireType.newLongReference().get();
mappedWire.writeEventName(key).int64forBinding(Long.MIN_VALUE, longValue);
mappedWire.updateHeader(pos, false);
return longValue;
} catch (StreamCorruptedException | EOFException e) {
throw new IORuntimeException(e);
} finally {
mappedBytes.release();
}
}
use of net.openhft.chronicle.core.io.IORuntimeException in project Chronicle-Queue by OpenHFT.
the class RollingCycleTest method testRollCycle.
@Test
public void testRollCycle() throws InterruptedException {
SetTimeProvider stp = new SetTimeProvider();
long start = System.currentTimeMillis() - 3 * 86_400_000;
stp.currentTimeMillis(start);
String basePath = OS.TARGET + "/testRollCycle" + System.nanoTime();
try (final ChronicleQueue queue = ChronicleQueueBuilder.single(basePath).testBlockSize().timeoutMS(5).rollCycle(RollCycles.TEST_DAILY).timeProvider(stp).build()) {
final ExcerptAppender appender = queue.acquireAppender().lazyIndexing(lazyIndexing);
int numWritten = 0;
for (int h = 0; h < 3; h++) {
stp.currentTimeMillis(start + TimeUnit.DAYS.toMillis(h));
for (int i = 0; i < 3; i++) {
appender.writeBytes(new TestBytesMarshallable(i));
numWritten++;
}
}
String expectedEager = "--- !!meta-data #binary\n" + "header: !SCQStore {\n" + " wireType: !WireType BINARY_LIGHT,\n" + " writePosition: [\n" + " 730,\n" + " 3135326126082\n" + " ],\n" + " roll: !SCQSRoll {\n" + " length: !int 86400000,\n" + " format: yyyyMMdd,\n" + " epoch: 0\n" + " },\n" + " indexing: !SCQSIndexing {\n" + " indexCount: 8,\n" + " indexSpacing: 1,\n" + " index2Index: 442,\n" + " lastIndex: 3\n" + " },\n" + " lastAcknowledgedIndexReplicated: -1,\n" + " recovery: !TimedStoreRecovery {\n" + " timeStamp: 0\n" + " },\n" + " deltaCheckpointInterval: 0,\n" + " lastIndexReplicated: -1,\n" + " sourceId: 0\n" + "}\n" + "# position: 442, header: -1\n" + "--- !!meta-data #binary\n" + "index2index: [\n" + " # length: 8, used: 1\n" + " 544,\n" + " 0, 0, 0, 0, 0, 0, 0\n" + "]\n" + "# position: 544, header: -1\n" + "--- !!meta-data #binary\n" + "index: [\n" + " # length: 8, used: 3\n" + " 640,\n" + " 685,\n" + " 730,\n" + " 0, 0, 0, 0, 0\n" + "]\n" + "# position: 640, header: 0\n" + "--- !!data #binary\n" + "00000280 10 6e 61 6d 65 5f 2d 31 31 35 35 34 ·nam e_-11554\n" + "00000290 38 34 35 37 36 7a cb 93 3d 38 51 d9 d4 f6 c9 2d 84576z·· =8Q····-\n" + "000002a0 a3 bd 70 39 9b b7 70 e9 8c 39 f0 1d 4f ··p9··p· ·9··O \n" + "# position: 685, header: 1\n" + "--- !!data #binary\n" + "000002b0 10 6e 61 6d 65 5f 2d 31 31 35 35 38 36 39 33 ·name_- 11558693\n" + "000002c0 32 35 6f 0e fb 68 d8 9c b8 19 fc cc 2c 35 92 f9 25o··h·· ····,5··\n" + "000002d0 4d 68 e5 f1 2c 55 f0 b8 46 09 Mh··,U·· F· \n" + "# position: 730, header: 2\n" + "--- !!data #binary\n" + "000002d0 10 6e ·n\n" + "000002e0 61 6d 65 5f 2d 31 31 35 34 37 31 35 30 37 39 90 ame_-115 4715079·\n" + "000002f0 45 c5 e6 f7 b9 1a 4b ea c3 2f 7f 17 5f 10 01 5c E·····K· ·/··_··\\\n" + "00000300 6e 62 fc cc 5e cc da nb··^·· \n" + "# position: 775, header: 2 EOF\n" + "--- !!not-ready-meta-data! #binary\n" + "...\n" + "# 130293 bytes remaining\n" + "--- !!meta-data #binary\n" + "header: !SCQStore {\n" + " wireType: !WireType BINARY_LIGHT,\n" + " writePosition: [\n" + " 730,\n" + " 3135326126082\n" + " ],\n" + " roll: !SCQSRoll {\n" + " length: !int 86400000,\n" + " format: yyyyMMdd,\n" + " epoch: 0\n" + " },\n" + " indexing: !SCQSIndexing {\n" + " indexCount: 8,\n" + " indexSpacing: 1,\n" + " index2Index: 442,\n" + " lastIndex: 3\n" + " },\n" + " lastAcknowledgedIndexReplicated: -1,\n" + " recovery: !TimedStoreRecovery {\n" + " timeStamp: 0\n" + " },\n" + " deltaCheckpointInterval: 0,\n" + " lastIndexReplicated: -1,\n" + " sourceId: 0\n" + "}\n" + "# position: 442, header: -1\n" + "--- !!meta-data #binary\n" + "index2index: [\n" + " # length: 8, used: 1\n" + " 544,\n" + " 0, 0, 0, 0, 0, 0, 0\n" + "]\n" + "# position: 544, header: -1\n" + "--- !!meta-data #binary\n" + "index: [\n" + " # length: 8, used: 3\n" + " 640,\n" + " 685,\n" + " 730,\n" + " 0, 0, 0, 0, 0\n" + "]\n" + "# position: 640, header: 0\n" + "--- !!data #binary\n" + "00000280 10 6e 61 6d 65 5f 2d 31 31 35 35 34 ·nam e_-11554\n" + "00000290 38 34 35 37 36 7a cb 93 3d 38 51 d9 d4 f6 c9 2d 84576z·· =8Q····-\n" + "000002a0 a3 bd 70 39 9b b7 70 e9 8c 39 f0 1d 4f ··p9··p· ·9··O \n" + "# position: 685, header: 1\n" + "--- !!data #binary\n" + "000002b0 10 6e 61 6d 65 5f 2d 31 31 35 35 38 36 39 33 ·name_- 11558693\n" + "000002c0 32 35 6f 0e fb 68 d8 9c b8 19 fc cc 2c 35 92 f9 25o··h·· ····,5··\n" + "000002d0 4d 68 e5 f1 2c 55 f0 b8 46 09 Mh··,U·· F· \n" + "# position: 730, header: 2\n" + "--- !!data #binary\n" + "000002d0 10 6e ·n\n" + "000002e0 61 6d 65 5f 2d 31 31 35 34 37 31 35 30 37 39 90 ame_-115 4715079·\n" + "000002f0 45 c5 e6 f7 b9 1a 4b ea c3 2f 7f 17 5f 10 01 5c E·····K· ·/··_··\\\n" + "00000300 6e 62 fc cc 5e cc da nb··^·· \n" + "# position: 775, header: 2 EOF\n" + "--- !!not-ready-meta-data! #binary\n" + "...\n" + "# 130293 bytes remaining\n" + "--- !!meta-data #binary\n" + "header: !SCQStore {\n" + " wireType: !WireType BINARY_LIGHT,\n" + " writePosition: [\n" + " 730,\n" + " 3135326126082\n" + " ],\n" + " roll: !SCQSRoll {\n" + " length: !int 86400000,\n" + " format: yyyyMMdd,\n" + " epoch: 0\n" + " },\n" + " indexing: !SCQSIndexing {\n" + " indexCount: 8,\n" + " indexSpacing: 1,\n" + " index2Index: 442,\n" + " lastIndex: 3\n" + " },\n" + " lastAcknowledgedIndexReplicated: -1,\n" + " recovery: !TimedStoreRecovery {\n" + " timeStamp: 0\n" + " },\n" + " deltaCheckpointInterval: 0,\n" + " lastIndexReplicated: -1,\n" + " sourceId: 0\n" + "}\n" + "# position: 442, header: -1\n" + "--- !!meta-data #binary\n" + "index2index: [\n" + " # length: 8, used: 1\n" + " 544,\n" + " 0, 0, 0, 0, 0, 0, 0\n" + "]\n" + "# position: 544, header: -1\n" + "--- !!meta-data #binary\n" + "index: [\n" + " # length: 8, used: 3\n" + " 640,\n" + " 685,\n" + " 730,\n" + " 0, 0, 0, 0, 0\n" + "]\n" + "# position: 640, header: 0\n" + "--- !!data #binary\n" + "00000280 10 6e 61 6d 65 5f 2d 31 31 35 35 34 ·nam e_-11554\n" + "00000290 38 34 35 37 36 7a cb 93 3d 38 51 d9 d4 f6 c9 2d 84576z·· =8Q····-\n" + "000002a0 a3 bd 70 39 9b b7 70 e9 8c 39 f0 1d 4f ··p9··p· ·9··O \n" + "# position: 685, header: 1\n" + "--- !!data #binary\n" + "000002b0 10 6e 61 6d 65 5f 2d 31 31 35 35 38 36 39 33 ·name_- 11558693\n" + "000002c0 32 35 6f 0e fb 68 d8 9c b8 19 fc cc 2c 35 92 f9 25o··h·· ····,5··\n" + "000002d0 4d 68 e5 f1 2c 55 f0 b8 46 09 Mh··,U·· F· \n" + "# position: 730, header: 2\n" + "--- !!data #binary\n" + "000002d0 10 6e ·n\n" + "000002e0 61 6d 65 5f 2d 31 31 35 34 37 31 35 30 37 39 90 ame_-115 4715079·\n" + "000002f0 45 c5 e6 f7 b9 1a 4b ea c3 2f 7f 17 5f 10 01 5c E·····K· ·/··_··\\\n" + "00000300 6e 62 fc cc 5e cc da nb··^·· \n" + "...\n" + "# 130293 bytes remaining\n";
String expectedLazy = "--- !!meta-data #binary\n" + "header: !SCQStore {\n" + " wireType: !WireType BINARY_LIGHT,\n" + " writePosition: [\n" + " 532,\n" + " 0\n" + " ],\n" + " roll: !SCQSRoll {\n" + " length: !int 86400000,\n" + " format: yyyyMMdd,\n" + " epoch: 0\n" + " },\n" + " indexing: !SCQSIndexing {\n" + " indexCount: 8,\n" + " indexSpacing: 1,\n" + " index2Index: 0,\n" + " lastIndex: 0\n" + " },\n" + " lastAcknowledgedIndexReplicated: -1,\n" + " recovery: !TimedStoreRecovery {\n" + " timeStamp: 0\n" + " },\n" + " deltaCheckpointInterval: 0,\n" + " lastIndexReplicated: -1,\n" + " sourceId: 0\n" + "}\n" + "# position: 442, header: 0\n" + "--- !!data #binary\n" + "000001b0 10 6e ·n\n" + "000001c0 61 6d 65 5f 2d 31 31 35 35 34 38 34 35 37 36 7a ame_-115 5484576z\n" + "000001d0 cb 93 3d 38 51 d9 d4 f6 c9 2d a3 bd 70 39 9b b7 ··=8Q··· ·-··p9··\n" + "000001e0 70 e9 8c 39 f0 1d 4f p··9··O \n" + "# position: 487, header: 1\n" + "--- !!data #binary\n" + "000001e0 10 6e 61 6d 65 ·name\n" + "000001f0 5f 2d 31 31 35 35 38 36 39 33 32 35 6f 0e fb 68 _-115586 9325o··h\n" + "00000200 d8 9c b8 19 fc cc 2c 35 92 f9 4d 68 e5 f1 2c 55 ······,5 ··Mh··,U\n" + "00000210 f0 b8 46 09 ··F· \n" + "# position: 532, header: 2\n" + "--- !!data #binary\n" + "00000210 10 6e 61 6d 65 5f 2d 31 ·name_-1\n" + "00000220 31 35 34 37 31 35 30 37 39 90 45 c5 e6 f7 b9 1a 15471507 9·E·····\n" + "00000230 4b ea c3 2f 7f 17 5f 10 01 5c 6e 62 fc cc 5e cc K··/··_· ·\\nb··^·\n" + "00000240 da · \n" + "# position: 577, header: 2 EOF\n" + "--- !!not-ready-meta-data! #binary\n" + "...\n" + "# 130491 bytes remaining\n" + "--- !!meta-data #binary\n" + "header: !SCQStore {\n" + " wireType: !WireType BINARY_LIGHT,\n" + " writePosition: [\n" + " 532,\n" + " 0\n" + " ],\n" + " roll: !SCQSRoll {\n" + " length: !int 86400000,\n" + " format: yyyyMMdd,\n" + " epoch: 0\n" + " },\n" + " indexing: !SCQSIndexing {\n" + " indexCount: 8,\n" + " indexSpacing: 1,\n" + " index2Index: 0,\n" + " lastIndex: 0\n" + " },\n" + " lastAcknowledgedIndexReplicated: -1,\n" + " recovery: !TimedStoreRecovery {\n" + " timeStamp: 0\n" + " },\n" + " deltaCheckpointInterval: 0,\n" + " lastIndexReplicated: -1,\n" + " sourceId: 0\n" + "}\n" + "# position: 442, header: 0\n" + "--- !!data #binary\n" + "000001b0 10 6e ·n\n" + "000001c0 61 6d 65 5f 2d 31 31 35 35 34 38 34 35 37 36 7a ame_-115 5484576z\n" + "000001d0 cb 93 3d 38 51 d9 d4 f6 c9 2d a3 bd 70 39 9b b7 ··=8Q··· ·-··p9··\n" + "000001e0 70 e9 8c 39 f0 1d 4f p··9··O \n" + "# position: 487, header: 1\n" + "--- !!data #binary\n" + "000001e0 10 6e 61 6d 65 ·name\n" + "000001f0 5f 2d 31 31 35 35 38 36 39 33 32 35 6f 0e fb 68 _-115586 9325o··h\n" + "00000200 d8 9c b8 19 fc cc 2c 35 92 f9 4d 68 e5 f1 2c 55 ······,5 ··Mh··,U\n" + "00000210 f0 b8 46 09 ··F· \n" + "# position: 532, header: 2\n" + "--- !!data #binary\n" + "00000210 10 6e 61 6d 65 5f 2d 31 ·name_-1\n" + "00000220 31 35 34 37 31 35 30 37 39 90 45 c5 e6 f7 b9 1a 15471507 9·E·····\n" + "00000230 4b ea c3 2f 7f 17 5f 10 01 5c 6e 62 fc cc 5e cc K··/··_· ·\\nb··^·\n" + "00000240 da · \n" + "# position: 577, header: 2 EOF\n" + "--- !!not-ready-meta-data! #binary\n" + "...\n" + "# 130491 bytes remaining\n" + "--- !!meta-data #binary\n" + "header: !SCQStore {\n" + " wireType: !WireType BINARY_LIGHT,\n" + " writePosition: [\n" + " 532,\n" + " 0\n" + " ],\n" + " roll: !SCQSRoll {\n" + " length: !int 86400000,\n" + " format: yyyyMMdd,\n" + " epoch: 0\n" + " },\n" + " indexing: !SCQSIndexing {\n" + " indexCount: 8,\n" + " indexSpacing: 1,\n" + " index2Index: 0,\n" + " lastIndex: 0\n" + " },\n" + " lastAcknowledgedIndexReplicated: -1,\n" + " recovery: !TimedStoreRecovery {\n" + " timeStamp: 0\n" + " },\n" + " deltaCheckpointInterval: 0,\n" + " lastIndexReplicated: -1,\n" + " sourceId: 0\n" + "}\n" + "# position: 442, header: 0\n" + "--- !!data #binary\n" + "000001b0 10 6e ·n\n" + "000001c0 61 6d 65 5f 2d 31 31 35 35 34 38 34 35 37 36 7a ame_-115 5484576z\n" + "000001d0 cb 93 3d 38 51 d9 d4 f6 c9 2d a3 bd 70 39 9b b7 ··=8Q··· ·-··p9··\n" + "000001e0 70 e9 8c 39 f0 1d 4f p··9··O \n" + "# position: 487, header: 1\n" + "--- !!data #binary\n" + "000001e0 10 6e 61 6d 65 ·name\n" + "000001f0 5f 2d 31 31 35 35 38 36 39 33 32 35 6f 0e fb 68 _-115586 9325o··h\n" + "00000200 d8 9c b8 19 fc cc 2c 35 92 f9 4d 68 e5 f1 2c 55 ······,5 ··Mh··,U\n" + "00000210 f0 b8 46 09 ··F· \n" + "# position: 532, header: 2\n" + "--- !!data #binary\n" + "00000210 10 6e 61 6d 65 5f 2d 31 ·name_-1\n" + "00000220 31 35 34 37 31 35 30 37 39 90 45 c5 e6 f7 b9 1a 15471507 9·E·····\n" + "00000230 4b ea c3 2f 7f 17 5f 10 01 5c 6e 62 fc cc 5e cc K··/··_· ·\\nb··^·\n" + "00000240 da · \n" + "...\n" + "# 130491 bytes remaining\n";
assertEquals(lazyIndexing ? expectedLazy : expectedEager, queue.dump());
System.out.println("Wrote: " + numWritten + " messages");
long numRead = 0;
final TestBytesMarshallable reusableData = new TestBytesMarshallable(0);
final ExcerptTailer currentPosTailer = queue.createTailer().toStart();
final ExcerptTailer endPosTailer = queue.createTailer().toEnd();
while (currentPosTailer.index() < endPosTailer.index()) {
try {
assertTrue(currentPosTailer.readBytes(reusableData));
} catch (AssertionError e) {
System.err.println("Could not read data at index: " + numRead + " " + Long.toHexString(currentPosTailer.cycle()) + " " + Long.toHexString(currentPosTailer.index()) + " " + e.getMessage() + " " + e);
throw e;
}
numRead++;
}
assertFalse(currentPosTailer.readBytes(reusableData));
System.out.println("Wrote " + numWritten + " Read " + numRead);
try {
IOTools.deleteDirWithFiles(basePath, 2);
} catch (IORuntimeException e) {
e.printStackTrace();
}
}
}
use of net.openhft.chronicle.core.io.IORuntimeException in project Chronicle-Queue by OpenHFT.
the class HelloWorldTest method testWithAsQueueService.
@Test
public void testWithAsQueueService() {
// acts as three processes in one test
// process A writes to the HelloWorld interface.
// process B read fromt he HelloWorld interface and writes to the
String input = OS.TARGET + "/input-" + System.nanoTime();
String output = OS.TARGET + "/output-" + System.nanoTime();
HelloReplier replier = createMock(HelloReplier.class);
replier.reply("Hello April");
replier.reply("Hello June");
replay(replier);
ServiceWrapperBuilder<HelloReplier> builder = ServiceWrapperBuilder.serviceBuilder(input, output, HelloReplier.class, HelloWorldImpl::new).inputSourceId(1).outputSourceId(2);
try (CloseableHelloWorld helloWorld = builder.inputWriter(CloseableHelloWorld.class);
MethodReader replyReader = builder.outputReader(replier);
ServiceWrapper helloWorldService = builder.get()) {
helloWorld.hello("April");
helloWorld.hello("June");
// System.out.println(helloWorldService.inputQueues()[0].dump());
for (int i = 0; i < 2; i++) {
while (!replyReader.readOne()) {
Thread.yield();
}
}
// System.out.println(helloWorldService.outputQueue().dump());
verify(replier);
} finally {
try {
IOTools.deleteDirWithFiles(new File(input), 2);
IOTools.deleteDirWithFiles(new File(output), 2);
} catch (IORuntimeException e) {
e.printStackTrace();
}
}
}
Aggregations