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.mapreduce;
019
020import static org.junit.Assert.assertFalse;
021
022import java.io.IOException;
023import java.util.Arrays;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.FileStatus;
026import org.apache.hadoop.fs.FileSystem;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.Cell;
029import org.apache.hadoop.hbase.CellScanner;
030import org.apache.hadoop.hbase.HBaseTestingUtility;
031import org.apache.hadoop.hbase.StartMiniClusterOption;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.client.Admin;
034import org.apache.hadoop.hbase.client.Result;
035import org.apache.hadoop.hbase.client.Table;
036import org.apache.hadoop.hbase.io.HFileLink;
037import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
038import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
039import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
040import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
041import org.apache.hadoop.hbase.util.Bytes;
042import org.apache.hadoop.hbase.util.FSUtils;
043import org.apache.hadoop.hbase.util.HFileArchiveUtil;
044import org.junit.Assert;
045import org.junit.Test;
046import org.slf4j.Logger;
047import org.slf4j.LoggerFactory;
048
049public abstract class TableSnapshotInputFormatTestBase {
050  private static final Logger LOG = LoggerFactory.getLogger(TableSnapshotInputFormatTestBase.class);
051  protected final HBaseTestingUtility UTIL = new HBaseTestingUtility();
052  protected static final int NUM_REGION_SERVERS = 2;
053  protected static final byte[][] FAMILIES = {Bytes.toBytes("f1"), Bytes.toBytes("f2")};
054
055  protected FileSystem fs;
056  protected Path rootDir;
057
058  public void setupCluster() throws Exception {
059    setupConf(UTIL.getConfiguration());
060    StartMiniClusterOption option = StartMiniClusterOption.builder()
061        .numRegionServers(NUM_REGION_SERVERS).numDataNodes(NUM_REGION_SERVERS)
062        .createRootDir(true).build();
063    UTIL.startMiniCluster(option);
064    rootDir = UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
065    fs = rootDir.getFileSystem(UTIL.getConfiguration());
066  }
067
068  public void tearDownCluster() throws Exception {
069    UTIL.shutdownMiniCluster();
070  }
071
072  private static void setupConf(Configuration conf) {
073    // Enable snapshot
074    conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true);
075  }
076
077  protected abstract void testWithMockedMapReduce(HBaseTestingUtility util, String snapshotName,
078    int numRegions, int numSplitsPerRegion, int expectedNumSplits, boolean setLocalityEnabledTo)
079    throws Exception;
080
081  protected abstract void testWithMapReduceImpl(HBaseTestingUtility util, TableName tableName,
082    String snapshotName, Path tableDir, int numRegions, int numSplitsPerRegion,
083    int expectedNumSplits, boolean shutdownCluster) throws Exception;
084
085  protected abstract byte[] getStartRow();
086
087  protected abstract byte[] getEndRow();
088
089  @Test
090  public void testWithMockedMapReduceSingleRegion() throws Exception {
091    testWithMockedMapReduce(UTIL, "testWithMockedMapReduceSingleRegion", 1, 1, 1, true);
092  }
093
094  @Test
095  public void testWithMockedMapReduceMultiRegion() throws Exception {
096    testWithMockedMapReduce(UTIL, "testWithMockedMapReduceMultiRegion", 10, 1, 8, false);
097  }
098
099  @Test
100  public void testWithMapReduceSingleRegion() throws Exception {
101    testWithMapReduce(UTIL, "testWithMapReduceSingleRegion", 1, 1, 1, false);
102  }
103
104  @Test
105  public void testWithMapReduceMultiRegion() throws Exception {
106    testWithMapReduce(UTIL, "testWithMapReduceMultiRegion", 10, 1, 8, false);
107  }
108
109  @Test
110  // run the MR job while HBase is offline
111  public void testWithMapReduceAndOfflineHBaseMultiRegion() throws Exception {
112    testWithMapReduce(UTIL, "testWithMapReduceAndOfflineHBaseMultiRegion", 10, 1, 8, true);
113  }
114
115  // Test that snapshot restore does not create back references in the HBase root dir.
116  @Test
117  public void testRestoreSnapshotDoesNotCreateBackRefLinks() throws Exception {
118    setupCluster();
119    TableName tableName = TableName.valueOf("testRestoreSnapshotDoesNotCreateBackRefLinks");
120    String snapshotName = "foo";
121
122    try {
123      createTableAndSnapshot(UTIL, tableName, snapshotName, getStartRow(), getEndRow(), 1);
124
125      Path tmpTableDir = UTIL.getDataTestDirOnTestFS(snapshotName);
126
127      testRestoreSnapshotDoesNotCreateBackRefLinksInit(tableName, snapshotName,tmpTableDir);
128
129      Path rootDir = FSUtils.getRootDir(UTIL.getConfiguration());
130      for (Path regionDir : FSUtils.getRegionDirs(fs, FSUtils.getTableDir(rootDir, tableName))) {
131        for (Path storeDir : FSUtils.getFamilyDirs(fs, regionDir)) {
132          for (FileStatus status : fs.listStatus(storeDir)) {
133            System.out.println(status.getPath());
134            if (StoreFileInfo.isValid(status)) {
135              Path archiveStoreDir = HFileArchiveUtil.getStoreArchivePath(UTIL.getConfiguration(),
136                tableName, regionDir.getName(), storeDir.getName());
137
138              Path path = HFileLink.getBackReferencesDir(storeDir, status.getPath().getName());
139              // assert back references directory is empty
140              assertFalse("There is a back reference in " + path, fs.exists(path));
141
142              path = HFileLink.getBackReferencesDir(archiveStoreDir, status.getPath().getName());
143              // assert back references directory is empty
144              assertFalse("There is a back reference in " + path, fs.exists(path));
145            }
146          }
147        }
148      }
149    } finally {
150      UTIL.getAdmin().deleteSnapshot(snapshotName);
151      UTIL.deleteTable(tableName);
152      tearDownCluster();
153    }
154  }
155
156  public abstract void testRestoreSnapshotDoesNotCreateBackRefLinksInit(TableName tableName,
157      String snapshotName, Path tmpTableDir) throws Exception;
158
159  protected void testWithMapReduce(HBaseTestingUtility util, String snapshotName,
160      int numRegions, int numSplitsPerRegion, int expectedNumSplits, boolean shutdownCluster)
161      throws Exception {
162    setupCluster();
163    try {
164      Path tableDir = util.getDataTestDirOnTestFS(snapshotName);
165      TableName tableName = TableName.valueOf("testWithMapReduce");
166      testWithMapReduceImpl(util, tableName, snapshotName, tableDir, numRegions,
167              numSplitsPerRegion, expectedNumSplits, shutdownCluster);
168    } finally {
169      tearDownCluster();
170    }
171  }
172
173  protected static void verifyRowFromMap(ImmutableBytesWritable key, Result result)
174    throws IOException {
175    byte[] row = key.get();
176    CellScanner scanner = result.cellScanner();
177    while (scanner.advance()) {
178      Cell cell = scanner.current();
179
180      //assert that all Cells in the Result have the same key
181      Assert.assertEquals(0, Bytes.compareTo(row, 0, row.length,
182        cell.getRowArray(), cell.getRowOffset(), cell.getRowLength()));
183    }
184
185    for (byte[] family : FAMILIES) {
186      byte[] actual = result.getValue(family, family);
187      Assert.assertArrayEquals(
188        "Row in snapshot does not match, expected:" + Bytes.toString(row) + " ,actual:" + Bytes
189          .toString(actual), row, actual);
190    }
191  }
192
193  protected static void createTableAndSnapshot(HBaseTestingUtility util, TableName tableName,
194    String snapshotName, byte[] startRow, byte[] endRow, int numRegions)
195    throws Exception {
196    try {
197      LOG.debug("Ensuring table doesn't exist.");
198      util.deleteTable(tableName);
199    } catch(Exception ex) {
200      // ignore
201    }
202
203    LOG.info("creating table '" + tableName + "'");
204    if (numRegions > 1) {
205      util.createTable(tableName, FAMILIES, 1, startRow, endRow, numRegions);
206    } else {
207      util.createTable(tableName, FAMILIES);
208    }
209    Admin admin = util.getAdmin();
210
211    LOG.info("put some stuff in the table");
212    Table table = util.getConnection().getTable(tableName);
213    util.loadTable(table, FAMILIES);
214
215    Path rootDir = FSUtils.getRootDir(util.getConfiguration());
216    FileSystem fs = rootDir.getFileSystem(util.getConfiguration());
217
218    LOG.info("snapshot");
219    SnapshotTestingUtils.createSnapshotAndValidate(admin, tableName,
220      Arrays.asList(FAMILIES), null, snapshotName, rootDir, fs, true);
221
222    LOG.info("load different values");
223    byte[] value = Bytes.toBytes("after_snapshot_value");
224    util.loadTable(table, FAMILIES, value);
225
226    LOG.info("cause flush to create new files in the region");
227    admin.flush(tableName);
228    table.close();
229  }
230}