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