001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.client; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertFalse; 022import static org.junit.jupiter.api.Assertions.assertTrue; 023import static org.mockito.ArgumentMatchers.any; 024import static org.mockito.ArgumentMatchers.argThat; 025import static org.mockito.Mockito.doAnswer; 026import static org.mockito.Mockito.mock; 027import static org.mockito.Mockito.times; 028import static org.mockito.Mockito.verify; 029 030import java.io.IOException; 031import java.util.List; 032import java.util.concurrent.TimeUnit; 033import java.util.stream.Stream; 034import org.apache.hadoop.conf.Configuration; 035import org.apache.hadoop.hbase.Cell; 036import org.apache.hadoop.hbase.CellBuilderFactory; 037import org.apache.hadoop.hbase.CellBuilderType; 038import org.apache.hadoop.hbase.HBaseConfiguration; 039import org.apache.hadoop.hbase.HBaseParameterizedTestTemplate; 040import org.apache.hadoop.hbase.HRegionLocation; 041import org.apache.hadoop.hbase.ServerName; 042import org.apache.hadoop.hbase.TableName; 043import org.apache.hadoop.hbase.client.metrics.ScanMetrics; 044import org.apache.hadoop.hbase.exceptions.OutOfOrderScannerNextException; 045import org.apache.hadoop.hbase.exceptions.ScannerResetException; 046import org.apache.hadoop.hbase.ipc.HBaseRpcController; 047import org.apache.hadoop.hbase.ipc.HBaseRpcControllerImpl; 048import org.apache.hadoop.hbase.security.User; 049import org.apache.hadoop.hbase.testclassification.ClientTests; 050import org.apache.hadoop.hbase.testclassification.MediumTests; 051import org.apache.hadoop.hbase.util.Bytes; 052import org.junit.jupiter.api.Tag; 053import org.junit.jupiter.api.TestTemplate; 054import org.junit.jupiter.params.provider.Arguments; 055 056import org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback; 057 058import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 059import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ClientService; 060import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanRequest; 061import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.ScanResponse; 062 063@Tag(ClientTests.TAG) 064@Tag(MediumTests.TAG) 065@HBaseParameterizedTestTemplate(name = "[{index}] remote={0}") 066public class TestAsyncScanSingleRegionRpcRetryingCaller { 067 068 private final boolean remote; 069 070 public TestAsyncScanSingleRegionRpcRetryingCaller(boolean remote) { 071 this.remote = remote; 072 } 073 074 public static Stream<Arguments> parameters() { 075 return Stream.of(Arguments.of(false), Arguments.of(true)); 076 } 077 078 private AsyncScanSingleRegionRpcRetryingCaller createCaller(AsyncConnectionImpl conn, 079 ClientService.Interface stub, ScanMetrics metrics) { 080 HRegionLocation location = 081 new HRegionLocation(RegionInfoBuilder.newBuilder(TableName.valueOf("test")).build(), 082 ServerName.valueOf("rs", 16020, 1)); 083 return conn.callerFactory.scanSingleRegion().id(1) 084 .setScan(new Scan().setScanMetricsEnabled(true)).metrics(metrics).remote(remote) 085 .resultCache(new CompleteScanResultCache()).consumer(mock(AdvancedScanResultConsumer.class)) 086 .stub(stub).location(location).maxAttempts(3).build(); 087 } 088 089 private void assertResultsMetrics(boolean heartbeat) throws Exception { 090 Configuration conf = HBaseConfiguration.create(); 091 User user = User.getCurrent(); 092 try (AsyncConnectionImpl conn = new AsyncConnectionImpl(conf, 093 new DoNothingConnectionRegistry(conf, user), "test", null, user)) { 094 ScanMetrics metrics = new ScanMetrics(); 095 AsyncScanSingleRegionRpcRetryingCaller caller = 096 createCaller(conn, mock(ClientService.Interface.class), metrics); 097 Cell cell = CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setType(Cell.Type.Put) 098 .setRow(Bytes.toBytes("row")).setFamily(Bytes.toBytes("cf")) 099 .setQualifier(Bytes.toBytes("cq")).setValue(Bytes.toBytes("v")).build(); 100 ScanResponse resp = ScanResponse.newBuilder() 101 .addResults(ProtobufUtil.toResult(Result.create(List.of(cell)))) 102 .setHeartbeatMessage(heartbeat).setMoreResultsInRegion(false).setMoreResults(false).build(); 103 104 // The results come with the open scanner response, so the scan completes without any RPC. 105 assertFalse(caller.start(new HBaseRpcControllerImpl(), resp).get(5, TimeUnit.SECONDS)); 106 long bytesInResults = metrics.countOfBytesInResults.get(); 107 assertTrue(bytesInResults > 0); 108 // Whether the results count as remote depends only on the region server location. 109 assertEquals(remote ? bytesInResults : 0, metrics.countOfBytesInRemoteResults.get()); 110 } 111 } 112 113 @TestTemplate 114 public void testResultsMetrics() throws Exception { 115 assertResultsMetrics(false); 116 } 117 118 @TestTemplate 119 public void testResultsMetricsOfHeartbeatResponse() throws Exception { 120 assertResultsMetrics(true); 121 } 122 123 private void assertRetryMetrics(IOException error, boolean closeScanner) throws Exception { 124 Configuration conf = HBaseConfiguration.create(); 125 User user = User.getCurrent(); 126 try (AsyncConnectionImpl conn = new AsyncConnectionImpl(conf, 127 new DoNothingConnectionRegistry(conf, user), "test", null, user)) { 128 ClientService.Interface stub = mock(ClientService.Interface.class); 129 doAnswer(invocation -> { 130 HBaseRpcController controller = invocation.getArgument(0); 131 ScanRequest request = invocation.getArgument(1); 132 RpcCallback<ScanResponse> done = invocation.getArgument(2); 133 if (request.getCloseScanner()) { 134 done.run(ScanResponse.getDefaultInstance()); 135 } else { 136 controller.setFailed(error); 137 done.run(null); 138 } 139 return null; 140 }).when(stub).scan(any(), any(), any()); 141 142 ScanMetrics metrics = new ScanMetrics(); 143 AsyncScanSingleRegionRpcRetryingCaller caller = createCaller(conn, stub, metrics); 144 145 // A successful open is followed by a scan-next RPC that fails and requires reopening. 146 assertTrue( 147 caller 148 .start(new HBaseRpcControllerImpl(), 149 ScanResponse.newBuilder().setMoreResultsInRegion(true).build()) 150 .get(5, TimeUnit.SECONDS)); 151 verify(stub).scan(any(), argThat(request -> !request.getCloseScanner()), any()); 152 verify(stub, times(closeScanner ? 1 : 0)).scan(any(), argThat(ScanRequest::getCloseScanner), 153 any()); 154 assertEquals(1, metrics.countOfRPCRetries.get()); 155 assertEquals(remote ? 1 : 0, metrics.countOfRemoteRPCRetries.get()); 156 } 157 } 158 159 @TestTemplate 160 public void testRetryMetricsAfterScannerReset() throws Exception { 161 assertRetryMetrics(new ScannerResetException("Injected scanner reset"), false); 162 } 163 164 @TestTemplate 165 public void testRetryMetricsAfterOutOfOrderScannerNext() throws Exception { 166 assertRetryMetrics(new OutOfOrderScannerNextException("Injected out-of-order scan"), true); 167 } 168}