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.Arrays;
031import java.util.regex.Pattern;
032import org.apache.hadoop.conf.Configuration;
033import org.apache.hadoop.hbase.HBaseClassTestRule;
034import org.apache.hadoop.hbase.HBaseTestingUtil;
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.apache.hadoop.hbase.util.EnvironmentEdgeManager;
043import org.junit.After;
044import org.junit.Before;
045import org.junit.ClassRule;
046import org.junit.Test;
047import org.junit.experimental.categories.Category;
048
049/**
050 * Test RemoteHTable retries.
051 */
052@Category({ RestTests.class, SmallTests.class })
053public class TestRemoteHTableRetries {
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 HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
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(response);
079    when(client.post(anyString(), anyString(), any())).thenReturn(response);
080
081    Configuration configuration = TEST_UTIL.getConfiguration();
082    configuration.setInt("hbase.rest.client.max.retries", RETRIES);
083    configuration.setInt("hbase.rest.client.sleep", SLEEP_TIME);
084
085    remoteTable = new RemoteHTable(client, TEST_UTIL.getConfiguration(), "MyTable");
086  }
087
088  @After
089  public void tearDownAfterClass() throws Exception {
090    remoteTable.close();
091  }
092
093  @Test
094  public void testDelete() throws Exception {
095    testTimedOutCall(new CallExecutor() {
096      @Override
097      public void run() throws Exception {
098        Delete delete = new Delete(Bytes.toBytes("delete"));
099        remoteTable.delete(delete);
100      }
101    });
102    verify(client, times(RETRIES)).delete(anyString());
103  }
104
105  @Test
106  public void testGet() throws Exception {
107    testTimedOutGetCall(new CallExecutor() {
108      @Override
109      public void run() throws Exception {
110        remoteTable.get(new Get(Bytes.toBytes("Get")));
111      }
112    });
113  }
114
115  @Test
116  public void testSingleRowPut() throws Exception {
117    testTimedOutCall(new CallExecutor() {
118      @Override
119      public void run() throws Exception {
120        remoteTable.put(new Put(Bytes.toBytes("Row")));
121      }
122    });
123    verify(client, times(RETRIES)).put(anyString(), anyString(), any());
124  }
125
126  @Test
127  public void testMultiRowPut() throws Exception {
128    testTimedOutCall(new CallExecutor() {
129      @Override
130      public void run() throws Exception {
131        Put[] puts = { new Put(Bytes.toBytes("Row1")), new Put(Bytes.toBytes("Row2")) };
132        remoteTable.put(Arrays.asList(puts));
133      }
134    });
135    verify(client, times(RETRIES)).put(anyString(), anyString(), any());
136  }
137
138  @Test
139  public void testGetScanner() throws Exception {
140    testTimedOutCall(new CallExecutor() {
141      @Override
142      public void run() throws Exception {
143        remoteTable.getScanner(new Scan());
144      }
145    });
146    verify(client, times(RETRIES)).post(anyString(), anyString(), any());
147  }
148
149  @Test
150  public void testCheckAndPut() throws Exception {
151    testTimedOutCall(new CallExecutor() {
152      @Override
153      public void run() throws Exception {
154        Put put = new Put(ROW_1);
155        put.addColumn(COLUMN_1, QUALIFIER_1, VALUE_1);
156        remoteTable.checkAndMutate(ROW_1, COLUMN_1).qualifier(QUALIFIER_1).ifEquals(VALUE_1)
157          .thenPut(put);
158      }
159    });
160    verify(client, times(RETRIES)).put(anyString(), anyString(), any());
161  }
162
163  @Test
164  public void testCheckAndDelete() throws Exception {
165    testTimedOutCall(new CallExecutor() {
166      @Override
167      public void run() throws Exception {
168        Put put = new Put(ROW_1);
169        put.addColumn(COLUMN_1, QUALIFIER_1, VALUE_1);
170        Delete delete = new Delete(ROW_1);
171        remoteTable.checkAndMutate(ROW_1, COLUMN_1).qualifier(QUALIFIER_1).ifEquals(VALUE_1)
172          .thenDelete(delete);
173      }
174    });
175  }
176
177  private void testTimedOutGetCall(CallExecutor callExecutor) throws Exception {
178    testTimedOutCall(callExecutor);
179    verify(client, times(RETRIES)).get(anyString(), anyString());
180  }
181
182  private void testTimedOutCall(CallExecutor callExecutor) throws Exception {
183    long start = EnvironmentEdgeManager.currentTime();
184    try {
185      callExecutor.run();
186      fail("should be timeout exception!");
187    } catch (IOException e) {
188      assertTrue(Pattern.matches(".*request timed out", e.toString()));
189    }
190    assertTrue((EnvironmentEdgeManager.currentTime() - start) > MAX_TIME);
191  }
192
193  private interface CallExecutor {
194    void run() throws Exception;
195  }
196}