chungen0126 commented on code in PR #11293: URL: https://github.com/apache/ozone/pull/11293#discussion_r4722808089
########## hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/helpers/RetentionUtil.java: ########## @@ -0,0 +1,147 @@ +/* + * 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.om.helpers; + +import java.time.DateTimeException; +import java.time.Instant; +import java.time.ZoneOffset; +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.RetentionTimeUnit; + +/** + * Validation and UTC date calculations for retention rules. + */ +public final class RetentionUtil { + private static final long MAX_EVENT_HOLD_YEARS = 100; + private static final long DAYS_PER_YEAR = 365; + private static final long MAX_EVENT_HOLD_DAYS = MAX_EVENT_HOLD_YEARS * DAYS_PER_YEAR; + + private RetentionUtil() { + } + + /** + * Validate bucket retention submitted by a write request at the given time in epoch milliseconds. + */ + public static void validateBucketRetention(BucketRetention retention, long currentTime) { + RetentionDuration duration = retention.getDuration(); + RetentionDuration eventHold = retention.getDefaultEventHold(); + if (duration == null && eventHold == null) { + throw new IllegalArgumentException("Default retention requires a duration or event hold"); + } + if (duration != null) { + calculateRetainUntilDate(duration, currentTime); + } + if (eventHold != null) { + validateEventHoldRule(eventHold); + if (duration != null && exceedsDefaultDuration(eventHold, duration)) { + throw new IllegalArgumentException("Event hold duration must not exceed default retention duration"); + } + } + } + + private static boolean exceedsDefaultDuration(RetentionDuration eventHold, RetentionDuration duration) { + long hold = eventHold.getDuration(); + long defaultDuration = duration.getDuration(); + if (eventHold.getTimeUnit() == duration.getTimeUnit()) { + return hold > defaultDuration; + } + if (eventHold.getTimeUnit() == RetentionTimeUnit.YEARS) { + // At most one leap day per four years, rounded up for the longest UTC interval. + return hold * DAYS_PER_YEAR + (hold + 3) / 4 > defaultDuration; + } + // A default of 100 years already covers any hold in days. Capping also avoids overflow. + long years = Math.min(defaultDuration, MAX_EVENT_HOLD_YEARS); + // The shortest interval can lose one leap day at a non-leap century. + return hold > years * DAYS_PER_YEAR + Math.max(0, years / 4 - 1); + } + + /** + * Validate retention submitted by a write request at the given time in epoch milliseconds. + */ + public static void validateObjectRetention(ObjectRetention retention, long currentTime) { + long retentionDate = retention.getRetentionDate(); + EventHold eventHold = retention.getEventHold(); + if (retentionDate == 0 && eventHold == null) { + throw new IllegalArgumentException("Retention requires an expiration time or event hold"); + } + if (retentionDate != 0) { + validateRetainUntilDate(retentionDate, currentTime); + } + if (eventHold != null) { + validateEventHoldRule(eventHold.getDuration()); + if (eventHold.getEnabled() && retentionDate != 0 + && retentionDate < calculateRetainUntilDate(eventHold.getDuration(), currentTime)) { + throw new IllegalArgumentException("Retention expiration must be at least the event hold duration from now"); + } + } + } + + private static void validateRule(RetentionDuration rule) { + if (rule == null || rule.getDuration() <= 0) { + throw new IllegalArgumentException("Retention rule requires a time unit and positive signed-long duration"); + } + } + + private static void validateEventHoldRule(RetentionDuration rule) { Review Comment: There are four cases for `EventHold`, and currently only one of them is handled correctly. After making `RetentionDuration` optional, the following three cases need to be fixed: - EventHold(false, null): currently cannot be constructed; should be accepted. - EventHold(false, 30 days): currently accepted; should be rejected. - EventHold(true, null): currently cannot be constructed; should be rejected. -- 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]
