Skip to content

Commit 66506a6

Browse files
Karthik PalanisamyApache9
authored andcommitted
HBASE-23095 Reuse FileStatus in StoreFileInfo (#674)
Signed-off-by: Anoop Sam John <[email protected]> Signed-off-by: Duo Zhang <[email protected]>
1 parent 4a37572 commit 66506a6

File tree

3 files changed

+153
-3
lines changed

3 files changed

+153
-3
lines changed

hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/StoreFileInfo.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ public class StoreFileInfo {
101101
// timestamp on when the file was created, is 0 and ignored for reference or link files
102102
private long createdTimestamp;
103103

104+
private long size;
105+
104106
/**
105107
* Create a Store File Info
106108
* @param conf the {@link Configuration} to use
@@ -109,6 +111,11 @@ public class StoreFileInfo {
109111
*/
110112
public StoreFileInfo(final Configuration conf, final FileSystem fs, final Path initialPath)
111113
throws IOException {
114+
this(conf, fs, null, initialPath);
115+
}
116+
117+
private StoreFileInfo(final Configuration conf, final FileSystem fs,
118+
final FileStatus fileStatus, final Path initialPath) throws IOException {
112119
assert fs != null;
113120
assert initialPath != null;
114121
assert conf != null;
@@ -136,7 +143,14 @@ public StoreFileInfo(final Configuration conf, final FileSystem fs, final Path i
136143
" reference to " + referencePath);
137144
} else if (isHFile(p)) {
138145
// HFile
139-
this.createdTimestamp = fs.getFileStatus(initialPath).getModificationTime();
146+
if (fileStatus != null) {
147+
this.createdTimestamp = fileStatus.getModificationTime();
148+
this.size = fileStatus.getLen();
149+
} else {
150+
FileStatus fStatus = fs.getFileStatus(initialPath);
151+
this.createdTimestamp = fStatus.getModificationTime();
152+
this.size = fStatus.getLen();
153+
}
140154
this.reference = null;
141155
this.link = null;
142156
} else {
@@ -152,7 +166,7 @@ public StoreFileInfo(final Configuration conf, final FileSystem fs, final Path i
152166
*/
153167
public StoreFileInfo(final Configuration conf, final FileSystem fs, final FileStatus fileStatus)
154168
throws IOException {
155-
this(conf, fs, fileStatus.getPath());
169+
this(conf, fs, fileStatus, fileStatus.getPath());
156170
}
157171

158172
/**
@@ -207,6 +221,14 @@ public StoreFileInfo(final Configuration conf, final FileSystem fs, final FileSt
207221
this.link = link;
208222
}
209223

224+
/**
225+
* Size of the Hfile
226+
* @return size
227+
*/
228+
public long getSize() {
229+
return size;
230+
}
231+
210232
/**
211233
* Sets the region coprocessor env.
212234
* @param coprocessorHost

hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotManifestV2.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,11 @@ public void storeFile(final SnapshotRegionManifest.Builder region,
129129
if (storeFile.isReference()) {
130130
sfManifest.setReference(storeFile.getReference().convert());
131131
}
132-
sfManifest.setFileSize(storeFile.getReferencedFileStatus(rootFs).getLen());
132+
if (!storeFile.isReference() && !storeFile.isLink()) {
133+
sfManifest.setFileSize(storeFile.getSize());
134+
} else {
135+
sfManifest.setFileSize(storeFile.getReferencedFileStatus(rootFs).getLen());
136+
}
133137
family.addStoreFiles(sfManifest.build());
134138
}
135139
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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

Comments
 (0)