1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
68
69 @AfterClass
70 public static void tearDownAfterClass() throws Exception {
71 TEST_UTIL.shutdownMiniCluster();
72 }
73
74
75
76
77 @Before
78 public void setUp() throws Exception {
79
80 }
81
82
83
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
107 assertTrue(((AbstractClientScanner)rs).renewLease());
108 assertTrue(Arrays.equals(rs.next().getRow(), ANOTHERROW));
109
110
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
118 assertTrue(Arrays.equals(rs.next().getRow(), ROW_BYTES));
119 assertTrue(((AbstractClientScanner)rs).renewLease());
120
121 assertNull(rs.next());
122
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 }