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