use of net.openhft.chronicle.threads.Pauser in project Chronicle-Queue by OpenHFT.
the class SCQIndexing method getSecondaryAddress0.
private long getSecondaryAddress0(@NotNull ExcerptContext ec, long timeoutMS, @NotNull LongArrayValues index2indexArr, int index2) throws TimeoutException {
long secondaryAddress;
Pauser pauser = ec.wireForIndex().pauser();
while (true) {
secondaryAddress = index2indexArr.getVolatileValueAt(index2);
if (secondaryAddress == BinaryLongReference.LONG_NOT_COMPLETE) {
pauser.pause(timeoutMS, TimeUnit.MILLISECONDS);
} else {
pauser.reset();
break;
}
}
return secondaryAddress;
}
use of net.openhft.chronicle.threads.Pauser in project Chronicle-Queue by OpenHFT.
the class SCQIndexing method acquireIndex2Index0.
long acquireIndex2Index0(@NotNull StoreRecovery recovery, @NotNull ExcerptContext ec, long timeoutMS) throws EOFException, TimeoutException, UnrecoverableTimeoutException, StreamCorruptedException {
long start = System.currentTimeMillis();
Pauser pauser = ec.wireForIndex().pauser();
try {
do {
long index2Index = this.index2Index.getVolatileValue();
if (index2Index == BinaryLongReference.LONG_NOT_COMPLETE) {
pauser.pause(timeoutMS, TimeUnit.MILLISECONDS);
continue;
}
if (index2Index != NOT_INITIALIZED)
return index2Index;
if (!this.index2Index.compareAndSwapValue(NOT_INITIALIZED, BinaryLongReference.LONG_NOT_COMPLETE))
continue;
long index = NOT_INITIALIZED;
try {
index = newIndex(recovery, ec, true, timeoutMS);
} finally {
this.index2Index.setOrderedValue(index);
}
return index;
} while (System.currentTimeMillis() < start + timeoutMS);
} finally {
pauser.reset();
}
throw new TimeoutException("index2index NOT_COMPLETE for too long.");
}
use of net.openhft.chronicle.threads.Pauser in project Chronicle-Queue by OpenHFT.
the class JDBCService method runLoop.
void runLoop() {
try {
JDBCResult result = out.acquireAppender().methodWriterBuilder(JDBCResult.class).recordHistory(true).get();
JDBCComponent js = new JDBCComponent(connectionSupplier, result);
MethodReader reader = in.createTailer().afterLastWritten(out).methodReader(js);
Pauser pauser = Pauser.millis(1, 10);
while (!closed) {
if (reader.readOne())
pauser.reset();
else
pauser.pause();
}
} catch (Throwable t) {
LOGGER.warn("Run loop exited", t);
}
}
use of net.openhft.chronicle.threads.Pauser 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.threads.Pauser in project Chronicle-Queue by OpenHFT.
the class TSQueueLock method acquireLock.
/**
* Stores current TID and PID to table store, and any other thread trying to acquire lock will wait for
* <code>chronicle.queue.lock.timeoutMS</code> millis (default is 30000) for the lock to be released, and if it is not
* able to lock, *overrides the lock*.
*/
@Override
public void acquireLock() {
throwExceptionIfClosed();
long tid = Thread.currentThread().getId();
if (isLockHeldByCurrentThread(tid)) {
return;
}
int count = 0;
long lockValueFromTid = getLockValueFromTid(tid);
long value = lock.getVolatileValue();
Pauser tlPauser = pauser.get();
try {
while (!lock.compareAndSwapValue(UNLOCKED, lockValueFromTid)) {
if (count++ > 1000 && Thread.currentThread().isInterrupted())
throw new InterruptedRuntimeException("Interrupted");
tlPauser.pause(timeout, TimeUnit.MILLISECONDS);
value = lock.getVolatileValue();
}
} catch (TimeoutException e) {
warnAndForceUnlock("Couldn't acquire lock", value);
acquireLock();
} finally {
tlPauser.reset();
}
}
Aggregations