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

IndexOutOfRangeException with RoundRobinRoutingLogic & SmallestMailboxRoutingLogic #908

Closed
jordansjones opened this issue Apr 26, 2015 · 0 comments

Comments

@jordansjones
Copy link
Contributor

Assuming Routee[] routees has 1 or more elements in the array, when Interlocked.Increment(ref _next) rolls over to a negative number, an IndexOutOfRangeException will be thrown.

The following is a simplistic example that demonstrates what is happening:

var items = new [] { 1, 2, 3, 4, 5 };
var count = int.MaxValue - 5;

for (var i = 0; i < 20; i++)
{
    items[Interlocked.Increment(ref count) % items.Length].Dump();
}

The following is a simplistic program that demonstrates the above while using Akka.

public class Program
{

    public const long Total = ((long) int.MaxValue) + 15;

    public static Stopwatch Stopwatch;

    public static void DoIt()
    {
        var system = ActorSystem.Create("GreetSystem");
        var greeter = system.ActorOf(
            Props.Create(() => new OutputActor())
                    .WithRouter(new RoundRobinPool(10).WithResizer(new DefaultResizer(5, 150)))
            );

        Stopwatch = Stopwatch.StartNew();
        for (var i = 0L; i < Total; i++)
        {
            greeter.Tell(i);
        }
    }

    public static void Main(string[] args)
    {
        DoIt();
    }

    public class OutputActor : ReceiveActor
    {

        private static long _count = -1;

        private static float _percent = -1f;

        private static readonly object Guard = new object();

        public OutputActor()
        {
            Receive<long>(x => DoStuff(x));
        }

        protected void DoStuff(long message)
        {
            var count = Interlocked.Increment(ref _count);
            lock (Guard)
            {
                var percent = (float) Math.Round((double) count / Total, 4);
                if (Math.Abs(percent - _percent) < .00001F) { return; }

                _percent = percent;
                Console.SetCursorPosition(0, 0);
                Console.Write("                    ");
                Console.SetCursorPosition(0, 0);
                Console.Write("{0:P2}", _percent);
            }
        }

    }

}
@jordansjones jordansjones changed the title IndexOutOfRangeException with RoundRobinRoutingLogic. IndexOutOfRangeException with RoundRobinRoutingLogic Apr 26, 2015
@jordansjones jordansjones changed the title IndexOutOfRangeException with RoundRobinRoutingLogic IndexOutOfRangeException with RoundRobinRoutingLogic & SmallestMailboxRoutingLogic Apr 27, 2015
rogeralsing added a commit that referenced this issue Apr 28, 2015
This was referenced Apr 28, 2015
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

No branches or pull requests

2 participants