This is an automated email from the ASF dual-hosted git repository.
devmadhuu pushed a commit to branch HDDS-11233
in repository https://gitbox.apache.org/repos/asf/ozone.git
The following commit(s) were added to refs/heads/HDDS-11233 by this push:
new 670378dbe56 HDDS-16039. Introduce S3StorageClass mapping to
OzoneStoragePolicy. (#11229)
670378dbe56 is described below
commit 670378dbe56372d195f2669e852935c53f1d99a7
Author: Devesh Kumar Singh <[email protected]>
AuthorDate: Fri Oct 30 13:23:13 2026 +0530
HDDS-16039. Introduce S3StorageClass mapping to OzoneStoragePolicy. (#11229)
---
.../common/src/main/resources/ozone-default.xml | 42 +++
.../hadoop/ozone/s3/OzoneConfigurationHolder.java | 13 +-
.../hadoop/ozone/s3/S3GatewayConfigKeys.java | 22 ++
.../org/apache/hadoop/ozone/s3/util/S3Consts.java | 7 +
.../hadoop/ozone/s3/util/S3StorageClass.java | 104 ++++++
.../ozone/s3/util/S3StorageClassResolver.java | 212 ++++++++++++
.../hadoop/ozone/s3/util/S3StorageClassSpec.java | 135 ++++++++
.../apache/hadoop/ozone/s3/util/S3StorageType.java | 19 +-
.../org/apache/hadoop/ozone/s3/TestGateway.java | 106 ++++++
.../hadoop/ozone/s3/util/TestS3StorageClass.java | 379 +++++++++++++++++++++
10 files changed, 1026 insertions(+), 13 deletions(-)
diff --git a/hadoop-hdds/common/src/main/resources/ozone-default.xml
b/hadoop-hdds/common/src/main/resources/ozone-default.xml
index c3e0e8f8afe..7aef1caf1ba 100644
--- a/hadoop-hdds/common/src/main/resources/ozone-default.xml
+++ b/hadoop-hdds/common/src/main/resources/ozone-default.xml
@@ -2111,6 +2111,48 @@
</description>
</property>
+ <property>
+ <name>ozone.s3g.storage-class.standard.storage-policy</name>
+ <value></value>
+ <tag>OZONE, S3GATEWAY</tag>
+ <description>
+ Storage policy for writes that request the STANDARD S3 storage class.
+ HOT or WARM. Leave empty to inherit the bucket's storage policy, or the
+ cluster default if the bucket has none. COLD is not allowed, because
+ those writes would then report the GLACIER storage class.
+ Validated when S3 Gateway starts. Invalid values prevent startup.
+ Restart S3 Gateway for changes to take effect.
+ </description>
+ </property>
+
+ <property>
+ <name>ozone.s3g.storage-class.reduced-redundancy.storage-policy</name>
+ <value></value>
+ <tag>OZONE, S3GATEWAY</tag>
+ <description>
+ Storage policy for writes that request the REDUCED_REDUNDANCY S3 storage
+ class. HOT or WARM. Leave empty to inherit the bucket's storage policy,
+ or the cluster default if the bucket has none. COLD is not allowed.
+ Validated when S3 Gateway starts. Invalid values prevent startup.
+ Restart S3 Gateway for changes to take effect.
+ </description>
+ </property>
+
+ <property>
+ <name>ozone.s3g.storage-class.standard-ia.storage-policy</name>
+ <value></value>
+ <tag>OZONE, S3GATEWAY</tag>
+ <description>
+ Storage policy for writes that request the STANDARD_IA S3 storage class.
+ HOT or WARM. Leave empty to inherit the bucket's storage policy, or the
+ cluster default if the bucket has none. COLD is not allowed, because
+ those writes would report the DEEP_ARCHIVE storage class.
+ Validated when S3 Gateway starts. Invalid values prevent startup.
+ Restart S3 Gateway for changes to take effect. Changes apply to new
+ writes; responses for existing objects use their stored policies.
+ </description>
+ </property>
+
<property>
<name>ozone.s3g.secret.http.enabled</name>
<value>false</value>
diff --git
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/OzoneConfigurationHolder.java
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/OzoneConfigurationHolder.java
index 88652f0457a..3e0dd617265 100644
---
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/OzoneConfigurationHolder.java
+++
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/OzoneConfigurationHolder.java
@@ -17,9 +17,9 @@
package org.apache.hadoop.ozone.s3;
-import com.google.common.annotations.VisibleForTesting;
import javax.enterprise.inject.Produces;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.ozone.s3.util.S3StorageClassResolver;
/**
* Ozone Configuration factory.
@@ -30,6 +30,7 @@
public final class OzoneConfigurationHolder {
private static OzoneConfiguration configuration;
+ private static S3StorageClassResolver storageClassResolver;
private OzoneConfigurationHolder() {
}
@@ -39,18 +40,24 @@ public static OzoneConfiguration configuration() {
return configuration;
}
- @VisibleForTesting
+ @Produces
+ public static S3StorageClassResolver storageClassResolver() {
+ return storageClassResolver;
+ }
+
public static void setConfiguration(
OzoneConfiguration conf) {
// Nullity check is used in case the configuration was already set
// in the MiniOzoneCluster
if (configuration == null) {
+ // Validate before publishing the configuration or starting any servers.
+ storageClassResolver = new S3StorageClassResolver(conf);
OzoneConfigurationHolder.configuration = conf;
}
}
- @VisibleForTesting
public static void resetConfiguration() {
configuration = null;
+ storageClassResolver = null;
}
}
diff --git
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/S3GatewayConfigKeys.java
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/S3GatewayConfigKeys.java
index cc674f4b0d6..30ce8544609 100644
---
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/S3GatewayConfigKeys.java
+++
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/S3GatewayConfigKeys.java
@@ -102,6 +102,28 @@ public final class S3GatewayConfigKeys {
public static final String OZONE_S3G_LIST_MAX_KEYS_LIMIT =
"ozone.s3g.list.max.keys.limit";
public static final int OZONE_S3G_LIST_MAX_KEYS_LIMIT_DEFAULT = 1000;
+ /**
+ * Storage policy for writes that request the STANDARD storage class: HOT,
WARM, or empty to
+ * inherit the bucket's policy. COLD is not allowed, because those writes
would then report
+ * GLACIER.
+ */
+ public static final String OZONE_S3G_STORAGE_CLASS_STANDARD_STORAGE_POLICY =
+ "ozone.s3g.storage-class.standard.storage-policy";
+
+ /**
+ * Storage policy for writes that request the REDUCED_REDUNDANCY storage
class: HOT, WARM, or
+ * empty to inherit the bucket's policy. COLD is not allowed.
+ */
+ public static final String
OZONE_S3G_STORAGE_CLASS_REDUCED_REDUNDANCY_STORAGE_POLICY =
+ "ozone.s3g.storage-class.reduced-redundancy.storage-policy";
+
+ /**
+ * Storage policy for writes that request the STANDARD_IA storage class:
HOT, WARM, or empty to
+ * inherit the bucket's policy. COLD is not allowed, because those writes
would report DEEP_ARCHIVE.
+ */
+ public static final String
OZONE_S3G_STORAGE_CLASS_STANDARD_IA_STORAGE_POLICY =
+ "ozone.s3g.storage-class.standard-ia.storage-policy";
+
/**
* Never constructed.
*/
diff --git
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3Consts.java
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3Consts.java
index 0cb61678d68..dd55bc48c71 100644
---
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3Consts.java
+++
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3Consts.java
@@ -148,6 +148,13 @@ public final class S3Consts {
// Maximum number of parts returned in one GetObjectAttributes response.
public static final int GET_OBJECT_ATTRIBUTES_MAX_PARTS_LIMIT = 1000;
+ // S3 storage class values passed via the `x-amz-storage-class` request
header.
+ public static final String S3_STORAGE_CLASS_REDUCED_REDUNDANCY =
"REDUCED_REDUNDANCY";
+ public static final String S3_STORAGE_CLASS_STANDARD = "STANDARD";
+ public static final String S3_STORAGE_CLASS_STANDARD_IA = "STANDARD_IA";
+ public static final String S3_STORAGE_CLASS_GLACIER = "GLACIER";
+ public static final String S3_STORAGE_CLASS_DEEP_ARCHIVE = "DEEP_ARCHIVE";
+
//Never Constructed
private S3Consts() {
diff --git
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3StorageClass.java
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3StorageClass.java
new file mode 100644
index 00000000000..0329385f021
--- /dev/null
+++
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3StorageClass.java
@@ -0,0 +1,104 @@
+/*
+ * 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.s3.util;
+
+import static org.apache.hadoop.hdds.client.OzoneStoragePolicy.COLD;
+import static
org.apache.hadoop.ozone.s3.util.S3Consts.S3_STORAGE_CLASS_DEEP_ARCHIVE;
+import static
org.apache.hadoop.ozone.s3.util.S3Consts.S3_STORAGE_CLASS_GLACIER;
+import static
org.apache.hadoop.ozone.s3.util.S3Consts.S3_STORAGE_CLASS_REDUCED_REDUNDANCY;
+import static
org.apache.hadoop.ozone.s3.util.S3Consts.S3_STORAGE_CLASS_STANDARD;
+import static
org.apache.hadoop.ozone.s3.util.S3Consts.S3_STORAGE_CLASS_STANDARD_IA;
+
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationFactor;
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationType;
+
+/**
+ * The S3 storage class names Ozone supports, each paired with its default
+ * {@link S3StorageClassSpec}.
+ *
+ * <p>This enum is the vocabulary only. The rules for mapping a class to Ozone
settings and back are
+ * in {@link S3StorageClassResolver}.
+ *
+ * <p>{@code STANDARD}, {@code REDUCED_REDUNDANCY}, and {@code STANDARD_IA}
define replication only.
+ * Their storage policy is selected through a gateway setting and is unset by
default, leaving the
+ * write path to use the bucket's policy or the cluster default.
+ * {@code GLACIER} and {@code DEEP_ARCHIVE} define both replication and a COLD
storage policy.
+ */
+public enum S3StorageClass {
+
+ REDUCED_REDUNDANCY(S3_STORAGE_CLASS_REDUCED_REDUNDANCY,
S3StorageClassSpec.newBuilder()
+ .setReplicationType(ReplicationType.RATIS)
+ .setReplicationFactor(ReplicationFactor.ONE)
+ .build()),
+ STANDARD(S3_STORAGE_CLASS_STANDARD, S3StorageClassSpec.newBuilder()
+ .setReplicationType(ReplicationType.RATIS)
+ .setReplicationFactor(ReplicationFactor.THREE)
+ .build()),
+ STANDARD_IA(S3_STORAGE_CLASS_STANDARD_IA, S3StorageClassSpec.newBuilder()
+ .setReplicationType(ReplicationType.EC)
+ .build()),
+ GLACIER(S3_STORAGE_CLASS_GLACIER, S3StorageClassSpec.newBuilder()
+ .setReplicationType(ReplicationType.RATIS)
+ .setReplicationFactor(ReplicationFactor.THREE)
+ .setStoragePolicy(COLD)
+ .build()),
+ DEEP_ARCHIVE(S3_STORAGE_CLASS_DEEP_ARCHIVE, S3StorageClassSpec.newBuilder()
+ .setReplicationType(ReplicationType.EC)
+ .setStoragePolicy(COLD)
+ .build());
+
+ private final String s3StorageClass;
+ private final S3StorageClassSpec spec;
+
+ S3StorageClass(String s3StorageClass, S3StorageClassSpec spec) {
+ this.s3StorageClass = s3StorageClass;
+ this.spec = spec;
+ }
+
+ public String getS3StorageClass() {
+ return s3StorageClass;
+ }
+
+ /**
+ * The class's default spec, before any gateway setting is applied. Use
+ * {@link S3StorageClassResolver#toSpec} to get the spec for a write.
+ */
+ public S3StorageClassSpec getSpec() {
+ return spec;
+ }
+
+ /**
+ * Look up the S3StorageClass matching an S3 storage-class header value.
+ *
+ * @param s3StorageClass the S3 storage class name (case-sensitive, with
surrounding whitespace ignored).
+ * @return the matching S3StorageClass.
+ * @throws IllegalArgumentException if the value is null or does not map to
+ * a supported storage class.
+ */
+ public static S3StorageClass fromS3StorageClass(String s3StorageClass) {
+ if (s3StorageClass == null) {
+ throw new IllegalArgumentException("Not supported s3StorageClass: null");
+ }
+ try {
+ return S3StorageClass.valueOf(s3StorageClass.trim());
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException(
+ "Not supported s3StorageClass: " + s3StorageClass);
+ }
+ }
+}
diff --git
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3StorageClassResolver.java
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3StorageClassResolver.java
new file mode 100644
index 00000000000..97be25b6d8f
--- /dev/null
+++
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3StorageClassResolver.java
@@ -0,0 +1,212 @@
+/*
+ * 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.s3.util;
+
+import static
org.apache.hadoop.ozone.s3.S3GatewayConfigKeys.OZONE_S3G_STORAGE_CLASS_REDUCED_REDUNDANCY_STORAGE_POLICY;
+import static
org.apache.hadoop.ozone.s3.S3GatewayConfigKeys.OZONE_S3G_STORAGE_CLASS_STANDARD_IA_STORAGE_POLICY;
+import static
org.apache.hadoop.ozone.s3.S3GatewayConfigKeys.OZONE_S3G_STORAGE_CLASS_STANDARD_STORAGE_POLICY;
+
+import jakarta.annotation.Nonnull;
+import jakarta.annotation.Nullable;
+import java.util.Locale;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.client.ECReplicationConfig;
+import org.apache.hadoop.hdds.client.OzoneStoragePolicy;
+import org.apache.hadoop.hdds.client.RatisReplicationConfig;
+import org.apache.hadoop.hdds.client.ReplicationConfig;
+import org.apache.hadoop.hdds.client.StoragePolicy;
+import org.apache.hadoop.hdds.conf.ConfigurationSource;
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationType;
+
+/**
+ * Maps between S3 storage class names and Ozone replication and storage
policy.
+ *
+ * <p>The two directions are deliberately not symmetric:
+ * <ul>
+ * <li><b>Forward</b> (request header to Ozone) is strict. An unknown name
is the client's mistake
+ * and is rejected.</li>
+ * <li><b>Reverse</b> (stored key to response header) never fails. It
describes data that already
+ * exists, so every combination of replication and storage policy maps to
some class.
+ * Replication decides the class, and only a COLD storage policy changes
it.</li>
+ * </ul>
+ */
+public final class S3StorageClassResolver {
+
+ /** EC layout used when neither the request nor the bucket specifies one. */
+ private static final ECReplicationConfig DEFAULT_EC_REPLICATION_CONFIG =
+ new ECReplicationConfig(3, 2);
+
+ private final S3StorageClassSpec standardSpec;
+ private final S3StorageClassSpec reducedRedundancySpec;
+ private final S3StorageClassSpec standardIaSpec;
+
+ /**
+ * Resolves gateway storage policy settings once, before the gateway accepts
requests.
+ *
+ * @param conf gateway configuration
+ * @throws IllegalArgumentException if any configured storage policy is not
valid
+ */
+ public S3StorageClassResolver(ConfigurationSource conf) {
+ standardSpec = S3StorageClass.STANDARD.getSpec().withStoragePolicy(
+ getConfiguredStoragePolicy(conf,
OZONE_S3G_STORAGE_CLASS_STANDARD_STORAGE_POLICY));
+ reducedRedundancySpec =
S3StorageClass.REDUCED_REDUNDANCY.getSpec().withStoragePolicy(
+ getConfiguredStoragePolicy(conf,
OZONE_S3G_STORAGE_CLASS_REDUCED_REDUNDANCY_STORAGE_POLICY));
+ standardIaSpec = S3StorageClass.STANDARD_IA.getSpec().withStoragePolicy(
+ getConfiguredStoragePolicy(conf,
OZONE_S3G_STORAGE_CLASS_STANDARD_IA_STORAGE_POLICY));
+ }
+
+ /**
+ * Forward mapping: the spec for a write that requests the given storage
class.
+ *
+ * <p>For {@code STANDARD}, {@code REDUCED_REDUNDANCY}, and {@code
STANDARD_IA}, the storage policy comes from the
+ * class's gateway setting resolved at startup. When the setting is empty,
the spec has no storage policy and the
+ * write path uses the bucket's policy or the cluster default. Configuration
is not read during request lookup.
+ *
+ * @param s3StorageClass the {@code x-amz-storage-class} header value
+ * @throws IllegalArgumentException if the storage class is null or unknown
+ */
+ @Nonnull
+ public S3StorageClassSpec toSpec(String s3StorageClass) {
+ S3StorageClass storageClass =
S3StorageClass.fromS3StorageClass(s3StorageClass);
+ switch (storageClass) {
+ case STANDARD:
+ return standardSpec;
+ case REDUCED_REDUNDANCY:
+ return reducedRedundancySpec;
+ case STANDARD_IA:
+ return standardIaSpec;
+ default:
+ return storageClass.getSpec();
+ }
+ }
+
+ /**
+ * The replication config for a write with the given spec.
+ *
+ * <p>The spec must supply a supported replication type (RATIS or EC). A
missing type is rejected even when a bucket
+ * replication config is supplied.
+ *
+ * <p>For EC, the layout comes from the {@code storage-config} request
header, then from the
+ * bucket's replication if it is EC, and otherwise defaults to EC 3-2.
+ *
+ * @param spec the spec from {@link #toSpec}
+ * @param storageConfigHeader the {@code storage-config} header value, or
null
+ * @param bucketReplicationConfig the bucket's default replication, used
only for an EC spec, or null
+ * @throws IllegalArgumentException if the spec has no supported replication
type, or
+ * {@code storageConfigHeader} is not a valid EC layout
+ */
+ @Nonnull
+ public static ReplicationConfig toReplicationConfig(S3StorageClassSpec spec,
+ @Nullable String storageConfigHeader, @Nullable ReplicationConfig
bucketReplicationConfig) {
+ ReplicationType type = spec.getReplicationType();
+ if (type == ReplicationType.EC) {
+ if (StringUtils.isNotEmpty(storageConfigHeader)) {
+ return new ECReplicationConfig(storageConfigHeader);
+ }
+ if (bucketReplicationConfig instanceof ECReplicationConfig) {
+ return bucketReplicationConfig;
+ }
+ return DEFAULT_EC_REPLICATION_CONFIG;
+ }
+ if (type == ReplicationType.RATIS) {
+ return RatisReplicationConfig.getInstance(spec.getReplicationFactor());
+ }
+ throw new IllegalArgumentException("Storage class spec has no supported
replication type: "
+ + spec);
+ }
+
+ /**
+ * Reverse mapping: the storage class to report for a stored key.
+ *
+ * <p>This never fails. Replication decides the class, and only a COLD
storage policy changes it:
+ * <pre>
+ * COLD + RATIS -> GLACIER
+ * COLD + EC -> DEEP_ARCHIVE
+ * otherwise, by replication:
+ * EC -> STANDARD_IA
+ * STAND_ALONE, or a single replica -> REDUCED_REDUNDANCY
+ * anything else -> STANDARD
+ * </pre>
+ * A null storage policy, as on keys written before storage policy support,
uses replication
+ * alone. A null replication reports {@code STANDARD}.
+ * The policy is taken from the stored key, not the current gateway
configuration. HOT and WARM
+ * share the same class for a given replication configuration; COLD has a
distinct archive class
+ * for RATIS and EC. Changes between EC layouts, such as 3-2 and 6-3, do not
change the class.
+ *
+ * @param replicationConfig the key's replication, or null
+ * @param storagePolicy the key's storage policy, or null
+ */
+ @Nonnull
+ public static S3StorageClass toS3StorageClass(@Nullable ReplicationConfig
replicationConfig,
+ @Nullable StoragePolicy storagePolicy) {
+ if (replicationConfig == null) {
+ return S3StorageClass.STANDARD;
+ }
+ if (storagePolicy == OzoneStoragePolicy.COLD) {
+ if (replicationConfig.getReplicationType() == ReplicationType.RATIS) {
+ return S3StorageClass.GLACIER;
+ }
+ if (replicationConfig.getReplicationType() == ReplicationType.EC) {
+ return S3StorageClass.DEEP_ARCHIVE;
+ }
+ }
+ return fromReplicationConfig(replicationConfig);
+ }
+
+ /**
+ * The storage class implied by replication alone. This is the mapping used
before storage policy
+ * support, kept unchanged so keys without a storage policy report the same
class as before.
+ */
+ @Nonnull
+ static S3StorageClass fromReplicationConfig(@Nonnull ReplicationConfig
replicationConfig) {
+ ReplicationType type = replicationConfig.getReplicationType();
+ if (type == ReplicationType.EC) {
+ return S3StorageClass.STANDARD_IA;
+ }
+ if (type == ReplicationType.STAND_ALONE ||
replicationConfig.getRequiredNodes() == 1) {
+ return S3StorageClass.REDUCED_REDUNDANCY;
+ }
+ return S3StorageClass.STANDARD;
+ }
+
+ /**
+ * Reads a per-class storage policy setting. Empty means no policy.
+ *
+ * @throws IllegalArgumentException if the value is not HOT or WARM
+ */
+ @Nullable
+ private static StoragePolicy getConfiguredStoragePolicy(ConfigurationSource
conf, String key) {
+ String value = conf.getTrimmed(key, "");
+ if (value.isEmpty()) {
+ return null;
+ }
+ OzoneStoragePolicy policy;
+ try {
+ policy = OzoneStoragePolicy.valueOf(value.toUpperCase(Locale.ROOT));
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException("Invalid storage policy '" + value +
"' for " + key
+ + ". Expected HOT, WARM or empty.", e);
+ }
+ if (policy == OzoneStoragePolicy.COLD) {
+ // COLD would make these writes report an archive class instead of the
class they asked for.
+ throw new IllegalArgumentException("Storage policy COLD is not allowed
for " + key
+ + ". Expected HOT, WARM or empty.");
+ }
+ return policy;
+ }
+}
diff --git
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3StorageClassSpec.java
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3StorageClassSpec.java
new file mode 100644
index 00000000000..507af6d364e
--- /dev/null
+++
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3StorageClassSpec.java
@@ -0,0 +1,135 @@
+/*
+ * 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.s3.util;
+
+import jakarta.annotation.Nullable;
+import java.util.Objects;
+import org.apache.hadoop.hdds.client.StoragePolicy;
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationFactor;
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationType;
+
+/**
+ * What an S3 storage class means in Ozone.
+ *
+ * <p>Fields may be unset in this value object. Each consumer defines how
unset fields are handled. In particular,
+ * {@link S3StorageClassResolver} requires an explicit replication type when
converting a spec to a replication config.
+ * An unset storage policy leaves policy resolution to the write path, for
example from the bucket.
+ *
+ * <p>The spec holds a replication type rather than a full replication config.
For EC, the layout is
+ * resolved at write time by {@link S3StorageClassResolver}, so it is not
fixed by the class.
+ */
+public final class S3StorageClassSpec {
+
+ private final ReplicationType replicationType;
+ private final ReplicationFactor replicationFactor;
+ private final StoragePolicy storagePolicy;
+
+ private S3StorageClassSpec(Builder b) {
+ this.replicationType = b.replicationType;
+ this.replicationFactor = b.replicationFactor;
+ this.storagePolicy = b.storagePolicy;
+ }
+
+ public static Builder newBuilder() {
+ return new Builder();
+ }
+
+ /** Replication type, or null if the class does not set one. */
+ @Nullable
+ public ReplicationType getReplicationType() {
+ return replicationType;
+ }
+
+ /** Replication factor for RATIS, or null if not set or not applicable. */
+ @Nullable
+ public ReplicationFactor getReplicationFactor() {
+ return replicationFactor;
+ }
+
+ /** Storage policy, or null to inherit it from the bucket or the cluster
default. */
+ @Nullable
+ public StoragePolicy getStoragePolicy() {
+ return storagePolicy;
+ }
+
+ /** Returns a copy of this spec with the given storage policy. */
+ S3StorageClassSpec withStoragePolicy(@Nullable StoragePolicy policy) {
+ return newBuilder()
+ .setReplicationType(replicationType)
+ .setReplicationFactor(replicationFactor)
+ .setStoragePolicy(policy)
+ .build();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (!(o instanceof S3StorageClassSpec)) {
+ return false;
+ }
+ S3StorageClassSpec that = (S3StorageClassSpec) o;
+ return replicationType == that.replicationType
+ && replicationFactor == that.replicationFactor
+ && Objects.equals(storagePolicy, that.storagePolicy);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(replicationType, replicationFactor, storagePolicy);
+ }
+
+ @Override
+ public String toString() {
+ return "S3StorageClassSpec{replicationType=" + replicationType
+ + ", replicationFactor=" + replicationFactor
+ + ", storagePolicy=" + storagePolicy + '}';
+ }
+
+ /**
+ * Builder for {@link S3StorageClassSpec}. Fields left unset stay null.
+ */
+ public static final class Builder {
+ private ReplicationType replicationType;
+ private ReplicationFactor replicationFactor;
+ private StoragePolicy storagePolicy;
+
+ private Builder() {
+ }
+
+ public Builder setReplicationType(@Nullable ReplicationType type) {
+ this.replicationType = type;
+ return this;
+ }
+
+ public Builder setReplicationFactor(@Nullable ReplicationFactor factor) {
+ this.replicationFactor = factor;
+ return this;
+ }
+
+ public Builder setStoragePolicy(@Nullable StoragePolicy policy) {
+ this.storagePolicy = policy;
+ return this;
+ }
+
+ public S3StorageClassSpec build() {
+ return new S3StorageClassSpec(this);
+ }
+ }
+}
diff --git
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3StorageType.java
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3StorageType.java
index c400bade417..4a1b689f9b7 100644
---
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3StorageType.java
+++
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/util/S3StorageType.java
@@ -24,8 +24,11 @@
/**
* Maps S3 storage class values to Ozone replication values.
+ *
+ * @deprecated Use {@link S3StorageClass} and {@link S3StorageClassResolver},
which also cover
+ * storage policy. Kept until the S3 Gateway call sites move to them.
*/
-
+@Deprecated
public enum S3StorageType {
REDUCED_REDUNDANCY(RatisReplicationConfig.getInstance(HddsProtos.ReplicationFactor.ONE)),
@@ -43,15 +46,11 @@ public ReplicationConfig getReplicationConfig() {
return replicationConfig;
}
+ /**
+ * The storage class implied by replication alone. Delegates to {@link
S3StorageClassResolver}
+ * so the two mappings cannot drift apart.
+ */
public static S3StorageType fromReplicationConfig(ReplicationConfig config) {
- if (config.getReplicationType() == HddsProtos.ReplicationType.EC) {
- return STANDARD_IA;
- }
- if (config.getReplicationType() == HddsProtos.ReplicationType.STAND_ALONE
||
- config.getRequiredNodes() == 1) {
- return S3StorageType.REDUCED_REDUNDANCY;
- } else {
- return S3StorageType.STANDARD;
- }
+ return
valueOf(S3StorageClassResolver.fromReplicationConfig(config).name());
}
}
diff --git
a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/TestGateway.java
b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/TestGateway.java
index e57b9816a0e..a8b6af4cc05 100644
---
a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/TestGateway.java
+++
b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/TestGateway.java
@@ -17,12 +17,31 @@
package org.apache.hadoop.ozone.s3;
+import static org.apache.hadoop.hdds.cli.GenericCli.EXECUTION_ERROR_EXIT_CODE;
+import static
org.apache.hadoop.ozone.s3.S3GatewayConfigKeys.OZONE_S3G_STORAGE_CLASS_REDUCED_REDUNDANCY_STORAGE_POLICY;
+import static
org.apache.hadoop.ozone.s3.S3GatewayConfigKeys.OZONE_S3G_STORAGE_CLASS_STANDARD_IA_STORAGE_POLICY;
+import static
org.apache.hadoop.ozone.s3.S3GatewayConfigKeys.OZONE_S3G_STORAGE_CLASS_STANDARD_STORAGE_POLICY;
+import static
org.apache.hadoop.ozone.s3.util.S3Consts.S3_STORAGE_CLASS_REDUCED_REDUNDANCY;
+import static
org.apache.hadoop.ozone.s3.util.S3Consts.S3_STORAGE_CLASS_STANDARD;
+import static
org.apache.hadoop.ozone.s3.util.S3Consts.S3_STORAGE_CLASS_STANDARD_IA;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.fail;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.util.Arrays;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.LogManager;
import java.util.logging.Logger;
+import org.apache.hadoop.hdds.client.OzoneStoragePolicy;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.ozone.s3.util.S3StorageClassResolver;
import org.apache.ozone.test.GenericTestUtils.LogCapturer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
@@ -39,12 +58,14 @@ public class TestGateway {
@BeforeEach
void saveRootLoggerHandlers() {
+ OzoneConfigurationHolder.resetConfiguration();
originalHandlers = rootLogger.getHandlers();
removeRootLoggerHandlers();
}
@AfterEach
void restoreRootLoggerHandlers() {
+ OzoneConfigurationHolder.resetConfiguration();
removeRootLoggerHandlers();
for (Handler handler : originalHandlers) {
rootLogger.addHandler(handler);
@@ -73,6 +94,91 @@ void redirectsJulLogsToSlf4j() {
}
}
+ @Test
+ void rejectsInvalidStoragePoliciesBeforeStartingServers() {
+ for (String key :
Arrays.asList(OZONE_S3G_STORAGE_CLASS_STANDARD_STORAGE_POLICY,
+ OZONE_S3G_STORAGE_CLASS_REDUCED_REDUNDANCY_STORAGE_POLICY,
+ OZONE_S3G_STORAGE_CLASS_STANDARD_IA_STORAGE_POLICY)) {
+ for (String value : Arrays.asList("COLD", "LUKEWARM")) {
+ Gateway gateway = new Gateway() {
+ @Override
+ public void start() {
+ fail("Invalid storage policy must prevent server startup");
+ }
+ };
+ gateway.getOzoneConf().set(key, value);
+ StringWriter errors = new StringWriter();
+ gateway.getCmd().setErr(new PrintWriter(errors));
+
+ assertEquals(EXECUTION_ERROR_EXIT_CODE, gateway.execute(new
String[0]));
+ assertThat(errors.toString()).contains(key, value, "Expected HOT, WARM
or empty.");
+ assertNull(OzoneConfigurationHolder.configuration());
+ assertNull(OzoneConfigurationHolder.storageClassResolver());
+ }
+ }
+ }
+
+ @Test
+ void keepsResolverForTheInitialConfiguration() {
+ OzoneConfiguration conf = new OzoneConfiguration();
+ conf.set(OZONE_S3G_STORAGE_CLASS_STANDARD_STORAGE_POLICY, "HOT");
+ conf.set(OZONE_S3G_STORAGE_CLASS_REDUCED_REDUNDANCY_STORAGE_POLICY,
"WARM");
+ conf.set(OZONE_S3G_STORAGE_CLASS_STANDARD_IA_STORAGE_POLICY, "HOT");
+ OzoneConfigurationHolder.setConfiguration(conf);
+ S3StorageClassResolver resolver =
OzoneConfigurationHolder.storageClassResolver();
+
+ OzoneConfiguration ignored = new OzoneConfiguration();
+ ignored.set(OZONE_S3G_STORAGE_CLASS_STANDARD_STORAGE_POLICY, "COLD");
+ OzoneConfigurationHolder.setConfiguration(ignored);
+
+ assertSame(conf, OzoneConfigurationHolder.configuration());
+ assertSame(resolver, OzoneConfigurationHolder.storageClassResolver());
+ assertEquals(OzoneStoragePolicy.HOT,
resolver.toSpec(S3_STORAGE_CLASS_STANDARD).getStoragePolicy());
+ assertEquals(OzoneStoragePolicy.WARM,
resolver.toSpec(S3_STORAGE_CLASS_REDUCED_REDUNDANCY).getStoragePolicy());
+ assertEquals(OzoneStoragePolicy.HOT,
resolver.toSpec(S3_STORAGE_CLASS_STANDARD_IA).getStoragePolicy());
+ }
+
+ @Test
+ void resetsResolverWithConfiguration() {
+ OzoneConfiguration conf = new OzoneConfiguration();
+ conf.set(OZONE_S3G_STORAGE_CLASS_STANDARD_STORAGE_POLICY, "HOT");
+ conf.set(OZONE_S3G_STORAGE_CLASS_STANDARD_IA_STORAGE_POLICY, "HOT");
+ OzoneConfigurationHolder.setConfiguration(conf);
+ S3StorageClassResolver previous =
OzoneConfigurationHolder.storageClassResolver();
+
+ OzoneConfigurationHolder.resetConfiguration();
+ assertNull(OzoneConfigurationHolder.configuration());
+ assertNull(OzoneConfigurationHolder.storageClassResolver());
+
+ OzoneConfiguration next = new OzoneConfiguration();
+ next.set(OZONE_S3G_STORAGE_CLASS_STANDARD_STORAGE_POLICY, "WARM");
+ next.set(OZONE_S3G_STORAGE_CLASS_STANDARD_IA_STORAGE_POLICY, "WARM");
+ OzoneConfigurationHolder.setConfiguration(next);
+ S3StorageClassResolver resolver =
OzoneConfigurationHolder.storageClassResolver();
+ assertSame(next, OzoneConfigurationHolder.configuration());
+ assertNotSame(previous, resolver);
+ assertEquals(OzoneStoragePolicy.WARM,
resolver.toSpec(S3_STORAGE_CLASS_STANDARD).getStoragePolicy());
+ assertEquals(OzoneStoragePolicy.HOT,
previous.toSpec(S3_STORAGE_CLASS_STANDARD_IA).getStoragePolicy());
+ assertEquals(OzoneStoragePolicy.WARM,
resolver.toSpec(S3_STORAGE_CLASS_STANDARD_IA).getStoragePolicy());
+ }
+
+ @Test
+ void canInitializeAfterCorrectingInvalidConfiguration() {
+ OzoneConfiguration conf = new OzoneConfiguration();
+ conf.set(OZONE_S3G_STORAGE_CLASS_STANDARD_STORAGE_POLICY, "HOT");
+ conf.set(OZONE_S3G_STORAGE_CLASS_REDUCED_REDUNDANCY_STORAGE_POLICY,
"COLD");
+ assertThrows(IllegalArgumentException.class, () ->
OzoneConfigurationHolder.setConfiguration(conf));
+ assertNull(OzoneConfigurationHolder.configuration());
+ assertNull(OzoneConfigurationHolder.storageClassResolver());
+
+ conf.set(OZONE_S3G_STORAGE_CLASS_REDUCED_REDUNDANCY_STORAGE_POLICY,
"WARM");
+ OzoneConfigurationHolder.setConfiguration(conf);
+ S3StorageClassResolver resolver =
OzoneConfigurationHolder.storageClassResolver();
+ assertSame(conf, OzoneConfigurationHolder.configuration());
+ assertEquals(OzoneStoragePolicy.HOT,
resolver.toSpec(S3_STORAGE_CLASS_STANDARD).getStoragePolicy());
+ assertEquals(OzoneStoragePolicy.WARM,
resolver.toSpec(S3_STORAGE_CLASS_REDUCED_REDUNDANCY).getStoragePolicy());
+ }
+
private void removeRootLoggerHandlers() {
for (Handler handler : rootLogger.getHandlers()) {
rootLogger.removeHandler(handler);
diff --git
a/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/util/TestS3StorageClass.java
b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/util/TestS3StorageClass.java
new file mode 100644
index 00000000000..42c4cff727a
--- /dev/null
+++
b/hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/util/TestS3StorageClass.java
@@ -0,0 +1,379 @@
+/*
+ * 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.s3.util;
+
+import static
org.apache.hadoop.ozone.s3.S3GatewayConfigKeys.OZONE_S3G_STORAGE_CLASS_REDUCED_REDUNDANCY_STORAGE_POLICY;
+import static
org.apache.hadoop.ozone.s3.S3GatewayConfigKeys.OZONE_S3G_STORAGE_CLASS_STANDARD_IA_STORAGE_POLICY;
+import static
org.apache.hadoop.ozone.s3.S3GatewayConfigKeys.OZONE_S3G_STORAGE_CLASS_STANDARD_STORAGE_POLICY;
+import static
org.apache.hadoop.ozone.s3.util.S3Consts.S3_STORAGE_CLASS_DEEP_ARCHIVE;
+import static
org.apache.hadoop.ozone.s3.util.S3Consts.S3_STORAGE_CLASS_GLACIER;
+import static
org.apache.hadoop.ozone.s3.util.S3Consts.S3_STORAGE_CLASS_REDUCED_REDUNDANCY;
+import static
org.apache.hadoop.ozone.s3.util.S3Consts.S3_STORAGE_CLASS_STANDARD;
+import static
org.apache.hadoop.ozone.s3.util.S3Consts.S3_STORAGE_CLASS_STANDARD_IA;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.Arrays;
+import java.util.List;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hdds.client.ECReplicationConfig;
+import org.apache.hadoop.hdds.client.OzoneStoragePolicy;
+import org.apache.hadoop.hdds.client.RatisReplicationConfig;
+import org.apache.hadoop.hdds.client.ReplicationConfig;
+import org.apache.hadoop.hdds.client.StandaloneReplicationConfig;
+import org.apache.hadoop.hdds.client.StoragePolicy;
+import org.apache.hadoop.hdds.conf.OzoneConfiguration;
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationFactor;
+import org.apache.hadoop.hdds.protocol.proto.HddsProtos.ReplicationType;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
+
+/** Tests the S3 storage class mapping to Ozone replication and storage
policy. */
+class TestS3StorageClass {
+
+ private static final ReplicationConfig RATIS_ONE =
+ RatisReplicationConfig.getInstance(ReplicationFactor.ONE);
+ private static final ReplicationConfig RATIS_THREE =
+ RatisReplicationConfig.getInstance(ReplicationFactor.THREE);
+ private static final ReplicationConfig STANDALONE_ONE =
+ StandaloneReplicationConfig.getInstance(ReplicationFactor.ONE);
+ // STANDALONE with three replicas still reports REDUCED_REDUNDANCY, which
only the replication
+ // type check catches; the single-replica check does not.
+ private static final ReplicationConfig STANDALONE_THREE =
+ StandaloneReplicationConfig.getInstance(ReplicationFactor.THREE);
+ private static final ReplicationConfig EC_3_2 = new ECReplicationConfig(3,
2);
+ private static final ReplicationConfig EC_6_3 = new ECReplicationConfig(6,
3);
+
+ private static final List<ReplicationConfig> REPLICATIONS = Arrays.asList(
+ RATIS_ONE, RATIS_THREE, STANDALONE_ONE, STANDALONE_THREE, EC_3_2,
+ new ECReplicationConfig(4, 2), EC_6_3,
+ new ECReplicationConfig(9, 3));
+
+ /** Every storage policy, plus null for keys written before storage policy
support. */
+ private static final List<StoragePolicy> POLICIES = Arrays.asList(
+ null, OzoneStoragePolicy.HOT, OzoneStoragePolicy.WARM,
OzoneStoragePolicy.COLD);
+
+ @Test
+ void testFromS3StorageClass() {
+ assertEquals(S3StorageClass.REDUCED_REDUNDANCY,
+
S3StorageClass.fromS3StorageClass(S3_STORAGE_CLASS_REDUCED_REDUNDANCY));
+ assertEquals(S3StorageClass.STANDARD,
+ S3StorageClass.fromS3StorageClass(S3_STORAGE_CLASS_STANDARD));
+ assertEquals(S3StorageClass.STANDARD_IA,
+ S3StorageClass.fromS3StorageClass(S3_STORAGE_CLASS_STANDARD_IA));
+ assertEquals(S3StorageClass.GLACIER,
+ S3StorageClass.fromS3StorageClass(S3_STORAGE_CLASS_GLACIER));
+ assertEquals(S3StorageClass.DEEP_ARCHIVE,
S3StorageClass.fromS3StorageClass("DEEP_ARCHIVE"));
+ assertEquals("DEEP_ARCHIVE",
S3StorageClass.DEEP_ARCHIVE.getS3StorageClass());
+ assertThrows(IllegalArgumentException.class,
+ () -> S3StorageClass.fromS3StorageClass("standard"));
+ assertThrows(IllegalArgumentException.class,
+ () -> S3StorageClass.fromS3StorageClass("INVALID"));
+ assertThrows(IllegalArgumentException.class,
+ () -> S3StorageClass.fromS3StorageClass(null));
+ }
+
+ @Test
+ void testFromS3StorageClassTrimsWhitespace() {
+ for (S3StorageClass storageClass : S3StorageClass.values()) {
+ String value = storageClass.getS3StorageClass();
+ for (String input : Arrays.asList(" " + value, value + " ", "\t " +
value + " \t")) {
+ assertEquals(storageClass, S3StorageClass.fromS3StorageClass(input));
+ }
+ }
+ }
+
+ @Test
+ void testFromS3StorageClassRejectsCaseMismatchAndBlankValues() {
+ for (String input : Arrays.asList("Standard", "standard_ia", "glacier", "
standard ", "", " \t ", "STANDARD IA",
+ "deep_archive", "Deep_Archive", "GLACIER_DEEP_ARCHIVE")) {
+ assertThrows(IllegalArgumentException.class, () ->
S3StorageClass.fromS3StorageClass(input));
+ }
+ }
+
+ /**
+ * Keys with no storage policy must report exactly what they reported before
storage policy
+ * support, so existing objects keep their storage class. Compares against
the original
+ * replication-only logic, copied here so a change to the production code
cannot change both.
+ */
+ @Test
+ void testReverseWithoutPolicyMatchesOriginalMapping() {
+ for (ReplicationConfig replication : REPLICATIONS) {
+ assertEquals(originalMapping(replication),
+ S3StorageClassResolver.toS3StorageClass(replication, null).name(),
+ "replication " + replication);
+ assertEquals(originalMapping(replication),
+ S3StorageType.fromReplicationConfig(replication).name(),
+ "deprecated S3StorageType for " + replication);
+ }
+ }
+
+ /** The reverse mapping describes stored data, so it must give an answer for
every combination. */
+ @Test
+ void testReverseNeverFails() {
+ for (ReplicationConfig replication : REPLICATIONS) {
+ for (StoragePolicy policy : POLICIES) {
+ assertNotNull(S3StorageClassResolver.toS3StorageClass(replication,
policy),
+ replication + " + " + policy);
+ }
+ }
+ for (StoragePolicy policy : POLICIES) {
+ assertEquals(S3StorageClass.STANDARD,
+ S3StorageClassResolver.toS3StorageClass(null, policy));
+ }
+ }
+
+ /** Only COLD changes the class. HOT and WARM leave replication to decide. */
+ @Test
+ void testReverseOnlyColdChangesTheClass() {
+ for (StoragePolicy policy : Arrays.asList(OzoneStoragePolicy.HOT,
OzoneStoragePolicy.WARM)) {
+ assertEquals(S3StorageClass.STANDARD,
+ S3StorageClassResolver.toS3StorageClass(RATIS_THREE, policy));
+ assertEquals(S3StorageClass.REDUCED_REDUNDANCY,
+ S3StorageClassResolver.toS3StorageClass(RATIS_ONE, policy));
+ assertEquals(S3StorageClass.STANDARD_IA,
+ S3StorageClassResolver.toS3StorageClass(EC_6_3, policy));
+ }
+
+ assertEquals(S3StorageClass.GLACIER,
+ S3StorageClassResolver.toS3StorageClass(RATIS_THREE,
OzoneStoragePolicy.COLD));
+ assertEquals(S3StorageClass.GLACIER,
+ S3StorageClassResolver.toS3StorageClass(RATIS_ONE,
OzoneStoragePolicy.COLD));
+ assertEquals(S3StorageClass.DEEP_ARCHIVE,
+ S3StorageClassResolver.toS3StorageClass(EC_6_3,
OzoneStoragePolicy.COLD));
+ assertEquals(S3StorageClass.REDUCED_REDUNDANCY,
+ S3StorageClassResolver.toS3StorageClass(STANDALONE_ONE,
OzoneStoragePolicy.COLD));
+ assertEquals(S3StorageClass.REDUCED_REDUNDANCY,
+ S3StorageClassResolver.toS3StorageClass(STANDALONE_THREE,
OzoneStoragePolicy.COLD));
+ }
+
+ /** The policy determines the EC class; changing the EC layout does not
change it. */
+ @Test
+ void testReverseIgnoresEcLayout() {
+ for (ReplicationConfig replication : REPLICATIONS) {
+ if (replication.getReplicationType() == ReplicationType.EC) {
+ for (StoragePolicy policy : Arrays.asList(null,
OzoneStoragePolicy.HOT, OzoneStoragePolicy.WARM)) {
+ assertEquals(S3StorageClass.STANDARD_IA,
+ S3StorageClassResolver.toS3StorageClass(replication, policy));
+ }
+ assertEquals(S3StorageClass.DEEP_ARCHIVE,
+ S3StorageClassResolver.toS3StorageClass(replication,
OzoneStoragePolicy.COLD));
+ }
+ }
+ }
+
+ @Test
+ void testForwardDefaultSpecs() {
+ S3StorageClassResolver resolver = new S3StorageClassResolver(new
OzoneConfiguration());
+
+ S3StorageClassSpec standard = resolver.toSpec(S3_STORAGE_CLASS_STANDARD);
+ assertEquals(ReplicationType.RATIS, standard.getReplicationType());
+ assertEquals(ReplicationFactor.THREE, standard.getReplicationFactor());
+ assertNull(standard.getStoragePolicy(), "unset by default, so the bucket's
policy applies");
+
+ S3StorageClassSpec reduced =
resolver.toSpec(S3_STORAGE_CLASS_REDUCED_REDUNDANCY);
+ assertEquals(ReplicationFactor.ONE, reduced.getReplicationFactor());
+ assertNull(reduced.getStoragePolicy());
+
+ S3StorageClassSpec standardIa =
resolver.toSpec(S3_STORAGE_CLASS_STANDARD_IA);
+ assertEquals(ReplicationType.EC, standardIa.getReplicationType());
+ assertNull(standardIa.getReplicationFactor());
+ assertNull(standardIa.getStoragePolicy());
+ assertEquals(OzoneStoragePolicy.COLD,
+ resolver.toSpec(S3_STORAGE_CLASS_GLACIER).getStoragePolicy());
+
+ S3StorageClassSpec deepArchive =
resolver.toSpec(S3_STORAGE_CLASS_DEEP_ARCHIVE);
+ assertEquals(ReplicationType.EC, deepArchive.getReplicationType());
+ assertNull(deepArchive.getReplicationFactor());
+ assertEquals(OzoneStoragePolicy.COLD, deepArchive.getStoragePolicy());
+
+ for (String input : Arrays.asList(null, "", " \t ", "INVALID",
"standard")) {
+ assertThrows(IllegalArgumentException.class, () ->
resolver.toSpec(input));
+ }
+ }
+
+ @Test
+ void testForwardConfiguredStoragePolicy() {
+ OzoneConfiguration conf = new OzoneConfiguration();
+ conf.set(OZONE_S3G_STORAGE_CLASS_STANDARD_STORAGE_POLICY, "hot");
+ conf.set(OZONE_S3G_STORAGE_CLASS_REDUCED_REDUNDANCY_STORAGE_POLICY, " WARM
");
+ conf.set(OZONE_S3G_STORAGE_CLASS_STANDARD_IA_STORAGE_POLICY, " hot ");
+ S3StorageClassResolver resolver = new S3StorageClassResolver(conf);
+
+ assertEquals(OzoneStoragePolicy.HOT,
+ resolver.toSpec(S3_STORAGE_CLASS_STANDARD).getStoragePolicy());
+ assertEquals(OzoneStoragePolicy.WARM,
resolver.toSpec(S3_STORAGE_CLASS_REDUCED_REDUNDANCY).getStoragePolicy());
+ assertEquals(OzoneStoragePolicy.HOT,
resolver.toSpec(S3_STORAGE_CLASS_STANDARD_IA).getStoragePolicy());
+ assertNull(S3StorageClass.STANDARD_IA.getSpec().getStoragePolicy());
+ // The setting applies only to its own class.
+ assertEquals(OzoneStoragePolicy.COLD,
+ resolver.toSpec(S3_STORAGE_CLASS_GLACIER).getStoragePolicy());
+ assertEquals(OzoneStoragePolicy.COLD,
resolver.toSpec(S3_STORAGE_CLASS_DEEP_ARCHIVE).getStoragePolicy());
+ }
+
+ /** COLD would make these writes report an archive class, so it is rejected.
*/
+ @Test
+ void testConstructionRejectsColdAndUnknownPolicy() {
+ for (String key :
Arrays.asList(OZONE_S3G_STORAGE_CLASS_STANDARD_STORAGE_POLICY,
+ OZONE_S3G_STORAGE_CLASS_REDUCED_REDUNDANCY_STORAGE_POLICY,
+ OZONE_S3G_STORAGE_CLASS_STANDARD_IA_STORAGE_POLICY)) {
+ for (String value : Arrays.asList("COLD", " cold ", "LUKEWARM")) {
+ OzoneConfiguration conf = new OzoneConfiguration();
+ conf.set(key, value);
+ IllegalArgumentException exception =
assertThrows(IllegalArgumentException.class,
+ () -> new S3StorageClassResolver(conf));
+ assertTrue(exception.getMessage().contains(key));
+ assertTrue(exception.getMessage().contains("Expected HOT, WARM or
empty."));
+ }
+ }
+ }
+
+ @Test
+ void testForwardReusesPoliciesResolvedAtConstruction() {
+ OzoneConfiguration conf = new OzoneConfiguration();
+ conf.set(OZONE_S3G_STORAGE_CLASS_STANDARD_STORAGE_POLICY, "HOT");
+ conf.set(OZONE_S3G_STORAGE_CLASS_REDUCED_REDUNDANCY_STORAGE_POLICY,
"WARM");
+ conf.set(OZONE_S3G_STORAGE_CLASS_STANDARD_IA_STORAGE_POLICY, "HOT");
+ S3StorageClassResolver resolver = new S3StorageClassResolver(conf);
+ S3StorageClassSpec standard = resolver.toSpec(S3_STORAGE_CLASS_STANDARD);
+ S3StorageClassSpec reduced =
resolver.toSpec(S3_STORAGE_CLASS_REDUCED_REDUNDANCY);
+ S3StorageClassSpec standardIa =
resolver.toSpec(S3_STORAGE_CLASS_STANDARD_IA);
+
+ conf.set(OZONE_S3G_STORAGE_CLASS_STANDARD_STORAGE_POLICY, "COLD");
+ conf.set(OZONE_S3G_STORAGE_CLASS_REDUCED_REDUNDANCY_STORAGE_POLICY,
"INVALID");
+ conf.set(OZONE_S3G_STORAGE_CLASS_STANDARD_IA_STORAGE_POLICY, "COLD");
+ assertSame(standard, resolver.toSpec(S3_STORAGE_CLASS_STANDARD));
+ assertSame(reduced, resolver.toSpec(S3_STORAGE_CLASS_REDUCED_REDUNDANCY));
+ assertSame(standardIa, resolver.toSpec(S3_STORAGE_CLASS_STANDARD_IA));
+ assertEquals(OzoneStoragePolicy.HOT, standard.getStoragePolicy());
+ assertEquals(OzoneStoragePolicy.WARM, reduced.getStoragePolicy());
+ assertEquals(OzoneStoragePolicy.HOT, standardIa.getStoragePolicy());
+ }
+
+ /**
+ * A STANDARD write with a configured storage policy must read back as
STANDARD, so changing the
+ * setting never relabels objects.
+ */
+ @Test
+ void testConfiguredPolicyRoundTripsToSameClass() {
+ for (String policy : Arrays.asList("", " \t ", "HOT", "WARM")) {
+ OzoneConfiguration conf = new OzoneConfiguration();
+ conf.set(OZONE_S3G_STORAGE_CLASS_STANDARD_STORAGE_POLICY, policy);
+ conf.set(OZONE_S3G_STORAGE_CLASS_REDUCED_REDUNDANCY_STORAGE_POLICY,
policy);
+ conf.set(OZONE_S3G_STORAGE_CLASS_STANDARD_IA_STORAGE_POLICY, policy);
+ S3StorageClassResolver resolver = new S3StorageClassResolver(conf);
+ if (StringUtils.isBlank(policy)) {
+
assertNull(resolver.toSpec(S3_STORAGE_CLASS_STANDARD).getStoragePolicy());
+
assertNull(resolver.toSpec(S3_STORAGE_CLASS_REDUCED_REDUNDANCY).getStoragePolicy());
+
assertNull(resolver.toSpec(S3_STORAGE_CLASS_STANDARD_IA).getStoragePolicy());
+ }
+ for (S3StorageClass storageClass : S3StorageClass.values()) {
+ S3StorageClassSpec spec =
resolver.toSpec(storageClass.getS3StorageClass());
+ ReplicationConfig replication =
+ S3StorageClassResolver.toReplicationConfig(spec, null, null);
+ assertEquals(storageClass,
+ S3StorageClassResolver.toS3StorageClass(replication,
spec.getStoragePolicy()),
+ storageClass + " with policy setting '" + policy + "'");
+ }
+ }
+ }
+
+ /** EC layout comes from the request header, then the bucket, then defaults
to 3-2. */
+ @ParameterizedTest
+ @EnumSource(value = S3StorageClass.class, names = {"STANDARD_IA",
"DEEP_ARCHIVE"})
+ void testForwardEcLayoutOrder(S3StorageClass storageClass) {
+ S3StorageClassResolver resolver = new S3StorageClassResolver(new
OzoneConfiguration());
+ S3StorageClassSpec ec = resolver.toSpec(storageClass.getS3StorageClass());
+
+ assertEquals(new ECReplicationConfig("rs-6-3-1024k"),
+ S3StorageClassResolver.toReplicationConfig(ec, "rs-6-3-1024k",
EC_3_2));
+ assertEquals(EC_6_3, S3StorageClassResolver.toReplicationConfig(ec, null,
EC_6_3));
+ assertEquals(EC_6_3, S3StorageClassResolver.toReplicationConfig(ec, "",
EC_6_3));
+ // A non-EC bucket replication is not used for an EC class.
+ assertEquals(EC_3_2, S3StorageClassResolver.toReplicationConfig(ec, null,
RATIS_THREE));
+ assertEquals(EC_3_2, S3StorageClassResolver.toReplicationConfig(ec, null,
null));
+
+ assertThrows(IllegalArgumentException.class,
+ () -> S3StorageClassResolver.toReplicationConfig(ec, "INVALID",
EC_6_3));
+ assertThrows(IllegalArgumentException.class,
+ () -> S3StorageClassResolver.toReplicationConfig(ec, "INVALID", null));
+ }
+
+ @Test
+ void testForwardRatisReplication() {
+ S3StorageClassResolver resolver = new S3StorageClassResolver(new
OzoneConfiguration());
+ // RATIS classes use their own factor and ignore the bucket's replication.
+ assertEquals(RATIS_THREE, S3StorageClassResolver.toReplicationConfig(
+ resolver.toSpec(S3_STORAGE_CLASS_STANDARD), null, EC_6_3));
+ assertEquals(RATIS_ONE, S3StorageClassResolver.toReplicationConfig(
+ resolver.toSpec(S3_STORAGE_CLASS_REDUCED_REDUNDANCY), null, null));
+ }
+
+ @Test
+ void testForwardRejectsMissingReplicationTypeWithBucketDefault() {
+ S3StorageClassSpec spec = S3StorageClassSpec.newBuilder()
+ .setStoragePolicy(OzoneStoragePolicy.COLD)
+ .build();
+
+ for (ReplicationConfig bucketConfig : Arrays.asList(RATIS_THREE, EC_6_3)) {
+ assertThrows(IllegalArgumentException.class,
+ () -> S3StorageClassResolver.toReplicationConfig(spec, null,
bucketConfig));
+ }
+ }
+
+ @Test
+ void testForwardRejectsMissingReplicationTypeWithoutBucketDefault() {
+ S3StorageClassSpec spec = S3StorageClassSpec.newBuilder().build();
+
+ assertThrows(IllegalArgumentException.class,
+ () -> S3StorageClassResolver.toReplicationConfig(spec, null, null));
+ }
+
+ @Test
+ void testSpecIsAValue() {
+ S3StorageClassSpec a = S3StorageClassSpec.newBuilder()
+ .setReplicationType(ReplicationType.RATIS)
+ .setReplicationFactor(ReplicationFactor.THREE)
+ .build();
+ S3StorageClassSpec b = S3StorageClassSpec.newBuilder()
+ .setReplicationType(ReplicationType.RATIS)
+ .setReplicationFactor(ReplicationFactor.THREE)
+ .build();
+ assertEquals(a, b);
+ assertEquals(a.hashCode(), b.hashCode());
+ // Unset fields stay null rather than taking a default.
+ assertNull(S3StorageClassSpec.newBuilder().build().getReplicationType());
+ }
+
+ /** The replication-only mapping as it was before storage policy support. */
+ private static String originalMapping(ReplicationConfig config) {
+ if (config.getReplicationType() == ReplicationType.EC) {
+ return "STANDARD_IA";
+ }
+ if (config.getReplicationType() == ReplicationType.STAND_ALONE
+ || config.getRequiredNodes() == 1) {
+ return "REDUCED_REDUNDANCY";
+ }
+ return "STANDARD";
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]