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.hbase.conf;
019
020import java.util.Collections;
021import java.util.Set;
022import java.util.WeakHashMap;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.yetus.audience.InterfaceAudience;
025import org.apache.yetus.audience.InterfaceStability;
026import org.slf4j.Logger;
027import org.slf4j.LoggerFactory;
028
029/**
030 * Maintains the set of all the classes which would like to get notified
031 * when the Configuration is reloaded from the disk in the Online Configuration
032 * Change mechanism, which lets you update certain configuration properties
033 * on-the-fly, without having to restart the cluster.
034 * <p>
035 * If a class has configuration properties which you would like to be able to
036 * change on-the-fly, do the following:
037 * <ol>
038 *   <li>Implement the {@link ConfigurationObserver} interface. This would require
039 *    you to implement the
040 *    {@link ConfigurationObserver#onConfigurationChange(Configuration)}
041 *    method.  This is a callback that is used to notify your class' instance
042 *    that the configuration has changed. In this method, you need to check
043 *    if the new values for the properties that are of interest to your class
044 *    are different from the cached values. If yes, update them.
045 *    <br />
046 *    However, be careful with this. Certain properties might be trivially
047 *    mutable online, but others might not. Two properties might be trivially
048 *    mutable by themselves, but not when changed together. For example, if a
049 *    method uses properties "a" and "b" to make some decision, and is running
050 *    in parallel when the notifyOnChange() method updates "a", but hasn't
051 *    yet updated "b", it might make a decision on the basis of a new value of
052 *    "a", and an old value of "b". This might introduce subtle bugs. This
053 *    needs to be dealt on a case-by-case basis, and this class does not provide
054 *    any protection from such cases.</li>
055 *
056 *   <li>Register the appropriate instance of the class with the
057 *    {@link ConfigurationManager} instance, using the
058 *    {@link ConfigurationManager#registerObserver(ConfigurationObserver)}
059 *    method. Be careful not to do this in the constructor, as you might cause
060 *    the 'this' reference to escape. Use a factory method, or an initialize()
061 *    method which is called after the construction of the object.</li>
062 *
063 *   <li>Deregister the instance using the
064 *    {@link ConfigurationManager#deregisterObserver(ConfigurationObserver)}
065 *    method when it is going out of scope. In case you are not able to do that
066 *    for any reason, it is still okay, since entries for dead observers are
067 *    automatically collected during GC. But nonetheless, it is still a good
068 *    practice to deregister your observer, whenever possible.</li>
069 * </ol>
070 * </p>
071 */
072@InterfaceAudience.Private
073@InterfaceStability.Evolving
074public class ConfigurationManager {
075  private static final Logger LOG = LoggerFactory.getLogger(ConfigurationManager.class);
076
077  // The set of Configuration Observers. These classes would like to get
078  // notified when the configuration is reloaded from disk. This is a set
079  // constructed from a WeakHashMap, whose entries would be removed if the
080  // observer classes go out of scope.
081  private final Set<ConfigurationObserver> configurationObservers =
082      Collections.newSetFromMap(new WeakHashMap<>());
083
084  /**
085   * Register an observer class
086   * @param observer observer to be registered.
087   */
088  public void registerObserver(ConfigurationObserver observer) {
089    synchronized (configurationObservers) {
090      configurationObservers.add(observer);
091      if (observer instanceof PropagatingConfigurationObserver) {
092        ((PropagatingConfigurationObserver) observer).registerChildren(this);
093      }
094    }
095  }
096
097  /**
098   * Deregister an observer class
099   * @param observer to be deregistered.
100   */
101  public void deregisterObserver(ConfigurationObserver observer) {
102    synchronized (configurationObservers) {
103      configurationObservers.remove(observer);
104      if (observer instanceof PropagatingConfigurationObserver) {
105        ((PropagatingConfigurationObserver) observer).deregisterChildren(this);
106      }
107    }
108  }
109
110  /**
111   * The conf object has been repopulated from disk, and we have to notify
112   * all the observers that are expressed interest to do that.
113   */
114  public void notifyAllObservers(Configuration conf) {
115    LOG.info("Starting to notify all observers that config changed.");
116    synchronized (configurationObservers) {
117      for (ConfigurationObserver observer : configurationObservers) {
118        try {
119          if (observer != null) {
120            observer.onConfigurationChange(conf);
121          }
122        } catch (Throwable t) {
123          LOG.error("Encountered a throwable while notifying observers: of type : {}({})",
124            observer.getClass().getCanonicalName(), observer, t);
125        }
126      }
127    }
128  }
129
130  /**
131   * @return the number of observers. 
132   */
133  public int getNumObservers() {
134    synchronized (configurationObservers) {
135      return configurationObservers.size();
136    }
137  }
138}