use of net.openhft.chronicle.core.io.IORuntimeException in project Chronicle-Queue by OpenHFT.
the class WriteBytesTest method testWriteBytes.
@Test
public void testWriteBytes() {
File dir = getTmpDir();
try (ChronicleQueue queue = binary(dir).testBlockSize().build()) {
ExcerptAppender appender = queue.acquireAppender();
ExcerptTailer tailer = queue.createTailer();
outgoingMsgBytes[0] = 'A';
outgoingBytes.write(outgoingMsgBytes);
postOneMessage(appender);
fetchOneMessage(tailer, incomingMsgBytes);
// System.out.println(new String(incomingMsgBytes));
outgoingBytes.clear();
outgoingMsgBytes[0] = 'A';
outgoingMsgBytes[1] = 'B';
outgoingBytes.write(outgoingMsgBytes);
postOneMessage(appender);
fetchOneMessage(tailer, incomingMsgBytes);
// System.out.println(new String(incomingMsgBytes));
} finally {
try {
IOTools.deleteDirWithFiles(dir, 2);
} catch (IORuntimeException e) {
// ignored
}
}
}
use of net.openhft.chronicle.core.io.IORuntimeException in project Chronicle-Queue by OpenHFT.
the class MethodReaderBenchmark method init.
@Override
public void init(JLBH jlbh) {
this.jlbh = jlbh;
String benchmarkQueuePath = System.getProperty("benchmarkQueuePath");
if (benchmarkQueuePath != null) {
System.out.println("Creating queue in dir: " + benchmarkQueuePath);
IOTools.deleteDirWithFiles(benchmarkQueuePath, 10);
queue = ChronicleQueue.single(benchmarkQueuePath);
} else {
System.out.println("Creating queue in temp dir");
try {
queue = ChronicleQueue.single(Files.createTempDirectory("temp").toString());
} catch (IOException e) {
throw new IORuntimeException(e);
}
}
appender = queue.acquireAppender();
writer = appender.methodWriter(AnInterface.class);
nextExecutionReport = new ExecutionReportDTO(ThreadLocalRandom.current());
nextOrder = new OrderDTO(ThreadLocalRandom.current());
consumerThread = new Thread(() -> {
try (final AffinityLock affinityLock = AffinityLock.acquireCore()) {
tailer = queue.createTailer().disableThreadSafetyCheck(true);
noArgsCallSampler = jlbh.addProbe("No args call");
oneIntCallSampler = jlbh.addProbe("One int call");
oneLongCallSampler = jlbh.addProbe("One long call");
smallDtoCallSampler = jlbh.addProbe("Small DTO call");
stringAndSmallDtoCallSampler = jlbh.addProbe("String and small DTO call");
bigDtoCallSampler = jlbh.addProbe("Big DTO call");
stringAndBigDtoCallSampler = jlbh.addProbe("String and big DTO call");
final AnInterfaceSamplingImpl samplingImpl = new AnInterfaceSamplingImpl();
reader = tailer.methodReader(samplingImpl);
while (!stopped) {
if (reader.readOne()) {
String startNsString;
do {
startNsString = tailer.readText();
} while (startNsString == null);
long startNs = Long.parseLong(startNsString);
samplingImpl.doSample(startNs);
jlbh.sample(System.nanoTime() - startNs);
}
}
}
});
consumerThread.start();
}
use of net.openhft.chronicle.core.io.IORuntimeException in project Chronicle-Queue by OpenHFT.
the class MarshallableTest method testWriteText.
@Test
public void testWriteText() {
File dir = getTmpDir();
try (ChronicleQueue queue = binary(dir).testBlockSize().build()) {
ExcerptAppender appender = queue.acquireAppender();
ExcerptTailer tailer = queue.createTailer();
ExcerptTailer tailer2 = queue.createTailer();
int runs = 1000;
for (int i = 0; i < runs; i++) appender.writeText("" + i);
for (int i = 0; i < runs; i++) assertEquals("" + i, tailer.readText());
StringBuilder sb = new StringBuilder();
for (int i = 0; i < runs; i++) {
assertTrue(tailer2.readText(sb));
assertEquals("" + i, sb.toString());
}
} finally {
try {
IOTools.deleteDirWithFiles(dir, 2);
} catch (IORuntimeException e) {
// ignored
}
}
}
use of net.openhft.chronicle.core.io.IORuntimeException in project Chronicle-Queue by OpenHFT.
the class SingleTableBuilder method build.
// *************************************************************************
//
// *************************************************************************
@NotNull
public TableStore<T> build() {
if (readOnly) {
if (!file.exists())
throw new IORuntimeException("Metadata file not found in readOnly mode");
// Wait a short time for the file to be initialized
TimingPauser pauser = Pauser.balanced();
try {
while (file.length() < OS.mapAlignment()) {
pauser.pause(1, TimeUnit.SECONDS);
}
} catch (TimeoutException e) {
throw new IORuntimeException("Metadata file found in readOnly mode, but not initialized yet");
}
}
MappedBytes bytes = null;
try {
if (!readOnly && file.createNewFile() && !file.canWrite()) {
throw new IllegalStateException("Cannot write to tablestore file " + file);
}
bytes = MappedBytes.mappedBytes(file, OS.SAFE_PAGE_SIZE, OS.SAFE_PAGE_SIZE, readOnly);
// these MappedBytes are shared, but the assumption is they shouldn't grow. Supports 2K entries.
bytes.disableThreadSafetyCheck(true);
// eagerly initialize backing MappedFile page - otherwise wire.writeFirstHeader() will try to lock the file
// to allocate the first byte store and that will cause lock overlap
bytes.readVolatileInt(0);
Wire wire = wireType.apply(bytes);
if (readOnly)
return SingleTableStore.doWithSharedLock(file, v -> {
try {
return readTableStore(wire);
} catch (IOException ex) {
throw Jvm.rethrow(ex);
}
}, () -> null);
else {
MappedBytes finalBytes = bytes;
return SingleTableStore.doWithExclusiveLock(file, v -> {
try {
if (wire.writeFirstHeader()) {
return writeTableStore(finalBytes, wire);
} else {
return readTableStore(wire);
}
} catch (IOException ex) {
throw Jvm.rethrow(ex);
}
}, () -> null);
}
} catch (IOException e) {
throw new IORuntimeException("file=" + file.getAbsolutePath(), e);
} finally {
if (bytes != null)
bytes.clearUsedByThread();
}
}
use of net.openhft.chronicle.core.io.IORuntimeException in project Chronicle-Queue by OpenHFT.
the class SingleChronicleQueueBuilder method initializeMetadata.
protected void initializeMetadata() {
File metapath = metapath();
validateRollCycle(metapath);
SCQMeta metadata = new SCQMeta(new SCQRoll(rollCycle(), epoch(), rollTime, rollTimeZone), deltaCheckpointInterval(), sourceId());
try {
boolean readOnly = readOnly();
metaStore = SingleTableBuilder.binary(metapath, metadata).readOnly(readOnly).build();
// check if metadata was overridden
SCQMeta newMeta = metaStore.metadata();
sourceId(newMeta.sourceId());
String format = newMeta.roll().format();
if (!format.equals(rollCycle().format())) {
// roll cycle changed
overrideRollCycleForFileName(format);
}
// if it was overridden - reset
rollTime = newMeta.roll().rollTime();
rollTimeZone = newMeta.roll().rollTimeZone();
epoch = newMeta.roll().epoch();
} catch (IORuntimeException ex) {
// readonly=true and file doesn't exist
if (OS.isWindows())
// we cant have a read-only table store on windows so we have no option but to throw the ex.
throw ex;
if (ex.getMessage().equals("Metadata file not found in readOnly mode"))
Jvm.warn().on(getClass(), "Failback to readonly tablestore " + ex);
else
Jvm.warn().on(getClass(), "Failback to readonly tablestore", ex);
metaStore = new ReadonlyTableStore<>(metadata);
}
}
Aggregations