tkalkirill commented on code in PR #791: URL: https://github.com/apache/ignite-3/pull/791#discussion_r861493412
########## modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/checkpoint/CheckpointReadWriteLock.java: ########## @@ -0,0 +1,149 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.pagememory.persistence.checkpoint; + +import java.util.concurrent.TimeUnit; +import org.apache.ignite.lang.IgniteInternalException; + +/** + * Wrapper of the classic read write lock with checkpoint features. + */ +public class CheckpointReadWriteLock { + private final ThreadLocal<Integer> checkpointReadLockHoldCount = ThreadLocal.withInitial(() -> 0); + + /** + * Any thread with a such prefix is managed by the checkpoint. So some conditions can rely on it(ex. we don't need a checkpoint lock + * there because checkpoint is already held write lock). + */ + // TODO: IGNITE-16898 I think it needs to be redone or relocated + static final String CHECKPOINT_RUNNER_THREAD_PREFIX = "checkpoint-runner"; + + /** Checkpoint lock. */ + private final ReentrantReadWriteLockWithTracking checkpointLock; + + /** + * Constructor. + * + * @param checkpointLock Checkpoint lock. + */ + public CheckpointReadWriteLock(ReentrantReadWriteLockWithTracking checkpointLock) { + this.checkpointLock = checkpointLock; + } + + /** + * Gets the checkpoint read lock. + * + * @throws IgniteInternalException If failed. + */ + public void readLock() { + if (isWriteLockHeldByCurrentThread()) { + return; + } + + checkpointLock.readLock().lock(); + + checkpointReadLockHoldCount.set(checkpointReadLockHoldCount.get() + 1); + } + + /** + * Tries to get a checkpoint read lock. + * + * @param timeout – Time to wait for the read lock. + * @param unit – Time unit of the timeout argument. + * @throws IgniteInternalException If failed. + */ + public boolean tryReadLock(long timeout, TimeUnit unit) throws InterruptedException { + if (isWriteLockHeldByCurrentThread()) { + return true; + } + + boolean res = checkpointLock.readLock().tryLock(timeout, unit); + + checkpointReadLockHoldCount.set(checkpointReadLockHoldCount.get() + 1); Review Comment: You're right, it's a bug, fixed it. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
