You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@eventmesh.apache.org by GitBox <gi...@apache.org> on 2021/09/18 04:17:12 UTC

[GitHub] [incubator-eventmesh] horoc commented on a change in pull request #526: add weighted random load balacne selector

horoc commented on a change in pull request #526:
URL: https://github.com/apache/incubator-eventmesh/pull/526#discussion_r711465289



##########
File path: eventmesh-common/src/main/java/org/apache/eventmesh/common/loadbalance/WeightRandomLoadBalanceSelector.java
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.eventmesh.common.loadbalance;
+
+import org.apache.commons.lang3.RandomUtils;
+
+import java.util.List;
+
+/**
+ * This selector use the weighted random strategy to select from list.
+ * If all the weights are same, it will randomly select one from list.
+ * If the weights are different, it will select one by using RandomUtils.nextInt(0, w0 + w1 ... + wn)
+ *
+ * @param <T> Target type
+ */
+public class WeightRandomLoadBalanceSelector<T> implements LoadBalanceSelector<T> {
+
+    private final List<Weight<T>> clusterGroup;
+
+    private final int totalWeight;
+
+    private boolean sameWeightGroup = true;
+
+    public WeightRandomLoadBalanceSelector(List<Weight<T>> clusterGroup) {

Review comment:
       > We need to throw an Exception if the clusterGroup is empty?
   > And in your code, it seems the `totalWeight` cannot <= 0?
   
   I add a not empty check in the constructor.
   totalWeight equals 0 is acceptable, it will make the sameWeightGroup true.
   totalWeight less than 0 maybe not possiable in the context, the IP_PORT_WEIGHT_PATTERN can not match a negative weight.




-- 
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: dev-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org