View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.client;
20  
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertNull;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.util.Arrays;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.hbase.CompatibilityFactory;
30  import org.apache.hadoop.hbase.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.HConstants;
32  import org.apache.hadoop.hbase.HTableDescriptor;
33  import org.apache.hadoop.hbase.TableName;
34  import org.apache.hadoop.hbase.ipc.MetricsHBaseServerSource;
35  import org.apache.hadoop.hbase.test.MetricsAssertHelper;
36  import org.apache.hadoop.hbase.testclassification.LargeTests;
37  import org.apache.hadoop.hbase.util.Bytes;
38  import org.junit.After;
39  import org.junit.AfterClass;
40  import org.junit.Before;
41  import org.junit.BeforeClass;
42  import org.junit.Test;
43  import org.junit.experimental.categories.Category;
44  
45  @Category(LargeTests.class)
46  public class TestLeaseRenewal {
47    public MetricsAssertHelper HELPER = CompatibilityFactory.getInstance(MetricsAssertHelper.class);
48  
49    final Log LOG = LogFactory.getLog(getClass());
50    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
51    private static byte[] FAMILY = Bytes.toBytes("testFamily");
52    private static final byte[] ANOTHERROW = Bytes.toBytes("anotherrow");
53    private final static byte[] COL_QUAL = Bytes.toBytes("f1");
54    private final static byte[] VAL_BYTES = Bytes.toBytes("v1");
55    private final static byte[] ROW_BYTES = Bytes.toBytes("r1");
56    private final static int leaseTimeout =
57        HConstants.DEFAULT_HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD / 4;
58  
59    /**
60     * @throws java.lang.Exception
61     */
62    @BeforeClass
63    public static void setUpBeforeClass() throws Exception {
64      TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD,
65        leaseTimeout);
66      TEST_UTIL.startMiniCluster();
67    }
68  
69    /**
70     * @throws java.lang.Exception
71     */
72    @AfterClass
73    public static void tearDownAfterClass() throws Exception {
74      TEST_UTIL.shutdownMiniCluster();
75    }
76  
77    /**
78     * @throws java.lang.Exception
79     */
80    @Before
81    public void setUp() throws Exception {
82      // Nothing to do.
83    }
84  
85    /**
86     * @throws java.lang.Exception
87     */
88    @After
89    public void tearDown() throws Exception {
90      for (HTableDescriptor htd : TEST_UTIL.getHBaseAdmin().listTables()) {
91        LOG.info("Tear down, remove table=" + htd.getTableName());
92        TEST_UTIL.deleteTable(htd.getTableName());
93      }
94    }
95  
96    @Test
97    public void testLeaseRenewal() throws Exception {
98      HTable table = TEST_UTIL.createTable(
99        TableName.valueOf("testLeaseRenewal"), FAMILY);
100     Put p = new Put(ROW_BYTES);
101     p.addColumn(FAMILY, COL_QUAL, VAL_BYTES);
102     table.put(p);
103     p = new Put(ANOTHERROW);
104     p.addColumn(FAMILY, COL_QUAL, VAL_BYTES);
105     table.put(p);
106     Scan s = new Scan();
107     s.setCaching(1);
108     ResultScanner rs = table.getScanner(s);
109     // make sure that calling renewLease does not impact the scan results
110     assertTrue(((AbstractClientScanner)rs).renewLease());
111     assertTrue(Arrays.equals(rs.next().getRow(), ANOTHERROW));
112     // renew the lease a few times, long enough to be sure
113     // the lease would have expired otherwise
114     Thread.sleep(leaseTimeout/2);
115     assertTrue(((AbstractClientScanner)rs).renewLease());
116     Thread.sleep(leaseTimeout/2);
117     assertTrue(((AbstractClientScanner)rs).renewLease());
118     Thread.sleep(leaseTimeout/2);
119     assertTrue(((AbstractClientScanner)rs).renewLease());
120     // make sure we haven't advanced the scanner
121     assertTrue(Arrays.equals(rs.next().getRow(), ROW_BYTES));
122     assertTrue(((AbstractClientScanner)rs).renewLease());
123     // make sure scanner is exhausted now
124     assertNull(rs.next());
125     // renewLease should return false now
126     assertFalse(((AbstractClientScanner)rs).renewLease());
127     rs.close();
128     table.close();
129     MetricsHBaseServerSource serverSource = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0)
130         .getRpcServer().getMetrics().getMetricsSource();
131     HELPER.assertCounter("exceptions.OutOfOrderScannerNextException", 0, serverSource);
132   }
133 }