@Test public void wrappingWorks() { long start = Long.MAX_VALUE - 4; PatternStrategy strategy = new RoundRobinLoadBalancer(3, 3, new AtomicLong(start)); long expectedStart = start % 3; assertEquals(expectedStart, strategy.nextIndices().iterator().next().intValue()); assertEquals(expectedStart + 1, strategy.nextIndices().iterator().next().intValue()); assertEquals(0, strategy.nextIndices().iterator().next().intValue()); assertEquals(1, strategy.nextIndices().iterator().next().intValue()); assertEquals(2, strategy.nextIndices().iterator().next().intValue()); }
@Override public Iterable<Integer> nextIndices() { long index = counter.getAndIncrement(); if (index >= flipPoint) { resetCounter(index); } SingleReaderArrayIterable iterable = new SingleReaderArrayIterable(maxAcquireAttempts); Integer[] orderToTry = iterable.getIndices(); for (int i = 0; i < maxAcquireAttempts; ++i) { orderToTry[i] = (int) ((index + i) % size); } shuffleTail(orderToTry); return iterable; }
RoundRobinLoadBalancer implements PatternStrategy { @Override public Iterable<Integer> nextIndices() { long index = counter.getAndIncrement(); if (index >= flipPoint) { resetCounter(index); } SingleReaderArrayIterable iterable = new SingleReaderArrayIterable(maxAcquireAttempts); Integer[] orderToTry = iterable.getIndices(); for (int i = 0; i < maxAcquireAttempts; ++i) { orderToTry[i] = (int) ((index + i) % size); } shuffleTail(orderToTry); return iterable; } }
RoundRobinLoadBalancer implements PatternStrategy { @Override public Iterable<Integer> nextIndices() { long index = counter.getAndIncrement(); if (index >= flipPoint) { resetCounter(index); } SingleReaderArrayIterable iterable = new SingleReaderArrayIterable(maxAcquireAttempts); Integer[] orderToTry = iterable.getIndices(); for (int i = 0; i < maxAcquireAttempts; ++i) { orderToTry[i] = (int) ((index + i) % size); } shuffleTail(orderToTry); return iterable; } RoundRobinLoadBalancer(int size);  RoundRobinLoadBalancer(int size, int maxAcquireAttempts);  RoundRobinLoadBalancer(int size, int maxAcquireAttempts, AtomicLong counter); }
RoundRobinLoadBalancer implements PatternStrategy { @Override public Iterable<Integer> nextIndices() { long index = counter.getAndIncrement(); if (index >= flipPoint) { resetCounter(index); } SingleReaderArrayIterable iterable = new SingleReaderArrayIterable(maxAcquireAttempts); Integer[] orderToTry = iterable.getIndices(); for (int i = 0; i < maxAcquireAttempts; ++i) { orderToTry[i] = (int) ((index + i) % size); } shuffleTail(orderToTry); return iterable; } RoundRobinLoadBalancer(int size);  RoundRobinLoadBalancer(int size, int maxAcquireAttempts);  RoundRobinLoadBalancer(int size, int maxAcquireAttempts, AtomicLong counter); @Override Iterable<Integer> nextIndices(); @Override int acquireCount(); }
RoundRobinLoadBalancer implements PatternStrategy { @Override public Iterable<Integer> nextIndices() { long index = counter.getAndIncrement(); if (index >= flipPoint) { resetCounter(index); } SingleReaderArrayIterable iterable = new SingleReaderArrayIterable(maxAcquireAttempts); Integer[] orderToTry = iterable.getIndices(); for (int i = 0; i < maxAcquireAttempts; ++i) { orderToTry[i] = (int) ((index + i) % size); } shuffleTail(orderToTry); return iterable; } RoundRobinLoadBalancer(int size);  RoundRobinLoadBalancer(int size, int maxAcquireAttempts);  RoundRobinLoadBalancer(int size, int maxAcquireAttempts, AtomicLong counter); @Override Iterable<Integer> nextIndices(); @Override int acquireCount();  }