Search in sources :

Example 1 with Pauser

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;
}
Also used : Pauser(net.openhft.chronicle.threads.Pauser)

Example 2 with Pauser

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.");
}
Also used : Pauser(net.openhft.chronicle.threads.Pauser) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with Pauser

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);
    }
}
Also used : MethodReader(net.openhft.chronicle.bytes.MethodReader) Pauser(net.openhft.chronicle.threads.Pauser)

Example 4 with Pauser

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();
    }
}
Also used : IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) StreamCorruptedException(java.io.StreamCorruptedException) TimingPauser(net.openhft.chronicle.threads.TimingPauser) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) Builder(net.openhft.chronicle.core.util.Builder) Wire(net.openhft.chronicle.wire.Wire) WireType(net.openhft.chronicle.wire.WireType) Jvm(net.openhft.chronicle.core.Jvm) File(java.io.File) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) ValueIn(net.openhft.chronicle.wire.ValueIn) StringUtils(net.openhft.chronicle.core.util.StringUtils) CLASS_ALIASES(net.openhft.chronicle.core.pool.ClassAliasPool.CLASS_ALIASES) OS(net.openhft.chronicle.core.OS) NotNull(org.jetbrains.annotations.NotNull) MetaDataKeys(net.openhft.chronicle.queue.impl.single.MetaDataKeys) Path(java.nio.file.Path) Pauser(net.openhft.chronicle.threads.Pauser) Wires(net.openhft.chronicle.wire.Wires) TableStore(net.openhft.chronicle.queue.impl.TableStore) IORuntimeException(net.openhft.chronicle.core.io.IORuntimeException) TimingPauser(net.openhft.chronicle.threads.TimingPauser) IOException(java.io.IOException) Wire(net.openhft.chronicle.wire.Wire) MappedBytes(net.openhft.chronicle.bytes.MappedBytes) TimeoutException(java.util.concurrent.TimeoutException) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with Pauser

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();
    }
}
Also used : InterruptedRuntimeException(net.openhft.chronicle.core.threads.InterruptedRuntimeException) TimingPauser(net.openhft.chronicle.threads.TimingPauser) Pauser(net.openhft.chronicle.threads.Pauser) TimeoutException(java.util.concurrent.TimeoutException) UnrecoverableTimeoutException(net.openhft.chronicle.wire.UnrecoverableTimeoutException)

Aggregations

Pauser (net.openhft.chronicle.threads.Pauser)8 TimeoutException (java.util.concurrent.TimeoutException)4 TimingPauser (net.openhft.chronicle.threads.TimingPauser)3 File (java.io.File)2 IORuntimeException (net.openhft.chronicle.core.io.IORuntimeException)2 InterruptedRuntimeException (net.openhft.chronicle.core.threads.InterruptedRuntimeException)2 ExcerptTailer (net.openhft.chronicle.queue.ExcerptTailer)2 UnrecoverableTimeoutException (net.openhft.chronicle.wire.UnrecoverableTimeoutException)2 ValueIn (net.openhft.chronicle.wire.ValueIn)2 Arguments (io.airlift.airline.Arguments)1 Command (io.airlift.airline.Command)1 Option (io.airlift.airline.Option)1 Unpooled (io.netty.buffer.Unpooled)1 IOException (java.io.IOException)1 StreamCorruptedException (java.io.StreamCorruptedException)1 BufferUnderflowException (java.nio.BufferUnderflowException)1 ByteBuffer (java.nio.ByteBuffer)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1