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.rest.client; 019 020import static org.junit.jupiter.api.Assertions.assertTrue; 021import static org.junit.jupiter.api.Assertions.fail; 022import static org.mockito.ArgumentMatchers.any; 023import static org.mockito.ArgumentMatchers.anyString; 024import static org.mockito.Mockito.mock; 025import static org.mockito.Mockito.times; 026import static org.mockito.Mockito.verify; 027import static org.mockito.Mockito.when; 028 029import java.io.IOException; 030import java.util.Arrays; 031import java.util.regex.Pattern; 032import org.apache.hadoop.conf.Configuration; 033import org.apache.hadoop.hbase.HBaseTestingUtil; 034import org.apache.hadoop.hbase.client.Delete; 035import org.apache.hadoop.hbase.client.Get; 036import org.apache.hadoop.hbase.client.Put; 037import org.apache.hadoop.hbase.client.Scan; 038import org.apache.hadoop.hbase.testclassification.RestTests; 039import org.apache.hadoop.hbase.testclassification.SmallTests; 040import org.apache.hadoop.hbase.util.Bytes; 041import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 042import org.junit.jupiter.api.AfterEach; 043import org.junit.jupiter.api.BeforeEach; 044import org.junit.jupiter.api.Tag; 045import org.junit.jupiter.api.Test; 046 047/** 048 * Test RemoteHTable retries. 049 */ 050@Tag(RestTests.TAG) 051@Tag(SmallTests.TAG) 052public class TestRemoteHTableRetries { 053 054 private static final int SLEEP_TIME = 50; 055 private static final int RETRIES = 3; 056 private static final long MAX_TIME = SLEEP_TIME * (RETRIES - 1); 057 058 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 059 060 private static final byte[] ROW_1 = Bytes.toBytes("testrow1"); 061 private static final byte[] COLUMN_1 = Bytes.toBytes("a"); 062 private static final byte[] QUALIFIER_1 = Bytes.toBytes("1"); 063 private static final byte[] VALUE_1 = Bytes.toBytes("testvalue1"); 064 065 private Client client; 066 private RemoteHTable remoteTable; 067 068 @BeforeEach 069 public void setup() throws Exception { 070 client = mock(Client.class); 071 Response response = new Response(509); 072 when(client.get(anyString(), anyString())).thenReturn(response); 073 when(client.delete(anyString())).thenReturn(response); 074 when(client.put(anyString(), anyString(), any())).thenReturn(response); 075 when(client.post(anyString(), anyString(), any())).thenReturn(response); 076 077 Configuration configuration = TEST_UTIL.getConfiguration(); 078 configuration.setInt("hbase.rest.client.max.retries", RETRIES); 079 configuration.setInt("hbase.rest.client.sleep", SLEEP_TIME); 080 081 remoteTable = new RemoteHTable(client, TEST_UTIL.getConfiguration(), "MyTable"); 082 } 083 084 @AfterEach 085 public void tearDownAfterClass() throws Exception { 086 remoteTable.close(); 087 } 088 089 @Test 090 public void testDelete() throws Exception { 091 testTimedOutCall(new CallExecutor() { 092 @Override 093 public void run() throws Exception { 094 Delete delete = new Delete(Bytes.toBytes("delete")); 095 remoteTable.delete(delete); 096 } 097 }); 098 verify(client, times(RETRIES)).delete(anyString()); 099 } 100 101 @Test 102 public void testGet() throws Exception { 103 testTimedOutGetCall(new CallExecutor() { 104 @Override 105 public void run() throws Exception { 106 remoteTable.get(new Get(Bytes.toBytes("Get"))); 107 } 108 }); 109 } 110 111 @Test 112 public void testSingleRowPut() throws Exception { 113 testTimedOutCall(new CallExecutor() { 114 @Override 115 public void run() throws Exception { 116 remoteTable.put(new Put(Bytes.toBytes("Row"))); 117 } 118 }); 119 verify(client, times(RETRIES)).put(anyString(), anyString(), any()); 120 } 121 122 @Test 123 public void testMultiRowPut() throws Exception { 124 testTimedOutCall(new CallExecutor() { 125 @Override 126 public void run() throws Exception { 127 Put[] puts = { new Put(Bytes.toBytes("Row1")), new Put(Bytes.toBytes("Row2")) }; 128 remoteTable.put(Arrays.asList(puts)); 129 } 130 }); 131 verify(client, times(RETRIES)).put(anyString(), anyString(), any()); 132 } 133 134 @Test 135 public void testGetScanner() throws Exception { 136 testTimedOutCall(new CallExecutor() { 137 @Override 138 public void run() throws Exception { 139 remoteTable.getScanner(new Scan()); 140 } 141 }); 142 verify(client, times(RETRIES)).post(anyString(), anyString(), any()); 143 } 144 145 @Test 146 public void testCheckAndPut() throws Exception { 147 testTimedOutCall(new CallExecutor() { 148 @Override 149 public void run() throws Exception { 150 Put put = new Put(ROW_1); 151 put.addColumn(COLUMN_1, QUALIFIER_1, VALUE_1); 152 remoteTable.checkAndMutate(ROW_1, COLUMN_1).qualifier(QUALIFIER_1).ifEquals(VALUE_1) 153 .thenPut(put); 154 } 155 }); 156 verify(client, times(RETRIES)).put(anyString(), anyString(), any()); 157 } 158 159 @Test 160 public void testCheckAndDelete() throws Exception { 161 testTimedOutCall(new CallExecutor() { 162 @Override 163 public void run() throws Exception { 164 Put put = new Put(ROW_1); 165 put.addColumn(COLUMN_1, QUALIFIER_1, VALUE_1); 166 Delete delete = new Delete(ROW_1); 167 remoteTable.checkAndMutate(ROW_1, COLUMN_1).qualifier(QUALIFIER_1).ifEquals(VALUE_1) 168 .thenDelete(delete); 169 } 170 }); 171 } 172 173 private void testTimedOutGetCall(CallExecutor callExecutor) throws Exception { 174 testTimedOutCall(callExecutor); 175 verify(client, times(RETRIES)).get(anyString(), anyString()); 176 } 177 178 private void testTimedOutCall(CallExecutor callExecutor) throws Exception { 179 long start = EnvironmentEdgeManager.currentTime(); 180 try { 181 callExecutor.run(); 182 fail("should be timeout exception!"); 183 } catch (IOException e) { 184 assertTrue(Pattern.matches(".*request timed out", e.toString())); 185 } 186 assertTrue((EnvironmentEdgeManager.currentTime() - start) > MAX_TIME); 187 } 188 189 private interface CallExecutor { 190 void run() throws Exception; 191 } 192}