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.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertThrows;
022
023import java.io.IOException;
024import java.util.Optional;
025import java.util.concurrent.atomic.AtomicInteger;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.SnapshotDescription;
029import org.apache.hadoop.hbase.client.TableDescriptor;
030import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
031import org.apache.hadoop.hbase.coprocessor.MasterCoprocessor;
032import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
033import org.apache.hadoop.hbase.coprocessor.MasterObserver;
034import org.apache.hadoop.hbase.coprocessor.ObserverContext;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.junit.jupiter.api.AfterEach;
037import org.junit.jupiter.api.BeforeEach;
038import org.junit.jupiter.api.Tag;
039import org.junit.jupiter.api.Test;
040import org.junit.jupiter.api.TestInfo;
041import org.slf4j.Logger;
042import org.slf4j.LoggerFactory;
043
044@Tag(MediumTests.TAG)
045public class TestSnapshotClientRetries {
046
047  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
048  private static final Logger LOG = LoggerFactory.getLogger(TestSnapshotClientRetries.class);
049
050  @BeforeEach
051  public void setUp() throws Exception {
052    TEST_UTIL.getConfiguration().set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
053      MasterSyncObserver.class.getName());
054    TEST_UTIL.startMiniCluster(1);
055  }
056
057  @AfterEach
058  public void tearDown() throws Exception {
059    TEST_UTIL.shutdownMiniCluster();
060  }
061
062  @Test
063  public void testSnapshotAlreadyExist(TestInfo testInfo) throws Exception {
064    final String snapshotName = "testSnapshotAlreadyExist";
065    TableName tableName = TableName.valueOf(testInfo.getTestMethod().get().getName());
066    TEST_UTIL.createTable(tableName, "f");
067    TEST_UTIL.getAdmin().snapshot(snapshotName, tableName);
068    assertThrows(SnapshotExistsException.class,
069      () -> snapshotAndAssertOneRetry(snapshotName, tableName));
070  }
071
072  @Test
073  public void testCloneNonExistentSnapshot(TestInfo testInfo) throws Exception {
074    final String snapshotName = "testCloneNonExistentSnapshot";
075    TableName tableName = TableName.valueOf(testInfo.getTestMethod().get().getName());
076    assertThrows(SnapshotDoesNotExistException.class,
077      () -> cloneAndAssertOneRetry(snapshotName, tableName));
078  }
079
080  public static class MasterSyncObserver implements MasterCoprocessor, MasterObserver {
081    volatile AtomicInteger snapshotCount = null;
082    volatile AtomicInteger cloneCount = null;
083
084    @Override
085    public Optional<MasterObserver> getMasterObserver() {
086      return Optional.of(this);
087    }
088
089    @Override
090    public void preSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
091      final SnapshotDescription snapshot, final TableDescriptor hTableDescriptor)
092      throws IOException {
093      if (snapshotCount != null) {
094        snapshotCount.incrementAndGet();
095      }
096    }
097
098    @Override
099    public void preCloneSnapshot(final ObserverContext<MasterCoprocessorEnvironment> ctx,
100      final SnapshotDescription snapshot, final TableDescriptor hTableDescriptor)
101      throws IOException {
102      if (cloneCount != null) {
103        cloneCount.incrementAndGet();
104      }
105    }
106  }
107
108  public void snapshotAndAssertOneRetry(final String snapshotName, final TableName tableName)
109    throws Exception {
110    MasterSyncObserver observer = getMasterSyncObserver();
111    observer.snapshotCount = new AtomicInteger(0);
112    TEST_UTIL.getAdmin().snapshot(snapshotName, tableName);
113    assertEquals(1, observer.snapshotCount.get());
114  }
115
116  public void cloneAndAssertOneRetry(final String snapshotName, final TableName tableName)
117    throws Exception {
118    MasterSyncObserver observer = getMasterSyncObserver();
119    observer.cloneCount = new AtomicInteger(0);
120    TEST_UTIL.getAdmin().cloneSnapshot(snapshotName, tableName);
121    assertEquals(1, observer.cloneCount.get());
122  }
123
124  private MasterSyncObserver getMasterSyncObserver() {
125    return (MasterSyncObserver) TEST_UTIL.getHBaseCluster().getMaster().getMasterCoprocessorHost()
126      .findCoprocessor(MasterSyncObserver.class.getName());
127  }
128}