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