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.fail;
021
022import java.io.IOException;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.CoprocessorEnvironment;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtility;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.MiniHBaseCluster;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.Waiter.Predicate;
031import org.apache.hadoop.hbase.client.Durability;
032import org.apache.hadoop.hbase.client.Put;
033import org.apache.hadoop.hbase.client.Table;
034import org.apache.hadoop.hbase.regionserver.HRegionServer;
035import org.apache.hadoop.hbase.testclassification.CoprocessorTests;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.apache.hadoop.hbase.wal.WALEdit;
039import org.junit.Assert;
040import org.junit.ClassRule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043import org.slf4j.Logger;
044import org.slf4j.LoggerFactory;
045
046/**
047 * Tests unhandled exceptions thrown by coprocessors running on a regionserver.. Expected result is
048 * that the regionserver will abort with an informative error message describing the set of its
049 * loaded coprocessors for crash diagnosis. (HBASE-4014).
050 */
051@Category({ CoprocessorTests.class, MediumTests.class })
052public class TestRegionServerCoprocessorExceptionWithAbort {
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056    HBaseClassTestRule.forClass(TestRegionServerCoprocessorExceptionWithAbort.class);
057
058  private static final Logger LOG =
059    LoggerFactory.getLogger(TestRegionServerCoprocessorExceptionWithAbort.class);
060  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
061  private static final TableName TABLE_NAME = TableName.valueOf("observed_table");
062
063  @Test
064  public void testExceptionDuringInitialization() throws Exception {
065    Configuration conf = TEST_UTIL.getConfiguration();
066    conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2); // Let's fail fast.
067    conf.setBoolean(CoprocessorHost.ABORT_ON_ERROR_KEY, true);
068    conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, "");
069    TEST_UTIL.startMiniCluster(2);
070    try {
071      MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
072      // Trigger one regionserver to fail as if it came up with a coprocessor
073      // that fails during initialization
074      final HRegionServer regionServer = cluster.getRegionServer(0);
075      conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
076        FailedInitializationObserver.class.getName());
077      regionServer.getRegionServerCoprocessorHost().loadSystemCoprocessors(conf,
078        CoprocessorHost.REGION_COPROCESSOR_CONF_KEY);
079      TEST_UTIL.waitFor(10000, 1000, new Predicate<Exception>() {
080        @Override
081        public boolean evaluate() throws Exception {
082          return regionServer.isAborted();
083        }
084      });
085    } finally {
086      TEST_UTIL.shutdownMiniCluster();
087    }
088  }
089
090  @Test
091  public void testExceptionFromCoprocessorDuringPut() throws Exception {
092    // set configure to indicate which cp should be loaded
093    Configuration conf = TEST_UTIL.getConfiguration();
094    conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2); // Let's fail fast.
095    conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, BuggyRegionObserver.class.getName());
096    conf.setBoolean(CoprocessorHost.ABORT_ON_ERROR_KEY, true);
097    TEST_UTIL.startMiniCluster(2);
098    try {
099      // When we try to write to TEST_TABLE, the buggy coprocessor will
100      // cause a NullPointerException, which will cause the regionserver (which
101      // hosts the region we attempted to write to) to abort.
102      final byte[] TEST_FAMILY = Bytes.toBytes("aaa");
103
104      Table table = TEST_UTIL.createMultiRegionTable(TABLE_NAME, TEST_FAMILY);
105      TEST_UTIL.waitUntilAllRegionsAssigned(TABLE_NAME);
106
107      // Note which regionServer will abort (after put is attempted).
108      final HRegionServer regionServer = TEST_UTIL.getRSForFirstRegionInTable(TABLE_NAME);
109
110      try {
111        final byte[] ROW = Bytes.toBytes("aaa");
112        Put put = new Put(ROW);
113        put.addColumn(TEST_FAMILY, ROW, ROW);
114        table.put(put);
115      } catch (IOException e) {
116        // The region server is going to be aborted.
117        // We may get an exception if we retry,
118        // which is not guaranteed.
119      }
120
121      // Wait 10 seconds for the regionserver to abort: expected result is that
122      // it will abort.
123      boolean aborted = false;
124      for (int i = 0; i < 10; i++) {
125        aborted = regionServer.isAborted();
126        if (aborted) {
127          break;
128        }
129        try {
130          Thread.sleep(1000);
131        } catch (InterruptedException e) {
132          fail("InterruptedException while waiting for regionserver " + "zk node to be deleted.");
133        }
134      }
135      Assert.assertTrue("The region server should have aborted", aborted);
136      table.close();
137    } finally {
138      TEST_UTIL.shutdownMiniCluster();
139    }
140  }
141
142  public static class FailedInitializationObserver implements RegionServerCoprocessor {
143    @SuppressWarnings("null")
144    @Override
145    public void start(CoprocessorEnvironment e) throws IOException {
146      // Trigger a NPE to fail the coprocessor
147      Integer i = null;
148      i = i + 1;
149    }
150  }
151
152  public static class BuggyRegionObserver extends SimpleRegionObserver {
153    @SuppressWarnings("null")
154    @Override
155    public void prePut(final ObserverContext<RegionCoprocessorEnvironment> c, final Put put,
156      final WALEdit edit, final Durability durability) {
157      String tableName =
158        c.getEnvironment().getRegion().getRegionInfo().getTable().getNameAsString();
159      if (tableName.equals("observed_table")) {
160        // Trigger a NPE to fail the coprocessor
161        Integer i = null;
162        i = i + 1;
163      }
164    }
165  }
166}