amaliujia commented on code in PR #11951: URL: https://github.com/apache/ozone/pull/11951#discussion_r4570953207
########## hadoop-ozone/cli-admin/src/main/java/org/apache/hadoop/hdds/scm/cli/storagepolicy/CheckStoragePolicySubCommand.java: ########## @@ -0,0 +1,236 @@ +/* + * 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.hdds.scm.cli.storagepolicy; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import org.apache.hadoop.fs.StorageType; +import org.apache.hadoop.hdds.cli.HddsVersionProvider; +import org.apache.hadoop.hdds.client.OzoneStoragePolicy; +import org.apache.hadoop.hdds.client.StorageTier; +import org.apache.hadoop.hdds.protocol.DatanodeDetails; +import org.apache.hadoop.hdds.scm.cli.ScmSubcommand; +import org.apache.hadoop.hdds.scm.client.ScmClient; +import org.apache.hadoop.hdds.scm.container.ContainerReplicaInfo; +import org.apache.hadoop.ozone.client.OzoneClient; +import org.apache.hadoop.ozone.client.OzoneKeyDetails; +import org.apache.hadoop.ozone.client.OzoneKeyLocation; +import org.apache.hadoop.ozone.shell.OzoneAddress; +import org.apache.hadoop.ozone.shell.keys.KeyUri; +import picocli.CommandLine; + +/** + * Handler of the command that checks whether the replicas of a key are on the + * storage type of the key's storage policy. + * + * <p>A replica matches when both its container storage type and the type of the volume it sits on + * equal the storage type of the policy's creation tier. A replica on the fallback tier does not match, + * and an unknown (null) type does not match. + */ [email protected]( + name = "check", + description = "Check whether the replicas of a key are on the storage type of its storage policy", + mixinStandardHelpOptions = true, + versionProvider = HddsVersionProvider.class) +public class CheckStoragePolicySubCommand extends ScmSubcommand { + + @CommandLine.Mixin + private KeyUri uri; + + @CommandLine.Option( + names = {"--show-replicas"}, + description = "Show the storage types of every replica of every container of the key.") + private boolean showReplicas; + + @Override + public void execute(ScmClient scmClient) throws IOException { + OzoneAddress address = uri.getValue(); + try (OzoneClient client = createOzoneClient(address)) { + OzoneKeyDetails keyDetails = client.getObjectStore() + .getVolume(address.getVolumeName()) + .getBucket(address.getBucketName()) + .getKey(address.getKeyName()); + System.out.println(buildReport(keyDetails, scmClient, showReplicas)); + } + } + + protected OzoneClient createOzoneClient(OzoneAddress address) throws IOException { + return address.createClient(getOzoneConf()); + } + + /** + * Checks the key and builds the text of the report. + * + * @param withReplicas true to add the container and replica details + */ + public String buildReport(OzoneKeyDetails keyDetails, ScmClient scmClient, boolean withReplicas) + throws IOException { + List<String> lines = new ArrayList<>(); + lines.add("Storage Policy for key '" + keyDetails.getVolumeName() + "/" + keyDetails.getBucketName() + "/" + + keyDetails.getName() + "':"); + + OzoneStoragePolicy policy = (OzoneStoragePolicy) keyDetails.getStoragePolicy(); + if (policy == null) { + lines.add(" Storage Policy: none recorded for this key"); + return String.join("\n", lines); + } + + int nodes = keyDetails.getReplicationConfig().getRequiredNodes(); + lines.add(" " + policy.name() + "{Creation Tier: " + describeTier(policy.getCreationTier(), nodes) + + ", Fallback Tier: " + describeTier(policy.getCreationFallbackTier(), nodes) + "}"); + + List<ContainerCheck> containerChecks = evaluate(keyDetails, scmClient); + boolean keyMatches = keyMatches(containerChecks); + lines.add(" Key Match Storage Policy: " + (!keyMatches ? "NO" Review Comment: thanks for the clraificaition! ########## hadoop-ozone/cli-admin/src/main/java/org/apache/hadoop/hdds/scm/cli/storagepolicy/CheckStoragePolicySubCommand.java: ########## @@ -0,0 +1,236 @@ +/* + * 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.hdds.scm.cli.storagepolicy; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import org.apache.hadoop.fs.StorageType; +import org.apache.hadoop.hdds.cli.HddsVersionProvider; +import org.apache.hadoop.hdds.client.OzoneStoragePolicy; +import org.apache.hadoop.hdds.client.StorageTier; +import org.apache.hadoop.hdds.protocol.DatanodeDetails; +import org.apache.hadoop.hdds.scm.cli.ScmSubcommand; +import org.apache.hadoop.hdds.scm.client.ScmClient; +import org.apache.hadoop.hdds.scm.container.ContainerReplicaInfo; +import org.apache.hadoop.ozone.client.OzoneClient; +import org.apache.hadoop.ozone.client.OzoneKeyDetails; +import org.apache.hadoop.ozone.client.OzoneKeyLocation; +import org.apache.hadoop.ozone.shell.OzoneAddress; +import org.apache.hadoop.ozone.shell.keys.KeyUri; +import picocli.CommandLine; + +/** + * Handler of the command that checks whether the replicas of a key are on the + * storage type of the key's storage policy. + * + * <p>A replica matches when both its container storage type and the type of the volume it sits on + * equal the storage type of the policy's creation tier. A replica on the fallback tier does not match, + * and an unknown (null) type does not match. + */ [email protected]( + name = "check", + description = "Check whether the replicas of a key are on the storage type of its storage policy", + mixinStandardHelpOptions = true, + versionProvider = HddsVersionProvider.class) +public class CheckStoragePolicySubCommand extends ScmSubcommand { + + @CommandLine.Mixin + private KeyUri uri; + + @CommandLine.Option( + names = {"--show-replicas"}, + description = "Show the storage types of every replica of every container of the key.") + private boolean showReplicas; + + @Override + public void execute(ScmClient scmClient) throws IOException { + OzoneAddress address = uri.getValue(); + try (OzoneClient client = createOzoneClient(address)) { + OzoneKeyDetails keyDetails = client.getObjectStore() + .getVolume(address.getVolumeName()) + .getBucket(address.getBucketName()) + .getKey(address.getKeyName()); + System.out.println(buildReport(keyDetails, scmClient, showReplicas)); + } + } + + protected OzoneClient createOzoneClient(OzoneAddress address) throws IOException { + return address.createClient(getOzoneConf()); + } + + /** + * Checks the key and builds the text of the report. + * + * @param withReplicas true to add the container and replica details + */ + public String buildReport(OzoneKeyDetails keyDetails, ScmClient scmClient, boolean withReplicas) + throws IOException { + List<String> lines = new ArrayList<>(); + lines.add("Storage Policy for key '" + keyDetails.getVolumeName() + "/" + keyDetails.getBucketName() + "/" + + keyDetails.getName() + "':"); + + OzoneStoragePolicy policy = (OzoneStoragePolicy) keyDetails.getStoragePolicy(); + if (policy == null) { + lines.add(" Storage Policy: none recorded for this key"); + return String.join("\n", lines); + } + + int nodes = keyDetails.getReplicationConfig().getRequiredNodes(); + lines.add(" " + policy.name() + "{Creation Tier: " + describeTier(policy.getCreationTier(), nodes) + + ", Fallback Tier: " + describeTier(policy.getCreationFallbackTier(), nodes) + "}"); + + List<ContainerCheck> containerChecks = evaluate(keyDetails, scmClient); + boolean keyMatches = keyMatches(containerChecks); + lines.add(" Key Match Storage Policy: " + (!keyMatches ? "NO" Review Comment: thanks for the clarification! -- 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]
