1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same directory.
9 import java
.util
.concurrent
.Executors
;
10 import java
.util
.concurrent
.ExecutorService
;
11 import java
.util
.concurrent
.TimeUnit
;
14 * <p>Helper class to collect DB statistics periodically at a period specified in
15 * constructor. Callback function (provided in constructor) is called with
16 * every statistics collection.</p>
18 * <p>Caller should call start() to start statistics collection. Shutdown() should
19 * be called to stop stats collection and should be called before statistics (
20 * provided in constructor) reference has been disposed.</p>
22 public class StatisticsCollector
{
23 private final List
<StatsCollectorInput
> _statsCollectorInputList
;
24 private final ExecutorService _executorService
;
25 private final int _statsCollectionInterval
;
26 private volatile boolean _isRunning
= true;
29 * Constructor for statistics collector.
31 * @param statsCollectorInputList List of statistics collector input.
32 * @param statsCollectionIntervalInMilliSeconds Statistics collection time
33 * period (specified in milliseconds).
35 public StatisticsCollector(
36 final List
<StatsCollectorInput
> statsCollectorInputList
,
37 final int statsCollectionIntervalInMilliSeconds
) {
38 _statsCollectorInputList
= statsCollectorInputList
;
39 _statsCollectionInterval
= statsCollectionIntervalInMilliSeconds
;
41 _executorService
= Executors
.newSingleThreadExecutor();
45 _executorService
.submit(collectStatistics());
49 * Shuts down statistics collector.
51 * @param shutdownTimeout Time in milli-seconds to wait for shutdown before
52 * killing the collection process.
53 * @throws java.lang.InterruptedException thrown if Threads are interrupted.
55 public void shutDown(final int shutdownTimeout
) throws InterruptedException
{
58 _executorService
.shutdownNow();
59 // Wait for collectStatistics runnable to finish so that disposal of
60 // statistics does not cause any exceptions to be thrown.
61 _executorService
.awaitTermination(shutdownTimeout
, TimeUnit
.MILLISECONDS
);
64 private Runnable
collectStatistics() {
65 return new Runnable() {
71 if(Thread
.currentThread().isInterrupted()) {
74 for(StatsCollectorInput statsCollectorInput
:
75 _statsCollectorInputList
) {
76 Statistics statistics
= statsCollectorInput
.getStatistics();
77 StatisticsCollectorCallback statsCallback
=
78 statsCollectorInput
.getCallback();
80 // Collect ticker data
81 for(TickerType ticker
: TickerType
.values()) {
82 long tickerValue
= statistics
.getTickerCount(ticker
);
83 statsCallback
.tickerCallback(ticker
, tickerValue
);
86 // Collect histogram data
87 for(HistogramType histogramType
: HistogramType
.values()) {
88 HistogramData histogramData
=
89 statistics
.getHistogramData(histogramType
);
90 statsCallback
.histogramCallback(histogramType
, histogramData
);
93 Thread
.sleep(_statsCollectionInterval
);
96 catch (InterruptedException e
) {
97 Thread
.currentThread().interrupt();
100 catch (Exception e
) {
101 throw new RuntimeException("Error while calculating statistics", e
);