vongosling commented on a change in pull request #342: URL: https://github.com/apache/incubator-eventmesh/pull/342#discussion_r633279123
########## File path: eventmesh-common/src/main/java/org/apache/eventmesh/common/loadbalance/RandomLoadBalanceSelector.java ########## @@ -0,0 +1,51 @@ +/* + * 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.collections4.CollectionUtils; +import org.apache.commons.lang3.RandomUtils; + +import java.util.List; + +/** + * This selector use random strategy. + * Each selection will randomly give one from the given list + * + * @param <T> + */ +public class RandomLoadBalanceSelector<T> implements LoadBalanceSelector<T> { + + private final List<T> clusterGroup; + + public RandomLoadBalanceSelector(List<T> clusterGroup) { + this.clusterGroup = clusterGroup; + } + + @Override + public T select() { + if (CollectionUtils.isEmpty(clusterGroup)) { + return null; Review comment: log.warn("No servers available" ) should before you return any default value. ########## File path: eventmesh-common/src/test/java/org/apache/eventmesh/common/loadbalance/WeightRoundRobinLoadBalanceSelectorTest.java ########## @@ -0,0 +1,64 @@ +/* + * 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.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class WeightRoundRobinLoadBalanceSelectorTest { + + private Logger logger = LoggerFactory.getLogger(WeightRoundRobinLoadBalanceSelectorTest.class); + + private WeightRoundRobinLoadBalanceSelector<String> weightRoundRobinLoadBalanceSelector; + + @Before + public void before() { + List<Weight<String>> weightList = new ArrayList<>(); + weightList.add(new Weight<>("A", 10)); + weightList.add(new Weight<>("B", 20)); + weightList.add(new Weight<>("C", 30)); + weightRoundRobinLoadBalanceSelector = new WeightRoundRobinLoadBalanceSelector<>(weightList); + } + + @Test + public void testSelect() { + Map<String, Integer> addressToNum = new HashMap<>(); + for (int i = 0; i < 100_000; i++) { + String select = weightRoundRobinLoadBalanceSelector.select(); + addressToNum.put(select, addressToNum.getOrDefault(select, 0) + 1); + } + addressToNum.forEach((key, value) -> { + logger.info("{}: {}", key, value); + }); + // just assert success if no exception + Assert.assertTrue(true); Review comment: How to prove C service has the highest priority in test? ########## File path: eventmesh-common/src/main/java/org/apache/eventmesh/common/loadbalance/WeightRoundRobinLoadBalanceSelector.java ########## @@ -0,0 +1,67 @@ +/* + * 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.collections4.CollectionUtils; + +import java.util.List; + +/** + * This selector use the weighted round robin strategy to select from list. + * If the weight is greater, the probability of being selected is larger. + * + * @param <T> + */ +public class WeightRoundRobinLoadBalanceSelector<T> implements LoadBalanceSelector<T> { + + private final List<Weight<T>> clusterGroup; + + private final int totalWeight; + + public WeightRoundRobinLoadBalanceSelector(List<Weight<T>> clusterGroup) { + int totalWeight = 0; + for (Weight<T> weight : clusterGroup) { + totalWeight += weight.getWeight(); + } + this.clusterGroup = clusterGroup; + this.totalWeight = totalWeight; + } + + + @Override + @SuppressWarnings("ConstantConditions") + public T select() { + if (CollectionUtils.isEmpty(clusterGroup)) { + return null; Review comment: The same... -- 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: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
