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