1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.metrics2.impl;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.hadoop.metrics2.MetricsExecutor;
24 import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
25 import org.apache.hadoop.metrics2.lib.MetricsExecutorImpl;
26
27 import java.util.concurrent.ScheduledFuture;
28 import java.util.concurrent.TimeUnit;
29
30
31
32
33
34
35
36
37
38 @edu.umd.cs.findbugs.annotations.SuppressWarnings(
39 value="LI_LAZY_INIT_STATIC",
40 justification="Yeah, its weird but its what we want")
41 public class JmxCacheBuster {
42 private static final Log LOG = LogFactory.getLog(JmxCacheBuster.class);
43 private static Object lock = new Object();
44 private static ScheduledFuture fut = null;
45 private static MetricsExecutor executor = new MetricsExecutorImpl();
46
47
48
49
50 public static void clearJmxCache() {
51
52
53 if (fut == null || (!fut.isDone() && fut.getDelay(TimeUnit.MILLISECONDS) > 100)) return;
54
55 synchronized (lock) {
56 fut = executor.getExecutor().schedule(new JmxCacheBusterRunnable(), 5, TimeUnit.SECONDS);
57 }
58 }
59
60 static class JmxCacheBusterRunnable implements Runnable {
61
62 @Override
63 public void run() {
64 LOG.trace("Clearing JMX mbean cache.");
65
66
67
68 try {
69 if (DefaultMetricsSystem.instance() != null ) {
70 DefaultMetricsSystem.instance().stop();
71 DefaultMetricsSystem.instance().start();
72 }
73
74 } catch (Exception exception ) {
75 LOG.debug("error clearing the jmx it appears the metrics system hasn't been started", exception);
76 }
77 }
78 }
79 }