]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/java/src/main/java/org/rocksdb/StatisticsCollector.java
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / java / src / main / java / org / rocksdb / StatisticsCollector.java
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.
5
6 package org.rocksdb;
7
8 import java.util.List;
9 import java.util.concurrent.Executors;
10 import java.util.concurrent.ExecutorService;
11 import java.util.concurrent.TimeUnit;
12
13 /**
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>
17 *
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>
21 */
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;
27
28 /**
29 * Constructor for statistics collector.
30 *
31 * @param statsCollectorInputList List of statistics collector input.
32 * @param statsCollectionIntervalInMilliSeconds Statistics collection time
33 * period (specified in milliseconds).
34 */
35 public StatisticsCollector(
36 final List<StatsCollectorInput> statsCollectorInputList,
37 final int statsCollectionIntervalInMilliSeconds) {
38 _statsCollectorInputList = statsCollectorInputList;
39 _statsCollectionInterval = statsCollectionIntervalInMilliSeconds;
40
41 _executorService = Executors.newSingleThreadExecutor();
42 }
43
44 public void start() {
45 _executorService.submit(collectStatistics());
46 }
47
48 /**
49 * Shuts down statistics collector.
50 *
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.
54 */
55 public void shutDown(final int shutdownTimeout) throws InterruptedException {
56 _isRunning = false;
57
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);
62 }
63
64 private Runnable collectStatistics() {
65 return new Runnable() {
66
67 @Override
68 public void run() {
69 while (_isRunning) {
70 try {
71 if(Thread.currentThread().isInterrupted()) {
72 break;
73 }
74 for(StatsCollectorInput statsCollectorInput :
75 _statsCollectorInputList) {
76 Statistics statistics = statsCollectorInput.getStatistics();
77 StatisticsCollectorCallback statsCallback =
78 statsCollectorInput.getCallback();
79
80 // Collect ticker data
81 for(TickerType ticker : TickerType.values()) {
82 long tickerValue = statistics.getTickerCount(ticker);
83 statsCallback.tickerCallback(ticker, tickerValue);
84 }
85
86 // Collect histogram data
87 for(HistogramType histogramType : HistogramType.values()) {
88 HistogramData histogramData =
89 statistics.getHistogramData(histogramType);
90 statsCallback.histogramCallback(histogramType, histogramData);
91 }
92
93 Thread.sleep(_statsCollectionInterval);
94 }
95 }
96 catch (InterruptedException e) {
97 Thread.currentThread().interrupt();
98 break;
99 }
100 catch (Exception e) {
101 throw new RuntimeException("Error while calculating statistics", e);
102 }
103 }
104 }
105 };
106 }
107 }