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