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.client;
019
020import static org.junit.Assert.assertEquals;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.MetaTableAccessor;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.regionserver.HRegionServer;
029import org.apache.hadoop.hbase.testclassification.ClientTests;
030import org.apache.hadoop.hbase.testclassification.LargeTests;
031import org.apache.hadoop.hbase.util.Bytes;
032import org.junit.AfterClass;
033import org.junit.Before;
034import org.junit.BeforeClass;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040
041/**
042 * Test various scanner timeout issues.
043 */
044@Category({ LargeTests.class, ClientTests.class })
045public class TestScannerTimeout {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049    HBaseClassTestRule.forClass(TestScannerTimeout.class);
050
051  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
052
053  private static final Logger LOG = LoggerFactory.getLogger(TestScannerTimeout.class);
054  private final static byte[] SOME_BYTES = Bytes.toBytes("f");
055  private final static TableName TABLE_NAME = TableName.valueOf("t");
056  private final static int NB_ROWS = 10;
057  // Be careful w/ what you set this timer to... it can get in the way of
058  // the mini cluster coming up -- the verification in particular.
059  private final static int THREAD_WAKE_FREQUENCY = 1000;
060  private final static int SCANNER_TIMEOUT = 15000;
061  private final static int SCANNER_CACHING = 5;
062
063  /**
064   * @throws java.lang.Exception
065   */
066  @BeforeClass
067  public static void setUpBeforeClass() throws Exception {
068    Configuration c = TEST_UTIL.getConfiguration();
069    c.setInt(HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD, SCANNER_TIMEOUT);
070    c.setInt(HConstants.THREAD_WAKE_FREQUENCY, THREAD_WAKE_FREQUENCY);
071    // We need more than one region server for this test
072    TEST_UTIL.startMiniCluster(2);
073    Table table = TEST_UTIL.createTable(TABLE_NAME, SOME_BYTES);
074    for (int i = 0; i < NB_ROWS; i++) {
075      Put put = new Put(Bytes.toBytes(i));
076      put.addColumn(SOME_BYTES, SOME_BYTES, SOME_BYTES);
077      table.put(put);
078    }
079    table.close();
080  }
081
082  /**
083   * @throws java.lang.Exception
084   */
085  @AfterClass
086  public static void tearDownAfterClass() throws Exception {
087    TEST_UTIL.shutdownMiniCluster();
088  }
089
090  /**
091   * @throws java.lang.Exception
092   */
093  @Before
094  public void setUp() throws Exception {
095    TEST_UTIL.ensureSomeNonStoppedRegionServersAvailable(2);
096  }
097
098  /**
099   * Test that scanner can continue even if the region server it was reading from failed. Before
100   * 2772, it reused the same scanner id.
101   */
102  @Test
103  public void test2772() throws Exception {
104    LOG.info("START************ test2772");
105    HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(TABLE_NAME);
106    Scan scan = new Scan();
107    // Set a very high timeout, we want to test what happens when a RS
108    // fails but the region is recovered before the lease times out.
109    // Since the RS is already created, this conf is client-side only for
110    // this new table
111    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
112    conf.setInt(HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD, SCANNER_TIMEOUT * 100);
113
114    Connection connection = ConnectionFactory.createConnection(conf);
115    Table higherScanTimeoutTable = connection.getTable(TABLE_NAME);
116    ResultScanner r = higherScanTimeoutTable.getScanner(scan);
117    // This takes way less than SCANNER_TIMEOUT*100
118    rs.abort("die!");
119    Result[] results = r.next(NB_ROWS);
120    assertEquals(NB_ROWS, results.length);
121    r.close();
122    higherScanTimeoutTable.close();
123    connection.close();
124    LOG.info("END ************ test2772");
125
126  }
127
128  /**
129   * Test that scanner won't miss any rows if the region server it was reading from failed. Before
130   * 3686, it would skip rows in the scan.
131   */
132  @Test
133  public void test3686a() throws Exception {
134    LOG.info("START ************ TEST3686A---1");
135    HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(TABLE_NAME);
136    LOG.info("START ************ TEST3686A---1111");
137
138    Scan scan = new Scan();
139    scan.setCaching(SCANNER_CACHING);
140    LOG.info("************ TEST3686A");
141    MetaTableAccessor.fullScanMetaAndPrint(TEST_UTIL.getAdmin().getConnection());
142    // Set a very high timeout, we want to test what happens when a RS
143    // fails but the region is recovered before the lease times out.
144    // Since the RS is already created, this conf is client-side only for
145    // this new table
146    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
147    conf.setInt(HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD, SCANNER_TIMEOUT * 100);
148    Connection connection = ConnectionFactory.createConnection(conf);
149    Table table = connection.getTable(TABLE_NAME);
150    LOG.info("START ************ TEST3686A---22");
151
152    ResultScanner r = table.getScanner(scan);
153    LOG.info("START ************ TEST3686A---33");
154
155    int count = 1;
156    r.next();
157    LOG.info("START ************ TEST3686A---44");
158
159    // Kill after one call to next(), which got 5 rows.
160    rs.abort("die!");
161    while (r.next() != null) {
162      count++;
163    }
164    assertEquals(NB_ROWS, count);
165    r.close();
166    table.close();
167    connection.close();
168    LOG.info("************ END TEST3686A");
169  }
170
171  /**
172   * Make sure that no rows are lost if the scanner timeout is longer on the client than the server,
173   * and the scan times out on the server but not the client.
174   */
175  @Test
176  public void test3686b() throws Exception {
177    LOG.info("START ************ test3686b");
178    HRegionServer rs = TEST_UTIL.getRSForFirstRegionInTable(TABLE_NAME);
179    Scan scan = new Scan();
180    scan.setCaching(SCANNER_CACHING);
181    // Set a very high timeout, we want to test what happens when a RS
182    // fails but the region is recovered before the lease times out.
183    // Since the RS is already created, this conf is client-side only for
184    // this new table
185    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
186    conf.setInt(HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD, SCANNER_TIMEOUT * 100);
187    Connection connection = ConnectionFactory.createConnection(conf);
188    Table higherScanTimeoutTable = connection.getTable(TABLE_NAME);
189    ResultScanner r = higherScanTimeoutTable.getScanner(scan);
190    int count = 1;
191    r.next();
192    // Sleep, allowing the scan to timeout on the server but not on the client.
193    Thread.sleep(SCANNER_TIMEOUT + 2000);
194    while (r.next() != null) {
195      count++;
196    }
197    assertEquals(NB_ROWS, count);
198    r.close();
199    higherScanTimeoutTable.close();
200    connection.close();
201    LOG.info("END ************ END test3686b");
202
203  }
204
205}