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), // RS is in process of closing a region 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 111 // Messages originating from Master to RS 112 M_RS_OPEN_REGION (20), // Master asking RS to open a region 113 M_RS_OPEN_ROOT (21), // Master asking RS to open root 114 M_RS_OPEN_META (22), // Master asking RS to open meta 115 M_RS_CLOSE_REGION (23), // Master asking RS to close a region 116 M_RS_CLOSE_ROOT (24), // Master asking RS to close root 117 M_RS_CLOSE_META (25), // Master asking RS to close meta 118 119 // Messages originating from Client to Master 120 C_M_DELETE_TABLE (40), // Client asking Master to delete a table 121 C_M_DISABLE_TABLE (41), // Client asking Master to disable a table 122 C_M_ENABLE_TABLE (42), // Client asking Master to enable a table 123 C_M_MODIFY_TABLE (43), // Client asking Master to modify a table 124 C_M_ADD_FAMILY (44), // Client asking Master to add family to table 125 C_M_DELETE_FAMILY (45), // Client asking Master to delete family of table 126 C_M_MODIFY_FAMILY (46), // Client asking Master to modify family of table 127 128 // Updates from master to ZK. This is done by the master and there is 129 // nothing to process by either Master or RS 130 M_ZK_REGION_OFFLINE (50), // Master adds this region as offline in ZK 131 132 // Master controlled events to be executed on the master 133 M_SERVER_SHUTDOWN (70), // Master is processing shutdown of a RS 134 M_META_SERVER_SHUTDOWN (72); // Master is processing shutdown of RS hosting a meta region (-ROOT- or .META.). 135 136 /** 137 * Constructor 138 */ 139 EventType(int value) {} 140 } 141 142 /** 143 * Default base class constructor. 144 */ 145 public EventHandler(Server server, EventType eventType) { 146 this.server = server; 147 this.eventType = eventType; 148 seqid = seqids.incrementAndGet(); 149 this.waitingTimeForEvents = server.getConfiguration(). 150 getInt("hbase.master.event.waiting.time", 1000); 151 } 152 153 public void run() { 154 try { 155 if (getListener() != null) getListener().beforeProcess(this); 156 process(); 157 if (getListener() != null) getListener().afterProcess(this); 158 } catch(Throwable t) { 159 LOG.error("Caught throwable while processing event " + eventType, t); 160 } 161 } 162 163 /** 164 * This method is the main processing loop to be implemented by the various 165 * subclasses. 166 * @throws IOException 167 */ 168 public abstract void process() throws IOException; 169 170 /** 171 * Return the event type 172 * @return The event type. 173 */ 174 public EventType getEventType() { 175 return this.eventType; 176 } 177 178 /** 179 * Get the priority level for this handler instance. This uses natural 180 * ordering so lower numbers are higher priority. 181 * <p> 182 * Lowest priority is Integer.MAX_VALUE. Highest priority is 0. 183 * <p> 184 * Subclasses should override this method to allow prioritizing handlers. 185 * <p> 186 * Handlers with the same priority are handled in FIFO order. 187 * <p> 188 * @return Integer.MAX_VALUE by default, override to set higher priorities 189 */ 190 public int getPriority() { 191 return Integer.MAX_VALUE; 192 } 193 194 /** 195 * @return This events' sequence id. 196 */ 197 public long getSeqid() { 198 return this.seqid; 199 } 200 201 /** 202 * Default prioritized runnable comparator which implements a FIFO ordering. 203 * <p> 204 * Subclasses should not override this. Instead, if they want to implement 205 * priority beyond FIFO, they should override {@link #getPriority()}. 206 */ 207 @Override 208 public int compareTo(Runnable o) { 209 EventHandler eh = (EventHandler)o; 210 if(getPriority() != eh.getPriority()) { 211 return (getPriority() < eh.getPriority()) ? -1 : 1; 212 } 213 return (this.seqid < eh.seqid) ? -1 : 1; 214 } 215 216 /** 217 * @return Current listener or null if none set. 218 */ 219 public synchronized EventHandlerListener getListener() { 220 return listener; 221 } 222 223 /** 224 * @param listener Listener to call pre- and post- {@link #process()}. 225 */ 226 public synchronized void setListener(EventHandlerListener listener) { 227 this.listener = listener; 228 } 229 }