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 * http://www.apache.org/licenses/LICENSE-2.0 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.apache.hadoop.hbase.snapshot; 017 018import java.io.IOException; 019import java.util.Collection; 020import java.util.HashMap; 021import java.util.Iterator; 022import java.util.List; 023import java.util.Map; 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.fs.FileStatus; 026import org.apache.hadoop.fs.FileSystem; 027import org.apache.hadoop.fs.Path; 028import org.apache.hadoop.hbase.HBaseClassTestRule; 029import org.apache.hadoop.hbase.HBaseTestingUtility; 030import org.apache.hadoop.hbase.TableName; 031import org.apache.hadoop.hbase.client.Admin; 032import org.apache.hadoop.hbase.client.RegionInfo; 033import org.apache.hadoop.hbase.client.Table; 034import org.apache.hadoop.hbase.master.snapshot.SnapshotManager; 035import org.apache.hadoop.hbase.regionserver.HRegionFileSystem; 036import org.apache.hadoop.hbase.regionserver.StoreFileInfo; 037import org.apache.hadoop.hbase.testclassification.MasterTests; 038import org.apache.hadoop.hbase.testclassification.MediumTests; 039import org.apache.hadoop.hbase.util.FSUtils; 040import org.junit.AfterClass; 041import org.junit.Assert; 042import org.junit.BeforeClass; 043import org.junit.ClassRule; 044import org.junit.Test; 045import org.junit.experimental.categories.Category; 046import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos.SnapshotDescription; 047import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos.SnapshotRegionManifest; 048 049/** 050 * Validate if storefile length match 051 * both snapshop manifest and filesystem. 052 */ 053@Category({ MasterTests.class, MediumTests.class }) 054public class TestSnapshotStoreFileSize { 055 056 @ClassRule 057 public static final HBaseClassTestRule CLASS_RULE = 058 HBaseClassTestRule.forClass(TestSnapshotStoreFileSize.class); 059 060 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); 061 private static final TableName TABLE_NAME = TableName.valueOf("t1"); 062 private static final String SNAPSHOT_NAME = "s1"; 063 private static final String FAMILY_NAME = "cf"; 064 private static Configuration conf; 065 private Admin admin; 066 private FileSystem fs; 067 068 @BeforeClass 069 public static void setup() throws Exception { 070 conf = UTIL.getConfiguration(); 071 conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true); 072 UTIL.startMiniCluster(1); 073 } 074 075 @AfterClass 076 public static void teardown() throws Exception { 077 UTIL.shutdownMiniCluster(); 078 } 079 080 @Test 081 public void testIsStoreFileSizeMatchFilesystemAndManifest() throws IOException { 082 admin = UTIL.getAdmin(); 083 fs = UTIL.getTestFileSystem(); 084 UTIL.createTable(TABLE_NAME, FAMILY_NAME.getBytes()); 085 Table table = admin.getConnection().getTable(TABLE_NAME); 086 UTIL.loadRandomRows(table, FAMILY_NAME.getBytes(), 3, 1000); 087 admin.snapshot(SNAPSHOT_NAME, TABLE_NAME); 088 089 Map<String, Long> storeFileInfoFromManifest = new HashMap<String, Long>(); 090 Map<String, Long> storeFileInfoFromFS = new HashMap<String, Long>(); 091 String storeFileName = ""; 092 long storeFilesize = 0L; 093 Path snapshotDir = SnapshotDescriptionUtils 094 .getCompletedSnapshotDir(SNAPSHOT_NAME, UTIL.getDefaultRootDirPath()); 095 SnapshotDescription snapshotDesc = SnapshotDescriptionUtils.readSnapshotInfo(fs, snapshotDir); 096 SnapshotManifest snaphotManifest = SnapshotManifest.open(conf, fs, snapshotDir, snapshotDesc); 097 List<SnapshotRegionManifest> regionManifest = snaphotManifest.getRegionManifests(); 098 for (int i = 0; i < regionManifest.size(); i++) { 099 SnapshotRegionManifest.FamilyFiles family = regionManifest.get(i).getFamilyFiles(0); 100 List<SnapshotRegionManifest.StoreFile> storeFiles = family.getStoreFilesList(); 101 for (int j = 0; j < storeFiles.size(); j++) { 102 storeFileName = storeFiles.get(j).getName(); 103 storeFilesize = storeFiles.get(j).getFileSize(); 104 storeFileInfoFromManifest.put(storeFileName, storeFilesize); 105 } 106 } 107 List<RegionInfo> regionsInfo = admin.getRegions(TABLE_NAME); 108 Path path = FSUtils.getTableDir(UTIL.getDefaultRootDirPath(), TABLE_NAME); 109 for (RegionInfo regionInfo : regionsInfo) { 110 HRegionFileSystem hRegionFileSystem = 111 HRegionFileSystem.openRegionFromFileSystem(conf, fs, path, regionInfo, true); 112 Collection<StoreFileInfo> storeFilesFS = hRegionFileSystem.getStoreFiles(FAMILY_NAME); 113 Iterator<StoreFileInfo> sfIterator = storeFilesFS.iterator(); 114 while (sfIterator.hasNext()) { 115 StoreFileInfo sfi = sfIterator.next(); 116 FileStatus[] fileStatus = FSUtils.listStatus(fs, sfi.getPath()); 117 storeFileName = fileStatus[0].getPath().getName(); 118 storeFilesize = fileStatus[0].getLen(); 119 storeFileInfoFromFS.put(storeFileName, storeFilesize); 120 } 121 } 122 Assert.assertEquals(storeFileInfoFromManifest, storeFileInfoFromFS); 123 } 124}