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 static org.junit.jupiter.api.Assertions.assertEquals;
021
022import java.io.IOException;
023import java.util.Collection;
024import java.util.HashMap;
025import java.util.Iterator;
026import java.util.List;
027import java.util.Map;
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.fs.FileStatus;
030import org.apache.hadoop.fs.FileSystem;
031import org.apache.hadoop.fs.Path;
032import org.apache.hadoop.hbase.HBaseTestingUtil;
033import org.apache.hadoop.hbase.TableName;
034import org.apache.hadoop.hbase.client.Admin;
035import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
036import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
037import org.apache.hadoop.hbase.client.RegionInfo;
038import org.apache.hadoop.hbase.client.Table;
039import org.apache.hadoop.hbase.master.snapshot.SnapshotManager;
040import org.apache.hadoop.hbase.regionserver.HRegionFileSystem;
041import org.apache.hadoop.hbase.regionserver.StoreFileInfo;
042import org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTracker;
043import org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTrackerFactory;
044import org.apache.hadoop.hbase.testclassification.MasterTests;
045import org.apache.hadoop.hbase.testclassification.MediumTests;
046import org.apache.hadoop.hbase.util.CommonFSUtils;
047import org.junit.jupiter.api.AfterAll;
048import org.junit.jupiter.api.BeforeAll;
049import org.junit.jupiter.api.Tag;
050import org.junit.jupiter.api.Test;
051
052import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos.SnapshotDescription;
053import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos.SnapshotRegionManifest;
054
055/**
056 * Validate if storefile length match both snapshop manifest and filesystem.
057 */
058@Tag(MasterTests.TAG)
059@Tag(MediumTests.TAG)
060public class TestSnapshotStoreFileSize {
061
062  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
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  @BeforeAll
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  @AfterAll
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      ColumnFamilyDescriptor hcd = ColumnFamilyDescriptorBuilder.of(FAMILY_NAME);
115      StoreFileTracker sft =
116        StoreFileTrackerFactory.create(conf, table.getDescriptor(), hcd, hRegionFileSystem);
117      Collection<StoreFileInfo> storeFilesFS = sft.load();
118      Iterator<StoreFileInfo> sfIterator = storeFilesFS.iterator();
119      while (sfIterator.hasNext()) {
120        StoreFileInfo sfi = sfIterator.next();
121        FileStatus[] fileStatus = CommonFSUtils.listStatus(fs, sfi.getPath());
122        storeFileName = fileStatus[0].getPath().getName();
123        storeFilesize = fileStatus[0].getLen();
124        storeFileInfoFromFS.put(storeFileName, storeFilesize);
125      }
126    }
127    assertEquals(storeFileInfoFromManifest, storeFileInfoFromFS);
128  }
129}