chihsuan commented on code in PR #12161:
URL: https://github.com/apache/ozone/pull/12161#discussion_r4786716382


##########
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/ObjectEndpoint.java:
##########
@@ -356,6 +361,29 @@ customMetadata, tags, multiDigestInputStream, getHeaders(),
     }
   }
 
+  /**
+   * Adds the {@code x-amz-expiration} header if enabled and the new object 
matches an enabled Expiration rule of
+   * the bucket lifecycle configuration. The object is already committed, so a 
failed lookup only omits the header.
+   */
+  private void addExpirationHeader(ResponseBuilder responseBuilder, 
OzoneBucket bucket, String keyPath,
+      Map<String, String> tags) {
+    if 
(!getOzoneConfiguration().getBoolean(OZONE_S3G_LIFECYCLE_EXPIRATION_HEADER_ENABLED,
+        OZONE_S3G_LIFECYCLE_EXPIRATION_HEADER_ENABLED_DEFAULT)) {
+      return;
+    }
+    try {
+      OzoneLifecycleConfiguration lifecycleConfiguration =
+          
getClientProtocol().getLifecycleConfiguration(bucket.getVolumeName(), 
bucket.getName());

Review Comment:
   Could we avoid an OM error for buckets without a lifecycle configuration? 
Each lookup logs an ERROR with a stack trace on OM, so the OM log could get 
noisy.



##########
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/ObjectEndpoint.java:
##########
@@ -356,6 +361,29 @@ customMetadata, tags, multiDigestInputStream, getHeaders(),
     }
   }
 
+  /**
+   * Adds the {@code x-amz-expiration} header if enabled and the new object 
matches an enabled Expiration rule of
+   * the bucket lifecycle configuration. The object is already committed, so a 
failed lookup only omits the header.
+   */
+  private void addExpirationHeader(ResponseBuilder responseBuilder, 
OzoneBucket bucket, String keyPath,
+      Map<String, String> tags) {
+    if 
(!getOzoneConfiguration().getBoolean(OZONE_S3G_LIFECYCLE_EXPIRATION_HEADER_ENABLED,
+        OZONE_S3G_LIFECYCLE_EXPIRATION_HEADER_ENABLED_DEFAULT)) {
+      return;
+    }
+    try {
+      OzoneLifecycleConfiguration lifecycleConfiguration =
+          
getClientProtocol().getLifecycleConfiguration(bucket.getVolumeName(), 
bucket.getName());
+      String expiration = 
S3LifecycleConfiguration.getExpirationHeader(lifecycleConfiguration, keyPath, 
tags,
+          Time.now());
+      if (expiration != null) {
+        responseBuilder.header(EXPIRATION_HEADER, expiration);
+      }
+    } catch (IOException ex) {

Review Comment:
   nit: A `Days` like 3000000 makes the formatter throw after the object is 
committed, so the PUT fails. Should the header be skipped in that case?



##########
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/ObjectEndpoint.java:
##########
@@ -356,6 +361,29 @@ customMetadata, tags, multiDigestInputStream, getHeaders(),
     }
   }
 
+  /**
+   * Adds the {@code x-amz-expiration} header if enabled and the new object 
matches an enabled Expiration rule of
+   * the bucket lifecycle configuration. The object is already committed, so a 
failed lookup only omits the header.
+   */
+  private void addExpirationHeader(ResponseBuilder responseBuilder, 
OzoneBucket bucket, String keyPath,
+      Map<String, String> tags) {
+    if 
(!getOzoneConfiguration().getBoolean(OZONE_S3G_LIFECYCLE_EXPIRATION_HEADER_ENABLED,
+        OZONE_S3G_LIFECYCLE_EXPIRATION_HEADER_ENABLED_DEFAULT)) {
+      return;
+    }

Review Comment:
   Should we omit the header while the lifecycle service is disabled? Nothing 
would expire the object then.



##########
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/S3LifecycleConfiguration.java:
##########
@@ -654,4 +661,87 @@ private static AndOperator convertFromOzoneAndOperator(
 
     return andOperator;
   }
+
+  /**
+   * Builds the value of the {@code x-amz-expiration} response header for a 
newly written object, in the form
+   * {@code expiry-date="Fri, 27 Nov 2026 00:00:00 GMT", rule-id="rule1"}. 
Among the enabled rules with an
+   * Expiration action that match the object, the one with the earliest expiry 
date is used. For a Days rule,
+   * the expiry date is the creation time plus the given days, rounded up to 
the next midnight UTC, as AWS S3 does.
+   *
+   * @param lifecycleConfiguration lifecycle configuration of the bucket
+   * @param keyName name of the object
+   * @param tags tags of the object
+   * @param creationTime creation time of the object in milliseconds since 
epoch
+   * @return the header value, or null if no enabled Expiration rule matches 
the object
+   */
+  public static String getExpirationHeader(OzoneLifecycleConfiguration 
lifecycleConfiguration, String keyName,
+      Map<String, String> tags, long creationTime) {
+    ZonedDateTime earliest = null;
+    String ruleId = null;
+    for (OzoneLifecycleConfiguration.OzoneLCRule rule : 
lifecycleConfiguration.getRules()) {
+      if (!STATUS_ENABLED.equals(rule.getStatus()) || rule.getExpiration() == 
null
+          || !matches(rule, keyName, tags)) {
+        continue;
+      }
+      ZonedDateTime expiry = getExpiryDate(rule.getExpiration(), creationTime);
+      if (expiry != null && (earliest == null || expiry.isBefore(earliest))) {
+        earliest = expiry;
+        ruleId = rule.getId();
+      }
+    }
+    if (earliest == null) {
+      return null;
+    }
+    return "expiry-date=\"" + RFC1123Util.FORMAT.format(earliest) + "\", 
rule-id=\"" + ruleId + "\"";
+  }
+
+  private static boolean matches(OzoneLifecycleConfiguration.OzoneLCRule rule, 
String keyName,

Review Comment:
   +1. Could OM compute the expiry on commit, like #12170 does for HEAD/GET? An 
optional `CommitKeyResponse` field, like `modificationTime`, would avoid the 
extra OM call per PUT.
   
https://github.com/apache/ozone/blob/ff77145ac99a6ff8c59f94404233582bcaf5f844/hadoop-ozone/interface-client/src/main/proto/OmClientProtocol.proto#L1689-L1693



-- 
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]

Reply via email to