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.master.procedure;
019
020import static org.junit.jupiter.api.Assertions.assertThrows;
021import static org.junit.jupiter.api.Assertions.fail;
022
023import java.io.IOException;
024import org.apache.hadoop.hbase.client.SnapshotDescription;
025import org.apache.hadoop.hbase.client.SnapshotType;
026import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
027import org.apache.hadoop.hbase.testclassification.LargeTests;
028import org.apache.hadoop.hbase.testclassification.MasterTests;
029import org.junit.jupiter.api.Tag;
030import org.junit.jupiter.api.Test;
031
032@Tag(MasterTests.TAG)
033@Tag(LargeTests.TAG)
034public class TestSnapshotProcedureBasicSnapshot extends TestSnapshotProcedure {
035
036  @Test
037  public void testSimpleSnapshotTable() throws Exception {
038    TEST_UTIL.getAdmin().snapshot(snapshot);
039    SnapshotTestingUtils.assertOneSnapshotThatMatches(TEST_UTIL.getAdmin(), snapshotProto);
040    SnapshotTestingUtils.confirmSnapshotValid(TEST_UTIL, snapshotProto, TABLE_NAME, CF);
041  }
042
043  @Test
044  public void testClientTakingTwoSnapshotOnSameTable() throws Exception {
045    Thread first = new Thread("first-client") {
046      @Override
047      public void run() {
048        try {
049          TEST_UTIL.getAdmin().snapshot(snapshot);
050        } catch (IOException e) {
051          LOG.error("first client failed taking snapshot", e);
052          fail("first client failed taking snapshot");
053        }
054      }
055    };
056    first.start();
057    Thread.sleep(1000);
058    // we don't allow different snapshot with same name
059    SnapshotDescription snapshotWithSameName =
060      new SnapshotDescription(SNAPSHOT_NAME, TABLE_NAME, SnapshotType.SKIPFLUSH);
061    assertThrows(org.apache.hadoop.hbase.snapshot.SnapshotCreationException.class,
062      () -> TEST_UTIL.getAdmin().snapshot(snapshotWithSameName));
063  }
064
065  @Test
066  public void testClientTakeSameSnapshotTwice() throws IOException, InterruptedException {
067    Thread first = new Thread("first-client") {
068      @Override
069      public void run() {
070        try {
071          TEST_UTIL.getAdmin().snapshot(snapshot);
072        } catch (IOException e) {
073          LOG.error("first client failed taking snapshot", e);
074          fail("first client failed taking snapshot");
075        }
076      }
077    };
078    first.start();
079    Thread.sleep(1000);
080    assertThrows(org.apache.hadoop.hbase.snapshot.SnapshotCreationException.class,
081      () -> TEST_UTIL.getAdmin().snapshot(snapshot));
082  }
083}