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