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