001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.metrics2.impl;
019
020import java.util.concurrent.ScheduledFuture;
021import java.util.concurrent.TimeUnit;
022import java.util.concurrent.atomic.AtomicReference;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026import org.apache.hadoop.hbase.classification.InterfaceAudience;
027import org.apache.hadoop.metrics2.MetricsExecutor;
028import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
029import org.apache.hadoop.metrics2.lib.MetricsExecutorImpl;
030
031/**
032 * JMX caches the beans that have been exported; even after the values are removed from hadoop's
033 * metrics system the keys and old values will still remain.  This class stops and restarts the
034 * Hadoop metrics system, forcing JMX to clear the cache of exported metrics.
035 *
036 * This class need to be in the o.a.h.metrics2.impl namespace as many of the variables/calls used
037 * are package private.
038 */
039@InterfaceAudience.Private
040public class JmxCacheBuster {
041  private static final Log LOG = LogFactory.getLog(JmxCacheBuster.class);
042  private static AtomicReference<ScheduledFuture> fut = new AtomicReference<>(null);
043  private static MetricsExecutor executor = new MetricsExecutorImpl();
044
045  private JmxCacheBuster() {
046    // Static only cache.
047  }
048
049  /**
050   * For JMX to forget about all previously exported metrics.
051   */
052  public static void clearJmxCache() {
053    //If there are more then 100 ms before the executor will run then everything should be merged.
054    ScheduledFuture future = fut.get();
055    if ((future == null || (!future.isDone() && future.getDelay(TimeUnit.MILLISECONDS) > 100))) {
056      // BAIL OUT
057      return;
058    }
059    future = executor.getExecutor().schedule(new JmxCacheBusterRunnable(), 5, TimeUnit.SECONDS);
060    fut.set(future);
061  }
062
063  final static class JmxCacheBusterRunnable implements Runnable {
064    @Override
065    public void run() {
066      if (LOG.isTraceEnabled()) {
067        LOG.trace("Clearing JMX mbean cache.");
068      }
069
070      // This is pretty extreme but it's the best way that
071      // I could find to get metrics to be removed.
072      try {
073        if (DefaultMetricsSystem.instance() != null) {
074          DefaultMetricsSystem.instance().stop();
075          // Sleep some time so that the rest of the hadoop metrics
076          // system knows that things are done
077          Thread.sleep(500);
078          DefaultMetricsSystem.instance().start();
079        }
080      }  catch (Exception exception)  {
081        LOG.debug("error clearing the jmx it appears the metrics system hasn't been started",
082            exception);
083      }
084    }
085  }
086}