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.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  import java.io.IOException;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.HBaseConfiguration;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.SmallTests;
31  import org.apache.hadoop.hbase.TableName;
32  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
33  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.IsSnapshotDoneRequest;
34  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.IsSnapshotDoneResponse;
35  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.SnapshotRequest;
36  import org.apache.hadoop.hbase.protobuf.generated.MasterProtos.SnapshotResponse;
37  import org.junit.Test;
38  import org.junit.experimental.categories.Category;
39  import org.mockito.Mockito;
40  
41  import com.google.protobuf.RpcController;
42  
43  /**
44   * Test snapshot logic from the client
45   */
46  @Category(SmallTests.class)
47  public class TestSnapshotFromAdmin {
48  
49    private static final Log LOG = LogFactory.getLog(TestSnapshotFromAdmin.class);
50  
51    /**
52     * Test that the logic for doing 'correct' back-off based on exponential increase and the max-time
53     * passed from the server ensures the correct overall waiting for the snapshot to finish.
54     * @throws Exception
55     */
56    @Test(timeout = 60000)
57    public void testBackoffLogic() throws Exception {
58      final int pauseTime = 100;
59      final int maxWaitTime =
60        HConstants.RETRY_BACKOFF[HConstants.RETRY_BACKOFF.length - 1] * pauseTime;
61      final int numRetries = HConstants.RETRY_BACKOFF.length;
62      // calculate the wait time, if we just do straight backoff (ignoring the expected time from
63      // master)
64      long ignoreExpectedTime = 0;
65      for (int i = 0; i < HConstants.RETRY_BACKOFF.length; i++) {
66        ignoreExpectedTime += HConstants.RETRY_BACKOFF[i] * pauseTime;
67      }
68      // the correct wait time, capping at the maxTime/tries + fudge room
69      final long time = pauseTime * 3 + ((maxWaitTime / numRetries) * 3) + 300;
70      assertTrue("Capped snapshot wait time isn't less that the uncapped backoff time "
71          + "- further testing won't prove anything.", time < ignoreExpectedTime);
72  
73      // setup the mocks
74      HConnectionManager.HConnectionImplementation mockConnection = Mockito
75          .mock(HConnectionManager.HConnectionImplementation.class);
76      Configuration conf = HBaseConfiguration.create();
77      // setup the conf to match the expected properties
78      conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, numRetries);
79      conf.setLong("hbase.client.pause", pauseTime);
80      // mock the master admin to our mock
81      MasterKeepAliveConnection mockMaster = Mockito.mock(MasterKeepAliveConnection.class);
82      Mockito.when(mockConnection.getConfiguration()).thenReturn(conf);
83      Mockito.when(mockConnection.getKeepAliveMasterService()).thenReturn(mockMaster);
84      // set the max wait time for the snapshot to complete
85      SnapshotResponse response = SnapshotResponse.newBuilder()
86          .setExpectedTimeout(maxWaitTime)
87          .build();
88      Mockito
89          .when(
90            mockMaster.snapshot((RpcController) Mockito.isNull(),
91              Mockito.any(SnapshotRequest.class))).thenReturn(response);
92      // setup the response
93      IsSnapshotDoneResponse.Builder builder = IsSnapshotDoneResponse.newBuilder();
94      builder.setDone(false);
95      // first five times, we return false, last we get success
96      Mockito.when(
97        mockMaster.isSnapshotDone((RpcController) Mockito.isNull(),
98          Mockito.any(IsSnapshotDoneRequest.class))).thenReturn(builder.build(), builder.build(),
99        builder.build(), builder.build(), builder.build(), builder.setDone(true).build());
100 
101     // setup the admin and run the test
102     HBaseAdmin admin = new HBaseAdmin(mockConnection);
103     String snapshot = "snapshot";
104     TableName table = TableName.valueOf("table");
105     // get start time
106     long start = System.currentTimeMillis();
107     admin.snapshot(snapshot, table);
108     long finish = System.currentTimeMillis();
109     long elapsed = (finish - start);
110     assertTrue("Elapsed time:" + elapsed + " is more than expected max:" + time, elapsed <= time);
111     admin.close();
112   }
113 
114   /**
115    * Make sure that we validate the snapshot name and the table name before we pass anything across
116    * the wire
117    * @throws Exception on failure
118    */
119   @Test
120   public void testValidateSnapshotName() throws Exception {
121     HConnectionManager.HConnectionImplementation mockConnection = Mockito
122         .mock(HConnectionManager.HConnectionImplementation.class);
123     Configuration conf = HBaseConfiguration.create();
124     Mockito.when(mockConnection.getConfiguration()).thenReturn(conf);
125     HBaseAdmin admin = new HBaseAdmin(mockConnection);
126     SnapshotDescription.Builder builder = SnapshotDescription.newBuilder();
127     // check that invalid snapshot names fail
128     failSnapshotStart(admin, builder.setName(HConstants.SNAPSHOT_DIR_NAME).build());
129     failSnapshotStart(admin, builder.setName("-snapshot").build());
130     failSnapshotStart(admin, builder.setName("snapshot fails").build());
131     failSnapshotStart(admin, builder.setName("snap$hot").build());
132     failSnapshotStart(admin, builder.setName("snap:hot").build());
133     // check the table name also get verified
134     failSnapshotStart(admin, builder.setName("snapshot").setTable(".table").build());
135     failSnapshotStart(admin, builder.setName("snapshot").setTable("-table").build());
136     failSnapshotStart(admin, builder.setName("snapshot").setTable("table fails").build());
137     failSnapshotStart(admin, builder.setName("snapshot").setTable("tab%le").build());
138 
139     // mock the master connection
140     MasterKeepAliveConnection master = Mockito.mock(MasterKeepAliveConnection.class);
141     Mockito.when(mockConnection.getKeepAliveMasterService()).thenReturn(master);
142     SnapshotResponse response = SnapshotResponse.newBuilder().setExpectedTimeout(0).build();
143     Mockito.when(
144       master.snapshot((RpcController) Mockito.isNull(), Mockito.any(SnapshotRequest.class)))
145         .thenReturn(response);
146     IsSnapshotDoneResponse doneResponse = IsSnapshotDoneResponse.newBuilder().setDone(true).build();
147     Mockito.when(
148       master.isSnapshotDone((RpcController) Mockito.isNull(),
149           Mockito.any(IsSnapshotDoneRequest.class))).thenReturn(doneResponse);
150 
151       // make sure that we can use valid names
152     admin.snapshot(builder.setName("snapshot").setTable("table").build());
153   }
154 
155   private void failSnapshotStart(HBaseAdmin admin, SnapshotDescription snapshot) throws IOException {
156     try {
157       admin.snapshot(snapshot);
158       fail("Snapshot should not have succeed with name:" + snapshot.getName());
159     } catch (IllegalArgumentException e) {
160       LOG.debug("Correctly failed to start snapshot:" + e.getMessage());
161     }
162   }
163 }