use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.
the class SingleCQFormat2Test method expected.
private static void expected(@NotNull ExcerptTailer tailer, String expected) {
try (DocumentContext dc = tailer.readingDocument()) {
assertTrue(dc.isPresent());
Bytes bytes2 = Bytes.allocateDirect(128);
dc.wire().copyTo(new TextWire(bytes2));
assertEquals(expected, bytes2.toString());
}
}
use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.
the class ChronicleQueueTwoThreads method doTest.
void doTest(boolean buffered) throws InterruptedException {
File name = getTmpDir();
AtomicLong counter = new AtomicLong();
Thread tailerThread = new Thread(() -> {
AffinityLock rlock = AffinityLock.acquireLock();
Bytes bytes = NativeBytes.nativeBytes(BYTES_LENGTH).unchecked(true);
try (ChronicleQueue rqueue = SingleChronicleQueueBuilder.fieldlessBinary(name).testBlockSize().build()) {
ExcerptTailer tailer = rqueue.createTailer();
while (!Thread.interrupted()) {
bytes.clear();
if (tailer.readBytes(bytes)) {
counter.incrementAndGet();
}
}
} finally {
if (rlock != null) {
rlock.release();
}
System.out.printf("Read %,d messages", counter.intValue());
}
}, "tailer thread");
long runs = 50_000;
Thread appenderThread = new Thread(() -> {
AffinityLock wlock = AffinityLock.acquireLock();
try {
ChronicleQueue wqueue = SingleChronicleQueueBuilder.fieldlessBinary(name).rollCycle(SMALL_DAILY).testBlockSize().buffered(buffered).build();
ExcerptAppender appender = wqueue.acquireAppender();
Bytes bytes = Bytes.allocateDirect(BYTES_LENGTH).unchecked(true);
long next = System.nanoTime() + INTERVAL_US * 1000;
for (int i = 0; i < runs; i++) {
while (System.nanoTime() < next) /* busy wait*/
;
long start = next;
bytes.readPositionRemaining(0, BYTES_LENGTH);
bytes.writeLong(0L, start);
appender.writeBytes(bytes);
next += INTERVAL_US * 1000;
}
wqueue.close();
} finally {
if (wlock != null) {
wlock.release();
}
}
}, "appender thread");
tailerThread.start();
Jvm.pause(100);
appenderThread.start();
appenderThread.join();
// Pause to allow tailer to catch up (if needed)
for (int i = 0; i < 10; i++) {
if (runs != counter.get())
Jvm.pause(Jvm.isDebug() ? 10000 : 100);
}
for (int i = 0; i < 10; i++) {
tailerThread.interrupt();
tailerThread.join(100);
}
assertEquals(runs, counter.get());
}
use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.
the class DirectChronicleQueueStringTest method readSome.
private void readSome(DirectChronicleQueue chronicle) {
NativeBytesStore allocate = NativeBytesStore.nativeStoreWithFixedCapacity(EXPECTED_BYTES.length);
final Bytes toRead = allocate.bytes();
AtomicLong offset = new AtomicLong(chronicle.firstBytes());
for (int i = 0; i < RUNS; i++) {
toRead.clear();
chronicle.readDocument(offset, toRead);
}
}
use of net.openhft.chronicle.bytes.Bytes 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;
}
use of net.openhft.chronicle.bytes.Bytes in project Chronicle-Queue by OpenHFT.
the class MicroToucher method execute.
public boolean execute() {
final Wire bufferWire = appender.wire();
if (bufferWire == null)
return false;
final long lastPosition = appender.lastPosition;
final long lastPage = lastPosition & ~0xFFF;
final long nextPage = (lastPosition + 0xFFF) & ~0xFFF;
Bytes bytes = bufferWire.bytes();
if (nextPage != lastPageTouched) {
lastPageTouched = nextPage;
try {
// best effort
final BytesStore bs = bytes.bytesStore();
if (bs.inside(nextPage, 8))
touchPage(nextPage, bs);
} catch (Throwable ignored) {
}
return true;
}
lastPageToSync = lastPage;
return false;
}
Aggregations