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

HBASE-27458 Use ReadWriteLock for region scanner readpoint map #4859

Merged
merged 4 commits into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -399,6 +399,7 @@ public void setRestoredRegion(boolean restoredRegion) {
private final int miniBatchSize;

final ConcurrentHashMap<RegionScanner, Long> scannerReadPoints;
final ReadPointCalculationLock smallestReadPointCalcLock;

/**
* The sequence ID that was enLongAddered when this region was opened.
Expand Down Expand Up @@ -443,19 +444,18 @@ public void setRestoredRegion(boolean restoredRegion) {
* this readPoint, are included in every read operation.
*/
public long getSmallestReadPoint() {
long minimumReadPoint;
// We need to ensure that while we are calculating the smallestReadPoint
// no new RegionScanners can grab a readPoint that we are unaware of.
// We achieve this by synchronizing on the scannerReadPoints object.
synchronized (scannerReadPoints) {
minimumReadPoint = mvcc.getReadPoint();
smallestReadPointCalcLock.lock(ReadPointCalculationLock.LockType.CALCULATION_LOCK);
try {
long minimumReadPoint = mvcc.getReadPoint();
for (Long readPoint : this.scannerReadPoints.values()) {
if (readPoint < minimumReadPoint) {
minimumReadPoint = readPoint;
}
minimumReadPoint = Math.min(minimumReadPoint, readPoint);
}
return minimumReadPoint;
} finally {
smallestReadPointCalcLock.unlock(ReadPointCalculationLock.LockType.CALCULATION_LOCK);
}
return minimumReadPoint;
}

/*
Expand Down Expand Up @@ -798,6 +798,8 @@ public HRegion(final HRegionFileSystem fs, final WAL wal, final Configuration co
}
this.rowLockWaitDuration = tmpRowLockDuration;

this.smallestReadPointCalcLock = new ReadPointCalculationLock(conf);

this.isLoadingCfsOnDemandDefault = conf.getBoolean(LOAD_CFS_ON_DEMAND_CONFIG_KEY, true);
this.htableDescriptor = htd;
Set<byte[]> families = this.htableDescriptor.getColumnFamilyNames();
Expand Down Expand Up @@ -8138,6 +8140,7 @@ private void doAttachReplicateRegionReplicaAction(WALKeyImpl walKey, WALEdit wal
// 1 x RegionSplitPolicy - splitPolicy
// 1 x MetricsRegion - metricsRegion
// 1 x MetricsRegionWrapperImpl - metricsRegionWrapper
// 1 x ReadPointCalculationLock - smallestReadPointCalcLock
public static final long DEEP_OVERHEAD = FIXED_OVERHEAD + ClassSize.OBJECT + // closeLock
(2 * ClassSize.ATOMIC_BOOLEAN) + // closed, closing
(3 * ClassSize.ATOMIC_LONG) + // numPutsWithoutWAL, dataInMemoryWithoutWAL,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.hadoop.hbase.regionserver;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.apache.hadoop.conf.Configuration;
import org.apache.yetus.audience.InterfaceAudience;

/**
* Lock to manage concurrency between RegionScanner and getSmallestReadPoint.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add more comments here to explain why we have this class and why we have a flag to control whether to use read write lock or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok.

*/
@InterfaceAudience.Private
public class ReadPointCalculationLock {

public enum LockType {
CALCULATION_LOCK,
RECORDING_LOCK
}

private final boolean useReadWriteLockForReadPoints;
private Lock lock;
private ReadWriteLock readWriteLock;

ReadPointCalculationLock(Configuration conf) {
this.useReadWriteLockForReadPoints =
conf.getBoolean("hbase.readpoints.read.write.lock.enable", false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better name it hbase.region.xxx, as the lock is per region.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok.

if (useReadWriteLockForReadPoints) {
readWriteLock = new ReentrantReadWriteLock();
} else {
lock = new ReentrantLock();
}
}

void lock(LockType lockType) {
if (useReadWriteLockForReadPoints) {
assert lock == null;
if (lockType == LockType.CALCULATION_LOCK) {
readWriteLock.writeLock().lock();
} else {
readWriteLock.readLock().lock();
}
} else {
assert readWriteLock == null;
lock.lock();
}
}

void unlock(LockType lockType) {
if (useReadWriteLockForReadPoints) {
assert lock == null;
if (lockType == LockType.CALCULATION_LOCK) {
readWriteLock.writeLock().unlock();
} else {
readWriteLock.readLock().unlock();
}
} else {
assert readWriteLock == null;
lock.unlock();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ private static boolean hasNonce(HRegion region, long nonce) {
long mvccReadPoint = PackagePrivateFieldAccessor.getMvccReadPoint(scan);
this.scannerReadPoints = region.scannerReadPoints;
this.rsServices = region.getRegionServerServices();
synchronized (scannerReadPoints) {
region.smallestReadPointCalcLock.lock(ReadPointCalculationLock.LockType.RECORDING_LOCK);
try {
if (mvccReadPoint > 0) {
this.readPt = mvccReadPoint;
} else if (hasNonce(region, nonce)) {
Expand All @@ -139,6 +140,8 @@ private static boolean hasNonce(HRegion region, long nonce) {
this.readPt = region.getReadPoint(isolationLevel);
}
scannerReadPoints.put(this, this.readPt);
} finally {
region.smallestReadPointCalcLock.unlock(ReadPointCalculationLock.LockType.RECORDING_LOCK);
}
initializeScanners(scan, additionalScanners);
}
Expand Down