Gargi-jais11 commented on code in PR #12161:
URL: https://github.com/apache/ozone/pull/12161#discussion_r4783417611


##########
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,
+      Map<String, String> tags) {
+    if (rule.getPrefix() != null) {
+      return keyName.startsWith(rule.getPrefix());
+    }
+    OzoneLifecycleConfiguration.OzoneLCFilter filter = rule.getFilter();
+    if (filter == null) {
+      return false;
+    }
+    if (filter.getPrefix() != null) {
+      return keyName.startsWith(filter.getPrefix());
+    }
+    if (filter.getTag() != null) {
+      return 
filter.getTag().getValue().equals(tags.get(filter.getTag().getKey()));
+    }
+    OzoneLifecycleConfiguration.LifecycleAndOperator andOperator = 
filter.getAndOperator();
+    if (andOperator != null) {
+      if (andOperator.getPrefix() != null && 
!keyName.startsWith(andOperator.getPrefix())) {
+        return false;
+      }
+      if (andOperator.getTags() != null) {
+        for (Map.Entry<String, String> tag : andOperator.getTags().entrySet()) 
{
+          if (!tag.getValue().equals(tags.get(tag.getKey()))) {
+            return false;
+          }
+        }
+      }
+    }
+    // An empty filter applies to all objects in the bucket.
+    return true;
+  }
+
+  private static ZonedDateTime 
getExpiryDate(OzoneLifecycleConfiguration.OzoneLCExpiration expiration,

Review Comment:
   It's correct that AWS rounds to midnight because AWS deletes on day 
boundaries. 
   what I see from code is  Ozone doesn't. `OmLCExpiration.isExpired() `treats 
a key as expired once `now > modificationTime + days`, with no rounding, and 
KeyLifecycleService uses that through `OmLCRule.match()`. 
   In your givet example, the header says the object lives until **27 Nov 
00:00** but KeyLifecycleService can delete it from **26 Nov 10:30** about 13.5 
hours earlier. A cleint that trusts the header could plan to read an object 
that is already gone.
   
   Date rules have a similar gap. `isExpired()` only expires objects created 
before the rule's Date. A PUT after that date gets a header with a date in the 
past, but Ozone never deletes that object.
   I beilve both should be consistent to each other.



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