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.Assert.assertThrows; 021import static org.mockito.Mockito.mock; 022import static org.mockito.Mockito.times; 023import static org.mockito.Mockito.verify; 024import static org.mockito.Mockito.when; 025 026import java.io.IOException; 027import org.apache.hadoop.conf.Configuration; 028import org.apache.hadoop.hbase.HBaseClassTestRule; 029import org.apache.hadoop.hbase.HConstants; 030import org.apache.hadoop.hbase.HRegionInfo; 031import org.apache.hadoop.hbase.HRegionLocation; 032import org.apache.hadoop.hbase.RegionLocations; 033import org.apache.hadoop.hbase.ServerName; 034import org.apache.hadoop.hbase.TableName; 035import org.apache.hadoop.hbase.TableNotEnabledException; 036import org.apache.hadoop.hbase.ipc.RpcControllerFactory; 037import org.apache.hadoop.hbase.testclassification.ClientTests; 038import org.apache.hadoop.hbase.testclassification.SmallTests; 039import org.apache.hadoop.hbase.util.Bytes; 040import org.junit.Before; 041import org.junit.ClassRule; 042import org.junit.Test; 043import org.junit.experimental.categories.Category; 044import org.junit.runner.RunWith; 045import org.mockito.Mock; 046import org.mockito.runners.MockitoJUnitRunner; 047 048@RunWith(MockitoJUnitRunner.class) 049@Category({ ClientTests.class, SmallTests.class }) 050public class TestReversedScannerCallable { 051 052 @ClassRule 053 public static final HBaseClassTestRule CLASS_RULE = 054 HBaseClassTestRule.forClass(TestReversedScannerCallable.class); 055 056 private static final TableName TABLE_NAME = TableName.valueOf("TestReversedScannerCallable"); 057 058 private static final String HOSTNAME = "localhost"; 059 private static final ServerName SERVERNAME = ServerName.valueOf(HOSTNAME, 60030, 123); 060 private static final byte[] ROW = Bytes.toBytes("row1"); 061 private static final Scan DEFAULT_SCAN = new Scan().withStartRow(ROW, true).setReversed(true); 062 063 @Mock 064 private ClusterConnection connection; 065 @Mock 066 private RpcControllerFactory rpcFactory; 067 @Mock 068 private RegionLocations regionLocations; 069 @Mock 070 private HRegionLocation regionLocation; 071 072 @Before 073 public void setUp() throws Exception { 074 when(connection.getConfiguration()).thenReturn(new Configuration()); 075 when(regionLocations.size()).thenReturn(1); 076 when(regionLocations.getRegionLocation(0)).thenReturn(regionLocation); 077 when(regionLocation.getHostname()).thenReturn(HOSTNAME); 078 when(regionLocation.getServerName()).thenReturn(SERVERNAME); 079 } 080 081 @Test 082 public void testPrepareAlwaysUsesCache() throws Exception { 083 when(connection.locateRegion(TABLE_NAME, ROW, true, true, 0)).thenReturn(regionLocations); 084 085 ReversedScannerCallable callable = 086 new ReversedScannerCallable(connection, TABLE_NAME, DEFAULT_SCAN, null, rpcFactory, 0); 087 callable.prepare(false); 088 callable.prepare(true); 089 090 verify(connection, times(2)).locateRegion(TABLE_NAME, ROW, true, true, 0); 091 } 092 093 @Test 094 public void testHandleDisabledTable() throws IOException { 095 when(connection.isTableDisabled(TABLE_NAME)).thenReturn(true); 096 097 ReversedScannerCallable callable = 098 new ReversedScannerCallable(connection, TABLE_NAME, DEFAULT_SCAN, null, rpcFactory, 0); 099 100 assertThrows(TableNotEnabledException.class, () -> callable.prepare(true)); 101 } 102 103 @Test 104 public void testUpdateSearchKeyCacheLocation() throws IOException { 105 byte[] regionName = RegionInfo.createRegionName(TABLE_NAME, 106 ConnectionUtils.createCloseRowBefore(ConnectionUtils.MAX_BYTE_ARRAY), "123", false); 107 HRegionInfo mockRegionInfo = mock(HRegionInfo.class); 108 when(mockRegionInfo.containsRow(ConnectionUtils.MAX_BYTE_ARRAY)).thenReturn(true); 109 when(mockRegionInfo.getEndKey()).thenReturn(HConstants.EMPTY_END_ROW); 110 when(mockRegionInfo.getRegionName()).thenReturn(regionName); 111 when(regionLocation.getRegionInfo()).thenReturn(mockRegionInfo); 112 113 IOException testThrowable = new IOException("test throwable"); 114 115 when(connection.locateRegion(TABLE_NAME, ConnectionUtils.MAX_BYTE_ARRAY, true, true, 0)) 116 .thenReturn(regionLocations); 117 118 Scan scan = new Scan().setReversed(true); 119 ReversedScannerCallable callable = 120 new ReversedScannerCallable(connection, TABLE_NAME, scan, null, rpcFactory, 0); 121 122 callable.prepare(false); 123 124 callable.throwable(testThrowable, true); 125 126 verify(connection).updateCachedLocations(TABLE_NAME, regionName, ConnectionUtils.MAX_BYTE_ARRAY, 127 testThrowable, SERVERNAME); 128 } 129}