xxubai commented on code in PR #4254:
URL: https://github.com/apache/amoro/pull/4254#discussion_r3472279271


##########
amoro-ams/src/main/java/org/apache/amoro/server/optimizing/dra/DynamicAllocationConfig.java:
##########
@@ -0,0 +1,362 @@
+/*
+ * 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.amoro.server.optimizing.dra;
+
+import org.apache.amoro.Constants;
+import org.apache.amoro.OptimizerProperties;
+import org.apache.amoro.config.ConfigHelpers;
+import org.apache.amoro.resource.ResourceGroup;
+import org.apache.amoro.utils.PropertyUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.time.Duration;
+import java.util.Map;
+
+/**
+ * Dynamic resource allocation (DRA) configuration of a resource group 
(AIP-5). Parsed from the
+ * group's properties; {@link #validate()} enforces the AIP-5 constraints.
+ */
+public class DynamicAllocationConfig {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(DynamicAllocationConfig.class);
+
+  private final String groupName;
+  private final String container;
+  private final boolean enabled;
+  private final int minParallelism;
+  private final String minParallelismRaw;
+  private final Integer maxParallelism;
+  private final Duration schedulerBacklogTimeout;
+  private final Duration sustainedBacklogTimeout;
+  private final Duration executorIdleTimeout;
+  private final Duration scaleDownCooldown;
+  private final Duration drainTimeout;
+
+  private DynamicAllocationConfig(
+      String groupName,
+      String container,
+      boolean enabled,
+      int minParallelism,
+      String minParallelismRaw,
+      Integer maxParallelism,
+      Duration schedulerBacklogTimeout,
+      Duration sustainedBacklogTimeout,
+      Duration executorIdleTimeout,
+      Duration scaleDownCooldown,
+      Duration drainTimeout) {
+    this.groupName = groupName;
+    this.container = container;
+    this.enabled = enabled;
+    this.minParallelism = minParallelism;
+    this.minParallelismRaw = minParallelismRaw;
+    this.maxParallelism = maxParallelism;
+    this.schedulerBacklogTimeout = schedulerBacklogTimeout;
+    this.sustainedBacklogTimeout = sustainedBacklogTimeout;
+    this.executorIdleTimeout = executorIdleTimeout;
+    this.scaleDownCooldown = scaleDownCooldown;
+    this.drainTimeout = drainTimeout;
+  }
+
+  /**
+   * Parse the DRA configuration of a resource group. Malformed numeric or 
duration values throw
+   * {@link IllegalArgumentException}; semantic constraints are checked by 
{@link #validate()}.
+   */
+  public static DynamicAllocationConfig parse(ResourceGroup group) {
+    Map<String, String> properties = group.getProperties();
+    boolean enabled =
+        PropertyUtil.propertyAsBoolean(
+            properties,
+            OptimizerProperties.DYNAMIC_ALLOCATION_ENABLED,
+            OptimizerProperties.DYNAMIC_ALLOCATION_ENABLED_DEFAULT);
+    int minParallelism = resolveMinParallelism(group);
+    String minParallelismRaw = rawMinParallelism(group);
+
+    Integer maxParallelism =
+        PropertyUtil.propertyAsNullableInt(
+            properties, 
OptimizerProperties.DYNAMIC_ALLOCATION_MAX_PARALLELISM);
+
+    return new DynamicAllocationConfig(
+        group.getName(),
+        group.getContainer(),
+        enabled,
+        minParallelism,
+        minParallelismRaw,
+        maxParallelism,
+        parseDuration(
+            properties,
+            OptimizerProperties.DYNAMIC_ALLOCATION_SCHEDULER_BACKLOG_TIMEOUT,
+            
OptimizerProperties.DYNAMIC_ALLOCATION_SCHEDULER_BACKLOG_TIMEOUT_DEFAULT),
+        parseDuration(
+            properties,
+            OptimizerProperties.DYNAMIC_ALLOCATION_SUSTAINED_BACKLOG_TIMEOUT,
+            
OptimizerProperties.DYNAMIC_ALLOCATION_SUSTAINED_BACKLOG_TIMEOUT_DEFAULT),
+        parseDuration(
+            properties,
+            OptimizerProperties.DYNAMIC_ALLOCATION_EXECUTOR_IDLE_TIMEOUT,
+            
OptimizerProperties.DYNAMIC_ALLOCATION_EXECUTOR_IDLE_TIMEOUT_DEFAULT),
+        parseDuration(
+            properties,
+            OptimizerProperties.DYNAMIC_ALLOCATION_SCALE_DOWN_COOLDOWN,
+            
OptimizerProperties.DYNAMIC_ALLOCATION_SCALE_DOWN_COOLDOWN_DEFAULT),
+        parseDuration(
+            properties,
+            OptimizerProperties.DYNAMIC_ALLOCATION_DRAIN_TIMEOUT,
+            OptimizerProperties.DYNAMIC_ALLOCATION_DRAIN_TIMEOUT_DEFAULT));
+  }
+
+  /**
+   * Resolve the effective min-parallelism of a group, honoring the deprecated 
flat {@code
+   * min-parallelism} as a fallback. Resolution order: {@link
+   * OptimizerProperties#DYNAMIC_ALLOCATION_MIN_PARALLELISM} → {@link
+   * OptimizerProperties#OPTIMIZER_GROUP_MIN_PARALLELISM} → {@code 0}. 
Lenient: an unparsable value
+   * falls back to {@code 0} rather than throwing, preserving legacy behavior. 
This is on the keeper
+   * hot path and therefore stays silent; deprecation is reported by {@link
+   * #warnDeprecatedMinParallelism(ResourceGroup)} at config-entry points 
instead.
+   */
+  public static int resolveMinParallelism(ResourceGroup group) {
+    Map<String, String> properties = group.getProperties();
+    String namespaced = 
properties.get(OptimizerProperties.DYNAMIC_ALLOCATION_MIN_PARALLELISM);
+    if (namespaced != null) {
+      return parseIntOrDefault(group.getName(), namespaced);
+    }
+    String legacy = 
properties.get(OptimizerProperties.OPTIMIZER_GROUP_MIN_PARALLELISM);
+    if (legacy != null) {
+      return parseIntOrDefault(group.getName(), legacy);
+    }
+    return OptimizerProperties.DYNAMIC_ALLOCATION_MIN_PARALLELISM_DEFAULT;
+  }
+
+  /**
+   * The raw, unparsed min-parallelism string the group relies on, following 
the same resolution
+   * order as {@link #resolveMinParallelism(ResourceGroup)} ({@link
+   * OptimizerProperties#DYNAMIC_ALLOCATION_MIN_PARALLELISM} → {@link
+   * OptimizerProperties#OPTIMIZER_GROUP_MIN_PARALLELISM}), or {@code null} 
when neither is set.
+   * Retained so {@link #validate()} can distinguish "explicitly configured 
but unparsable" from
+   * "unset", which the lenient resolved {@code int} collapses to the same 
{@code 0}.
+   */
+  private static String rawMinParallelism(ResourceGroup group) {
+    Map<String, String> properties = group.getProperties();
+    String namespaced = 
properties.get(OptimizerProperties.DYNAMIC_ALLOCATION_MIN_PARALLELISM);
+    if (namespaced != null) {
+      return namespaced;
+    }
+    return properties.get(OptimizerProperties.OPTIMIZER_GROUP_MIN_PARALLELISM);
+  }
+
+  /**
+   * Log a one-off deprecation warning when a group still relies on the flat 
{@code
+   * min-parallelism}. Intended for config-entry points (startup load, REST 
create/update), not the
+   * keeper hot path.
+   */
+  public static void warnDeprecatedMinParallelism(ResourceGroup group) {

Review Comment:
   `warnDeprecatedMinParallelism` can be included in `parse`



##########
amoro-ams/src/main/java/org/apache/amoro/server/DefaultOptimizingService.java:
##########
@@ -181,6 +182,18 @@ private void 
loadOptimizingQueues(List<DefaultTableRuntime> tableRuntimeList) {
     optimizerGroups.forEach(
         group -> {
           String groupName = group.getName();
+          // Fail-safe: a persisted group carrying an invalid DRA config (e.g. 
manual DB edits)
+          // must not crash AMS. Surface it and fall back to DRA-disabled 
behavior.

Review Comment:
   Do you have a more implicit way to disable it? It doesn’t seem appropriate 
to validate the group's configuration when loading optimizing queues.



##########
amoro-ams/src/main/java/org/apache/amoro/server/optimizing/dra/DynamicAllocationConfig.java:
##########
@@ -0,0 +1,362 @@
+/*
+ * 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.amoro.server.optimizing.dra;
+
+import org.apache.amoro.Constants;
+import org.apache.amoro.OptimizerProperties;
+import org.apache.amoro.config.ConfigHelpers;
+import org.apache.amoro.resource.ResourceGroup;
+import org.apache.amoro.utils.PropertyUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.time.Duration;
+import java.util.Map;
+
+/**
+ * Dynamic resource allocation (DRA) configuration of a resource group 
(AIP-5). Parsed from the
+ * group's properties; {@link #validate()} enforces the AIP-5 constraints.
+ */
+public class DynamicAllocationConfig {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(DynamicAllocationConfig.class);
+
+  private final String groupName;
+  private final String container;
+  private final boolean enabled;
+  private final int minParallelism;
+  private final String minParallelismRaw;
+  private final Integer maxParallelism;
+  private final Duration schedulerBacklogTimeout;
+  private final Duration sustainedBacklogTimeout;
+  private final Duration executorIdleTimeout;
+  private final Duration scaleDownCooldown;
+  private final Duration drainTimeout;
+
+  private DynamicAllocationConfig(
+      String groupName,
+      String container,
+      boolean enabled,
+      int minParallelism,
+      String minParallelismRaw,
+      Integer maxParallelism,
+      Duration schedulerBacklogTimeout,
+      Duration sustainedBacklogTimeout,
+      Duration executorIdleTimeout,
+      Duration scaleDownCooldown,
+      Duration drainTimeout) {
+    this.groupName = groupName;
+    this.container = container;
+    this.enabled = enabled;
+    this.minParallelism = minParallelism;
+    this.minParallelismRaw = minParallelismRaw;
+    this.maxParallelism = maxParallelism;
+    this.schedulerBacklogTimeout = schedulerBacklogTimeout;
+    this.sustainedBacklogTimeout = sustainedBacklogTimeout;
+    this.executorIdleTimeout = executorIdleTimeout;
+    this.scaleDownCooldown = scaleDownCooldown;
+    this.drainTimeout = drainTimeout;
+  }
+
+  /**
+   * Parse the DRA configuration of a resource group. Malformed numeric or 
duration values throw
+   * {@link IllegalArgumentException}; semantic constraints are checked by 
{@link #validate()}.
+   */
+  public static DynamicAllocationConfig parse(ResourceGroup group) {
+    Map<String, String> properties = group.getProperties();
+    boolean enabled =
+        PropertyUtil.propertyAsBoolean(
+            properties,
+            OptimizerProperties.DYNAMIC_ALLOCATION_ENABLED,
+            OptimizerProperties.DYNAMIC_ALLOCATION_ENABLED_DEFAULT);
+    int minParallelism = resolveMinParallelism(group);
+    String minParallelismRaw = rawMinParallelism(group);

Review Comment:
   Why need additional raw `min-parallelism`



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