lrhkobe opened a new pull request #605:
URL: https://github.com/apache/incubator-eventmesh/pull/605
Fixes **ISSUE#<>.**
### Motivation
As to the follow scene:
The cluster has 2 eventmesh instance: {EventMesh-01, EventMesh-02}
There are 100 eventmesh-client with same group distributed in eventmesh, and
the distributed data is: {EventMesh-01: 75 , EventMesh-02: 25}
**The current algorithm:**
```
int sum = 0;
for(Integer item : clientDistributionMap.values()){
sum += item.intValue();
}
int currentNum = 0;
if(clientDistributionMap.get(eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshName)
!= null){
currentNum =
clientDistributionMap.get(eventMeshTCPServer.getEventMeshTCPConfiguration().eventMeshName);
}
int avgNum = sum / clientDistributionMap.size();
int judge = avgNum >= 2 ? avgNum/2 : 1;
if(currentNum - avgNum > judge) {
...
}
```
In this scene, rebalance will not happen, because the rebalance condition is
not satisfied.
As to EventMesh-01, currentNum =75, avgNum=50, judge=25 --> currentNum -
avgNum > judge : false
As to EventMesh-02, currentNum =25, avgNum=50, judge=25 --> currentNum -
avgNum > judge : false
Obviously, the rebalance result is not proper.
Expected Rebalance Result:{EventMesh-01: 50, EventMesh-02: 50}
**Improved algorithm:**
```
public int caculateRedirectNum(String eventMeshName, String group, String
purpose, Map<String, Integer> clientDistributionMap) throws Exception{
int sum = 0;
for(Integer item : clientDistributionMap.values()){
sum += item.intValue();
}
int currentNum = 0;
if(clientDistributionMap.get(eventMeshName) != null){
currentNum = clientDistributionMap.get(eventMeshName);
}
int avgNum = sum / clientDistributionMap.size();
int modNum = sum % clientDistributionMap.size();
List<String> eventMeshList = new
ArrayList<>(clientDistributionMap.keySet());
Collections.sort(eventMeshList);
int index = -1;
for(int i=0; i < Math.min(modNum, eventMeshList.size()); i++){
if(StringUtils.equals(eventMeshName, eventMeshList.get(i))){
index = i;
break;
}
}
int rebalanceResult = 0;
if(avgNum == 0){
rebalanceResult = 1;
}else {
rebalanceResult = (modNum != 0 && index < modNum && index >= 0)
? avgNum + 1 : avgNum;
}
logger.info("rebalance caculateRedirectNum,group:{},
purpose:{},sum:{},avgNum:{},modNum:{},index:{},
currentNum:{},rebalanceResult:{}", group, purpose, sum, avgNum, modNum, index,
currentNum, rebalanceResult);
return currentNum - rebalanceResult;
}
```
if the num of client is less than the num of eventmesh, then choose
eventmesh randomly with the same num of client;
otherwise, calculate the accurate num which current eventmesh should be
allocated.
--
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]