|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.apache.hadoop.hbase.snapshot; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Iterator; |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import org.apache.hadoop.conf.Configuration; |
| 25 | +import org.apache.hadoop.fs.FileStatus; |
| 26 | +import org.apache.hadoop.fs.FileSystem; |
| 27 | +import org.apache.hadoop.fs.Path; |
| 28 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 29 | +import org.apache.hadoop.hbase.HBaseTestingUtility; |
| 30 | +import org.apache.hadoop.hbase.TableName; |
| 31 | +import org.apache.hadoop.hbase.client.Admin; |
| 32 | +import org.apache.hadoop.hbase.client.RegionInfo; |
| 33 | +import org.apache.hadoop.hbase.client.Table; |
| 34 | +import org.apache.hadoop.hbase.master.snapshot.SnapshotManager; |
| 35 | +import org.apache.hadoop.hbase.regionserver.HRegionFileSystem; |
| 36 | +import org.apache.hadoop.hbase.regionserver.StoreFileInfo; |
| 37 | +import org.apache.hadoop.hbase.testclassification.MasterTests; |
| 38 | +import org.apache.hadoop.hbase.testclassification.MediumTests; |
| 39 | +import org.apache.hadoop.hbase.util.FSUtils; |
| 40 | +import org.junit.AfterClass; |
| 41 | +import org.junit.Assert; |
| 42 | +import org.junit.BeforeClass; |
| 43 | +import org.junit.ClassRule; |
| 44 | +import org.junit.Test; |
| 45 | +import org.junit.experimental.categories.Category; |
| 46 | +import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos.SnapshotDescription; |
| 47 | +import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos.SnapshotRegionManifest; |
| 48 | + |
| 49 | +/** |
| 50 | + * Validate if storefile length match |
| 51 | + * both snapshop manifest and filesystem. |
| 52 | + */ |
| 53 | +@Category({ MasterTests.class, MediumTests.class }) |
| 54 | +public class TestSnapshotStoreFileSize { |
| 55 | + |
| 56 | + @ClassRule |
| 57 | + public static final HBaseClassTestRule CLASS_RULE = |
| 58 | + HBaseClassTestRule.forClass(TestSnapshotStoreFileSize.class); |
| 59 | + |
| 60 | + private static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); |
| 61 | + private static final TableName TABLE_NAME = TableName.valueOf("t1"); |
| 62 | + private static final String SNAPSHOT_NAME = "s1"; |
| 63 | + private static final String FAMILY_NAME = "cf"; |
| 64 | + private static Configuration conf; |
| 65 | + private Admin admin; |
| 66 | + private FileSystem fs; |
| 67 | + |
| 68 | + @BeforeClass |
| 69 | + public static void setup() throws Exception { |
| 70 | + conf = UTIL.getConfiguration(); |
| 71 | + conf.setBoolean(SnapshotManager.HBASE_SNAPSHOT_ENABLED, true); |
| 72 | + UTIL.startMiniCluster(1); |
| 73 | + } |
| 74 | + |
| 75 | + @AfterClass |
| 76 | + public static void teardown() throws Exception { |
| 77 | + UTIL.shutdownMiniCluster(); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void testIsStoreFileSizeMatchFilesystemAndManifest() throws IOException { |
| 82 | + admin = UTIL.getAdmin(); |
| 83 | + fs = UTIL.getTestFileSystem(); |
| 84 | + UTIL.createTable(TABLE_NAME, FAMILY_NAME.getBytes()); |
| 85 | + Table table = admin.getConnection().getTable(TABLE_NAME); |
| 86 | + UTIL.loadRandomRows(table, FAMILY_NAME.getBytes(), 3, 1000); |
| 87 | + admin.snapshot(SNAPSHOT_NAME, TABLE_NAME); |
| 88 | + |
| 89 | + Map<String, Long> storeFileInfoFromManifest = new HashMap<String, Long>(); |
| 90 | + Map<String, Long> storeFileInfoFromFS = new HashMap<String, Long>(); |
| 91 | + String storeFileName = ""; |
| 92 | + long storeFilesize = 0L; |
| 93 | + Path snapshotDir = SnapshotDescriptionUtils |
| 94 | + .getCompletedSnapshotDir(SNAPSHOT_NAME, UTIL.getDefaultRootDirPath()); |
| 95 | + SnapshotDescription snapshotDesc = SnapshotDescriptionUtils.readSnapshotInfo(fs, snapshotDir); |
| 96 | + SnapshotManifest snaphotManifest = SnapshotManifest.open(conf, fs, snapshotDir, snapshotDesc); |
| 97 | + List<SnapshotRegionManifest> regionManifest = snaphotManifest.getRegionManifests(); |
| 98 | + for (int i = 0; i < regionManifest.size(); i++) { |
| 99 | + 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 | +} |
0 commit comments