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.client;
19  
20  import static org.junit.Assert.fail;
21  
22  import java.io.IOException;
23  
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HBaseConfiguration;
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.HConstants;
28  import org.apache.hadoop.hbase.HTableDescriptor;
29  import org.apache.hadoop.hbase.MasterNotRunningException;
30  import org.apache.hadoop.hbase.PleaseHoldException;
31  import org.apache.hadoop.hbase.TableName;
32  import org.apache.hadoop.hbase.ZooKeeperConnectionException;
33  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.CreateTableRequest;
34  import org.apache.hadoop.hbase.testclassification.SmallTests;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  import org.mockito.Mockito;
38  import org.mortbay.log.Log;
39  
40  import com.google.protobuf.RpcController;
41  import com.google.protobuf.ServiceException;
42  
43  @Category(SmallTests.class)
44  public class TestHBaseAdminNoCluster {
45    /**
46     * Verify that PleaseHoldException gets retried.
47     * HBASE-8764
48     * @throws IOException 
49     * @throws ZooKeeperConnectionException 
50     * @throws MasterNotRunningException 
51     * @throws ServiceException 
52     */
53    @Test
54    public void testMasterMonitorCollableRetries()
55    throws MasterNotRunningException, ZooKeeperConnectionException, IOException, ServiceException {
56      Configuration configuration = HBaseConfiguration.create();
57      // Set the pause and retry count way down.
58      configuration.setLong(HConstants.HBASE_CLIENT_PAUSE, 1);
59      final int count = 10;
60      configuration.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, count);
61      // Get mocked connection.   Getting the connection will register it so when HBaseAdmin is
62      // constructed with same configuration, it will find this mocked connection.
63      HConnection connection = HConnectionTestingUtility.getMockedConnection(configuration);
64      // Mock so we get back the master interface.  Make it so when createTable is called, we throw
65      // the PleaseHoldException.
66      MasterKeepAliveConnection masterAdmin =
67        Mockito.mock(MasterKeepAliveConnection.class);
68      Mockito.when(masterAdmin.createTable((RpcController)Mockito.any(),
69        (CreateTableRequest)Mockito.any())).
70          thenThrow(new ServiceException("Test fail").initCause(new PleaseHoldException("test")));
71      Mockito.when(connection.getKeepAliveMasterService()).thenReturn(masterAdmin);
72      // Mock up our admin Interfaces
73      HBaseAdmin admin = new HBaseAdmin(configuration);
74      try {
75        HTableDescriptor htd =
76            new HTableDescriptor(TableName.valueOf("testMasterMonitorCollableRetries"));
77        // Pass any old htable descriptor; not important
78        try {
79          admin.createTable(htd, HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE);
80          fail();
81        } catch (RetriesExhaustedException e) {
82          Log.info("Expected fail", e);
83        }
84        // Assert we were called 'count' times.
85        Mockito.verify(masterAdmin, Mockito.atLeast(count)).createTable((RpcController)Mockito.any(),
86          (CreateTableRequest)Mockito.any());
87      } finally {
88        admin.close();
89        if (connection != null)HConnectionManager.deleteConnection(configuration);
90      }
91    }
92  }