ArafatKhan2198 commented on code in PR #12213:
URL: https://github.com/apache/ozone/pull/12213#discussion_r4759111055
##########
hadoop-ozone/cli-shell/src/main/java/org/apache/hadoop/ozone/shell/keys/PutKeyHandler.java:
##########
@@ -69,10 +73,25 @@ public class PutKeyHandler extends KeyHandler {
description = "Store key only if it already exists and its generation
matches the value provided")
private Long expectedGeneration;
+ @Option(names = {"--storage-policy", "-s"},
+ description = "Key StoragePolicy. Allowed values: HOT, WARM, COLD, null.
"
+ + "If not specified, the key inherits the StoragePolicy of the
bucket.")
+ private String storagePolicyStr;
+
+ private static final String NULL_STORAGE_POLICY = "null";
+
@Override
protected void execute(OzoneClient client, OzoneAddress address)
throws IOException {
+ StoragePolicy storagePolicy = parseStoragePolicy(storagePolicyStr);
+ // TODO(HDDS-16679): Streaming writes support StoragePolicy.
+ Preconditions.checkArgument(!(stream && storagePolicy != null),
Review Comment:
Say someone uses --stream --storage-policy HOT while OM is down. The shell
tries connecting to OM before reaching this check, so they get a connection
error and never see that their options are invalid. Can we check these
arguments before creating the client? That would also match the PR
description’s promise to reject them before any RPC.
##########
hadoop-ozone/cli-shell/src/main/java/org/apache/hadoop/ozone/shell/keys/PutKeyHandler.java:
##########
@@ -106,35 +125,59 @@ protected void execute(OzoneClient client, OzoneAddress
address)
replicationConfig, chunkSize);
} else {
async(dataFile, bucket, keyName, keyMetadata,
- replicationConfig, chunkSize);
+ replicationConfig, chunkSize, storagePolicy);
+ }
+ }
+
+ /**
+ * Parse a user-supplied {@code --storage-policy} value into a {@link
StoragePolicy}. Returns {@code null} when the
+ * caller passed "null" (any case) or an empty value, so that the key
inherits the StoragePolicy of the bucket.
+ *
+ * @throws IllegalArgumentException if the value is not one of HOT, WARM,
COLD, or "null".
+ */
+ private static StoragePolicy parseStoragePolicy(String value) {
+ if (Strings.isNullOrEmpty(value) ||
NULL_STORAGE_POLICY.equalsIgnoreCase(value)) {
+ return null;
+ }
+ try {
+ return OzoneStoragePolicy.valueOf(value.toUpperCase());
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException("Invalid storage policy: " + value
+ + ". Allowed String values are: HOT, WARM, COLD, or null.");
}
}
private void async(
File dataFile, OzoneBucket bucket,
String keyName, Map<String, String> keyMetadata,
- ReplicationConfig replicationConfig, int chunkSize)
+ ReplicationConfig replicationConfig, int chunkSize, StoragePolicy
storagePolicy)
throws IOException {
if (isVerbose()) {
out().println("API: async");
}
try (InputStream input = Files.newInputStream(dataFile.toPath());
- OutputStream output = createOrReplaceKey(bucket, keyName,
dataFile.length(), keyMetadata, replicationConfig)) {
+ OutputStream output = createOrReplaceKey(bucket, keyName,
dataFile.length(), keyMetadata, replicationConfig,
+ storagePolicy)) {
IOUtils.copyBytes(input, output, chunkSize);
}
}
private OzoneOutputStream createOrReplaceKey(OzoneBucket bucket, String
keyName,
- long size, Map<String, String> keyMetadata, ReplicationConfig
replicationConfig
- ) throws IOException {
+ long size, Map<String, String> keyMetadata, ReplicationConfig
replicationConfig,
+ StoragePolicy storagePolicy) throws IOException {
if (expectedGeneration != null) {
final long existingGeneration = expectedGeneration;
Preconditions.checkArgument(existingGeneration > 0,
"expectedGeneration must be positive, but was %s",
existingGeneration);
return bucket.rewriteKey(keyName, size, existingGeneration,
replicationConfig, keyMetadata);
}
- return bucket.createKey(keyName, size, replicationConfig, keyMetadata);
+ return bucket.createKey(keyName, CreateKeyOptions.builder()
+ .setSize(size)
+ .setReplicationConfig(replicationConfig)
+ .setMetadata(keyMetadata)
+ .setStoragePolicy(storagePolicy)
Review Comment:
What happens if someone runs key put --storage-policy COLD on a bucket that
uses EC? SCM could pick an ARCHIVE-tier container, but the EC writer doesn’t
pass the storage type to the DataNode.
On a node with both DISK and ARCHIVE volumes, the write could succeed on
DISK while the key records COLD. Can we block this combination until EC
supports it, including when EC comes from the bucket default?
##########
hadoop-ozone/cli-shell/src/test/java/org/apache/hadoop/ozone/shell/keys/TestPutKeyHandler.java:
##########
@@ -0,0 +1,168 @@
+/*
+ * 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.shell.keys;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import com.google.common.collect.ImmutableMap;
+import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import org.apache.hadoop.hdds.client.OzoneStoragePolicy;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.ozone.client.ObjectStore;
+import org.apache.hadoop.ozone.client.OzoneBucket;
+import org.apache.hadoop.ozone.client.OzoneClient;
+import org.apache.hadoop.ozone.client.OzoneVolume;
+import org.apache.hadoop.ozone.client.io.OzoneOutputStream;
+import org.apache.hadoop.ozone.client.protocol.CreateKeyOptions;
+import org.apache.hadoop.ozone.shell.OzoneAddress;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.ValueSource;
+import org.mockito.ArgumentCaptor;
+import picocli.CommandLine;
+
+/**
+ * Unit tests for the {@code --storage-policy} option of PutKeyHandler.
+ */
+public class TestPutKeyHandler {
+
+ private static final byte[] CONTENT = "put key
content".getBytes(StandardCharsets.UTF_8);
+
+ private PutKeyHandler cmd;
+ private OzoneBucket bucket;
+ private OzoneAddress address;
+ private Path dataFile;
+ private ByteArrayOutputStream written;
+
+ @BeforeEach
+ public void setup(@TempDir Path tempDir) throws Exception {
+ // Override getConf() so execute() can be called directly without going
+ // through Handler.call(), which normally sets the OzoneConfiguration.
+ cmd = new PutKeyHandler() {
+ @Override
+ public OzoneConfiguration getConf() {
+ return new OzoneConfiguration();
+ }
+ };
+ address = new OzoneAddress("o3://ozone1/vol/bucket/key");
+ dataFile = tempDir.resolve("data.txt");
+ Files.write(dataFile, CONTENT);
+
+ written = new ByteArrayOutputStream();
+ bucket = mock(OzoneBucket.class);
+ when(bucket.getMetadata()).thenReturn(ImmutableMap.of());
+ when(bucket.createKey(anyString(), any(CreateKeyOptions.class)))
+ .thenReturn(new OzoneOutputStream(written, null));
+ }
+
+ @ParameterizedTest
+ @CsvSource({"HOT,HOT", "WARM,WARM", "COLD,COLD", "hot,HOT", "cold,COLD"})
+ public void testStoragePolicyIsPassedToCreateKey(String argument,
OzoneStoragePolicy expected) throws Exception {
+ parseArgs("--storage-policy", argument);
+
+ cmd.execute(buildClient(), address);
+
+ CreateKeyOptions options = captureCreateKeyOptions();
+ assertThat(options.getStoragePolicy()).isEqualTo(expected);
+ assertThat(options.getSize()).isEqualTo(CONTENT.length);
+ assertThat(written.toByteArray()).isEqualTo(CONTENT);
+ }
+
+ @Test
+ public void testShortOptionName() throws Exception {
+ parseArgs("-s", "COLD");
+
+ cmd.execute(buildClient(), address);
+
+
assertThat(captureCreateKeyOptions().getStoragePolicy()).isEqualTo(OzoneStoragePolicy.COLD);
+ }
+
+ @Test
+ public void testNoStoragePolicyOptionLeavesPolicyUnset() throws Exception {
+ parseArgs();
+
+ cmd.execute(buildClient(), address);
+
+ CreateKeyOptions options = captureCreateKeyOptions();
+ assertThat(options.getStoragePolicy()).isNull();
+ assertThat(options.getSize()).isEqualTo(CONTENT.length);
+ assertThat(written.toByteArray()).isEqualTo(CONTENT);
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"null", "NULL", ""})
+ public void testNullStoragePolicyLeavesPolicyUnset(String argument) throws
Exception {
+ parseArgs("--storage-policy", argument);
+
+ cmd.execute(buildClient(), address);
+
+ assertThat(captureCreateKeyOptions().getStoragePolicy()).isNull();
+ }
+
+ @Test
+ public void testInvalidStoragePolicyIsRejected() throws Exception {
Review Comment:
Can we add tests for a policy combined with `--stream` and
`--expected-generation`? If either check gets removed later, the upload could
succeed through a path that ignores the requested policy.
For example, the user asks for COLD but gets the bucket’s WARM policy
instead. Using a missing input file would also check that the option error
comes before the file-open error.
--
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]