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.HBaseTestingUtility;
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.apache.hadoop.hbase.util.Bytes;
034import org.junit.After;
035import org.junit.AfterClass;
036import org.junit.Before;
037import org.junit.BeforeClass;
038import org.junit.ClassRule;
039import org.junit.Ignore;
040import org.junit.Rule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043import org.junit.rules.TestName;
044import org.slf4j.Logger;
045import org.slf4j.LoggerFactory;
046
047/**
048 * Tests that are adjunct to {@link TestExportSnapshot}. They used to be in same test suite but the
049 * test suite ran too close to the maximum time limit so we split these out. Uses facility from
050 * TestExportSnapshot where possible.
051 * @see TestExportSnapshot
052 */
053@Ignore // HBASE-24493
054@Category({ VerySlowMapReduceTests.class, LargeTests.class })
055public class TestExportSnapshotAdjunct {
056  private static final Logger LOG = LoggerFactory.getLogger(TestExportSnapshotAdjunct.class);
057
058  @ClassRule
059  public static final HBaseClassTestRule CLASS_RULE =
060    HBaseClassTestRule.forClass(TestExportSnapshotAdjunct.class);
061  @Rule
062  public final TestName testName = new TestName();
063
064  protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
065
066  protected TableName tableName;
067  private String emptySnapshotName;
068  private String snapshotName;
069  private int tableNumFiles;
070  private Admin admin;
071
072  @BeforeClass
073  public static void setUpBeforeClass() throws Exception {
074    TestExportSnapshot.setUpBaseConf(TEST_UTIL.getConfiguration());
075    TEST_UTIL.startMiniCluster(3);
076    TEST_UTIL.startMiniMapReduceCluster();
077  }
078
079  /**
080   * Check for references to '/tmp'. We are trying to avoid having references to outside of the test
081   * data dir when running tests. References outside of the test dir makes it so concurrent tests
082   * can stamp on each other by mistake. This check is for references to the 'tmp'. This is a
083   * strange place for this test but I want somewhere where the configuration is full -- filed w/
084   * hdfs and mapreduce configurations.
085   */
086  private void checkForReferencesToTmpDir() {
087    Configuration conf = TEST_UTIL.getConfiguration();
088    for (Iterator<Map.Entry<String, String>> i = conf.iterator(); i.hasNext();) {
089      Map.Entry<String, String> e = i.next();
090      if (e.getKey().contains("original.hbase.dir")) {
091        continue;
092      }
093      if (e.getValue().contains("java.io.tmpdir")) {
094        continue;
095      }
096      if (e.getValue().contains("hadoop.tmp.dir")) {
097        continue;
098      }
099      if (e.getValue().contains("hbase.tmp.dir")) {
100        continue;
101      }
102      assertFalse(e.getKey() + " " + e.getValue(), e.getValue().contains("tmp"));
103    }
104  }
105
106  @AfterClass
107  public static void tearDownAfterClass() throws Exception {
108    TEST_UTIL.shutdownMiniMapReduceCluster();
109    TEST_UTIL.shutdownMiniCluster();
110  }
111
112  /**
113   * Create a table and take a snapshot of the table used by the export test.
114   */
115  @Before
116  public void setUp() throws Exception {
117    this.admin = TEST_UTIL.getAdmin();
118
119    tableName = TableName.valueOf("testtb-" + testName.getMethodName());
120    snapshotName = "snaptb0-" + testName.getMethodName();
121    emptySnapshotName = "emptySnaptb0-" + testName.getMethodName();
122
123    // Create Table
124    SnapshotTestingUtils.createPreSplitTable(TEST_UTIL, tableName, 2, TestExportSnapshot.FAMILY);
125
126    // Take an empty snapshot
127    admin.snapshot(emptySnapshotName, tableName);
128
129    // Add some rows
130    SnapshotTestingUtils.loadData(TEST_UTIL, tableName, 50, 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, Bytes.toBytes(snapshotName),
155      Bytes.toBytes(snapshotName), tableNumFiles, TEST_UTIL.getDefaultRootDirPath(), copyDir, true,
156      false, 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, Bytes.toBytes(snapshotName),
172      Bytes.toBytes(snapshotName), tableNumFiles, TEST_UTIL.getDefaultRootDirPath(), copyDir, true,
173      false, null, false);
174  }
175}