Search in sources :

Example 1 with TimingPauser

use of net.openhft.chronicle.threads.TimingPauser in project Chronicle-Queue by OpenHFT.

the class TableStoreWriteLock method lock.

/**
 * Guaranteed to succeed in getting the lock (may involve timeout and recovery) or else throw.
 * <p>This is not re-entrant i.e. if you lock and try and lock again it will timeout and recover
 */
@Override
public void lock() {
    throwExceptionIfClosed();
    assert checkNotAlreadyLocked();
    long currentLockValue = 0;
    TimingPauser tlPauser = pauser.get();
    try {
        currentLockValue = lock.getVolatileValue();
        while (!lock.compareAndSwapValue(UNLOCKED, PID)) {
            if (Thread.currentThread().isInterrupted())
                throw new InterruptedRuntimeException("Interrupted for the lock file:" + path);
            tlPauser.pause(timeout, TimeUnit.MILLISECONDS);
            currentLockValue = lock.getVolatileValue();
        }
        // noinspection ConstantConditions,AssertWithSideEffects
        assert (lockedByThread = Thread.currentThread()) != null && (lockedHere = new StackTrace()) != null;
    // success
    } catch (TimeoutException e) {
        final String lockedBy = getLockedBy(currentLockValue);
        final String warningMsg = "Couldn't acquire write lock " + "after " + timeout + " ms " + "for the lock file:" + path + ". " + "Lock was held by " + lockedBy;
        if (forceUnlockOnTimeoutWhen == UnlockMode.NEVER)
            throw new UnrecoverableTimeoutException(new IllegalStateException(warningMsg + UNLOCK_MAIN_MSG));
        else if (forceUnlockOnTimeoutWhen == UnlockMode.LOCKING_PROCESS_DEAD) {
            if (forceUnlockIfProcessIsDead())
                lock();
            else
                throw new UnrecoverableTimeoutException(new IllegalStateException(warningMsg + UNLOCK_MAIN_MSG));
        } else {
            warn().on(getClass(), warningMsg + UNLOCKING_FORCIBLY_MSG);
            forceUnlock(currentLockValue);
            lock();
        }
    } finally {
        tlPauser.reset();
    }
}
Also used : UnrecoverableTimeoutException(net.openhft.chronicle.wire.UnrecoverableTimeoutException) InterruptedRuntimeException(net.openhft.chronicle.core.threads.InterruptedRuntimeException) TimingPauser(net.openhft.chronicle.threads.TimingPauser) StackTrace(net.openhft.chronicle.core.StackTrace) UnrecoverableTimeoutException(net.openhft.chronicle.wire.UnrecoverableTimeoutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with TimingPauser

use of net.openhft.chronicle.threads.TimingPauser 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)

Aggregations

TimeoutException (java.util.concurrent.TimeoutException)2 TimingPauser (net.openhft.chronicle.threads.TimingPauser)2 File (java.io.File)1 IOException (java.io.IOException)1 StreamCorruptedException (java.io.StreamCorruptedException)1 Path (java.nio.file.Path)1 Objects (java.util.Objects)1 TimeUnit (java.util.concurrent.TimeUnit)1 MappedBytes (net.openhft.chronicle.bytes.MappedBytes)1 Jvm (net.openhft.chronicle.core.Jvm)1 OS (net.openhft.chronicle.core.OS)1 StackTrace (net.openhft.chronicle.core.StackTrace)1 IORuntimeException (net.openhft.chronicle.core.io.IORuntimeException)1 CLASS_ALIASES (net.openhft.chronicle.core.pool.ClassAliasPool.CLASS_ALIASES)1 InterruptedRuntimeException (net.openhft.chronicle.core.threads.InterruptedRuntimeException)1 Builder (net.openhft.chronicle.core.util.Builder)1 StringUtils (net.openhft.chronicle.core.util.StringUtils)1 TableStore (net.openhft.chronicle.queue.impl.TableStore)1 MetaDataKeys (net.openhft.chronicle.queue.impl.single.MetaDataKeys)1 Pauser (net.openhft.chronicle.threads.Pauser)1