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.coprocessor;
21  
22  import static org.junit.Assert.assertFalse;
23  import static org.junit.Assert.assertNotNull;
24  import static org.junit.Assert.assertNull;
25  import static org.junit.Assert.assertTrue;
26  
27  import java.io.IOException;
28  import java.util.Collection;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.NavigableMap;
32  import java.util.concurrent.CountDownLatch;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  import org.apache.hadoop.conf.Configuration;
37  import org.apache.hadoop.hbase.CoprocessorEnvironment;
38  import org.apache.hadoop.hbase.HBaseTestingUtility;
39  import org.apache.hadoop.hbase.HColumnDescriptor;
40  import org.apache.hadoop.hbase.HRegionInfo;
41  import org.apache.hadoop.hbase.HTableDescriptor;
42  import org.apache.hadoop.hbase.MiniHBaseCluster;
43  import org.apache.hadoop.hbase.NamespaceDescriptor;
44  import org.apache.hadoop.hbase.ServerName;
45  import org.apache.hadoop.hbase.TableName;
46  import org.apache.hadoop.hbase.client.Admin;
47  import org.apache.hadoop.hbase.client.HTable;
48  import org.apache.hadoop.hbase.master.AssignmentManager;
49  import org.apache.hadoop.hbase.master.HMaster;
50  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
51  import org.apache.hadoop.hbase.master.RegionPlan;
52  import org.apache.hadoop.hbase.master.RegionState;
53  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
54  import org.apache.hadoop.hbase.protobuf.RequestConverter;
55  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
56  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.GetTableDescriptorsRequest;
57  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.GetTableNamesRequest;
58  import org.apache.hadoop.hbase.protobuf.generated.QuotaProtos.Quotas;
59  import org.apache.hadoop.hbase.regionserver.HRegionServer;
60  import org.apache.hadoop.hbase.testclassification.MediumTests;
61  import org.apache.hadoop.hbase.util.Bytes;
62  import org.apache.hadoop.hbase.util.Threads;
63  import org.junit.AfterClass;
64  import org.junit.BeforeClass;
65  import org.junit.Rule;
66  import org.junit.Test;
67  import org.junit.experimental.categories.Category;
68  import org.junit.rules.TestName;
69  
70  /**
71   * Tests invocation of the {@link org.apache.hadoop.hbase.coprocessor.MasterObserver}
72   * interface hooks at all appropriate times during normal HMaster operations.
73   */
74  @Category(MediumTests.class)
75  public class TestMasterObserver {
76    private static final Log LOG = LogFactory.getLog(TestMasterObserver.class);
77  
78    public static CountDownLatch tableCreationLatch = new CountDownLatch(1);
79    public static CountDownLatch tableDeletionLatch = new CountDownLatch(1);
80  
81    public static class CPMasterObserver implements MasterObserver {
82  
83      private boolean bypass = false;
84      private boolean preCreateTableCalled;
85      private boolean postCreateTableCalled;
86      private boolean preDeleteTableCalled;
87      private boolean postDeleteTableCalled;
88      private boolean preTruncateTableCalled;
89      private boolean postTruncateTableCalled;
90      private boolean preModifyTableCalled;
91      private boolean postModifyTableCalled;
92      private boolean preCreateNamespaceCalled;
93      private boolean postCreateNamespaceCalled;
94      private boolean preDeleteNamespaceCalled;
95      private boolean postDeleteNamespaceCalled;
96      private boolean preModifyNamespaceCalled;
97      private boolean postModifyNamespaceCalled;
98      private boolean preGetNamespaceDescriptorCalled;
99      private boolean postGetNamespaceDescriptorCalled;
100     private boolean preListNamespaceDescriptorsCalled;
101     private boolean postListNamespaceDescriptorsCalled;
102     private boolean preAddColumnCalled;
103     private boolean postAddColumnCalled;
104     private boolean preModifyColumnCalled;
105     private boolean postModifyColumnCalled;
106     private boolean preDeleteColumnCalled;
107     private boolean postDeleteColumnCalled;
108     private boolean preEnableTableCalled;
109     private boolean postEnableTableCalled;
110     private boolean preDisableTableCalled;
111     private boolean postDisableTableCalled;
112     private boolean preMoveCalled;
113     private boolean postMoveCalled;
114     private boolean preAssignCalled;
115     private boolean postAssignCalled;
116     private boolean preUnassignCalled;
117     private boolean postUnassignCalled;
118     private boolean preRegionOfflineCalled;
119     private boolean postRegionOfflineCalled;
120     private boolean preBalanceCalled;
121     private boolean postBalanceCalled;
122     private boolean preBalanceSwitchCalled;
123     private boolean postBalanceSwitchCalled;
124     private boolean preShutdownCalled;
125     private boolean preStopMasterCalled;
126     private boolean preMasterInitializationCalled;
127     private boolean postStartMasterCalled;
128     private boolean startCalled;
129     private boolean stopCalled;
130     private boolean preSnapshotCalled;
131     private boolean postSnapshotCalled;
132     private boolean preListSnapshotCalled;
133     private boolean postListSnapshotCalled;
134     private boolean preCloneSnapshotCalled;
135     private boolean postCloneSnapshotCalled;
136     private boolean preRestoreSnapshotCalled;
137     private boolean postRestoreSnapshotCalled;
138     private boolean preDeleteSnapshotCalled;
139     private boolean postDeleteSnapshotCalled;
140     private boolean preCreateTableHandlerCalled;
141     private boolean postCreateTableHandlerCalled;
142     private boolean preDeleteTableHandlerCalled;
143     private boolean postDeleteTableHandlerCalled;
144     private boolean preTruncateTableHandlerCalled;
145     private boolean postTruncateTableHandlerCalled;
146     private boolean preAddColumnHandlerCalled;
147     private boolean postAddColumnHandlerCalled;
148     private boolean preModifyColumnHandlerCalled;
149     private boolean postModifyColumnHandlerCalled;
150     private boolean preDeleteColumnHandlerCalled;
151     private boolean postDeleteColumnHandlerCalled;
152     private boolean preEnableTableHandlerCalled;
153     private boolean postEnableTableHandlerCalled;
154     private boolean preDisableTableHandlerCalled;
155     private boolean postDisableTableHandlerCalled;
156     private boolean preModifyTableHandlerCalled;
157     private boolean postModifyTableHandlerCalled;
158     private boolean preGetTableDescriptorsCalled;
159     private boolean postGetTableDescriptorsCalled;
160     private boolean postGetTableNamesCalled;
161     private boolean preGetTableNamesCalled;
162 
163     public void enableBypass(boolean bypass) {
164       this.bypass = bypass;
165     }
166 
167     public void resetStates() {
168       preCreateTableCalled = false;
169       postCreateTableCalled = false;
170       preDeleteTableCalled = false;
171       postDeleteTableCalled = false;
172       preTruncateTableCalled = false;
173       postTruncateTableCalled = false;
174       preModifyTableCalled = false;
175       postModifyTableCalled = false;
176       preCreateNamespaceCalled = false;
177       postCreateNamespaceCalled = false;
178       preDeleteNamespaceCalled = false;
179       postDeleteNamespaceCalled = false;
180       preModifyNamespaceCalled = false;
181       postModifyNamespaceCalled = false;
182       preGetNamespaceDescriptorCalled = false;
183       postGetNamespaceDescriptorCalled = false;
184       preListNamespaceDescriptorsCalled = false;
185       postListNamespaceDescriptorsCalled = false;
186       preAddColumnCalled = false;
187       postAddColumnCalled = false;
188       preModifyColumnCalled = false;
189       postModifyColumnCalled = false;
190       preDeleteColumnCalled = false;
191       postDeleteColumnCalled = false;
192       preEnableTableCalled = false;
193       postEnableTableCalled = false;
194       preDisableTableCalled = false;
195       postDisableTableCalled = false;
196       preMoveCalled= false;
197       postMoveCalled = false;
198       preAssignCalled = false;
199       postAssignCalled = false;
200       preUnassignCalled = false;
201       postUnassignCalled = false;
202       preRegionOfflineCalled = false;
203       postRegionOfflineCalled = false;
204       preBalanceCalled = false;
205       postBalanceCalled = false;
206       preBalanceSwitchCalled = false;
207       postBalanceSwitchCalled = false;
208       preSnapshotCalled = false;
209       postSnapshotCalled = false;
210       preListSnapshotCalled = false;
211       postListSnapshotCalled = false;
212       preCloneSnapshotCalled = false;
213       postCloneSnapshotCalled = false;
214       preRestoreSnapshotCalled = false;
215       postRestoreSnapshotCalled = false;
216       preDeleteSnapshotCalled = false;
217       postDeleteSnapshotCalled = false;
218       preCreateTableHandlerCalled = false;
219       postCreateTableHandlerCalled = false;
220       preDeleteTableHandlerCalled = false;
221       postDeleteTableHandlerCalled = false;
222       preTruncateTableHandlerCalled = false;
223       postTruncateTableHandlerCalled = false;
224       preModifyTableHandlerCalled = false;
225       postModifyTableHandlerCalled = false;
226       preAddColumnHandlerCalled = false;
227       postAddColumnHandlerCalled = false;
228       preModifyColumnHandlerCalled = false;
229       postModifyColumnHandlerCalled = false;
230       preDeleteColumnHandlerCalled = false;
231       postDeleteColumnHandlerCalled = false;
232       preEnableTableHandlerCalled = false;
233       postEnableTableHandlerCalled = false;
234       preDisableTableHandlerCalled = false;
235       postDisableTableHandlerCalled = false;
236       preModifyTableHandlerCalled = false;
237       postModifyTableHandlerCalled = false;
238       preGetTableDescriptorsCalled = false;
239       postGetTableDescriptorsCalled = false;
240       postGetTableNamesCalled = false;
241       preGetTableNamesCalled = false;
242     }
243 
244     @Override
245     public void preCreateTable(ObserverContext<MasterCoprocessorEnvironment> env,
246         HTableDescriptor desc, HRegionInfo[] regions) throws IOException {
247       if (bypass) {
248         env.bypass();
249       }
250       preCreateTableCalled = true;
251     }
252 
253     @Override
254     public void postCreateTable(ObserverContext<MasterCoprocessorEnvironment> env,
255         HTableDescriptor desc, HRegionInfo[] regions) throws IOException {
256       postCreateTableCalled = true;
257     }
258 
259     public boolean wasCreateTableCalled() {
260       return preCreateTableCalled && postCreateTableCalled;
261     }
262 
263     public boolean preCreateTableCalledOnly() {
264       return preCreateTableCalled && !postCreateTableCalled;
265     }
266 
267     @Override
268     public void preDeleteTable(ObserverContext<MasterCoprocessorEnvironment> env,
269         TableName tableName) throws IOException {
270       if (bypass) {
271         env.bypass();
272       }
273       preDeleteTableCalled = true;
274     }
275 
276     @Override
277     public void postDeleteTable(ObserverContext<MasterCoprocessorEnvironment> env,
278         TableName tableName) throws IOException {
279       postDeleteTableCalled = true;
280     }
281 
282     public boolean wasDeleteTableCalled() {
283       return preDeleteTableCalled && postDeleteTableCalled;
284     }
285 
286     public boolean preDeleteTableCalledOnly() {
287       return preDeleteTableCalled && !postDeleteTableCalled;
288     }
289 
290     @Override
291     public void preTruncateTable(ObserverContext<MasterCoprocessorEnvironment> env,
292         TableName tableName) throws IOException {
293       if (bypass) {
294         env.bypass();
295       }
296       preTruncateTableCalled = true;
297     }
298 
299     @Override
300     public void postTruncateTable(ObserverContext<MasterCoprocessorEnvironment> env,
301         TableName tableName) throws IOException {
302       postTruncateTableCalled = true;
303     }
304 
305     public boolean wasTruncateTableCalled() {
306       return preTruncateTableCalled && postTruncateTableCalled;
307     }
308 
309     public boolean preTruncateTableCalledOnly() {
310       return preTruncateTableCalled && !postTruncateTableCalled;
311     }
312 
313     @Override
314     public void preModifyTable(ObserverContext<MasterCoprocessorEnvironment> env,
315         TableName tableName, HTableDescriptor htd) throws IOException {
316       if (bypass) {
317         env.bypass();
318       }else{
319         env.shouldBypass();
320       }
321       preModifyTableCalled = true;
322     }
323 
324     @Override
325     public void postModifyTable(ObserverContext<MasterCoprocessorEnvironment> env,
326         TableName tableName, HTableDescriptor htd) throws IOException {
327       postModifyTableCalled = true;
328     }
329 
330     public boolean wasModifyTableCalled() {
331       return preModifyTableCalled && postModifyTableCalled;
332     }
333 
334     public boolean preModifyTableCalledOnly() {
335       return preModifyTableCalled && !postModifyTableCalled;
336     }
337 
338     @Override
339     public void preCreateNamespace(ObserverContext<MasterCoprocessorEnvironment> env,
340         NamespaceDescriptor ns) throws IOException {
341       if (bypass) {
342         env.bypass();
343       }
344       preCreateNamespaceCalled = true;
345     }
346 
347     @Override
348     public void postCreateNamespace(ObserverContext<MasterCoprocessorEnvironment> env,
349         NamespaceDescriptor ns) throws IOException {
350       postCreateNamespaceCalled = true;
351     }
352 
353     public boolean wasCreateNamespaceCalled() {
354       return preCreateNamespaceCalled && postCreateNamespaceCalled;
355     }
356 
357     public boolean preCreateNamespaceCalledOnly() {
358       return preCreateNamespaceCalled && !postCreateNamespaceCalled;
359     }
360 
361     @Override
362     public void preDeleteNamespace(ObserverContext<MasterCoprocessorEnvironment> env,
363         String name) throws IOException {
364       if (bypass) {
365         env.bypass();
366       }
367       preDeleteNamespaceCalled = true;
368     }
369 
370     @Override
371     public void postDeleteNamespace(ObserverContext<MasterCoprocessorEnvironment> env,
372         String name) throws IOException {
373       postDeleteNamespaceCalled = true;
374     }
375 
376     public boolean wasDeleteNamespaceCalled() {
377       return preDeleteNamespaceCalled && postDeleteNamespaceCalled;
378     }
379 
380     public boolean preDeleteNamespaceCalledOnly() {
381       return preDeleteNamespaceCalled && !postDeleteNamespaceCalled;
382     }
383 
384     @Override
385     public void preModifyNamespace(ObserverContext<MasterCoprocessorEnvironment> env,
386         NamespaceDescriptor ns) throws IOException {
387       if (bypass) {
388         env.bypass();
389       }
390       preModifyNamespaceCalled = true;
391     }
392 
393     @Override
394     public void postModifyNamespace(ObserverContext<MasterCoprocessorEnvironment> env,
395         NamespaceDescriptor ns) throws IOException {
396       postModifyNamespaceCalled = true;
397     }
398 
399     public boolean wasModifyNamespaceCalled() {
400       return preModifyNamespaceCalled && postModifyNamespaceCalled;
401     }
402 
403     public boolean preModifyNamespaceCalledOnly() {
404       return preModifyNamespaceCalled && !postModifyNamespaceCalled;
405     }
406 
407 
408     @Override
409     public void preGetNamespaceDescriptor(ObserverContext<MasterCoprocessorEnvironment> ctx,
410         String namespace) throws IOException {
411       preGetNamespaceDescriptorCalled = true;
412     }
413 
414     @Override
415     public void postGetNamespaceDescriptor(ObserverContext<MasterCoprocessorEnvironment> ctx,
416         NamespaceDescriptor ns) throws IOException {
417       postGetNamespaceDescriptorCalled = true;
418     }
419 
420     public boolean wasGetNamespaceDescriptorCalled() {
421       return preGetNamespaceDescriptorCalled && postGetNamespaceDescriptorCalled;
422     }
423 
424     @Override
425     public void preListNamespaceDescriptors(ObserverContext<MasterCoprocessorEnvironment> env,
426         List<NamespaceDescriptor> descriptors) throws IOException {
427       if (bypass) {
428         env.bypass();
429       }
430       preListNamespaceDescriptorsCalled = true;
431     }
432 
433     @Override
434     public void postListNamespaceDescriptors(ObserverContext<MasterCoprocessorEnvironment> env,
435         List<NamespaceDescriptor> descriptors) throws IOException {
436       postListNamespaceDescriptorsCalled = true;
437     }
438 
439     public boolean wasListNamespaceDescriptorsCalled() {
440       return preListNamespaceDescriptorsCalled && postListNamespaceDescriptorsCalled;
441     }
442 
443     public boolean preListNamespaceDescriptorsCalledOnly() {
444       return preListNamespaceDescriptorsCalled && !postListNamespaceDescriptorsCalled;
445     }
446 
447     @Override
448     public void preAddColumn(ObserverContext<MasterCoprocessorEnvironment> env,
449         TableName tableName, HColumnDescriptor column) throws IOException {
450       if (bypass) {
451         env.bypass();
452       }else{
453         env.shouldBypass();
454       }
455 
456       preAddColumnCalled = true;
457     }
458 
459     @Override
460     public void postAddColumn(ObserverContext<MasterCoprocessorEnvironment> env,
461         TableName tableName, HColumnDescriptor column) throws IOException {
462       postAddColumnCalled = true;
463     }
464 
465     public boolean wasAddColumnCalled() {
466       return preAddColumnCalled && postAddColumnCalled;
467     }
468 
469     public boolean preAddColumnCalledOnly() {
470       return preAddColumnCalled && !postAddColumnCalled;
471     }
472 
473     @Override
474     public void preModifyColumn(ObserverContext<MasterCoprocessorEnvironment> env,
475         TableName tableName, HColumnDescriptor descriptor) throws IOException {
476       if (bypass) {
477         env.bypass();
478       }
479       preModifyColumnCalled = true;
480     }
481 
482     @Override
483     public void postModifyColumn(ObserverContext<MasterCoprocessorEnvironment> env,
484         TableName tableName, HColumnDescriptor descriptor) throws IOException {
485       postModifyColumnCalled = true;
486     }
487 
488     public boolean wasModifyColumnCalled() {
489       return preModifyColumnCalled && postModifyColumnCalled;
490     }
491 
492     public boolean preModifyColumnCalledOnly() {
493       return preModifyColumnCalled && !postModifyColumnCalled;
494     }
495 
496     @Override
497     public void preDeleteColumn(ObserverContext<MasterCoprocessorEnvironment> env,
498         TableName tableName, byte[] c) throws IOException {
499       if (bypass) {
500         env.bypass();
501       }
502       preDeleteColumnCalled = true;
503     }
504 
505     @Override
506     public void postDeleteColumn(ObserverContext<MasterCoprocessorEnvironment> env,
507         TableName tableName, byte[] c) throws IOException {
508       postDeleteColumnCalled = true;
509     }
510 
511     public boolean wasDeleteColumnCalled() {
512       return preDeleteColumnCalled && postDeleteColumnCalled;
513     }
514 
515     public boolean preDeleteColumnCalledOnly() {
516       return preDeleteColumnCalled && !postDeleteColumnCalled;
517     }
518 
519     @Override
520     public void preEnableTable(ObserverContext<MasterCoprocessorEnvironment> env,
521         TableName tableName) throws IOException {
522       if (bypass) {
523         env.bypass();
524       }
525       preEnableTableCalled = true;
526     }
527 
528     @Override
529     public void postEnableTable(ObserverContext<MasterCoprocessorEnvironment> env,
530         TableName tableName) throws IOException {
531       postEnableTableCalled = true;
532     }
533 
534     public boolean wasEnableTableCalled() {
535       return preEnableTableCalled && postEnableTableCalled;
536     }
537 
538     public boolean preEnableTableCalledOnly() {
539       return preEnableTableCalled && !postEnableTableCalled;
540     }
541 
542     @Override
543     public void preDisableTable(ObserverContext<MasterCoprocessorEnvironment> env,
544         TableName tableName) throws IOException {
545       if (bypass) {
546         env.bypass();
547       }
548       preDisableTableCalled = true;
549     }
550 
551     @Override
552     public void postDisableTable(ObserverContext<MasterCoprocessorEnvironment> env,
553         TableName tableName) throws IOException {
554       postDisableTableCalled = true;
555     }
556 
557     public boolean wasDisableTableCalled() {
558       return preDisableTableCalled && postDisableTableCalled;
559     }
560 
561     public boolean preDisableTableCalledOnly() {
562       return preDisableTableCalled && !postDisableTableCalled;
563     }
564 
565     @Override
566     public void preMove(ObserverContext<MasterCoprocessorEnvironment> env,
567         HRegionInfo region, ServerName srcServer, ServerName destServer)
568     throws IOException {
569       if (bypass) {
570         env.bypass();
571       }
572       preMoveCalled = true;
573     }
574 
575     @Override
576     public void postMove(ObserverContext<MasterCoprocessorEnvironment> env, HRegionInfo region,
577         ServerName srcServer, ServerName destServer)
578     throws IOException {
579       postMoveCalled = true;
580     }
581 
582     public boolean wasMoveCalled() {
583       return preMoveCalled && postMoveCalled;
584     }
585 
586     public boolean preMoveCalledOnly() {
587       return preMoveCalled && !postMoveCalled;
588     }
589 
590     @Override
591     public void preAssign(ObserverContext<MasterCoprocessorEnvironment> env,
592         final HRegionInfo regionInfo) throws IOException {
593       if (bypass) {
594         env.bypass();
595       }
596       preAssignCalled = true;
597     }
598 
599     @Override
600     public void postAssign(ObserverContext<MasterCoprocessorEnvironment> env,
601         final HRegionInfo regionInfo) throws IOException {
602       postAssignCalled = true;
603     }
604 
605     public boolean wasAssignCalled() {
606       return preAssignCalled && postAssignCalled;
607     }
608 
609     public boolean preAssignCalledOnly() {
610       return preAssignCalled && !postAssignCalled;
611     }
612 
613     @Override
614     public void preUnassign(ObserverContext<MasterCoprocessorEnvironment> env,
615         final HRegionInfo regionInfo, final boolean force) throws IOException {
616       if (bypass) {
617         env.bypass();
618       }
619       preUnassignCalled = true;
620     }
621 
622     @Override
623     public void postUnassign(ObserverContext<MasterCoprocessorEnvironment> env,
624         final HRegionInfo regionInfo, final boolean force) throws IOException {
625       postUnassignCalled = true;
626     }
627 
628     public boolean wasUnassignCalled() {
629       return preUnassignCalled && postUnassignCalled;
630     }
631 
632     public boolean preUnassignCalledOnly() {
633       return preUnassignCalled && !postUnassignCalled;
634     }
635 
636     @Override
637     public void preRegionOffline(ObserverContext<MasterCoprocessorEnvironment> env,
638         final HRegionInfo regionInfo) throws IOException {
639       preRegionOfflineCalled = true;
640     }
641 
642     @Override
643     public void postRegionOffline(ObserverContext<MasterCoprocessorEnvironment> env,
644         final HRegionInfo regionInfo) throws IOException {
645       postRegionOfflineCalled = true;
646     }
647 
648     public boolean wasRegionOfflineCalled() {
649       return preRegionOfflineCalled && postRegionOfflineCalled;
650     }
651 
652     public boolean preRegionOfflineCalledOnly() {
653       return preRegionOfflineCalled && !postRegionOfflineCalled;
654     }
655 
656     @Override
657     public void preBalance(ObserverContext<MasterCoprocessorEnvironment> env)
658         throws IOException {
659       if (bypass) {
660         env.bypass();
661       }
662       preBalanceCalled = true;
663     }
664 
665     @Override
666     public void postBalance(ObserverContext<MasterCoprocessorEnvironment> env,
667         List<RegionPlan> plans) throws IOException {
668       postBalanceCalled = true;
669     }
670 
671     public boolean wasBalanceCalled() {
672       return preBalanceCalled && postBalanceCalled;
673     }
674 
675     public boolean preBalanceCalledOnly() {
676       return preBalanceCalled && !postBalanceCalled;
677     }
678 
679     @Override
680     public boolean preBalanceSwitch(ObserverContext<MasterCoprocessorEnvironment> env, boolean b)
681         throws IOException {
682       if (bypass) {
683         env.bypass();
684       }
685       preBalanceSwitchCalled = true;
686       return b;
687     }
688 
689     @Override
690     public void postBalanceSwitch(ObserverContext<MasterCoprocessorEnvironment> env,
691         boolean oldValue, boolean newValue) throws IOException {
692       postBalanceSwitchCalled = true;
693     }
694 
695     public boolean wasBalanceSwitchCalled() {
696       return preBalanceSwitchCalled && postBalanceSwitchCalled;
697     }
698 
699     public boolean preBalanceSwitchCalledOnly() {
700       return preBalanceSwitchCalled && !postBalanceSwitchCalled;
701     }
702 
703     @Override
704     public void preShutdown(ObserverContext<MasterCoprocessorEnvironment> env)
705         throws IOException {
706       preShutdownCalled = true;
707     }
708 
709     @Override
710     public void preStopMaster(ObserverContext<MasterCoprocessorEnvironment> env)
711         throws IOException {
712       preStopMasterCalled = true;
713     }
714 
715     @Override
716     public void preMasterInitialization(
717         ObserverContext<MasterCoprocessorEnvironment> ctx) throws IOException {
718       preMasterInitializationCalled = true;
719     }
720 
721     public boolean wasMasterInitializationCalled(){
722       return preMasterInitializationCalled;
723     }
724 
725     @Override
726     public void postStartMaster(ObserverContext<MasterCoprocessorEnvironment> ctx)
727         throws IOException {
728       postStartMasterCalled = true;
729     }
730 
731     public boolean wasStartMasterCalled() {
732       return postStartMasterCalled;
733     }
734 
735     @Override
736     public void start(CoprocessorEnvironment env) throws IOException {
737       startCalled = true;
738     }
739 
740     @Override
741     public void stop(CoprocessorEnvironment env) throws IOException {
742       stopCalled = true;
743     }
744 
745     public boolean wasStarted() { return startCalled; }
746 
747     public boolean wasStopped() { return stopCalled; }
748 
749     @Override
750     public void preSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
751         final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor)
752         throws IOException {
753       preSnapshotCalled = true;
754     }
755 
756     @Override
757     public void postSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
758         final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor)
759         throws IOException {
760       postSnapshotCalled = true;
761     }
762 
763     public boolean wasSnapshotCalled() {
764       return preSnapshotCalled && postSnapshotCalled;
765     }
766     
767     @Override
768     public void preListSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
769         final SnapshotDescription snapshot) throws IOException {
770       preListSnapshotCalled = true;
771     }
772 
773     @Override
774     public void postListSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
775         final SnapshotDescription snapshot) throws IOException {
776       postListSnapshotCalled = true;
777     }
778 
779     public boolean wasListSnapshotCalled() {
780       return preListSnapshotCalled && postListSnapshotCalled;
781     }
782 
783     @Override
784     public void preCloneSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
785         final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor)
786         throws IOException {
787       preCloneSnapshotCalled = true;
788     }
789 
790     @Override
791     public void postCloneSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
792         final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor)
793         throws IOException {
794       postCloneSnapshotCalled = true;
795     }
796 
797     public boolean wasCloneSnapshotCalled() {
798       return preCloneSnapshotCalled && postCloneSnapshotCalled;
799     }
800 
801     @Override
802     public void preRestoreSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
803         final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor)
804         throws IOException {
805       preRestoreSnapshotCalled = true;
806     }
807 
808     @Override
809     public void postRestoreSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
810         final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor)
811         throws IOException {
812       postRestoreSnapshotCalled = true;
813     }
814 
815     public boolean wasRestoreSnapshotCalled() {
816       return preRestoreSnapshotCalled && postRestoreSnapshotCalled;
817     }
818 
819     @Override
820     public void preDeleteSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
821         final SnapshotDescription snapshot) throws IOException {
822       preDeleteSnapshotCalled = true;
823     }
824 
825     @Override
826     public void postDeleteSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
827         final SnapshotDescription snapshot) throws IOException {
828       postDeleteSnapshotCalled = true;
829     }
830 
831     public boolean wasDeleteSnapshotCalled() {
832       return preDeleteSnapshotCalled && postDeleteSnapshotCalled;
833     }
834 
835     @Override
836     public void preCreateTableHandler(
837         ObserverContext<MasterCoprocessorEnvironment> env,
838         HTableDescriptor desc, HRegionInfo[] regions) throws IOException {
839       if (bypass) {
840         env.bypass();
841       }
842       preCreateTableHandlerCalled = true;
843     }
844 
845     @Override
846     public void postCreateTableHandler(
847         ObserverContext<MasterCoprocessorEnvironment> ctx,
848         HTableDescriptor desc, HRegionInfo[] regions) throws IOException {
849       postCreateTableHandlerCalled = true;
850       tableCreationLatch.countDown();
851     }
852 
853     public boolean wasPreCreateTableHandlerCalled(){
854       return preCreateTableHandlerCalled;
855     }
856     public boolean wasCreateTableHandlerCalled() {
857       return preCreateTableHandlerCalled && postCreateTableHandlerCalled;
858     }
859 
860     public boolean wasCreateTableHandlerCalledOnly() {
861       return preCreateTableHandlerCalled && !postCreateTableHandlerCalled;
862     }
863 
864     @Override
865     public void preDeleteTableHandler(
866         ObserverContext<MasterCoprocessorEnvironment> env, TableName tableName)
867         throws IOException {
868       if (bypass) {
869         env.bypass();
870       }
871       preDeleteTableHandlerCalled = true;
872     }
873 
874     @Override
875     public void postDeleteTableHandler(
876         ObserverContext<MasterCoprocessorEnvironment> ctx, TableName tableName)
877         throws IOException {
878       postDeleteTableHandlerCalled = true;
879       tableDeletionLatch.countDown();
880     }
881 
882     public boolean wasDeleteTableHandlerCalled() {
883       return preDeleteTableHandlerCalled && postDeleteTableHandlerCalled;
884     }
885 
886     public boolean wasDeleteTableHandlerCalledOnly() {
887       return preDeleteTableHandlerCalled && !postDeleteTableHandlerCalled;
888     }
889 
890     @Override
891     public void preTruncateTableHandler(
892         ObserverContext<MasterCoprocessorEnvironment> env, TableName tableName)
893         throws IOException {
894       if (bypass) {
895         env.bypass();
896       }
897       preTruncateTableHandlerCalled = true;
898     }
899 
900     @Override
901     public void postTruncateTableHandler(
902         ObserverContext<MasterCoprocessorEnvironment> ctx, TableName tableName)
903         throws IOException {
904       postTruncateTableHandlerCalled = true;
905     }
906 
907     public boolean wasTruncateTableHandlerCalled() {
908       return preTruncateTableHandlerCalled && postTruncateTableHandlerCalled;
909     }
910 
911     public boolean wasTruncateTableHandlerCalledOnly() {
912       return preTruncateTableHandlerCalled && !postTruncateTableHandlerCalled;
913     }
914 
915     @Override
916     public void preModifyTableHandler(
917         ObserverContext<MasterCoprocessorEnvironment> env, TableName tableName,
918         HTableDescriptor htd) throws IOException {
919       if (bypass) {
920         env.bypass();
921       }
922       preModifyTableHandlerCalled = true;
923     }
924 
925     @Override
926     public void postModifyTableHandler(
927         ObserverContext<MasterCoprocessorEnvironment> env, TableName tableName,
928         HTableDescriptor htd) throws IOException {
929       postModifyTableHandlerCalled = true;
930     }
931 
932     public boolean wasModifyTableHandlerCalled() {
933       return preModifyColumnHandlerCalled && postModifyColumnHandlerCalled;
934     }
935 
936     public boolean wasModifyTableHandlerCalledOnly() {
937       return preModifyColumnHandlerCalled && !postModifyColumnHandlerCalled;
938     }
939 
940     @Override
941     public void preAddColumnHandler(
942         ObserverContext<MasterCoprocessorEnvironment> env, TableName tableName,
943         HColumnDescriptor column) throws IOException {
944       if (bypass) {
945         env.bypass();
946       }
947       preAddColumnHandlerCalled = true;
948     }
949 
950     @Override
951     public void postAddColumnHandler(
952         ObserverContext<MasterCoprocessorEnvironment> ctx, TableName tableName,
953         HColumnDescriptor column) throws IOException {
954       postAddColumnHandlerCalled = true;
955     }
956     public boolean wasAddColumnHandlerCalled() {
957       return preAddColumnHandlerCalled && postAddColumnHandlerCalled;
958     }
959 
960     public boolean preAddColumnHandlerCalledOnly() {
961       return preAddColumnHandlerCalled && !postAddColumnHandlerCalled;
962     }
963 
964     @Override
965     public void preModifyColumnHandler(
966         ObserverContext<MasterCoprocessorEnvironment> env, TableName tableName,
967         HColumnDescriptor descriptor) throws IOException {
968       if (bypass) {
969         env.bypass();
970       }
971       preModifyColumnHandlerCalled = true;
972     }
973 
974     @Override
975     public void postModifyColumnHandler(
976         ObserverContext<MasterCoprocessorEnvironment> ctx, TableName tableName,
977         HColumnDescriptor descriptor) throws IOException {
978       postModifyColumnHandlerCalled = true;
979     }
980 
981     public boolean wasModifyColumnHandlerCalled() {
982       return preModifyColumnHandlerCalled && postModifyColumnHandlerCalled;
983     }
984 
985     public boolean preModifyColumnHandlerCalledOnly() {
986       return preModifyColumnHandlerCalled && !postModifyColumnHandlerCalled;
987     }
988     @Override
989     public void preDeleteColumnHandler(
990         ObserverContext<MasterCoprocessorEnvironment> env, TableName tableName,
991         byte[] c) throws IOException {
992       if (bypass) {
993         env.bypass();
994       }
995       preDeleteColumnHandlerCalled = true;
996     }
997 
998     @Override
999     public void postDeleteColumnHandler(
1000         ObserverContext<MasterCoprocessorEnvironment> ctx, TableName tableName,
1001         byte[] c) throws IOException {
1002       postDeleteColumnHandlerCalled = true;
1003     }
1004 
1005     public boolean wasDeleteColumnHandlerCalled() {
1006       return preDeleteColumnHandlerCalled && postDeleteColumnHandlerCalled;
1007     }
1008 
1009     public boolean preDeleteColumnHandlerCalledOnly() {
1010       return preDeleteColumnHandlerCalled && !postDeleteColumnHandlerCalled;
1011     }
1012 
1013     @Override
1014     public void preEnableTableHandler(
1015         ObserverContext<MasterCoprocessorEnvironment> env, TableName tableName)
1016         throws IOException {
1017       if (bypass) {
1018         env.bypass();
1019       }
1020       preEnableTableHandlerCalled = true;
1021     }
1022 
1023     @Override
1024     public void postEnableTableHandler(
1025         ObserverContext<MasterCoprocessorEnvironment> ctx, TableName tableName)
1026         throws IOException {
1027       postEnableTableHandlerCalled = true;
1028     }
1029 
1030     public boolean wasEnableTableHandlerCalled() {
1031       return preEnableTableHandlerCalled && postEnableTableHandlerCalled;
1032     }
1033 
1034     public boolean preEnableTableHandlerCalledOnly() {
1035       return preEnableTableHandlerCalled && !postEnableTableHandlerCalled;
1036     }
1037 
1038     @Override
1039     public void preDisableTableHandler(
1040         ObserverContext<MasterCoprocessorEnvironment> env, TableName tableName)
1041         throws IOException {
1042       if (bypass) {
1043         env.bypass();
1044       }
1045       preDisableTableHandlerCalled = true;
1046     }
1047 
1048     @Override
1049     public void postDisableTableHandler(
1050         ObserverContext<MasterCoprocessorEnvironment> ctx, TableName tableName)
1051         throws IOException {
1052       postDisableTableHandlerCalled = true;
1053     }
1054 
1055     public boolean wasDisableTableHandlerCalled() {
1056       return preDisableTableHandlerCalled && postDisableTableHandlerCalled;
1057     }
1058 
1059     public boolean preDisableTableHandlerCalledOnly() {
1060       return preDisableTableHandlerCalled && !postDisableTableHandlerCalled;
1061     }
1062 
1063     @Override
1064     public void preGetTableDescriptors(ObserverContext<MasterCoprocessorEnvironment> ctx,
1065         List<TableName> tableNamesList, List<HTableDescriptor> descriptors) throws IOException {
1066     }
1067 
1068     @Override
1069     public void postGetTableDescriptors(ObserverContext<MasterCoprocessorEnvironment> ctx,
1070         List<HTableDescriptor> descriptors) throws IOException {
1071     }
1072 
1073     @Override
1074     public void preGetTableDescriptors(ObserverContext<MasterCoprocessorEnvironment> ctx,
1075         List<TableName> tableNamesList, List<HTableDescriptor> descriptors,
1076         String regex) throws IOException {
1077       preGetTableDescriptorsCalled = true;
1078     }
1079 
1080     @Override
1081     public void postGetTableDescriptors(ObserverContext<MasterCoprocessorEnvironment> ctx,
1082         List<TableName> tableNamesList, List<HTableDescriptor> descriptors,
1083         String regex) throws IOException {
1084       postGetTableDescriptorsCalled = true;
1085     }
1086 
1087     public boolean wasGetTableDescriptorsCalled() {
1088       return preGetTableDescriptorsCalled && postGetTableDescriptorsCalled;
1089     }
1090 
1091     @Override
1092     public void preGetTableNames(ObserverContext<MasterCoprocessorEnvironment> ctx,
1093         List<HTableDescriptor> descriptors, String regex) throws IOException {
1094       preGetTableNamesCalled = true;
1095     }
1096 
1097     @Override
1098     public void postGetTableNames(ObserverContext<MasterCoprocessorEnvironment> ctx,
1099         List<HTableDescriptor> descriptors, String regex) throws IOException {
1100       postGetTableNamesCalled = true;
1101     }
1102 
1103     public boolean wasGetTableNamesCalled() {
1104       return preGetTableNamesCalled && postGetTableNamesCalled;
1105     }
1106 
1107     @Override
1108     public void preTableFlush(ObserverContext<MasterCoprocessorEnvironment> ctx,
1109         TableName tableName) throws IOException {
1110     }
1111 
1112     @Override
1113     public void postTableFlush(ObserverContext<MasterCoprocessorEnvironment> ctx,
1114         TableName tableName) throws IOException {
1115     }
1116     
1117     @Override
1118     public void preSetUserQuota(final ObserverContext<MasterCoprocessorEnvironment> ctx,
1119         final String userName, final Quotas quotas) throws IOException {
1120     }
1121 
1122     @Override
1123     public void postSetUserQuota(final ObserverContext<MasterCoprocessorEnvironment> ctx,
1124         final String userName, final Quotas quotas) throws IOException {
1125     }
1126 
1127     @Override
1128     public void preSetUserQuota(final ObserverContext<MasterCoprocessorEnvironment> ctx,
1129         final String userName, final TableName tableName, final Quotas quotas) throws IOException {
1130     }
1131 
1132     @Override
1133     public void postSetUserQuota(final ObserverContext<MasterCoprocessorEnvironment> ctx,
1134         final String userName, final TableName tableName, final Quotas quotas) throws IOException {
1135     }
1136 
1137     @Override
1138     public void preSetUserQuota(final ObserverContext<MasterCoprocessorEnvironment> ctx,
1139         final String userName, final String namespace, final Quotas quotas) throws IOException {
1140     }
1141 
1142     @Override
1143     public void postSetUserQuota(final ObserverContext<MasterCoprocessorEnvironment> ctx,
1144         final String userName, final String namespace, final Quotas quotas) throws IOException {
1145     }
1146 
1147     @Override
1148     public void preSetTableQuota(final ObserverContext<MasterCoprocessorEnvironment> ctx,
1149         final TableName tableName, final Quotas quotas) throws IOException {
1150     }
1151 
1152     @Override
1153     public void postSetTableQuota(final ObserverContext<MasterCoprocessorEnvironment> ctx,
1154         final TableName tableName, final Quotas quotas) throws IOException {
1155     }
1156 
1157     @Override
1158     public void preSetNamespaceQuota(final ObserverContext<MasterCoprocessorEnvironment> ctx,
1159         final String namespace, final Quotas quotas) throws IOException {
1160     }
1161 
1162     @Override
1163     public void postSetNamespaceQuota(final ObserverContext<MasterCoprocessorEnvironment> ctx,
1164         final String namespace, final Quotas quotas) throws IOException {
1165     }
1166   }
1167 
1168   private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
1169   private static byte[] TEST_SNAPSHOT = Bytes.toBytes("observed_snapshot");
1170   private static TableName TEST_CLONE = TableName.valueOf("observed_clone");
1171   private static byte[] TEST_FAMILY = Bytes.toBytes("fam1");
1172   private static byte[] TEST_FAMILY2 = Bytes.toBytes("fam2");
1173   @Rule public TestName name = new TestName();
1174 
1175   @BeforeClass
1176   public static void setupBeforeClass() throws Exception {
1177     Configuration conf = UTIL.getConfiguration();
1178     conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
1179         CPMasterObserver.class.getName());
1180     conf.set("hbase.master.hfilecleaner.plugins",
1181       "org.apache.hadoop.hbase.master.cleaner.HFileLinkCleaner," +
1182       "org.apache.hadoop.hbase.master.snapshot.SnapshotHFileCleaner");
1183     conf.set("hbase.master.logcleaner.plugins",
1184       "org.apache.hadoop.hbase.master.snapshot.SnapshotLogCleaner");
1185     // We need more than one data server on this test
1186     UTIL.startMiniCluster(2);
1187   }
1188 
1189   @AfterClass
1190   public static void tearDownAfterClass() throws Exception {
1191     UTIL.shutdownMiniCluster();
1192   }
1193 
1194   @Test (timeout=180000)
1195   public void testStarted() throws Exception {
1196     MiniHBaseCluster cluster = UTIL.getHBaseCluster();
1197 
1198     HMaster master = cluster.getMaster();
1199     assertTrue("Master should be active", master.isActiveMaster());
1200     MasterCoprocessorHost host = master.getMasterCoprocessorHost();
1201     assertNotNull("CoprocessorHost should not be null", host);
1202     CPMasterObserver cp = (CPMasterObserver)host.findCoprocessor(
1203         CPMasterObserver.class.getName());
1204     assertNotNull("CPMasterObserver coprocessor not found or not installed!", cp);
1205 
1206     // check basic lifecycle
1207     assertTrue("MasterObserver should have been started", cp.wasStarted());
1208     assertTrue("preMasterInitialization() hook should have been called",
1209         cp.wasMasterInitializationCalled());
1210     assertTrue("postStartMaster() hook should have been called",
1211         cp.wasStartMasterCalled());
1212   }
1213 
1214   @Test (timeout=180000)
1215   public void testTableOperations() throws Exception {
1216     MiniHBaseCluster cluster = UTIL.getHBaseCluster();
1217     final TableName tableName = TableName.valueOf(name.getMethodName());
1218     HMaster master = cluster.getMaster();
1219     MasterCoprocessorHost host = master.getMasterCoprocessorHost();
1220     CPMasterObserver cp = (CPMasterObserver)host.findCoprocessor(
1221         CPMasterObserver.class.getName());
1222     cp.enableBypass(true);
1223     cp.resetStates();
1224     assertFalse("No table created yet", cp.wasCreateTableCalled());
1225 
1226     // create a table
1227     HTableDescriptor htd = new HTableDescriptor(tableName);
1228     htd.addFamily(new HColumnDescriptor(TEST_FAMILY));
1229     Admin admin = UTIL.getHBaseAdmin();
1230 
1231     tableCreationLatch = new CountDownLatch(1);
1232     admin.createTable(htd);
1233     // preCreateTable can't bypass default action.
1234     assertTrue("Test table should be created", cp.wasCreateTableCalled());
1235     tableCreationLatch.await();
1236     assertTrue("Table pre create handler called.", cp
1237         .wasPreCreateTableHandlerCalled());
1238     assertTrue("Table create handler should be called.",
1239         cp.wasCreateTableHandlerCalled());
1240 
1241     tableCreationLatch = new CountDownLatch(1);
1242     admin.disableTable(tableName);
1243     assertTrue(admin.isTableDisabled(tableName));
1244     // preDisableTable can't bypass default action.
1245     assertTrue("Coprocessor should have been called on table disable",
1246       cp.wasDisableTableCalled());
1247     assertTrue("Disable table handler should be called.",
1248         cp.wasDisableTableHandlerCalled());
1249 
1250     // enable
1251     assertFalse(cp.wasEnableTableCalled());
1252     admin.enableTable(tableName);
1253     assertTrue(admin.isTableEnabled(tableName));
1254     // preEnableTable can't bypass default action.
1255     assertTrue("Coprocessor should have been called on table enable",
1256       cp.wasEnableTableCalled());
1257     assertTrue("Enable table handler should be called.",
1258         cp.wasEnableTableHandlerCalled());
1259 
1260     admin.disableTable(tableName);
1261     assertTrue(admin.isTableDisabled(tableName));
1262 
1263     // modify table
1264     htd.setMaxFileSize(512 * 1024 * 1024);
1265     modifyTableSync(admin, tableName, htd);
1266     // preModifyTable can't bypass default action.
1267     assertTrue("Test table should have been modified",
1268       cp.wasModifyTableCalled());
1269 
1270     // add a column family
1271     admin.addColumn(tableName, new HColumnDescriptor(TEST_FAMILY2));
1272     assertTrue("New column family shouldn't have been added to test table",
1273       cp.preAddColumnCalledOnly());
1274 
1275     // modify a column family
1276     HColumnDescriptor hcd1 = new HColumnDescriptor(TEST_FAMILY2);
1277     hcd1.setMaxVersions(25);
1278     admin.modifyColumn(tableName, hcd1);
1279     assertTrue("Second column family should be modified",
1280       cp.preModifyColumnCalledOnly());
1281 
1282     // truncate table
1283     admin.truncateTable(tableName, false);
1284 
1285     // delete table
1286     admin.disableTable(tableName);
1287     assertTrue(admin.isTableDisabled(tableName));
1288     deleteTable(admin, tableName);
1289     assertFalse("Test table should have been deleted",
1290         admin.tableExists(tableName));
1291     // preDeleteTable can't bypass default action.
1292     assertTrue("Coprocessor should have been called on table delete",
1293         cp.wasDeleteTableCalled());
1294     assertTrue("Delete table handler should be called.",
1295         cp.wasDeleteTableHandlerCalled());
1296 
1297     // turn off bypass, run the tests again
1298     cp.enableBypass(false);
1299     cp.resetStates();
1300 
1301     admin.createTable(htd);
1302     assertTrue("Test table should be created", cp.wasCreateTableCalled());
1303     tableCreationLatch.await();
1304     assertTrue("Table pre create handler called.", cp
1305         .wasPreCreateTableHandlerCalled());
1306     assertTrue("Table create handler should be called.",
1307         cp.wasCreateTableHandlerCalled());
1308 
1309     // disable
1310     assertFalse(cp.wasDisableTableCalled());
1311     assertFalse(cp.wasDisableTableHandlerCalled());
1312     admin.disableTable(tableName);
1313     assertTrue(admin.isTableDisabled(tableName));
1314     assertTrue("Coprocessor should have been called on table disable",
1315       cp.wasDisableTableCalled());
1316     assertTrue("Disable table handler should be called.",
1317         cp.wasDisableTableHandlerCalled());
1318 
1319     // modify table
1320     htd.setMaxFileSize(512 * 1024 * 1024);
1321     modifyTableSync(admin, tableName, htd);
1322     assertTrue("Test table should have been modified",
1323         cp.wasModifyTableCalled());
1324     // add a column family
1325     admin.addColumn(tableName, new HColumnDescriptor(TEST_FAMILY2));
1326     assertTrue("New column family should have been added to test table",
1327         cp.wasAddColumnCalled());
1328     assertTrue("Add column handler should be called.",
1329         cp.wasAddColumnHandlerCalled());
1330 
1331     // modify a column family
1332     HColumnDescriptor hcd = new HColumnDescriptor(TEST_FAMILY2);
1333     hcd.setMaxVersions(25);
1334     admin.modifyColumn(tableName, hcd);
1335     assertTrue("Second column family should be modified",
1336         cp.wasModifyColumnCalled());
1337     assertTrue("Modify table handler should be called.",
1338         cp.wasModifyColumnHandlerCalled());
1339 
1340     // enable
1341     assertFalse(cp.wasEnableTableCalled());
1342     assertFalse(cp.wasEnableTableHandlerCalled());
1343     admin.enableTable(tableName);
1344     assertTrue(admin.isTableEnabled(tableName));
1345     assertTrue("Coprocessor should have been called on table enable",
1346         cp.wasEnableTableCalled());
1347     assertTrue("Enable table handler should be called.",
1348         cp.wasEnableTableHandlerCalled());
1349 
1350     // disable again
1351     admin.disableTable(tableName);
1352     assertTrue(admin.isTableDisabled(tableName));
1353 
1354     // delete column
1355     assertFalse("No column family deleted yet", cp.wasDeleteColumnCalled());
1356     assertFalse("Delete table column handler should not be called.",
1357         cp.wasDeleteColumnHandlerCalled());
1358     admin.deleteColumn(tableName, TEST_FAMILY2);
1359     HTableDescriptor tableDesc = admin.getTableDescriptor(tableName);
1360     assertNull("'"+Bytes.toString(TEST_FAMILY2)+"' should have been removed",
1361         tableDesc.getFamily(TEST_FAMILY2));
1362     assertTrue("Coprocessor should have been called on column delete",
1363         cp.wasDeleteColumnCalled());
1364     assertTrue("Delete table column handler should be called.",
1365         cp.wasDeleteColumnHandlerCalled());
1366 
1367     // delete table
1368     assertFalse("No table deleted yet", cp.wasDeleteTableCalled());
1369     assertFalse("Delete table handler should not be called.",
1370         cp.wasDeleteTableHandlerCalled());
1371     deleteTable(admin, tableName);
1372     assertFalse("Test table should have been deleted",
1373         admin.tableExists(tableName));
1374     assertTrue("Coprocessor should have been called on table delete",
1375         cp.wasDeleteTableCalled());
1376     assertTrue("Delete table handler should be called.",
1377         cp.wasDeleteTableHandlerCalled());
1378   }
1379 
1380   @Test (timeout=180000)
1381   public void testSnapshotOperations() throws Exception {
1382     final TableName tableName = TableName.valueOf(name.getMethodName());
1383     MiniHBaseCluster cluster = UTIL.getHBaseCluster();
1384     HMaster master = cluster.getMaster();
1385     MasterCoprocessorHost host = master.getMasterCoprocessorHost();
1386     CPMasterObserver cp = (CPMasterObserver)host.findCoprocessor(
1387         CPMasterObserver.class.getName());
1388     cp.resetStates();
1389 
1390     // create a table
1391     HTableDescriptor htd = new HTableDescriptor(tableName);
1392     htd.addFamily(new HColumnDescriptor(TEST_FAMILY));
1393     Admin admin = UTIL.getHBaseAdmin();
1394 
1395     tableCreationLatch = new CountDownLatch(1);
1396     admin.createTable(htd);
1397     tableCreationLatch.await();
1398     tableCreationLatch = new CountDownLatch(1);
1399 
1400     admin.disableTable(tableName);
1401     assertTrue(admin.isTableDisabled(tableName));
1402 
1403     try {
1404       // Test snapshot operation
1405       assertFalse("Coprocessor should not have been called yet",
1406         cp.wasSnapshotCalled());
1407       admin.snapshot(TEST_SNAPSHOT, tableName);
1408       assertTrue("Coprocessor should have been called on snapshot",
1409         cp.wasSnapshotCalled());
1410       
1411       //Test list operation
1412       admin.listSnapshots();
1413       assertTrue("Coprocessor should have been called on snapshot list",
1414         cp.wasListSnapshotCalled());
1415 
1416       // Test clone operation
1417       admin.cloneSnapshot(TEST_SNAPSHOT, TEST_CLONE);
1418       assertTrue("Coprocessor should have been called on snapshot clone",
1419         cp.wasCloneSnapshotCalled());
1420       assertFalse("Coprocessor restore should not have been called on snapshot clone",
1421         cp.wasRestoreSnapshotCalled());
1422       admin.disableTable(TEST_CLONE);
1423       assertTrue(admin.isTableDisabled(tableName));
1424       deleteTable(admin, TEST_CLONE);
1425 
1426       // Test restore operation
1427       cp.resetStates();
1428       admin.restoreSnapshot(TEST_SNAPSHOT);
1429       assertTrue("Coprocessor should have been called on snapshot restore",
1430         cp.wasRestoreSnapshotCalled());
1431       assertFalse("Coprocessor clone should not have been called on snapshot restore",
1432         cp.wasCloneSnapshotCalled());
1433 
1434       admin.deleteSnapshot(TEST_SNAPSHOT);
1435       assertTrue("Coprocessor should have been called on snapshot delete",
1436         cp.wasDeleteSnapshotCalled());
1437     } finally {
1438       deleteTable(admin, tableName);
1439     }
1440   }
1441 
1442   @Test (timeout=180000)
1443   public void testNamespaceOperations() throws Exception {
1444     MiniHBaseCluster cluster = UTIL.getHBaseCluster();
1445     String testNamespace = "observed_ns";
1446     HMaster master = cluster.getMaster();
1447     MasterCoprocessorHost host = master.getMasterCoprocessorHost();
1448     CPMasterObserver cp = (CPMasterObserver)host.findCoprocessor(
1449         CPMasterObserver.class.getName());
1450 
1451     cp.enableBypass(false);
1452     cp.resetStates();
1453 
1454 
1455     // create a table
1456     Admin admin = UTIL.getHBaseAdmin();
1457     admin.createNamespace(NamespaceDescriptor.create(testNamespace).build());
1458     assertTrue("Test namespace should be created", cp.wasCreateNamespaceCalled());
1459 
1460     assertNotNull(admin.getNamespaceDescriptor(testNamespace));
1461     assertTrue("Test namespace descriptor should have been called",
1462         cp.wasGetNamespaceDescriptorCalled());
1463 
1464     // turn off bypass, run the tests again
1465     cp.enableBypass(true);
1466     cp.resetStates();
1467 
1468     admin.modifyNamespace(NamespaceDescriptor.create(testNamespace).build());
1469     assertTrue("Test namespace should not have been modified",
1470         cp.preModifyNamespaceCalledOnly());
1471 
1472     assertNotNull(admin.getNamespaceDescriptor(testNamespace));
1473     assertTrue("Test namespace descriptor should have been called",
1474         cp.wasGetNamespaceDescriptorCalled());
1475 
1476     admin.deleteNamespace(testNamespace);
1477     assertTrue("Test namespace should not have been deleted", cp.preDeleteNamespaceCalledOnly());
1478 
1479     assertNotNull(admin.getNamespaceDescriptor(testNamespace));
1480     assertTrue("Test namespace descriptor should have been called",
1481         cp.wasGetNamespaceDescriptorCalled());
1482 
1483     cp.enableBypass(false);
1484     cp.resetStates();
1485 
1486     // delete table
1487     admin.modifyNamespace(NamespaceDescriptor.create(testNamespace).build());
1488     assertTrue("Test namespace should have been modified", cp.wasModifyNamespaceCalled());
1489 
1490     admin.deleteNamespace(testNamespace);
1491     assertTrue("Test namespace should have been deleted", cp.wasDeleteNamespaceCalled());
1492 
1493     cp.enableBypass(true);
1494     cp.resetStates();
1495 
1496     admin.createNamespace(NamespaceDescriptor.create(testNamespace).build());
1497     assertTrue("Test namespace should not be created", cp.preCreateNamespaceCalledOnly());
1498 
1499     // turn on bypass, run the test
1500     cp.enableBypass(true);
1501     cp.resetStates();
1502 
1503     admin.listNamespaceDescriptors();
1504     assertTrue("post listNamespace should not have been called",
1505                cp.preListNamespaceDescriptorsCalledOnly());
1506 
1507     // turn off bypass, run the tests again
1508     cp.enableBypass(false);
1509     cp.resetStates();
1510 
1511     admin.listNamespaceDescriptors();
1512     assertTrue("post listNamespace should have been called",
1513                cp.wasListNamespaceDescriptorsCalled());
1514   }
1515 
1516   private void modifyTableSync(Admin admin, TableName tableName, HTableDescriptor htd)
1517       throws IOException {
1518     admin.modifyTable(tableName, htd);
1519     //wait until modify table finishes
1520     for (int t = 0; t < 100; t++) { //10 sec timeout
1521       HTableDescriptor td = admin.getTableDescriptor(htd.getTableName());
1522       if (td.equals(htd)) {
1523         break;
1524       }
1525       Threads.sleep(100);
1526     }
1527   }
1528 
1529   @Test (timeout=180000)
1530   public void testRegionTransitionOperations() throws Exception {
1531     final TableName tableName = TableName.valueOf(name.getMethodName());
1532     MiniHBaseCluster cluster = UTIL.getHBaseCluster();
1533 
1534     HMaster master = cluster.getMaster();
1535     MasterCoprocessorHost host = master.getMasterCoprocessorHost();
1536     CPMasterObserver cp = (CPMasterObserver)host.findCoprocessor(
1537         CPMasterObserver.class.getName());
1538     cp.enableBypass(false);
1539     cp.resetStates();
1540 
1541     HTable table = UTIL.createMultiRegionTable(tableName, TEST_FAMILY);
1542 
1543     try {
1544       UTIL.waitUntilAllRegionsAssigned(tableName);
1545 
1546       NavigableMap<HRegionInfo, ServerName> regions = table.getRegionLocations();
1547       Map.Entry<HRegionInfo, ServerName> firstGoodPair = null;
1548       for (Map.Entry<HRegionInfo, ServerName> e: regions.entrySet()) {
1549         if (e.getValue() != null) {
1550           firstGoodPair = e;
1551           break;
1552         }
1553       }
1554       assertNotNull("Found a non-null entry", firstGoodPair);
1555       LOG.info("Found " + firstGoodPair.toString());
1556       // Try to force a move
1557       Collection<ServerName> servers = master.getClusterStatus().getServers();
1558       String destName = null;
1559       String serverNameForFirstRegion = firstGoodPair.getValue().toString();
1560       LOG.info("serverNameForFirstRegion=" + serverNameForFirstRegion);
1561       ServerName masterServerName = master.getServerName();
1562       boolean found = false;
1563       // Find server that is NOT carrying the first region
1564       for (ServerName info : servers) {
1565         LOG.info("ServerName=" + info);
1566         if (!serverNameForFirstRegion.equals(info.getServerName())
1567             && !masterServerName.equals(info)) {
1568           destName = info.toString();
1569           found = true;
1570           break;
1571         }
1572       }
1573       assertTrue("Found server", found);
1574       LOG.info("Found " + destName);
1575       master.getMasterRpcServices().moveRegion(null, RequestConverter.buildMoveRegionRequest(
1576           firstGoodPair.getKey().getEncodedNameAsBytes(),Bytes.toBytes(destName)));
1577       assertTrue("Coprocessor should have been called on region move",
1578         cp.wasMoveCalled());
1579 
1580       // make sure balancer is on
1581       master.balanceSwitch(true);
1582       assertTrue("Coprocessor should have been called on balance switch",
1583           cp.wasBalanceSwitchCalled());
1584 
1585       // turn balancer off
1586       master.balanceSwitch(false);
1587 
1588       // wait for assignments to finish, if any
1589       AssignmentManager mgr = master.getAssignmentManager();
1590       Collection<RegionState> transRegions =
1591         mgr.getRegionStates().getRegionsInTransition().values();
1592       for (RegionState state : transRegions) {
1593         mgr.getRegionStates().waitOnRegionToClearRegionsInTransition(state.getRegion());
1594       }
1595 
1596       // move half the open regions from RS 0 to RS 1
1597       HRegionServer rs = cluster.getRegionServer(0);
1598       byte[] destRS = Bytes.toBytes(cluster.getRegionServer(1).getServerName().toString());
1599       //Make sure no regions are in transition now
1600       waitForRITtoBeZero(master);
1601       List<HRegionInfo> openRegions = ProtobufUtil.getOnlineRegions(rs.getRSRpcServices());
1602       int moveCnt = openRegions.size()/2;
1603       for (int i=0; i<moveCnt; i++) {
1604         HRegionInfo info = openRegions.get(i);
1605         if (!info.isMetaTable()) {
1606           master.getMasterRpcServices().moveRegion(null, RequestConverter.buildMoveRegionRequest(
1607               openRegions.get(i).getEncodedNameAsBytes(), destRS));
1608         }
1609       }
1610       //Make sure no regions are in transition now
1611       waitForRITtoBeZero(master);
1612       // now trigger a balance
1613       master.balanceSwitch(true);
1614       boolean balanceRun = master.balance();
1615       assertTrue("Coprocessor should be called on region rebalancing",
1616           cp.wasBalanceCalled());
1617     } finally {
1618       Admin admin = UTIL.getHBaseAdmin();
1619       admin.disableTable(tableName);
1620       deleteTable(admin, tableName);
1621     }
1622   }
1623 
1624   private void waitForRITtoBeZero(HMaster master) throws Exception {
1625     // wait for assignments to finish
1626     AssignmentManager mgr = master.getAssignmentManager();
1627     Collection<RegionState> transRegions =
1628       mgr.getRegionStates().getRegionsInTransition().values();
1629     for (RegionState state : transRegions) {
1630       mgr.getRegionStates().waitOnRegionToClearRegionsInTransition(state.getRegion());
1631     }
1632   }
1633 
1634   @Test (timeout=180000)
1635   public void testTableDescriptorsEnumeration() throws Exception {
1636     MiniHBaseCluster cluster = UTIL.getHBaseCluster();
1637 
1638     HMaster master = cluster.getMaster();
1639     MasterCoprocessorHost host = master.getMasterCoprocessorHost();
1640     CPMasterObserver cp = (CPMasterObserver)host.findCoprocessor(
1641         CPMasterObserver.class.getName());
1642     cp.resetStates();
1643 
1644     GetTableDescriptorsRequest req =
1645         RequestConverter.buildGetTableDescriptorsRequest((List<TableName>)null);
1646     master.getMasterRpcServices().getTableDescriptors(null, req);
1647 
1648     assertTrue("Coprocessor should be called on table descriptors request",
1649       cp.wasGetTableDescriptorsCalled());
1650   }
1651 
1652   @Test (timeout=180000)
1653   public void testTableNamesEnumeration() throws Exception {
1654     MiniHBaseCluster cluster = UTIL.getHBaseCluster();
1655 
1656     HMaster master = cluster.getMaster();
1657     MasterCoprocessorHost host = master.getMasterCoprocessorHost();
1658     CPMasterObserver cp = (CPMasterObserver)host.findCoprocessor(
1659         CPMasterObserver.class.getName());
1660     cp.resetStates();
1661 
1662     master.getMasterRpcServices().getTableNames(null,
1663         GetTableNamesRequest.newBuilder().build());
1664     assertTrue("Coprocessor should be called on table names request",
1665       cp.wasGetTableNamesCalled());
1666   }
1667 
1668   private void deleteTable(Admin admin, TableName tableName) throws Exception {
1669     // NOTE: We need a latch because admin is not sync,
1670     // so the postOp coprocessor method may be called after the admin operation returned.
1671     tableDeletionLatch = new CountDownLatch(1);
1672     admin.deleteTable(tableName);
1673     tableDeletionLatch.await();
1674     tableDeletionLatch = new CountDownLatch(1);
1675   }
1676 }