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