Load Balancing Algorithms for Deep Resolution xxl-rpc

Posted by bazza84 on Mon, 14 Oct 2019 18:36:54 +0200

introduce

In xxl-rpc, five load balancing algorithms are provided, including polling, randomization, LRU, LFU and consistent HASH.
Package location:

Source code interpretation

1. load balancing abstract class
/**
 * Machine addresses are the same under grouping, and different JOBs are evenly scattered on different machines to ensure the average JOB allocation of machines under grouping, and each JOB schedules one of them regularly.
 *      a,virtual node: Solving the problem of imbalance
 *      b,hash method replace hashCode: String The hashCode may be duplicated, and the range of hashCode values needs to be further expanded.
 *
 * @author xuxueli 2018-12-04
 */
public abstract class XxlRpcLoadBalance {
	// addressSet is the address set of serviceKey
    public abstract String route(String serviceKey, TreeSet<String> addressSet);

}

2. randomised

Random algorithm is very simple. Random class is used to generate random numbers.

public class XxlRpcLoadBalanceRandomStrategy extends XxlRpcLoadBalance {

    private Random random = new Random();

    @Override
    public String route(String serviceKey, TreeSet<String> addressSet) {
        // Array converts set to an arr ay
        String[] addressArr = addressSet.toArray(new String[addressSet.size()]);

        // Random random length
        String finalAddress = addressArr[random.nextInt(addressSet.size())];
        return finalAddress;
    }

}
3. polling

Polling means taking turns. Use% of the number of service providers, and use count is plus operation, so the result is next to, say, 1, 2, 3 service providers, the algorithm can ensure that this time is 1, next time is 2, next time is 3.

/**
 * round
 *
 * @author xuxueli 2018-12-04
 */
public class XxlRpcLoadBalanceRoundStrategy extends XxlRpcLoadBalance {
    // Machine used to record the number of times a service is selected by this number% of the number of service providers
    private ConcurrentMap<String, Integer> routeCountEachJob = new ConcurrentHashMap<String, Integer>();
    private long CACHE_VALID_TIME = 0;
    private int count(String serviceKey) {
        // cache clear needs to clear the map after a period of time
        if (System.currentTimeMillis() > CACHE_VALID_TIME) {
            routeCountEachJob.clear();
            CACHE_VALID_TIME = System.currentTimeMillis() + 24*60*60*1000;
        }

        // count++
        Integer count = routeCountEachJob.get(serviceKey);
        //  More than 1000000 will be initialized, using random numbers to prevent the first time always thrown to the first machine
        count = (count==null || count>1000000)?(new Random().nextInt(100)):++count;  // Active Random once at initialization to relieve first-time pressure
        routeCountEachJob.put(serviceKey, count);
        return count;
    }

    @Override
    public String route(String serviceKey, TreeSet<String> addressSet) {
        // arr
        String[] addressArr = addressSet.toArray(new String[addressSet.size()]);

        // round
        String finalAddress = addressArr[count(serviceKey)%addressArr.length];
        return finalAddress;
    }

}

Topics: Programming