Skip to content

Commit 4efcff0

Browse files
bshashikantShashikant Banerjee
authored andcommitted
HDFS-15483. Ordered snapshot deletion: Disallow rename between two snapshottable directories. (apache#2172)
(cherry picked from commit 092bfe7) Change-Id: I4d2b8105dc2f9fbebb9c79100edfb1b1c36f3347
1 parent af0ddce commit 4efcff0

File tree

4 files changed

+172
-15
lines changed

4 files changed

+172
-15
lines changed

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirRenameOp.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.hadoop.hdfs.server.namenode.FSDirectory.DirOp;
3333
import org.apache.hadoop.hdfs.server.namenode.INode.BlocksMapUpdateInfo;
3434
import org.apache.hadoop.hdfs.server.namenode.snapshot.Snapshot;
35+
import org.apache.hadoop.hdfs.server.namenode.snapshot.SnapshotManager;
3536
import org.apache.hadoop.hdfs.util.ReadOnlyList;
3637
import org.apache.hadoop.util.ChunkedArrayList;
3738
import org.apache.hadoop.util.Time;
@@ -192,7 +193,7 @@ static INodesInPath unprotectedRenameTo(FSDirectory fsd,
192193
}
193194

194195
validateNestSnapshot(fsd, src, dstParent.asDirectory(), snapshottableDirs);
195-
196+
checkUnderSameSnapshottableRoot(fsd, srcIIP, dstIIP);
196197
fsd.ezManager.checkMoveValidity(srcIIP, dstIIP);
197198
// Ensure dst has quota to accommodate rename
198199
verifyFsLimitsForRename(fsd, srcIIP, dstIIP);
@@ -406,6 +407,7 @@ static RenameResult unprotectedRenameTo(FSDirectory fsd,
406407

407408
validateNestSnapshot(fsd, src,
408409
dstParent.asDirectory(), srcSnapshottableDirs);
410+
checkUnderSameSnapshottableRoot(fsd, srcIIP, dstIIP);
409411

410412
// Ensure dst has quota to accommodate rename
411413
verifyFsLimitsForRename(fsd, srcIIP, dstIIP);
@@ -811,6 +813,29 @@ void updateQuotasInSourceTree(BlockStoragePolicySuite bsps) {
811813
}
812814
}
813815

816+
private static void checkUnderSameSnapshottableRoot(
817+
FSDirectory fsd, INodesInPath srcIIP, INodesInPath dstIIP)
818+
throws IOException {
819+
// Ensure rename out of a snapshottable root is not permitted if ordered
820+
// snapshot deletion feature is enabled
821+
SnapshotManager snapshotManager = fsd.getFSNamesystem().
822+
getSnapshotManager();
823+
if (snapshotManager.isSnapshotDeletionOrdered() && fsd.getFSNamesystem()
824+
.isSnapshotTrashRootEnabled()) {
825+
INodeDirectory srcRoot = snapshotManager.
826+
getSnapshottableAncestorDir(srcIIP);
827+
INodeDirectory dstRoot = snapshotManager.
828+
getSnapshottableAncestorDir(dstIIP);
829+
// Ensure snapshoottable root for both src and dest are same.
830+
if (srcRoot != dstRoot) {
831+
String errMsg = "Source " + srcIIP.getPath() +
832+
" and dest " + dstIIP.getPath() + " are not under " +
833+
"the same snapshot root.";
834+
throw new SnapshotException(errMsg);
835+
}
836+
}
837+
}
838+
814839
private static RenameResult createRenameResult(FSDirectory fsd,
815840
INodesInPath dst, boolean filesDeleted,
816841
BlocksMapUpdateInfo collectedBlocks) throws IOException {

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,10 @@ public class FSNamesystem implements Namesystem, FSNamesystemMBean,
373373
.getLogger(FSNamesystem.class.getName());
374374

375375
// The following are private configurations
376-
static final String DFS_NAMENODE_SNAPSHOT_TRASHROOT_ENABLED =
376+
public static final String DFS_NAMENODE_SNAPSHOT_TRASHROOT_ENABLED =
377377
"dfs.namenode.snapshot.trashroot.enabled";
378-
static final boolean DFS_NAMENODE_SNAPSHOT_TRASHROOT_ENABLED_DEFAULT = false;
378+
public static final boolean DFS_NAMENODE_SNAPSHOT_TRASHROOT_ENABLED_DEFAULT
379+
= false;
379380

380381
private final MetricsRegistry registry = new MetricsRegistry("FSNamesystem");
381382
@Metric final MutableRatesWithAggregation detailedLockHoldTimeMetrics =
@@ -455,6 +456,7 @@ private void logAuditEvent(boolean succeeded,
455456
private final UserGroupInformation fsOwner;
456457
private final String supergroup;
457458
private final boolean standbyShouldCheckpoint;
459+
private final boolean isSnapshotTrashRootEnabled;
458460
private final int snapshotDiffReportLimit;
459461
private final int blockDeletionIncrement;
460462

@@ -837,6 +839,9 @@ static FSNamesystem loadFromDisk(Configuration conf) throws IOException {
837839
// Get the checksum type from config
838840
String checksumTypeStr = conf.get(DFS_CHECKSUM_TYPE_KEY,
839841
DFS_CHECKSUM_TYPE_DEFAULT);
842+
this.isSnapshotTrashRootEnabled = conf.getBoolean(
843+
DFS_NAMENODE_SNAPSHOT_TRASHROOT_ENABLED,
844+
DFS_NAMENODE_SNAPSHOT_TRASHROOT_ENABLED_DEFAULT);
840845
DataChecksum.Type checksumType;
841846
try {
842847
checksumType = DataChecksum.Type.valueOf(checksumTypeStr);
@@ -864,8 +869,7 @@ static FSNamesystem loadFromDisk(Configuration conf) throws IOException {
864869
CommonConfigurationKeysPublic.HADOOP_SECURITY_KEY_PROVIDER_PATH,
865870
""),
866871
blockManager.getStoragePolicySuite().getDefaultPolicy().getId(),
867-
conf.getBoolean(DFS_NAMENODE_SNAPSHOT_TRASHROOT_ENABLED,
868-
DFS_NAMENODE_SNAPSHOT_TRASHROOT_ENABLED_DEFAULT));
872+
isSnapshotTrashRootEnabled);
869873

870874
this.maxFsObjects = conf.getLong(DFS_NAMENODE_MAX_OBJECTS_KEY,
871875
DFS_NAMENODE_MAX_OBJECTS_DEFAULT);
@@ -1006,6 +1010,10 @@ public int getMaxListOpenFilesResponses() {
10061010
return maxListOpenFilesResponses;
10071011
}
10081012

1013+
boolean isSnapshotTrashRootEnabled() {
1014+
return isSnapshotTrashRootEnabled;
1015+
}
1016+
10091017
void lockRetryCache() {
10101018
if (retryCache != null) {
10111019
retryCache.lock();

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/snapshot/SnapshotManager.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -365,21 +365,35 @@ void assertFirstSnapshot(INodeDirectory dir,
365365
* @param iip INodesInPath for the directory to get snapshot root.
366366
* @return the snapshot root INodeDirectory
367367
*/
368+
public INodeDirectory checkAndGetSnapshottableAncestorDir(
369+
final INodesInPath iip) throws IOException {
370+
final INodeDirectory dir = getSnapshottableAncestorDir(iip);
371+
if (dir == null) {
372+
throw new SnapshotException("Directory is neither snapshottable nor" +
373+
" under a snap root!");
374+
}
375+
return dir;
376+
}
377+
368378
public INodeDirectory getSnapshottableAncestorDir(final INodesInPath iip)
369379
throws IOException {
370380
final String path = iip.getPath();
371-
final INodeDirectory dir = INodeDirectory.valueOf(iip.getLastINode(), path);
381+
final INode inode = iip.getLastINode();
382+
final INodeDirectory dir;
383+
if (inode instanceof INodeDirectory) {
384+
dir = INodeDirectory.valueOf(inode, path);
385+
} else {
386+
dir = INodeDirectory.valueOf(iip.getINode(-2), iip.getParentPath());
387+
}
372388
if (dir.isSnapshottable()) {
373389
return dir;
374-
} else {
375-
for (INodeDirectory snapRoot : this.snapshottables.values()) {
376-
if (dir.isAncestorDirectory(snapRoot)) {
377-
return snapRoot;
378-
}
390+
}
391+
for (INodeDirectory snapRoot : this.snapshottables.values()) {
392+
if (dir.isAncestorDirectory(snapRoot)) {
393+
return snapRoot;
379394
}
380-
throw new SnapshotException("Directory is neither snapshottable nor" +
381-
" under a snap root!");
382395
}
396+
return null;
383397
}
384398

385399
public boolean isDescendantOfSnapshotRoot(INodeDirectory dir) {
@@ -639,7 +653,7 @@ public SnapshotDiffReport diff(final INodesInPath iip,
639653
// All the check for path has been included in the valueOf method.
640654
INodeDirectory snapshotRootDir;
641655
if (this.snapshotDiffAllowSnapRootDescendant) {
642-
snapshotRootDir = getSnapshottableAncestorDir(iip);
656+
snapshotRootDir = checkAndGetSnapshottableAncestorDir(iip);
643657
} else {
644658
snapshotRootDir = getSnapshottableRoot(iip);
645659
}
@@ -672,7 +686,7 @@ public SnapshotDiffReportListing diff(final INodesInPath iip,
672686
// All the check for path has been included in the valueOf method.
673687
INodeDirectory snapshotRootDir;
674688
if (this.snapshotDiffAllowSnapRootDescendant) {
675-
snapshotRootDir = getSnapshottableAncestorDir(iip);
689+
snapshotRootDir = checkAndGetSnapshottableAncestorDir(iip);
676690
} else {
677691
snapshotRootDir = getSnapshottableRoot(iip);
678692
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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.hdfs.server.namenode.snapshot;
19+
20+
import org.apache.hadoop.conf.Configuration;
21+
import org.apache.hadoop.fs.Path;
22+
import org.apache.hadoop.hdfs.DistributedFileSystem;
23+
import org.apache.hadoop.hdfs.MiniDFSCluster;
24+
import org.apache.hadoop.hdfs.DFSTestUtil;
25+
import org.junit.After;
26+
import org.junit.Assert;
27+
import org.junit.Before;
28+
import org.junit.Test;
29+
30+
31+
import java.io.IOException;
32+
33+
import static org.apache.hadoop.hdfs.server.namenode.snapshot.SnapshotManager.DFS_NAMENODE_SNAPSHOT_DELETION_ORDERED;
34+
import static org.apache.hadoop.hdfs.server.namenode.FSNamesystem.DFS_NAMENODE_SNAPSHOT_TRASHROOT_ENABLED;
35+
/**
36+
* Test Rename with ordered snapshot deletion.
37+
*/
38+
public class TestRenameWithOrderedSnapshotDeletion {
39+
private final Path snapshottableDir
40+
= new Path("/" + getClass().getSimpleName());
41+
private DistributedFileSystem hdfs;
42+
private MiniDFSCluster cluster;
43+
44+
@Before
45+
public void setUp() throws Exception {
46+
final Configuration conf = new Configuration();
47+
conf.setBoolean(DFS_NAMENODE_SNAPSHOT_DELETION_ORDERED, true);
48+
conf.setBoolean(DFS_NAMENODE_SNAPSHOT_TRASHROOT_ENABLED, true);
49+
50+
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(0).build();
51+
cluster.waitActive();
52+
hdfs = cluster.getFileSystem();
53+
}
54+
55+
@After
56+
public void tearDown() throws Exception {
57+
if (cluster != null) {
58+
cluster.shutdown();
59+
cluster = null;
60+
}
61+
}
62+
63+
@Test(timeout = 60000)
64+
public void testRename() throws Exception {
65+
final Path dir1 = new Path("/dir1");
66+
final Path dir2 = new Path("/dir2");
67+
final Path sub0 = new Path(snapshottableDir, "sub0");
68+
final Path sub1 = new Path(snapshottableDir, "sub1");
69+
hdfs.mkdirs(sub0);
70+
hdfs.mkdirs(dir2);
71+
final Path file1 = new Path(dir1, "file1");
72+
final Path file2 = new Path(sub0, "file2");
73+
hdfs.mkdirs(snapshottableDir);
74+
hdfs.mkdirs(dir1);
75+
hdfs.mkdirs(dir2);
76+
hdfs.mkdirs(sub0);
77+
DFSTestUtil.createFile(hdfs, file1, 0, (short) 1, 0);
78+
DFSTestUtil.createFile(hdfs, file2, 0, (short) 1, 0);
79+
hdfs.allowSnapshot(snapshottableDir);
80+
// rename from non snapshottable dir to snapshottable dir should fail
81+
validateRename(file1, sub0);
82+
hdfs.createSnapshot(snapshottableDir, "s0");
83+
validateRename(file1, sub0);
84+
// rename across non snapshottable dirs should work
85+
hdfs.rename(file1, dir2);
86+
// rename beyond snapshottable root should fail
87+
validateRename(file2, dir1);
88+
// rename within snapshottable root should work
89+
hdfs.rename(file2, snapshottableDir);
90+
91+
// rename dirs outside snapshottable root should work
92+
hdfs.rename(dir2, dir1);
93+
// rename dir into snapshottable root should fail
94+
validateRename(dir1, snapshottableDir);
95+
// rename dir outside snapshottable root should fail
96+
validateRename(sub0, dir2);
97+
// rename dir within snapshottable root should work
98+
hdfs.rename(sub0, sub1);
99+
}
100+
101+
private void validateRename(Path src, Path dest) {
102+
try {
103+
hdfs.rename(src, dest);
104+
Assert.fail("Expected exception not thrown.");
105+
} catch (IOException ioe) {
106+
Assert.assertTrue(ioe.getMessage().contains("are not under the" +
107+
" same snapshot root."));
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)