001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.coprocessor; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertTrue; 023import static org.junit.Assert.fail; 024 025import java.io.IOException; 026import java.util.Optional; 027import org.apache.hadoop.conf.Configuration; 028import org.apache.hadoop.hbase.Abortable; 029import org.apache.hadoop.hbase.CoprocessorEnvironment; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.HBaseTestingUtility; 032import org.apache.hadoop.hbase.HColumnDescriptor; 033import org.apache.hadoop.hbase.HConstants; 034import org.apache.hadoop.hbase.HTableDescriptor; 035import org.apache.hadoop.hbase.MiniHBaseCluster; 036import org.apache.hadoop.hbase.TableName; 037import org.apache.hadoop.hbase.client.Admin; 038import org.apache.hadoop.hbase.client.RegionInfo; 039import org.apache.hadoop.hbase.client.TableDescriptor; 040import org.apache.hadoop.hbase.master.HMaster; 041import org.apache.hadoop.hbase.master.MasterCoprocessorHost; 042import org.apache.hadoop.hbase.testclassification.CoprocessorTests; 043import org.apache.hadoop.hbase.testclassification.MediumTests; 044import org.apache.hadoop.hbase.util.Bytes; 045import org.apache.hadoop.hbase.zookeeper.ZKNodeTracker; 046import org.apache.hadoop.hbase.zookeeper.ZKWatcher; 047import org.junit.AfterClass; 048import org.junit.BeforeClass; 049import org.junit.ClassRule; 050import org.junit.Test; 051import org.junit.experimental.categories.Category; 052 053/** 054 * Tests unhandled exceptions thrown by coprocessors running on master. 055 * Expected result is that the master will abort with an informative 056 * error message describing the set of its loaded coprocessors for crash diagnosis. 057 * (HBASE-4014). 058 */ 059@Category({CoprocessorTests.class, MediumTests.class}) 060public class TestMasterCoprocessorExceptionWithAbort { 061 062 @ClassRule 063 public static final HBaseClassTestRule CLASS_RULE = 064 HBaseClassTestRule.forClass(TestMasterCoprocessorExceptionWithAbort.class); 065 066 public static class MasterTracker extends ZKNodeTracker { 067 public boolean masterZKNodeWasDeleted = false; 068 069 public MasterTracker(ZKWatcher zkw, String masterNode, Abortable abortable) { 070 super(zkw, masterNode, abortable); 071 } 072 073 @Override 074 public synchronized void nodeDeleted(String path) { 075 if (path.equals("/hbase/master")) { 076 masterZKNodeWasDeleted = true; 077 } 078 } 079 } 080 081 public static class CreateTableThread extends Thread { 082 HBaseTestingUtility UTIL; 083 public CreateTableThread(HBaseTestingUtility UTIL) { 084 this.UTIL = UTIL; 085 } 086 087 @Override 088 public void run() { 089 // create a table : master coprocessor will throw an exception and not 090 // catch it. 091 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(TEST_TABLE)); 092 htd.addFamily(new HColumnDescriptor(TEST_FAMILY)); 093 try { 094 Admin admin = UTIL.getAdmin(); 095 admin.createTable(htd); 096 fail("BuggyMasterObserver failed to throw an exception."); 097 } catch (IOException e) { 098 assertEquals("HBaseAdmin threw an interrupted IOException as expected.", 099 "java.io.InterruptedIOException", e.getClass().getName()); 100 } 101 } 102 } 103 104 public static class BuggyMasterObserver implements MasterCoprocessor, MasterObserver { 105 private boolean preCreateTableCalled; 106 private boolean postCreateTableCalled; 107 private boolean startCalled; 108 private boolean postStartMasterCalled; 109 110 @Override 111 public Optional<MasterObserver> getMasterObserver() { 112 return Optional.of(this); 113 } 114 115 @Override 116 public void postCreateTable(ObserverContext<MasterCoprocessorEnvironment> env, 117 TableDescriptor desc, RegionInfo[] regions) throws IOException { 118 // cause a NullPointerException and don't catch it: this will cause the 119 // master to abort(). 120 Integer i; 121 i = null; 122 i = i++; 123 } 124 125 public boolean wasCreateTableCalled() { 126 return preCreateTableCalled && postCreateTableCalled; 127 } 128 129 @Override 130 public void postStartMaster(ObserverContext<MasterCoprocessorEnvironment> ctx) 131 throws IOException { 132 postStartMasterCalled = true; 133 } 134 135 public boolean wasStartMasterCalled() { 136 return postStartMasterCalled; 137 } 138 139 @Override 140 public void start(CoprocessorEnvironment env) throws IOException { 141 startCalled = true; 142 } 143 144 public boolean wasStarted() { 145 return startCalled; 146 } 147 } 148 149 private static HBaseTestingUtility UTIL = new HBaseTestingUtility(); 150 private static byte[] TEST_TABLE = Bytes.toBytes("observed_table"); 151 private static byte[] TEST_FAMILY = Bytes.toBytes("fam1"); 152 153 @BeforeClass 154 public static void setupBeforeClass() throws Exception { 155 Configuration conf = UTIL.getConfiguration(); 156 conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY, 157 BuggyMasterObserver.class.getName()); 158 conf.setBoolean(CoprocessorHost.ABORT_ON_ERROR_KEY, true); 159 conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2); // Fail fast 160 UTIL.startMiniCluster(); 161 } 162 163 @AfterClass 164 public static void teardownAfterClass() throws Exception { 165 UTIL.shutdownMiniCluster(); 166 } 167 168 @Test 169 public void testExceptionFromCoprocessorWhenCreatingTable() 170 throws IOException { 171 MiniHBaseCluster cluster = UTIL.getHBaseCluster(); 172 173 HMaster master = cluster.getMaster(); 174 MasterCoprocessorHost host = master.getMasterCoprocessorHost(); 175 BuggyMasterObserver cp = host.findCoprocessor(BuggyMasterObserver.class); 176 assertFalse("No table created yet", cp.wasCreateTableCalled()); 177 178 // set a watch on the zookeeper /hbase/master node. If the master dies, 179 // the node will be deleted. 180 ZKWatcher zkw = new ZKWatcher(UTIL.getConfiguration(), 181 "unittest", new Abortable() { 182 @Override 183 public void abort(String why, Throwable e) { 184 throw new RuntimeException("Fatal ZK error: " + why, e); 185 } 186 @Override 187 public boolean isAborted() { 188 return false; 189 } 190 }); 191 192 MasterTracker masterTracker = new MasterTracker(zkw,"/hbase/master", 193 new Abortable() { 194 @Override 195 public void abort(String why, Throwable e) { 196 throw new RuntimeException("Fatal ZK master tracker error, why=", e); 197 } 198 @Override 199 public boolean isAborted() { 200 return false; 201 } 202 }); 203 204 masterTracker.start(); 205 zkw.registerListener(masterTracker); 206 207 // Test (part of the) output that should have be printed by master when it aborts: 208 // (namely the part that shows the set of loaded coprocessors). 209 // In this test, there is only a single coprocessor (BuggyMasterObserver). 210 assertTrue(HMaster.getLoadedCoprocessors(). 211 contains(TestMasterCoprocessorExceptionWithAbort.BuggyMasterObserver.class.getName())); 212 213 CreateTableThread createTableThread = new CreateTableThread(UTIL); 214 215 // Attempting to create a table (using createTableThread above) triggers an NPE in BuggyMasterObserver. 216 // Master will then abort and the /hbase/master zk node will be deleted. 217 createTableThread.start(); 218 219 // Wait up to 30 seconds for master's /hbase/master zk node to go away after master aborts. 220 for (int i = 0; i < 30; i++) { 221 if (masterTracker.masterZKNodeWasDeleted == true) { 222 break; 223 } 224 try { 225 Thread.sleep(1000); 226 } catch (InterruptedException e) { 227 fail("InterruptedException while waiting for master zk node to " 228 + "be deleted."); 229 } 230 } 231 232 assertTrue("Master aborted on coprocessor exception, as expected.", 233 masterTracker.masterZKNodeWasDeleted); 234 235 createTableThread.interrupt(); 236 try { 237 createTableThread.join(1000); 238 } catch (InterruptedException e) { 239 assertTrue("Ignoring InterruptedException while waiting for " + 240 " createTableThread.join().", true); 241 } 242 } 243 244} 245