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.io.IOException;
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.HBaseCommonTestingUtil;
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.EnvironmentEdgeManager;
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 * Tests V1 snapshots only. Used to ALSO test v2 but strange failure so separate the tests.
045 * See companion file for test of v2 snapshot.
046 * @see TestExportSnapshotV2NoCluster
047 */
048@Category({MapReduceTests.class, MediumTests.class})
049public class TestExportSnapshotV1NoCluster {
050  @ClassRule
051  public static final HBaseClassTestRule CLASS_RULE =
052      HBaseClassTestRule.forClass(TestExportSnapshotV1NoCluster.class);
053  private static final Logger LOG = LoggerFactory.getLogger(TestExportSnapshotV1NoCluster.class);
054
055  private HBaseCommonTestingUtil testUtil = new HBaseCommonTestingUtil();
056  private Path testDir;
057  private FileSystem fs;
058
059  @Before
060  public void setUpBefore() throws Exception {
061    // Make sure testDir is on LocalFileSystem
062    this.fs = FileSystem.getLocal(this.testUtil.getConfiguration());
063    this.testDir = setup(fs, this.testUtil);
064    LOG.info("fs={}, fsuri={}, fswd={}, testDir={}", this.fs, this.fs.getUri(),
065      this.fs.getWorkingDirectory(), this.testDir);
066    assertTrue("FileSystem '" + fs + "' is not local", fs instanceof LocalFileSystem);
067  }
068
069  /**
070   * Setup for test. Returns path to test data dir. Sets configuration into the passed
071   * hctu.getConfiguration.
072   */
073  static Path setup(FileSystem fs, HBaseCommonTestingUtil hctu) throws IOException {
074    Path testDir = hctu.getDataTestDir().makeQualified(fs.getUri(), fs.getWorkingDirectory());
075    hctu.getConfiguration().setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
076    hctu.getConfiguration().setInt("hbase.regionserver.msginterval", 100);
077    hctu.getConfiguration().setInt("hbase.client.pause", 250);
078    hctu.getConfiguration().setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6);
079    hctu.getConfiguration().setBoolean("hbase.master.enabletable.roundrobin", true);
080    hctu.getConfiguration().setInt("mapreduce.map.maxattempts", 10);
081    hctu.getConfiguration().set(HConstants.HBASE_DIR, testDir.toString());
082    return testDir.makeQualified(fs.getUri(), fs.getWorkingDirectory());
083  }
084
085  /**
086   * V1 snapshot test
087   */
088  @Test
089  public void testSnapshotWithRefsExportFileSystemState() throws Exception {
090    final SnapshotMock snapshotMock = new SnapshotMock(testUtil.getConfiguration(),
091      this.fs, testDir);
092    final SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV1("tableWithRefsV1",
093      "tableWithRefsV1");
094    testSnapshotWithRefsExportFileSystemState(this.fs, builder, testUtil, testDir);
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  static void testSnapshotWithRefsExportFileSystemState(FileSystem fs,
102     SnapshotMock.SnapshotBuilder builder, HBaseCommonTestingUtil testUtil, Path testDir)
103        throws Exception {
104    Path[] r1Files = builder.addRegion();
105    Path[] r2Files = builder.addRegion();
106    builder.commit();
107    int snapshotFilesCount = r1Files.length + r2Files.length;
108    String snapshotName = builder.getSnapshotDescription().getName();
109    TableName tableName = builder.getTableDescriptor().getTableName();
110    TestExportSnapshot.testExportFileSystemState(testUtil.getConfiguration(),
111      tableName, snapshotName, snapshotName, snapshotFilesCount,
112      testDir, getDestinationDir(fs, testUtil, testDir), false, null, true);
113  }
114
115  static Path getDestinationDir(FileSystem fs, HBaseCommonTestingUtil hctu, Path testDir)
116      throws IOException {
117    Path path = new Path(new Path(testDir, "export-test"),
118      "export-" + EnvironmentEdgeManager.currentTime()).makeQualified(fs.getUri(),
119      fs.getWorkingDirectory());
120    LOG.info("Export destination={}, fs={}, fsurl={}, fswd={}, testDir={}", path, fs, fs.getUri(),
121      fs.getWorkingDirectory(), testDir);
122    return path;
123  }
124}