Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RandomRule policy modification #393

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mujun0312
Copy link

@mujun0312 mujun0312 commented Dec 5, 2018

In the RandomRule's choose method, it use upList to get server, the index of the server come from "ThreadLocalRandom.current().nextInt(serverCount)", so the serverCount is the number of all server. But to get the reachable server is to use upList like this:
image
It seems to happen that the index(come from index = ThreadLocalRandom.current().nextInt(serverCount) ) is larger than the upList's size.

@mujun0312
Copy link
Author

@qiangdavidliu Could you please review?

* only get more restrictive.
*/
return null;
}

int index = chooseRandomInt(serverCount);
server = upList.get(index);
server = allList.get(index);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Server should not be selected on upList ?

int index = chooseRandomInt(upCount);
server = upList.get(index);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, server should be selected on upList, but the index should not be chosen from serverCount, it may be greater than upList.size()

int serverCount = allList.size();
if (serverCount == 0) {
if (serverCount == 0 || upCount == 0) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check upCount isn't sufficient ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the server that is reachable is down, then the upCount is 0

@broncho
Copy link

broncho commented Sep 5, 2023

just use reachable servers.

public Server choose(ILoadBalancer lb, Object key) {
        if (lb == null) {
            return null;
        }
        Server server = null;

        while (server == null) {
            if (Thread.interrupted()) {
                return null;
            }
            List<Server> upList = lb.getReachableServers();
            List<Server> allList = lb.getAllServers();

            int serverCount = allList.size();
            int upServerCount = upList.size();
            if (serverCount == 0  || upServerCount  ==0 ) {
                /*
                 * No servers. End regardless of pass, because subsequent passes
                 * only get more restrictive.
                 */
                return null;
            }

           // int index = chooseRandomInt(serverCount);
            //server = upList.get(index);
            
            int index = chooseRandomInt(upServerCount );
            server = upList.get(index);  

            if (server == null) {
                /*
                 * The only time this should happen is if the server list were
                 * somehow trimmed. This is a transient condition. Retry after
                 * yielding.
                 */
                Thread.yield();
                continue;
            }

            if (server.isAlive()) {
                return (server);
            }

            // Shouldn't actually happen.. but must be transient or a bug.
            server = null;
            Thread.yield();
        }

        return server;

    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants