rich7420 commented on code in PR #11157:
URL: https://github.com/apache/ozone/pull/11157#discussion_r4704833890
##########
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/ObjectEndpoint.java:
##########
@@ -514,6 +527,91 @@ static void addLastModifiedDate(
RFC1123Util.FORMAT.format(lastModificationTime));
}
+ /**
+ * Adds the {@code x-amz-expiration} header when an enabled lifecycle
+ * expiration rule covers the key, as S3 does: the value names the date the
+ * object is scheduled for deletion and the rule that schedules it. When more
+ * than one rule covers the key, the earliest expiry is reported.
+ * <p>
+ * The header is advisory, so a bucket without a lifecycle configuration, a
+ * caller who may not read it, or any other lookup failure only leaves the
+ * header out; the HEAD itself still succeeds.
+ */
+ private void addExpirationHeader(ResponseBuilder responseBuilder,
+ String bucketName, String keyPath, OzoneKey key) {
+ if (lifecycleAbsenceCache.isKnownAbsent(bucketName)) {
+ return;
+ }
+ try {
+ OzoneLifecycleConfiguration lifecycleConfiguration = getClientProtocol()
+ .getLifecycleConfiguration(key.getVolumeName(), bucketName);
+
+ ZonedDateTime earliest = null;
+ String ruleId = null;
+ for (OzoneLifecycleConfiguration.OzoneLCRule rule :
lifecycleConfiguration.getRules()) {
+ if (!rule.isEnabled() || rule.getExpiration() == null
+ || !rule.matches(keyPath, key.getTags())) {
+ continue;
+ }
+ ZonedDateTime expiryDate = expiryDateOf(rule.getExpiration(),
key.getModificationTime());
+ if (expiryDate != null && (earliest == null ||
expiryDate.isBefore(earliest))) {
+ earliest = expiryDate;
+ ruleId = rule.getId();
+ }
+ }
+
+ String quotedRuleId = earliest == null ? null : quoteRuleId(ruleId);
+ if (quotedRuleId != null) {
+ responseBuilder.header(EXPIRATION_HEADER,
+ String.format("expiry-date=\"%s\", rule-id=%s",
RFC1123Util.FORMAT.format(earliest), quotedRuleId));
+ }
+ } catch (OMException ex) {
+ if (ex.getResult() == ResultCodes.LIFECYCLE_CONFIGURATION_NOT_FOUND) {
+ lifecycleAbsenceCache.markAbsent(bucketName);
+ } else {
+ LOG.debug("Omitting {} header for {}/{}", EXPIRATION_HEADER,
bucketName, keyPath, ex);
+ }
+ } catch (IOException | RuntimeException ex) {
+ LOG.debug("Omitting {} header for {}/{}", EXPIRATION_HEADER, bucketName,
keyPath, ex);
+ }
+ }
+
+ /**
+ * Renders a lifecycle rule id as an RFC 7230 quoted-string. Rule ids are
+ * supplied by the client and only validated for length, so a quote or a
+ * backslash is escaped here. An id carrying a control character cannot be
+ * represented in a header value at all; that yields null so the caller drops
+ * the header rather than emitting a malformed one.
+ */
+ private static String quoteRuleId(String ruleId) {
+ for (int i = 0; i < ruleId.length(); i++) {
+ char c = ruleId.charAt(i);
+ if ((c < 0x20 && c != '\t') || c == 0x7f) {
+ return null;
+ }
+ }
+ return '"' + ruleId.replace("\\", "\\\\").replace("\"", "\\\"") + '"';
+ }
+
+ /**
+ * S3 reports a {@code Days} expiry as the object's modification time plus
the
+ * configured days, rounded up to the next midnight UTC. A {@code Date}
expiry
+ * is already validated to be midnight UTC, so it is reported as is.
+ */
+ private static ZonedDateTime expiryDateOf(
+ OzoneLifecycleConfiguration.OzoneLCExpiration expiration, Instant
modificationTime) {
+ ZoneId gmt = ZoneId.of(OzoneConsts.OZONE_TIME_ZONE);
+ if (expiration.getDays() != null) {
+ return modificationTime.atZone(gmt).plusDays(expiration.getDays())
Review Comment:
OM's `OmLCExpiration.isExpired` uses `mtime + days` with no rounding, so a
key becomes eligible for deletion up to 24h before this date. Could the two
share one computation?
##########
hadoop-ozone/s3gateway/src/main/java/org/apache/hadoop/ozone/s3/endpoint/ObjectEndpoint.java:
##########
@@ -514,6 +527,91 @@ static void addLastModifiedDate(
RFC1123Util.FORMAT.format(lastModificationTime));
}
+ /**
+ * Adds the {@code x-amz-expiration} header when an enabled lifecycle
+ * expiration rule covers the key, as S3 does: the value names the date the
+ * object is scheduled for deletion and the rule that schedules it. When more
+ * than one rule covers the key, the earliest expiry is reported.
+ * <p>
+ * The header is advisory, so a bucket without a lifecycle configuration, a
+ * caller who may not read it, or any other lookup failure only leaves the
+ * header out; the HEAD itself still succeeds.
+ */
+ private void addExpirationHeader(ResponseBuilder responseBuilder,
+ String bucketName, String keyPath, OzoneKey key) {
+ if (lifecycleAbsenceCache.isKnownAbsent(bucketName)) {
+ return;
+ }
+ try {
+ OzoneLifecycleConfiguration lifecycleConfiguration = getClientProtocol()
+ .getLifecycleConfiguration(key.getVolumeName(), bucketName);
+
+ ZonedDateTime earliest = null;
+ String ruleId = null;
+ for (OzoneLifecycleConfiguration.OzoneLCRule rule :
lifecycleConfiguration.getRules()) {
+ if (!rule.isEnabled() || rule.getExpiration() == null
+ || !rule.matches(keyPath, key.getTags())) {
+ continue;
+ }
+ ZonedDateTime expiryDate = expiryDateOf(rule.getExpiration(),
key.getModificationTime());
+ if (expiryDate != null && (earliest == null ||
expiryDate.isBefore(earliest))) {
+ earliest = expiryDate;
+ ruleId = rule.getId();
+ }
+ }
+
+ String quotedRuleId = earliest == null ? null : quoteRuleId(ruleId);
+ if (quotedRuleId != null) {
+ responseBuilder.header(EXPIRATION_HEADER,
+ String.format("expiry-date=\"%s\", rule-id=%s",
RFC1123Util.FORMAT.format(earliest), quotedRuleId));
+ }
+ } catch (OMException ex) {
+ if (ex.getResult() == ResultCodes.LIFECYCLE_CONFIGURATION_NOT_FOUND) {
+ lifecycleAbsenceCache.markAbsent(bucketName);
+ } else {
+ LOG.debug("Omitting {} header for {}/{}", EXPIRATION_HEADER,
bucketName, keyPath, ex);
+ }
+ } catch (IOException | RuntimeException ex) {
+ LOG.debug("Omitting {} header for {}/{}", EXPIRATION_HEADER, bucketName,
keyPath, ex);
+ }
+ }
+
+ /**
+ * Renders a lifecycle rule id as an RFC 7230 quoted-string. Rule ids are
+ * supplied by the client and only validated for length, so a quote or a
+ * backslash is escaped here. An id carrying a control character cannot be
+ * represented in a header value at all; that yields null so the caller drops
+ * the header rather than emitting a malformed one.
+ */
+ private static String quoteRuleId(String ruleId) {
+ for (int i = 0; i < ruleId.length(); i++) {
+ char c = ruleId.charAt(i);
+ if ((c < 0x20 && c != '\t') || c == 0x7f) {
+ return null;
+ }
+ }
+ return '"' + ruleId.replace("\\", "\\\\").replace("\"", "\\\"") + '"';
Review Comment:
The HeadObject doc says the rule-id is URL-encoded. Could this use
`S3Utils.s3urlEncode`? With a quote escaped instead, SDK v1's parser reads
`we"ird` back as `we\`.
##########
hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectHead.java:
##########
@@ -321,6 +332,217 @@ private static Stream<Arguments>
reservedMetadataCollisionCases() {
Arguments.of(HttpHeaders.CONTENT_DISPOSITION,
CONTENT_DISPOSITION_CUSTOM, "inline", "user-disp"));
}
+ @Test
+ public void testHeadObjectIncludesExpirationForDaysRule() throws Exception {
+ String keyName = "logs/app.log";
+ createKey(keyName);
+ setLifecycleRules(daysRule("logs-30d", "logs/", 30));
+
+ Response response = keyEndpoint.head(bucketName, keyName);
+
+ assertEquals(HttpStatus.SC_OK, response.getStatus());
+ ZonedDateTime expiry = expectedDaysExpiry(keyName, 30);
+ assertEquals("expiry-date=\"" + RFC1123Util.FORMAT.format(expiry) + "\",
rule-id=\"logs-30d\"",
+ response.getHeaderString(EXPIRATION_HEADER));
+ }
+
+ @Test
+ public void testHeadObjectIncludesExpirationForDateRule() throws Exception {
+ String keyName = "logs/app.log";
+ createKey(keyName);
+ setLifecycleRules(new OmLCRule.Builder()
+ .setId("logs-date")
+ .setPrefix("logs/")
+ .setEnabled(true)
+ .setAction(new
OmLCExpiration.Builder().setDate("2042-04-02T00:00:00Z").build())
+ .build());
+
+ Response response = keyEndpoint.head(bucketName, keyName);
+
+ assertEquals("expiry-date=\"Wed, 02 Apr 2042 00:00:00 GMT\",
rule-id=\"logs-date\"",
+ response.getHeaderString(EXPIRATION_HEADER));
+ }
+
+ @Test
+ public void testHeadObjectIncludesExpirationForTagFilterRule() throws
Exception {
Review Comment:
Could we add a missing-tag case and an `And` case that shouldn't match? With
both matchers always returning true, all 31 tests still passed for me.
##########
hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectHead.java:
##########
@@ -321,6 +332,217 @@ private static Stream<Arguments>
reservedMetadataCollisionCases() {
Arguments.of(HttpHeaders.CONTENT_DISPOSITION,
CONTENT_DISPOSITION_CUSTOM, "inline", "user-disp"));
}
+ @Test
+ public void testHeadObjectIncludesExpirationForDaysRule() throws Exception {
+ String keyName = "logs/app.log";
+ createKey(keyName);
+ setLifecycleRules(daysRule("logs-30d", "logs/", 30));
+
+ Response response = keyEndpoint.head(bucketName, keyName);
+
+ assertEquals(HttpStatus.SC_OK, response.getStatus());
+ ZonedDateTime expiry = expectedDaysExpiry(keyName, 30);
+ assertEquals("expiry-date=\"" + RFC1123Util.FORMAT.format(expiry) + "\",
rule-id=\"logs-30d\"",
+ response.getHeaderString(EXPIRATION_HEADER));
+ }
+
+ @Test
+ public void testHeadObjectIncludesExpirationForDateRule() throws Exception {
+ String keyName = "logs/app.log";
+ createKey(keyName);
+ setLifecycleRules(new OmLCRule.Builder()
+ .setId("logs-date")
+ .setPrefix("logs/")
+ .setEnabled(true)
+ .setAction(new
OmLCExpiration.Builder().setDate("2042-04-02T00:00:00Z").build())
+ .build());
+
+ Response response = keyEndpoint.head(bucketName, keyName);
+
+ assertEquals("expiry-date=\"Wed, 02 Apr 2042 00:00:00 GMT\",
rule-id=\"logs-date\"",
+ response.getHeaderString(EXPIRATION_HEADER));
+ }
+
+ @Test
+ public void testHeadObjectIncludesExpirationForTagFilterRule() throws
Exception {
+ String keyName = "head-tagged";
+
when(headers.getHeaderString(TAG_HEADER)).thenReturn("tag1=value1&tag2=value2");
+ assertSucceeds(() -> put(keyEndpoint, bucketName, keyName, "c"));
+ setLifecycleRules(new OmLCRule.Builder()
+ .setId("tagged-10d")
+ .setEnabled(true)
+ .setFilter(new OmLCFilter.Builder().setTag("tag1", "value1").build())
+ .setAction(new OmLCExpiration.Builder().setDays(10).build())
+ .build());
+
+ Response response = keyEndpoint.head(bucketName, keyName);
+
+ ZonedDateTime expiry = expectedDaysExpiry(keyName, 10);
+ assertEquals("expiry-date=\"" + RFC1123Util.FORMAT.format(expiry) + "\",
rule-id=\"tagged-10d\"",
+ response.getHeaderString(EXPIRATION_HEADER));
+ }
+
+ @Test
+ public void testHeadObjectReportsEarliestExpirationOfMatchingRules() throws
Exception {
+ String keyName = "logs/app.log";
+ createKey(keyName);
+ setLifecycleRules(daysRule("logs-30d", "logs/", 30), daysRule("logs-7d",
"logs/", 7));
Review Comment:
Could `logs-7d` go first, or sit between two longer rules? With `[30d, 7d]`,
a "last matching rule wins" implementation passes too.
##########
hadoop-ozone/s3gateway/src/test/java/org/apache/hadoop/ozone/s3/endpoint/TestObjectHead.java:
##########
@@ -321,6 +332,217 @@ private static Stream<Arguments>
reservedMetadataCollisionCases() {
Arguments.of(HttpHeaders.CONTENT_DISPOSITION,
CONTENT_DISPOSITION_CUSTOM, "inline", "user-disp"));
}
+ @Test
+ public void testHeadObjectIncludesExpirationForDaysRule() throws Exception {
+ String keyName = "logs/app.log";
+ createKey(keyName);
+ setLifecycleRules(daysRule("logs-30d", "logs/", 30));
+
+ Response response = keyEndpoint.head(bucketName, keyName);
+
+ assertEquals(HttpStatus.SC_OK, response.getStatus());
+ ZonedDateTime expiry = expectedDaysExpiry(keyName, 30);
+ assertEquals("expiry-date=\"" + RFC1123Util.FORMAT.format(expiry) + "\",
rule-id=\"logs-30d\"",
+ response.getHeaderString(EXPIRATION_HEADER));
+ }
+
+ @Test
+ public void testHeadObjectIncludesExpirationForDateRule() throws Exception {
+ String keyName = "logs/app.log";
+ createKey(keyName);
+ setLifecycleRules(new OmLCRule.Builder()
+ .setId("logs-date")
+ .setPrefix("logs/")
+ .setEnabled(true)
+ .setAction(new
OmLCExpiration.Builder().setDate("2042-04-02T00:00:00Z").build())
+ .build());
+
+ Response response = keyEndpoint.head(bucketName, keyName);
+
+ assertEquals("expiry-date=\"Wed, 02 Apr 2042 00:00:00 GMT\",
rule-id=\"logs-date\"",
+ response.getHeaderString(EXPIRATION_HEADER));
+ }
+
+ @Test
+ public void testHeadObjectIncludesExpirationForTagFilterRule() throws
Exception {
+ String keyName = "head-tagged";
+
when(headers.getHeaderString(TAG_HEADER)).thenReturn("tag1=value1&tag2=value2");
+ assertSucceeds(() -> put(keyEndpoint, bucketName, keyName, "c"));
+ setLifecycleRules(new OmLCRule.Builder()
+ .setId("tagged-10d")
+ .setEnabled(true)
+ .setFilter(new OmLCFilter.Builder().setTag("tag1", "value1").build())
+ .setAction(new OmLCExpiration.Builder().setDays(10).build())
+ .build());
+
+ Response response = keyEndpoint.head(bucketName, keyName);
+
+ ZonedDateTime expiry = expectedDaysExpiry(keyName, 10);
+ assertEquals("expiry-date=\"" + RFC1123Util.FORMAT.format(expiry) + "\",
rule-id=\"tagged-10d\"",
+ response.getHeaderString(EXPIRATION_HEADER));
+ }
+
+ @Test
+ public void testHeadObjectReportsEarliestExpirationOfMatchingRules() throws
Exception {
+ String keyName = "logs/app.log";
+ createKey(keyName);
+ setLifecycleRules(daysRule("logs-30d", "logs/", 30), daysRule("logs-7d",
"logs/", 7));
+
+ Response response = keyEndpoint.head(bucketName, keyName);
+
+ ZonedDateTime expiry = expectedDaysExpiry(keyName, 7);
+ assertEquals("expiry-date=\"" + RFC1123Util.FORMAT.format(expiry) + "\",
rule-id=\"logs-7d\"",
+ response.getHeaderString(EXPIRATION_HEADER));
+ }
+
+ @Test
+ public void testHeadObjectOmitsExpirationWhenNoRuleMatches() throws
Exception {
+ String keyName = "data/app.log";
+ createKey(keyName);
+ setLifecycleRules(daysRule("logs-30d", "logs/", 30));
+
+ Response response = keyEndpoint.head(bucketName, keyName);
+
+ assertEquals(HttpStatus.SC_OK, response.getStatus());
+ assertNull(response.getHeaderString(EXPIRATION_HEADER));
+ }
+
+ @Test
+ public void testHeadObjectOmitsExpirationForDisabledRule() throws Exception {
+ String keyName = "logs/app.log";
+ createKey(keyName);
+ setLifecycleRules(new OmLCRule.Builder()
+ .setId("logs-30d")
+ .setPrefix("logs/")
+ .setEnabled(false)
+ .setAction(new OmLCExpiration.Builder().setDays(30).build())
+ .build());
+
+ Response response = keyEndpoint.head(bucketName, keyName);
+
+ assertNull(response.getHeaderString(EXPIRATION_HEADER));
+ }
+
+ @Test
+ public void
testHeadObjectOmitsExpirationForAbortIncompleteMultipartUploadRule() throws
Exception {
+ String keyName = "uploads/part";
+ createKey(keyName);
+ setLifecycleRules(new OmLCRule.Builder()
+ .setId("abort-7d")
+ .setPrefix("uploads/")
+ .setEnabled(true)
+ .setAction(new
OmLCAbortIncompleteMultipartUpload.Builder().setDaysAfterInitiation(7).build())
+ .build());
+
+ Response response = keyEndpoint.head(bucketName, keyName);
+
+ assertNull(response.getHeaderString(EXPIRATION_HEADER));
+ }
+
+ @Test
+ public void testHeadObjectEscapesQuoteInRuleId() throws Exception {
+ String keyName = "logs/app.log";
+ createKey(keyName);
+ setLifecycleRules(daysRule("we\"ird", "logs/", 30));
+
+ Response response = keyEndpoint.head(bucketName, keyName);
+
+ ZonedDateTime expiry = expectedDaysExpiry(keyName, 30);
+ assertEquals("expiry-date=\"" + RFC1123Util.FORMAT.format(expiry) + "\",
rule-id=\"we\\\"ird\"",
+ response.getHeaderString(EXPIRATION_HEADER));
+ }
+
+ @Test
+ public void testHeadObjectOmitsExpirationForRuleIdWithControlCharacter()
throws Exception {
+ String keyName = "logs/app.log";
+ createKey(keyName);
+ // A CR/LF cannot be represented in a header value, so the header is
dropped
+ // rather than emitted malformed.
+ setLifecycleRules(daysRule("bad\r\nid", "logs/", 30));
+
+ Response response = keyEndpoint.head(bucketName, keyName);
+
+ assertEquals(HttpStatus.SC_OK, response.getStatus());
+ assertNull(response.getHeaderString(EXPIRATION_HEADER));
+ }
+
+ @Test
+ public void testHeadObjectRemembersAbsentLifecycleConfiguration() throws
Exception {
+ String keyName = "logs/app.log";
+ createKey(keyName);
+
+ // The bucket has no configuration, so the lookup fails and is remembered.
+ assertNull(keyEndpoint.head(bucketName, keyName)
+ .getHeaderString(EXPIRATION_HEADER));
+
+ setLifecycleRules(daysRule("logs-30d", "logs/", 30));
+
+ // Within the TTL the gateway does not ask OM again, so the rule added in
the
+ // meantime is not reported yet. That bounded staleness is the contract
here.
+ assertNull(keyEndpoint.head(bucketName, keyName)
+ .getHeaderString(EXPIRATION_HEADER));
+ }
+
+ @Test
+ public void testHeadObjectAlwaysLooksUpLifecycleWhenCacheDisabled() throws
Exception {
+ OzoneConfiguration conf = new OzoneConfiguration();
+ conf.set(OZONE_S3G_LIFECYCLE_MISSING_CONFIGURATION_CACHE_TTL, "0s");
+ keyEndpoint = EndpointBuilder.newObjectEndpointBuilder()
+ .setClient(clientStub)
+ .setHeaders(headers)
+ .setConfig(conf)
+ .build();
+
+ String keyName = "logs/app.log";
+ createKey(keyName);
+ assertNull(keyEndpoint.head(bucketName, keyName)
+ .getHeaderString(EXPIRATION_HEADER));
+
+ setLifecycleRules(daysRule("logs-30d", "logs/", 30));
+
+ // With the cache off every request looks the configuration up again, so
the
+ // rule is reported straight away.
+ assertNotNull(keyEndpoint.head(bucketName, keyName)
+ .getHeaderString(EXPIRATION_HEADER));
+ }
+
+ @Test
+ public void testHeadObjectOmitsExpirationWithoutLifecycleConfiguration()
throws Exception {
+ String keyName = "logs/app.log";
+ createKey(keyName);
+
+ Response response = keyEndpoint.head(bucketName, keyName);
+
+ assertEquals(HttpStatus.SC_OK, response.getStatus());
+ assertNull(response.getHeaderString(EXPIRATION_HEADER));
+ }
+
+ private void setLifecycleRules(OmLCRule... rules) throws IOException {
+ bucket.setLifecycleConfiguration(new OmLifecycleConfiguration.Builder()
+ .setVolume(bucket.getVolumeName())
+ .setBucket(bucketName)
+ .setBucketLayout(BucketLayout.OBJECT_STORE)
+ .setCreationTime(Instant.now().toEpochMilli())
+ .setRules(Arrays.asList(rules))
+ .build());
+ }
+
+ private static OmLCRule daysRule(String id, String prefix, int days) {
+ return new OmLCRule.Builder()
+ .setId(id)
+ .setPrefix(prefix)
+ .setEnabled(true)
+ .setAction(new OmLCExpiration.Builder().setDays(days).build())
+ .build();
+ }
+
+ /** S3 reports modification time plus Days, rounded up to the next midnight
UTC. */
+ private ZonedDateTime expectedDaysExpiry(String keyName, int days) throws
IOException {
Review Comment:
Could a `Days` test assert a literal date from a fixed modification time?
`expectedDaysExpiry` repeats the production formula, so the rounding is only
checked against itself.
--
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]