Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IGNITE-13775 checkpointRWLock wrapper refactoring. #8516

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
package org.apache.ignite.internal.processors.cache.persistence.checkpoint;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Function;
import org.apache.ignite.IgniteException;
import org.apache.ignite.IgniteLogger;
import org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager;
import org.apache.ignite.internal.util.typedef.internal.U;
import org.apache.ignite.internal.util.ReentrantReadWriteLockWithTracking;

import static org.apache.ignite.IgniteSystemProperties.getBoolean;
import static org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager.IGNITE_PDS_LOG_CP_READ_LOCK_HOLDERS;
Expand All @@ -45,18 +44,16 @@ public class CheckpointReadWriteLock {
static final String CHECKPOINT_RUNNER_THREAD_PREFIX = "checkpoint-runner";

/** Checkpont lock. */
private final ReentrantReadWriteLock checkpointLock;
private final ReentrantReadWriteLockWithTracking checkpointLock;

/**
* @param logger Logger.
*/
CheckpointReadWriteLock(Function<Class<?>, IgniteLogger> logger) {
ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

if (getBoolean(IGNITE_PDS_LOG_CP_READ_LOCK_HOLDERS))
checkpointLock = new U.ReentrantReadWriteLockTracer(lock, logger.apply(getClass()), 5_000);
checkpointLock = new ReentrantReadWriteLockWithTracking(logger.apply(getClass()), 5_000);
else
checkpointLock = lock;
checkpointLock = new ReentrantReadWriteLockWithTracking();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Consumer;
import java.util.jar.JarFile;
import java.util.logging.ConsoleHandler;
Expand Down Expand Up @@ -242,7 +241,6 @@
import org.apache.ignite.internal.util.typedef.F;
import org.apache.ignite.internal.util.typedef.G;
import org.apache.ignite.internal.util.typedef.P1;
import org.apache.ignite.internal.util.typedef.T2;
import org.apache.ignite.internal.util.typedef.X;
import org.apache.ignite.internal.util.typedef.internal.A;
import org.apache.ignite.internal.util.typedef.internal.SB;
Expand Down Expand Up @@ -363,9 +361,6 @@ public abstract class IgniteUtils {
/** Default user version. */
public static final String DFLT_USER_VERSION = "0";

/** Lock hold message. */
public static final String LOCK_HOLD_MESSAGE = "ReadLock held the lock more than ";

/** Cache for {@link GridPeerDeployAware} fields to speed up reflection. */
private static final ConcurrentMap<String, IgniteBiTuple<Class<?>, Collection<Field>>> p2pFields =
new ConcurrentHashMap<>();
Expand Down Expand Up @@ -11528,180 +11523,6 @@ public static Runnable wrapIgniteFuture(Runnable r, GridFutureAdapter<?> fut) {
};
}

/** */
public static class ReentrantReadWriteLockTracer extends ReentrantReadWriteLock {
/** */
private static final long serialVersionUID = 0L;

/** Read lock. */
private final ReadLockTracer readLock;

/** Write lock. */
private final WriteLockTracer writeLock;

/** Lock print threshold. */
private long readLockThreshold;

/** */
private IgniteLogger log;

/**
* @param delegate RWLock delegate.
* @param log Ignite logger.
* @param readLockThreshold ReadLock threshold timeout.
*
*/
public ReentrantReadWriteLockTracer(ReentrantReadWriteLock delegate, IgniteLogger log, long readLockThreshold) {
this.log = log;

readLock = new ReadLockTracer(delegate, log, readLockThreshold);

writeLock = new WriteLockTracer(delegate);

this.readLockThreshold = readLockThreshold;
}

/** {@inheritDoc} */
@Override public ReadLock readLock() {
return readLock;
}

/** {@inheritDoc} */
@Override public WriteLock writeLock() {
return writeLock;
}

/** */
public long lockWaitThreshold() {
return readLockThreshold;
}
}

/** */
private static class ReadLockTracer extends ReentrantReadWriteLock.ReadLock {
/** */
private static final long serialVersionUID = 0L;

/** Delegate. */
private final ReentrantReadWriteLock.ReadLock delegate;

/** */
private static final ThreadLocal<T2<Integer, Long>> READ_LOCK_HOLDER_TS =
ThreadLocal.withInitial(() -> new T2<>(0, 0L));

/** */
private IgniteLogger log;

/** */
private long readLockThreshold;

/** */
public ReadLockTracer(ReentrantReadWriteLock lock, IgniteLogger log, long readLockThreshold) {
super(lock);

delegate = lock.readLock();

this.log = log;

this.readLockThreshold = readLockThreshold;
}

/** */
private void inc() {
T2<Integer, Long> val = READ_LOCK_HOLDER_TS.get();

int cntr = val.get1();

if (cntr == 0)
val.set2(U.currentTimeMillis());

val.set1(++cntr);

READ_LOCK_HOLDER_TS.set(val);
}

/** */
private void dec() {
T2<Integer, Long> val = READ_LOCK_HOLDER_TS.get();

int cntr = val.get1();

if (--cntr == 0) {
long timeout = U.currentTimeMillis() - val.get2();

if (timeout > readLockThreshold) {
GridStringBuilder sb = new GridStringBuilder();

sb.a(LOCK_HOLD_MESSAGE + timeout + " ms." + nl());

U.printStackTrace(Thread.currentThread().getId(), sb);

U.warn(log, sb.toString());
}
}

val.set1(cntr);

READ_LOCK_HOLDER_TS.set(val);
}

/** {@inheritDoc} */
@SuppressWarnings("LockAcquiredButNotSafelyReleased")
@Override public void lock() {
delegate.lock();

inc();
}

/** {@inheritDoc} */
@SuppressWarnings("LockAcquiredButNotSafelyReleased")
@Override public void lockInterruptibly() throws InterruptedException {
delegate.lockInterruptibly();

inc();
}

/** {@inheritDoc} */
@Override public boolean tryLock() {
if (delegate.tryLock()) {
inc();

return true;
}
else
return false;
}

/** {@inheritDoc} */
@Override public boolean tryLock(long time, @NotNull TimeUnit unit) throws InterruptedException {
if (delegate.tryLock(time, unit)) {
inc();

return true;
}
else
return false;
}

/** {@inheritDoc} */
@Override public void unlock() {
delegate.unlock();

dec();
}
}

/** */
private static class WriteLockTracer extends ReentrantReadWriteLock.WriteLock {
/** */
private static final long serialVersionUID = 0L;

/** */
public WriteLockTracer(ReentrantReadWriteLock lock) {
super(lock);
}
}

/**
* @param key Cipher Key.
* @param encMode Enc mode see {@link Cipher#ENCRYPT_MODE}, {@link Cipher#DECRYPT_MODE}, etc.
Expand Down
Loading