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.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023
024import java.io.IOException;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.Table;
030import org.apache.hadoop.hbase.mob.MobConstants;
031import org.apache.hadoop.hbase.mob.MobUtils;
032import org.apache.hadoop.hbase.snapshot.MobSnapshotTestingUtils.SnapshotMock;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.apache.hadoop.hbase.util.Bytes;
035import org.apache.hadoop.hbase.util.CommonFSUtils;
036import org.junit.jupiter.api.Tag;
037import org.junit.jupiter.api.Test;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040
041/**
042 * Test the restore/clone operation from a file-system point of view.
043 */
044@Tag(MediumTests.TAG)
045public class TestMobRestoreSnapshotHelper extends TestRestoreSnapshotHelper {
046
047  final Logger LOG = LoggerFactory.getLogger(getClass());
048
049  @Override
050  protected void setupConf(Configuration conf) {
051    conf.setInt(MobConstants.MOB_FILE_CACHE_SIZE_KEY, 0);
052  }
053
054  @Override
055  protected SnapshotMock createSnapshotMock() throws IOException {
056    return new SnapshotMock(TEST_UTIL.getConfiguration(), fs, rootDir);
057  }
058
059  @Override
060  protected void createTableAndSnapshot(TableName tableName, String snapshotName)
061    throws IOException {
062    byte[] column = Bytes.toBytes("A");
063    Table table = MobSnapshotTestingUtils.createMobTable(TEST_UTIL, tableName, column);
064    TEST_UTIL.loadTable(table, column);
065    TEST_UTIL.getAdmin().snapshot(snapshotName, tableName);
066  }
067
068  @Test
069  public void testRestoreMobTableFromSnapshot() throws IOException {
070    TableName mobTableName = TableName.valueOf("testRestoreMobTable");
071    String snapshotName = "testRestoreMobTable_snapshot";
072    createTableAndSnapshot(mobTableName, snapshotName);
073    assertTrue(TEST_UTIL.getAdmin().tableExists(mobTableName));
074    assertTrue(MobUtils.hasMobColumns(TEST_UTIL.getAdmin().getDescriptor(mobTableName)));
075    assertTrue(TEST_UTIL.getAdmin().listSnapshots().stream()
076      .anyMatch(snapshotDescription -> snapshotName.equals(snapshotDescription.getName())));
077
078    // Clone the snapshot to a new MOB table
079    TableName newMobTableName = TableName.valueOf("newTestRestoreMobTable");
080    TEST_UTIL.getAdmin().cloneSnapshot(snapshotName, newMobTableName);
081    assertTrue(TEST_UTIL.getAdmin().tableExists(newMobTableName));
082    assertTrue(MobUtils.hasMobColumns(TEST_UTIL.getAdmin().getDescriptor(newMobTableName)));
083
084    Path hbaseRootDir = TEST_UTIL.getDefaultRootDirPath();
085    Path mobRegionPath = MobUtils.getMobTableDir(hbaseRootDir, newMobTableName);
086    assertTrue(fs.exists(mobRegionPath));
087    Path errorMobRegionPathInTableDir =
088      new Path(CommonFSUtils.getTableDir(hbaseRootDir, newMobTableName),
089        MobUtils.getMobRegionInfo(newMobTableName).getEncodedName());
090    assertFalse(fs.exists(errorMobRegionPathInTableDir));
091
092    try (Table originMobTable = TEST_UTIL.getConnection().getTable(mobTableName);
093      Table clonedMobTable = TEST_UTIL.getConnection().getTable(newMobTableName)) {
094      assertEquals(HBaseTestingUtil.countRows(originMobTable),
095        HBaseTestingUtil.countRows(clonedMobTable));
096    }
097
098    // Delete the original MOB table and restore it from the snapshot
099    TEST_UTIL.deleteTable(mobTableName);
100    assertFalse(TEST_UTIL.getAdmin().tableExists(mobTableName));
101    TEST_UTIL.getAdmin().cloneSnapshot(snapshotName, mobTableName);
102    assertTrue(TEST_UTIL.getAdmin().tableExists(mobTableName));
103    assertTrue(MobUtils.hasMobColumns(TEST_UTIL.getAdmin().getDescriptor(mobTableName)));
104    mobRegionPath = MobUtils.getMobTableDir(hbaseRootDir, mobTableName);
105    assertTrue(fs.exists(mobRegionPath));
106    errorMobRegionPathInTableDir = new Path(CommonFSUtils.getTableDir(hbaseRootDir, mobTableName),
107      MobUtils.getMobRegionInfo(mobTableName).getEncodedName());
108    assertFalse(fs.exists(errorMobRegionPathInTableDir));
109  }
110}