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


##########
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:
   This almost cover every thing... We don't need to have such filtering.



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

Review Comment:
   Let's try to minimize class creation. I think we can have the Wagedcapacity 
in the cache. We don't have to build such manager.



##########
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);

Review Comment:
   Why we merge here... Original design is because some config in IS should be 
stay with Resource Config. But even there is IS change event, I don't think we 
need to merge any change from IS to resource config. I don't get the merge 
operation here...



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