1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.client;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.IOException;
24
25 import org.apache.hadoop.hbase.HBaseTestingUtility;
26 import org.apache.hadoop.hbase.HRegionInfo;
27 import org.apache.hadoop.hbase.TableName;
28 import org.apache.hadoop.hbase.client.ConnectionManager.HConnectionImplementation;
29 import org.apache.hadoop.hbase.protobuf.RequestConverter;
30 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
31 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanRequest;
32 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanResponse;
33 import org.apache.hadoop.hbase.testclassification.MediumTests;
34 import org.apache.hadoop.hbase.testclassification.RegionServerTests;
35 import org.apache.hadoop.hbase.util.Bytes;
36 import org.junit.AfterClass;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.junit.experimental.categories.Category;
40
41 import com.google.protobuf.ServiceException;
42
43
44
45
46 @Category({ RegionServerTests.class, MediumTests.class })
47 public class TestAlwaysSetScannerId {
48
49 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
50
51 private static final TableName TABLE_NAME = TableName.valueOf("test");
52
53 private static final byte[] CF = Bytes.toBytes("cf");
54
55 private static final byte[] CQ = Bytes.toBytes("cq");
56
57 private static final int COUNT = 10;
58
59 private static HRegionInfo HRI;
60
61 private static ClientProtos.ClientService.BlockingInterface STUB;
62
63 @BeforeClass
64 public static void setUp() throws Exception {
65 UTIL.startMiniCluster(1);
66 try (Table table = UTIL.createTable(TABLE_NAME, CF)) {
67 for (int i = 0; i < COUNT; i++) {
68 table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i)));
69 }
70 }
71 HRI = UTIL.getHBaseAdmin().getTableRegions(TABLE_NAME).get(0);
72 STUB = ((HConnectionImplementation)UTIL.getConnection())
73 .getClient(UTIL.getHBaseCluster().getRegionServer(0).getServerName());
74 }
75
76 @AfterClass
77 public static void tearDown() throws Exception {
78 UTIL.shutdownMiniCluster();
79 }
80
81 @Test
82 public void test() throws ServiceException, IOException {
83 Scan scan = new Scan();
84 ScanRequest req = RequestConverter.buildScanRequest(HRI.getRegionName(), scan, 1, false);
85 ScanResponse resp = STUB.scan(null, req);
86 assertTrue(resp.hasScannerId());
87 long scannerId = resp.getScannerId();
88 int nextCallSeq = 0;
89
90 for (int i = 0; i < COUNT / 2; i++) {
91 req = RequestConverter.buildScanRequest(scannerId, 1, false, nextCallSeq++, false, false, -1);
92 resp = STUB.scan(null, req);
93 assertTrue(resp.hasScannerId());
94 assertEquals(scannerId, resp.getScannerId());
95 }
96
97 req = RequestConverter.buildScanRequest(scannerId, 0, false, nextCallSeq++, false, true, -1);
98 resp = STUB.scan(null, req);
99 assertTrue(resp.hasScannerId());
100 assertEquals(scannerId, resp.getScannerId());
101
102 req = RequestConverter.buildScanRequest(scannerId, 0, true, false);
103 resp = STUB.scan(null, req);
104 assertTrue(resp.hasScannerId());
105 assertEquals(scannerId, resp.getScannerId());
106 }
107 }