Skip to content

Commit fde8128

Browse files
committed
HBASE-23197 'IllegalArgumentException: Wrong FS' on edits replay when… (#740)
Signed-off-by: Josh Elser <elserj@apache.org>
1 parent 8ddcffb commit fde8128

File tree

7 files changed

+281
-10
lines changed

7 files changed

+281
-10
lines changed

hbase-server/src/main/java/org/apache/hadoop/hbase/backup/HFileArchiver.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@
3737
import org.apache.hadoop.fs.FileSystem;
3838
import org.apache.hadoop.fs.Path;
3939
import org.apache.hadoop.fs.PathFilter;
40+
import org.apache.hadoop.hbase.HConstants;
4041
import org.apache.hadoop.hbase.client.RegionInfo;
4142
import org.apache.hadoop.hbase.regionserver.HStoreFile;
4243
import org.apache.hadoop.hbase.util.Bytes;
44+
import org.apache.hadoop.hbase.util.CommonFSUtils;
4345
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
4446
import org.apache.hadoop.hbase.util.FSUtils;
4547
import org.apache.hadoop.hbase.util.HFileArchiveUtil;
@@ -296,8 +298,45 @@ public static void archiveFamilyByFamilyDir(FileSystem fs, Configuration conf,
296298
*/
297299
public static void archiveStoreFiles(Configuration conf, FileSystem fs, RegionInfo regionInfo,
298300
Path tableDir, byte[] family, Collection<HStoreFile> compactedFiles)
299-
throws IOException, FailedArchiveException {
301+
throws IOException {
302+
Path storeArchiveDir = HFileArchiveUtil.getStoreArchivePath(conf, regionInfo, tableDir, family);
303+
archive(fs, regionInfo, family, compactedFiles, storeArchiveDir);
304+
}
305+
306+
/**
307+
* Archive recovered edits using existing logic for archiving store files. This is currently only
308+
* relevant when <b>hbase.region.archive.recovered.edits</b> is true, as recovered edits shouldn't
309+
* be kept after replay. In theory, we could use very same method available for archiving
310+
* store files, but supporting WAL dir and store files on different FileSystems added the need for
311+
* extra validation of the passed FileSystem instance and the path where the archiving edits
312+
* should be placed.
313+
* @param conf {@link Configuration} to determine the archive directory.
314+
* @param fs the filesystem used for storing WAL files.
315+
* @param regionInfo {@link RegionInfo} a pseudo region representation for the archiving logic.
316+
* @param family a pseudo familiy representation for the archiving logic.
317+
* @param replayedEdits the recovered edits to be archived.
318+
* @throws IOException if files can't be achived due to some internal error.
319+
*/
320+
public static void archiveRecoveredEdits(Configuration conf, FileSystem fs, RegionInfo regionInfo,
321+
byte[] family, Collection<HStoreFile> replayedEdits)
322+
throws IOException {
323+
String workingDir = conf.get(CommonFSUtils.HBASE_WAL_DIR, conf.get(HConstants.HBASE_DIR));
324+
//extra sanity checks for the right FS
325+
Path path = new Path(workingDir);
326+
if(path.isAbsoluteAndSchemeAuthorityNull()){
327+
//no schema specified on wal dir value, so it's on same FS as StoreFiles
328+
path = new Path(conf.get(HConstants.HBASE_DIR));
329+
}
330+
if(path.toUri().getScheme()!=null && !path.toUri().getScheme().equals(fs.getScheme())){
331+
throw new IOException("Wrong file system! Should be " + path.toUri().getScheme() +
332+
", but got " + fs.getScheme());
333+
}
334+
path = HFileArchiveUtil.getStoreArchivePathForRootDir(path, regionInfo, family);
335+
archive(fs, regionInfo, family, replayedEdits, path);
336+
}
300337

338+
private static void archive(FileSystem fs, RegionInfo regionInfo, byte[] family,
339+
Collection<HStoreFile> compactedFiles, Path storeArchiveDir) throws IOException {
301340
// sometimes in testing, we don't have rss, so we need to check for that
302341
if (fs == null) {
303342
LOG.warn("Passed filesystem is null, so just deleting files without archiving for {}," +
@@ -315,9 +354,6 @@ public static void archiveStoreFiles(Configuration conf, FileSystem fs, RegionIn
315354
// build the archive path
316355
if (regionInfo == null || family == null) throw new IOException(
317356
"Need to have a region and a family to archive from.");
318-
319-
Path storeArchiveDir = HFileArchiveUtil.getStoreArchivePath(conf, regionInfo, tableDir, family);
320-
321357
// make sure we don't archive if we can't and that the archive dir exists
322358
if (!fs.mkdirs(storeArchiveDir)) {
323359
throw new IOException("Could not make archive directory (" + storeArchiveDir + ") for store:"

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,8 +1896,8 @@ public HRegionFileSystem getRegionFileSystem() {
18961896
}
18971897

18981898
/** @return the WAL {@link HRegionFileSystem} used by this region */
1899-
HRegionFileSystem getRegionWALFileSystem() throws IOException {
1900-
return new HRegionFileSystem(conf, getWalFileSystem(),
1899+
HRegionWALFileSystem getRegionWALFileSystem() throws IOException {
1900+
return new HRegionWALFileSystem(conf, getWalFileSystem(),
19011901
FSUtils.getWALTableDir(conf, htableDescriptor.getTableName()), fs.getRegionInfo());
19021902
}
19031903

@@ -4590,7 +4590,7 @@ protected long replayRecoveredEditsIfAny(Map<byte[], Long> maxSeqIdInStores,
45904590
for (Path file : files) {
45914591
fakeStoreFiles.add(new HStoreFile(walFS, file, this.conf, null, null, true));
45924592
}
4593-
getRegionWALFileSystem().removeStoreFiles(fakeFamilyName, fakeStoreFiles);
4593+
getRegionWALFileSystem().archiveRecoveredEdits(fakeFamilyName, fakeStoreFiles);
45944594
} else {
45954595
for (Path file : Iterables.concat(files, filesUnderWrongRegionWALDir)) {
45964596
if (!walFS.delete(file, false)) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ public class HRegionFileSystem {
8080

8181
private final RegionInfo regionInfo;
8282
//regionInfo for interacting with FS (getting encodedName, etc)
83-
private final RegionInfo regionInfoForFs;
84-
private final Configuration conf;
83+
final RegionInfo regionInfoForFs;
84+
final Configuration conf;
8585
private final Path tableDir;
86-
private final FileSystem fs;
86+
final FileSystem fs;
8787
private final Path regionDir;
8888

8989
/**
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.regionserver;
19+
20+
import java.io.IOException;
21+
import java.util.Collection;
22+
import org.apache.hadoop.conf.Configuration;
23+
import org.apache.hadoop.fs.FileSystem;
24+
import org.apache.hadoop.fs.Path;
25+
import org.apache.hadoop.hbase.backup.HFileArchiver;
26+
import org.apache.hadoop.hbase.client.RegionInfo;
27+
import org.apache.hadoop.hbase.util.Bytes;
28+
import org.apache.yetus.audience.InterfaceAudience;
29+
30+
/**
31+
* A Wrapper for the region FileSystem operations adding WAL specific operations
32+
*/
33+
@InterfaceAudience.Private
34+
public class HRegionWALFileSystem extends HRegionFileSystem {
35+
36+
HRegionWALFileSystem(Configuration conf, FileSystem fs, Path tableDir, RegionInfo regionInfo) {
37+
super(conf, fs, tableDir, regionInfo);
38+
}
39+
40+
/**
41+
* Closes and archives the specified store files from the specified family.
42+
* @param familyName Family that contains the store filesMeta
43+
* @param storeFiles set of store files to remove
44+
* @throws IOException if the archiving fails
45+
*/
46+
public void archiveRecoveredEdits(String familyName, Collection<HStoreFile> storeFiles)
47+
throws IOException {
48+
HFileArchiver.archiveRecoveredEdits(this.conf, this.fs, this.regionInfoForFs,
49+
Bytes.toBytes(familyName), storeFiles);
50+
}
51+
}

hbase-server/src/main/java/org/apache/hadoop/hbase/util/HFileArchiveUtil.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,23 @@ public static Path getStoreArchivePath(Configuration conf,
8585
return HStore.getStoreHomedir(tableArchiveDir, region, family);
8686
}
8787

88+
/**
89+
* Gets the archive directory under specified root dir. One scenario where this is useful is
90+
* when WAL and root dir are configured under different file systems,
91+
* i.e. root dir on S3 and WALs on HDFS.
92+
* This is mostly useful for archiving recovered edits, when
93+
* <b>hbase.region.archive.recovered.edits</b> is enabled.
94+
* @param rootDir {@link Path} the root dir under which archive path should be created.
95+
* @param region parent region information under which the store currently lives
96+
* @param family name of the family in the store
97+
* @return {@link Path} to the WAL FS directory to archive the given store
98+
* or <tt>null</tt> if it should not be archived
99+
*/
100+
public static Path getStoreArchivePathForRootDir(Path rootDir, RegionInfo region, byte[] family) {
101+
Path tableArchiveDir = getTableArchivePath(rootDir, region.getTable());
102+
return HStore.getStoreHomedir(tableArchiveDir, region, family);
103+
}
104+
88105
/**
89106
* Get the archive directory for a given region under the specified table
90107
* @param tableName the table name. Cannot be null.

hbase-server/src/test/java/org/apache/hadoop/hbase/backup/TestHFileArchiving.java

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@
2020
import static org.junit.Assert.assertEquals;
2121
import static org.junit.Assert.assertFalse;
2222
import static org.junit.Assert.assertTrue;
23+
import static org.mockito.ArgumentMatchers.any;
24+
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.times;
26+
import static org.mockito.Mockito.verify;
27+
import static org.mockito.Mockito.when;
2328

2429
import java.io.IOException;
2530
import java.security.PrivilegedExceptionAction;
2631
import java.util.ArrayList;
32+
import java.util.Collection;
2733
import java.util.Collections;
2834
import java.util.List;
2935
import java.util.stream.Collectors;
@@ -39,15 +45,18 @@
3945
import org.apache.hadoop.hbase.Stoppable;
4046
import org.apache.hadoop.hbase.TableName;
4147
import org.apache.hadoop.hbase.client.Admin;
48+
import org.apache.hadoop.hbase.client.RegionInfo;
4249
import org.apache.hadoop.hbase.client.Table;
4350
import org.apache.hadoop.hbase.master.cleaner.DirScanPool;
4451
import org.apache.hadoop.hbase.master.cleaner.HFileCleaner;
4552
import org.apache.hadoop.hbase.regionserver.ConstantSizeRegionSplitPolicy;
4653
import org.apache.hadoop.hbase.regionserver.HRegion;
4754
import org.apache.hadoop.hbase.regionserver.HRegionServer;
55+
import org.apache.hadoop.hbase.regionserver.HStoreFile;
4856
import org.apache.hadoop.hbase.testclassification.MediumTests;
4957
import org.apache.hadoop.hbase.testclassification.MiscTests;
5058
import org.apache.hadoop.hbase.util.Bytes;
59+
import org.apache.hadoop.hbase.util.CommonFSUtils;
5160
import org.apache.hadoop.hbase.util.FSUtils;
5261
import org.apache.hadoop.hbase.util.HFileArchiveTestingUtil;
5362
import org.apache.hadoop.hbase.util.HFileArchiveUtil;
@@ -62,6 +71,7 @@
6271
import org.junit.Test;
6372
import org.junit.experimental.categories.Category;
6473
import org.junit.rules.TestName;
74+
import org.mockito.ArgumentCaptor;
6575
import org.slf4j.Logger;
6676
import org.slf4j.LoggerFactory;
6777

@@ -123,6 +133,107 @@ public static void cleanupTest() throws Exception {
123133
POOL.shutdownNow();
124134
}
125135

136+
@Test
137+
public void testArchiveStoreFilesDifferentFileSystemsWallWithSchemaPlainRoot() throws Exception {
138+
String walDir = "mockFS://mockFSAuthority:9876/mockDir/wals/";
139+
String baseDir = FSUtils.getRootDir(UTIL.getConfiguration()).toString() + "/";
140+
testArchiveStoreFilesDifferentFileSystems(walDir, baseDir,
141+
HFileArchiver::archiveStoreFiles);
142+
}
143+
144+
@Test
145+
public void testArchiveStoreFilesDifferentFileSystemsWallNullPlainRoot() throws Exception {
146+
String baseDir = FSUtils.getRootDir(UTIL.getConfiguration()).toString() + "/";
147+
testArchiveStoreFilesDifferentFileSystems(null, baseDir,
148+
HFileArchiver::archiveStoreFiles);
149+
}
150+
151+
@Test
152+
public void testArchiveStoreFilesDifferentFileSystemsWallAndRootSame() throws Exception {
153+
String baseDir = FSUtils.getRootDir(UTIL.getConfiguration()).toString() + "/";
154+
testArchiveStoreFilesDifferentFileSystems("/hbase/wals/", baseDir,
155+
HFileArchiver::archiveStoreFiles);
156+
}
157+
158+
private void testArchiveStoreFilesDifferentFileSystems(String walDir, String expectedBase,
159+
ArchivingFunction<Configuration, FileSystem, RegionInfo, Path, byte[],
160+
Collection<HStoreFile>> archivingFunction) throws IOException {
161+
FileSystem mockedFileSystem = mock(FileSystem.class);
162+
Configuration conf = new Configuration(UTIL.getConfiguration());
163+
if(walDir != null) {
164+
conf.set(CommonFSUtils.HBASE_WAL_DIR, walDir);
165+
}
166+
Path filePath = new Path("/mockDir/wals/mockFile");
167+
when(mockedFileSystem.getScheme()).thenReturn("mockFS");
168+
when(mockedFileSystem.mkdirs(any())).thenReturn(true);
169+
when(mockedFileSystem.exists(any())).thenReturn(true);
170+
RegionInfo mockedRegion = mock(RegionInfo.class);
171+
TableName tableName = TableName.valueOf("mockTable");
172+
when(mockedRegion.getTable()).thenReturn(tableName);
173+
when(mockedRegion.getEncodedName()).thenReturn("mocked-region-encoded-name");
174+
Path tableDir = new Path("mockFS://mockDir/tabledir");
175+
byte[] family = Bytes.toBytes("testfamily");
176+
HStoreFile mockedFile = mock(HStoreFile.class);
177+
List<HStoreFile> list = new ArrayList<>();
178+
list.add(mockedFile);
179+
when(mockedFile.getPath()).thenReturn(filePath);
180+
when(mockedFileSystem.rename(any(),any())).thenReturn(true);
181+
archivingFunction.apply(conf, mockedFileSystem, mockedRegion, tableDir, family, list);
182+
ArgumentCaptor<Path> pathCaptor = ArgumentCaptor.forClass(Path.class);
183+
verify(mockedFileSystem, times(2)).rename(pathCaptor.capture(), any());
184+
String expectedDir = expectedBase +
185+
"archive/data/default/mockTable/mocked-region-encoded-name/testfamily/mockFile";
186+
assertTrue(pathCaptor.getAllValues().get(0).toString().equals(expectedDir));
187+
}
188+
189+
@FunctionalInterface
190+
private interface ArchivingFunction<Configuration, FS, Region, Dir, Family, Files> {
191+
void apply(Configuration config, FS fs, Region region, Dir dir, Family family, Files files)
192+
throws IOException;
193+
}
194+
195+
@Test
196+
public void testArchiveRecoveredEditsWalDirNull() throws Exception {
197+
testArchiveRecoveredEditsWalDirNullOrSame(null);
198+
}
199+
200+
@Test
201+
public void testArchiveRecoveredEditsWalDirSameFsStoreFiles() throws Exception {
202+
testArchiveRecoveredEditsWalDirNullOrSame("/wal-dir");
203+
}
204+
205+
private void testArchiveRecoveredEditsWalDirNullOrSame(String walDir) throws Exception {
206+
String originalRootDir = UTIL.getConfiguration().get(HConstants.HBASE_DIR);
207+
try {
208+
String baseDir = "mockFS://mockFSAuthority:9876/hbase/";
209+
UTIL.getConfiguration().set(HConstants.HBASE_DIR, baseDir);
210+
testArchiveStoreFilesDifferentFileSystems(walDir, baseDir,
211+
(conf, fs, region, dir, family, list) -> HFileArchiver
212+
.archiveRecoveredEdits(conf, fs, region, family, list));
213+
} finally {
214+
UTIL.getConfiguration().set(HConstants.HBASE_DIR, originalRootDir);
215+
}
216+
}
217+
218+
@Test(expected = IOException.class)
219+
public void testArchiveRecoveredEditsWrongFS() throws Exception {
220+
String baseDir = FSUtils.getRootDir(UTIL.getConfiguration()).toString() + "/";
221+
//Internally, testArchiveStoreFilesDifferentFileSystems will pass a "mockedFS"
222+
// to HFileArchiver.archiveRecoveredEdits, but since wal-dir is supposedly on same FS
223+
// as root dir it would lead to conflicting FSes and an IOException is expected.
224+
testArchiveStoreFilesDifferentFileSystems("/wal-dir", baseDir,
225+
(conf, fs, region, dir, family, list) -> HFileArchiver
226+
.archiveRecoveredEdits(conf, fs, region, family, list));
227+
}
228+
229+
@Test
230+
public void testArchiveRecoveredEditsWalDirDifferentFS() throws Exception {
231+
String walDir = "mockFS://mockFSAuthority:9876/mockDir/wals/";
232+
testArchiveStoreFilesDifferentFileSystems(walDir, walDir,
233+
(conf, fs, region, dir, family, list) ->
234+
HFileArchiver.archiveRecoveredEdits(conf, fs, region, family, list));
235+
}
236+
126237
@Test
127238
public void testRemoveRegionDirOnArchive() throws Exception {
128239
final TableName tableName = TableName.valueOf(name.getMethodName());

0 commit comments

Comments
 (0)