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.ipc.MetricsHBaseServerSource;
34  import org.apache.hadoop.hbase.test.MetricsAssertHelper;
35  import org.apache.hadoop.hbase.testclassification.LargeTests;
36  import org.apache.hadoop.hbase.util.Bytes;
37  import org.junit.After;
38  import org.junit.AfterClass;
39  import org.junit.Before;
40  import org.junit.BeforeClass;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  @Category(LargeTests.class)
45  public class TestLeaseRenewal {
46    public MetricsAssertHelper HELPER = CompatibilityFactory.getInstance(MetricsAssertHelper.class);
47  
48    final Log LOG = LogFactory.getLog(getClass());
49    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
50    private static byte[] FAMILY = Bytes.toBytes("testFamily");
51    private static final byte[] ANOTHERROW = Bytes.toBytes("anotherrow");
52    private final static byte[] COL_QUAL = Bytes.toBytes("f1");
53    private final static byte[] VAL_BYTES = Bytes.toBytes("v1");
54    private final static byte[] ROW_BYTES = Bytes.toBytes("r1");
55    private final static int leaseTimeout = HConstants.DEFAULT_HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD/4;
56  
57    /**
58     * @throws java.lang.Exception
59     */
60    @BeforeClass
61    public static void setUpBeforeClass() throws Exception {
62      TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD, leaseTimeout);
63      TEST_UTIL.startMiniCluster();
64    }
65  
66    /**
67     * @throws java.lang.Exception
68     */
69    @AfterClass
70    public static void tearDownAfterClass() throws Exception {
71      TEST_UTIL.shutdownMiniCluster();
72    }
73  
74    /**
75     * @throws java.lang.Exception
76     */
77    @Before
78    public void setUp() throws Exception {
79      // Nothing to do.
80    }
81  
82    /**
83     * @throws java.lang.Exception
84     */
85    @After
86    public void tearDown() throws Exception {
87      for (HTableDescriptor htd : TEST_UTIL.getHBaseAdmin().listTables()) {
88        LOG.info("Tear down, remove table=" + htd.getTableName());
89        TEST_UTIL.deleteTable(htd.getTableName());
90      }
91    }
92  
93    @Test
94    public void testLeaseRenewal() throws Exception {
95      HTable table = TEST_UTIL.createTable(
96        Bytes.toBytes("testLeaseRenewal"), FAMILY);
97      Put p = new Put(ROW_BYTES);
98      p.add(FAMILY, COL_QUAL, VAL_BYTES);
99      table.put(p);
100     p = new Put(ANOTHERROW);
101     p.add(FAMILY, COL_QUAL, VAL_BYTES);
102     table.put(p);
103     Scan s = new Scan();
104     s.setCaching(1);
105     ResultScanner rs = table.getScanner(s);
106     // make sure that calling renewLease does not impact the scan results
107     assertTrue(((AbstractClientScanner)rs).renewLease());
108     assertTrue(Arrays.equals(rs.next().getRow(), ANOTHERROW));
109     // renew the lease a few times, long enough to be sure
110     // the lease would have expired otherwise
111     Thread.sleep(leaseTimeout/2);
112     assertTrue(((AbstractClientScanner)rs).renewLease());
113     Thread.sleep(leaseTimeout/2);
114     assertTrue(((AbstractClientScanner)rs).renewLease());
115     Thread.sleep(leaseTimeout/2);
116     assertTrue(((AbstractClientScanner)rs).renewLease());
117     // make sure we haven't advanced the scanner
118     assertTrue(Arrays.equals(rs.next().getRow(), ROW_BYTES));
119     assertTrue(((AbstractClientScanner)rs).renewLease());
120     // make sure scanner is exhausted now
121     assertNull(rs.next());
122     // renewLease should return false now
123     assertFalse(((AbstractClientScanner)rs).renewLease());
124     rs.close();
125     table.close();
126     MetricsHBaseServerSource serverSource = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0)
127         .getRpcServer().getMetrics().getMetricsSource();
128     HELPER.assertCounter("exceptions.OutOfOrderScannerNextException", 0, serverSource);
129   }
130 }