View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.rest.client;
19  
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  import static org.mockito.Matchers.any;
23  import static org.mockito.Matchers.anyString;
24  import static org.mockito.Mockito.mock;
25  import static org.mockito.Mockito.times;
26  import static org.mockito.Mockito.verify;
27  import static org.mockito.Mockito.when;
28  
29  import java.io.IOException;
30  import java.util.Arrays;
31  import java.util.regex.Pattern;
32  
33  import org.apache.hadoop.conf.Configuration;
34  import org.apache.hadoop.hbase.HBaseTestingUtility;
35  import org.apache.hadoop.hbase.testclassification.SmallTests;
36  import org.apache.hadoop.hbase.client.Delete;
37  import org.apache.hadoop.hbase.client.Get;
38  import org.apache.hadoop.hbase.client.Put;
39  import org.apache.hadoop.hbase.client.Scan;
40  import org.apache.hadoop.hbase.util.Bytes;
41  import org.junit.After;
42  import org.junit.Before;
43  import org.junit.Test;
44  import org.junit.experimental.categories.Category;
45  
46  /**
47   * Test RemoteHTable retries.
48   */
49  @Category(SmallTests.class)
50  public class TestRemoteHTableRetries {
51    private static final int SLEEP_TIME = 50;
52    private static final int RETRIES = 3;
53    private static final long MAX_TIME = SLEEP_TIME * (RETRIES - 1);
54  
55    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
56  
57    private static final byte[] ROW_1 = Bytes.toBytes("testrow1");
58    private static final byte[] COLUMN_1 = Bytes.toBytes("a");
59    private static final byte[] QUALIFIER_1 = Bytes.toBytes("1");
60    private static final byte[] VALUE_1 = Bytes.toBytes("testvalue1");
61  
62    private Client client;
63    private RemoteHTable remoteTable;
64  
65    @Before
66    public void setup() throws Exception {
67      client = mock(Client.class);
68      Response response = new Response(509);
69      when(client.get(anyString(), anyString())).thenReturn(response);
70      when(client.delete(anyString())).thenReturn(response);
71      when(client.put(anyString(), anyString(), any(byte[].class))).thenReturn(
72          response);
73      when(client.post(anyString(), anyString(), any(byte[].class))).thenReturn(
74          response);
75  
76      Configuration configuration = TEST_UTIL.getConfiguration();
77      configuration.setInt("hbase.rest.client.max.retries", RETRIES);
78      configuration.setInt("hbase.rest.client.sleep", SLEEP_TIME);
79  
80      remoteTable = new RemoteHTable(client, TEST_UTIL.getConfiguration(),
81          "MyTable");
82    }
83  
84    @After
85    public void tearDownAfterClass() throws Exception {
86      remoteTable.close();
87    }
88  
89    @Test
90    public void testDelete() throws Exception {
91      testTimedOutCall(new CallExecutor() {
92        @Override
93        public void run() throws Exception {
94          Delete delete = new Delete(Bytes.toBytes("delete"));
95          remoteTable.delete(delete);
96        }
97      });
98      verify(client, times(RETRIES)).delete(anyString());
99    }
100 
101   @Test
102   public void testGet() throws Exception {
103     testTimedOutGetCall(new CallExecutor() {
104       @Override
105       public void run() throws Exception {
106         remoteTable.get(new Get(Bytes.toBytes("Get")));
107       }
108     });
109   }
110 
111   @Test
112   public void testSingleRowPut() throws Exception {
113     testTimedOutCall(new CallExecutor() {
114       @Override
115       public void run() throws Exception {
116         remoteTable.put(new Put(Bytes.toBytes("Row")));
117       }
118     });
119     verify(client, times(RETRIES)).put(anyString(), anyString(), any(byte[].class));
120   }
121 
122   @Test
123   public void testMultiRowPut() throws Exception {
124     testTimedOutCall(new CallExecutor() {
125       @Override
126       public void run() throws Exception {
127         Put[] puts = { new Put(Bytes.toBytes("Row1")), new Put(Bytes.toBytes("Row2")) };
128         remoteTable.put(Arrays.asList(puts));
129       }
130     });
131     verify(client, times(RETRIES)).put(anyString(), anyString(), any(byte[].class));
132   }
133 
134   @Test
135   public void testGetScanner() throws Exception {
136     testTimedOutCall(new CallExecutor() {
137       @Override
138       public void run() throws Exception {
139         remoteTable.getScanner(new Scan());
140       }
141     });
142     verify(client, times(RETRIES)).post(anyString(), anyString(), any(byte[].class));
143   }
144 
145   @Test
146   public void testCheckAndPut() throws Exception {
147     testTimedOutCall(new CallExecutor() {
148       @Override
149       public void run() throws Exception {
150         Put put = new Put(ROW_1);
151         put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
152         remoteTable.checkAndPut(ROW_1, COLUMN_1, QUALIFIER_1, VALUE_1, put );
153       }
154     });
155     verify(client, times(RETRIES)).put(anyString(), anyString(), any(byte[].class));
156   }
157 
158   @Test
159   public void testCheckAndDelete() throws Exception {
160     testTimedOutCall(new CallExecutor() {
161       @Override
162       public void run() throws Exception {
163         Put put = new Put(ROW_1);
164         put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
165         Delete delete= new Delete(ROW_1);
166         remoteTable.checkAndDelete(ROW_1, COLUMN_1, QUALIFIER_1, VALUE_1, delete);
167       }
168     });
169   }
170 
171   private void testTimedOutGetCall(CallExecutor callExecutor) throws Exception {
172     testTimedOutCall(callExecutor);
173     verify(client, times(RETRIES)).get(anyString(), anyString());
174   }
175 
176   private void testTimedOutCall(CallExecutor callExecutor) throws Exception {
177     long start = System.currentTimeMillis();
178     try {
179       callExecutor.run();
180       fail("should be timeout exception!");
181     } catch (IOException e) {
182       assertTrue(Pattern.matches(".*request timed out", e.toString()));
183     }
184     assertTrue((System.currentTimeMillis() - start) > MAX_TIME);
185   }
186 
187   private interface CallExecutor {
188     void run() throws Exception;
189   }
190 }