Jagger
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
CircularExclusiveAccessLoadBalancer.java
Go to the documentation of this file.
1 package com.griddynamics.jagger.invoker;
2 
3 import com.griddynamics.jagger.util.Pair;
4 import com.griddynamics.jagger.util.Timeout;
5 import org.slf4j.Logger;
6 import org.slf4j.LoggerFactory;
7 
8 import java.util.concurrent.TimeUnit;
9 
24 public class CircularExclusiveAccessLoadBalancer<Q, E> extends ExclusiveAccessLoadBalancer<Q, E> {
25 
26  private final static Logger log = LoggerFactory.getLogger(CircularExclusiveAccessLoadBalancer.class);
27 
28  public CircularExclusiveAccessLoadBalancer(PairSupplierFactory<Q, E> pairSupplierFactory) {
29  super(pairSupplierFactory);
30  }
31 
32  private final static String POLL_TIMEOUT_NAME = "load balancer poll next timeout";
33 
34  private Timeout pollTimeout = new Timeout(10 * 60 * 1000, POLL_TIMEOUT_NAME); // 10 minutes by default
35 
36  @Override
37  protected boolean isToCircleAnIteration() {
38  return true;
39  }
40 
41  protected Pair<Q, E> pollNext() {
42 
43  Pair<Q, E> next = null;
44  long startMillis = System.currentTimeMillis();
45  try {
46  next = getPairQueue().poll(pollTimeout.getValue(), TimeUnit.MILLISECONDS);
47  } catch (InterruptedException ignored) {
48  }
49 
50  if (next == null) {
51  throw new IllegalStateException(String.format("Didn't manage to poll the next pair. Timeout %s",
52  pollTimeout));
53  }
54 
55  long endMillis = System.currentTimeMillis();
56  if (endMillis - startMillis > 1000) {
57  log.warn(
58  "It took {} ms to poll the next pair for the load balancer. Possible reason: not enough test data for selected load",
59  endMillis - startMillis);
60  }
61 
62  return next;
63  }
64 
65  public void setPollTimeout(long pollTimeout) {
66  this.pollTimeout = new Timeout(pollTimeout, POLL_TIMEOUT_NAME);
67  }
68 }