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.client; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.fail; 022 023import java.io.IOException; 024import java.util.HashSet; 025import java.util.Set; 026import org.apache.hadoop.fs.Path; 027import org.apache.hadoop.hbase.TableName; 028import org.apache.hadoop.hbase.master.MasterFileSystem; 029import org.apache.hadoop.hbase.regionserver.NoSuchColumnFamilyException; 030import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils; 031import org.apache.hadoop.hbase.util.Bytes; 032import org.apache.hadoop.hbase.util.CommonFSUtils; 033import org.apache.hadoop.hbase.util.FSUtils; 034import org.junit.jupiter.api.TestTemplate; 035 036public class RestoreSnapshotFromClientSchemaChangeTestBase 037 extends RestoreSnapshotFromClientTestBase { 038 039 protected RestoreSnapshotFromClientSchemaChangeTestBase(int numReplicas) { 040 super(numReplicas); 041 } 042 043 private Set<String> getFamiliesFromFS(final TableName tableName) throws IOException { 044 MasterFileSystem mfs = TEST_UTIL.getMiniHBaseCluster().getMaster().getMasterFileSystem(); 045 Set<String> families = new HashSet<>(); 046 Path tableDir = CommonFSUtils.getTableDir(mfs.getRootDir(), tableName); 047 for (Path regionDir : FSUtils.getRegionDirs(mfs.getFileSystem(), tableDir)) { 048 for (Path familyDir : FSUtils.getFamilyDirs(mfs.getFileSystem(), regionDir)) { 049 families.add(familyDir.getName()); 050 } 051 } 052 return families; 053 } 054 055 protected ColumnFamilyDescriptor getTestRestoreSchemaChangeHCD() { 056 return ColumnFamilyDescriptorBuilder.of(TEST_FAMILY2); 057 } 058 059 @TestTemplate 060 public void testRestoreSchemaChange() throws Exception { 061 Table table = TEST_UTIL.getConnection().getTable(tableName); 062 063 // Add one column family and put some data in it 064 admin.disableTable(tableName); 065 admin.addColumnFamily(tableName, getTestRestoreSchemaChangeHCD()); 066 admin.enableTable(tableName); 067 assertEquals(2, table.getDescriptor().getColumnFamilyCount()); 068 TableDescriptor htd = admin.getDescriptor(tableName); 069 assertEquals(2, htd.getColumnFamilyCount()); 070 SnapshotTestingUtils.loadData(TEST_UTIL, tableName, 500, TEST_FAMILY2); 071 long snapshot2Rows = snapshot1Rows + 500L; 072 assertEquals(snapshot2Rows, countRows(table)); 073 assertEquals(500, countRows(table, TEST_FAMILY2)); 074 Set<String> fsFamilies = getFamiliesFromFS(tableName); 075 assertEquals(2, fsFamilies.size()); 076 077 // Take a snapshot 078 admin.disableTable(tableName); 079 admin.snapshot(snapshotName2, tableName); 080 081 // Restore the snapshot (without the cf) 082 admin.restoreSnapshot(snapshotName0); 083 admin.enableTable(tableName); 084 assertEquals(1, table.getDescriptor().getColumnFamilyCount()); 085 try { 086 countRows(table, TEST_FAMILY2); 087 fail("family '" + Bytes.toString(TEST_FAMILY2) + "' should not exists"); 088 } catch (NoSuchColumnFamilyException e) { 089 // expected 090 } 091 assertEquals(snapshot0Rows, countRows(table)); 092 htd = admin.getDescriptor(tableName); 093 assertEquals(1, htd.getColumnFamilyCount()); 094 fsFamilies = getFamiliesFromFS(tableName); 095 assertEquals(1, fsFamilies.size()); 096 097 // Restore back the snapshot (with the cf) 098 admin.disableTable(tableName); 099 admin.restoreSnapshot(snapshotName2); 100 admin.enableTable(tableName); 101 htd = admin.getDescriptor(tableName); 102 assertEquals(2, htd.getColumnFamilyCount()); 103 assertEquals(2, table.getDescriptor().getColumnFamilyCount()); 104 assertEquals(500, countRows(table, TEST_FAMILY2)); 105 assertEquals(snapshot2Rows, countRows(table)); 106 fsFamilies = getFamiliesFromFS(tableName); 107 assertEquals(2, fsFamilies.size()); 108 table.close(); 109 } 110}