View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.master;
21  
22  import org.apache.hadoop.classification.InterfaceAudience;
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.*;
25  import org.apache.hadoop.hbase.coprocessor.*;
26  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
27  
28  import java.io.IOException;
29  import java.util.List;
30  
31  /**
32   * Provides the coprocessor framework and environment for master oriented
33   * operations.  {@link HMaster} interacts with the loaded coprocessors
34   * through this class.
35   */
36  @InterfaceAudience.Private
37  public class MasterCoprocessorHost
38      extends CoprocessorHost<MasterCoprocessorHost.MasterEnvironment> {
39  
40    /**
41     * Coprocessor environment extension providing access to master related
42     * services.
43     */
44    static class MasterEnvironment extends CoprocessorHost.Environment
45        implements MasterCoprocessorEnvironment {
46      private MasterServices masterServices;
47  
48      public MasterEnvironment(final Class<?> implClass, final Coprocessor impl,
49          final int priority, final int seq, final Configuration conf,
50          final MasterServices services) {
51        super(impl, priority, seq, conf);
52        this.masterServices = services;
53      }
54  
55      public MasterServices getMasterServices() {
56        return masterServices;
57      }
58    }
59  
60    private MasterServices masterServices;
61  
62    MasterCoprocessorHost(final MasterServices services, final Configuration conf) {
63      super(services);
64      this.conf = conf;
65      this.masterServices = services;
66      loadSystemCoprocessors(conf, MASTER_COPROCESSOR_CONF_KEY);
67    }
68  
69    @Override
70    public MasterEnvironment createEnvironment(final Class<?> implClass,
71        final Coprocessor instance, final int priority, final int seq,
72        final Configuration conf) {
73      for (Class<?> c : implClass.getInterfaces()) {
74        if (CoprocessorService.class.isAssignableFrom(c)) {
75          masterServices.registerService(((CoprocessorService)instance).getService());
76        }
77      }
78      return new MasterEnvironment(implClass, instance, priority, seq, conf,
79          masterServices);
80    }
81  
82    public boolean preCreateNamespace(final NamespaceDescriptor ns) throws IOException {
83      boolean bypass = false;
84      ObserverContext<MasterCoprocessorEnvironment> ctx = null;
85      for (MasterEnvironment env: coprocessors) {
86        if (env.getInstance() instanceof MasterObserver) {
87          ctx = ObserverContext.createAndPrepare(env, ctx);
88          Thread currentThread = Thread.currentThread();
89          ClassLoader cl = currentThread.getContextClassLoader();
90          try {
91            currentThread.setContextClassLoader(env.getClassLoader());
92            ((MasterObserver)env.getInstance()).preCreateNamespace(ctx, ns);
93          } catch (Throwable e) {
94            handleCoprocessorThrowable(env, e);
95          } finally {
96            currentThread.setContextClassLoader(cl);
97          }
98          bypass |= ctx.shouldBypass();
99          if (ctx.shouldComplete()) {
100           break;
101         }
102       }
103     }
104     return bypass;
105   }
106 
107   public void postCreateNamespace(final NamespaceDescriptor ns) throws IOException {
108     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
109     for (MasterEnvironment env: coprocessors) {
110       if (env.getInstance() instanceof MasterObserver) {
111         ctx = ObserverContext.createAndPrepare(env, ctx);
112         Thread currentThread = Thread.currentThread();
113         ClassLoader cl = currentThread.getContextClassLoader();
114         try {
115           currentThread.setContextClassLoader(env.getClassLoader());
116           ((MasterObserver)env.getInstance()).postCreateNamespace(ctx, ns);
117         } catch (Throwable e) {
118           handleCoprocessorThrowable(env, e);
119         } finally {
120           currentThread.setContextClassLoader(cl);
121         }
122         if (ctx.shouldComplete()) {
123           break;
124         }
125       }
126     }
127   }
128 
129   public boolean preDeleteNamespace(final String namespaceName) throws IOException {
130     boolean bypass = false;
131     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
132     for (MasterEnvironment env: coprocessors) {
133       if (env.getInstance() instanceof MasterObserver) {
134         ctx = ObserverContext.createAndPrepare(env, ctx);
135         Thread currentThread = Thread.currentThread();
136         ClassLoader cl = currentThread.getContextClassLoader();
137         try {
138           currentThread.setContextClassLoader(env.getClassLoader());
139           ((MasterObserver)env.getInstance()).preDeleteNamespace(ctx, namespaceName);
140         } catch (Throwable e) {
141           handleCoprocessorThrowable(env, e);
142         } finally {
143           currentThread.setContextClassLoader(cl);
144         }
145         bypass |= ctx.shouldBypass();
146         if (ctx.shouldComplete()) {
147           break;
148         }
149       }
150     }
151     return bypass;
152   }
153 
154   public void postDeleteNamespace(final String namespaceName) throws IOException {
155     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
156     for (MasterEnvironment env: coprocessors) {
157       if (env.getInstance() instanceof MasterObserver) {
158         ctx = ObserverContext.createAndPrepare(env, ctx);
159         Thread currentThread = Thread.currentThread();
160         ClassLoader cl = currentThread.getContextClassLoader();
161         try {
162           currentThread.setContextClassLoader(env.getClassLoader());
163           ((MasterObserver)env.getInstance()).postDeleteNamespace(ctx, namespaceName);
164         } catch (Throwable e) {
165           handleCoprocessorThrowable(env, e);
166         } finally {
167           currentThread.setContextClassLoader(cl);
168         }
169         if (ctx.shouldComplete()) {
170           break;
171         }
172       }
173     }
174   }
175 
176   public boolean preModifyNamespace(final NamespaceDescriptor ns) throws IOException {
177     boolean bypass = false;
178     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
179     for (MasterEnvironment env: coprocessors) {
180       if (env.getInstance() instanceof MasterObserver) {
181         ctx = ObserverContext.createAndPrepare(env, ctx);
182         Thread currentThread = Thread.currentThread();
183         ClassLoader cl = currentThread.getContextClassLoader();
184         try {
185           currentThread.setContextClassLoader(env.getClassLoader());
186           ((MasterObserver)env.getInstance()).preModifyNamespace(ctx, ns);
187         } catch (Throwable e) {
188           handleCoprocessorThrowable(env, e);
189         } finally {
190           currentThread.setContextClassLoader(cl);
191         }
192         bypass |= ctx.shouldBypass();
193         if (ctx.shouldComplete()) {
194           break;
195         }
196       }
197     }
198     return bypass;
199   }
200 
201   public void postModifyNamespace(final NamespaceDescriptor ns) throws IOException {
202     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
203     for (MasterEnvironment env: coprocessors) {
204       if (env.getInstance() instanceof MasterObserver) {
205         ctx = ObserverContext.createAndPrepare(env, ctx);
206         Thread currentThread = Thread.currentThread();
207         ClassLoader cl = currentThread.getContextClassLoader();
208         try {
209           currentThread.setContextClassLoader(env.getClassLoader());
210           ((MasterObserver)env.getInstance()).postModifyNamespace(ctx, ns);
211         } catch (Throwable e) {
212           handleCoprocessorThrowable(env, e);
213         } finally {
214           currentThread.setContextClassLoader(cl);
215         }
216         if (ctx.shouldComplete()) {
217           break;
218         }
219       }
220     }
221   }
222 
223   /* Implementation of hooks for invoking MasterObservers */
224 
225   public void preCreateTable(final HTableDescriptor htd, final HRegionInfo[] regions)
226     throws IOException {
227     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
228     for (MasterEnvironment env: coprocessors) {
229       if (env.getInstance() instanceof MasterObserver) {
230         ctx = ObserverContext.createAndPrepare(env, ctx);
231         Thread currentThread = Thread.currentThread();
232         ClassLoader cl = currentThread.getContextClassLoader();
233         try {
234           currentThread.setContextClassLoader(env.getClassLoader());
235           ((MasterObserver)env.getInstance()).preCreateTable(ctx, htd, regions);
236         } catch (Throwable e) {
237           handleCoprocessorThrowable(env, e);
238         } finally {
239           currentThread.setContextClassLoader(cl);
240         }
241         if (ctx.shouldComplete()) {
242           break;
243         }
244       }
245     }
246   }
247 
248   public void postCreateTable(final HTableDescriptor htd, final HRegionInfo[] regions)
249     throws IOException {
250     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
251     for (MasterEnvironment env: coprocessors) {
252       if (env.getInstance() instanceof MasterObserver) {
253         ctx = ObserverContext.createAndPrepare(env, ctx);
254         Thread currentThread = Thread.currentThread();
255         ClassLoader cl = currentThread.getContextClassLoader();
256         try {
257           currentThread.setContextClassLoader(env.getClassLoader());
258           ((MasterObserver)env.getInstance()).postCreateTable(ctx, htd, regions);
259         } catch (Throwable e) {
260           handleCoprocessorThrowable(env, e);
261         } finally {
262           currentThread.setContextClassLoader(cl);
263         }
264         if (ctx.shouldComplete()) {
265           break;
266         }
267       }
268     }
269   }
270 
271   public void preCreateTableHandler(final HTableDescriptor htd, final HRegionInfo[] regions)
272       throws IOException {
273     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
274     for (MasterEnvironment env : coprocessors) {
275       if (env.getInstance() instanceof MasterObserver) {
276         ctx = ObserverContext.createAndPrepare(env, ctx);
277         Thread currentThread = Thread.currentThread();
278         ClassLoader cl = currentThread.getContextClassLoader();
279         try {
280           currentThread.setContextClassLoader(env.getClassLoader());
281           ((MasterObserver) env.getInstance()).preCreateTableHandler(ctx, htd, regions);
282         } catch (Throwable e) {
283           handleCoprocessorThrowable(env, e);
284         } finally {
285           currentThread.setContextClassLoader(cl);
286         }
287         if (ctx.shouldComplete()) {
288           break;
289         }
290       }
291     }
292   }
293 
294   public void postCreateTableHandler(final HTableDescriptor htd, final HRegionInfo[] regions)
295       throws IOException {
296     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
297     for (MasterEnvironment env : coprocessors) {
298       if (env.getInstance() instanceof MasterObserver) {
299         ctx = ObserverContext.createAndPrepare(env, ctx);
300         Thread currentThread = Thread.currentThread();
301         ClassLoader cl = currentThread.getContextClassLoader();
302         try {
303           currentThread.setContextClassLoader(env.getClassLoader());
304           ((MasterObserver) env.getInstance()).postCreateTableHandler(ctx, htd, regions);
305         } catch (Throwable e) {
306           handleCoprocessorThrowable(env, e);
307         } finally {
308           currentThread.setContextClassLoader(cl);
309         }
310         if (ctx.shouldComplete()) {
311           break;
312         }
313       }
314     }
315   }
316 
317   public void preDeleteTable(final TableName tableName) throws IOException {
318     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
319     for (MasterEnvironment env: coprocessors) {
320       if (env.getInstance() instanceof MasterObserver) {
321         ctx = ObserverContext.createAndPrepare(env, ctx);
322         Thread currentThread = Thread.currentThread();
323         ClassLoader cl = currentThread.getContextClassLoader();
324         try {
325           currentThread.setContextClassLoader(env.getClassLoader());
326           ((MasterObserver)env.getInstance()).preDeleteTable(ctx, tableName);
327         } catch (Throwable e) {
328           handleCoprocessorThrowable(env, e);
329         } finally {
330           currentThread.setContextClassLoader(cl);
331         }
332         if (ctx.shouldComplete()) {
333           break;
334         }
335       }
336     }
337   }
338 
339   public void postDeleteTable(final TableName tableName) throws IOException {
340     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
341     for (MasterEnvironment env: coprocessors) {
342       if (env.getInstance() instanceof MasterObserver) {
343         ctx = ObserverContext.createAndPrepare(env, ctx);
344         Thread currentThread = Thread.currentThread();
345         ClassLoader cl = currentThread.getContextClassLoader();
346         try {
347           currentThread.setContextClassLoader(env.getClassLoader());
348           ((MasterObserver)env.getInstance()).postDeleteTable(ctx, tableName);
349         } catch (Throwable e) {
350           handleCoprocessorThrowable(env, e);
351         } finally {
352           currentThread.setContextClassLoader(cl);
353         }
354         if (ctx.shouldComplete()) {
355           break;
356         }
357       }
358     }
359   }
360 
361   public void preDeleteTableHandler(final TableName tableName) throws IOException {
362     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
363     for (MasterEnvironment env : coprocessors) {
364       if (env.getInstance() instanceof MasterObserver) {
365         ctx = ObserverContext.createAndPrepare(env, ctx);
366         Thread currentThread = Thread.currentThread();
367         ClassLoader cl = currentThread.getContextClassLoader();
368         try {
369           currentThread.setContextClassLoader(env.getClassLoader());
370           ((MasterObserver) env.getInstance()).preDeleteTableHandler(ctx, tableName);
371         } catch (Throwable e) {
372           handleCoprocessorThrowable(env, e);
373         } finally {
374           currentThread.setContextClassLoader(cl);
375         }
376         if (ctx.shouldComplete()) {
377           break;
378         }
379       }
380     }
381   }
382 
383   public void postDeleteTableHandler(final TableName tableName) throws IOException {
384     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
385     for (MasterEnvironment env : coprocessors) {
386       if (env.getInstance() instanceof MasterObserver) {
387         ctx = ObserverContext.createAndPrepare(env, ctx);
388         Thread currentThread = Thread.currentThread();
389         ClassLoader cl = currentThread.getContextClassLoader();
390         try {
391           currentThread.setContextClassLoader(env.getClassLoader());
392           ((MasterObserver) env.getInstance()).postDeleteTableHandler(ctx, tableName);
393         } catch (Throwable e) {
394           handleCoprocessorThrowable(env, e);
395         } finally {
396           currentThread.setContextClassLoader(cl);
397         }
398         if (ctx.shouldComplete()) {
399           break;
400         }
401       }
402     }
403   }
404 
405   public void preTruncateTable(TableName tableName) throws IOException {
406     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
407     for (MasterEnvironment env: coprocessors) {
408       if (env.getInstance() instanceof MasterObserver) {
409         ctx = ObserverContext.createAndPrepare(env, ctx);
410         try {
411           ((MasterObserver)env.getInstance()).preTruncateTable(ctx, tableName);
412         } catch (Throwable e) {
413           handleCoprocessorThrowable(env, e);
414         }
415         if (ctx.shouldComplete()) {
416           break;
417         }
418       }
419     }
420   }
421 
422   public void postTruncateTable(TableName tableName) throws IOException {
423     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
424     for (MasterEnvironment env: coprocessors) {
425       if (env.getInstance() instanceof MasterObserver) {
426         ctx = ObserverContext.createAndPrepare(env, ctx);
427         try {
428           ((MasterObserver)env.getInstance()).postTruncateTable(ctx, tableName);
429         } catch (Throwable e) {
430           handleCoprocessorThrowable(env, e);
431         }
432         if (ctx.shouldComplete()) {
433           break;
434         }
435       }
436     }
437   }
438 
439   public void preTruncateTableHandler(TableName tableName) throws IOException {
440     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
441     for (MasterEnvironment env : coprocessors) {
442       if (env.getInstance() instanceof MasterObserver) {
443         ctx = ObserverContext.createAndPrepare(env, ctx);
444         try {
445           ((MasterObserver) env.getInstance()).preTruncateTableHandler(ctx, tableName);
446         } catch (Throwable e) {
447           handleCoprocessorThrowable(env, e);
448         }
449         if (ctx.shouldComplete()) {
450           break;
451         }
452       }
453     }
454   }
455 
456   public void postTruncateTableHandler(TableName tableName) throws IOException {
457     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
458     for (MasterEnvironment env : coprocessors) {
459       if (env.getInstance() instanceof MasterObserver) {
460         ctx = ObserverContext.createAndPrepare(env, ctx);
461         try {
462           ((MasterObserver) env.getInstance()).postTruncateTableHandler(ctx, tableName);
463         } catch (Throwable e) {
464           handleCoprocessorThrowable(env, e);
465         }
466         if (ctx.shouldComplete()) {
467           break;
468         }
469       }
470     }
471   }
472 
473   public void preModifyTable(final TableName tableName, final HTableDescriptor htd)
474       throws IOException {
475     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
476     for (MasterEnvironment env: coprocessors) {
477       if (env.getInstance() instanceof MasterObserver) {
478         ctx = ObserverContext.createAndPrepare(env, ctx);
479         Thread currentThread = Thread.currentThread();
480         ClassLoader cl = currentThread.getContextClassLoader();
481         try {
482           currentThread.setContextClassLoader(env.getClassLoader());
483           ((MasterObserver)env.getInstance()).preModifyTable(ctx, tableName, htd);
484         } catch (Throwable e) {
485           handleCoprocessorThrowable(env, e);
486         } finally {
487           currentThread.setContextClassLoader(cl);
488         }
489         if (ctx.shouldComplete()) {
490           break;
491         }
492       }
493     }
494   }
495 
496   public void postModifyTable(final TableName tableName, final HTableDescriptor htd)
497       throws IOException {
498     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
499     for (MasterEnvironment env: coprocessors) {
500       if (env.getInstance() instanceof MasterObserver) {
501         ctx = ObserverContext.createAndPrepare(env, ctx);
502         Thread currentThread = Thread.currentThread();
503         ClassLoader cl = currentThread.getContextClassLoader();
504         try {
505           currentThread.setContextClassLoader(env.getClassLoader());
506           ((MasterObserver)env.getInstance()).postModifyTable(ctx, tableName, htd);
507         } catch (Throwable e) {
508           handleCoprocessorThrowable(env, e);
509         } finally {
510           currentThread.setContextClassLoader(cl);
511         }
512         if (ctx.shouldComplete()) {
513           break;
514         }
515       }
516     }
517   }
518 
519   public void preModifyTableHandler(final TableName tableName, final HTableDescriptor htd)
520       throws IOException {
521     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
522     for (MasterEnvironment env : coprocessors) {
523       if (env.getInstance() instanceof MasterObserver) {
524         ctx = ObserverContext.createAndPrepare(env, ctx);
525         Thread currentThread = Thread.currentThread();
526         ClassLoader cl = currentThread.getContextClassLoader();
527         try {
528           currentThread.setContextClassLoader(env.getClassLoader());
529           ((MasterObserver) env.getInstance()).preModifyTableHandler(ctx, tableName, htd);
530         } catch (Throwable e) {
531           handleCoprocessorThrowable(env, e);
532         } finally {
533           currentThread.setContextClassLoader(cl);
534         }
535         if (ctx.shouldComplete()) {
536           break;
537         }
538       }
539     }
540   }
541 
542   public void postModifyTableHandler(final TableName tableName, final HTableDescriptor htd)
543       throws IOException {
544     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
545     for (MasterEnvironment env : coprocessors) {
546       if (env.getInstance() instanceof MasterObserver) {
547         ctx = ObserverContext.createAndPrepare(env, ctx);
548         Thread currentThread = Thread.currentThread();
549         ClassLoader cl = currentThread.getContextClassLoader();
550         try {
551           currentThread.setContextClassLoader(env.getClassLoader());
552           ((MasterObserver) env.getInstance()).postModifyTableHandler(ctx, tableName, htd);
553         } catch (Throwable e) {
554           handleCoprocessorThrowable(env, e);
555         } finally {
556           currentThread.setContextClassLoader(cl);
557         }
558         if (ctx.shouldComplete()) {
559           break;
560         }
561       }
562     }
563   }
564 
565   public boolean preAddColumn(final TableName tableName, final HColumnDescriptor column)
566       throws IOException {
567     boolean bypass = false;
568     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
569     for (MasterEnvironment env: coprocessors) {
570       if (env.getInstance() instanceof MasterObserver) {
571         ctx = ObserverContext.createAndPrepare(env, ctx);
572         Thread currentThread = Thread.currentThread();
573         ClassLoader cl = currentThread.getContextClassLoader();
574         try {
575           currentThread.setContextClassLoader(env.getClassLoader());
576           ((MasterObserver)env.getInstance()).preAddColumn(ctx, tableName, column);
577         } catch (Throwable e) {
578           handleCoprocessorThrowable(env, e);
579         } finally {
580           currentThread.setContextClassLoader(cl);
581         }
582         bypass |= ctx.shouldBypass();
583         if (ctx.shouldComplete()) {
584           break;
585         }
586       }
587     }
588     return bypass;
589   }
590 
591   public void postAddColumn(final TableName tableName, final HColumnDescriptor column)
592       throws IOException {
593     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
594     for (MasterEnvironment env: coprocessors) {
595       if (env.getInstance() instanceof MasterObserver) {
596         ctx = ObserverContext.createAndPrepare(env, ctx);
597         Thread currentThread = Thread.currentThread();
598         ClassLoader cl = currentThread.getContextClassLoader();
599         try {
600           currentThread.setContextClassLoader(env.getClassLoader());
601           ((MasterObserver)env.getInstance()).postAddColumn(ctx, tableName, column);
602         } catch (Throwable e) {
603           handleCoprocessorThrowable(env, e);
604         } finally {
605           currentThread.setContextClassLoader(cl);
606         }
607         if (ctx.shouldComplete()) {
608           break;
609         }
610       }
611     }
612   }
613 
614   public boolean preAddColumnHandler(final TableName tableName, final HColumnDescriptor column)
615       throws IOException {
616     boolean bypass = false;
617     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
618     for (MasterEnvironment env : coprocessors) {
619       if (env.getInstance() instanceof MasterObserver) {
620         ctx = ObserverContext.createAndPrepare(env, ctx);
621         Thread currentThread = Thread.currentThread();
622         ClassLoader cl = currentThread.getContextClassLoader();
623         try {
624           currentThread.setContextClassLoader(env.getClassLoader());
625           ((MasterObserver) env.getInstance()).preAddColumnHandler(ctx, tableName, column);
626         } catch (Throwable e) {
627           handleCoprocessorThrowable(env, e);
628         } finally {
629           currentThread.setContextClassLoader(cl);
630         }
631         bypass |= ctx.shouldBypass();
632         if (ctx.shouldComplete()) {
633           break;
634         }
635       }
636     }
637     return bypass;
638   }
639 
640   public void postAddColumnHandler(final TableName tableName, final HColumnDescriptor column)
641       throws IOException {
642     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
643     for (MasterEnvironment env : coprocessors) {
644       if (env.getInstance() instanceof MasterObserver) {
645         ctx = ObserverContext.createAndPrepare(env, ctx);
646         Thread currentThread = Thread.currentThread();
647         ClassLoader cl = currentThread.getContextClassLoader();
648         try {
649           currentThread.setContextClassLoader(env.getClassLoader());
650           ((MasterObserver) env.getInstance()).postAddColumnHandler(ctx, tableName, column);
651         } catch (Throwable e) {
652           handleCoprocessorThrowable(env, e);
653         } finally {
654           currentThread.setContextClassLoader(cl);
655         }
656         if (ctx.shouldComplete()) {
657           break;
658         }
659       }
660     }
661   }
662 
663   public boolean preModifyColumn(final TableName tableName, final HColumnDescriptor descriptor)
664       throws IOException {
665     boolean bypass = false;
666     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
667     for (MasterEnvironment env: coprocessors) {
668       if (env.getInstance() instanceof MasterObserver) {
669         ctx = ObserverContext.createAndPrepare(env, ctx);
670         Thread currentThread = Thread.currentThread();
671         ClassLoader cl = currentThread.getContextClassLoader();
672         try {
673           currentThread.setContextClassLoader(env.getClassLoader());
674           ((MasterObserver)env.getInstance()).preModifyColumn(ctx, tableName, descriptor);
675         } catch (Throwable e) {
676           handleCoprocessorThrowable(env, e);
677         } finally {
678           currentThread.setContextClassLoader(cl);
679         }
680         bypass |= ctx.shouldBypass();
681         if (ctx.shouldComplete()) {
682           break;
683         }
684       }
685     }
686     return bypass;
687   }
688 
689   public void postModifyColumn(final TableName tableName, final HColumnDescriptor descriptor)
690       throws IOException {
691     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
692     for (MasterEnvironment env: coprocessors) {
693       if (env.getInstance() instanceof MasterObserver) {
694         ctx = ObserverContext.createAndPrepare(env, ctx);
695         Thread currentThread = Thread.currentThread();
696         ClassLoader cl = currentThread.getContextClassLoader();
697         try {
698           currentThread.setContextClassLoader(env.getClassLoader());
699           ((MasterObserver)env.getInstance()).postModifyColumn(ctx, tableName, descriptor);
700         } catch (Throwable e) {
701           handleCoprocessorThrowable(env, e);
702         } finally {
703           currentThread.setContextClassLoader(cl);
704         }
705         if (ctx.shouldComplete()) {
706           break;
707         }
708       }
709     }
710   }
711 
712   public boolean preModifyColumnHandler(final TableName tableName,
713       final HColumnDescriptor descriptor) throws IOException {
714     boolean bypass = false;
715     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
716     for (MasterEnvironment env : coprocessors) {
717       if (env.getInstance() instanceof MasterObserver) {
718         ctx = ObserverContext.createAndPrepare(env, ctx);
719         Thread currentThread = Thread.currentThread();
720         ClassLoader cl = currentThread.getContextClassLoader();
721         try {
722           currentThread.setContextClassLoader(env.getClassLoader());
723           ((MasterObserver) env.getInstance()).preModifyColumnHandler(ctx, tableName, descriptor);
724         } catch (Throwable e) {
725           handleCoprocessorThrowable(env, e);
726         } finally {
727           currentThread.setContextClassLoader(cl);
728         }
729         bypass |= ctx.shouldBypass();
730         if (ctx.shouldComplete()) {
731           break;
732         }
733       }
734     }
735     return bypass;
736   }
737 
738   public void postModifyColumnHandler(final TableName tableName,
739       final HColumnDescriptor descriptor) throws IOException {
740     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
741     for (MasterEnvironment env : coprocessors) {
742       if (env.getInstance() instanceof MasterObserver) {
743         ctx = ObserverContext.createAndPrepare(env, ctx);
744         Thread currentThread = Thread.currentThread();
745         ClassLoader cl = currentThread.getContextClassLoader();
746         try {
747           currentThread.setContextClassLoader(env.getClassLoader());
748           ((MasterObserver) env.getInstance()).postModifyColumnHandler(ctx, tableName, descriptor);
749         } catch (Throwable e) {
750           handleCoprocessorThrowable(env, e);
751         } finally {
752           currentThread.setContextClassLoader(cl);
753         }
754         if (ctx.shouldComplete()) {
755           break;
756         }
757       }
758     }
759   }
760 
761   public boolean preDeleteColumn(final TableName tableName, final byte [] c) throws IOException {
762     boolean bypass = false;
763     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
764     for (MasterEnvironment env: coprocessors) {
765       if (env.getInstance() instanceof MasterObserver) {
766         ctx = ObserverContext.createAndPrepare(env, ctx);
767         Thread currentThread = Thread.currentThread();
768         ClassLoader cl = currentThread.getContextClassLoader();
769         try {
770           currentThread.setContextClassLoader(env.getClassLoader());
771           ((MasterObserver)env.getInstance()).preDeleteColumn(ctx, tableName, c);
772         } catch (Throwable e) {
773           handleCoprocessorThrowable(env, e);
774         } finally {
775           currentThread.setContextClassLoader(cl);
776         }
777         bypass |= ctx.shouldBypass();
778         if (ctx.shouldComplete()) {
779           break;
780         }
781       }
782     }
783     return bypass;
784   }
785 
786   public void postDeleteColumn(final TableName tableName, final byte [] c) throws IOException {
787     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
788     for (MasterEnvironment env: coprocessors) {
789       if (env.getInstance() instanceof MasterObserver) {
790         ctx = ObserverContext.createAndPrepare(env, ctx);
791         Thread currentThread = Thread.currentThread();
792         ClassLoader cl = currentThread.getContextClassLoader();
793         try {
794           currentThread.setContextClassLoader(env.getClassLoader());
795           ((MasterObserver)env.getInstance()).postDeleteColumn(ctx, tableName, c);
796         } catch (Throwable e) {
797           handleCoprocessorThrowable(env, e);
798         } finally {
799           currentThread.setContextClassLoader(cl);
800         }
801         if (ctx.shouldComplete()) {
802           break;
803         }
804       }
805     }
806   }
807 
808   public boolean preDeleteColumnHandler(final TableName tableName, final byte[] c)
809       throws IOException {
810     boolean bypass = false;
811     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
812     for (MasterEnvironment env : coprocessors) {
813       if (env.getInstance() instanceof MasterObserver) {
814         ctx = ObserverContext.createAndPrepare(env, ctx);
815         Thread currentThread = Thread.currentThread();
816         ClassLoader cl = currentThread.getContextClassLoader();
817         try {
818           currentThread.setContextClassLoader(env.getClassLoader());
819           ((MasterObserver) env.getInstance()).preDeleteColumnHandler(ctx, tableName, c);
820         } catch (Throwable e) {
821           handleCoprocessorThrowable(env, e);
822         } finally {
823           currentThread.setContextClassLoader(cl);
824         }
825         bypass |= ctx.shouldBypass();
826         if (ctx.shouldComplete()) {
827           break;
828         }
829       }
830     }
831     return bypass;
832   }
833 
834   public void postDeleteColumnHandler(final TableName tableName, final byte[] c)
835       throws IOException {
836     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
837     for (MasterEnvironment env : coprocessors) {
838       if (env.getInstance() instanceof MasterObserver) {
839         ctx = ObserverContext.createAndPrepare(env, ctx);
840         Thread currentThread = Thread.currentThread();
841         ClassLoader cl = currentThread.getContextClassLoader();
842         try {
843           currentThread.setContextClassLoader(env.getClassLoader());
844           ((MasterObserver) env.getInstance()).postDeleteColumnHandler(ctx, tableName, c);
845         } catch (Throwable e) {
846           handleCoprocessorThrowable(env, e);
847         } finally {
848           currentThread.setContextClassLoader(cl);
849         }
850         if (ctx.shouldComplete()) {
851           break;
852         }
853       }
854     }
855   }
856 
857   public void preEnableTable(final TableName tableName) throws IOException {
858     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
859     for (MasterEnvironment env: coprocessors) {
860       if (env.getInstance() instanceof MasterObserver) {
861         ctx = ObserverContext.createAndPrepare(env, ctx);
862         Thread currentThread = Thread.currentThread();
863         ClassLoader cl = currentThread.getContextClassLoader();
864         try {
865           currentThread.setContextClassLoader(env.getClassLoader());
866           ((MasterObserver)env.getInstance()).preEnableTable(ctx, tableName);
867         } catch (Throwable e) {
868           handleCoprocessorThrowable(env, e);
869         } finally {
870           currentThread.setContextClassLoader(cl);
871         }
872         if (ctx.shouldComplete()) {
873           break;
874         }
875       }
876     }
877   }
878 
879   public void postEnableTable(final TableName tableName) throws IOException {
880     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
881     for (MasterEnvironment env: coprocessors) {
882       if (env.getInstance() instanceof MasterObserver) {
883         ctx = ObserverContext.createAndPrepare(env, ctx);
884         Thread currentThread = Thread.currentThread();
885         ClassLoader cl = currentThread.getContextClassLoader();
886         try {
887           currentThread.setContextClassLoader(env.getClassLoader());
888           ((MasterObserver)env.getInstance()).postEnableTable(ctx, tableName);
889         } catch (Throwable e) {
890           handleCoprocessorThrowable(env, e);
891         } finally {
892           currentThread.setContextClassLoader(cl);
893         }
894         if (ctx.shouldComplete()) {
895           break;
896         }
897       }
898     }
899   }
900 
901   public void preEnableTableHandler(final TableName tableName) throws IOException {
902     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
903     for (MasterEnvironment env : coprocessors) {
904       if (env.getInstance() instanceof MasterObserver) {
905         ctx = ObserverContext.createAndPrepare(env, ctx);
906         Thread currentThread = Thread.currentThread();
907         ClassLoader cl = currentThread.getContextClassLoader();
908         try {
909           currentThread.setContextClassLoader(env.getClassLoader());
910           ((MasterObserver) env.getInstance()).preEnableTableHandler(ctx, tableName);
911         } catch (Throwable e) {
912           handleCoprocessorThrowable(env, e);
913         } finally {
914           currentThread.setContextClassLoader(cl);
915         }
916         if (ctx.shouldComplete()) {
917           break;
918         }
919       }
920     }
921   }
922 
923   public void postEnableTableHandler(final TableName tableName) throws IOException {
924     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
925     for (MasterEnvironment env : coprocessors) {
926       if (env.getInstance() instanceof MasterObserver) {
927         ctx = ObserverContext.createAndPrepare(env, ctx);
928         Thread currentThread = Thread.currentThread();
929         ClassLoader cl = currentThread.getContextClassLoader();
930         try {
931           currentThread.setContextClassLoader(env.getClassLoader());
932           ((MasterObserver) env.getInstance()).postEnableTableHandler(ctx, tableName);
933         } catch (Throwable e) {
934           handleCoprocessorThrowable(env, e);
935         } finally {
936           currentThread.setContextClassLoader(cl);
937         }
938         if (ctx.shouldComplete()) {
939           break;
940         }
941       }
942     }
943   }
944 
945   public void preDisableTable(final TableName tableName) throws IOException {
946     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
947     for (MasterEnvironment env: coprocessors) {
948       if (env.getInstance() instanceof MasterObserver) {
949         ctx = ObserverContext.createAndPrepare(env, ctx);
950         Thread currentThread = Thread.currentThread();
951         ClassLoader cl = currentThread.getContextClassLoader();
952         try {
953           currentThread.setContextClassLoader(env.getClassLoader());
954           ((MasterObserver)env.getInstance()).preDisableTable(ctx, tableName);
955         } catch (Throwable e) {
956           handleCoprocessorThrowable(env, e);
957         } finally {
958           currentThread.setContextClassLoader(cl);
959         }
960         if (ctx.shouldComplete()) {
961           break;
962         }
963       }
964     }
965   }
966 
967   public void postDisableTable(final TableName tableName) throws IOException {
968     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
969     for (MasterEnvironment env: coprocessors) {
970       if (env.getInstance() instanceof MasterObserver) {
971         ctx = ObserverContext.createAndPrepare(env, ctx);
972         Thread currentThread = Thread.currentThread();
973         ClassLoader cl = currentThread.getContextClassLoader();
974         try {
975           currentThread.setContextClassLoader(env.getClassLoader());
976           ((MasterObserver)env.getInstance()).postDisableTable(ctx, tableName);
977         } catch (Throwable e) {
978           handleCoprocessorThrowable(env, e);
979         } finally {
980           currentThread.setContextClassLoader(cl);
981         }
982         if (ctx.shouldComplete()) {
983           break;
984         }
985       }
986     }
987   }
988 
989   public void preDisableTableHandler(final TableName tableName) throws IOException {
990     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
991     for (MasterEnvironment env : coprocessors) {
992       if (env.getInstance() instanceof MasterObserver) {
993         ctx = ObserverContext.createAndPrepare(env, ctx);
994         Thread currentThread = Thread.currentThread();
995         ClassLoader cl = currentThread.getContextClassLoader();
996         try {
997           currentThread.setContextClassLoader(env.getClassLoader());
998           ((MasterObserver) env.getInstance()).preDisableTableHandler(ctx, tableName);
999         } catch (Throwable e) {
1000           handleCoprocessorThrowable(env, e);
1001         } finally {
1002           currentThread.setContextClassLoader(cl);
1003         }
1004         if (ctx.shouldComplete()) {
1005           break;
1006         }
1007       }
1008     }
1009   }
1010 
1011   public void postDisableTableHandler(final TableName tableName) throws IOException {
1012     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1013     for (MasterEnvironment env : coprocessors) {
1014       if (env.getInstance() instanceof MasterObserver) {
1015         ctx = ObserverContext.createAndPrepare(env, ctx);
1016         Thread currentThread = Thread.currentThread();
1017         ClassLoader cl = currentThread.getContextClassLoader();
1018         try {
1019           currentThread.setContextClassLoader(env.getClassLoader());
1020           ((MasterObserver) env.getInstance()).postDisableTableHandler(ctx, tableName);
1021         } catch (Throwable e) {
1022           handleCoprocessorThrowable(env, e);
1023         } finally {
1024           currentThread.setContextClassLoader(cl);
1025         }
1026         if (ctx.shouldComplete()) {
1027           break;
1028         }
1029       }
1030     }
1031   }
1032 
1033   public boolean preMove(final HRegionInfo region, final ServerName srcServer,
1034       final ServerName destServer) throws IOException {
1035     boolean bypass = false;
1036     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1037     for (MasterEnvironment env: coprocessors) {
1038       if (env.getInstance() instanceof MasterObserver) {
1039         ctx = ObserverContext.createAndPrepare(env, ctx);
1040         Thread currentThread = Thread.currentThread();
1041         ClassLoader cl = currentThread.getContextClassLoader();
1042         try {
1043           currentThread.setContextClassLoader(env.getClassLoader());
1044           ((MasterObserver)env.getInstance()).preMove(ctx, region, srcServer, destServer);
1045         } catch (Throwable e) {
1046           handleCoprocessorThrowable(env, e);
1047         } finally {
1048           currentThread.setContextClassLoader(cl);
1049         }
1050         bypass |= ctx.shouldBypass();
1051         if (ctx.shouldComplete()) {
1052           break;
1053         }
1054       }
1055     }
1056     return bypass;
1057   }
1058 
1059   public void postMove(final HRegionInfo region, final ServerName srcServer,
1060       final ServerName destServer) throws IOException {
1061     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1062     for (MasterEnvironment env: coprocessors) {
1063       if (env.getInstance() instanceof MasterObserver) {
1064         ctx = ObserverContext.createAndPrepare(env, ctx);
1065         Thread currentThread = Thread.currentThread();
1066         ClassLoader cl = currentThread.getContextClassLoader();
1067         try {
1068           currentThread.setContextClassLoader(env.getClassLoader());
1069           ((MasterObserver)env.getInstance()).postMove(ctx, region, srcServer, destServer);
1070         } catch (Throwable e) {
1071           handleCoprocessorThrowable(env, e);
1072         } finally {
1073           currentThread.setContextClassLoader(cl);
1074         }
1075         if (ctx.shouldComplete()) {
1076           break;
1077         }
1078       }
1079     }
1080   }
1081 
1082   public boolean preAssign(final HRegionInfo regionInfo) throws IOException {
1083     boolean bypass = false;
1084     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1085     for (MasterEnvironment env: coprocessors) {
1086       if (env.getInstance() instanceof MasterObserver) {
1087         ctx = ObserverContext.createAndPrepare(env, ctx);
1088         Thread currentThread = Thread.currentThread();
1089         ClassLoader cl = currentThread.getContextClassLoader();
1090         try {
1091           currentThread.setContextClassLoader(env.getClassLoader());
1092           ((MasterObserver) env.getInstance()).preAssign(ctx, regionInfo);
1093         } catch (Throwable e) {
1094           handleCoprocessorThrowable(env, e);
1095         } finally {
1096           currentThread.setContextClassLoader(cl);
1097         }
1098         bypass |= ctx.shouldBypass();
1099         if (ctx.shouldComplete()) {
1100           break;
1101         }
1102       }
1103     }
1104     return bypass;
1105   }
1106 
1107   public void postAssign(final HRegionInfo regionInfo) throws IOException {
1108     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1109     for (MasterEnvironment env: coprocessors) {
1110       if (env.getInstance() instanceof MasterObserver) {
1111         ctx = ObserverContext.createAndPrepare(env, ctx);
1112         Thread currentThread = Thread.currentThread();
1113         ClassLoader cl = currentThread.getContextClassLoader();
1114         try {
1115           currentThread.setContextClassLoader(env.getClassLoader());
1116           ((MasterObserver)env.getInstance()).postAssign(ctx, regionInfo);
1117         } catch (Throwable e) {
1118           handleCoprocessorThrowable(env, e);
1119         } finally {
1120           currentThread.setContextClassLoader(cl);
1121         }
1122         if (ctx.shouldComplete()) {
1123           break;
1124         }
1125       }
1126     }
1127   }
1128 
1129   public boolean preUnassign(final HRegionInfo regionInfo, final boolean force)
1130       throws IOException {
1131     boolean bypass = false;
1132     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1133     for (MasterEnvironment env: coprocessors) {
1134       if (env.getInstance() instanceof MasterObserver) {
1135         ctx = ObserverContext.createAndPrepare(env, ctx);
1136         Thread currentThread = Thread.currentThread();
1137         ClassLoader cl = currentThread.getContextClassLoader();
1138         try {
1139           currentThread.setContextClassLoader(env.getClassLoader());
1140           ((MasterObserver)env.getInstance()).preUnassign(ctx, regionInfo, force);
1141         } catch (Throwable e) {
1142           handleCoprocessorThrowable(env, e);
1143         } finally {
1144           currentThread.setContextClassLoader(cl);
1145         }
1146         bypass |= ctx.shouldBypass();
1147         if (ctx.shouldComplete()) {
1148           break;
1149         }
1150       }
1151     }
1152     return bypass;
1153   }
1154 
1155   public void postUnassign(final HRegionInfo regionInfo, final boolean force) throws IOException {
1156     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1157     for (MasterEnvironment env: coprocessors) {
1158       if (env.getInstance() instanceof MasterObserver) {
1159         ctx = ObserverContext.createAndPrepare(env, ctx);
1160         Thread currentThread = Thread.currentThread();
1161         ClassLoader cl = currentThread.getContextClassLoader();
1162         try {
1163           currentThread.setContextClassLoader(env.getClassLoader());
1164           ((MasterObserver)env.getInstance()).postUnassign(ctx, regionInfo, force);
1165         } catch (Throwable e) {
1166           handleCoprocessorThrowable(env, e);
1167         } finally {
1168           currentThread.setContextClassLoader(cl);
1169         }
1170         if (ctx.shouldComplete()) {
1171           break;
1172         }
1173       }
1174     }
1175   }
1176 
1177   public void preRegionOffline(final HRegionInfo regionInfo) throws IOException {
1178     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1179     for (MasterEnvironment env : coprocessors) {
1180       if (env.getInstance() instanceof MasterObserver) {
1181         ctx = ObserverContext.createAndPrepare(env, ctx);
1182         Thread currentThread = Thread.currentThread();
1183         ClassLoader cl = currentThread.getContextClassLoader();
1184         try {
1185           currentThread.setContextClassLoader(env.getClassLoader());
1186           ((MasterObserver) env.getInstance()).preRegionOffline(ctx, regionInfo);
1187         } catch (Throwable e) {
1188           handleCoprocessorThrowable(env, e);
1189         } finally {
1190           currentThread.setContextClassLoader(cl);
1191         }
1192         if (ctx.shouldComplete()) {
1193           break;
1194         }
1195       }
1196     }
1197   }
1198 
1199   public void postRegionOffline(final HRegionInfo regionInfo) throws IOException {
1200     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1201     for (MasterEnvironment env : coprocessors) {
1202       if (env.getInstance() instanceof MasterObserver) {
1203         ctx = ObserverContext.createAndPrepare(env, ctx);
1204         Thread currentThread = Thread.currentThread();
1205         ClassLoader cl = currentThread.getContextClassLoader();
1206         try {
1207           currentThread.setContextClassLoader(env.getClassLoader());
1208           ((MasterObserver) env.getInstance()).postRegionOffline(ctx, regionInfo);
1209         } catch (Throwable e) {
1210           handleCoprocessorThrowable(env, e);
1211         } finally {
1212           currentThread.setContextClassLoader(cl);
1213         }
1214         if (ctx.shouldComplete()) {
1215           break;
1216         }
1217       }
1218     }
1219   }
1220 
1221   public boolean preBalance() throws IOException {
1222     boolean bypass = false;
1223     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1224     for (MasterEnvironment env: coprocessors) {
1225       if (env.getInstance() instanceof MasterObserver) {
1226         ctx = ObserverContext.createAndPrepare(env, ctx);
1227         Thread currentThread = Thread.currentThread();
1228         ClassLoader cl = currentThread.getContextClassLoader();
1229         try {
1230           currentThread.setContextClassLoader(env.getClassLoader());
1231           ((MasterObserver)env.getInstance()).preBalance(ctx);
1232         } catch (Throwable e) {
1233           handleCoprocessorThrowable(env, e);
1234         } finally {
1235           currentThread.setContextClassLoader(cl);
1236         }
1237         bypass |= ctx.shouldBypass();
1238         if (ctx.shouldComplete()) {
1239           break;
1240         }
1241       }
1242     }
1243     return bypass;
1244   }
1245 
1246   public void postBalance(final List<RegionPlan> plans) throws IOException {
1247     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1248     for (MasterEnvironment env: coprocessors) {
1249       if (env.getInstance() instanceof MasterObserver) {
1250         ctx = ObserverContext.createAndPrepare(env, ctx);
1251         Thread currentThread = Thread.currentThread();
1252         ClassLoader cl = currentThread.getContextClassLoader();
1253         try {
1254           currentThread.setContextClassLoader(env.getClassLoader());
1255           ((MasterObserver)env.getInstance()).postBalance(ctx, plans);
1256         } catch (Throwable e) {
1257           handleCoprocessorThrowable(env, e);
1258         } finally {
1259           currentThread.setContextClassLoader(cl);
1260         }
1261         if (ctx.shouldComplete()) {
1262           break;
1263         }
1264       }
1265     }
1266   }
1267 
1268   public boolean preBalanceSwitch(final boolean b) throws IOException {
1269     boolean balance = b;
1270     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1271     for (MasterEnvironment env: coprocessors) {
1272       if (env.getInstance() instanceof MasterObserver) {
1273         ctx = ObserverContext.createAndPrepare(env, ctx);
1274         Thread currentThread = Thread.currentThread();
1275         ClassLoader cl = currentThread.getContextClassLoader();
1276         try {
1277           currentThread.setContextClassLoader(env.getClassLoader());
1278           balance = ((MasterObserver)env.getInstance()).preBalanceSwitch(ctx, balance);
1279         } catch (Throwable e) {
1280           handleCoprocessorThrowable(env, e);
1281         } finally {
1282           currentThread.setContextClassLoader(cl);
1283         }
1284         if (ctx.shouldComplete()) {
1285           break;
1286         }
1287       }
1288     }
1289     return balance;
1290   }
1291 
1292   void postBalanceSwitch(final boolean oldValue, final boolean newValue)
1293       throws IOException {
1294     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1295     for (MasterEnvironment env: coprocessors) {
1296       if (env.getInstance() instanceof MasterObserver) {
1297         ctx = ObserverContext.createAndPrepare(env, ctx);
1298         Thread currentThread = Thread.currentThread();
1299         ClassLoader cl = currentThread.getContextClassLoader();
1300         try {
1301           currentThread.setContextClassLoader(env.getClassLoader());
1302           ((MasterObserver)env.getInstance()).postBalanceSwitch(ctx, oldValue, newValue);
1303         } catch (Throwable e) {
1304           handleCoprocessorThrowable(env, e);
1305         } finally {
1306           currentThread.setContextClassLoader(cl);
1307         }
1308         if (ctx.shouldComplete()) {
1309           break;
1310         }
1311       }
1312     }
1313   }
1314 
1315   public void preShutdown() throws IOException {
1316     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1317     for (MasterEnvironment env: coprocessors) {
1318       if (env.getInstance() instanceof MasterObserver) {
1319         ctx = ObserverContext.createAndPrepare(env, ctx);
1320         Thread currentThread = Thread.currentThread();
1321         ClassLoader cl = currentThread.getContextClassLoader();
1322         try {
1323           currentThread.setContextClassLoader(env.getClassLoader());
1324           ((MasterObserver)env.getInstance()).preShutdown(ctx);
1325         } catch (Throwable e) {
1326           handleCoprocessorThrowable(env, e);
1327         } finally {
1328           currentThread.setContextClassLoader(cl);
1329         }
1330         if (ctx.shouldComplete()) {
1331           break;
1332         }
1333       }
1334       // invoke coprocessor stop method
1335       shutdown(env);
1336     }
1337   }
1338 
1339   public void preStopMaster() throws IOException {
1340     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1341     for (MasterEnvironment env: coprocessors) {
1342       if (env.getInstance() instanceof MasterObserver) {
1343         ctx = ObserverContext.createAndPrepare(env, ctx);
1344         Thread currentThread = Thread.currentThread();
1345         ClassLoader cl = currentThread.getContextClassLoader();
1346         try {
1347           currentThread.setContextClassLoader(env.getClassLoader());
1348           ((MasterObserver)env.getInstance()).preStopMaster(ctx);
1349         } catch (Throwable e) {
1350           handleCoprocessorThrowable(env, e);
1351         } finally {
1352           currentThread.setContextClassLoader(cl);
1353         }
1354         if (ctx.shouldComplete()) {
1355           break;
1356         }
1357       }
1358       // invoke coprocessor stop method
1359       shutdown(env);
1360     }
1361   }
1362 
1363   public void preMasterInitialization() throws IOException {
1364     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1365     for (MasterEnvironment env : coprocessors) {
1366       if (env.getInstance() instanceof MasterObserver) {
1367         ctx = ObserverContext.createAndPrepare(env, ctx);
1368         Thread currentThread = Thread.currentThread();
1369         ClassLoader cl = currentThread.getContextClassLoader();
1370         try {
1371           currentThread.setContextClassLoader(env.getClassLoader());
1372           ((MasterObserver) env.getInstance()).preMasterInitialization(ctx);
1373         } catch (Throwable e) {
1374           handleCoprocessorThrowable(env, e);
1375         } finally {
1376           currentThread.setContextClassLoader(cl);
1377         }
1378         if (ctx.shouldComplete()) {
1379           break;
1380         }
1381       }
1382     }
1383   }
1384 
1385   public void postStartMaster() throws IOException {
1386     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1387     for (MasterEnvironment env: coprocessors) {
1388       if (env.getInstance() instanceof MasterObserver) {
1389         ctx = ObserverContext.createAndPrepare(env, ctx);
1390         Thread currentThread = Thread.currentThread();
1391         ClassLoader cl = currentThread.getContextClassLoader();
1392         try {
1393           currentThread.setContextClassLoader(env.getClassLoader());
1394           ((MasterObserver)env.getInstance()).postStartMaster(ctx);
1395         } catch (Throwable e) {
1396           handleCoprocessorThrowable(env, e);
1397         } finally {
1398           currentThread.setContextClassLoader(cl);
1399         }
1400         if (ctx.shouldComplete()) {
1401           break;
1402         }
1403       }
1404     }
1405   }
1406 
1407   public void preSnapshot(final SnapshotDescription snapshot,
1408       final HTableDescriptor hTableDescriptor) throws IOException {
1409     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1410     for (MasterEnvironment env: coprocessors) {
1411       if (env.getInstance() instanceof MasterObserver) {
1412         ctx = ObserverContext.createAndPrepare(env, ctx);
1413         Thread currentThread = Thread.currentThread();
1414         ClassLoader cl = currentThread.getContextClassLoader();
1415         try {
1416           currentThread.setContextClassLoader(env.getClassLoader());
1417           ((MasterObserver)env.getInstance()).preSnapshot(ctx, snapshot, hTableDescriptor);
1418         } catch (Throwable e) {
1419           handleCoprocessorThrowable(env, e);
1420         } finally {
1421           currentThread.setContextClassLoader(cl);
1422         }
1423         if (ctx.shouldComplete()) {
1424           break;
1425         }
1426       }
1427     }
1428   }
1429 
1430   public void postSnapshot(final SnapshotDescription snapshot,
1431       final HTableDescriptor hTableDescriptor) throws IOException {
1432     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1433     for (MasterEnvironment env: coprocessors) {
1434       if (env.getInstance() instanceof MasterObserver) {
1435         ctx = ObserverContext.createAndPrepare(env, ctx);
1436         Thread currentThread = Thread.currentThread();
1437         ClassLoader cl = currentThread.getContextClassLoader();
1438         try {
1439           currentThread.setContextClassLoader(env.getClassLoader());
1440           ((MasterObserver)env.getInstance()).postSnapshot(ctx, snapshot, hTableDescriptor);
1441         } catch (Throwable e) {
1442           handleCoprocessorThrowable(env, e);
1443         } finally {
1444           currentThread.setContextClassLoader(cl);
1445         }
1446         if (ctx.shouldComplete()) {
1447           break;
1448         }
1449       }
1450     }
1451   }
1452 
1453   public void preCloneSnapshot(final SnapshotDescription snapshot,
1454       final HTableDescriptor hTableDescriptor) throws IOException {
1455     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1456     for (MasterEnvironment env: coprocessors) {
1457       if (env.getInstance() instanceof MasterObserver) {
1458         ctx = ObserverContext.createAndPrepare(env, ctx);
1459         Thread currentThread = Thread.currentThread();
1460         ClassLoader cl = currentThread.getContextClassLoader();
1461         try {
1462           currentThread.setContextClassLoader(env.getClassLoader());
1463           ((MasterObserver)env.getInstance()).preCloneSnapshot(ctx, snapshot,
1464             hTableDescriptor);
1465         } catch (Throwable e) {
1466           handleCoprocessorThrowable(env, e);
1467         } finally {
1468           currentThread.setContextClassLoader(cl);
1469         }
1470         if (ctx.shouldComplete()) {
1471           break;
1472         }
1473       }
1474     }
1475   }
1476 
1477   public void postCloneSnapshot(final SnapshotDescription snapshot,
1478       final HTableDescriptor hTableDescriptor) throws IOException {
1479     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1480     for (MasterEnvironment env: coprocessors) {
1481       if (env.getInstance() instanceof MasterObserver) {
1482         ctx = ObserverContext.createAndPrepare(env, ctx);
1483         Thread currentThread = Thread.currentThread();
1484         ClassLoader cl = currentThread.getContextClassLoader();
1485         try {
1486           currentThread.setContextClassLoader(env.getClassLoader());
1487           ((MasterObserver)env.getInstance()).postCloneSnapshot(ctx, snapshot,
1488             hTableDescriptor);
1489         } catch (Throwable e) {
1490           handleCoprocessorThrowable(env, e);
1491         } finally {
1492           currentThread.setContextClassLoader(cl);
1493         }
1494         if (ctx.shouldComplete()) {
1495           break;
1496         }
1497       }
1498     }
1499   }
1500 
1501   public void preRestoreSnapshot(final SnapshotDescription snapshot,
1502       final HTableDescriptor hTableDescriptor) throws IOException {
1503     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1504     for (MasterEnvironment env: coprocessors) {
1505       if (env.getInstance() instanceof MasterObserver) {
1506         ctx = ObserverContext.createAndPrepare(env, ctx);
1507         Thread currentThread = Thread.currentThread();
1508         ClassLoader cl = currentThread.getContextClassLoader();
1509         try {
1510           currentThread.setContextClassLoader(env.getClassLoader());
1511           ((MasterObserver)env.getInstance()).preRestoreSnapshot(ctx, snapshot,
1512             hTableDescriptor);
1513         } catch (Throwable e) {
1514           handleCoprocessorThrowable(env, e);
1515         } finally {
1516           currentThread.setContextClassLoader(cl);
1517         }
1518         if (ctx.shouldComplete()) {
1519           break;
1520         }
1521       }
1522     }
1523   }
1524 
1525   public void postRestoreSnapshot(final SnapshotDescription snapshot,
1526       final HTableDescriptor hTableDescriptor) throws IOException {
1527     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1528     for (MasterEnvironment env: coprocessors) {
1529       if (env.getInstance() instanceof MasterObserver) {
1530         ctx = ObserverContext.createAndPrepare(env, ctx);
1531         Thread currentThread = Thread.currentThread();
1532         ClassLoader cl = currentThread.getContextClassLoader();
1533         try {
1534           currentThread.setContextClassLoader(env.getClassLoader());
1535           ((MasterObserver)env.getInstance()).postRestoreSnapshot(ctx, snapshot,
1536             hTableDescriptor);
1537         } catch (Throwable e) {
1538           handleCoprocessorThrowable(env, e);
1539         } finally {
1540           currentThread.setContextClassLoader(cl);
1541         }
1542         if (ctx.shouldComplete()) {
1543           break;
1544         }
1545       }
1546     }
1547   }
1548 
1549   public void preDeleteSnapshot(final SnapshotDescription snapshot) throws IOException {
1550     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1551     for (MasterEnvironment env: coprocessors) {
1552       if (env.getInstance() instanceof MasterObserver) {
1553         ctx = ObserverContext.createAndPrepare(env, ctx);
1554         Thread currentThread = Thread.currentThread();
1555         ClassLoader cl = currentThread.getContextClassLoader();
1556         try {
1557           currentThread.setContextClassLoader(env.getClassLoader());
1558           ((MasterObserver)env.getInstance()).preDeleteSnapshot(ctx, snapshot);
1559         } catch (Throwable e) {
1560           handleCoprocessorThrowable(env, e);
1561         } finally {
1562           currentThread.setContextClassLoader(cl);
1563         }
1564         if (ctx.shouldComplete()) {
1565           break;
1566         }
1567       }
1568     }
1569   }
1570 
1571   public void postDeleteSnapshot(final SnapshotDescription snapshot) throws IOException {
1572     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1573     for (MasterEnvironment env: coprocessors) {
1574       if (env.getInstance() instanceof MasterObserver) {
1575         ctx = ObserverContext.createAndPrepare(env, ctx);
1576         Thread currentThread = Thread.currentThread();
1577         ClassLoader cl = currentThread.getContextClassLoader();
1578         try {
1579           currentThread.setContextClassLoader(env.getClassLoader());
1580           ((MasterObserver)env.getInstance()).postDeleteSnapshot(ctx, snapshot);
1581         } catch (Throwable e) {
1582           handleCoprocessorThrowable(env, e);
1583         } finally {
1584           currentThread.setContextClassLoader(cl);
1585         }
1586         if (ctx.shouldComplete()) {
1587           break;
1588         }
1589       }
1590     }
1591   }
1592 
1593   public boolean preGetTableDescriptors(final List<TableName> tableNamesList,
1594       final List<HTableDescriptor> descriptors) throws IOException {
1595     boolean bypass = false;
1596     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1597     for (MasterEnvironment env : coprocessors) {
1598       if (env.getInstance() instanceof MasterObserver) {
1599         ctx = ObserverContext.createAndPrepare(env, ctx);
1600         Thread currentThread = Thread.currentThread();
1601         ClassLoader cl = currentThread.getContextClassLoader();
1602         try {
1603           currentThread.setContextClassLoader(env.getClassLoader());
1604           ((MasterObserver) env.getInstance()).preGetTableDescriptors(ctx,
1605             tableNamesList, descriptors);
1606         } catch (Throwable e) {
1607           handleCoprocessorThrowable(env, e);
1608         } finally {
1609           currentThread.setContextClassLoader(cl);
1610         }
1611         bypass |= ctx.shouldBypass();
1612         if (ctx.shouldComplete()) {
1613           break;
1614         }
1615       }
1616     }
1617     return bypass;
1618   }
1619 
1620   public void postGetTableDescriptors(final List<HTableDescriptor> descriptors)
1621       throws IOException {
1622     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1623     for (MasterEnvironment env: coprocessors) {
1624       if (env.getInstance() instanceof MasterObserver) {
1625         ctx = ObserverContext.createAndPrepare(env, ctx);
1626         Thread currentThread = Thread.currentThread();
1627         ClassLoader cl = currentThread.getContextClassLoader();
1628         try {
1629           currentThread.setContextClassLoader(env.getClassLoader());
1630           ((MasterObserver)env.getInstance()).postGetTableDescriptors(ctx, descriptors);
1631         } catch (Throwable e) {
1632           handleCoprocessorThrowable(env, e);
1633         } finally {
1634           currentThread.setContextClassLoader(cl);
1635         }
1636         if (ctx.shouldComplete()) {
1637           break;
1638         }
1639       }
1640     }
1641   }
1642 
1643   public void preTableFlush(final TableName tableName) throws IOException {
1644     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1645     for (MasterEnvironment env: coprocessors) {
1646       if (env.getInstance() instanceof MasterObserver) {
1647         ctx = ObserverContext.createAndPrepare(env, ctx);
1648         try {
1649           ((MasterObserver)env.getInstance()).preTableFlush(ctx, tableName);
1650         } catch (Throwable e) {
1651           handleCoprocessorThrowable(env, e);
1652         }
1653         if (ctx.shouldComplete()) {
1654           break;
1655         }
1656       }
1657     }
1658   }
1659 
1660   public void postTableFlush(final TableName tableName) throws IOException {
1661     ObserverContext<MasterCoprocessorEnvironment> ctx = null;
1662     for (MasterEnvironment env: coprocessors) {
1663       if (env.getInstance() instanceof MasterObserver) {
1664         ctx = ObserverContext.createAndPrepare(env, ctx);
1665         try {
1666           ((MasterObserver)env.getInstance()).postTableFlush(ctx, tableName);
1667         } catch (Throwable e) {
1668           handleCoprocessorThrowable(env, e);
1669         }
1670         if (ctx.shouldComplete()) {
1671           break;
1672         }
1673       }
1674     }
1675   }
1676 
1677 }