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