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.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(
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")), new Put(Bytes.toBytes("Row2")) };
135        remoteTable.put(Arrays.asList(puts));
136      }
137    });
138    verify(client, times(RETRIES)).put(anyString(), anyString(), any());
139  }
140
141  @Test
142  public void testGetScanner() throws Exception {
143    testTimedOutCall(new CallExecutor() {
144      @Override
145      public void run() throws Exception {
146        remoteTable.getScanner(new Scan());
147      }
148    });
149    verify(client, times(RETRIES)).post(anyString(), anyString(), any());
150  }
151
152  @Test
153  public void testCheckAndPut() throws Exception {
154    testTimedOutCall(new CallExecutor() {
155      @Override
156      public void run() throws Exception {
157        Put put = new Put(ROW_1);
158        put.addColumn(COLUMN_1, QUALIFIER_1, VALUE_1);
159        remoteTable.checkAndMutate(ROW_1, COLUMN_1).qualifier(QUALIFIER_1)
160            .ifEquals(VALUE_1).thenPut(put);
161      }
162    });
163    verify(client, times(RETRIES)).put(anyString(), anyString(), any());
164  }
165
166  @Test
167  public void testCheckAndDelete() throws Exception {
168    testTimedOutCall(new CallExecutor() {
169      @Override
170      public void run() throws Exception {
171        Put put = new Put(ROW_1);
172        put.addColumn(COLUMN_1, QUALIFIER_1, VALUE_1);
173        Delete delete= new Delete(ROW_1);
174        remoteTable.checkAndMutate(ROW_1, COLUMN_1).qualifier(QUALIFIER_1)
175            .ifEquals(VALUE_1).thenDelete(delete);
176      }
177    });
178  }
179
180  private void testTimedOutGetCall(CallExecutor callExecutor) throws Exception {
181    testTimedOutCall(callExecutor);
182    verify(client, times(RETRIES)).get(anyString(), anyString());
183  }
184
185  private void testTimedOutCall(CallExecutor callExecutor) throws Exception {
186    long start = EnvironmentEdgeManager.currentTime();
187    try {
188      callExecutor.run();
189      fail("should be timeout exception!");
190    } catch (IOException e) {
191      assertTrue(Pattern.matches(".*request timed out", e.toString()));
192    }
193    assertTrue((EnvironmentEdgeManager.currentTime() - start) > MAX_TIME);
194  }
195
196  private interface CallExecutor {
197    void run() throws Exception;
198  }
199}