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.ipc.HMasterInterface;
32  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
33  import org.apache.hadoop.hbase.snapshot.HSnapshotDescription;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  import org.mockito.Mockito;
37  
38  import com.google.protobuf.RpcController;
39  
40  /**
41   * Test snapshot logic from the client
42   */
43  @Category(SmallTests.class)
44  public class TestSnapshotsFromAdmin {
45  
46    private static final Log LOG = LogFactory.getLog(TestSnapshotsFromAdmin.class);
47  
48    /**
49     * Test that the logic for doing 'correct' back-off based on exponential increase and the max-time
50     * passed from the server ensures the correct overall waiting for the snapshot to finish.
51     * @throws Exception
52     */
53    @Test(timeout = 10000)
54    public void testBackoffLogic() throws Exception {
55      final int maxWaitTime = 7500;
56      final int numRetries = 10;
57      final int pauseTime = 500;
58      // calculate the wait time, if we just do straight backoff (ignoring the expected time from
59      // master)
60      long ignoreExpectedTime = 0;
61      for (int i = 0; i < 6; i++) {
62        ignoreExpectedTime += HConstants.RETRY_BACKOFF[i] * pauseTime;
63      }
64      // the correct wait time, capping at the maxTime/tries + fudge room
65      final long time = pauseTime * 3 + ((maxWaitTime / numRetries) * 3) + 300;
66      assertTrue("Capped snapshot wait time isn't less that the uncapped backoff time "
67          + "- further testing won't prove anything.", time < ignoreExpectedTime);
68  
69      // setup the mocks
70      HConnectionManager.HConnectionImplementation mockConnection = Mockito
71          .mock(HConnectionManager.HConnectionImplementation.class);
72      Configuration conf = HBaseConfiguration.create();
73      // setup the conf to match the expected properties
74      conf.setInt("hbase.client.retries.number", numRetries);
75      conf.setLong("hbase.client.pause", pauseTime);
76      // mock the master admin to our mock
77      HMasterInterface mockMaster = Mockito.mock(HMasterInterface.class);
78      Mockito.when(mockConnection.getConfiguration()).thenReturn(conf);
79      Mockito.when(mockConnection.getMaster()).thenReturn(mockMaster);
80      // set the max wait time for the snapshot to complete
81      Mockito
82          .when(
83            mockMaster.snapshot(
84              Mockito.any(HSnapshotDescription.class))).thenReturn((long)maxWaitTime);
85      // first five times, we return false, last we get success
86      Mockito.when(
87        mockMaster.isSnapshotDone(
88          Mockito.any(HSnapshotDescription.class))).thenReturn(false, false,
89            false, false, false, true);
90  
91      // setup the admin and run the test
92      HBaseAdmin admin = new HBaseAdmin(mockConnection);
93      String snapshot = "snasphot";
94      String table = "table";
95      // get start time
96      long start = System.currentTimeMillis();
97      admin.snapshot(snapshot, table);
98      long finish = System.currentTimeMillis();
99      long elapsed = (finish - start);
100     assertTrue("Elapsed time:" + elapsed + " is more than expected max:" + time, elapsed <= time);
101   }
102 
103   /**
104    * Make sure that we validate the snapshot name and the table name before we pass anything across
105    * the wire
106    * @throws IOException on failure
107    */
108   @Test
109   public void testValidateSnapshotName() throws IOException {
110     HConnectionManager.HConnectionImplementation mockConnection = Mockito
111         .mock(HConnectionManager.HConnectionImplementation.class);
112     Configuration conf = HBaseConfiguration.create();
113     Mockito.when(mockConnection.getConfiguration()).thenReturn(conf);
114     HBaseAdmin admin = new HBaseAdmin(mockConnection);
115     SnapshotDescription.Builder builder = SnapshotDescription.newBuilder();
116     // check that invalid snapshot names fail
117     failSnapshotStart(admin, builder.setName(".snapshot").build());
118     failSnapshotStart(admin, builder.setName("-snapshot").build());
119     failSnapshotStart(admin, builder.setName("snapshot fails").build());
120     failSnapshotStart(admin, builder.setName("snap$hot").build());
121     // check the table name also get verified
122     failSnapshotStart(admin, builder.setName("snapshot").setTable(".table").build());
123     failSnapshotStart(admin, builder.setName("snapshot").setTable("-table").build());
124     failSnapshotStart(admin, builder.setName("snapshot").setTable("table fails").build());
125     failSnapshotStart(admin, builder.setName("snapshot").setTable("tab%le").build());
126   }
127 
128   private void failSnapshotStart(HBaseAdmin admin, SnapshotDescription snapshot) throws IOException {
129     try {
130       admin.snapshot(snapshot);
131       fail("Snapshot should not have succeed with name:" + snapshot.getName());
132     } catch (IllegalArgumentException e) {
133       LOG.debug("Correctly failed to start snapshot:" + e.getMessage());
134     }
135   }
136 }