/*
 * Druid - a distributed column store.
 * Copyright (C) 2012, 2013  Metamarkets Group Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package io.druid.query;

import com.metamx.emitter.service.ServiceEmitter;
import com.metamx.emitter.service.ServiceMetricEvent;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;

public class MetricsEmittingExecutorService extends DelegatingExecutorService
{
  private final ExecutorService base;
  private final ServiceEmitter emitter;
  private final ServiceMetricEvent.Builder metricBuilder;

  public MetricsEmittingExecutorService(
      ExecutorService base,
      ServiceEmitter emitter,
      ServiceMetricEvent.Builder metricBuilder
  )
  {
    super(base);

    this.base = base;
    this.emitter = emitter;
    this.metricBuilder = metricBuilder;
  }

  @Override
  public <T> Future<T> submit(Callable<T> tCallable)
  {
    emitMetrics();
    return base.submit(tCallable);
  }

  @Override
  public void execute(Runnable runnable)
  {
    emitMetrics();
    base.execute(runnable);
  }

  private void emitMetrics()
  {
    if (base instanceof PrioritizedExecutorService) {
      emitter.emit(metricBuilder.build("exec/backlog", ((PrioritizedExecutorService) base).getQueueSize()));
    }
  }
}

@@ -854,151 +854,157 @@
 com.
-metamx.emitter.service.ServiceEmitter;%0Aimport com.metamx.emitter.service.ServiceMetricEvent;%0A%0Aimport java.util.concurrent.Callable;%0Aimport java
+google.common.util.concurrent.ForwardingListeningExecutorService;%0Aimport com.google.common.util.concurrent.ListenableFuture;%0Aimport com.google.common
 .uti
@@ -1016,16 +1016,25 @@
 current.
+Listening
 Executor
@@ -1053,36 +1053,105 @@
 ort 
-java.util.concurrent.Future;
+com.metamx.emitter.service.ServiceEmitter;%0Aimport com.metamx.emitter.service.ServiceMetricEvent;%0A
 %0Aimp
@@ -1179,26 +1179,16 @@
 ent.
-ThreadPoolExecutor
+Callable
 ;%0A%0Ap
@@ -1242,15 +1242,24 @@
 nds 
-Delegat
+ForwardingListen
 ingE
@@ -1291,16 +1291,25 @@
 e final 
+Listening
 Executor
@@ -1316,19 +1316,23 @@
 Service 
-bas
+delegat
 e;%0A  pri
@@ -1472,16 +1472,25 @@
 (%0A      
+Listening
 Executor
@@ -1497,19 +1497,23 @@
 Service 
-bas
+delegat
 e,%0A     
@@ -1602,20 +1602,16 @@
   super(
-base
 );%0A%0A    
@@ -1619,18 +1619,26 @@
 his.
-base = bas
+delegate = delegat
 e;%0A 
@@ -1728,18 +1728,118 @@
 %0A  p
-ublic %3CT%3E 
+rotected ListeningExecutorService delegate()%0A  %7B%0A    return delegate;%0A  %7D%0A%0A  @Override%0A  public %3CT%3E Listenable
 Futu
@@ -1908,19 +1908,23 @@
  return 
-bas
+delegat
 e.submit
@@ -2021,19 +2021,23 @@
 ();%0A    
-bas
+delegat
 e.execut
@@ -2095,19 +2095,23 @@
     if (
-bas
+delegat
 e instan
@@ -2234,11 +2234,15 @@
 ce) 
-bas
+delegat
 e).g

make MetricsEmittingExecutorService listenable