View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.snapshot;
20  
21  import java.io.IOException;
22  import java.util.concurrent.atomic.AtomicInteger;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.HTableDescriptor;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.coprocessor.BaseMasterObserver;
30  import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
31  import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
32  import org.apache.hadoop.hbase.coprocessor.ObserverContext;
33  import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.SnapshotDescription;
34  import org.apache.hadoop.hbase.snapshot.SnapshotExistsException;
35  import org.apache.hadoop.hbase.snapshot.SnapshotDoesNotExistException;
36  import org.apache.hadoop.hbase.testclassification.MediumTests;
37  import org.apache.hadoop.hbase.util.TestTableName;
38  
39  import org.junit.After;
40  import org.junit.Before;
41  import org.junit.Rule;
42  import org.junit.Test;
43  import org.junit.experimental.categories.Category;
44  
45  import static org.junit.Assert.assertEquals;
46  import static org.junit.Assert.assertTrue;
47  import static org.junit.Assert.fail;
48  
49  @Category({ MediumTests.class })
50  public class TestSnapshotClientRetries {
51    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
52    private static final Log LOG = LogFactory.getLog(TestSnapshotClientRetries.class);
53  
54    @Rule public TestTableName TEST_TABLE = new TestTableName();
55  
56    @Before
57    public void setUp() throws Exception {
58      TEST_UTIL.getConfiguration().set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
59        MasterSyncObserver.class.getName());
60      TEST_UTIL.startMiniCluster(1);
61    }
62  
63    @After
64    public void tearDown() throws Exception {
65      TEST_UTIL.shutdownMiniCluster();
66    }
67  
68    @Test(timeout = 60000, expected=SnapshotExistsException.class)
69    public void testSnapshotAlreadyExist() throws Exception {
70      final String snapshotName = "testSnapshotAlreadyExist";
71      TEST_UTIL.createTable(TEST_TABLE.getTableName(), "f");
72      TEST_UTIL.getHBaseAdmin().snapshot(snapshotName, TEST_TABLE.getTableName());
73      snapshotAndAssertOneRetry(snapshotName, TEST_TABLE.getTableName());
74    }
75  
76    @Test(timeout = 60000, expected=SnapshotDoesNotExistException.class)
77    public void testCloneNonExistentSnapshot() throws Exception {
78      final String snapshotName = "testCloneNonExistentSnapshot";
79      cloneAndAssertOneRetry(snapshotName, TEST_TABLE.getTableName());
80    }
81  
82    public static class MasterSyncObserver extends BaseMasterObserver {
83      volatile AtomicInteger snapshotCount = null;
84      volatile AtomicInteger cloneCount = null;
85  
86      @Override
87      public void preSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
88          final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor)
89          throws IOException {
90        if (snapshotCount != null) {
91          snapshotCount.incrementAndGet();
92        }
93      }
94  
95      @Override
96      public void preCloneSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
97          final SnapshotDescription snapshot, final HTableDescriptor hTableDescriptor)
98          throws IOException {
99        if (cloneCount != null) {
100         cloneCount.incrementAndGet();
101       }
102     }
103   }
104 
105   public void snapshotAndAssertOneRetry(final String snapshotName, final TableName tableName)
106       throws Exception {
107     MasterSyncObserver observer = getMasterSyncObserver();
108     observer.snapshotCount = new AtomicInteger(0);
109     TEST_UTIL.getHBaseAdmin().snapshot(snapshotName, tableName);
110     assertEquals(1, observer.snapshotCount.get());
111   }
112 
113   public void cloneAndAssertOneRetry(final String snapshotName, final TableName tableName)
114       throws Exception {
115     MasterSyncObserver observer = getMasterSyncObserver();
116     observer.cloneCount = new AtomicInteger(0);
117     TEST_UTIL.getHBaseAdmin().cloneSnapshot(snapshotName, tableName);
118     assertEquals(1, observer.cloneCount.get());
119   }
120 
121   private MasterSyncObserver getMasterSyncObserver() {
122     return (MasterSyncObserver)TEST_UTIL.getHBaseCluster().getMaster()
123       .getMasterCoprocessorHost().findCoprocessor(MasterSyncObserver.class.getName());
124   }
125 }