Jagger
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
JLoadBalancer.java
Go to the documentation of this file.
1 package com.griddynamics.jagger.user.test.configurations.loadbalancer;
2 
3 import com.griddynamics.jagger.invoker.CircularExclusiveAccessLoadBalancer;
4 import com.griddynamics.jagger.invoker.ExclusiveAccessLoadBalancer;
5 import com.griddynamics.jagger.invoker.NonCircularExclusiveAccessLoadBalancer;
6 import com.griddynamics.jagger.invoker.OneByOneLoadBalancer;
7 import com.griddynamics.jagger.invoker.OneByOnePairSupplierFactory;
8 import com.griddynamics.jagger.invoker.PairSupplierFactory;
9 import com.griddynamics.jagger.invoker.PairSupplierFactoryLoadBalancer;
10 import com.griddynamics.jagger.invoker.QueryPoolLoadBalancer;
11 import com.griddynamics.jagger.invoker.RandomLoadBalancer;
12 import com.griddynamics.jagger.invoker.RoundRobinLoadBalancer;
13 import com.griddynamics.jagger.invoker.RoundRobinPairSupplierFactory;
14 import com.griddynamics.jagger.invoker.SimpleCircularLoadBalancer;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17 
18 import java.io.Serializable;
19 import java.util.Objects;
20 import java.util.Random;
21 
27 public class JLoadBalancer implements Serializable {
28 
29  private final static Logger log = LoggerFactory.getLogger(JLoadBalancer.class);
30 
36  public static Builder builder(DefaultLoadBalancer loadBalancer) {
37  return new Builder(loadBalancer);
38  }
39 
46  public enum DefaultLoadBalancer {
47  ROUND_ROBIN, ONE_BY_ONE
48  }
49 
50  public static class Builder {
51  private final DefaultLoadBalancer loadBalancer;
52  private Long seed;
53  private boolean exclusiveAccess;
54  private boolean oneIterationOnly;
55 
61  public Builder(DefaultLoadBalancer loadBalancer) {
62  this.loadBalancer = loadBalancer;
63  }
64 
74  public Builder withRandomSeed(long seed) {
75  this.seed = seed;
76  return this;
77  }
78 
88  this.exclusiveAccess = true;
89  return this;
90  }
91 
99  this.oneIterationOnly = true;
100  this.exclusiveAccess = true;
101 
102  return this;
103  }
104 
108  public QueryPoolLoadBalancer build() {
109 
110  PairSupplierFactory pairSupplierFactory = null;
111  switch (loadBalancer) {
112  case ONE_BY_ONE:
113  pairSupplierFactory = new OneByOnePairSupplierFactory();
114  break;
115  case ROUND_ROBIN:
116  default:
117  pairSupplierFactory = new RoundRobinPairSupplierFactory();
118  break;
119  }
120 
121  PairSupplierFactoryLoadBalancer loadBalancer = null;
122  if (exclusiveAccess) {
123  if (oneIterationOnly) {
124  loadBalancer = new NonCircularExclusiveAccessLoadBalancer(pairSupplierFactory);
125  } else {
126  loadBalancer = new CircularExclusiveAccessLoadBalancer(pairSupplierFactory);
127  }
128  ((ExclusiveAccessLoadBalancer)loadBalancer).setRandomnessSeed(seed);
129  } else {
130  if (Objects.nonNull(seed)) {
131  loadBalancer = new RandomLoadBalancer(seed, pairSupplierFactory);
132  } else {
133  loadBalancer = new SimpleCircularLoadBalancer(pairSupplierFactory);
134  }
135  }
136 
137  log.info("Built a {} load balancer", loadBalancer);
138  return loadBalancer;
139  }
140  }
141 }