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