From fcf8021d0ae8be2b646601f783e0a84912ae7be8 Mon Sep 17 00:00:00 2001 From: "zhanghaobo@kanzhun.com" Date: Tue, 23 Apr 2024 16:39:18 +0800 Subject: [PATCH 01/12] HDFS-17496. DataNode supports more fine-grained dataset lock based on blockid. --- .../server/common/DataNodeLockManager.java | 3 +- .../server/datanode/DataSetLockManager.java | 33 ++++- .../hdfs/server/datanode/DatanodeUtil.java | 26 ++++ .../fsdataset/impl/FsDatasetImpl.java | 118 ++++++++++++------ .../datanode/TestDataSetLockManager.java | 11 ++ 5 files changed, 147 insertions(+), 44 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/DataNodeLockManager.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/DataNodeLockManager.java index e7a3b38357ac9..cb22a0570627f 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/DataNodeLockManager.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/DataNodeLockManager.java @@ -29,7 +29,8 @@ public interface DataNodeLockManager { */ enum LockLevel { BLOCK_POOl, - VOLUME + VOLUME, + DIR } /** diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataSetLockManager.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataSetLockManager.java index 5579541eb72d8..52242df5e1c7b 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataSetLockManager.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataSetLockManager.java @@ -94,6 +94,8 @@ private String generateLockName(LockLevel level, String... resources) { + resources[0] + "volume lock :" + resources[1]); } return resources[0] + resources[1]; + } else if (resources.length == 3 && level == LockLevel.DIR) { + return resources[0] + resources[1] + resources[2]; } else { throw new IllegalArgumentException("lock level do not match resource"); } @@ -153,7 +155,7 @@ public DataSetLockManager() { public AutoCloseDataSetLock readLock(LockLevel level, String... resources) { if (level == LockLevel.BLOCK_POOl) { return getReadLock(level, resources[0]); - } else { + } else if (level == LockLevel.VOLUME){ AutoCloseDataSetLock bpLock = getReadLock(LockLevel.BLOCK_POOl, resources[0]); AutoCloseDataSetLock volLock = getReadLock(level, resources); volLock.setParentLock(bpLock); @@ -162,6 +164,17 @@ public AutoCloseDataSetLock readLock(LockLevel level, String... resources) { resources[0]); } return volLock; + } else { + AutoCloseDataSetLock bpLock = getReadLock(LockLevel.BLOCK_POOl, resources[0]); + AutoCloseDataSetLock volLock = getReadLock(LockLevel.VOLUME, resources[0], resources[1]); + volLock.setParentLock(bpLock); + AutoCloseDataSetLock dirLock = getWriteLock(level, resources); + dirLock.setParentLock(volLock); + if (openLockTrace) { + LOG.debug("Sub lock " + resources[0] + resources[1] + resources[2] + " parent lock " + + resources[0] + resources[1]); + } + return dirLock; } } @@ -169,7 +182,7 @@ public AutoCloseDataSetLock readLock(LockLevel level, String... resources) { public AutoCloseDataSetLock writeLock(LockLevel level, String... resources) { if (level == LockLevel.BLOCK_POOl) { return getWriteLock(level, resources[0]); - } else { + } else if (level == LockLevel.VOLUME) { AutoCloseDataSetLock bpLock = getReadLock(LockLevel.BLOCK_POOl, resources[0]); AutoCloseDataSetLock volLock = getWriteLock(level, resources); volLock.setParentLock(bpLock); @@ -178,6 +191,17 @@ public AutoCloseDataSetLock writeLock(LockLevel level, String... resources) { resources[0]); } return volLock; + } else { + AutoCloseDataSetLock bpLock = getReadLock(LockLevel.BLOCK_POOl, resources[0]); + AutoCloseDataSetLock volLock = getReadLock(LockLevel.VOLUME, resources[0], resources[1]); + volLock.setParentLock(bpLock); + AutoCloseDataSetLock dirLock = getReadLock(level, resources); + dirLock.setParentLock(volLock); + if (openLockTrace) { + LOG.debug("Sub lock " + resources[0] + resources[1] + resources[2] + " parent lock " + + resources[0] + resources[1]); + } + return dirLock; } } @@ -224,8 +248,13 @@ public void addLock(LockLevel level, String... resources) { String lockName = generateLockName(level, resources); if (level == LockLevel.BLOCK_POOl) { lockMap.addLock(lockName, new ReentrantReadWriteLock(isFair)); + } else if (level == LockLevel.VOLUME) { + lockMap.addLock(resources[0], new ReentrantReadWriteLock(isFair)); + lockMap.addLock(lockName, new ReentrantReadWriteLock(isFair)); } else { lockMap.addLock(resources[0], new ReentrantReadWriteLock(isFair)); + lockMap.addLock(generateLockName(LockLevel.VOLUME, resources[0], resources[1]), + new ReentrantReadWriteLock(isFair)); lockMap.addLock(lockName, new ReentrantReadWriteLock(isFair)); } } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java index c98ff5413bd85..af7603178eae1 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java @@ -21,6 +21,8 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.hdfs.protocol.Block; @@ -127,6 +129,30 @@ public static File idToBlockDir(File root, long blockId) { return new File(root, path); } + /** + * Take an example: we hava a block with blockid mapping to: + * "/data1/hadoop/hdfs/datanode/current/BP-xxxx/current/finalized/subdir0/subdir0" + * We return "subdir0/subdir0" + * @return + */ + public static String idToBlockDirSuffixName(long blockId) { + int d1 = (int) ((blockId >> 16) & 0x1F); + int d2 = (int) ((blockId >> 8) & 0x1F); + return DataStorage.BLOCK_SUBDIR_PREFIX + d1 + SEP + + DataStorage.BLOCK_SUBDIR_PREFIX + d2; + } + + public static List getAllSubDirNameForDataSetLock() { + List res = new ArrayList<>(); + for (int d1 = 0; d1 <= 0x1F; d1++) { + for (int d2 = 0; d2 <= 0x1F; d2++) { + res.add(DataStorage.BLOCK_SUBDIR_PREFIX + d1 + SEP + + DataStorage.BLOCK_SUBDIR_PREFIX + d2); + } + } + return res; + } + /** * @return the FileInputStream for the meta data of the given block. * @throws FileNotFoundException diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java index eeec1bb728825..8347a332b65a9 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java @@ -138,6 +138,7 @@ class FsDatasetImpl implements FsDatasetSpi { static final Logger LOG = LoggerFactory.getLogger(FsDatasetImpl.class); private final static boolean isNativeIOAvailable; + private static final String SEP = System.getProperty("file.separator"); private Timer timer; static { isNativeIOAvailable = NativeIO.isAvailable(); @@ -198,8 +199,9 @@ public FsVolumeImpl getVolume(final ExtendedBlock b) { @Override // FsDatasetSpi public Block getStoredBlock(String bpid, long blkid) throws IOException { - try (AutoCloseableLock lock = lockManager.readLock(LockLevel.BLOCK_POOl, - bpid)) { + try (AutoCloseableLock lock = lockManager.readLock(LockLevel.DIR, + bpid, getReplicaInfo(bpid, blkid).getStorageUuid(), + DatanodeUtil.idToBlockDirSuffixName(blkid))) { ReplicaInfo r = volumeMap.get(bpid, blkid); if (r == null) { return null; @@ -430,6 +432,13 @@ private synchronized void activateVolume( FsVolumeReference ref) throws IOException { for (String bp : volumeMap.getBlockPoolList()) { lockManager.addLock(LockLevel.VOLUME, bp, ref.getVolume().getStorageID()); + List allSubDirNameForDataSetLock = DatanodeUtil.getAllSubDirNameForDataSetLock(); + for (String dir : allSubDirNameForDataSetLock) { + lockManager.addLock(LockLevel.DIR, bp, ref.getVolume().getStorageID(), dir); + LOG.info("Added DIR lock for bpid:{}, volume storageid:{}, dir:{}", + bp, ref.getVolume().getStorageID(), dir); + + } } DatanodeStorage dnStorage = storageMap.get(sd.getStorageUuid()); if (dnStorage != null) { @@ -629,6 +638,12 @@ public void removeVolumes( for (String storageUuid : storageToRemove) { storageMap.remove(storageUuid); for (String bp : volumeMap.getBlockPoolList()) { + List allSubDirNameForDataSetLock = DatanodeUtil.getAllSubDirNameForDataSetLock(); + for (String dir : allSubDirNameForDataSetLock) { + lockManager.removeLock(LockLevel.DIR, bp, storageUuid, dir); + LOG.info("Removed DIR lock for bpid:{}, volume storageid:{}, dir:{}", + bp, storageUuid, dir); + } lockManager.removeLock(LockLevel.VOLUME, bp, storageUuid); } } @@ -819,8 +834,9 @@ public InputStream getBlockInputStream(ExtendedBlock b, long seekOffset) throws IOException { ReplicaInfo info; - try (AutoCloseableLock lock = lockManager.readLock(LockLevel.BLOCK_POOl, - b.getBlockPoolId())) { + try (AutoCloseableLock lock = lockManager.readLock(LockLevel.DIR, + b.getBlockPoolId(), getStorageUuidForLock(b), + DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { info = volumeMap.get(b.getBlockPoolId(), b.getLocalBlock()); } @@ -914,8 +930,9 @@ String getStorageUuidForLock(ExtendedBlock b) @Override // FsDatasetSpi public ReplicaInputStreams getTmpInputStreams(ExtendedBlock b, long blkOffset, long metaOffset) throws IOException { - try (AutoCloseDataSetLock l = lockManager.readLock(LockLevel.VOLUME, - b.getBlockPoolId(), getStorageUuidForLock(b))) { + try (AutoCloseDataSetLock l = lockManager.readLock(LockLevel.DIR, + b.getBlockPoolId(), getStorageUuidForLock(b), + DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { ReplicaInfo info = getReplicaInfo(b); FsVolumeReference ref = info.getVolume().obtainReference(); try { @@ -1380,8 +1397,9 @@ static void computeChecksum(ReplicaInfo srcReplica, File dstMeta, @Override // FsDatasetSpi public ReplicaHandler append(ExtendedBlock b, long newGS, long expectedBlockLen) throws IOException { - try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.VOLUME, - b.getBlockPoolId(), getStorageUuidForLock(b))) { + try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, + b.getBlockPoolId(), getStorageUuidForLock(b), + DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { // If the block was successfully finalized because all packets // were successfully processed at the Datanode but the ack for // some of the packets were not received by the client. The client @@ -1433,8 +1451,9 @@ public ReplicaHandler append(ExtendedBlock b, private ReplicaInPipeline append(String bpid, ReplicaInfo replicaInfo, long newGS, long estimateBlockLen) throws IOException { - try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.VOLUME, - bpid, replicaInfo.getStorageUuid())) { + try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, + bpid, replicaInfo.getStorageUuid(), + DatanodeUtil.idToBlockDirSuffixName(replicaInfo.getBlockId()))) { // If the block is cached, start uncaching it. if (replicaInfo.getState() != ReplicaState.FINALIZED) { throw new IOException("Only a Finalized replica can be appended to; " @@ -1530,8 +1549,8 @@ public ReplicaHandler recoverAppend( while (true) { try { - try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.BLOCK_POOl, - b.getBlockPoolId())) { + try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, + b.getBlockPoolId(), getStorageUuidForLock(b), DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { ReplicaInfo replicaInfo = recoverCheck(b, newGS, expectedBlockLen); FsVolumeReference ref = replicaInfo.getVolume().obtainReference(); ReplicaInPipeline replica; @@ -1564,8 +1583,9 @@ public Replica recoverClose(ExtendedBlock b, long newGS, b, newGS, expectedBlockLen); while (true) { try { - try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.VOLUME, - b.getBlockPoolId(), getStorageUuidForLock(b))) { + try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, + b.getBlockPoolId(), getStorageUuidForLock(b), + DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { // check replica's state ReplicaInfo replicaInfo = recoverCheck(b, newGS, expectedBlockLen); // bump the replica's GS @@ -1650,8 +1670,9 @@ public ReplicaHandler createRbw( } ReplicaInPipeline newReplicaInfo; - try (AutoCloseableLock l = lockManager.writeLock(LockLevel.VOLUME, - b.getBlockPoolId(), v.getStorageID())) { + try (AutoCloseableLock l = lockManager.writeLock(LockLevel.DIR, + b.getBlockPoolId(), v.getStorageID(), + DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { newReplicaInfo = v.createRbw(b); if (newReplicaInfo.getReplicaInfo().getState() != ReplicaState.RBW) { throw new IOException("CreateRBW returned a replica of state " @@ -1681,8 +1702,9 @@ public ReplicaHandler recoverRbw( try { while (true) { try { - try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.VOLUME, - b.getBlockPoolId(), getStorageUuidForLock(b))) { + try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, + b.getBlockPoolId(), getStorageUuidForLock(b), + DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { ReplicaInfo replicaInfo = getReplicaInfo(b.getBlockPoolId(), b.getBlockId()); // check the replica's state @@ -1713,8 +1735,9 @@ public ReplicaHandler recoverRbw( private ReplicaHandler recoverRbwImpl(ReplicaInPipeline rbw, ExtendedBlock b, long newGS, long minBytesRcvd, long maxBytesRcvd) throws IOException { - try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.VOLUME, - b.getBlockPoolId(), getStorageUuidForLock(b))) { + try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, + b.getBlockPoolId(), getStorageUuidForLock(b), + DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { // check generation stamp long replicaGenerationStamp = rbw.getGenerationStamp(); if (replicaGenerationStamp < b.getGenerationStamp() || @@ -1775,8 +1798,9 @@ private ReplicaHandler recoverRbwImpl(ReplicaInPipeline rbw, public ReplicaInPipeline convertTemporaryToRbw( final ExtendedBlock b) throws IOException { long startTimeMs = Time.monotonicNow(); - try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.VOLUME, - b.getBlockPoolId(), getStorageUuidForLock(b))) { + try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, + b.getBlockPoolId(), getStorageUuidForLock(b), + DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { final long blockId = b.getBlockId(); final long expectedGs = b.getGenerationStamp(); final long visible = b.getNumBytes(); @@ -1915,8 +1939,8 @@ public ReplicaHandler createTemporary(StorageType storageType, .getNumBytes()); FsVolumeImpl v = (FsVolumeImpl) ref.getVolume(); ReplicaInPipeline newReplicaInfo; - try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.VOLUME, - b.getBlockPoolId(), v.getStorageID())) { + try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, + b.getBlockPoolId(), v.getStorageID(), DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { try { newReplicaInfo = v.createTemporary(b); LOG.debug("creating temporary for block: {} on volume: {}", @@ -1973,8 +1997,9 @@ public void finalizeBlock(ExtendedBlock b, boolean fsyncDir) ReplicaInfo replicaInfo = null; ReplicaInfo finalizedReplicaInfo = null; long startTimeMs = Time.monotonicNow(); - try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.VOLUME, - b.getBlockPoolId(), getStorageUuidForLock(b))) { + try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, + b.getBlockPoolId(), getStorageUuidForLock(b), + DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { if (Thread.interrupted()) { // Don't allow data modifications from interrupted threads throw new IOException("Cannot finalize block: " + b + " from Interrupted Thread"); @@ -2010,8 +2035,9 @@ public void finalizeBlock(ExtendedBlock b, boolean fsyncDir) private ReplicaInfo finalizeReplica(String bpid, ReplicaInfo replicaInfo) throws IOException { - try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.VOLUME, - bpid, replicaInfo.getStorageUuid())) { + try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, + bpid, replicaInfo.getStorageUuid(), + DatanodeUtil.idToBlockDirSuffixName(replicaInfo.getBlockId()))) { // Compare generation stamp of old and new replica before finalizing if (volumeMap.get(bpid, replicaInfo.getBlockId()).getGenerationStamp() > replicaInfo.getGenerationStamp()) { @@ -2060,8 +2086,9 @@ private ReplicaInfo finalizeReplica(String bpid, ReplicaInfo replicaInfo) @Override // FsDatasetSpi public void unfinalizeBlock(ExtendedBlock b) throws IOException { long startTimeMs = Time.monotonicNow(); - try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.VOLUME, - b.getBlockPoolId(), getStorageUuidForLock(b))) { + try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, + b.getBlockPoolId(), getStorageUuidForLock(b), + DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { ReplicaInfo replicaInfo = volumeMap.get(b.getBlockPoolId(), b.getLocalBlock()); if (replicaInfo != null && @@ -2459,7 +2486,8 @@ boolean removeReplicaFromMem(final ExtendedBlock block, final FsVolumeImpl volum final String bpid = block.getBlockPoolId(); final Block localBlock = block.getLocalBlock(); final long blockId = localBlock.getBlockId(); - try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.BLOCK_POOl, bpid)) { + try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, bpid, volume.getStorageID(), + DatanodeUtil.idToBlockDirSuffixName(blockId))) { final ReplicaInfo info = volumeMap.get(bpid, localBlock); if (info == null) { ReplicaInfo infoByBlockId = volumeMap.get(bpid, blockId); @@ -2548,8 +2576,8 @@ private void cacheBlock(String bpid, long blockId) { bpid + ": ReplicaInfo not found."); return; } - try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.VOLUME, bpid, - info.getStorageUuid())) { + try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, bpid, + info.getStorageUuid(), DatanodeUtil.idToBlockDirSuffixName(blockId))) { boolean success = false; try { info = volumeMap.get(bpid, blockId); @@ -3002,8 +3030,8 @@ ReplicaRecoveryInfo initReplicaRecovery(String bpid, ReplicaMap map, } LOG.info("initReplicaRecovery: " + block + ", recoveryId=" + recoveryId + ", replica=" + replica); - try (AutoCloseDataSetLock l = lockManager.writeLock(LockLevel.VOLUME, bpid, - replica.getStorageUuid())) { + try (AutoCloseDataSetLock l = lockManager.writeLock(LockLevel.DIR, bpid, + replica.getStorageUuid(), DatanodeUtil.idToBlockDirSuffixName(block.getBlockId()))) { return initReplicaRecoveryImpl(bpid, map, block, recoveryId); } } catch (MustStopExistingWriter e) { @@ -3024,8 +3052,8 @@ static ReplicaRecoveryInfo initReplicaRecovery(String bpid, ReplicaMap map, } LOG.info("initReplicaRecovery: " + block + ", recoveryId=" + recoveryId + ", replica=" + replica); - try (AutoCloseDataSetLock l = lockManager.writeLock(LockLevel.VOLUME, bpid, - replica.getStorageUuid())) { + try (AutoCloseDataSetLock l = lockManager.writeLock(LockLevel.DIR, bpid, + replica.getStorageUuid(), DatanodeUtil.idToBlockDirSuffixName(block.getBlockId()))) { return initReplicaRecoveryImpl(bpid, map, block, recoveryId); } } catch (MustStopExistingWriter e) { @@ -3231,8 +3259,9 @@ private ReplicaInfo updateReplicaUnderRecovery( @Override // FsDatasetSpi public long getReplicaVisibleLength(final ExtendedBlock block) throws IOException { - try (AutoCloseableLock lock = lockManager.readLock(LockLevel.BLOCK_POOl, - block.getBlockPoolId())) { + try (AutoCloseableLock lock = lockManager.readLock(LockLevel.DIR, + block.getBlockPoolId(), getStorageUuidForLock(block), + DatanodeUtil.idToBlockDirSuffixName(block.getBlockId()))) { final Replica replica = getReplicaInfo(block.getBlockPoolId(), block.getBlockId()); if (replica.getGenerationStamp() < block.getGenerationStamp()) { @@ -3259,6 +3288,12 @@ public void addBlockPool(String bpid, Configuration conf) Set vols = storageMap.keySet(); for (String v : vols) { lockManager.addLock(LockLevel.VOLUME, bpid, v); + List allSubDirNameForDataSetLock = DatanodeUtil.getAllSubDirNameForDataSetLock(); + for (String dir : allSubDirNameForDataSetLock) { + lockManager.addLock(LockLevel.DIR, bpid, v, dir); + LOG.info("Added DIR lock for bpid:{}, volume storageid:{}, dir:{}", + bpid, v, dir); + } } } try { @@ -3386,8 +3421,9 @@ public void deleteBlockPool(String bpid, boolean force) @Override // FsDatasetSpi public BlockLocalPathInfo getBlockLocalPathInfo(ExtendedBlock block) throws IOException { - try (AutoCloseableLock lock = lockManager.readLock(LockLevel.BLOCK_POOl, - block.getBlockPoolId())) { + try (AutoCloseableLock lock = lockManager.readLock(LockLevel.DIR, + block.getBlockPoolId(), getStorageUuidForLock(block), + DatanodeUtil.idToBlockDirSuffixName(block.getBlockId()))) { final Replica replica = volumeMap.get(block.getBlockPoolId(), block.getBlockId()); if (replica == null) { diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDataSetLockManager.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDataSetLockManager.java index b514accdf16e9..9efdc3f89a684 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDataSetLockManager.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDataSetLockManager.java @@ -37,6 +37,7 @@ public void init() { public void testBaseFunc() { manager.addLock(LockLevel.BLOCK_POOl, "BPtest"); manager.addLock(LockLevel.VOLUME, "BPtest", "Volumetest"); + manager.addLock(LockLevel.DIR, "BPtest", "Volumetest", "SubDirtest"); AutoCloseDataSetLock lock = manager.writeLock(LockLevel.BLOCK_POOl, "BPtest"); AutoCloseDataSetLock lock1 = manager.readLock(LockLevel.BLOCK_POOl, "BPtest"); @@ -62,6 +63,15 @@ public void testBaseFunc() { manager.lockLeakCheck(); assertNull(manager.getLastException()); + AutoCloseDataSetLock lock6 = manager.writeLock(LockLevel.BLOCK_POOl, "BPtest"); + AutoCloseDataSetLock lock7 = manager.readLock(LockLevel.VOLUME, "BPtest", "Volumetest"); + AutoCloseDataSetLock lock8 = manager.readLock(LockLevel.DIR, "BPtest", "Volumetest", "SubDirtest"); + lock8.close(); + lock7.close(); + lock6.close(); + manager.lockLeakCheck(); + assertNull(manager.getLastException()); + manager.writeLock(LockLevel.VOLUME, "BPtest", "Volumetest"); manager.lockLeakCheck(); @@ -89,4 +99,5 @@ public void testLockLeakCheck() { Exception lastException = manager.getLastException(); assertEquals(lastException.getMessage(), "lock Leak"); } + } From db36a2bdede38d7d416e00b79cd90791ab8547b8 Mon Sep 17 00:00:00 2001 From: "zhanghaobo@kanzhun.com" Date: Tue, 23 Apr 2024 16:45:22 +0800 Subject: [PATCH 02/12] remove used statement. --- .../hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java | 1 - 1 file changed, 1 deletion(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java index 8347a332b65a9..cdd2f202ccbd6 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java @@ -138,7 +138,6 @@ class FsDatasetImpl implements FsDatasetSpi { static final Logger LOG = LoggerFactory.getLogger(FsDatasetImpl.class); private final static boolean isNativeIOAvailable; - private static final String SEP = System.getProperty("file.separator"); private Timer timer; static { isNativeIOAvailable = NativeIO.isAvailable(); From dc097134424c4a4d0954e4265e95992c02ae0840 Mon Sep 17 00:00:00 2001 From: "zhanghaobo@kanzhun.com" Date: Fri, 28 Jun 2024 11:04:49 +0800 Subject: [PATCH 03/12] fix bug --- .../hadoop/hdfs/server/datanode/DataSetLockManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataSetLockManager.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataSetLockManager.java index 52242df5e1c7b..a24eb334834ec 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataSetLockManager.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataSetLockManager.java @@ -168,7 +168,7 @@ public AutoCloseDataSetLock readLock(LockLevel level, String... resources) { AutoCloseDataSetLock bpLock = getReadLock(LockLevel.BLOCK_POOl, resources[0]); AutoCloseDataSetLock volLock = getReadLock(LockLevel.VOLUME, resources[0], resources[1]); volLock.setParentLock(bpLock); - AutoCloseDataSetLock dirLock = getWriteLock(level, resources); + AutoCloseDataSetLock dirLock = getReadLock(level, resources); dirLock.setParentLock(volLock); if (openLockTrace) { LOG.debug("Sub lock " + resources[0] + resources[1] + resources[2] + " parent lock " + @@ -195,7 +195,7 @@ public AutoCloseDataSetLock writeLock(LockLevel level, String... resources) { AutoCloseDataSetLock bpLock = getReadLock(LockLevel.BLOCK_POOl, resources[0]); AutoCloseDataSetLock volLock = getReadLock(LockLevel.VOLUME, resources[0], resources[1]); volLock.setParentLock(bpLock); - AutoCloseDataSetLock dirLock = getReadLock(level, resources); + AutoCloseDataSetLock dirLock = getWriteLock(level, resources); dirLock.setParentLock(volLock); if (openLockTrace) { LOG.debug("Sub lock " + resources[0] + resources[1] + resources[2] + " parent lock " + From 89ca2cdd61c71516014cbbb2e5c479f769aa316e Mon Sep 17 00:00:00 2001 From: zhanghaobo Date: Wed, 27 Nov 2024 14:26:58 +0800 Subject: [PATCH 04/12] fix checkstyles. --- .../apache/hadoop/hdfs/server/datanode/DatanodeUtil.java | 4 ++-- .../hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java | 7 ++++--- .../hdfs/server/datanode/TestDataSetLockManager.java | 3 ++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java index af7603178eae1..3b3e9444f59df 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java @@ -130,7 +130,7 @@ public static File idToBlockDir(File root, long blockId) { } /** - * Take an example: we hava a block with blockid mapping to: + * Take an example. We hava a block with blockid mapping to: * "/data1/hadoop/hdfs/datanode/current/BP-xxxx/current/finalized/subdir0/subdir0" * We return "subdir0/subdir0" * @return @@ -141,7 +141,7 @@ public static String idToBlockDirSuffixName(long blockId) { return DataStorage.BLOCK_SUBDIR_PREFIX + d1 + SEP + DataStorage.BLOCK_SUBDIR_PREFIX + d2; } - + public static List getAllSubDirNameForDataSetLock() { List res = new ArrayList<>(); for (int d1 = 0; d1 <= 0x1F; d1++) { diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java index cdd2f202ccbd6..c118190827ba6 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java @@ -436,7 +436,6 @@ private synchronized void activateVolume( lockManager.addLock(LockLevel.DIR, bp, ref.getVolume().getStorageID(), dir); LOG.info("Added DIR lock for bpid:{}, volume storageid:{}, dir:{}", bp, ref.getVolume().getStorageID(), dir); - } } DatanodeStorage dnStorage = storageMap.get(sd.getStorageUuid()); @@ -1549,7 +1548,8 @@ public ReplicaHandler recoverAppend( while (true) { try { try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, - b.getBlockPoolId(), getStorageUuidForLock(b), DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { + b.getBlockPoolId(), getStorageUuidForLock(b), + DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { ReplicaInfo replicaInfo = recoverCheck(b, newGS, expectedBlockLen); FsVolumeReference ref = replicaInfo.getVolume().obtainReference(); ReplicaInPipeline replica; @@ -1939,7 +1939,8 @@ public ReplicaHandler createTemporary(StorageType storageType, FsVolumeImpl v = (FsVolumeImpl) ref.getVolume(); ReplicaInPipeline newReplicaInfo; try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, - b.getBlockPoolId(), v.getStorageID(), DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { + b.getBlockPoolId(), v.getStorageID(), + DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { try { newReplicaInfo = v.createTemporary(b); LOG.debug("creating temporary for block: {} on volume: {}", diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDataSetLockManager.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDataSetLockManager.java index 9efdc3f89a684..533aae6e27109 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDataSetLockManager.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDataSetLockManager.java @@ -65,7 +65,8 @@ public void testBaseFunc() { AutoCloseDataSetLock lock6 = manager.writeLock(LockLevel.BLOCK_POOl, "BPtest"); AutoCloseDataSetLock lock7 = manager.readLock(LockLevel.VOLUME, "BPtest", "Volumetest"); - AutoCloseDataSetLock lock8 = manager.readLock(LockLevel.DIR, "BPtest", "Volumetest", "SubDirtest"); + AutoCloseDataSetLock lock8 = manager.readLock(LockLevel.DIR, + "BPtest", "Volumetest", "SubDirtest"); lock8.close(); lock7.close(); lock6.close(); From c723265a5f95d118c66a808eefabc3eadcdfe07f Mon Sep 17 00:00:00 2001 From: zhanghaobo Date: Wed, 27 Nov 2024 14:32:06 +0800 Subject: [PATCH 05/12] fix checkstyles. --- .../hadoop/hdfs/server/datanode/TestDataSetLockManager.java | 1 - 1 file changed, 1 deletion(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDataSetLockManager.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDataSetLockManager.java index 533aae6e27109..6cb12d2681f82 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDataSetLockManager.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/TestDataSetLockManager.java @@ -100,5 +100,4 @@ public void testLockLeakCheck() { Exception lastException = manager.getLastException(); assertEquals(lastException.getMessage(), "lock Leak"); } - } From 3d579ae5cdb57e1a7c8e43b5d91a4d385c0ea530 Mon Sep 17 00:00:00 2001 From: zhanghaobo Date: Thu, 28 Nov 2024 23:43:19 +0800 Subject: [PATCH 06/12] fix failed UTs --- .../datanode/fsdataset/impl/TestFsDatasetImpl.java | 8 +++++++- .../hadoop/hdfs/server/namenode/ha/TestDNFencing.java | 11 ++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/TestFsDatasetImpl.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/TestFsDatasetImpl.java index 975874edb1fc0..fa677f0ceab0d 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/TestFsDatasetImpl.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/TestFsDatasetImpl.java @@ -68,6 +68,7 @@ import org.apache.hadoop.hdfs.server.datanode.FinalizedReplica; import org.apache.hadoop.hdfs.server.datanode.ReplicaHandler; import org.apache.hadoop.hdfs.server.datanode.ReplicaInfo; +import org.apache.hadoop.hdfs.server.datanode.ReplicaNotFoundException; import org.apache.hadoop.hdfs.server.datanode.ShortCircuitRegistry; import org.apache.hadoop.hdfs.server.datanode.StorageLocation; import org.apache.hadoop.hdfs.server.datanode.fsdataset.DataNodeVolumeMetrics; @@ -1946,7 +1947,12 @@ public void delayDeleteReplica() { assertFalse(uuids.contains(dn.getDatanodeUuid())); // This replica has deleted from datanode memory. - assertNull(ds.getStoredBlock(bpid, extendedBlock.getBlockId())); + try { + Block storedBlock = ds.getStoredBlock(bpid, extendedBlock.getBlockId()); + assertNull(storedBlock); + } catch (Exception e) { + GenericTestUtils.assertExceptionContains("ReplicaNotFoundException", e); + } } finally { cluster.shutdown(); DataNodeFaultInjector.set(oldInjector); diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestDNFencing.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestDNFencing.java index 9d79e496102db..2846c16c220e6 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestDNFencing.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestDNFencing.java @@ -50,6 +50,7 @@ import org.apache.hadoop.hdfs.server.datanode.DataNode; import org.apache.hadoop.hdfs.server.datanode.DataNodeTestUtils; import org.apache.hadoop.hdfs.server.datanode.InternalDataNodeTestUtils; +import org.apache.hadoop.hdfs.server.datanode.ReplicaNotFoundException; import org.apache.hadoop.hdfs.server.namenode.NameNode; import org.apache.hadoop.hdfs.server.namenode.NameNodeAdapter; import org.apache.hadoop.io.IOUtils; @@ -596,9 +597,13 @@ private int getTrueReplication(MiniDFSCluster cluster, ExtendedBlock block) throws IOException { int count = 0; for (DataNode dn : cluster.getDataNodes()) { - if (DataNodeTestUtils.getFSDataset(dn).getStoredBlock( - block.getBlockPoolId(), block.getBlockId()) != null) { - count++; + try { + if (DataNodeTestUtils.getFSDataset(dn).getStoredBlock( + block.getBlockPoolId(), block.getBlockId()) != null) { + count++; + } + } catch (ReplicaNotFoundException e) { + continue; } } return count; From 94d6660eafbca5e7d9f997b34c01ebcbac57ea15 Mon Sep 17 00:00:00 2001 From: zhanghaobo Date: Fri, 29 Nov 2024 10:54:26 +0800 Subject: [PATCH 07/12] trigger yetus. From 475184630877c02d9f1f7f9fd60ca5837dfe77c5 Mon Sep 17 00:00:00 2001 From: zhanghaobo Date: Tue, 3 Dec 2024 09:43:24 +0800 Subject: [PATCH 08/12] fix checkstyle --- .../org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java | 3 ++- .../hdfs/server/datanode/fsdataset/impl/TestFsDatasetImpl.java | 1 - 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java index 3b3e9444f59df..dc5d70756277c 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java @@ -133,7 +133,8 @@ public static File idToBlockDir(File root, long blockId) { * Take an example. We hava a block with blockid mapping to: * "/data1/hadoop/hdfs/datanode/current/BP-xxxx/current/finalized/subdir0/subdir0" * We return "subdir0/subdir0" - * @return + * @param blockId blockId + * @return The two-level subdir name */ public static String idToBlockDirSuffixName(long blockId) { int d1 = (int) ((blockId >> 16) & 0x1F); diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/TestFsDatasetImpl.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/TestFsDatasetImpl.java index fa677f0ceab0d..f58ee729ef98f 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/TestFsDatasetImpl.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/TestFsDatasetImpl.java @@ -68,7 +68,6 @@ import org.apache.hadoop.hdfs.server.datanode.FinalizedReplica; import org.apache.hadoop.hdfs.server.datanode.ReplicaHandler; import org.apache.hadoop.hdfs.server.datanode.ReplicaInfo; -import org.apache.hadoop.hdfs.server.datanode.ReplicaNotFoundException; import org.apache.hadoop.hdfs.server.datanode.ShortCircuitRegistry; import org.apache.hadoop.hdfs.server.datanode.StorageLocation; import org.apache.hadoop.hdfs.server.datanode.fsdataset.DataNodeVolumeMetrics; From 263a5ca83213fd369f88d2b6fda96b073f3991b4 Mon Sep 17 00:00:00 2001 From: zhanghaobo Date: Tue, 3 Dec 2024 14:46:01 +0800 Subject: [PATCH 09/12] checkAndUpdate use subdir lock --- .../hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java index c118190827ba6..6598a8c3dca81 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java @@ -2774,7 +2774,8 @@ public void checkAndUpdate(String bpid, ScanInfo scanInfo) lastDirScannerNotifyTime = startTimeMs; } String storageUuid = vol.getStorageID(); - try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.VOLUME, bpid, storageUuid)) { + try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, bpid, + vol.getStorageID(), DatanodeUtil.idToBlockDirSuffixName(blockId))) { if (!storageMap.containsKey(storageUuid)) { // Storage was already removed return; From d4f9eb19dd2ce497c056cea837ff33944a22174e Mon Sep 17 00:00:00 2001 From: zhanghaobo Date: Sun, 8 Dec 2024 18:56:11 +0800 Subject: [PATCH 10/12] fix npe check. --- .../hadoop/hdfs/server/datanode/DataSetLockManager.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataSetLockManager.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataSetLockManager.java index a24eb334834ec..61492467a4129 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataSetLockManager.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataSetLockManager.java @@ -95,6 +95,11 @@ private String generateLockName(LockLevel level, String... resources) { } return resources[0] + resources[1]; } else if (resources.length == 3 && level == LockLevel.DIR) { + if (resources[0] == null || resources[1] == null || resources[2] == null) { + throw new IllegalArgumentException("acquire a null dataset lock : " + + resources[0] + ",volume lock :" + resources[1] + + ",subdir lock :" + resources[2]); + } return resources[0] + resources[1] + resources[2]; } else { throw new IllegalArgumentException("lock level do not match resource"); From 9eb153c7b7d62360d8387cbea91fd15cafbb0bed Mon Sep 17 00:00:00 2001 From: zhanghaobo Date: Mon, 16 Dec 2024 14:04:35 +0800 Subject: [PATCH 11/12] Add interface DataSetSubLockStrategy --- .../org/apache/hadoop/hdfs/DFSConfigKeys.java | 4 ++ .../datanode/DataSetSubLockStrategy.java | 36 +++++++++++ .../hdfs/server/datanode/DatanodeUtil.java | 27 -------- .../datanode/ModDataSetSubLockStrategy.java | 53 ++++++++++++++++ .../fsdataset/impl/FsDatasetImpl.java | 62 +++++++++++-------- 5 files changed, 128 insertions(+), 54 deletions(-) create mode 100644 hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataSetSubLockStrategy.java create mode 100644 hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/ModDataSetSubLockStrategy.java diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSConfigKeys.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSConfigKeys.java index dd3193fdadff2..d85e7c5823192 100755 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSConfigKeys.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/DFSConfigKeys.java @@ -1744,6 +1744,10 @@ public class DFSConfigKeys extends CommonConfigurationKeys { public static final boolean DFS_DATANODE_LOCKMANAGER_TRACE_DEFAULT = false; + public static final String DFS_DATANODE_DATASET_SUBLOCK_COUNT_KEY = + "dfs.datanode.dataset.sublock.count"; + public static final long DFS_DATANODE_DATASET_SUBLOCK_COUNT_DEFAULT = 1000L; + // dfs.client.retry confs are moved to HdfsClientConfigKeys.Retry @Deprecated public static final String DFS_CLIENT_RETRY_POLICY_ENABLED_KEY diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataSetSubLockStrategy.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataSetSubLockStrategy.java new file mode 100644 index 0000000000000..7ba1df8df5232 --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DataSetSubLockStrategy.java @@ -0,0 +1,36 @@ +/** + * 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.hdfs.server.datanode; + +import java.util.List; + +/** + * This interface is used to generate sub lock name for a blockid. + */ +public interface DataSetSubLockStrategy { + + /** + * Generate sub lock name for the given blockid. + * @param blockid the block id. + * @return sub lock name for the input blockid. + */ + String blockIdToSubLock(long blockid); + + List getAllSubLockName(); +} diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java index dc5d70756277c..c98ff5413bd85 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/DatanodeUtil.java @@ -21,8 +21,6 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.hdfs.protocol.Block; @@ -129,31 +127,6 @@ public static File idToBlockDir(File root, long blockId) { return new File(root, path); } - /** - * Take an example. We hava a block with blockid mapping to: - * "/data1/hadoop/hdfs/datanode/current/BP-xxxx/current/finalized/subdir0/subdir0" - * We return "subdir0/subdir0" - * @param blockId blockId - * @return The two-level subdir name - */ - public static String idToBlockDirSuffixName(long blockId) { - int d1 = (int) ((blockId >> 16) & 0x1F); - int d2 = (int) ((blockId >> 8) & 0x1F); - return DataStorage.BLOCK_SUBDIR_PREFIX + d1 + SEP + - DataStorage.BLOCK_SUBDIR_PREFIX + d2; - } - - public static List getAllSubDirNameForDataSetLock() { - List res = new ArrayList<>(); - for (int d1 = 0; d1 <= 0x1F; d1++) { - for (int d2 = 0; d2 <= 0x1F; d2++) { - res.add(DataStorage.BLOCK_SUBDIR_PREFIX + d1 + SEP + - DataStorage.BLOCK_SUBDIR_PREFIX + d2); - } - } - return res; - } - /** * @return the FileInputStream for the meta data of the given block. * @throws FileNotFoundException diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/ModDataSetSubLockStrategy.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/ModDataSetSubLockStrategy.java new file mode 100644 index 0000000000000..3f22ca7a0b8ca --- /dev/null +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/ModDataSetSubLockStrategy.java @@ -0,0 +1,53 @@ +/** + * 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.hdfs.server.datanode; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; + +public class ModDataSetSubLockStrategy implements DataSetSubLockStrategy { + public static final Logger LOG = LoggerFactory.getLogger(DataSetSubLockStrategy.class); + + private static final String LOCK_NAME_PERFIX = "SubLock"; + private long modFactor; + + public ModDataSetSubLockStrategy(long mod) { + if (mod <= 0) { + mod = 1L; + } + this.modFactor = mod; + } + + @Override + public String blockIdToSubLock(long blockid) { + return LOCK_NAME_PERFIX + String.valueOf(blockid % modFactor); + } + + @Override + public List getAllSubLockName() { + List res = new ArrayList<>(); + for (long i = 0L; i < modFactor; i++) { + res.add(LOCK_NAME_PERFIX + i); + } + return res; + } +} diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java index 6598a8c3dca81..91b12daef8143 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/fsdataset/impl/FsDatasetImpl.java @@ -65,9 +65,11 @@ import org.apache.hadoop.hdfs.server.common.DataNodeLockManager.LockLevel; import org.apache.hadoop.hdfs.server.datanode.DataNodeFaultInjector; import org.apache.hadoop.hdfs.server.datanode.DataSetLockManager; +import org.apache.hadoop.hdfs.server.datanode.DataSetSubLockStrategy; import org.apache.hadoop.hdfs.server.datanode.FileIoProvider; import org.apache.hadoop.hdfs.server.datanode.FinalizedReplica; import org.apache.hadoop.hdfs.server.datanode.LocalReplica; +import org.apache.hadoop.hdfs.server.datanode.ModDataSetSubLockStrategy; import org.apache.hadoop.hdfs.server.datanode.metrics.DataNodeMetrics; import org.apache.hadoop.util.AutoCloseableLock; import org.apache.hadoop.hdfs.protocol.Block; @@ -200,7 +202,7 @@ public Block getStoredBlock(String bpid, long blkid) throws IOException { try (AutoCloseableLock lock = lockManager.readLock(LockLevel.DIR, bpid, getReplicaInfo(bpid, blkid).getStorageUuid(), - DatanodeUtil.idToBlockDirSuffixName(blkid))) { + datasetSubLockStrategy.blockIdToSubLock(blkid))) { ReplicaInfo r = volumeMap.get(bpid, blkid); if (r == null) { return null; @@ -289,6 +291,9 @@ public LengthInputStream getMetaDataInputStream(ExtendedBlock b) private long lastDirScannerNotifyTime; private volatile long lastDirScannerFinishTime; + private final DataSetSubLockStrategy datasetSubLockStrategy; + private final long datasetSubLockCount; + /** * An FSDataset has a directory where it loads its data files. */ @@ -393,6 +398,9 @@ public LengthInputStream getMetaDataInputStream(ExtendedBlock b) DFSConfigKeys.DFS_DATANODE_DIRECTORYSCAN_MAX_NOTIFY_COUNT_KEY, DFSConfigKeys.DFS_DATANODE_DIRECTORYSCAN_MAX_NOTIFY_COUNT_DEFAULT); lastDirScannerNotifyTime = System.currentTimeMillis(); + datasetSubLockCount = conf.getLong(DFSConfigKeys.DFS_DATANODE_DATASET_SUBLOCK_COUNT_KEY, + DFSConfigKeys.DFS_DATANODE_DATASET_SUBLOCK_COUNT_DEFAULT); + this.datasetSubLockStrategy = new ModDataSetSubLockStrategy(datasetSubLockCount); } /** @@ -431,7 +439,7 @@ private synchronized void activateVolume( FsVolumeReference ref) throws IOException { for (String bp : volumeMap.getBlockPoolList()) { lockManager.addLock(LockLevel.VOLUME, bp, ref.getVolume().getStorageID()); - List allSubDirNameForDataSetLock = DatanodeUtil.getAllSubDirNameForDataSetLock(); + List allSubDirNameForDataSetLock = datasetSubLockStrategy.getAllSubLockName(); for (String dir : allSubDirNameForDataSetLock) { lockManager.addLock(LockLevel.DIR, bp, ref.getVolume().getStorageID(), dir); LOG.info("Added DIR lock for bpid:{}, volume storageid:{}, dir:{}", @@ -636,7 +644,7 @@ public void removeVolumes( for (String storageUuid : storageToRemove) { storageMap.remove(storageUuid); for (String bp : volumeMap.getBlockPoolList()) { - List allSubDirNameForDataSetLock = DatanodeUtil.getAllSubDirNameForDataSetLock(); + List allSubDirNameForDataSetLock = datasetSubLockStrategy.getAllSubLockName(); for (String dir : allSubDirNameForDataSetLock) { lockManager.removeLock(LockLevel.DIR, bp, storageUuid, dir); LOG.info("Removed DIR lock for bpid:{}, volume storageid:{}, dir:{}", @@ -834,7 +842,7 @@ public InputStream getBlockInputStream(ExtendedBlock b, ReplicaInfo info; try (AutoCloseableLock lock = lockManager.readLock(LockLevel.DIR, b.getBlockPoolId(), getStorageUuidForLock(b), - DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { + datasetSubLockStrategy.blockIdToSubLock(b.getBlockId()))) { info = volumeMap.get(b.getBlockPoolId(), b.getLocalBlock()); } @@ -930,7 +938,7 @@ public ReplicaInputStreams getTmpInputStreams(ExtendedBlock b, long blkOffset, long metaOffset) throws IOException { try (AutoCloseDataSetLock l = lockManager.readLock(LockLevel.DIR, b.getBlockPoolId(), getStorageUuidForLock(b), - DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { + datasetSubLockStrategy.blockIdToSubLock(b.getBlockId()))) { ReplicaInfo info = getReplicaInfo(b); FsVolumeReference ref = info.getVolume().obtainReference(); try { @@ -1397,7 +1405,7 @@ public ReplicaHandler append(ExtendedBlock b, long newGS, long expectedBlockLen) throws IOException { try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, b.getBlockPoolId(), getStorageUuidForLock(b), - DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { + datasetSubLockStrategy.blockIdToSubLock(b.getBlockId()))) { // If the block was successfully finalized because all packets // were successfully processed at the Datanode but the ack for // some of the packets were not received by the client. The client @@ -1451,7 +1459,7 @@ private ReplicaInPipeline append(String bpid, throws IOException { try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, bpid, replicaInfo.getStorageUuid(), - DatanodeUtil.idToBlockDirSuffixName(replicaInfo.getBlockId()))) { + datasetSubLockStrategy.blockIdToSubLock(replicaInfo.getBlockId()))) { // If the block is cached, start uncaching it. if (replicaInfo.getState() != ReplicaState.FINALIZED) { throw new IOException("Only a Finalized replica can be appended to; " @@ -1549,7 +1557,7 @@ public ReplicaHandler recoverAppend( try { try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, b.getBlockPoolId(), getStorageUuidForLock(b), - DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { + datasetSubLockStrategy.blockIdToSubLock(b.getBlockId()))) { ReplicaInfo replicaInfo = recoverCheck(b, newGS, expectedBlockLen); FsVolumeReference ref = replicaInfo.getVolume().obtainReference(); ReplicaInPipeline replica; @@ -1584,7 +1592,7 @@ public Replica recoverClose(ExtendedBlock b, long newGS, try { try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, b.getBlockPoolId(), getStorageUuidForLock(b), - DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { + datasetSubLockStrategy.blockIdToSubLock(b.getBlockId()))) { // check replica's state ReplicaInfo replicaInfo = recoverCheck(b, newGS, expectedBlockLen); // bump the replica's GS @@ -1671,7 +1679,7 @@ public ReplicaHandler createRbw( ReplicaInPipeline newReplicaInfo; try (AutoCloseableLock l = lockManager.writeLock(LockLevel.DIR, b.getBlockPoolId(), v.getStorageID(), - DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { + datasetSubLockStrategy.blockIdToSubLock(b.getBlockId()))) { newReplicaInfo = v.createRbw(b); if (newReplicaInfo.getReplicaInfo().getState() != ReplicaState.RBW) { throw new IOException("CreateRBW returned a replica of state " @@ -1703,7 +1711,7 @@ public ReplicaHandler recoverRbw( try { try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, b.getBlockPoolId(), getStorageUuidForLock(b), - DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { + datasetSubLockStrategy.blockIdToSubLock(b.getBlockId()))) { ReplicaInfo replicaInfo = getReplicaInfo(b.getBlockPoolId(), b.getBlockId()); // check the replica's state @@ -1736,7 +1744,7 @@ private ReplicaHandler recoverRbwImpl(ReplicaInPipeline rbw, throws IOException { try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, b.getBlockPoolId(), getStorageUuidForLock(b), - DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { + datasetSubLockStrategy.blockIdToSubLock(b.getBlockId()))) { // check generation stamp long replicaGenerationStamp = rbw.getGenerationStamp(); if (replicaGenerationStamp < b.getGenerationStamp() || @@ -1799,7 +1807,7 @@ public ReplicaInPipeline convertTemporaryToRbw( long startTimeMs = Time.monotonicNow(); try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, b.getBlockPoolId(), getStorageUuidForLock(b), - DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { + datasetSubLockStrategy.blockIdToSubLock(b.getBlockId()))) { final long blockId = b.getBlockId(); final long expectedGs = b.getGenerationStamp(); final long visible = b.getNumBytes(); @@ -1940,7 +1948,7 @@ public ReplicaHandler createTemporary(StorageType storageType, ReplicaInPipeline newReplicaInfo; try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, b.getBlockPoolId(), v.getStorageID(), - DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { + datasetSubLockStrategy.blockIdToSubLock(b.getBlockId()))) { try { newReplicaInfo = v.createTemporary(b); LOG.debug("creating temporary for block: {} on volume: {}", @@ -1999,7 +2007,7 @@ public void finalizeBlock(ExtendedBlock b, boolean fsyncDir) long startTimeMs = Time.monotonicNow(); try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, b.getBlockPoolId(), getStorageUuidForLock(b), - DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { + datasetSubLockStrategy.blockIdToSubLock(b.getBlockId()))) { if (Thread.interrupted()) { // Don't allow data modifications from interrupted threads throw new IOException("Cannot finalize block: " + b + " from Interrupted Thread"); @@ -2037,7 +2045,7 @@ private ReplicaInfo finalizeReplica(String bpid, ReplicaInfo replicaInfo) throws IOException { try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, bpid, replicaInfo.getStorageUuid(), - DatanodeUtil.idToBlockDirSuffixName(replicaInfo.getBlockId()))) { + datasetSubLockStrategy.blockIdToSubLock(replicaInfo.getBlockId()))) { // Compare generation stamp of old and new replica before finalizing if (volumeMap.get(bpid, replicaInfo.getBlockId()).getGenerationStamp() > replicaInfo.getGenerationStamp()) { @@ -2088,7 +2096,7 @@ public void unfinalizeBlock(ExtendedBlock b) throws IOException { long startTimeMs = Time.monotonicNow(); try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, b.getBlockPoolId(), getStorageUuidForLock(b), - DatanodeUtil.idToBlockDirSuffixName(b.getBlockId()))) { + datasetSubLockStrategy.blockIdToSubLock(b.getBlockId()))) { ReplicaInfo replicaInfo = volumeMap.get(b.getBlockPoolId(), b.getLocalBlock()); if (replicaInfo != null && @@ -2487,7 +2495,7 @@ boolean removeReplicaFromMem(final ExtendedBlock block, final FsVolumeImpl volum final Block localBlock = block.getLocalBlock(); final long blockId = localBlock.getBlockId(); try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, bpid, volume.getStorageID(), - DatanodeUtil.idToBlockDirSuffixName(blockId))) { + datasetSubLockStrategy.blockIdToSubLock(blockId))) { final ReplicaInfo info = volumeMap.get(bpid, localBlock); if (info == null) { ReplicaInfo infoByBlockId = volumeMap.get(bpid, blockId); @@ -2577,7 +2585,7 @@ private void cacheBlock(String bpid, long blockId) { return; } try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, bpid, - info.getStorageUuid(), DatanodeUtil.idToBlockDirSuffixName(blockId))) { + info.getStorageUuid(), datasetSubLockStrategy.blockIdToSubLock(blockId))) { boolean success = false; try { info = volumeMap.get(bpid, blockId); @@ -2775,7 +2783,7 @@ public void checkAndUpdate(String bpid, ScanInfo scanInfo) } String storageUuid = vol.getStorageID(); try (AutoCloseableLock lock = lockManager.writeLock(LockLevel.DIR, bpid, - vol.getStorageID(), DatanodeUtil.idToBlockDirSuffixName(blockId))) { + vol.getStorageID(), datasetSubLockStrategy.blockIdToSubLock(blockId))) { if (!storageMap.containsKey(storageUuid)) { // Storage was already removed return; @@ -3031,8 +3039,8 @@ ReplicaRecoveryInfo initReplicaRecovery(String bpid, ReplicaMap map, } LOG.info("initReplicaRecovery: " + block + ", recoveryId=" + recoveryId + ", replica=" + replica); - try (AutoCloseDataSetLock l = lockManager.writeLock(LockLevel.DIR, bpid, - replica.getStorageUuid(), DatanodeUtil.idToBlockDirSuffixName(block.getBlockId()))) { + try (AutoCloseDataSetLock l = lockManager.writeLock(LockLevel.VOLUME, bpid, + replica.getStorageUuid())) { return initReplicaRecoveryImpl(bpid, map, block, recoveryId); } } catch (MustStopExistingWriter e) { @@ -3053,8 +3061,8 @@ static ReplicaRecoveryInfo initReplicaRecovery(String bpid, ReplicaMap map, } LOG.info("initReplicaRecovery: " + block + ", recoveryId=" + recoveryId + ", replica=" + replica); - try (AutoCloseDataSetLock l = lockManager.writeLock(LockLevel.DIR, bpid, - replica.getStorageUuid(), DatanodeUtil.idToBlockDirSuffixName(block.getBlockId()))) { + try (AutoCloseDataSetLock l = lockManager.writeLock(LockLevel.VOLUME, bpid, + replica.getStorageUuid())) { return initReplicaRecoveryImpl(bpid, map, block, recoveryId); } } catch (MustStopExistingWriter e) { @@ -3262,7 +3270,7 @@ public long getReplicaVisibleLength(final ExtendedBlock block) throws IOException { try (AutoCloseableLock lock = lockManager.readLock(LockLevel.DIR, block.getBlockPoolId(), getStorageUuidForLock(block), - DatanodeUtil.idToBlockDirSuffixName(block.getBlockId()))) { + datasetSubLockStrategy.blockIdToSubLock(block.getBlockId()))) { final Replica replica = getReplicaInfo(block.getBlockPoolId(), block.getBlockId()); if (replica.getGenerationStamp() < block.getGenerationStamp()) { @@ -3289,7 +3297,7 @@ public void addBlockPool(String bpid, Configuration conf) Set vols = storageMap.keySet(); for (String v : vols) { lockManager.addLock(LockLevel.VOLUME, bpid, v); - List allSubDirNameForDataSetLock = DatanodeUtil.getAllSubDirNameForDataSetLock(); + List allSubDirNameForDataSetLock = datasetSubLockStrategy.getAllSubLockName(); for (String dir : allSubDirNameForDataSetLock) { lockManager.addLock(LockLevel.DIR, bpid, v, dir); LOG.info("Added DIR lock for bpid:{}, volume storageid:{}, dir:{}", @@ -3424,7 +3432,7 @@ public BlockLocalPathInfo getBlockLocalPathInfo(ExtendedBlock block) throws IOException { try (AutoCloseableLock lock = lockManager.readLock(LockLevel.DIR, block.getBlockPoolId(), getStorageUuidForLock(block), - DatanodeUtil.idToBlockDirSuffixName(block.getBlockId()))) { + datasetSubLockStrategy.blockIdToSubLock(block.getBlockId()))) { final Replica replica = volumeMap.get(block.getBlockPoolId(), block.getBlockId()); if (replica == null) { From 226cd5b046e5a4aaeb66c58812740d0714fc9bbb Mon Sep 17 00:00:00 2001 From: zhanghaobo Date: Tue, 24 Dec 2024 10:15:58 +0800 Subject: [PATCH 12/12] fix failed UT. --- .../hdfs/server/datanode/ModDataSetSubLockStrategy.java | 2 +- .../hadoop-hdfs/src/main/resources/hdfs-default.xml | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/ModDataSetSubLockStrategy.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/ModDataSetSubLockStrategy.java index 3f22ca7a0b8ca..5e736e54716fd 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/ModDataSetSubLockStrategy.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/datanode/ModDataSetSubLockStrategy.java @@ -39,7 +39,7 @@ public ModDataSetSubLockStrategy(long mod) { @Override public String blockIdToSubLock(long blockid) { - return LOCK_NAME_PERFIX + String.valueOf(blockid % modFactor); + return LOCK_NAME_PERFIX + (blockid % modFactor); } @Override diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/resources/hdfs-default.xml b/hadoop-hdfs-project/hadoop-hdfs/src/main/resources/hdfs-default.xml index 2ab25f8329ce6..6bfed9a2904c0 100755 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/resources/hdfs-default.xml +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/resources/hdfs-default.xml @@ -6568,6 +6568,15 @@ problem. In produce default set false, because it's have little performance loss. + + + dfs.datanode.dataset.sublock.count + 1000 + + The dataset readwrite lock counts for a volume. + + + dfs.client.fsck.connect.timeout 60000ms