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