Jagger
 All Classes Namespaces Files Functions Variables Enumerator Groups Pages
QueryPoolLoadBalancer.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010-2012 Grid Dynamics Consulting Services, Inc, All Rights Reserved
3  * http://www.griddynamics.com
4  *
5  * This library is free software; you can redistribute it and/or modify it under the terms of
6  * the Apache License; either
7  * version 2.0 of the License, or any later version.
8  *
9  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
10  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
11  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
13  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
15  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
16  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
17  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19  */
20 
21 package com.griddynamics.jagger.invoker;
22 
23 import com.griddynamics.jagger.util.Pair;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 
27 import java.util.Iterator;
28 
37 public abstract class QueryPoolLoadBalancer<Q, E> implements LoadBalancer<Q, E> {
38 
39  private final static Logger log = LoggerFactory.getLogger(QueryPoolLoadBalancer.class);
40 
41  protected Iterable<Q> queryProvider;
42  protected Iterable<E> endpointProvider;
43  protected KernelInfo kernelInfo;
44 
45  protected volatile boolean initialized = false;
46  protected final Object lock = new Object();
47 
49  }
50 
51  public QueryPoolLoadBalancer(Iterable<Q> queryProvider, Iterable<E> endpointProvider){
52  this.queryProvider = queryProvider;
53  this.endpointProvider = endpointProvider;
54  }
55 
56  public void setQueryProvider(Iterable<Q> queryProvider){
57  this.queryProvider = queryProvider;
58  }
59 
60  public void setEndpointProvider(Iterable<E> endpointProvider){
61  this.endpointProvider = endpointProvider;
62  }
63 
64  public void setKernelInfo(KernelInfo kernelInfo) {
65  this.kernelInfo = kernelInfo;
66  }
67 
68  @Override
69  public final Iterator<Pair<Q, E>> iterator() {
70  return provide();
71  }
72 
73  @Override
74  public int endpointSize() {
75  return getIterableSize(endpointProvider);
76  }
77 
81  public void init() {
82  synchronized (lock) {
83  if (initialized) {
84  log.debug("already initialized. returning...");
85  return;
86  }
87 
88  if (endpointProvider == null) {
89  throw new IllegalStateException("Endpoint provider is null");
90  } else {
91  log.info("total endpoints number - {}", endpointSize());
92  }
93 
94  if (queryProvider == null) {
95  log.info("Query provider is null.");
96  } else {
97  log.info("total queries number - {}", querySize());
98  }
99 
100  log.info("kernel info: {}", kernelInfo);
101 
102  initialized = true;
103  }
104  }
105 
106  @Override
107  public int querySize() {
108  return getIterableSize(queryProvider);
109  }
110 
111  public int getIterableSize(Iterable iterable){
112  if (iterable == null)
113  return 0;
114 
115  Iterator<Q> iterator = iterable.iterator();
116  int size = 0;
117  while (iterator.hasNext()){
118  iterator.next();
119  size++;
120  }
121  return size;
122  }
123 }