errose28 commented on code in PR #8422: URL: https://github.com/apache/ozone/pull/8422#discussion_r2122123494
########## hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/debug/replicas/ReplicaStateVerifier.java: ########## @@ -0,0 +1,222 @@ +/* + * 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); + ContainerDataProto.State state = containerData.getState(); + if (state == ContainerDataProto.State.UNHEALTHY || + state == ContainerDataProto.State.INVALID || + state == ContainerDataProto.State.DELETED) { + replicaCheckMsg.append(state.name()); + } 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); + if (data != null) { + containerDataCache.put(key, data); + } + return data; + } + + private ContainerDataProto fetchContainerDataFromDatanode(DatanodeDetails dn, long containerId, + OmKeyLocationInfo keyLocation, int replicaIndex, + ContainerInfoToken containerInfoToken) + throws IOException { + XceiverClientSpi client = null; + ReadContainerResponseProto response; + try { + Pipeline pipeline = Pipeline.newBuilder(keyLocation.getPipeline()) + .setReplicationConfig(StandaloneReplicationConfig.getInstance(ONE)) + .setNodes(Collections.singletonList(dn)) + .setReplicaIndexes(Collections.singletonMap(dn, replicaIndex)) + .build(); + String encodedToken = containerInfoToken.getEncodedToken(); + + client = xceiverClientManager.acquireClientForReadData(pipeline); + response = ContainerProtocolCalls + .readContainer(client, containerId, encodedToken); Review Comment: Can we file a Jira for this and add a comment to this part of the code linking to the Jira? Ideally we should depend on protocol aspects like result codes instead of error messages for determining what went wrong, but I'm ok leaving this workaround in if we have documented it as an area that needs to be fixed. -- 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]
