This is an automated email from the ASF dual-hosted git repository.

sammichen pushed a commit to branch HDDS-8342
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/HDDS-8342 by this push:
     new 949747727b3 HDDS-14377. Refactor OmLifecycleConfiguration#Builder 
(#9688)
949747727b3 is described below

commit 949747727b38e12e3c51806311790d1565e1b003
Author: Bolin Lin <[email protected]>
AuthorDate: Thu Feb 12 06:31:26 2026 -0500

    HDDS-14377. Refactor OmLifecycleConfiguration#Builder (#9688)
---
 .../ozone/om/helpers/OmLifecycleConfiguration.java | 25 ++++++---
 .../apache/hadoop/ozone/om/helpers/OMLCUtils.java  | 11 +++-
 .../hadoop/ozone/om/helpers/TestOmLCRule.java      |  2 +-
 .../om/helpers/TestOmLifeCycleConfiguration.java   |  8 +--
 .../OMLifecycleConfigurationSetRequest.java        | 13 +++--
 .../ozone/om/service/TestKeyLifecycleService.java  | 60 ++++++++++++++--------
 .../s3/endpoint/S3LifecycleConfiguration.java      | 12 +++--
 7 files changed, 87 insertions(+), 44 deletions(-)

diff --git 
a/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OmLifecycleConfiguration.java
 
b/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OmLifecycleConfiguration.java
index 7b8b4a1045f..60c181cdf95 100644
--- 
a/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OmLifecycleConfiguration.java
+++ 
b/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/OmLifecycleConfiguration.java
@@ -110,6 +110,11 @@ public BucketLayout getBucketLayout() {
    * @throws OMException if the validation fails
    */
   public void valid() throws OMException {
+    validateFields(volume, bucket, rules, bucketLayout, creationTime);
+  }
+
+  private static void validateFields(String volume, String bucket, 
List<OmLCRule> rules,
+      BucketLayout bucketLayout, long creationTime) throws OMException {
     if (StringUtils.isBlank(volume)) {
       throw new OMException("Invalid lifecycle configuration: Volume cannot be 
blank.",
           OMException.ResultCodes.INVALID_REQUEST);
@@ -130,7 +135,7 @@ public void valid() throws OMException {
           + LC_MAX_RULES + " rules", OMException.ResultCodes.INVALID_REQUEST);
     }
 
-    if (!hasNoDuplicateID()) {
+    if (!hasNoDuplicateID(rules)) {
       throw new OMException("Invalid lifecycle configuration: Duplicate rule 
IDs found.",
           OMException.ResultCodes.INVALID_REQUEST);
     }
@@ -140,7 +145,7 @@ public void valid() throws OMException {
     }
   }
 
-  private boolean hasNoDuplicateID() {
+  private static boolean hasNoDuplicateID(List<OmLCRule> rules) {
     return rules.size() == rules.stream()
         .map(OmLCRule::getId)
         .collect(Collectors.toSet())
@@ -312,14 +317,18 @@ public Builder setUpdateID(long uID) {
     }
 
     @Override
-    public OmLifecycleConfiguration buildObject() {
-      return new OmLifecycleConfiguration(this);
+    protected void validate() {
+      super.validate();
+      try {
+        validateFields(volume, bucket, rules, bucketLayout, creationTime);
+      } catch (OMException e) {
+        throw new IllegalArgumentException(e.getMessage(), e);
+      }
     }
 
-    public OmLifecycleConfiguration buildAndValid() throws OMException {
-      OmLifecycleConfiguration omLifecycleConfiguration = buildObject();
-      omLifecycleConfiguration.valid();
-      return omLifecycleConfiguration;
+    @Override
+    protected OmLifecycleConfiguration buildObject() {
+      return new OmLifecycleConfiguration(this);
     }
   }
 }
diff --git 
a/hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/OMLCUtils.java
 
b/hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/OMLCUtils.java
index 2de068cafea..2fa3ce32216 100644
--- 
a/hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/OMLCUtils.java
+++ 
b/hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/OMLCUtils.java
@@ -47,7 +47,16 @@ public final class OMLCUtils {
 
   public static void assertOMException(Executable action, 
OMException.ResultCodes expectedResultCode,
       String expectedMessageContent) {
-    OMException e = assertThrows(OMException.class, action);
+    Exception thrown = assertThrows(Exception.class, action);
+    OMException e;
+    if (thrown instanceof OMException) {
+      e = (OMException) thrown;
+    } else if (thrown instanceof IllegalArgumentException
+        && thrown.getCause() instanceof OMException) {
+      e = (OMException) thrown.getCause();
+    } else {
+      throw new AssertionError("Expected OMException but got: " + 
thrown.getClass().getName(), thrown);
+    }
     assertEquals(expectedResultCode, e.getResult());
     assertTrue(e.getMessage().contains(expectedMessageContent),
         "Expected: " + expectedMessageContent + "\n Actual: " + 
e.getMessage());
diff --git 
a/hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmLCRule.java
 
b/hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmLCRule.java
index 4524e4adfa8..f81b8afca3a 100644
--- 
a/hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmLCRule.java
+++ 
b/hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmLCRule.java
@@ -185,7 +185,7 @@ public void testDuplicateRuleIDs() throws OMException {
         .setBucket("bucket")
         .setRules(rules);
 
-    assertOMException(() -> config.buildAndValid(), INVALID_REQUEST, 
"Duplicate rule IDs found");
+    assertOMException(config::build, INVALID_REQUEST, "Duplicate rule IDs 
found");
   }
 
   @Test
diff --git 
a/hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmLifeCycleConfiguration.java
 
b/hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmLifeCycleConfiguration.java
index 3afb45e84db..b3e5fe551c0 100644
--- 
a/hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmLifeCycleConfiguration.java
+++ 
b/hadoop-ozone/common/src/test/java/org/apache/hadoop/ozone/om/helpers/TestOmLifeCycleConfiguration.java
@@ -73,14 +73,14 @@ public void testCreateInValidLCConfiguration() throws 
OMException {
     List<OmLCRule> rules = Collections.singletonList(rule);
 
     OmLifecycleConfiguration.Builder lcc0 = getOmLifecycleConfiguration(null, 
"bucket", rules);
-    assertOMException(() -> lcc0.buildAndValid(), INVALID_REQUEST, "Volume 
cannot be blank");
+    assertOMException(lcc0::build, INVALID_REQUEST, "Volume cannot be blank");
 
     OmLifecycleConfiguration.Builder lcc1 = 
getOmLifecycleConfiguration("volume", null, rules);
-    assertOMException(() -> lcc1.buildAndValid(), INVALID_REQUEST, "Bucket 
cannot be blank");
+    assertOMException(lcc1::build, INVALID_REQUEST, "Bucket cannot be blank");
 
     OmLifecycleConfiguration.Builder lcc3 = getOmLifecycleConfiguration(
         "volume", "bucket", Collections.emptyList());
-    assertOMException(() -> lcc3.buildAndValid(), INVALID_REQUEST,
+    assertOMException(lcc3::build, INVALID_REQUEST,
         "At least one rules needs to be specified in a lifecycle 
configuration");
 
     List<OmLCRule> rules4 = new ArrayList<>(
@@ -94,7 +94,7 @@ public void testCreateInValidLCConfiguration() throws 
OMException {
       rules4.add(r);
     }
     OmLifecycleConfiguration.Builder lcc4 = 
getOmLifecycleConfiguration("volume", "bucket", rules4);
-    assertOMException(() -> lcc4.buildAndValid(), INVALID_REQUEST,
+    assertOMException(lcc4::build, INVALID_REQUEST,
         "The number of lifecycle rules must not exceed the allowed limit of");
   }
 
diff --git 
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/lifecycle/OMLifecycleConfigurationSetRequest.java
 
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/lifecycle/OMLifecycleConfigurationSetRequest.java
index 2be7f688564..144a53b4745 100644
--- 
a/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/lifecycle/OMLifecycleConfigurationSetRequest.java
+++ 
b/hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/request/lifecycle/OMLifecycleConfigurationSetRequest.java
@@ -148,9 +148,16 @@ public OMClientResponse 
validateAndUpdateCache(OzoneManager ozoneManager, Execut
       OmLifecycleConfiguration.Builder lcBuilder =
           
OmLifecycleConfiguration.getBuilderFromProtobuf(lifecycleConfiguration);
       lcBuilder.setUpdateID(transactionLogIndex);
-      OmLifecycleConfiguration omLifecycleConfiguration =
-          lcBuilder.setBucketObjectID(bucketInfo.getObjectID()).build();
-      omLifecycleConfiguration.valid();
+      OmLifecycleConfiguration omLifecycleConfiguration;
+      try {
+        omLifecycleConfiguration =
+            lcBuilder.setBucketObjectID(bucketInfo.getObjectID()).build();
+      } catch (IllegalArgumentException e) {
+        if (e.getCause() instanceof OMException) {
+          throw (OMException) e.getCause();
+        }
+        throw e;
+      }
       auditMap = omLifecycleConfiguration.toAuditMap();
 
       metadataManager.getLifecycleConfigurationTable().addCacheEntry(
diff --git 
a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/service/TestKeyLifecycleService.java
 
b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/service/TestKeyLifecycleService.java
index 3769ba41a8c..e499e0dd9ef 100644
--- 
a/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/service/TestKeyLifecycleService.java
+++ 
b/hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/service/TestKeyLifecycleService.java
@@ -1846,21 +1846,29 @@ private OmLifecycleRuleAndOperator.Builder 
getOmLCAndOperatorBuilder(
 
   private void createLifecyclePolicy(String volume, String bucket, 
BucketLayout layout, String prefix,
       OmLCFilter filter, String date, boolean enabled) throws IOException {
-    OmLifecycleConfiguration lcc = new OmLifecycleConfiguration.Builder()
-        .setVolume(volume)
-        .setBucket(bucket)
-        .setBucketLayout(layout)
-        .setBucketObjectID(bucketObjectID)
-        .setRules(Collections.singletonList(new OmLCRule.Builder()
-            .setId(String.valueOf(OBJECT_ID_COUNTER.getAndIncrement()))
-            .setEnabled(enabled)
-            .setPrefix(prefix)
-            .setFilter(filter)
-            .setAction(new OmLCExpiration.Builder()
-                .setDate(date)
-                .build())
-            .build()))
-        .build();
+    OmLifecycleConfiguration lcc;
+    try {
+      lcc = new OmLifecycleConfiguration.Builder()
+          .setVolume(volume)
+          .setBucket(bucket)
+          .setBucketLayout(layout)
+          .setBucketObjectID(bucketObjectID)
+          .setRules(Collections.singletonList(new OmLCRule.Builder()
+              .setId(String.valueOf(OBJECT_ID_COUNTER.getAndIncrement()))
+              .setEnabled(enabled)
+              .setPrefix(prefix)
+              .setFilter(filter)
+              .setAction(new OmLCExpiration.Builder()
+                  .setDate(date)
+                  .build())
+              .build()))
+          .build();
+    } catch (IllegalArgumentException e) {
+      if (e.getCause() instanceof OMException) {
+        throw (OMException) e.getCause();
+      }
+      throw e;
+    }
     String key = "/" + volume + "/" + bucket;
     LifecycleConfiguration lcProto = lcc.getProtobuf();
     OmLifecycleConfiguration canonicalLcc = 
OmLifecycleConfiguration.getFromProtobuf(lcProto);
@@ -1872,13 +1880,21 @@ private void createLifecyclePolicy(String volume, 
String bucket, BucketLayout la
 
   private void createLifecyclePolicy(String volume, String bucket, 
BucketLayout layout, List<OmLCRule> ruleList)
       throws IOException {
-    OmLifecycleConfiguration lcc = new OmLifecycleConfiguration.Builder()
-        .setVolume(volume)
-        .setBucket(bucket)
-        .setBucketObjectID(bucketObjectID)
-        .setBucketLayout(layout)
-        .setRules(ruleList)
-        .build();
+    OmLifecycleConfiguration lcc;
+    try {
+      lcc = new OmLifecycleConfiguration.Builder()
+          .setVolume(volume)
+          .setBucket(bucket)
+          .setBucketObjectID(bucketObjectID)
+          .setBucketLayout(layout)
+          .setRules(ruleList)
+          .build();
+    } catch (IllegalArgumentException e) {
+      if (e.getCause() instanceof OMException) {
+        throw (OMException) e.getCause();
+      }
+      throw e;
+    }
     String key = "/" + volume + "/" + bucket;
     LifecycleConfiguration lcProto = lcc.getProtobuf();
     OmLifecycleConfiguration canonicalLcc = 
OmLifecycleConfiguration.getFromProtobuf(lcProto);
diff --git 
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/S3LifecycleConfiguration.java
 
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/S3LifecycleConfiguration.java
index 193538ced75..5cbc4a275d2 100644
--- 
a/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/S3LifecycleConfiguration.java
+++ 
b/hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/S3LifecycleConfiguration.java
@@ -264,12 +264,14 @@ public OmLifecycleConfiguration 
toOmLifecycleConfiguration(OzoneBucket ozoneBuck
         builder.addRule(convertToOmRule(rule));
       }
 
-      return builder.buildAndValid();
-    } catch (Exception ex) {
-      if (ex instanceof IllegalStateException) {
-        throw S3ErrorTable.newError(S3ErrorTable.INVALID_REQUEST, 
ozoneBucket.getName(), ex);
+      return builder.build();
+    } catch (IllegalArgumentException ex) {
+      if (ex.getCause() instanceof OMException) {
+        throw (OMException) ex.getCause();
       }
-      throw ex;
+      throw S3ErrorTable.newError(S3ErrorTable.INVALID_REQUEST, 
ozoneBucket.getName(), ex);
+    } catch (IllegalStateException ex) {
+      throw S3ErrorTable.newError(S3ErrorTable.INVALID_REQUEST, 
ozoneBucket.getName(), ex);
     }
   }
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to