You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@helix.apache.org by GitBox <gi...@apache.org> on 2019/08/07 20:02:30 UTC

[GitHub] [helix] narendly commented on a change in pull request #381: Implement the POC work greedy constraint based algorithm

narendly commented on a change in pull request #381: Implement the POC work greedy constraint based algorithm
URL: https://github.com/apache/helix/pull/381#discussion_r311731548
 
 

 ##########
 File path: helix-core/src/main/java/org/apache/helix/controller/rebalancer/waged/algorithm/ConstraintBasedAlgorithm.java
 ##########
 @@ -0,0 +1,109 @@
+package org.apache.helix.controller.rebalancer.waged.algorithm;
+
+import org.apache.helix.controller.rebalancer.waged.constraints.HardConstraint;
+import org.apache.helix.controller.rebalancer.waged.constraints.SoftConstraint;
+import org.apache.helix.controller.rebalancer.waged.model.AssignableNode;
+import org.apache.helix.controller.rebalancer.waged.model.AssignableReplica;
+import org.apache.helix.controller.rebalancer.waged.model.ClusterContext;
+import org.apache.helix.controller.rebalancer.waged.model.ClusterModel;
+import org.apache.helix.controller.rebalancer.waged.model.OptimalAssignment;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+
+/**
+ * The algorithm is based on a given set of constraints
+ * - HardConstraint: Approve or deny the assignment given its condition, any assignment cannot bypass any "hard constraint"
+ * - SoftConstraint: Evaluate the assignment by points/rewards/scores, a higher point means a better assignment
+ * <p>
+ * The goal is to avoid all "hard constraints" while accumulating the most points(rewards) from "soft constraints"
+ */
+public class ConstraintBasedAlgorithm implements RebalanceAlgorithm {
+    private static final Logger LOG = LoggerFactory.getLogger(ConstraintBasedAlgorithm.class);
+    private final List<HardConstraint> _hardConstraints;
+    private final List<SoftConstraint> _softConstraints;
+
+    public ConstraintBasedAlgorithm(List<HardConstraint> hardConstraints, List<SoftConstraint> softConstraints) {
+        _hardConstraints = hardConstraints;
+        _softConstraints = softConstraints;
+    }
+
+    @Override
+    public OptimalAssignment calculate(ClusterModel clusterModel) {
+        OptimalAssignment currentAssignment = new OptimalAssignment();
+        Map<String, Set<AssignableReplica>> replicasByResource = clusterModel.getAssignableReplicaMap();
+        List<AssignableNode> nodes = new ArrayList<>(clusterModel.getAssignableNodes().values());
+        ClusterContext clusterContext = clusterModel.getContext();
+
+
+        for (String resource : replicasByResource.keySet()) {
 
 Review comment:
   Could you use Map.Entry on replicasByResource here? Performance is better with Map.Entry/entrySet() since you're calling Map.get(K).

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@helix.apache.org
For additional commands, e-mail: reviews-help@helix.apache.org