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 java.util.Arrays; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.fs.Path; 028import org.apache.hadoop.hbase.HBaseTestingUtil; 029import org.apache.hadoop.hbase.NamespaceDescriptor; 030import org.apache.hadoop.hbase.TableName; 031import org.apache.hadoop.hbase.client.Admin; 032import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 033import org.apache.hadoop.hbase.client.Put; 034import org.apache.hadoop.hbase.client.Table; 035import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 036import org.apache.hadoop.hbase.mob.MobConstants; 037import org.apache.hadoop.hbase.mob.MobUtils; 038import org.apache.hadoop.hbase.snapshot.MobSnapshotTestingUtils.SnapshotMock; 039import org.apache.hadoop.hbase.testclassification.MediumTests; 040import org.apache.hadoop.hbase.util.Bytes; 041import org.apache.hadoop.hbase.util.CommonFSUtils; 042import org.junit.jupiter.api.Tag; 043import org.junit.jupiter.api.Test; 044import org.slf4j.Logger; 045import org.slf4j.LoggerFactory; 046 047/** 048 * Test the restore/clone operation from a file-system point of view. 049 */ 050@Tag(MediumTests.TAG) 051public class TestMobRestoreSnapshotHelper extends TestRestoreSnapshotHelper { 052 053 final Logger LOG = LoggerFactory.getLogger(getClass()); 054 055 @Override 056 protected void setupConf(Configuration conf) { 057 conf.setInt(MobConstants.MOB_FILE_CACHE_SIZE_KEY, 0); 058 } 059 060 @Override 061 protected SnapshotMock createSnapshotMock() throws IOException { 062 return new SnapshotMock(TEST_UTIL.getConfiguration(), fs, rootDir); 063 } 064 065 @Override 066 protected void createTableAndSnapshot(TableName tableName, String snapshotName) 067 throws IOException { 068 byte[] column = Bytes.toBytes("A"); 069 Table table = MobSnapshotTestingUtils.createMobTable(TEST_UTIL, tableName, column); 070 TEST_UTIL.loadTable(table, column); 071 TEST_UTIL.getAdmin().snapshot(snapshotName, tableName); 072 } 073 074 @Test 075 public void testRestoreMobTableFromSnapshot() throws IOException { 076 TableName mobTableName = TableName.valueOf("testRestoreMobTable"); 077 String snapshotName = "testRestoreMobTable_snapshot"; 078 createTableAndSnapshot(mobTableName, snapshotName); 079 assertTrue(TEST_UTIL.getAdmin().tableExists(mobTableName)); 080 assertTrue(MobUtils.hasMobColumns(TEST_UTIL.getAdmin().getDescriptor(mobTableName))); 081 assertTrue(TEST_UTIL.getAdmin().listSnapshots().stream() 082 .anyMatch(snapshotDescription -> snapshotName.equals(snapshotDescription.getName()))); 083 084 // Clone the snapshot to a new MOB table 085 TableName newMobTableName = TableName.valueOf("newTestRestoreMobTable"); 086 TEST_UTIL.getAdmin().cloneSnapshot(snapshotName, newMobTableName); 087 assertTrue(TEST_UTIL.getAdmin().tableExists(newMobTableName)); 088 assertTrue(MobUtils.hasMobColumns(TEST_UTIL.getAdmin().getDescriptor(newMobTableName))); 089 090 Path hbaseRootDir = TEST_UTIL.getDefaultRootDirPath(); 091 Path mobRegionPath = MobUtils.getMobTableDir(hbaseRootDir, newMobTableName); 092 assertTrue(fs.exists(mobRegionPath)); 093 Path errorMobRegionPathInTableDir = 094 new Path(CommonFSUtils.getTableDir(hbaseRootDir, newMobTableName), 095 MobUtils.getMobRegionInfo(newMobTableName).getEncodedName()); 096 assertFalse(fs.exists(errorMobRegionPathInTableDir)); 097 098 try (Table originMobTable = TEST_UTIL.getConnection().getTable(mobTableName); 099 Table clonedMobTable = TEST_UTIL.getConnection().getTable(newMobTableName)) { 100 assertEquals(HBaseTestingUtil.countRows(originMobTable), 101 HBaseTestingUtil.countRows(clonedMobTable)); 102 } 103 104 // Delete the original MOB table and restore it from the snapshot 105 TEST_UTIL.deleteTable(mobTableName); 106 assertFalse(TEST_UTIL.getAdmin().tableExists(mobTableName)); 107 TEST_UTIL.getAdmin().cloneSnapshot(snapshotName, mobTableName); 108 assertTrue(TEST_UTIL.getAdmin().tableExists(mobTableName)); 109 assertTrue(MobUtils.hasMobColumns(TEST_UTIL.getAdmin().getDescriptor(mobTableName))); 110 mobRegionPath = MobUtils.getMobTableDir(hbaseRootDir, mobTableName); 111 assertTrue(fs.exists(mobRegionPath)); 112 errorMobRegionPathInTableDir = new Path(CommonFSUtils.getTableDir(hbaseRootDir, mobTableName), 113 MobUtils.getMobRegionInfo(mobTableName).getEncodedName()); 114 assertFalse(fs.exists(errorMobRegionPathInTableDir)); 115 } 116 117 @Test 118 public void testSecondSnapshotAfterMobRestore() throws IOException { 119 testSecondSnapshotAfterMobRestore(TableName.valueOf("testSecondSnapshotAfterMobRestore"), 120 "testSecondSnapshotAfterMobRestore_snapshot", "testSecondSnapshotAfterMobRestore_snapshot2"); 121 } 122 123 @Test 124 public void testSecondSnapshotAfterMobTableRestoreWithNamespace() throws IOException { 125 String namespace = "testMobSnapshotNs"; 126 TEST_UTIL.getAdmin().createNamespace(NamespaceDescriptor.create(namespace).build()); 127 try { 128 testSecondSnapshotAfterMobRestore( 129 TableName.valueOf(namespace, "testSecondSnapshotAfterMobRestore"), 130 "testSecondSnapshotAfterMobRestoreWithNamespace_snapshot", 131 "testSecondSnapshotAfterMobRestoreWithNamespace_snapshot2"); 132 } finally { 133 TEST_UTIL.getAdmin().deleteNamespace(namespace); 134 } 135 } 136 137 private void testSecondSnapshotAfterMobRestore(TableName tableName, String snapshotName, 138 String secondSnapshotName) throws IOException { 139 Admin admin = TEST_UTIL.getAdmin(); 140 byte[] family = Bytes.toBytes("info"); 141 byte[] data = Bytes.toBytes("test"); 142 byte[] mobData = new byte[100]; 143 Arrays.fill(mobData, (byte) 'A'); 144 145 try { 146 admin.createTable( 147 TableDescriptorBuilder.newBuilder(tableName).setColumnFamily(ColumnFamilyDescriptorBuilder 148 .newBuilder(family).setMobEnabled(true).setMobThreshold(10L).build()).build()); 149 assertTrue(admin.tableExists(tableName)); 150 try (Table table = TEST_UTIL.getConnection().getTable(tableName)) { 151 table.put(new Put(Bytes.toBytes("row1")).addColumn(family, Bytes.toBytes("q"), data)); 152 table.put(new Put(Bytes.toBytes("row2")).addColumn(family, Bytes.toBytes("q"), mobData)); 153 } 154 admin.flush(tableName); 155 admin.snapshot(snapshotName, tableName); 156 157 TEST_UTIL.deleteTable(tableName); 158 assertFalse(admin.tableExists(tableName)); 159 admin.cloneSnapshot(snapshotName, tableName); 160 assertTrue(admin.tableExists(tableName)); 161 162 try (Table table = TEST_UTIL.getConnection().getTable(tableName)) { 163 table.put(new Put(Bytes.toBytes("row3")).addColumn(family, Bytes.toBytes("q"), data)); 164 table.put(new Put(Bytes.toBytes("row4")).addColumn(family, Bytes.toBytes("q"), mobData)); 165 } 166 admin.flush(tableName); 167 admin.snapshot(secondSnapshotName, tableName); 168 169 TEST_UTIL.deleteTable(tableName); 170 assertFalse(admin.tableExists(tableName)); 171 admin.cloneSnapshot(secondSnapshotName, tableName); 172 assertTrue(admin.tableExists(tableName)); 173 assertEquals(4, TEST_UTIL.countRows(tableName)); 174 175 } finally { 176 if (admin.tableExists(tableName)) { 177 TEST_UTIL.deleteTable(tableName); 178 } 179 deleteSnapshotIfExists(admin, secondSnapshotName); 180 deleteSnapshotIfExists(admin, snapshotName); 181 } 182 } 183 184 private void deleteSnapshotIfExists(Admin admin, String snapshotName) throws IOException { 185 if ( 186 admin.listSnapshots().stream().anyMatch(snapshot -> snapshotName.equals(snapshot.getName())) 187 ) { 188 admin.deleteSnapshot(snapshotName); 189 } 190 } 191}