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.assertFalse;
021
022import java.util.Iterator;
023import java.util.Map;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtil;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.client.Admin;
031import org.apache.hadoop.hbase.testclassification.LargeTests;
032import org.apache.hadoop.hbase.testclassification.VerySlowMapReduceTests;
033import org.junit.After;
034import org.junit.AfterClass;
035import org.junit.Before;
036import org.junit.BeforeClass;
037import org.junit.ClassRule;
038import org.junit.Ignore;
039import org.junit.Rule;
040import org.junit.Test;
041import org.junit.experimental.categories.Category;
042import org.junit.rules.TestName;
043import org.slf4j.Logger;
044import org.slf4j.LoggerFactory;
045
046/**
047 * Tests that are adjunct to {@link TestExportSnapshot}. They used to be in same test suite but the
048 * test suite ran too close to the maximum time limit so we split these out. Uses facility from
049 * TestExportSnapshot where possible.
050 * @see TestExportSnapshot
051 */
052@Ignore // HBASE-24493
053@Category({ VerySlowMapReduceTests.class, LargeTests.class })
054public class TestExportSnapshotAdjunct {
055  private static final Logger LOG = LoggerFactory.getLogger(TestExportSnapshotAdjunct.class);
056
057  @ClassRule
058  public static final HBaseClassTestRule CLASS_RULE =
059    HBaseClassTestRule.forClass(TestExportSnapshotAdjunct.class);
060  @Rule
061  public final TestName testName = new TestName();
062
063  protected final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
064
065  protected TableName tableName;
066  private String emptySnapshotName;
067  private String snapshotName;
068  private int tableNumFiles;
069  private Admin admin;
070
071  @BeforeClass
072  public static void setUpBeforeClass() throws Exception {
073    TestExportSnapshot.setUpBaseConf(TEST_UTIL.getConfiguration());
074    TEST_UTIL.startMiniCluster(3);
075    TEST_UTIL.startMiniMapReduceCluster();
076  }
077
078  /**
079   * Check for references to '/tmp'. We are trying to avoid having references to outside of the test
080   * data dir when running tests. References outside of the test dir makes it so concurrent tests
081   * can stamp on each other by mistake. This check is for references to the 'tmp'. This is a
082   * strange place for this test but I want somewhere where the configuration is full -- filed w/
083   * hdfs and mapreduce configurations.
084   */
085  private void checkForReferencesToTmpDir() {
086    Configuration conf = TEST_UTIL.getConfiguration();
087    for (Iterator<Map.Entry<String, String>> i = conf.iterator(); i.hasNext();) {
088      Map.Entry<String, String> e = i.next();
089      if (e.getKey().contains("original.hbase.dir")) {
090        continue;
091      }
092      if (e.getValue().contains("java.io.tmpdir")) {
093        continue;
094      }
095      if (e.getValue().contains("hadoop.tmp.dir")) {
096        continue;
097      }
098      if (e.getValue().contains("hbase.tmp.dir")) {
099        continue;
100      }
101      assertFalse(e.getKey() + " " + e.getValue(), e.getValue().contains("tmp"));
102    }
103  }
104
105  @AfterClass
106  public static void tearDownAfterClass() throws Exception {
107    TEST_UTIL.shutdownMiniMapReduceCluster();
108    TEST_UTIL.shutdownMiniCluster();
109  }
110
111  /**
112   * Create a table and take a snapshot of the table used by the export test.
113   */
114  @Before
115  public void setUp() throws Exception {
116    this.admin = TEST_UTIL.getAdmin();
117
118    tableName = TableName.valueOf("testtb-" + testName.getMethodName());
119    snapshotName = "snaptb0-" + testName.getMethodName();
120    emptySnapshotName = "emptySnaptb0-" + testName.getMethodName();
121
122    // Create Table
123    SnapshotTestingUtils.createPreSplitTable(TEST_UTIL, tableName, 2, TestExportSnapshot.FAMILY);
124
125    // Take an empty snapshot
126    admin.snapshot(emptySnapshotName, tableName);
127
128    // Add some rows
129    SnapshotTestingUtils.loadData(TEST_UTIL, tableName, 50, TestExportSnapshot.FAMILY);
130    tableNumFiles = admin.getRegions(tableName).size();
131
132    // take a snapshot
133    admin.snapshot(snapshotName, tableName);
134  }
135
136  @After
137  public void tearDown() throws Exception {
138    TEST_UTIL.deleteTable(tableName);
139    SnapshotTestingUtils.deleteAllSnapshots(TEST_UTIL.getAdmin());
140    SnapshotTestingUtils.deleteArchiveDirectory(TEST_UTIL);
141  }
142
143  /**
144   * Check that ExportSnapshot will succeed if something fails but the retry succeed.
145   */
146  @Test
147  public void testExportRetry() throws Exception {
148    Path copyDir = TestExportSnapshot.getLocalDestinationDir(TEST_UTIL);
149    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
150    conf.setBoolean(ExportSnapshot.Testing.CONF_TEST_FAILURE, true);
151    conf.setInt(ExportSnapshot.Testing.CONF_TEST_FAILURE_COUNT, 2);
152    conf.setInt("mapreduce.map.maxattempts", 3);
153    TestExportSnapshot.testExportFileSystemState(conf, tableName, snapshotName, snapshotName,
154      tableNumFiles, TEST_UTIL.getDefaultRootDirPath(), copyDir, true, false, null, true);
155  }
156
157  /**
158   * Check that ExportSnapshot will fail if we inject failure more times than MR will retry.
159   */
160  @Test
161  public void testExportFailure() throws Exception {
162    Path copyDir = TestExportSnapshot.getLocalDestinationDir(TEST_UTIL);
163    FileSystem fs = FileSystem.get(copyDir.toUri(), new Configuration());
164    copyDir = copyDir.makeQualified(fs.getUri(), fs.getWorkingDirectory());
165    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
166    conf.setBoolean(ExportSnapshot.Testing.CONF_TEST_FAILURE, true);
167    conf.setInt(ExportSnapshot.Testing.CONF_TEST_FAILURE_COUNT, 4);
168    conf.setInt("mapreduce.map.maxattempts", 3);
169    TestExportSnapshot.testExportFileSystemState(conf, tableName, snapshotName, snapshotName,
170      tableNumFiles, TEST_UTIL.getDefaultRootDirPath(), copyDir, true, false, null, false);
171  }
172}