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;
021import java.util.Iterator;
022import java.util.Map;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.fs.FileSystem;
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.Admin;
030import org.apache.hadoop.hbase.testclassification.LargeTests;
031import org.apache.hadoop.hbase.testclassification.VerySlowMapReduceTests;
032import org.junit.After;
033import org.junit.AfterClass;
034import org.junit.Before;
035import org.junit.BeforeClass;
036import org.junit.ClassRule;
037import org.junit.Ignore;
038import org.junit.Rule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041import org.junit.rules.TestName;
042import org.slf4j.Logger;
043import org.slf4j.LoggerFactory;
044
045/**
046 * Tests that are adjunct to {@link TestExportSnapshot}. They used to be in same test suite but
047 * the test suite ran too close to the maximum time limit so we split these out. Uses
048 * facility from TestExportSnapshot where possible.
049 * @see TestExportSnapshot
050 */
051@Ignore // HBASE-24493
052@Category({VerySlowMapReduceTests.class, LargeTests.class})
053public class TestExportSnapshotAdjunct {
054  private static final Logger LOG = LoggerFactory.getLogger(TestExportSnapshotAdjunct.class);
055
056  @ClassRule
057  public static final HBaseClassTestRule CLASS_RULE =
058      HBaseClassTestRule.forClass(TestExportSnapshotAdjunct.class);
059  @Rule
060  public final TestName testName = new TestName();
061
062  protected final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
063
064  protected TableName tableName;
065  private String emptySnapshotName;
066  private String snapshotName;
067  private int tableNumFiles;
068  private Admin admin;
069
070  @BeforeClass
071  public static void setUpBeforeClass() throws Exception {
072    TestExportSnapshot.setUpBaseConf(TEST_UTIL.getConfiguration());
073    TEST_UTIL.startMiniCluster(3);
074    TEST_UTIL.startMiniMapReduceCluster();
075  }
076
077  /**
078   * Check for references to '/tmp'. We are trying to avoid having references to outside of the
079   * test data dir when running tests. References outside of the test dir makes it so concurrent
080   * tests can stamp on each other by mistake. This check is for references to the 'tmp'.
081   *
082   * This is a strange place for this test but I want somewhere where the configuration is
083   * full -- filed w/ 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,
130      TestExportSnapshot.FAMILY);
131    tableNumFiles = admin.getRegions(tableName).size();
132
133    // take a snapshot
134    admin.snapshot(snapshotName, tableName);
135  }
136
137  @After
138  public void tearDown() throws Exception {
139    TEST_UTIL.deleteTable(tableName);
140    SnapshotTestingUtils.deleteAllSnapshots(TEST_UTIL.getAdmin());
141    SnapshotTestingUtils.deleteArchiveDirectory(TEST_UTIL);
142  }
143
144  /**
145   * Check that ExportSnapshot will succeed if something fails but the retry succeed.
146   */
147  @Test
148  public void testExportRetry() throws Exception {
149    Path copyDir = TestExportSnapshot.getLocalDestinationDir(TEST_UTIL);
150    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
151    conf.setBoolean(ExportSnapshot.Testing.CONF_TEST_FAILURE, true);
152    conf.setInt(ExportSnapshot.Testing.CONF_TEST_FAILURE_COUNT, 2);
153    conf.setInt("mapreduce.map.maxattempts", 3);
154    TestExportSnapshot.testExportFileSystemState(conf, tableName,
155      snapshotName, snapshotName, tableNumFiles, TEST_UTIL.getDefaultRootDirPath(),
156      copyDir, true, null, true);
157  }
158
159  /**
160   * Check that ExportSnapshot will fail if we inject failure more times than MR will retry.
161   */
162  @Test
163  public void testExportFailure() throws Exception {
164    Path copyDir = TestExportSnapshot.getLocalDestinationDir(TEST_UTIL);
165    FileSystem fs = FileSystem.get(copyDir.toUri(), new Configuration());
166    copyDir = copyDir.makeQualified(fs.getUri(), fs.getWorkingDirectory());
167    Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
168    conf.setBoolean(ExportSnapshot.Testing.CONF_TEST_FAILURE, true);
169    conf.setInt(ExportSnapshot.Testing.CONF_TEST_FAILURE_COUNT, 4);
170    conf.setInt("mapreduce.map.maxattempts", 3);
171    TestExportSnapshot.testExportFileSystemState(conf, tableName,
172      snapshotName, snapshotName, tableNumFiles, TEST_UTIL.getDefaultRootDirPath(),
173      copyDir, true, null, false);
174  }
175}