aryangupta1998 commented on code in PR #8422: URL: https://github.com/apache/ozone/pull/8422#discussion_r2085443450
########## hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/replicas/ReplicaStateVerifier.java: ########## @@ -0,0 +1,221 @@ +/* + * 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.ozone.debug.replicas; + +import static org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationFactor.ONE; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import java.io.IOException; +import java.util.Collections; +import java.util.Objects; +import org.apache.hadoop.hdds.client.StandaloneReplicationConfig; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.protocol.DatanodeDetails; +import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ContainerDataProto; +import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ReadContainerResponseProto; +import org.apache.hadoop.hdds.scm.XceiverClientManager; +import org.apache.hadoop.hdds.scm.XceiverClientSpi; +import org.apache.hadoop.hdds.scm.cli.ContainerOperationClient; +import org.apache.hadoop.hdds.scm.container.ContainerInfo; +import org.apache.hadoop.hdds.scm.pipeline.Pipeline; +import org.apache.hadoop.hdds.scm.storage.ContainerProtocolCalls; +import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfo; + +/** + * Verifies the state of a replica from the DN. + * [DELETED, UNHEALTHY, INVALID] are considered bad states. + */ +public class ReplicaStateVerifier implements ReplicaVerifier { + private static final String CHECK_TYPE = "replicaState"; + private final ContainerOperationClient containerOperationClient; + private final XceiverClientManager xceiverClientManager; + // cache for replica details from the DNs + private final Cache<ReplicaKey, ContainerDataProto> containerDataCache = CacheBuilder.newBuilder() + .maximumSize(1000) + .build(); + // cache for container info and encodedToken from the SCM + private final Cache<Long, ContainerInfoToken> encodedTokenCache = CacheBuilder.newBuilder() + .maximumSize(1000) + .build(); + + public ReplicaStateVerifier(OzoneConfiguration conf) throws IOException { + containerOperationClient = new ContainerOperationClient(conf); + xceiverClientManager = containerOperationClient.getXceiverClientManager(); + } + + @Override + public String getType() { + return CHECK_TYPE; + } + + @Override + public BlockVerificationResult verifyBlock(DatanodeDetails datanode, OmKeyLocationInfo keyLocation, + int replicaIndex) { + try { + StringBuilder replicaCheckMsg = new StringBuilder().append("Replica state is "); + boolean pass = false; + + ContainerInfoToken containerInfoToken = getContainerInfoToken(keyLocation.getContainerID()); + ContainerInfo containerInfo = containerInfoToken.getContainerInfo(); + + ContainerDataProto containerData = getContainerData(datanode, keyLocation, replicaIndex, containerInfoToken); + if (containerData.getState().equals(ContainerDataProto.State.UNHEALTHY)) { + replicaCheckMsg.append("UNHEALTHY"); + } else if (containerData.getState().equals(ContainerDataProto.State.INVALID)) { + replicaCheckMsg.append("INVALID"); + } else if (containerData.getState().equals(ContainerDataProto.State.DELETED)) { + replicaCheckMsg.append("DELETED"); + } else { + pass = true; + } Review Comment: Something like, ```suggestion if (state == ContainerDataProto.State.UNHEALTHY || state == ContainerDataProto.State.INVALID || state == ContainerDataProto.State.DELETED) { replicaCheckMsg.append(state.name()); } else { pass = true; } ``` ########## hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/replicas/ReplicaStateVerifier.java: ########## @@ -0,0 +1,221 @@ +/* + * 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.ozone.debug.replicas; + +import static org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationFactor.ONE; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import java.io.IOException; +import java.util.Collections; +import java.util.Objects; +import org.apache.hadoop.hdds.client.StandaloneReplicationConfig; +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.protocol.DatanodeDetails; +import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ContainerDataProto; +import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ReadContainerResponseProto; +import org.apache.hadoop.hdds.scm.XceiverClientManager; +import org.apache.hadoop.hdds.scm.XceiverClientSpi; +import org.apache.hadoop.hdds.scm.cli.ContainerOperationClient; +import org.apache.hadoop.hdds.scm.container.ContainerInfo; +import org.apache.hadoop.hdds.scm.pipeline.Pipeline; +import org.apache.hadoop.hdds.scm.storage.ContainerProtocolCalls; +import org.apache.hadoop.ozone.om.helpers.OmKeyLocationInfo; + +/** + * Verifies the state of a replica from the DN. + * [DELETED, UNHEALTHY, INVALID] are considered bad states. + */ +public class ReplicaStateVerifier implements ReplicaVerifier { + private static final String CHECK_TYPE = "replicaState"; + private final ContainerOperationClient containerOperationClient; + private final XceiverClientManager xceiverClientManager; + // cache for replica details from the DNs + private final Cache<ReplicaKey, ContainerDataProto> containerDataCache = CacheBuilder.newBuilder() + .maximumSize(1000) + .build(); + // cache for container info and encodedToken from the SCM + private final Cache<Long, ContainerInfoToken> encodedTokenCache = CacheBuilder.newBuilder() + .maximumSize(1000) + .build(); + + public ReplicaStateVerifier(OzoneConfiguration conf) throws IOException { + containerOperationClient = new ContainerOperationClient(conf); + xceiverClientManager = containerOperationClient.getXceiverClientManager(); + } + + @Override + public String getType() { + return CHECK_TYPE; + } + + @Override + public BlockVerificationResult verifyBlock(DatanodeDetails datanode, OmKeyLocationInfo keyLocation, + int replicaIndex) { + try { + StringBuilder replicaCheckMsg = new StringBuilder().append("Replica state is "); + boolean pass = false; + + ContainerInfoToken containerInfoToken = getContainerInfoToken(keyLocation.getContainerID()); + ContainerInfo containerInfo = containerInfoToken.getContainerInfo(); + + ContainerDataProto containerData = getContainerData(datanode, keyLocation, replicaIndex, containerInfoToken); + if (containerData.getState().equals(ContainerDataProto.State.UNHEALTHY)) { + replicaCheckMsg.append("UNHEALTHY"); + } else if (containerData.getState().equals(ContainerDataProto.State.INVALID)) { + replicaCheckMsg.append("INVALID"); + } else if (containerData.getState().equals(ContainerDataProto.State.DELETED)) { + replicaCheckMsg.append("DELETED"); + } else { + pass = true; + } + replicaCheckMsg.append(", Container state in SCM is ").append(containerInfo.getState()); + + if (pass) { + return BlockVerificationResult.pass(); + } else { + return BlockVerificationResult.failCheck(replicaCheckMsg.toString()); + } + } catch (IOException e) { + return BlockVerificationResult.failIncomplete(e.getMessage()); + } + } + + public ContainerDataProto getContainerData(DatanodeDetails dn, OmKeyLocationInfo keyLocation, + int replicaIndex, ContainerInfoToken containerInfoToken) + throws IOException { + long containerId = containerInfoToken.getContainerInfo().getContainerID(); + ReplicaKey key = new ReplicaKey(dn, containerId); + ContainerDataProto cachedData = containerDataCache.getIfPresent(key); + if (cachedData != null) { + return cachedData; + } + // Cache miss - fetch and store + ContainerDataProto data = fetchContainerDataFromDatanode(dn, containerId, keyLocation, + replicaIndex, containerInfoToken); + containerDataCache.put(key, data); Review Comment: ```suggestion if (data != null) { containerDataCache.put(key, data); } ``` If data is null by chance, we'll get a null value from the cache every time! -- 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: issues-unsubscr...@ozone.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@ozone.apache.org For additional commands, e-mail: issues-h...@ozone.apache.org