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