1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.metrics2.impl;
19
20 import java.util.concurrent.ScheduledFuture;
21 import java.util.concurrent.TimeUnit;
22 import java.util.concurrent.atomic.AtomicBoolean;
23 import java.util.concurrent.atomic.AtomicReference;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.hbase.classification.InterfaceAudience;
28 import org.apache.hadoop.metrics2.MetricsExecutor;
29 import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
30 import org.apache.hadoop.metrics2.lib.MetricsExecutorImpl;
31 import org.apache.hadoop.util.StringUtils;
32
33 import com.google.common.annotations.VisibleForTesting;
34
35
36
37
38
39
40
41
42
43 @InterfaceAudience.Private
44 public class JmxCacheBuster {
45 private static final Log LOG = LogFactory.getLog(JmxCacheBuster.class);
46 private static AtomicReference<ScheduledFuture> fut = new AtomicReference<>(null);
47 private static MetricsExecutor executor = new MetricsExecutorImpl();
48 private static AtomicBoolean stopped = new AtomicBoolean(false);
49
50 private JmxCacheBuster() {
51
52 }
53
54
55
56
57 public static void clearJmxCache() {
58 if (LOG.isTraceEnabled()) {
59 LOG.trace("clearing JMX Cache" + StringUtils.stringifyException(new Exception()));
60 }
61
62 ScheduledFuture future = fut.get();
63 if ((future != null && (!future.isDone() && future.getDelay(TimeUnit.MILLISECONDS) > 100))) {
64
65 return;
66 }
67 if (stopped.get()) {
68 return;
69 }
70 future = executor.getExecutor().schedule(new JmxCacheBusterRunnable(), 5, TimeUnit.SECONDS);
71 fut.set(future);
72 }
73
74
75
76
77
78 @VisibleForTesting
79 public static void stop() {
80 stopped.set(true);
81 ScheduledFuture future = fut.get();
82 future.cancel(false);
83 }
84
85
86
87
88
89 @VisibleForTesting
90 public static void restart() {
91 stopped.set(false);
92 }
93
94 final static class JmxCacheBusterRunnable implements Runnable {
95 @Override
96 public void run() {
97 if (LOG.isTraceEnabled()) {
98 LOG.trace("Clearing JMX mbean cache.");
99 }
100
101
102
103 try {
104 if (DefaultMetricsSystem.instance() != null) {
105 DefaultMetricsSystem.instance().stop();
106
107
108 Thread.sleep(500);
109 DefaultMetricsSystem.instance().start();
110 }
111 } catch (Exception exception) {
112 LOG.debug("error clearing the jmx it appears the metrics system hasn't been started",
113 exception);
114 }
115 }
116 }
117 }