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.assertFalse;
021import static org.junit.Assert.assertTrue;
022import static org.junit.Assert.fail;
023
024import java.io.IOException;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.Durability;
030import org.apache.hadoop.hbase.client.Put;
031import org.apache.hadoop.hbase.client.Table;
032import org.apache.hadoop.hbase.regionserver.HRegionServer;
033import org.apache.hadoop.hbase.testclassification.CoprocessorTests;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.apache.hadoop.hbase.wal.WALEdit;
037import org.junit.AfterClass;
038import org.junit.BeforeClass;
039import org.junit.ClassRule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042
043/**
044 * Tests unhandled exceptions thrown by coprocessors running on regionserver.
045 * Expected result is that the region server will remove the buggy coprocessor from
046 * its set of coprocessors and throw a org.apache.hadoop.hbase.exceptions.DoNotRetryIOException
047 * back to the client.
048 * (HBASE-4014).
049 */
050@Category({CoprocessorTests.class, MediumTests.class})
051public class TestRegionServerCoprocessorExceptionWithRemove {
052
053  @ClassRule
054  public static final HBaseClassTestRule CLASS_RULE =
055      HBaseClassTestRule.forClass(TestRegionServerCoprocessorExceptionWithRemove.class);
056
057  public static class BuggyRegionObserver extends SimpleRegionObserver {
058    @SuppressWarnings("null")
059    @Override
060    public void prePut(final ObserverContext<RegionCoprocessorEnvironment> c,
061                       final Put put, final WALEdit edit,
062                       final Durability durability) {
063      String tableName =
064          c.getEnvironment().getRegion().getRegionInfo().getTable().getNameAsString();
065      if (tableName.equals("observed_table")) {
066        // Trigger a NPE to fail the coprocessor
067        Integer i = null;
068        i = i + 1;
069      }
070    }
071  }
072
073  private static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
074
075  @BeforeClass
076  public static void setupBeforeClass() throws Exception {
077    // set configure to indicate which cp should be loaded
078    Configuration conf = TEST_UTIL.getConfiguration();
079    conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
080        BuggyRegionObserver.class.getName());
081    TEST_UTIL.getConfiguration().setBoolean(CoprocessorHost.ABORT_ON_ERROR_KEY, false);
082    TEST_UTIL.startMiniCluster();
083  }
084
085  @AfterClass
086  public static void teardownAfterClass() throws Exception {
087    TEST_UTIL.shutdownMiniCluster();
088  }
089
090  @Test
091  public void testExceptionFromCoprocessorDuringPut()
092      throws IOException, InterruptedException {
093    // Set watches on the zookeeper nodes for all of the regionservers in the
094    // cluster. When we try to write to TEST_TABLE, the buggy coprocessor will
095    // cause a NullPointerException, which will cause the regionserver (which
096    // hosts the region we attempted to write to) to abort. In turn, this will
097    // cause the nodeDeleted() method of the DeadRegionServer tracker to
098    // execute, which will set the rsZKNodeDeleted flag to true, which will
099    // pass this test.
100
101    TableName TEST_TABLE = TableName.valueOf("observed_table");
102    byte[] TEST_FAMILY = Bytes.toBytes("aaa");
103
104    Table table = TEST_UTIL.createMultiRegionTable(TEST_TABLE, TEST_FAMILY);
105    TEST_UTIL.waitUntilAllRegionsAssigned(TEST_TABLE);
106    // Note which regionServer that should survive the buggy coprocessor's
107    // prePut().
108    HRegionServer regionServer =
109        TEST_UTIL.getRSForFirstRegionInTable(TEST_TABLE);
110
111    boolean threwIOE = false;
112    try {
113      final byte[] ROW = Bytes.toBytes("aaa");
114      Put put = new Put(ROW);
115      put.addColumn(TEST_FAMILY, ROW, ROW);
116      table.put(put);
117      // We may need two puts to reliably get an exception
118      table.put(put);
119    } catch (IOException e) {
120      threwIOE = true;
121    } finally {
122      assertTrue("The regionserver should have thrown an exception", threwIOE);
123    }
124
125    // Wait 10 seconds for the regionserver to abort: expected result is that
126    // it will survive and not abort.
127    for (int i = 0; i < 10; i++) {
128      assertFalse(regionServer.isAborted());
129      try {
130        Thread.sleep(1000);
131      } catch (InterruptedException e) {
132        fail("InterruptedException while waiting for regionserver " +
133            "zk node to be deleted.");
134      }
135    }
136    table.close();
137  }
138
139}
140