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