001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.snapshot;
019
020import static org.junit.Assert.assertEquals;
021
022import java.io.IOException;
023import java.util.Optional;
024import java.util.concurrent.atomic.AtomicInteger;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtility;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.TableNameTestRule;
029import org.apache.hadoop.hbase.client.SnapshotDescription;
030import org.apache.hadoop.hbase.client.TableDescriptor;
031import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
032import org.apache.hadoop.hbase.coprocessor.MasterCoprocessor;
033import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
034import org.apache.hadoop.hbase.coprocessor.MasterObserver;
035import org.apache.hadoop.hbase.coprocessor.ObserverContext;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.junit.After;
038import org.junit.Before;
039import org.junit.ClassRule;
040import org.junit.Rule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043import org.slf4j.Logger;
044import org.slf4j.LoggerFactory;
045
046@Category({ MediumTests.class })
047public class TestSnapshotClientRetries {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051    HBaseClassTestRule.forClass(TestSnapshotClientRetries.class);
052
053  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
054  private static final Logger LOG = LoggerFactory.getLogger(TestSnapshotClientRetries.class);
055
056  @Rule
057  public TableNameTestRule testTable = new TableNameTestRule();
058
059  @Before
060  public void setUp() throws Exception {
061    TEST_UTIL.getConfiguration().set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
062      MasterSyncObserver.class.getName());
063    TEST_UTIL.startMiniCluster(1);
064  }
065
066  @After
067  public void tearDown() throws Exception {
068    TEST_UTIL.shutdownMiniCluster();
069  }
070
071  @Test(expected = SnapshotExistsException.class)
072  public void testSnapshotAlreadyExist() throws Exception {
073    final String snapshotName = "testSnapshotAlreadyExist";
074    TEST_UTIL.createTable(testTable.getTableName(), "f");
075    TEST_UTIL.getAdmin().snapshot(snapshotName, testTable.getTableName());
076    snapshotAndAssertOneRetry(snapshotName, testTable.getTableName());
077  }
078
079  @Test(expected = SnapshotDoesNotExistException.class)
080  public void testCloneNonExistentSnapshot() throws Exception {
081    final String snapshotName = "testCloneNonExistentSnapshot";
082    cloneAndAssertOneRetry(snapshotName, testTable.getTableName());
083  }
084
085  public static class MasterSyncObserver implements MasterCoprocessor, MasterObserver {
086    volatile AtomicInteger snapshotCount = null;
087    volatile AtomicInteger cloneCount = null;
088
089    @Override
090    public Optional<MasterObserver> getMasterObserver() {
091      return Optional.of(this);
092    }
093
094    @Override
095    public void preSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
096      final SnapshotDescription snapshot, final TableDescriptor hTableDescriptor)
097      throws IOException {
098      if (snapshotCount != null) {
099        snapshotCount.incrementAndGet();
100      }
101    }
102
103    @Override
104    public void preCloneSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
105      final SnapshotDescription snapshot, final TableDescriptor hTableDescriptor)
106      throws IOException {
107      if (cloneCount != null) {
108        cloneCount.incrementAndGet();
109      }
110    }
111  }
112
113  public void snapshotAndAssertOneRetry(final String snapshotName, final TableName tableName)
114    throws Exception {
115    MasterSyncObserver observer = getMasterSyncObserver();
116    observer.snapshotCount = new AtomicInteger(0);
117    TEST_UTIL.getAdmin().snapshot(snapshotName, tableName);
118    assertEquals(1, observer.snapshotCount.get());
119  }
120
121  public void cloneAndAssertOneRetry(final String snapshotName, final TableName tableName)
122    throws Exception {
123    MasterSyncObserver observer = getMasterSyncObserver();
124    observer.cloneCount = new AtomicInteger(0);
125    TEST_UTIL.getAdmin().cloneSnapshot(snapshotName, tableName);
126    assertEquals(1, observer.cloneCount.get());
127  }
128
129  private MasterSyncObserver getMasterSyncObserver() {
130    return (MasterSyncObserver) TEST_UTIL.getHBaseCluster().getMaster().getMasterCoprocessorHost()
131      .findCoprocessor(MasterSyncObserver.class.getName());
132  }
133}