Skip to content

RoundRobinLoadBalancer will raise ArrayIndexOutOfBoundsException when position positive overflow #1074

@jizhuozhi

Description

@jizhuozhi

Describe the bug
RoundRobinLoadBalancer is using an AtomicInteger to represent current position, when positive overflow, we are just using Math.abs to fix it, it will raise ArrayIndexOutOfBoundsException.

Sample
It's to hard to reproduce this issue in production environment, so I using jshell to reproduce it.

jshell> import java.util.concurrent.atomic.AtomicInteger
jshell> var instances = Arrays.asList("1", "2", "3", "4", "5")
jshell> var position = new AtomicInteger(Integer.MAX_VALUE)
jshell> var pos = Math.abs(position.incrementAndGet())
pos ==> -2147483648
jshell> var instance = instances.get(pos % instances.size())
|  java.lang.ArrayIndexOutOfBoundsException:Index -3 out of bounds for length 5
|        at Arrays$ArrayList.get (Arrays.java:4165)
|        at do_it$Aux (#5:1)
|        at (#5:1)

It as same as the source code

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions