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. Expected result is that the 055 * master will abort with an informative error message describing the set of its loaded coprocessors 056 * for crash diagnosis. (HBASE-4014). 057 */ 058@Category({ CoprocessorTests.class, MediumTests.class }) 059public class TestMasterCoprocessorExceptionWithAbort { 060 061 @ClassRule 062 public static final HBaseClassTestRule CLASS_RULE = 063 HBaseClassTestRule.forClass(TestMasterCoprocessorExceptionWithAbort.class); 064 065 public static class MasterTracker extends ZKNodeTracker { 066 public boolean masterZKNodeWasDeleted = false; 067 068 public MasterTracker(ZKWatcher zkw, String masterNode, Abortable abortable) { 069 super(zkw, masterNode, abortable); 070 } 071 072 @Override 073 public synchronized void nodeDeleted(String path) { 074 if (path.equals("/hbase/master")) { 075 masterZKNodeWasDeleted = true; 076 } 077 } 078 } 079 080 public static class CreateTableThread extends Thread { 081 HBaseTestingUtility UTIL; 082 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, BuggyMasterObserver.class.getName()); 157 conf.setBoolean(CoprocessorHost.ABORT_ON_ERROR_KEY, true); 158 conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2); // Fail fast 159 UTIL.startMiniCluster(); 160 } 161 162 @AfterClass 163 public static void teardownAfterClass() throws Exception { 164 UTIL.shutdownMiniCluster(); 165 } 166 167 @Test 168 public void testExceptionFromCoprocessorWhenCreatingTable() throws IOException { 169 MiniHBaseCluster cluster = UTIL.getHBaseCluster(); 170 171 HMaster master = cluster.getMaster(); 172 MasterCoprocessorHost host = master.getMasterCoprocessorHost(); 173 BuggyMasterObserver cp = host.findCoprocessor(BuggyMasterObserver.class); 174 assertFalse("No table created yet", cp.wasCreateTableCalled()); 175 176 // set a watch on the zookeeper /hbase/master node. If the master dies, 177 // the node will be deleted. 178 ZKWatcher zkw = new ZKWatcher(UTIL.getConfiguration(), "unittest", new Abortable() { 179 @Override 180 public void abort(String why, Throwable e) { 181 throw new RuntimeException("Fatal ZK error: " + why, e); 182 } 183 184 @Override 185 public boolean isAborted() { 186 return false; 187 } 188 }); 189 190 MasterTracker masterTracker = new MasterTracker(zkw, "/hbase/master", new Abortable() { 191 @Override 192 public void abort(String why, Throwable e) { 193 throw new RuntimeException("Fatal ZK master tracker error, why=", e); 194 } 195 196 @Override 197 public boolean isAborted() { 198 return false; 199 } 200 }); 201 202 masterTracker.start(); 203 zkw.registerListener(masterTracker); 204 205 // Test (part of the) output that should have be printed by master when it aborts: 206 // (namely the part that shows the set of loaded coprocessors). 207 // In this test, there is only a single coprocessor (BuggyMasterObserver). 208 assertTrue(HMaster.getLoadedCoprocessors() 209 .contains(TestMasterCoprocessorExceptionWithAbort.BuggyMasterObserver.class.getName())); 210 211 CreateTableThread createTableThread = new CreateTableThread(UTIL); 212 213 // Attempting to create a table (using createTableThread above) triggers an NPE in 214 // BuggyMasterObserver. 215 // Master will then abort and the /hbase/master zk node will be deleted. 216 createTableThread.start(); 217 218 // Wait up to 30 seconds for master's /hbase/master zk node to go away after master aborts. 219 for (int i = 0; i < 30; i++) { 220 if (masterTracker.masterZKNodeWasDeleted == true) { 221 break; 222 } 223 try { 224 Thread.sleep(1000); 225 } catch (InterruptedException e) { 226 fail("InterruptedException while waiting for master zk node to " + "be deleted."); 227 } 228 } 229 230 assertTrue("Master aborted on coprocessor exception, as expected.", 231 masterTracker.masterZKNodeWasDeleted); 232 233 createTableThread.interrupt(); 234 try { 235 createTableThread.join(1000); 236 } catch (InterruptedException e) { 237 assertTrue("Ignoring InterruptedException while waiting for " + " createTableThread.join().", 238 true); 239 } 240 } 241 242}