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>We give the enums indices so we can add types later and keep them 100 * grouped together rather than have to add them always to the end as we 101 * would have to if we used raw enum ordinals. 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 C_M_SNAPSHOT_TABLE (48), // Client asking Master to snapshot an offline table 132 C_M_RESTORE_SNAPSHOT (49), // Client asking Master to snapshot an offline table 133 134 // Updates from master to ZK. This is done by the master and there is 135 // nothing to process by either Master or RS 136 M_ZK_REGION_OFFLINE (50), // Master adds this region as offline in ZK 137 M_ZK_REGION_CLOSING (51), // Master adds this region as closing in ZK 138 139 // Master controlled events to be executed on the master 140 M_SERVER_SHUTDOWN (70), // Master is processing shutdown of a RS 141 M_META_SERVER_SHUTDOWN (72); // Master is processing shutdown of RS hosting a meta region (-ROOT- or .META.). 142 143 /** 144 * Constructor 145 */ 146 EventType(int value) {} 147 public boolean isOnlineSchemaChangeSupported() { 148 return ( 149 this.equals(EventType.C_M_ADD_FAMILY) || 150 this.equals(EventType.C_M_DELETE_FAMILY) || 151 this.equals(EventType.C_M_MODIFY_FAMILY) || 152 this.equals(EventType.C_M_MODIFY_TABLE) 153 ); 154 } 155 } 156 157 /** 158 * Default base class constructor. 159 */ 160 public EventHandler(Server server, EventType eventType) { 161 this.server = server; 162 this.eventType = eventType; 163 seqid = seqids.incrementAndGet(); 164 this.waitingTimeForEvents = server.getConfiguration(). 165 getInt("hbase.master.event.waiting.time", 1000); 166 } 167 168 public void run() { 169 try { 170 if (getListener() != null) getListener().beforeProcess(this); 171 process(); 172 if (getListener() != null) getListener().afterProcess(this); 173 } catch(Throwable t) { 174 LOG.error("Caught throwable while processing event " + eventType, t); 175 } 176 } 177 178 /** 179 * This method is the main processing loop to be implemented by the various 180 * subclasses. 181 * @throws IOException 182 */ 183 public abstract void process() throws IOException; 184 185 /** 186 * Return the event type 187 * @return The event type. 188 */ 189 public EventType getEventType() { 190 return this.eventType; 191 } 192 193 /** 194 * Get the priority level for this handler instance. This uses natural 195 * ordering so lower numbers are higher priority. 196 * <p> 197 * Lowest priority is Integer.MAX_VALUE. Highest priority is 0. 198 * <p> 199 * Subclasses should override this method to allow prioritizing handlers. 200 * <p> 201 * Handlers with the same priority are handled in FIFO order. 202 * <p> 203 * @return Integer.MAX_VALUE by default, override to set higher priorities 204 */ 205 public int getPriority() { 206 return Integer.MAX_VALUE; 207 } 208 209 /** 210 * @return This events' sequence id. 211 */ 212 public long getSeqid() { 213 return this.seqid; 214 } 215 216 /** 217 * Default prioritized runnable comparator which implements a FIFO ordering. 218 * <p> 219 * Subclasses should not override this. Instead, if they want to implement 220 * priority beyond FIFO, they should override {@link #getPriority()}. 221 */ 222 @Override 223 public int compareTo(Runnable o) { 224 EventHandler eh = (EventHandler)o; 225 if(getPriority() != eh.getPriority()) { 226 return (getPriority() < eh.getPriority()) ? -1 : 1; 227 } 228 return (this.seqid < eh.seqid) ? -1 : 1; 229 } 230 231 /** 232 * @return Current listener or null if none set. 233 */ 234 public synchronized EventHandlerListener getListener() { 235 return listener; 236 } 237 238 /** 239 * @param listener Listener to call pre- and post- {@link #process()}. 240 */ 241 public synchronized void setListener(EventHandlerListener listener) { 242 this.listener = listener; 243 } 244 245 @Override 246 public String toString() { 247 return "Event #" + getSeqid() + 248 " of type " + eventType + 249 " (" + getInformativeName() + ")"; 250 } 251 252 /** 253 * Event implementations should override thie class to provide an 254 * informative name about what event they are handling. For example, 255 * event-specific information such as which region or server is 256 * being processed should be included if possible. 257 */ 258 public String getInformativeName() { 259 return this.getClass().toString(); 260 } 261 }