From 408645c4ef4c0ea0f675dc4689397cb11a7a6499 Mon Sep 17 00:00:00 2001 From: zhangduo Date: Fri, 5 May 2017 14:46:17 +0800 Subject: [PATCH] HBASE-18000 Make sure we always return the scanner id with ScanResponse Signed-off-by: Andrew Purtell --- .../hbase/regionserver/RSRpcServices.java | 3 + .../hbase/client/TestAlwaysSetScannerId.java | 106 ++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAlwaysSetScannerId.java diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/RSRpcServices.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/RSRpcServices.java index 27f54205a7c..92dfe82228b 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/RSRpcServices.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/RSRpcServices.java @@ -2792,6 +2792,9 @@ public class RSRpcServices implements HBaseRPCErrorHandler, try { if (request.hasScannerId()) { rsh = getRegionScanner(request); + // The downstream projects such as AsyncHBase in OpenTSDB need this value. See HBASE-18000 + // for more details. + builder.setScannerId(request.getScannerId()); } else { rsh = newRegionScanner(request, builder); } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAlwaysSetScannerId.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAlwaysSetScannerId.java new file mode 100644 index 00000000000..8e1e37fd1d6 --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAlwaysSetScannerId.java @@ -0,0 +1,106 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.client; + +import static org.junit.Assert.*; + +import java.io.IOException; + +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.HRegionInfo; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.ConnectionManager.HConnectionImplementation; +import org.apache.hadoop.hbase.protobuf.RequestConverter; +import org.apache.hadoop.hbase.protobuf.generated.ClientProtos; +import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanRequest; +import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ScanResponse; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.apache.hadoop.hbase.testclassification.RegionServerTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import com.google.protobuf.ServiceException; + +/** + * Testcase to make sure that we always set scanner id in ScanResponse. See HBASE-18000. + */ +@Category({ RegionServerTests.class, MediumTests.class }) +public class TestAlwaysSetScannerId { + + private static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); + + private static final TableName TABLE_NAME = TableName.valueOf("test"); + + private static final byte[] CF = Bytes.toBytes("cf"); + + private static final byte[] CQ = Bytes.toBytes("cq"); + + private static final int COUNT = 10; + + private static HRegionInfo HRI; + + private static ClientProtos.ClientService.BlockingInterface STUB; + + @BeforeClass + public static void setUp() throws Exception { + UTIL.startMiniCluster(1); + try (Table table = UTIL.createTable(TABLE_NAME, CF)) { + for (int i = 0; i < COUNT; i++) { + table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i))); + } + } + HRI = UTIL.getHBaseAdmin().getTableRegions(TABLE_NAME).get(0); + STUB = ((HConnectionImplementation)UTIL.getConnection()) + .getClient(UTIL.getHBaseCluster().getRegionServer(0).getServerName()); + } + + @AfterClass + public static void tearDown() throws Exception { + UTIL.shutdownMiniCluster(); + } + + @Test + public void test() throws ServiceException, IOException { + Scan scan = new Scan(); + ScanRequest req = RequestConverter.buildScanRequest(HRI.getRegionName(), scan, 1, false); + ScanResponse resp = STUB.scan(null, req); + assertTrue(resp.hasScannerId()); + long scannerId = resp.getScannerId(); + int nextCallSeq = 0; + // test next + for (int i = 0; i < 5; i++) { + req = RequestConverter.buildScanRequest(scannerId, 1, false, nextCallSeq++, false, false, -1); + resp = STUB.scan(null, req); + assertTrue(resp.hasScannerId()); + assertEquals(scannerId, resp.getScannerId()); + } + // test renew + req = RequestConverter.buildScanRequest(scannerId, 0, false, nextCallSeq++, false, true, -1); + resp = STUB.scan(null, req); + assertTrue(resp.hasScannerId()); + assertEquals(scannerId, resp.getScannerId()); + // test close + req = RequestConverter.buildScanRequest(scannerId, 0, true, false); + resp = STUB.scan(null, req); + assertTrue(resp.hasScannerId()); + assertEquals(scannerId, resp.getScannerId()); + } +}