Beispiel #1
0
    public void prepare(String query, InetAddress toExclude) throws InterruptedException {
      for (Map.Entry<Host, HostConnectionPool> entry : pools.entrySet()) {
        if (entry.getKey().getAddress().equals(toExclude)) continue;

        // Let's not wait too long if we can't get a connection. Things
        // will fix themselves once the user tries a query anyway.
        Connection c = null;
        try {
          c = entry.getValue().borrowConnection(200, TimeUnit.MILLISECONDS);
          c.write(new PrepareMessage(query)).get();
        } catch (ConnectionException e) {
          // Again, not being able to prepare the query right now is no big deal, so just ignore
        } catch (BusyConnectionException e) {
          // Same as above
        } catch (TimeoutException e) {
          // Same as above
        } catch (ExecutionException e) {
          // We shouldn't really get exception while preparing a
          // query, so log this (but ignore otherwise as it's not a big deal)
          logger.error(
              String.format(
                  "Unexpected error while preparing query (%s) on %s", query, entry.getKey()),
              e);
        } finally {
          if (c != null) entry.getValue().returnConnection(c);
        }
      }
    }
Beispiel #2
0
    private boolean shutdown(long timeout, TimeUnit unit) throws InterruptedException {

      if (!isShutdown.compareAndSet(false, true)) return true;

      long start = System.nanoTime();
      boolean success = true;
      for (HostConnectionPool pool : pools.values())
        success &= pool.shutdown(timeout - Cluster.timeSince(start, unit), unit);
      return success;
    }
Beispiel #3
0
    public void onDown(Host host) {
      loadBalancer.onDown(host);
      HostConnectionPool pool = pools.remove(host);

      // This should not be necessary but it's harmless
      if (pool != null) pool.shutdown();

      // If we've remove a host, the loadBalancer is allowed to change his mind on host distances.
      for (Host h : cluster.getMetadata().allHosts()) {
        if (!h.getMonitor().isUp()) continue;

        HostDistance dist = loadBalancer.distance(h);
        if (dist != HostDistance.IGNORED) {
          HostConnectionPool p = pools.get(h);
          if (p == null) addHost(host);
          else p.hostDistance = dist;
        }
      }
    }
Beispiel #4
0
 private HostConnectionPool addHost(Host host) {
   try {
     HostDistance distance = loadBalancer.distance(host);
     if (distance == HostDistance.IGNORED) {
       return pools.get(host);
     } else {
       logger.debug("Adding {} to list of queried hosts", host);
       return pools.put(host, new HostConnectionPool(host, distance, this));
     }
   } catch (AuthenticationException e) {
     logger.error("Error creating pool to {} ({})", host, e.getMessage());
     host.getMonitor()
         .signalConnectionFailure(new ConnectionException(e.getHost(), e.getMessage()));
     return pools.get(host);
   } catch (ConnectionException e) {
     logger.debug("Error creating pool to {} ({})", host, e.getMessage());
     host.getMonitor().signalConnectionFailure(e);
     return pools.get(host);
   }
 }
Beispiel #5
0
    /*
     * When the set of live nodes change, the loadbalancer will change his
     * mind on host distances. It might change it on the node that came/left
     * but also on other nodes (for instance, if a node dies, another
     * previously ignored node may be now considered).
     *
     * This method ensures that all hosts for which a pool should exist
     * have one, and hosts that shouldn't don't.
     */
    private void updateCreatedPools() {
      for (Host h : cluster.getMetadata().allHosts()) {
        HostDistance dist = loadBalancingPolicy().distance(h);
        HostConnectionPool pool = pools.get(h);

        if (pool == null) {
          if (dist != HostDistance.IGNORED && h.getMonitor().isUp()) addOrRenewPool(h);
        } else if (dist != pool.hostDistance) {
          if (dist == HostDistance.IGNORED) {
            removePool(h);
          } else {
            pool.hostDistance = dist;
          }
        }
      }
    }
Beispiel #6
0
 private void removePool(Host host) {
   HostConnectionPool pool = pools.remove(host);
   if (pool != null) pool.shutdown();
 }
Beispiel #7
0
 public void onRemove(Host host) {
   loadBalancer.onRemove(host);
   HostConnectionPool pool = pools.remove(host);
   if (pool != null) pool.shutdown();
 }
Beispiel #8
0
    private void shutdown() {

      if (!isShutdown.compareAndSet(false, true)) return;

      for (HostConnectionPool pool : pools.values()) pool.shutdown();
    }