HsbcJone commented on code in PR #1386:
URL: https://github.com/apache/flink-agents/pull/1386#discussion_r4613993335


##########
api/src/main/java/org/apache/flink/agents/api/configuration/DurationFormat.java:
##########
@@ -0,0 +1,316 @@
+/*
+ * 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.flink.agents.api.configuration;
+
+import org.apache.flink.agents.api.agents.AgentExecutionOptions;
+
+import java.math.BigInteger;
+import java.time.Duration;
+import java.time.temporal.ChronoUnit;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Locale;
+import java.util.Map;
+
+/**
+ * Parses and formats framework durations in the style of Flink's duration 
configuration.
+ *
+ * <p>A value is an integer and one unit, optionally separated by whitespace, 
such as {@code 30 s}
+ * or {@code 5 min}. A leading {@code -} is allowed. A unit is required; a 
bare number is rejected.
+ * Recognized units, case-insensitively, are {@code ns}, {@code us}/{@code 
µs}, {@code ms}, {@code
+ * s}, {@code min}/{@code m}, {@code h}, and {@code d}, plus the longer 
singular and plural forms
+ * ({@code second}, {@code seconds}, {@code millisecond}, and so on). {@code 
m} means minutes.
+ *
+ * <p>The value must be a whole number of microseconds, because the Python 
counterpart is {@code
+ * datetime.timedelta}. {@code 1000 ns} is one microsecond; {@code 1 ns} is 
rejected. This parser
+ * does not truncate to a coarser unit. A consumer that cannot honor 
microseconds rejects the value
+ * when the option is read.
+ */
+public final class DurationFormat {
+
+    private static final BigInteger NANOS_PER_SECOND = 
BigInteger.valueOf(1_000_000_000L);
+    private static final BigInteger NANOS_PER_MICRO = 
BigInteger.valueOf(1_000L);
+
+    private static final Map<String, ChronoUnit> UNITS = units();
+
+    private static final ChronoUnit[] FORMAT_UNITS = {
+        ChronoUnit.DAYS,
+        ChronoUnit.HOURS,
+        ChronoUnit.MINUTES,
+        ChronoUnit.SECONDS,
+        ChronoUnit.MILLIS,
+        ChronoUnit.MICROS,
+        ChronoUnit.NANOS
+    };
+
+    private static final String[] FORMAT_LABELS = {"d", "h", "min", "s", "ms", 
"us", "ns"};
+
+    private DurationFormat() {}
+
+    /**
+     * Parses a duration string or returns a {@link Duration} unchanged after 
the same alignment
+     * check.
+     */
+    public static Duration parse(Object value) {
+        if (value instanceof Duration) {
+            return requireMicrosecondAligned((Duration) value, 
value.toString());
+        }
+        if (value instanceof CharSequence) {
+            return parse(value.toString());
+        }
+        throw invalid(
+                String.valueOf(value),
+                "a duration needs a number and a unit, for example '30 s' or 
'5 min',"
+                        + " not a bare number");
+    }
+
+    /** Parses one duration string. */
+    public static Duration parse(String text) {
+        if (text == null) {
+            throw invalid("null", "a duration needs a number and a unit, for 
example '30 s'");
+        }
+        String trimmed = text.trim();
+        if (trimmed.isEmpty()) {
+            throw invalid(text, "a duration needs a number and a unit, for 
example '30 s'");
+        }
+
+        boolean negative = false;
+        int index = 0;
+        if (trimmed.charAt(0) == '-') {
+            negative = true;
+            index++;
+            while (index < trimmed.length() && trimmed.charAt(index) == ' ') {
+                index++;
+            }
+        }
+        int numberEnd = index;
+        while (numberEnd < trimmed.length() && 
isDigit(trimmed.charAt(numberEnd))) {
+            numberEnd++;
+        }
+        if (numberEnd == index) {
+            throw invalid(
+                    text, "a duration needs an integer and a unit, for example 
'30 s' or '5 min'");
+        }
+        String number = trimmed.substring(index, numberEnd);
+        String unitLabel = 
trimmed.substring(numberEnd).trim().toLowerCase(Locale.US);
+        if (unitLabel.startsWith(".")) {
+            throw invalid(text, "a duration needs an integer and a unit, for 
example '1500 ms'");
+        }
+        if (unitLabel.isEmpty()) {
+            throw invalid(
+                    text,
+                    "a duration needs a unit, for example '30 s' or '30 ms', 
not a bare number");
+        }
+        ChronoUnit unit = UNITS.get(unitLabel);
+        if (unit == null) {
+            throw invalid(
+                    text,
+                    "time unit '"
+                            + unitLabel
+                            + "' is not recognized. Use ns, us, ms, s, min, h, 
or d");
+        }
+
+        final BigInteger magnitude;
+        try {
+            magnitude = new BigInteger(number);
+        } catch (NumberFormatException e) {
+            throw invalid(text, "the number cannot be represented as an 
integer");
+        }
+        BigInteger nanos = 
magnitude.multiply(BigInteger.valueOf(unit.getDuration().toNanos()));
+        if (nanos.mod(NANOS_PER_MICRO).signum() != 0) {
+            throw invalid(text, "durations finer than 1 us are not supported");
+        }
+        if (negative) {
+            nanos = nanos.negate();
+        }
+        try {
+            return durationFromNanos(nanos);
+        } catch (ArithmeticException e) {
+            throw invalid(text, "the value overflows a duration");
+        }
+    }
+
+    /**
+     * Formats a duration with the largest unit that does not lose precision, 
for example {@code 60
+     * s} as {@code 1 min}. Zero is {@code 0 ms}.
+     */
+    public static String format(Duration duration) {
+        if (duration.isZero()) {
+            return "0 ms";
+        }
+        if (duration.isNegative()) {
+            return "-" + format(duration.negated());
+        }
+        BigInteger nanos = toNanos(duration);
+        for (int i = 0; i < FORMAT_UNITS.length; i++) {
+            BigInteger unitNanos = 
BigInteger.valueOf(FORMAT_UNITS[i].getDuration().toNanos());
+            if (nanos.mod(unitNanos).signum() == 0) {
+                return nanos.divide(unitNanos).toString() + " " + 
FORMAT_LABELS[i];
+            }
+        }
+        throw new IllegalArgumentException("Cannot format duration " + 
duration);
+    }
+
+    /** True when the duration is non-null and strictly greater than zero. */
+    public static boolean isPositive(Duration duration) {
+        return duration != null && !duration.isZero() && 
!duration.isNegative();
+    }
+
+    /** Returns {@link Duration#ZERO} when {@code duration} is null or 
negative. */
+    public static Duration zeroIfNegative(Duration duration) {
+        if (duration == null || duration.isNegative()) {
+            return Duration.ZERO;
+        }
+        return duration;
+    }
+
+    /**
+     * Nanoseconds of a positive duration. Null, zero, and negative return 
{@code 0}. A positive
+     * duration that does not fit in a {@code long} throws {@link 
ArithmeticException}.
+     */
+    public static long positiveNanos(Duration duration) {
+        if (!isPositive(duration)) {
+            return 0L;
+        }
+        return duration.toNanos();
+    }
+
+    /**
+     * Whole milliseconds recorded for one retry sleep. This is the same count 
{@link
+     * Thread#sleep(long)} waits and the value added to {@code retryWaitMs} 
and {@code
+     * total_retry_wait_ms}. A non-positive duration contributes zero. A 
sub-millisecond remainder
+     * is dropped, matching {@code Duration#toMillis()}; retry wait rejects 
that remainder before
+     * the sleep. A duration that does not fit in a {@code long} throws {@link 
ArithmeticException}.
+     */
+    public static long recordedRetryWaitMillis(Duration duration) {
+        if (!isPositive(duration)) {
+            return 0L;
+        }
+        long millis = duration.toMillis();
+        return Math.max(0L, millis);
+    }
+
+    /**
+     * Applies the precision rule of a duration option. Retry wait and 
short-term memory TTL must be
+     * a whole number of milliseconds when positive. Other durations, 
including the async batch
+     * timeout, keep the parsed microsecond value.
+     */
+    public static Duration validateConsumedDuration(String optionKey, Duration 
duration) {

Review Comment:
   Thanks. I moved the retry-wait and short-term memory TTL checks out of 
`AgentConfiguration.get`/`set` into `validateExecutionOptions`. 
`CompileUtils.connectToAgent` calls it before constructing the operator 
factory, and the Python plan-JSON overload uses that same method. There is no 
separate Python local submit path.
   
   Both options now use `Duration.toMillis()`. A non-positive retry wait does 
not wait. A positive retry wait is truncated toward zero, including a result of 
zero, so `1500 us` waits 1 ms and `500 us` does not wait. A non-positive TTL 
disables expiration. A positive TTL must convert to more than 0 ms, so `500 us` 
fails at submission and `1500 us` enables a 1 ms TTL. Overflow from 
`toMillis()` also fails at submission. `OperatorStateManager` uses the 
truncated milliseconds and no longer has its own TTL check. Java and Python 
chat and decision retries sleep and record that same truncated value. 
`async.batch.timeout` is unchanged and still keeps the full microsecond 
deadline.



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

Reply via email to