1   /*
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.coprocessor;
22  
23  import java.io.IOException;
24  import java.io.InterruptedIOException;
25  
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.*;
28  import org.apache.hadoop.hbase.client.HBaseAdmin;
29  import org.apache.hadoop.hbase.master.HMaster;
30  import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
31  import org.apache.hadoop.hbase.util.Bytes;
32  import org.apache.hadoop.hbase.zookeeper.ZooKeeperNodeTracker;
33  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
34  import org.junit.AfterClass;
35  import org.junit.BeforeClass;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  
39  import static org.junit.Assert.*;
40  
41  /**
42   * Tests unhandled exceptions thrown by coprocessors running on master.
43   * Expected result is that the master will abort with an informative
44   * error message describing the set of its loaded coprocessors for crash diagnosis.
45   * (HBASE-4014).
46   */
47  @Category(MediumTests.class)
48  public class TestMasterCoprocessorExceptionWithAbort {
49  
50    public static class MasterTracker extends ZooKeeperNodeTracker {
51      public boolean masterZKNodeWasDeleted = false;
52  
53      public MasterTracker(ZooKeeperWatcher zkw, String masterNode, Abortable abortable) {
54        super(zkw, masterNode, abortable);
55      }
56  
57      @Override
58      public synchronized void nodeDeleted(String path) {
59        if (path.equals("/hbase/master")) {
60          masterZKNodeWasDeleted = true;
61        }
62      }
63    }
64  
65    public static class CreateTableThread extends Thread {
66      HBaseTestingUtility UTIL;
67      public CreateTableThread(HBaseTestingUtility UTIL) {
68        this.UTIL = UTIL;
69      }
70  
71      @Override
72      public void run() {
73        // create a table : master coprocessor will throw an exception and not
74        // catch it.
75        HTableDescriptor htd = new HTableDescriptor(TEST_TABLE);
76        htd.addFamily(new HColumnDescriptor(TEST_FAMILY));
77        try {
78          HBaseAdmin admin = UTIL.getHBaseAdmin();
79          admin.createTable(htd);
80          fail("BuggyMasterObserver failed to throw an exception.");
81        } catch (IOException e) {
82          assertEquals("HBaseAdmin threw an interrupted IOException as expected.",
83              e.getClass().getName(), "java.io.InterruptedIOException");
84        }
85     }
86    }
87  
88    public static class BuggyMasterObserver extends BaseMasterObserver {
89      private boolean preCreateTableCalled;
90      private boolean postCreateTableCalled;
91      private boolean startCalled;
92      private boolean postStartMasterCalled;
93  
94      @Override
95      public void postCreateTable(ObserverContext<MasterCoprocessorEnvironment> env,
96          HTableDescriptor desc, HRegionInfo[] regions) throws IOException {
97        // cause a NullPointerException and don't catch it: this will cause the
98        // master to abort().
99        Integer i;
100       i = null;
101       i = i++;
102     }
103 
104     public boolean wasCreateTableCalled() {
105       return preCreateTableCalled && postCreateTableCalled;
106     }
107 
108     @Override
109     public void postStartMaster(ObserverContext<MasterCoprocessorEnvironment> ctx)
110         throws IOException {
111       postStartMasterCalled = true;
112     }
113 
114     public boolean wasStartMasterCalled() {
115       return postStartMasterCalled;
116     }
117 
118     @Override
119     public void start(CoprocessorEnvironment env) throws IOException {
120       startCalled = true;
121     }
122 
123     public boolean wasStarted() {
124       return startCalled;
125     }
126   }
127 
128   private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
129   private static byte[] TEST_TABLE = Bytes.toBytes("observed_table");
130   private static byte[] TEST_FAMILY = Bytes.toBytes("fam1");
131 
132   @BeforeClass
133   public static void setupBeforeClass() throws Exception {
134     Configuration conf = UTIL.getConfiguration();
135     conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
136         BuggyMasterObserver.class.getName());
137     conf.set("hbase.coprocessor.abortonerror", "true");
138     UTIL.startMiniCluster();
139   }
140 
141   @AfterClass
142   public static void teardownAfterClass() throws Exception {
143     UTIL.shutdownMiniCluster();
144   }
145 
146   @Test(timeout=30000)
147   public void testExceptionFromCoprocessorWhenCreatingTable()
148       throws IOException {
149     MiniHBaseCluster cluster = UTIL.getHBaseCluster();
150 
151     HMaster master = cluster.getMaster();
152     MasterCoprocessorHost host = master.getCoprocessorHost();
153     BuggyMasterObserver cp = (BuggyMasterObserver)host.findCoprocessor(
154         BuggyMasterObserver.class.getName());
155     assertFalse("No table created yet", cp.wasCreateTableCalled());
156 
157     // set a watch on the zookeeper /hbase/master node. If the master dies,
158     // the node will be deleted.
159     ZooKeeperWatcher zkw = new ZooKeeperWatcher(UTIL.getConfiguration(),
160       "unittest", new Abortable() {
161       @Override
162       public void abort(String why, Throwable e) {
163         throw new RuntimeException("Fatal ZK error: " + why, e);
164       }
165       @Override
166       public boolean isAborted() {
167         return false;
168       }
169     });
170 
171     MasterTracker masterTracker = new MasterTracker(zkw,"/hbase/master",
172         new Abortable() {
173           @Override
174           public void abort(String why, Throwable e) {
175             throw new RuntimeException("Fatal ZK master tracker error, why=", e);
176           }
177           @Override
178           public boolean isAborted() {
179             return false;
180           }
181         });
182 
183     masterTracker.start();
184     zkw.registerListener(masterTracker);
185 
186     // Test (part of the) output that should have be printed by master when it aborts:
187     // (namely the part that shows the set of loaded coprocessors).
188     // In this test, there is only a single coprocessor (BuggyMasterObserver).
189     assertTrue(master.getLoadedCoprocessors().
190       equals("[" +
191           TestMasterCoprocessorExceptionWithAbort.BuggyMasterObserver.class.getName() +
192           "]"));
193 
194     CreateTableThread createTableThread = new CreateTableThread(UTIL);
195 
196     // Attempting to create a table (using createTableThread above) triggers an NPE in BuggyMasterObserver.
197     // Master will then abort and the /hbase/master zk node will be deleted.
198     createTableThread.start();
199 
200     // Wait up to 30 seconds for master's /hbase/master zk node to go away after master aborts.
201     for (int i = 0; i < 30; i++) {
202       if (masterTracker.masterZKNodeWasDeleted == true) {
203         break;
204       }
205       try {
206         Thread.sleep(1000);
207       } catch (InterruptedException e) {
208         fail("InterruptedException while waiting for master zk node to "
209             + "be deleted.");
210       }
211     }
212 
213     assertTrue("Master aborted on coprocessor exception, as expected.",
214         masterTracker.masterZKNodeWasDeleted);
215 
216     createTableThread.interrupt();
217     try {
218       createTableThread.join(1000);
219     } catch (InterruptedException e) {
220       assertTrue("Ignoring InterruptedException while waiting for " +
221           " createTableThread.join().", true);
222     }
223   }
224 
225 
226   @org.junit.Rule
227   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
228     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
229 }
230