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.regionserver;
019
020import java.io.IOException;
021import java.util.Optional;
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.HConstants;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.client.Delete;
027import org.apache.hadoop.hbase.client.Get;
028import org.apache.hadoop.hbase.client.Increment;
029import org.apache.hadoop.hbase.client.Result;
030import org.apache.hadoop.hbase.client.Table;
031import org.apache.hadoop.hbase.client.TableDescriptor;
032import org.apache.hadoop.hbase.coprocessor.ObserverContext;
033import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
034import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
035import org.apache.hadoop.hbase.coprocessor.RegionObserver;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.apache.hadoop.hbase.util.Threads;
039import org.junit.AfterClass;
040import org.junit.Assert;
041import org.junit.BeforeClass;
042import org.junit.ClassRule;
043import org.junit.Rule;
044import org.junit.Test;
045import org.junit.experimental.categories.Category;
046import org.junit.rules.TestName;
047
048@Category({ MediumTests.class })
049public class TestSettingTimeoutOnBlockingPoint {
050
051  @ClassRule
052  public static final HBaseClassTestRule CLASS_RULE =
053    HBaseClassTestRule.forClass(TestSettingTimeoutOnBlockingPoint.class);
054
055  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
056  private static final byte[] FAM = Bytes.toBytes("f");
057  private static final byte[] ROW1 = Bytes.toBytes("row1");
058  private static final byte[] ROW2 = Bytes.toBytes("row2");
059
060  @Rule
061  public TestName testName = new TestName();
062
063  @BeforeClass
064  public static void setUpBeforeClass() throws Exception {
065    TEST_UTIL.getConfiguration().setBoolean(HConstants.STATUS_PUBLISHED, true);
066    TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 1);
067    // simulate queue blocking
068    TEST_UTIL.getConfiguration().setInt(HConstants.REGION_SERVER_HANDLER_COUNT, 2);
069    TEST_UTIL.startMiniCluster(2);
070  }
071
072  @AfterClass
073  public static void setUpAfterClass() throws Exception {
074    TEST_UTIL.shutdownMiniCluster();
075  }
076
077  public static class SleepCoprocessor implements RegionCoprocessor, RegionObserver {
078    public static final int SLEEP_TIME = 10000;
079
080    @Override
081    public Optional<RegionObserver> getRegionObserver() {
082      return Optional.of(this);
083    }
084
085    @Override
086    public Result preIncrementAfterRowLock(final ObserverContext<RegionCoprocessorEnvironment> e,
087      final Increment increment) throws IOException {
088      Threads.sleep(SLEEP_TIME);
089      return null;
090    }
091  }
092
093  @Test
094  public void testRowLock() throws IOException {
095    TableDescriptor hdt = TEST_UTIL.createModifyableTableDescriptor(testName.getMethodName())
096      .setCoprocessor(SleepCoprocessor.class.getName()).build();
097    TEST_UTIL.createTable(hdt, new byte[][] { FAM }, TEST_UTIL.getConfiguration());
098    TableName tableName = hdt.getTableName();
099    Thread incrementThread = new Thread(() -> {
100      try {
101        try (Table table = TEST_UTIL.getConnection().getTable(tableName)) {
102          table.incrementColumnValue(ROW1, FAM, FAM, 1);
103        }
104      } catch (IOException e) {
105        Assert.fail(e.getMessage());
106      }
107    });
108    Thread getThread = new Thread(() -> {
109      try (Table table =
110        TEST_UTIL.getConnection().getTableBuilder(tableName, null).setRpcTimeout(1000).build()) {
111        Delete delete = new Delete(ROW1);
112        table.delete(delete);
113      } catch (IOException e) {
114        Assert.fail(e.getMessage());
115      }
116    });
117
118    incrementThread.start();
119    Threads.sleep(1000);
120    getThread.start();
121    Threads.sleep(2000);
122    try (Table table =
123      TEST_UTIL.getConnection().getTableBuilder(tableName, null).setRpcTimeout(1000).build()) {
124      // We have only two handlers. The first thread will get a write lock for row1 and occupy
125      // the first handler. The second thread need a read lock for row1, it should quit after 1000
126      // ms and give back the handler because it can not get the lock in time.
127      // So we can get the value using the second handler.
128      table.get(new Get(ROW2)); // Will throw exception if the timeout checking is failed
129    } finally {
130      incrementThread.interrupt();
131      getThread.interrupt();
132    }
133  }
134}