KevinWikant commented on code in PR #7179: URL: https://github.com/apache/hadoop/pull/7179#discussion_r1894077582
########## hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/UnderConstructionBlocks.java: ########## @@ -0,0 +1,331 @@ +/** + * 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.blockmanagement; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hdfs.DFSConfigKeys; +import org.apache.hadoop.hdfs.protocol.Block; +import org.apache.hadoop.thirdparty.com.google.common.collect.Maps; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.time.Duration; +import java.time.Instant; +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * The BlockManager will not add an Under Construction + * block to the DatanodeDescriptor StorageInfos until + * the block is fully committed and finalized. + * The UC block replicas are instead tracked here + * for the DatanodeAdminManager to use. + * Note that this is tracked in-memory only, as such + * some Under Construction blocks may be missed under + * scenarios where Namenode is restarted. + **/ +public class UnderConstructionBlocks { + private static final Logger LOG = + LoggerFactory.getLogger(UnderConstructionBlocks.class); + + // Amount of time to wait in between checking all block replicas + private static final Duration LONG_UNDER_CONSTRUCTION_BLOCK_CHECK_INTERVAL + = Duration.ofMinutes(5); + // Amount of time to wait before logging each individual block replica + // as warning. + private static final Duration LONG_UNDER_CONSTRUCTION_BLOCK_WARN_THRESHOLD + = Duration.ofHours(2); + private static final Duration LONG_UNDER_CONSTRUCTION_BLOCK_WARN_INTERVAL + = Duration.ofMinutes(30); + + private final Map<Block, Set<BlockReplica>> replicasByBlockId = + Maps.newHashMap(); + private final boolean enabled; + private int count = 0; + // DatanodeAdminMonitor invokes logWarningForLongUnderConstructionBlocks every 30 seconds. + // To reduce the number of times this method loops through the Under Construction blocks, + // the interval is limited by LONG_UNDER_CONSTRUCTION_BLOCK_CHECK_INTERVAL. + private Instant nextWarnLogCheck = + Instant.now().plus(LONG_UNDER_CONSTRUCTION_BLOCK_CHECK_INTERVAL); + + static class BlockReplica { + private final Block block; + private final DatanodeDescriptor dn; + private final Instant firstReportedTime; + private Instant nextWarnLog; + + BlockReplica(Block block, + DatanodeDescriptor dn) { + this.block = block; + this.dn = dn; + this.firstReportedTime = Instant.now(); + this.nextWarnLog = firstReportedTime.plus(LONG_UNDER_CONSTRUCTION_BLOCK_WARN_THRESHOLD); + } + + Block getBlock() { + return block; + } + + DatanodeDescriptor getDatanode() { + return dn; + } + + boolean shouldLogWarning() { + if (Instant.now().isBefore(nextWarnLog)) { + return false; + } + nextWarnLog = Instant.now().plus(LONG_UNDER_CONSTRUCTION_BLOCK_WARN_INTERVAL); + return true; + } + + Duration getDurationSinceReporting() { + return Duration.between(firstReportedTime, Instant.now()); + } + + @Override + public String toString() { Review Comment: Nice catch, I missed updating this when leveraging this existing code: https://github.com/apache/hadoop/blob/trunk/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/PendingDataNodeMessages.java#L69 -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
