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.TestTableName;
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 public TestTableName TEST_TABLE = new TestTableName();
057
058  @Before
059  public void setUp() throws Exception {
060    TEST_UTIL.getConfiguration().set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
061      MasterSyncObserver.class.getName());
062    TEST_UTIL.startMiniCluster(1);
063  }
064
065  @After
066  public void tearDown() throws Exception {
067    TEST_UTIL.shutdownMiniCluster();
068  }
069
070  @Test(expected=SnapshotExistsException.class)
071  public void testSnapshotAlreadyExist() throws Exception {
072    final String snapshotName = "testSnapshotAlreadyExist";
073    TEST_UTIL.createTable(TEST_TABLE.getTableName(), "f");
074    TEST_UTIL.getAdmin().snapshot(snapshotName, TEST_TABLE.getTableName());
075    snapshotAndAssertOneRetry(snapshotName, TEST_TABLE.getTableName());
076  }
077
078  @Test(expected=SnapshotDoesNotExistException.class)
079  public void testCloneNonExistentSnapshot() throws Exception {
080    final String snapshotName = "testCloneNonExistentSnapshot";
081    cloneAndAssertOneRetry(snapshotName, TEST_TABLE.getTableName());
082  }
083
084  public static class MasterSyncObserver implements MasterCoprocessor, MasterObserver {
085    volatile AtomicInteger snapshotCount = null;
086    volatile AtomicInteger cloneCount = null;
087
088    @Override
089    public Optional<MasterObserver> getMasterObserver() {
090      return Optional.of(this);
091    }
092
093    @Override
094    public void preSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
095        final SnapshotDescription snapshot, final TableDescriptor hTableDescriptor)
096        throws IOException {
097      if (snapshotCount != null) {
098        snapshotCount.incrementAndGet();
099      }
100    }
101
102    @Override
103    public void preCloneSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
104        final SnapshotDescription snapshot, final TableDescriptor hTableDescriptor)
105        throws IOException {
106      if (cloneCount != null) {
107        cloneCount.incrementAndGet();
108      }
109    }
110  }
111
112  public void snapshotAndAssertOneRetry(final String snapshotName, final TableName tableName)
113      throws Exception {
114    MasterSyncObserver observer = getMasterSyncObserver();
115    observer.snapshotCount = new AtomicInteger(0);
116    TEST_UTIL.getAdmin().snapshot(snapshotName, tableName);
117    assertEquals(1, observer.snapshotCount.get());
118  }
119
120  public void cloneAndAssertOneRetry(final String snapshotName, final TableName tableName)
121      throws Exception {
122    MasterSyncObserver observer = getMasterSyncObserver();
123    observer.cloneCount = new AtomicInteger(0);
124    TEST_UTIL.getAdmin().cloneSnapshot(snapshotName, tableName);
125    assertEquals(1, observer.cloneCount.get());
126  }
127
128  private MasterSyncObserver getMasterSyncObserver() {
129    return (MasterSyncObserver)TEST_UTIL.getHBaseCluster().getMaster()
130      .getMasterCoprocessorHost().findCoprocessor(MasterSyncObserver.class.getName());
131  }
132}