use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.
the class ThroughputPerfMain method main.
public static void main(String[] args) {
String base = path + "/delete-" + Time.uniqueId() + ".me";
long start = System.nanoTime();
long count = 0;
nbs = BytesStore.nativeStoreWithFixedCapacity(size);
long blockSize = OS.is64Bit() ? 4L << 30 : 256L << 20;
try (ChronicleQueue q = ChronicleQueue.singleBuilder(base).rollCycle(RollCycles.LARGE_HOURLY_XSPARSE).blockSize(blockSize).build()) {
ExcerptAppender appender = q.acquireAppender();
long lastIndex = -1;
do {
int defaultIndexSpacing = q.rollCycle().defaultIndexSpacing();
Wire wire = appender.wire();
int writeCount = (int) (defaultIndexSpacing - (lastIndex & (defaultIndexSpacing - 1)) - 1);
if (wire != null && writeCount > 0) {
MappedBytes bytes = (MappedBytes) wire.bytes();
long address = bytes.addressForWrite(bytes.writePosition());
long bstart = bytes.start();
long bcap = bytes.realCapacity();
long canWrite = bcap - (bytes.writePosition() - bstart);
long lengthCount = writeMessages(address, canWrite, writeCount);
bytes.writeSkip((int) lengthCount);
lastIndex += lengthCount >> 32;
count += lengthCount >> 32;
} else {
try (DocumentContext dc = appender.writingDocument()) {
dc.wire().bytes().write(nbs);
}
lastIndex = appender.lastIndexAppended();
count++;
}
} while (start + time * 1e9 > System.nanoTime());
}
nbs.releaseLast();
long mid = System.nanoTime();
long time1 = mid - start;
Bytes bytes = Bytes.allocateElasticDirect(64);
try (ChronicleQueue q = ChronicleQueue.singleBuilder(base).rollCycle(RollCycles.LARGE_HOURLY_XSPARSE).blockSize(blockSize).build()) {
ExcerptTailer tailer = q.createTailer();
for (long i = 0; i < count; i++) {
try (DocumentContext dc = tailer.readingDocument()) {
bytes.clear();
bytes.write(dc.wire().bytes());
}
}
}
bytes.releaseLast();
long end = System.nanoTime();
long time2 = end - mid;
System.out.printf("Writing %,d messages took %.3f seconds, at a rate of %,d per second%n", count, time1 / 1e9, (long) (1e9 * count / time1));
System.out.printf("Reading %,d messages took %.3f seconds, at a rate of %,d per second%n", count, time2 / 1e9, (long) (1e9 * count / time2));
// make sure its cleaned up for windows to delete.
System.gc();
IOTools.deleteDirWithFiles(base, 2);
}
use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.
the class AppenderLockOnlyAppliesToFileTest method concurrentLockItUp.
/*
Failed tests:
AppenderLockOnlyAppliesToFileTest.concurrentLockItUp:59 Writer thread completed before timeout
*/
@Ignore("fails too often")
@Test
public void concurrentLockItUp() throws InterruptedException {
final AtomicReference<String> writerQueueFile = new AtomicReference<>();
final File path = DirectoryUtils.tempDir(this.getClass().getSimpleName());
final SingleChronicleQueueBuilder builder = ChronicleQueueBuilder.single(path).sourceId(1).rollCycle(ROLL_CYCLE).timeoutMS(TIMEOUT_MS);
final String initialFile;
final DocumentContext initialContext = builder.build().acquireAppender().writingDocument();
initialContext.wire().writeText("abcd");
initialFile = getFilename(initialContext);
// don't close context
final long afterInitialWrite = System.currentTimeMillis();
final CountDownLatch writerStarted = new CountDownLatch(1);
final CountDownLatch writerFinished = new CountDownLatch(1);
Thread writerThread = new Thread(() -> {
ExcerptAppender appender = builder.build().acquireAppender();
writerStarted.countDown();
// wait for less than timeout and more than roll
Jvm.pause(WAIT_FOR_ROLL_MS);
try (@NotNull DocumentContext context = appender.writingDocument()) {
// should not have been held up by locking
writerQueueFile.set(getFilename(context));
Wire wire = context.wire();
wire.writeText("hello");
writerFinished.countDown();
}
});
writerThread.start();
assertTrue("Writer thread not started", writerStarted.await(1, TimeUnit.SECONDS));
assertTrue("Writer thread completed before timeout", writerFinished.await(WAIT_FOR_ROLL_MS + 50, TimeUnit.MILLISECONDS));
assertFalse("Threads wrote to different queue cycles, so no locking occurred", initialFile.equals(writerQueueFile.get()));
assertTrue("We are within timeout", System.currentTimeMillis() < afterInitialWrite + TIMEOUT_MS);
ExcerptTailer tailer = builder.build().createTailer();
// this call to readingDocument waits for timeout and logs out ".... resetting header after timeout ...."
try (DocumentContext rd = tailer.readingDocument()) {
assertFalse("We are outside timeout", System.currentTimeMillis() < afterInitialWrite + TIMEOUT_MS);
assertTrue("Something was written", rd.isPresent());
String value = rd.wire().readText();
assertEquals("the first (locked) write is lost", "hello", value);
}
try (DocumentContext rd = tailer.readingDocument()) {
assertFalse("Should be only one message in the queue", rd.isPresent());
}
try {
initialContext.close();
fail("close should have thrown");
} catch (IllegalStateException e) {
// expected for close to throw
}
}
use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.
the class DetectNotReadyEntriesTest method testDeadEntries.
@Test
public void testDeadEntries() throws FileNotFoundException {
// TODO FIX.
if (OS.isWindows())
return;
File dir = new File(OS.TARGET, getClass().getSimpleName() + "-" + System.nanoTime());
dir.mkdir();
MappedBytes bytes = MappedBytes.mappedBytes(new File(dir, "19700101" + SingleChronicleQueue.SUFFIX), 64 << 10);
Wire wire = new BinaryWire(bytes);
try (DocumentContext dc = wire.writingDocument(true)) {
dc.wire().writeEventName(() -> "header").typePrefix(SingleChronicleQueueStore.class).marshallable(w -> {
w.write(() -> "wireType").object(WireType.BINARY);
w.write(() -> "writePosition").int64forBinding(288 + 4 + 17);
w.write(() -> "roll").typedMarshallable(new SCQRoll(RollCycles.DAILY, 0));
w.write(() -> "indexing").typedMarshallable(new SCQIndexing(WireType.BINARY, 32 << 10, 32));
w.write(() -> "lastAcknowledgedIndexReplicated").int64forBinding(0);
});
}
long pos = wire.bytes().writePosition();
try (DocumentContext dc = wire.writingDocument(false)) {
dc.wire().write("test").text("Hello World");
}
assertEquals(17, wire.bytes().readInt(pos));
// make it incomplete, note that the length is removed,
// since writing a length into an incomplete excerpt is not allowed
wire.bytes().writeInt(pos, Wires.NOT_COMPLETE);
assertEquals("--- !!meta-data #binary\n" + "header: !SCQStore {\n" + " wireType: !WireType BINARY,\n" + " writePosition: 309,\n" + " roll: !SCQSRoll {\n" + " length: !int 86400000,\n" + " format: yyyyMMdd,\n" + " epoch: 0\n" + " },\n" + " indexing: !SCQSIndexing {\n" + " indexCount: !int 32768,\n" + " indexSpacing: 32,\n" + " index2Index: 0,\n" + " lastIndex: 0\n" + " },\n" + " lastAcknowledgedIndexReplicated: 0\n" + "}\n" + "# position: 288, header: -1 or 0\n" + "--- !!not-ready-data! #binary\n" + "...\n" + "# 17 bytes remaining\n", Wires.fromSizePrefixedBlobs(bytes.readPosition(0)));
bytes.release();
try (SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(dir).testBlockSize().build()) {
queue.acquireAppender().writeText("Bye for now");
}
try {
IOTools.shallowDeleteDirWithFiles(dir.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
}
use of net.openhft.chronicle.wire.Wire in project Chronicle-Queue by OpenHFT.
the class RollEOFTest method removeEOF.
private void removeEOF(Path path) throws IOException {
long blockSize = 64 << 10;
long chunkSize = OS.pageAlign(blockSize);
long overlapSize = OS.pageAlign(blockSize / 4);
final MappedBytes mappedBytes = MappedBytes.mappedBytes(path.toFile(), chunkSize, overlapSize, false);
mappedBytes.reserve();
try {
final Wire wire = WireType.BINARY_LIGHT.apply(mappedBytes);
final Bytes<?> bytes = wire.bytes();
bytes.readLimit(bytes.capacity());
bytes.readSkip(4);
// move past header
try (final SingleChronicleQueueStore qs = loadStore(wire)) {
assertNotNull(qs);
long l = qs.writePosition();
long len = Wires.lengthOf(bytes.readVolatileInt(l));
long eofOffset = l + len + 4L;
bytes.writePosition(eofOffset);
bytes.writeInt(0);
}
} finally {
mappedBytes.release();
}
}
use of net.openhft.chronicle.wire.Wire in project kie-wb-common by kiegroup.
the class ClientIPCImpl method readThisDocument.
private DefaultKieCompilationResponseOffProcess readThisDocument(ExcerptTailer tailer) {
if (logger.isDebugEnabled()) {
logger.debug("current index on readThisDocument:{}", tailer.index());
}
DefaultKieCompilationResponseOffProcess res = null;
try (DocumentContext dc = tailer.readingDocument()) {
if (dc.isPresent()) {
if (logger.isDebugEnabled()) {
logger.debug("Document Context index:{}", dc.index());
}
Wire wire = dc.wire();
Bytes bytes = wire.bytes();
if (!bytes.isEmpty()) {
try {
Object obj = deserialize(bytes.toByteArray());
res = (DefaultKieCompilationResponseOffProcess) obj;
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}
}
}
if (res == null) {
res = new DefaultKieCompilationResponseOffProcess(false, "");
}
return res;
}
Aggregations