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  
52    private static final int SLEEP_TIME = 50;
53    private static final int RETRIES = 3;
54    private static final long MAX_TIME = SLEEP_TIME * (RETRIES - 1);
55  
56    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
57    
58    private static final byte[] ROW_1 = Bytes.toBytes("testrow1");
59    private static final byte[] COLUMN_1 = Bytes.toBytes("a");
60    private static final byte[] QUALIFIER_1 = Bytes.toBytes("1");
61    private static final byte[] VALUE_1 = Bytes.toBytes("testvalue1");
62  
63    private Client client;
64    private RemoteHTable remoteTable;
65    
66    @Before
67    public void setup() throws Exception {
68      client = mock(Client.class);
69      Response response = new Response(509);
70      when(client.get(anyString(), anyString())).thenReturn(response);
71      when(client.delete(anyString())).thenReturn(response);
72      when(client.put(anyString(), anyString(), any(byte[].class))).thenReturn(
73          response);
74      when(client.post(anyString(), anyString(), any(byte[].class))).thenReturn(
75          response);
76  
77      Configuration configuration = TEST_UTIL.getConfiguration();
78      configuration.setInt("hbase.rest.client.max.retries", RETRIES);
79      configuration.setInt("hbase.rest.client.sleep", SLEEP_TIME);
80  
81      remoteTable = new RemoteHTable(client, TEST_UTIL.getConfiguration(),
82          "MyTable");
83    }
84  
85    @After
86    public void tearDownAfterClass() throws Exception {
87      remoteTable.close();
88    }
89    
90    @Test
91    public void testDelete() throws Exception {
92      testTimedOutCall(new CallExecutor() {
93        @Override
94        public void run() throws Exception {
95          Delete delete = new Delete(Bytes.toBytes("delete"));
96          remoteTable.delete(delete);
97        }
98      });
99      verify(client, times(RETRIES)).delete(anyString());
100   }
101   
102   @Test
103   public void testGet() throws Exception {
104     testTimedOutGetCall(new CallExecutor() {
105       @Override
106       public void run() throws Exception {
107         remoteTable.get(new Get(Bytes.toBytes("Get")));
108       }
109     });
110   }
111 
112   @Test
113   public void testSingleRowPut() throws Exception {
114     testTimedOutCall(new CallExecutor() {
115       @Override
116       public void run() throws Exception {
117         remoteTable.put(new Put(Bytes.toBytes("Row")));
118       }
119     });
120     verify(client, times(RETRIES)).put(anyString(), anyString(), any(byte[].class));
121   }
122   
123   @Test
124   public void testMultiRowPut() throws Exception {
125     testTimedOutCall(new CallExecutor() {
126       @Override
127       public void run() throws Exception {
128         Put[] puts = { new Put(Bytes.toBytes("Row1")),
129             new Put(Bytes.toBytes("Row2")) };
130         remoteTable.put(Arrays.asList(puts));
131       }
132     });
133     verify(client, times(RETRIES)).put(anyString(), anyString(), any(byte[].class));
134   }
135 
136   @Test
137   public void testGetScanner() throws Exception {
138     testTimedOutCall(new CallExecutor() {
139       @Override
140       public void run() throws Exception {
141         remoteTable.getScanner(new Scan());
142       }
143     });
144     verify(client, times(RETRIES)).post(anyString(), anyString(), any(byte[].class));
145   }
146   
147   @Test
148   public void testCheckAndPut() throws Exception {
149     testTimedOutCall(new CallExecutor() {
150       @Override
151       public void run() throws Exception {
152         Put put = new Put(ROW_1);
153         put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
154         remoteTable.checkAndPut(ROW_1, COLUMN_1, QUALIFIER_1, VALUE_1, put );
155       }
156     });
157     verify(client, times(RETRIES)).put(anyString(), anyString(), any(byte[].class));
158   }
159 
160   @Test
161   public void testCheckAndDelete() throws Exception {
162     testTimedOutCall(new CallExecutor() {
163       @Override
164       public void run() throws Exception {
165         Put put = new Put(ROW_1);
166         put.add(COLUMN_1, QUALIFIER_1, VALUE_1);
167         Delete delete= new Delete(ROW_1);
168         remoteTable.checkAndDelete(ROW_1, COLUMN_1, QUALIFIER_1,  VALUE_1, delete );
169       }
170     });
171   }
172   
173   private void testTimedOutGetCall(CallExecutor callExecutor) throws Exception {
174     testTimedOutCall(callExecutor);
175     verify(client, times(RETRIES)).get(anyString(), anyString());
176   }
177   
178   private void testTimedOutCall(CallExecutor callExecutor) throws Exception {
179     long start = System.currentTimeMillis();
180     try {
181       callExecutor.run();
182       fail("should be timeout exception!");
183     } catch (IOException e) {
184       assertTrue(Pattern.matches(".*request timed out", e.toString()));
185     }
186     assertTrue((System.currentTimeMillis() - start) > MAX_TIME);
187   }
188 
189   private static interface CallExecutor {
190     void run() throws Exception;
191   }
192 
193 }