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 java.io.IOException;
23  import java.util.List;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.Coprocessor;
29  import org.apache.hadoop.hbase.HColumnDescriptor;
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.HTableDescriptor;
32  import org.apache.hadoop.hbase.NamespaceDescriptor;
33  import org.apache.hadoop.hbase.ServerName;
34  import org.apache.hadoop.hbase.TableName;
35  import org.apache.hadoop.hbase.classification.InterfaceAudience;
36  import org.apache.hadoop.hbase.coprocessor.*;
37  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
38  import org.apache.hadoop.hbase.protobuf.generated.QuotaProtos.Quotas;
39  
40  /**
41   * Provides the coprocessor framework and environment for master oriented
42   * operations.  {@link HMaster} interacts with the loaded coprocessors
43   * through this class.
44   */
45  @InterfaceAudience.Private
46  public class MasterCoprocessorHost
47      extends CoprocessorHost<MasterCoprocessorHost.MasterEnvironment> {
48  
49    private static final Log LOG = LogFactory.getLog(MasterCoprocessorHost.class);
50  
51    /**
52     * Coprocessor environment extension providing access to master related
53     * services.
54     */
55    static class MasterEnvironment extends CoprocessorHost.Environment
56        implements MasterCoprocessorEnvironment {
57      private MasterServices masterServices;
58  
59      public MasterEnvironment(final Class<?> implClass, final Coprocessor impl,
60          final int priority, final int seq, final Configuration conf,
61          final MasterServices services) {
62        super(impl, priority, seq, conf);
63        this.masterServices = services;
64      }
65  
66      public MasterServices getMasterServices() {
67        return masterServices;
68      }
69    }
70  
71    private MasterServices masterServices;
72  
73    public MasterCoprocessorHost(final MasterServices services, final Configuration conf) {
74      super(services);
75      this.conf = conf;
76      this.masterServices = services;
77      // Log the state of coprocessor loading here; should appear only once or
78      // twice in the daemon log, depending on HBase version, because there is
79      // only one MasterCoprocessorHost instance in the master process
80      boolean coprocessorsEnabled = conf.getBoolean(COPROCESSORS_ENABLED_CONF_KEY,
81        DEFAULT_COPROCESSORS_ENABLED);
82      LOG.info("System coprocessor loading is " + (coprocessorsEnabled ? "enabled" : "disabled"));
83      loadSystemCoprocessors(conf, MASTER_COPROCESSOR_CONF_KEY);
84    }
85  
86    @Override
87    public MasterEnvironment createEnvironment(final Class<?> implClass,
88        final Coprocessor instance, final int priority, final int seq,
89        final Configuration conf) {
90      for (Class<?> c : implClass.getInterfaces()) {
91        if (CoprocessorService.class.isAssignableFrom(c)) {
92          masterServices.registerService(((CoprocessorService)instance).getService());
93        }
94      }
95      return new MasterEnvironment(implClass, instance, priority, seq, conf,
96          masterServices);
97    }
98  
99    public boolean preCreateNamespace(final NamespaceDescriptor ns) throws IOException {
100     return execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
101       @Override
102       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
103           throws IOException {
104         oserver.preCreateNamespace(ctx, ns);
105       }
106     });
107   }
108 
109   public void postCreateNamespace(final NamespaceDescriptor ns) throws IOException {
110     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
111       @Override
112       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
113           throws IOException {
114         oserver.postCreateNamespace(ctx, ns);
115       }
116     });
117   }
118 
119   public boolean preDeleteNamespace(final String namespaceName) throws IOException {
120     return execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
121       @Override
122       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
123           throws IOException {
124         oserver.preDeleteNamespace(ctx, namespaceName);
125       }
126     });
127   }
128 
129   public void postDeleteNamespace(final String namespaceName) throws IOException {
130     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
131       @Override
132       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
133           throws IOException {
134         oserver.postDeleteNamespace(ctx, namespaceName);
135       }
136     });
137   }
138 
139   public boolean preModifyNamespace(final NamespaceDescriptor ns) throws IOException {
140     return execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
141       @Override
142       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
143           throws IOException {
144         oserver.preModifyNamespace(ctx, ns);
145       }
146     });
147   }
148 
149   public void postModifyNamespace(final NamespaceDescriptor ns) throws IOException {
150     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
151       @Override
152       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
153           throws IOException {
154         oserver.postModifyNamespace(ctx, ns);
155       }
156     });
157   }
158 
159   public void preGetNamespaceDescriptor(final String namespaceName)
160       throws IOException {
161     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
162       @Override
163       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
164           throws IOException {
165         oserver.preGetNamespaceDescriptor(ctx, namespaceName);
166       }
167     });
168   }
169 
170   public void postGetNamespaceDescriptor(final NamespaceDescriptor ns)
171       throws IOException {
172     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
173       @Override
174       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
175           throws IOException {
176         oserver.postGetNamespaceDescriptor(ctx, ns);
177       }
178     });
179   }
180 
181   public boolean preListNamespaceDescriptors(final List<NamespaceDescriptor> descriptors)
182       throws IOException {
183     return execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
184       @Override
185       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
186           throws IOException {
187         oserver.preListNamespaceDescriptors(ctx, descriptors);
188       }
189     });
190   }
191 
192   public void postListNamespaceDescriptors(final List<NamespaceDescriptor> descriptors)
193       throws IOException {
194     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
195       @Override
196       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
197           throws IOException {
198         oserver.postListNamespaceDescriptors(ctx, descriptors);
199       }
200     });
201   }
202 
203   /* Implementation of hooks for invoking MasterObservers */
204 
205   public void preCreateTable(final HTableDescriptor htd, final HRegionInfo[] regions)
206       throws IOException {
207     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
208       @Override
209       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
210           throws IOException {
211         oserver.preCreateTable(ctx, htd, regions);
212       }
213     });
214   }
215 
216   public void postCreateTable(final HTableDescriptor htd, final HRegionInfo[] regions)
217       throws IOException {
218     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
219       @Override
220       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
221           throws IOException {
222         oserver.postCreateTable(ctx, htd, regions);
223       }
224     });
225   }
226 
227   public void preCreateTableHandler(final HTableDescriptor htd, final HRegionInfo[] regions)
228       throws IOException {
229     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
230       @Override
231       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
232           throws IOException {
233         oserver.preCreateTableHandler(ctx, htd, regions);
234       }
235     });
236   }
237 
238   public void postCreateTableHandler(final HTableDescriptor htd, final HRegionInfo[] regions)
239       throws IOException {
240     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
241       @Override
242       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
243           throws IOException {
244         oserver.postCreateTableHandler(ctx, htd, regions);
245       }
246     });
247   }
248 
249   public void preDeleteTable(final TableName tableName) throws IOException {
250     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
251       @Override
252       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
253           throws IOException {
254         oserver.preDeleteTable(ctx, tableName);
255       }
256     });
257   }
258 
259   public void postDeleteTable(final TableName tableName) throws IOException {
260     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
261       @Override
262       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
263           throws IOException {
264         oserver.postDeleteTable(ctx, tableName);
265       }
266     });
267   }
268 
269   public void preDeleteTableHandler(final TableName tableName) throws IOException {
270     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
271       @Override
272       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
273           throws IOException {
274         oserver.preDeleteTableHandler(ctx, tableName);
275       }
276     });
277   }
278 
279   public void postDeleteTableHandler(final TableName tableName) throws IOException {
280     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
281       @Override
282       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
283           throws IOException {
284         oserver.postDeleteTableHandler(ctx, tableName);
285       }
286     });
287   }
288 
289   public void preTruncateTable(final TableName tableName) throws IOException {
290     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
291       @Override
292       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
293           throws IOException {
294         oserver.preTruncateTable(ctx, tableName);
295       }
296     });
297   }
298 
299   public void postTruncateTable(final TableName tableName) throws IOException {
300     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
301       @Override
302       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
303           throws IOException {
304         oserver.postTruncateTable(ctx, tableName);
305       }
306     });
307   }
308 
309   public void preTruncateTableHandler(final TableName tableName) throws IOException {
310     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
311       @Override
312       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
313           throws IOException {
314         oserver.preTruncateTableHandler(ctx, tableName);
315       }
316     });
317   }
318 
319   public void postTruncateTableHandler(final TableName tableName) throws IOException {
320     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
321       @Override
322       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
323           throws IOException {
324         oserver.postTruncateTableHandler(ctx, tableName);
325       }
326     });
327   }
328 
329   public void preModifyTable(final TableName tableName, final HTableDescriptor htd)
330       throws IOException {
331     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
332       @Override
333       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
334           throws IOException {
335         oserver.preModifyTable(ctx, tableName, htd);
336       }
337     });
338   }
339 
340   public void postModifyTable(final TableName tableName, final HTableDescriptor htd)
341       throws IOException {
342     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
343       @Override
344       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
345           throws IOException {
346         oserver.postModifyTable(ctx, tableName, htd);
347       }
348     });
349   }
350 
351   public void preModifyTableHandler(final TableName tableName, final HTableDescriptor htd)
352       throws IOException {
353     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
354       @Override
355       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
356           throws IOException {
357         oserver.preModifyTableHandler(ctx, tableName, htd);
358       }
359     });
360   }
361 
362   public void postModifyTableHandler(final TableName tableName, final HTableDescriptor htd)
363       throws IOException {
364     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
365       @Override
366       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
367           throws IOException {
368         oserver.postModifyTableHandler(ctx, tableName, htd);
369       }
370     });
371   }
372 
373   public boolean preAddColumn(final TableName tableName, final HColumnDescriptor column)
374       throws IOException {
375     return execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
376       @Override
377       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
378           throws IOException {
379         oserver.preAddColumn(ctx, tableName, column);
380       }
381     });
382   }
383 
384   public void postAddColumn(final TableName tableName, final HColumnDescriptor column)
385       throws IOException {
386     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
387       @Override
388       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
389           throws IOException {
390         oserver.postAddColumn(ctx, tableName, column);
391       }
392     });
393   }
394 
395   public boolean preAddColumnHandler(final TableName tableName, final HColumnDescriptor column)
396       throws IOException {
397     return execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
398       @Override
399       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
400           throws IOException {
401         oserver.preAddColumnHandler(ctx, tableName, column);
402       }
403     });
404   }
405 
406   public void postAddColumnHandler(final TableName tableName, final HColumnDescriptor column)
407       throws IOException {
408     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
409       @Override
410       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
411           throws IOException {
412         oserver.postAddColumnHandler(ctx, tableName, column);
413       }
414     });
415   }
416 
417   public boolean preModifyColumn(final TableName tableName, final HColumnDescriptor descriptor)
418       throws IOException {
419     return execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
420       @Override
421       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
422           throws IOException {
423         oserver.preModifyColumn(ctx, tableName, descriptor);
424       }
425     });
426   }
427 
428   public void postModifyColumn(final TableName tableName, final HColumnDescriptor descriptor)
429       throws IOException {
430     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
431       @Override
432       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
433           throws IOException {
434         oserver.postModifyColumn(ctx, tableName, descriptor);
435       }
436     });
437   }
438 
439   public boolean preModifyColumnHandler(final TableName tableName,
440       final HColumnDescriptor descriptor) throws IOException {
441     return execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
442       @Override
443       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
444           throws IOException {
445         oserver.preModifyColumnHandler(ctx, tableName, descriptor);
446       }
447     });
448   }
449 
450   public void postModifyColumnHandler(final TableName tableName,
451       final HColumnDescriptor descriptor) throws IOException {
452     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
453       @Override
454       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
455           throws IOException {
456         oserver.postModifyColumnHandler(ctx, tableName, descriptor);
457       }
458     });
459   }
460 
461   public boolean preDeleteColumn(final TableName tableName, final byte [] c) throws IOException {
462     return execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
463       @Override
464       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
465           throws IOException {
466         oserver.preDeleteColumn(ctx, tableName, c);
467       }
468     });
469   }
470 
471   public void postDeleteColumn(final TableName tableName, final byte [] c) throws IOException {
472     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
473       @Override
474       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
475           throws IOException {
476         oserver.postDeleteColumn(ctx, tableName, c);
477       }
478     });
479   }
480 
481   public boolean preDeleteColumnHandler(final TableName tableName, final byte[] c)
482       throws IOException {
483     return execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
484       @Override
485       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
486           throws IOException {
487         oserver.preDeleteColumnHandler(ctx, tableName, c);
488       }
489     });
490   }
491 
492   public void postDeleteColumnHandler(final TableName tableName, final byte[] c)
493       throws IOException {
494     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
495       @Override
496       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
497           throws IOException {
498         oserver.postDeleteColumnHandler(ctx, tableName, c);
499       }
500     });
501   }
502 
503   public void preEnableTable(final TableName tableName) throws IOException {
504     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
505       @Override
506       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
507           throws IOException {
508         oserver.preEnableTable(ctx, tableName);
509       }
510     });
511   }
512 
513   public void postEnableTable(final TableName tableName) throws IOException {
514     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
515       @Override
516       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
517           throws IOException {
518         oserver.postEnableTable(ctx, tableName);
519       }
520     });
521   }
522 
523   public void preEnableTableHandler(final TableName tableName) throws IOException {
524     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
525       @Override
526       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
527           throws IOException {
528         oserver.preEnableTableHandler(ctx, tableName);
529       }
530     });
531   }
532 
533   public void postEnableTableHandler(final TableName tableName) throws IOException {
534     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
535       @Override
536       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
537           throws IOException {
538         oserver.postEnableTableHandler(ctx, tableName);
539       }
540     });
541   }
542 
543   public void preDisableTable(final TableName tableName) throws IOException {
544     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
545       @Override
546       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
547           throws IOException {
548         oserver.preDisableTable(ctx, tableName);
549       }
550     });
551   }
552 
553   public void postDisableTable(final TableName tableName) throws IOException {
554     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
555       @Override
556       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
557           throws IOException {
558         oserver.postDisableTable(ctx, tableName);
559       }
560     });
561   }
562 
563   public void preDisableTableHandler(final TableName tableName) throws IOException {
564     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
565       @Override
566       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
567           throws IOException {
568         oserver.preDisableTableHandler(ctx, tableName);
569       }
570     });
571   }
572 
573   public void postDisableTableHandler(final TableName tableName) throws IOException {
574     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
575       @Override
576       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
577           throws IOException {
578         oserver.postDisableTableHandler(ctx, tableName);
579       }
580     });
581   }
582 
583   public boolean preMove(final HRegionInfo region, final ServerName srcServer,
584       final ServerName destServer) throws IOException {
585     return execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
586       @Override
587       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
588           throws IOException {
589         oserver.preMove(ctx, region, srcServer, destServer);
590       }
591     });
592   }
593 
594   public void postMove(final HRegionInfo region, final ServerName srcServer,
595       final ServerName destServer) throws IOException {
596     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
597       @Override
598       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
599           throws IOException {
600         oserver.postMove(ctx, region, srcServer, destServer);
601       }
602     });
603   }
604 
605   public boolean preAssign(final HRegionInfo regionInfo) throws IOException {
606     return execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
607       @Override
608       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
609           throws IOException {
610         oserver.preAssign(ctx, regionInfo);
611       }
612     });
613   }
614 
615   public void postAssign(final HRegionInfo regionInfo) throws IOException {
616     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
617       @Override
618       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
619           throws IOException {
620         oserver.postAssign(ctx, regionInfo);
621       }
622     });
623   }
624 
625   public boolean preUnassign(final HRegionInfo regionInfo, final boolean force)
626       throws IOException {
627     return execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
628       @Override
629       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
630           throws IOException {
631         oserver.preUnassign(ctx, regionInfo, force);
632       }
633     });
634   }
635 
636   public void postUnassign(final HRegionInfo regionInfo, final boolean force) throws IOException {
637     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
638       @Override
639       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
640           throws IOException {
641         oserver.postUnassign(ctx, regionInfo, force);
642       }
643     });
644   }
645 
646   public void preRegionOffline(final HRegionInfo regionInfo) throws IOException {
647     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
648       @Override
649       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
650           throws IOException {
651         oserver.preRegionOffline(ctx, regionInfo);
652       }
653     });
654   }
655 
656   public void postRegionOffline(final HRegionInfo regionInfo) throws IOException {
657     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
658       @Override
659       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
660           throws IOException {
661         oserver.postRegionOffline(ctx, regionInfo);
662       }
663     });
664   }
665 
666   public boolean preBalance() throws IOException {
667     return execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
668       @Override
669       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
670           throws IOException {
671         oserver.preBalance(ctx);
672       }
673     });
674   }
675 
676   public void postBalance(final List<RegionPlan> plans) throws IOException {
677     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
678       @Override
679       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
680           throws IOException {
681         oserver.postBalance(ctx, plans);
682       }
683     });
684   }
685 
686   public boolean preBalanceSwitch(final boolean b) throws IOException {
687     return execOperationWithResult(b, coprocessors.isEmpty() ? null :
688         new CoprocessorOperationWithResult<Boolean>() {
689       @Override
690       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
691           throws IOException {
692         setResult(oserver.preBalanceSwitch(ctx, getResult()));
693       }
694     });
695   }
696 
697   public void postBalanceSwitch(final boolean oldValue, final boolean newValue)
698       throws IOException {
699     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
700       @Override
701       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
702           throws IOException {
703         oserver.postBalanceSwitch(ctx, oldValue, newValue);
704       }
705     });
706   }
707 
708   public void preShutdown() throws IOException {
709     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
710       @Override
711       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
712           throws IOException {
713         oserver.preShutdown(ctx);
714       }
715       @Override
716       public void postEnvCall(MasterEnvironment env) {
717         // invoke coprocessor stop method
718         shutdown(env);
719       }
720     });
721   }
722 
723   public void preStopMaster() throws IOException {
724     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
725       @Override
726       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
727           throws IOException {
728         oserver.preStopMaster(ctx);
729       }
730       @Override
731       public void postEnvCall(MasterEnvironment env) {
732         // invoke coprocessor stop method
733         shutdown(env);
734       }
735     });
736   }
737 
738   public void preMasterInitialization() throws IOException {
739     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
740       @Override
741       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
742           throws IOException {
743         oserver.preMasterInitialization(ctx);
744       }
745     });
746   }
747 
748   public void postStartMaster() throws IOException {
749     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
750       @Override
751       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
752           throws IOException {
753         oserver.postStartMaster(ctx);
754       }
755     });
756   }
757 
758   public void preSnapshot(final SnapshotDescription snapshot,
759       final HTableDescriptor hTableDescriptor) throws IOException {
760     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
761       @Override
762       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
763           throws IOException {
764         oserver.preSnapshot(ctx, snapshot, hTableDescriptor);
765       }
766     });
767   }
768 
769   public void postSnapshot(final SnapshotDescription snapshot,
770       final HTableDescriptor hTableDescriptor) throws IOException {
771     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
772       @Override
773       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
774           throws IOException {
775         oserver.postSnapshot(ctx, snapshot, hTableDescriptor);
776       }
777     });
778   }
779 
780   public void preListSnapshot(final SnapshotDescription snapshot) throws IOException {
781     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
782       @Override
783       public void call(MasterObserver observer, ObserverContext<MasterCoprocessorEnvironment> ctx)
784           throws IOException {
785         observer.preListSnapshot(ctx, snapshot);
786       }
787     });
788   }
789 
790   public void postListSnapshot(final SnapshotDescription snapshot) throws IOException {
791     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
792       @Override
793       public void call(MasterObserver observer, ObserverContext<MasterCoprocessorEnvironment> ctx)
794           throws IOException {
795         observer.postListSnapshot(ctx, snapshot);
796       }
797     });
798   }
799   
800   public void preCloneSnapshot(final SnapshotDescription snapshot,
801       final HTableDescriptor hTableDescriptor) throws IOException {
802     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
803       @Override
804       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
805           throws IOException {
806         oserver.preCloneSnapshot(ctx, snapshot, hTableDescriptor);
807       }
808     });
809   }
810 
811   public void postCloneSnapshot(final SnapshotDescription snapshot,
812       final HTableDescriptor hTableDescriptor) throws IOException {
813     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
814       @Override
815       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
816           throws IOException {
817         oserver.postCloneSnapshot(ctx, snapshot, hTableDescriptor);
818       }
819     });
820   }
821 
822   public void preRestoreSnapshot(final SnapshotDescription snapshot,
823       final HTableDescriptor hTableDescriptor) throws IOException {
824     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
825       @Override
826       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
827           throws IOException {
828         oserver.preRestoreSnapshot(ctx, snapshot, hTableDescriptor);
829       }
830     });
831   }
832 
833   public void postRestoreSnapshot(final SnapshotDescription snapshot,
834       final HTableDescriptor hTableDescriptor) throws IOException {
835     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
836       @Override
837       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
838           throws IOException {
839         oserver.postRestoreSnapshot(ctx, snapshot, hTableDescriptor);
840       }
841     });
842   }
843 
844   public void preDeleteSnapshot(final SnapshotDescription snapshot) throws IOException {
845     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
846       @Override
847       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
848           throws IOException {
849         oserver.preDeleteSnapshot(ctx, snapshot);
850       }
851     });
852   }
853 
854   public void postDeleteSnapshot(final SnapshotDescription snapshot) throws IOException {
855     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
856       @Override
857       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
858           throws IOException {
859         oserver.postDeleteSnapshot(ctx, snapshot);
860       }
861     });
862   }
863 
864   @Deprecated
865   public boolean preGetTableDescriptors(final List<TableName> tableNamesList,
866     final List<HTableDescriptor> descriptors) throws IOException {
867     return execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
868       @Override
869       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
870           throws IOException {
871         oserver.preGetTableDescriptors(ctx, tableNamesList, descriptors);
872       }
873     });
874   }
875 
876   @Deprecated
877   public void postGetTableDescriptors(final List<HTableDescriptor> descriptors)
878       throws IOException {
879     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
880       @Override
881       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
882           throws IOException {
883         oserver.postGetTableDescriptors(ctx, descriptors);
884       }
885     });
886   }
887 
888   public boolean preGetTableDescriptors(final List<TableName> tableNamesList,
889       final List<HTableDescriptor> descriptors, final String regex) throws IOException {
890     return execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
891       @Override
892       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
893           throws IOException {
894         oserver.preGetTableDescriptors(ctx, tableNamesList, descriptors, regex);
895       }
896     });
897   }
898 
899   public void postGetTableDescriptors(final List<TableName> tableNamesList,
900       final List<HTableDescriptor> descriptors, final String regex) throws IOException {
901     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
902       @Override
903       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
904           throws IOException {
905         oserver.postGetTableDescriptors(ctx, tableNamesList, descriptors, regex);
906       }
907     });
908   }
909 
910   public boolean preGetTableNames(final List<HTableDescriptor> descriptors,
911       final String regex) throws IOException {
912     return execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
913       @Override
914       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
915           throws IOException {
916         oserver.preGetTableNames(ctx, descriptors, regex);
917       }
918     });
919   }
920 
921   public void postGetTableNames(final List<HTableDescriptor> descriptors,
922       final String regex) throws IOException {
923     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
924       @Override
925       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
926           throws IOException {
927         oserver.postGetTableNames(ctx, descriptors, regex);
928       }
929     });
930   }
931 
932   public void preTableFlush(final TableName tableName) throws IOException {
933     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
934       @Override
935       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
936           throws IOException {
937         oserver.preTableFlush(ctx, tableName);
938       }
939     });
940   }
941 
942   public void postTableFlush(final TableName tableName) throws IOException {
943     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
944       @Override
945       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
946           throws IOException {
947         oserver.postTableFlush(ctx, tableName);
948       }
949     });
950   }
951   
952   public void preSetUserQuota(final String user, final Quotas quotas) throws IOException {
953     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
954       @Override
955       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
956           throws IOException {
957         oserver.preSetUserQuota(ctx, user, quotas);
958       }
959     });
960   }
961 
962   public void postSetUserQuota(final String user, final Quotas quotas) throws IOException {
963     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
964       @Override
965       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
966           throws IOException {
967         oserver.postSetUserQuota(ctx, user, quotas);
968       }
969     });
970   }
971 
972   public void preSetUserQuota(final String user, final TableName table, final Quotas quotas)
973       throws IOException {
974     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
975       @Override
976       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
977           throws IOException {
978         oserver.preSetUserQuota(ctx, user, table, quotas);
979       }
980     });
981   }
982 
983   public void postSetUserQuota(final String user, final TableName table, final Quotas quotas)
984       throws IOException {
985     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
986       @Override
987       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
988           throws IOException {
989         oserver.postSetUserQuota(ctx, user, table, quotas);
990       }
991     });
992   }
993 
994   public void preSetUserQuota(final String user, final String namespace, final Quotas quotas)
995       throws IOException {
996     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
997       @Override
998       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
999           throws IOException {
1000         oserver.preSetUserQuota(ctx, user, namespace, quotas);
1001       }
1002     });
1003   }
1004 
1005   public void postSetUserQuota(final String user, final String namespace, final Quotas quotas)
1006       throws IOException {
1007     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
1008       @Override
1009       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
1010           throws IOException {
1011         oserver.postSetUserQuota(ctx, user, namespace, quotas);
1012       }
1013     });
1014   }
1015 
1016   public void preSetTableQuota(final TableName table, final Quotas quotas) throws IOException {
1017     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
1018       @Override
1019       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
1020           throws IOException {
1021         oserver.preSetTableQuota(ctx, table, quotas);
1022       }
1023     });
1024   }
1025 
1026   public void postSetTableQuota(final TableName table, final Quotas quotas) throws IOException {
1027     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
1028       @Override
1029       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
1030           throws IOException {
1031         oserver.postSetTableQuota(ctx, table, quotas);
1032       }
1033     });
1034   }
1035 
1036   public void preSetNamespaceQuota(final String namespace, final Quotas quotas) throws IOException {
1037     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
1038       @Override
1039       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
1040           throws IOException {
1041         oserver.preSetNamespaceQuota(ctx, namespace, quotas);
1042       }
1043     });
1044   }
1045 
1046   public void postSetNamespaceQuota(final String namespace, final Quotas quotas) 
1047       throws IOException {
1048     execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
1049       @Override
1050       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
1051           throws IOException {
1052         oserver.postSetNamespaceQuota(ctx, namespace, quotas);
1053       }
1054     });
1055   }
1056 
1057   private static abstract class CoprocessorOperation
1058       extends ObserverContext<MasterCoprocessorEnvironment> {
1059     public CoprocessorOperation() {
1060     }
1061 
1062     public abstract void call(MasterObserver oserver,
1063         ObserverContext<MasterCoprocessorEnvironment> ctx) throws IOException;
1064 
1065     public void postEnvCall(MasterEnvironment env) {
1066     }
1067   }
1068 
1069   private static abstract class CoprocessorOperationWithResult<T> extends CoprocessorOperation {
1070     private T result = null;
1071     public void setResult(final T result) { this.result = result; }
1072     public T getResult() { return this.result; }
1073   }
1074 
1075   private <T> T execOperationWithResult(final T defaultValue,
1076       final CoprocessorOperationWithResult<T> ctx) throws IOException {
1077     if (ctx == null) return defaultValue;
1078     ctx.setResult(defaultValue);
1079     execOperation(ctx);
1080     return ctx.getResult();
1081   }
1082 
1083   private boolean execOperation(final CoprocessorOperation ctx) throws IOException {
1084     if (ctx == null) return false;
1085     boolean bypass = false;
1086     for (MasterEnvironment env: coprocessors) {
1087       if (env.getInstance() instanceof MasterObserver) {
1088         ctx.prepare(env);
1089         Thread currentThread = Thread.currentThread();
1090         ClassLoader cl = currentThread.getContextClassLoader();
1091         try {
1092           currentThread.setContextClassLoader(env.getClassLoader());
1093           ctx.call((MasterObserver)env.getInstance(), ctx);
1094         } catch (Throwable e) {
1095           handleCoprocessorThrowable(env, e);
1096         } finally {
1097           currentThread.setContextClassLoader(cl);
1098         }
1099         bypass |= ctx.shouldBypass();
1100         if (ctx.shouldComplete()) {
1101           break;
1102         }
1103       }
1104       ctx.postEnvCall(env);
1105     }
1106     return bypass;
1107   }
1108 }