desaikomal commented on code in PR #2649:
URL: https://github.com/apache/helix/pull/2649#discussion_r1359505103


##########
helix-core/src/main/java/org/apache/helix/controller/rebalancer/waged/WagedInstanceCapacity.java:
##########
@@ -215,7 +221,7 @@ public synchronized boolean 
checkAndReduceInstanceCapacity(String instance, Stri
     }
     _allocatedPartitionsMap.computeIfAbsent(instance, k -> new HashMap<>())
         .computeIfAbsent(resName, k -> new HashSet<>()).add(partitionName);
-    LOG.info("Reduced capacity for instance: " + instance + " for resource: " 
+ resName
+    LOG.debug("Reduced capacity for instance: " + instance + " for resource: " 
+ resName

Review Comment:
   great ... 



##########
helix-core/src/main/java/org/apache/helix/controller/rebalancer/waged/WagedInstanceCapacity.java:
##########
@@ -130,28 +136,29 @@ public void 
processPendingMessages(ResourceControllerDataProvider cache,
     }
   }
 
-
-  private void processCurrentState(ResourceControllerDataProvider cache,
-      CurrentStateOutput currentStateOutput, Map<String, Resource> resourceMap,
-      WagedResourceWeightsProvider weightProvider) {
+  void processCurrentState(ResourceControllerDataProvider cache,
+      CurrentStateOutput currentStateOutput, Map<String, Resource> 
resourceMap) {
 
     // Iterate through all the resources
     for (Map.Entry<String, Resource> entry : resourceMap.entrySet()) {
       String resName = entry.getKey();
       Resource resource = entry.getValue();
 
-      // if Resource is WAGED managed, then we need to manage the capacity.
-      if (!WagedValidationUtil.isWagedEnabled(cache.getIdealState(resName))) {
-        continue;
-      }
-
       // list of partitions in the resource
       Collection<Partition> partitions = resource.getPartitions();
 
+      // calculate merge resource-config one for each resource.
+      ResourceConfig resourceConfig = cache.getResourceConfig(resName);
+      IdealState is = cache.getIdealState(resName);
+      ResourceConfig mergedResourceConfig =
+          ResourceConfig.mergeIdealStateWithResourceConfig(resourceConfig, is);
+
       for (Partition partition : partitions) {
         String partitionName = partition.getPartitionName();
         // Get Partition Weight
-        Map<String, Integer> partCapacity = 
weightProvider.getPartitionWeights(resName, partitionName);
+        Map<String, Integer> partCapacity =
+            WagedRebalanceUtil.fetchCapacityUsage(partitionName, 
mergedResourceConfig, cache.getClusterConfig());

Review Comment:
   you can check it in next change. Also based on Junkai comment, please 
evaulate if we even need it?



##########
helix-core/src/main/java/org/apache/helix/controller/rebalancer/waged/WagedInstanceCapacityManager.java:
##########
@@ -0,0 +1,119 @@
+package org.apache.helix.controller.rebalancer.waged;
+
+/*
+ * 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.
+ */
+
+import java.util.Map;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import org.apache.helix.controller.dataproviders.BaseControllerDataProvider;
+import 
org.apache.helix.controller.dataproviders.ResourceControllerDataProvider;
+import org.apache.helix.controller.rebalancer.util.WagedValidationUtil;
+import org.apache.helix.controller.stages.AttributeName;
+import org.apache.helix.controller.stages.ClusterEvent;
+import org.apache.helix.controller.stages.CurrentStateOutput;
+import org.apache.helix.model.Resource;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Manager class responsible for abstracting the management operations on the 
{@link WagedInstanceCapacity}
+ * cache such as rebuild and update operations on various scenarios.
+ */
+public class WagedInstanceCapacityManager {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(WagedInstanceCapacityManager.class);
+
+  private static class SingletonHelper {
+    private static final WagedInstanceCapacityManager INSTANCE = new 
WagedInstanceCapacityManager();
+  }
+
+  public static WagedInstanceCapacityManager getInstance() {
+    return SingletonHelper.INSTANCE;
+  }
+
+  public void processEvent(ClusterEvent event, CurrentStateOutput 
currentStateOutput) {
+    BaseControllerDataProvider cache = 
event.getAttribute(AttributeName.ControllerDataProvider.name());
+    final Map<String, Resource> resourceMap = 
event.getAttribute(AttributeName.RESOURCES.name());
+
+    if (shouldNoOp(cache, resourceMap, event)) {
+      return;
+    }
+
+    ResourceControllerDataProvider dataProvider = 
(ResourceControllerDataProvider) cache;
+    Map<String, Resource> wagedEnabledResourceMap = resourceMap.entrySet()
+        .stream()
+        .filter(entry -> 
WagedValidationUtil.isWagedEnabled(cache.getIdealState(entry.getKey())))
+        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
+
+    if (wagedEnabledResourceMap.isEmpty()) {
+      return;
+    }
+
+    // Phase 1: Rebuild Always
+    // DONE: we only need to compute when there are resource using Waged. We 
should
+    // do this as perf improvement in the future.
+    WagedInstanceCapacity capacityProvider = new 
WagedInstanceCapacity(dataProvider);
+    WagedResourceWeightsProvider weightProvider = new 
WagedResourceWeightsProvider(dataProvider);
+
+    // Process the currentState and update the available instance capacity.
+    capacityProvider.process(dataProvider, currentStateOutput, 
wagedEnabledResourceMap, weightProvider);
+    dataProvider.setWagedCapacityProviders(capacityProvider, weightProvider);
+  }
+
+  /**
+   * Function that checks whether we should return early, without any action 
on the capacity map or not.
+   * @param cache it is the cluster level cache for the resources.
+   * @param event the cluster event that is undergoing processing.
+   * @return true, of the condition evaluate to true and no action is needed, 
else false.
+   */
+  static boolean shouldNoOp(BaseControllerDataProvider cache, Map<String, 
Resource> resourceMap, ClusterEvent event) {
+    if (!(cache instanceof ResourceControllerDataProvider)) {
+      return true;
+    }
+
+    if (resourceMap == null || resourceMap.isEmpty()) {
+      return true;
+    }
+
+    ResourceControllerDataProvider dataProvider = 
(ResourceControllerDataProvider) cache;
+    if (Objects.isNull(dataProvider.getWagedInstanceCapacity())) {
+      return false;
+    }
+
+    switch (event.getEventType()) {

Review Comment:
   essentially, we need to recompute only for few events, so may be having 
other way check may be good



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