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