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.assertTrue;
021import java.net.URI;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.fs.FileSystem;
024import org.apache.hadoop.fs.LocalFileSystem;
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseCommonTestingUtility;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
031import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils.SnapshotMock;
032import org.apache.hadoop.hbase.testclassification.MapReduceTests;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.junit.Before;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039import org.slf4j.Logger;
040import org.slf4j.LoggerFactory;
041
042/**
043 * Test Export Snapshot Tool
044 */
045@Category({MapReduceTests.class, MediumTests.class})
046public class TestExportSnapshotNoCluster {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050      HBaseClassTestRule.forClass(TestExportSnapshotNoCluster.class);
051
052  private static final Logger LOG = LoggerFactory.getLogger(TestExportSnapshotNoCluster.class);
053
054  private final HBaseCommonTestingUtility testUtil = new HBaseCommonTestingUtility();
055  private FileSystem fs;
056  private Path testDir;
057
058  public void setUpBaseConf(Configuration conf) {
059    conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
060    conf.setInt("hbase.regionserver.msginterval", 100);
061    conf.setInt("hbase.client.pause", 250);
062    conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6);
063    conf.setBoolean("hbase.master.enabletable.roundrobin", true);
064    conf.setInt("mapreduce.map.maxattempts", 10);
065    conf.set(HConstants.HBASE_DIR, testDir.toString());
066  }
067
068  @Before
069  public void setUpBefore() throws Exception {
070    // Make sure testDir is on LocalFileSystem
071    testDir = testUtil.getDataTestDir().makeQualified(URI.create("file:///"), new Path("/"));
072    fs = testDir.getFileSystem(testUtil.getConfiguration());
073    assertTrue("FileSystem '" + fs + "' is not local", fs instanceof LocalFileSystem);
074
075    setUpBaseConf(testUtil.getConfiguration());
076  }
077
078  @Test
079  public void testSnapshotV1WithRefsExportFileSystemState() throws Exception {
080    final SnapshotMock snapshotMock = new SnapshotMock(testUtil.getConfiguration(), fs, testDir);
081    final SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV1("tableWithRefsV1",
082      "tableWithRefsV1");
083    testSnapshotWithRefsExportFileSystemState(builder);
084  }
085
086  @Test
087  public void testSnapshotV2WithRefsExportFileSystemState() throws Exception {
088    final SnapshotMock snapshotMock = new SnapshotMock(testUtil.getConfiguration(), fs, testDir);
089    final SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV2("tableWithRefsV2",
090      "tableWithRefsV2");
091    testSnapshotWithRefsExportFileSystemState(builder);
092  }
093
094  /**
095   * Generates a couple of regions for the specified SnapshotMock,
096   * and then it will run the export and verification.
097   */
098  private void testSnapshotWithRefsExportFileSystemState(SnapshotMock.SnapshotBuilder builder)
099      throws Exception {
100    Path[] r1Files = builder.addRegion();
101    Path[] r2Files = builder.addRegion();
102    builder.commit();
103    int snapshotFilesCount = r1Files.length + r2Files.length;
104
105    byte[] snapshotName = Bytes.toBytes(builder.getSnapshotDescription().getName());
106    TableName tableName = builder.getTableDescriptor().getTableName();
107    TestExportSnapshot.testExportFileSystemState(testUtil.getConfiguration(),
108      tableName, snapshotName, snapshotName, snapshotFilesCount,
109      testDir, getDestinationDir(), false, null, true);
110  }
111
112  private Path getDestinationDir() {
113    Path path = new Path(new Path(testDir, "export-test"), "export-" + System.currentTimeMillis());
114    LOG.info("HDFS export destination path: " + path);
115    return path;
116  }
117}