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 org.apache.hadoop.conf.Configuration; 021import org.apache.yetus.audience.InterfaceAudience; 022import org.apache.yetus.audience.InterfaceStability; 023import org.slf4j.Logger; 024import org.slf4j.LoggerFactory; 025 026import java.util.Collections; 027import java.util.Set; 028import java.util.WeakHashMap; 029 030/** 031 * Maintains the set of all the classes which would like to get notified 032 * when the Configuration is reloaded from the disk in the Online Configuration 033 * Change mechanism, which lets you update certain configuration properties 034 * on-the-fly, without having to restart the cluster. 035 * 036 * If a class has configuration properties which you would like to be able to 037 * change on-the-fly, do the following: 038 * 1. 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 * 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. 055 * 056 * 2. Register the appropriate instance of the class with the 057 * {@link ConfigurationManager} instance, using the 058 * {@link ConfigurationManager#registerObserver(ConfigurationObserver)} 059 * method. For the RS side of things, the ConfigurationManager is a static 060 * member of the {@link org.apache.hadoop.hbase.regionserver.HRegionServer} 061 * class. Be careful not to do this in the constructor, as you might cause 062 * the 'this' reference to escape. Use a factory method, or an initialize() 063 * method which is called after the construction of the object. 064 * 065 * 3. Deregister the instance using the 066 * {@link ConfigurationManager#deregisterObserver(ConfigurationObserver)} 067 * method when it is going out of scope. In case you are not able to do that 068 * for any reason, it is still okay, since entries for dead observers are 069 * automatically collected during GC. But nonetheless, it is still a good 070 * practice to deregister your observer, whenever possible. 071 * 072 */ 073@InterfaceAudience.Private 074@InterfaceStability.Evolving 075public class ConfigurationManager { 076 private static final Logger LOG = LoggerFactory.getLogger(ConfigurationManager.class); 077 078 // The set of Configuration Observers. These classes would like to get 079 // notified when the configuration is reloaded from disk. This is a set 080 // constructed from a WeakHashMap, whose entries would be removed if the 081 // observer classes go out of scope. 082 private final Set<ConfigurationObserver> configurationObservers = 083 Collections.newSetFromMap(new WeakHashMap<ConfigurationObserver, 084 Boolean>()); 085 086 /** 087 * Register an observer class 088 * @param observer 089 */ 090 public void registerObserver(ConfigurationObserver observer) { 091 synchronized (configurationObservers) { 092 configurationObservers.add(observer); 093 if (observer instanceof PropagatingConfigurationObserver) { 094 ((PropagatingConfigurationObserver) observer).registerChildren(this); 095 } 096 } 097 } 098 099 /** 100 * Deregister an observer class 101 * @param observer 102 */ 103 public void deregisterObserver(ConfigurationObserver observer) { 104 synchronized (configurationObservers) { 105 configurationObservers.remove(observer); 106 if (observer instanceof PropagatingConfigurationObserver) { 107 ((PropagatingConfigurationObserver) observer).deregisterChildren(this); 108 } 109 } 110 } 111 112 /** 113 * The conf object has been repopulated from disk, and we have to notify 114 * all the observers that are expressed interest to do that. 115 */ 116 public void notifyAllObservers(Configuration conf) { 117 LOG.info("Starting to notify all observers that config changed."); 118 synchronized (configurationObservers) { 119 for (ConfigurationObserver observer : configurationObservers) { 120 try { 121 if (observer != null) { 122 observer.onConfigurationChange(conf); 123 } 124 } catch (Throwable t) { 125 LOG.error("Encountered a throwable while notifying observers: " + " of type : " + 126 observer.getClass().getCanonicalName() + "(" + observer + ")", t); 127 } 128 } 129 } 130 } 131 132 /** 133 * @return the number of observers. 134 */ 135 public int getNumObservers() { 136 synchronized (configurationObservers) { 137 return configurationObservers.size(); 138 } 139 } 140}