1 /** 2 * Copyright 2010 The Apache Software Foundation 3 * 4 * Licensed to the Apache Software Foundation (ASF) under one 5 * or more contributor license agreements. See the NOTICE file 6 * distributed with this work for additional information 7 * regarding copyright ownership. The ASF licenses this file 8 * to you under the Apache License, Version 2.0 (the 9 * "License"); you may not use this file except in compliance 10 * with the License. You may obtain a copy of the License at 11 * 12 * http://www.apache.org/licenses/LICENSE-2.0 13 * 14 * Unless required by applicable law or agreed to in writing, software 15 * distributed under the License is distributed on an "AS IS" BASIS, 16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * See the License for the specific language governing permissions and 18 * limitations under the License. 19 */ 20 package org.apache.hadoop.hbase.executor; 21 22 import java.io.IOException; 23 import java.util.concurrent.atomic.AtomicLong; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.hadoop.hbase.Server; 28 29 30 /** 31 * Abstract base class for all HBase event handlers. Subclasses should 32 * implement the {@link #process()} method. Subclasses should also do all 33 * necessary checks up in their constructor if possible -- check table exists, 34 * is disabled, etc. -- so they fail fast rather than later when process is 35 * running. Do it this way because process be invoked directly but event 36 * handlers are also 37 * run in an executor context -- i.e. asynchronously -- and in this case, 38 * exceptions thrown at process time will not be seen by the invoker, not till 39 * we implement a call-back mechanism so the client can pick them up later. 40 * <p> 41 * Event handlers have an {@link EventType}. 42 * {@link EventType} is a list of ALL handler event types. We need to keep 43 * a full list in one place -- and as enums is a good shorthand for an 44 * implemenations -- because event handlers can be passed to executors when 45 * they are to be run asynchronously. The 46 * hbase executor, see {@link ExecutorService}, has a switch for passing 47 * event type to executor. 48 * <p> 49 * Event listeners can be installed and will be called pre- and post- process if 50 * this EventHandler is run in a Thread (its a Runnable so if its {@link #run()} 51 * method gets called). Implement 52 * {@link EventHandlerListener}s, and registering using 53 * {@link #setListener(EventHandlerListener)}. 54 * @see ExecutorService 55 */ 56 public abstract class EventHandler implements Runnable, Comparable<Runnable> { 57 private static final Log LOG = LogFactory.getLog(EventHandler.class); 58 59 // type of event this object represents 60 protected EventType eventType; 61 62 protected Server server; 63 64 // sequence id generator for default FIFO ordering of events 65 protected static AtomicLong seqids = new AtomicLong(0); 66 67 // sequence id for this event 68 private final long seqid; 69 70 // Listener to call pre- and post- processing. May be null. 71 private EventHandlerListener listener; 72 73 // Time to wait for events to happen, should be kept short 74 protected final int waitingTimeForEvents; 75 76 /** 77 * This interface provides pre- and post-process hooks for events. 78 */ 79 public interface EventHandlerListener { 80 /** 81 * Called before any event is processed 82 * @param event The event handler whose process method is about to be called. 83 */ 84 public void beforeProcess(EventHandler event); 85 /** 86 * Called after any event is processed 87 * @param event The event handler whose process method is about to be called. 88 */ 89 public void afterProcess(EventHandler event); 90 } 91 92 /** 93 * List of all HBase event handler types. Event types are named by a 94 * convention: event type names specify the component from which the event 95 * originated and then where its destined -- e.g. RS2ZK_ prefix means the 96 * event came from a regionserver destined for zookeeper -- and then what 97 * the even is; e.g. REGION_OPENING. 98 * 99 * <p>WARNING: Please do not insert, remove or swap any line in this enum 100 * Doing so would change or shift all the codes used to serialize 101 * events, which makes backwards compatibility very hard for clients. 102 */ 103 public enum EventType { 104 // Messages originating from RS (NOTE: there is NO direct communication from 105 // RS to Master). These are a result of RS updates into ZK. 106 //RS_ZK_REGION_CLOSING (1), // It is replaced by M_ZK_REGION_CLOSING(HBASE-4739) 107 RS_ZK_REGION_CLOSED (2), // RS has finished closing a region 108 RS_ZK_REGION_OPENING (3), // RS is in process of opening a region 109 RS_ZK_REGION_OPENED (4), // RS has finished opening a region 110 RS_ZK_REGION_SPLITTING (5), // RS has started a region split 111 RS_ZK_REGION_SPLIT (6), // RS split has completed. 112 RS_ZK_REGION_FAILED_OPEN (7), // RS failed to open a region 113 114 // Messages originating from Master to RS 115 M_RS_OPEN_REGION (20), // Master asking RS to open a region 116 M_RS_OPEN_ROOT (21), // Master asking RS to open root 117 M_RS_OPEN_META (22), // Master asking RS to open meta 118 M_RS_CLOSE_REGION (23), // Master asking RS to close a region 119 M_RS_CLOSE_ROOT (24), // Master asking RS to close root 120 M_RS_CLOSE_META (25), // Master asking RS to close meta 121 122 // Messages originating from Client to Master 123 C_M_DELETE_TABLE (40), // Client asking Master to delete a table 124 C_M_DISABLE_TABLE (41), // Client asking Master to disable a table 125 C_M_ENABLE_TABLE (42), // Client asking Master to enable a table 126 C_M_MODIFY_TABLE (43), // Client asking Master to modify a table 127 C_M_ADD_FAMILY (44), // Client asking Master to add family to table 128 C_M_DELETE_FAMILY (45), // Client asking Master to delete family of table 129 C_M_MODIFY_FAMILY (46), // Client asking Master to modify family of table 130 C_M_CREATE_TABLE (47), // Client asking Master to create a table 131 132 // Updates from master to ZK. This is done by the master and there is 133 // nothing to process by either Master or RS 134 M_ZK_REGION_OFFLINE (50), // Master adds this region as offline in ZK 135 M_ZK_REGION_CLOSING (51), // Master adds this region as closing in ZK 136 137 // Master controlled events to be executed on the master 138 M_SERVER_SHUTDOWN (70), // Master is processing shutdown of a RS 139 M_META_SERVER_SHUTDOWN (72), // Master is processing shutdown of RS hosting a meta region (-ROOT- or .META.). 140 141 // WARNING: Please do not insert, remove or swap any line in this enum. 142 // RegionTransitionData.write() uses eventType.ordinal() that is the enum index 143 // and not the value specified in the enum definition. so we can't add stuff in the middle. 144 C_M_SNAPSHOT_TABLE (48), // Client asking Master to snapshot an offline table 145 C_M_RESTORE_SNAPSHOT (49); // Client asking Master to snapshot an offline table 146 147 /** 148 * Constructor 149 */ 150 EventType(int value) {} 151 public boolean isOnlineSchemaChangeSupported() { 152 return ( 153 this.equals(EventType.C_M_ADD_FAMILY) || 154 this.equals(EventType.C_M_DELETE_FAMILY) || 155 this.equals(EventType.C_M_MODIFY_FAMILY) || 156 this.equals(EventType.C_M_MODIFY_TABLE) 157 ); 158 } 159 } 160 161 /** 162 * Default base class constructor. 163 */ 164 public EventHandler(Server server, EventType eventType) { 165 this.server = server; 166 this.eventType = eventType; 167 seqid = seqids.incrementAndGet(); 168 this.waitingTimeForEvents = server.getConfiguration(). 169 getInt("hbase.master.event.waiting.time", 1000); 170 } 171 172 public void run() { 173 try { 174 if (getListener() != null) getListener().beforeProcess(this); 175 process(); 176 if (getListener() != null) getListener().afterProcess(this); 177 } catch(Throwable t) { 178 LOG.error("Caught throwable while processing event " + eventType, t); 179 } 180 } 181 182 /** 183 * This method is the main processing loop to be implemented by the various 184 * subclasses. 185 * @throws IOException 186 */ 187 public abstract void process() throws IOException; 188 189 /** 190 * Return the event type 191 * @return The event type. 192 */ 193 public EventType getEventType() { 194 return this.eventType; 195 } 196 197 /** 198 * Get the priority level for this handler instance. This uses natural 199 * ordering so lower numbers are higher priority. 200 * <p> 201 * Lowest priority is Integer.MAX_VALUE. Highest priority is 0. 202 * <p> 203 * Subclasses should override this method to allow prioritizing handlers. 204 * <p> 205 * Handlers with the same priority are handled in FIFO order. 206 * <p> 207 * @return Integer.MAX_VALUE by default, override to set higher priorities 208 */ 209 public int getPriority() { 210 return Integer.MAX_VALUE; 211 } 212 213 /** 214 * @return This events' sequence id. 215 */ 216 public long getSeqid() { 217 return this.seqid; 218 } 219 220 /** 221 * Default prioritized runnable comparator which implements a FIFO ordering. 222 * <p> 223 * Subclasses should not override this. Instead, if they want to implement 224 * priority beyond FIFO, they should override {@link #getPriority()}. 225 */ 226 @Override 227 public int compareTo(Runnable o) { 228 EventHandler eh = (EventHandler)o; 229 if(getPriority() != eh.getPriority()) { 230 return (getPriority() < eh.getPriority()) ? -1 : 1; 231 } 232 return (this.seqid < eh.seqid) ? -1 : 1; 233 } 234 235 /** 236 * @return Current listener or null if none set. 237 */ 238 public synchronized EventHandlerListener getListener() { 239 return listener; 240 } 241 242 /** 243 * @param listener Listener to call pre- and post- {@link #process()}. 244 */ 245 public synchronized void setListener(EventHandlerListener listener) { 246 this.listener = listener; 247 } 248 249 @Override 250 public String toString() { 251 return "Event #" + getSeqid() + 252 " of type " + eventType + 253 " (" + getInformativeName() + ")"; 254 } 255 256 /** 257 * Event implementations should override thie class to provide an 258 * informative name about what event they are handling. For example, 259 * event-specific information such as which region or server is 260 * being processed should be included if possible. 261 */ 262 public String getInformativeName() { 263 return this.getClass().toString(); 264 } 265 }