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