1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
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.AtomicReference;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.hbase.classification.InterfaceAudience;
27 import org.apache.hadoop.metrics2.MetricsExecutor;
28 import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
29 import org.apache.hadoop.metrics2.lib.MetricsExecutorImpl;
30
31 /**
32 * JMX caches the beans that have been exported; even after the values are removed from hadoop's
33 * metrics system the keys and old values will still remain. This class stops and restarts the
34 * Hadoop metrics system, forcing JMX to clear the cache of exported metrics.
35 *
36 * This class need to be in the o.a.h.metrics2.impl namespace as many of the variables/calls used
37 * are package private.
38 */
39 @InterfaceAudience.Private
40 public class JmxCacheBuster {
41 private static final Log LOG = LogFactory.getLog(JmxCacheBuster.class);
42 private static AtomicReference<ScheduledFuture> fut = new AtomicReference<>(null);
43 private static MetricsExecutor executor = new MetricsExecutorImpl();
44
45 private JmxCacheBuster() {
46 // Static only cache.
47 }
48
49 /**
50 * For JMX to forget about all previously exported metrics.
51 */
52 public static void clearJmxCache() {
53 //If there are more then 100 ms before the executor will run then everything should be merged.
54 ScheduledFuture future = fut.get();
55 if ((future != null && (!future.isDone() && future.getDelay(TimeUnit.MILLISECONDS) > 100))) {
56 // BAIL OUT
57 return;
58 }
59 future = executor.getExecutor().schedule(new JmxCacheBusterRunnable(), 5, TimeUnit.SECONDS);
60 fut.set(future);
61 }
62
63 final static class JmxCacheBusterRunnable implements Runnable {
64 @Override
65 public void run() {
66 if (LOG.isTraceEnabled()) {
67 LOG.trace("Clearing JMX mbean cache.");
68 }
69
70 // This is pretty extreme but it's the best way that
71 // I could find to get metrics to be removed.
72 try {
73 if (DefaultMetricsSystem.instance() != null) {
74 DefaultMetricsSystem.instance().stop();
75 // Sleep some time so that the rest of the hadoop metrics
76 // system knows that things are done
77 Thread.sleep(500);
78 DefaultMetricsSystem.instance().start();
79 }
80 } catch (Exception exception) {
81 LOG.debug("error clearing the jmx it appears the metrics system hasn't been started",
82 exception);
83 }
84 }
85 }
86 }