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