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.regex.Pattern; 031import org.apache.hadoop.conf.Configuration; 032import org.apache.hadoop.hbase.HBaseClassTestRule; 033import org.apache.hadoop.hbase.HBaseTestingUtility; 034import org.apache.hadoop.hbase.HTableDescriptor; 035import org.apache.hadoop.hbase.TableName; 036import org.apache.hadoop.hbase.testclassification.RestTests; 037import org.apache.hadoop.hbase.testclassification.SmallTests; 038import org.apache.hadoop.hbase.util.Bytes; 039import org.junit.Before; 040import org.junit.ClassRule; 041import org.junit.Test; 042import org.junit.experimental.categories.Category; 043 044/** 045 * Tests {@link RemoteAdmin} retries. 046 */ 047@Category({RestTests.class, SmallTests.class}) 048public class TestRemoteAdminRetries { 049 050 @ClassRule 051 public static final HBaseClassTestRule CLASS_RULE = 052 HBaseClassTestRule.forClass(TestRemoteAdminRetries.class); 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 HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 059 060 private RemoteAdmin remoteAdmin; 061 private Client client; 062 063 @Before 064 public void setup() throws Exception { 065 client = mock(Client.class); 066 Response response = new Response(509); 067 when(client.get(anyString(), anyString())).thenReturn(response); 068 when(client.delete(anyString())).thenReturn(response); 069 when(client.put(anyString(), anyString(), any())).thenReturn(response); 070 when(client.post(anyString(), anyString(), any())).thenReturn(response); 071 Configuration configuration = TEST_UTIL.getConfiguration(); 072 073 configuration.setInt("hbase.rest.client.max.retries", RETRIES); 074 configuration.setInt("hbase.rest.client.sleep", SLEEP_TIME); 075 076 remoteAdmin = new RemoteAdmin(client, TEST_UTIL.getConfiguration(), "MyTable"); 077 } 078 079 @Test 080 public void testFailingGetRestVersion() throws Exception { 081 testTimedOutGetCall(new CallExecutor() { 082 @Override 083 public void run() throws Exception { 084 remoteAdmin.getRestVersion(); 085 } 086 }); 087 } 088 089 @Test 090 public void testFailingGetClusterStatus() throws Exception { 091 testTimedOutGetCall(new CallExecutor() { 092 @Override 093 public void run() throws Exception { 094 remoteAdmin.getClusterStatus(); 095 } 096 }); 097 } 098 099 @Test 100 public void testFailingGetClusterVersion() throws Exception { 101 testTimedOutGetCall(new CallExecutor() { 102 @Override 103 public void run() throws Exception { 104 remoteAdmin.getClusterVersion(); 105 } 106 }); 107 } 108 109 @Test 110 public void testFailingGetTableAvailable() throws Exception { 111 testTimedOutCall(new CallExecutor() { 112 @Override 113 public void run() throws Exception { 114 remoteAdmin.isTableAvailable(Bytes.toBytes("TestTable")); 115 } 116 }); 117 } 118 119 @Test 120 @SuppressWarnings("deprecation") 121 public void testFailingCreateTable() throws Exception { 122 testTimedOutCall(new CallExecutor() { 123 @Override 124 public void run() throws Exception { 125 remoteAdmin.createTable(new HTableDescriptor(TableName.valueOf("TestTable"))); 126 } 127 }); 128 verify(client, times(RETRIES)).put(anyString(), anyString(), any()); 129 } 130 131 @Test 132 public void testFailingDeleteTable() throws Exception { 133 testTimedOutCall(new CallExecutor() { 134 @Override 135 public void run() throws Exception { 136 remoteAdmin.deleteTable("TestTable"); 137 } 138 }); 139 verify(client, times(RETRIES)).delete(anyString()); 140 } 141 142 @Test 143 public void testFailingGetTableList() throws Exception { 144 testTimedOutGetCall(new CallExecutor() { 145 @Override 146 public void run() throws Exception { 147 remoteAdmin.getTableList(); 148 } 149 }); 150 } 151 152 private void testTimedOutGetCall(CallExecutor callExecutor) throws Exception { 153 testTimedOutCall(callExecutor); 154 verify(client, times(RETRIES)).get(anyString(), anyString()); 155 } 156 157 private void testTimedOutCall(CallExecutor callExecutor) throws Exception { 158 long start = System.currentTimeMillis(); 159 try { 160 callExecutor.run(); 161 fail("should be timeout exception!"); 162 } catch (IOException e) { 163 assertTrue(Pattern.matches(".*MyTable.*timed out", e.toString())); 164 } 165 assertTrue((System.currentTimeMillis() - start) > MAX_TIME); 166 } 167 168 private static interface CallExecutor { 169 void run() throws Exception; 170 } 171 172}