All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.redismq.rebalance.AllocateMessageQueueAveragely Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version

package com.redismq.rebalance;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * Average Hashing queue algorithm
 */
@Slf4j
public class AllocateMessageQueueAveragely implements AllocateMessageQueueStrategy {

    @Override
    public List allocate(String currentCID, List mqAll,
                                 List cidAll) {
        if (currentCID == null || currentCID.length() < 1) {
            throw new IllegalArgumentException("currentCID is empty");
        }
        if (mqAll == null || mqAll.isEmpty()) {
            throw new IllegalArgumentException("mqAll is null or mqAll empty");
        }
        if (cidAll == null || cidAll.isEmpty()) {
            throw new IllegalArgumentException("cidAll is null or cidAll empty");
        }

        List result = new ArrayList<>();
        if (!cidAll.contains(currentCID)) {
            log.error("clientIds not contains currentCID");
            return result;
        }

        int index = cidAll.indexOf(currentCID);
        int mod = mqAll.size() % cidAll.size();
        int averageSize =
                mqAll.size() <= cidAll.size() ? 1 : (mod > 0 && index < mod ? mqAll.size() / cidAll.size()
                        + 1 : mqAll.size() / cidAll.size());
        int startIndex = (mod > 0 && index < mod) ? index * averageSize : index * averageSize + mod;
        int range = Math.min(averageSize, mqAll.size() - startIndex);
        for (int i = 0; i < range; i++) {
            result.add(mqAll.get((startIndex + i) % mqAll.size()));
        }
        return result;
    }

    @Override
    public String getName() {
        return "AVG";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy