예제 #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);
        }
      }
    }
 /**
  * Returns a grouped and sorted map of all registered metrics which match then given {@link
  * MetricPredicate}.
  *
  * @param predicate a predicate which metrics have to match to be in the results
  * @return all registered metrics which match {@code predicate}, sorted by name
  */
 public SortedMap<String, SortedMap<MetricName, Metric>> getGroupedMetrics(
     MetricPredicate predicate) {
   final SortedMap<String, SortedMap<MetricName, Metric>> groups =
       new TreeMap<String, SortedMap<MetricName, Metric>>();
   for (Map.Entry<MetricName, Metric> entry : metrics.entrySet()) {
     final String qualifiedTypeName = entry.getKey().getGroup() + "." + entry.getKey().getType();
     if (predicate.matches(entry.getKey(), entry.getValue())) {
       final String scopedName;
       if (entry.getKey().hasScope()) {
         scopedName = qualifiedTypeName + "." + entry.getKey().getScope();
       } else {
         scopedName = qualifiedTypeName;
       }
       SortedMap<MetricName, Metric> group = groups.get(scopedName);
       if (group == null) {
         group = new TreeMap<MetricName, Metric>();
         groups.put(scopedName, group);
       }
       group.put(entry.getKey(), entry.getValue());
     }
   }
   return Collections.unmodifiableSortedMap(groups);
 }
 /**
  * Adds a {@link MetricsRegistryListener} to a collection of listeners that will be notified on
  * metric creation. Listeners will be notified in the order in which they are added.
  *
  * <p><b>N.B.:</b> The listener will be notified of all existing metrics when it first registers.
  *
  * @param listener the listener that will be notified
  */
 public void addListener(MetricsRegistryListener listener) {
   listeners.add(listener);
   for (Map.Entry<MetricName, Metric> entry : metrics.entrySet()) {
     listener.onMetricAdded(entry.getKey(), entry.getValue());
   }
 }