Skip to content

Commit 4183ac9

Browse files
committed
HADOOP-19217. Introduce getTrashPolicy to FileSystem API
1 parent d9c3308 commit 4183ac9

File tree

9 files changed

+603
-11
lines changed

9 files changed

+603
-11
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileSystem.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3429,6 +3429,29 @@ public Collection<? extends BlockStoragePolicySpi> getAllStoragePolicies()
34293429
+ " doesn't support getAllStoragePolicies");
34303430
}
34313431

3432+
/**
3433+
* Get the trash policy implementation used by this FileSystem. This trash policy
3434+
* is used by classes of {@link Trash} to implement the trash behavior.
3435+
* <p>
3436+
* FileSystem implementation can consider overriding this method to handle
3437+
* situation where a single FileSystem client shares a configuration, but
3438+
* each FileSystem scheme requires a distinct TrashPolicy implementation.
3439+
*
3440+
* @param conf configuration which can be used to choose the TrashPolicy
3441+
* implementation.
3442+
* @return TrashPolicy implementation by this filesystem.
3443+
* The default implementation returns the configured TrashPolicy
3444+
* based on the value of the configuration parameter fs.trash.classname
3445+
* of the passed configuration.
3446+
*/
3447+
@InterfaceAudience.Public
3448+
@InterfaceStability.Unstable
3449+
public TrashPolicy getTrashPolicy(Configuration conf) {
3450+
Class<? extends TrashPolicy> trashClass = conf.getClass(
3451+
"fs.trash.classname", TrashPolicyDefault.class, TrashPolicy.class);
3452+
return ReflectionUtils.newInstance(trashClass, conf);
3453+
}
3454+
34323455
/**
34333456
* Get the root directory of Trash for current user when the path specified
34343457
* is deleted.

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FilterFileSystem.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,11 @@ public Collection<? extends BlockStoragePolicySpi> getAllStoragePolicies()
686686
return fs.getAllStoragePolicies();
687687
}
688688

689+
@Override
690+
public TrashPolicy getTrashPolicy(Configuration conf) {
691+
return fs.getTrashPolicy(conf);
692+
}
693+
689694
@Override
690695
public Path getTrashRoot(Path path) {
691696
return fs.getTrashRoot(path);

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/TrashPolicy.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ public Path getCurrentTrashDir(Path path) throws IOException {
126126
*/
127127
public abstract Runnable getEmptier() throws IOException;
128128

129+
/**
130+
* Return the deletion interval associated with the trash policy. Deletion interval
131+
* can be used as trash emptier {@link #getEmptier()} check interval, as well as used
132+
* as the trash expiry configuration used in the checkpoint {@link #deleteCheckpoint()}
133+
* @return deletion interval.
134+
*/
135+
public final long getDeletionInterval() {
136+
return deletionInterval;
137+
}
138+
129139
/**
130140
* Get an instance of the configured TrashPolicy based on the value
131141
* of the configuration parameter fs.trash.classname.
@@ -146,18 +156,19 @@ public static TrashPolicy getInstance(Configuration conf, FileSystem fs, Path ho
146156
}
147157

148158
/**
149-
* Get an instance of the configured TrashPolicy based on the value
150-
* of the configuration parameter fs.trash.classname.
159+
* Get an instance of the TrashPolicy associated with the FileSystem implementation of
160+
* {@link FileSystem#getTrashPolicy(Configuration)}. The configuration passed might be used
161+
* by the FileSystem implementation to pick the {@link TrashPolicy} implementation. The default
162+
* {@link FileSystem#getTrashPolicy(Configuration)} checks fs.trash.classname to pick the
163+
* {@link TrashPolicy} implementation.
151164
*
152165
* @param conf the configuration to be used
153166
* @param fs the file system to be used
154167
* @return an instance of TrashPolicy
155168
*/
156169
public static TrashPolicy getInstance(Configuration conf, FileSystem fs) {
157-
Class<? extends TrashPolicy> trashClass = conf.getClass(
158-
"fs.trash.classname", TrashPolicyDefault.class, TrashPolicy.class);
159-
TrashPolicy trash = ReflectionUtils.newInstance(trashClass, conf);
160-
trash.initialize(conf, fs); // initialize TrashPolicy
161-
return trash;
170+
TrashPolicy trashPolicy = fs.getTrashPolicy(conf);
171+
trashPolicy.initialize(conf, fs); // initialize TrashPolicy
172+
return trashPolicy;
162173
}
163174
}

hadoop-common-project/hadoop-common/src/site/markdown/filesystem/filesystem.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,36 @@ The path does not have to exist, but the path does need to be valid and reconcil
636636
* The path returned is a directory
637637

638638

639+
### `TrashPolicy getTrashPolicy(Configuration conf)`
640+
641+
Get the trash policy implementation used by this FileSystem.
642+
643+
This method allows different FileSystem implementations to use different TrashPolicy
644+
implementations. This is important in environments where multiple FileSystem schemes
645+
are used (e.g., HDFS and Ozone), as each may require a distinct TrashPolicy.
646+
647+
#### Preconditions
648+
649+
#### Postconditions
650+
651+
result = a valid TrashPolicy instance associated with the FileSystem implementation
652+
653+
The default implementation:
654+
1. Reads the configuration parameter `fs.trash.classname` (defaults to `TrashPolicyDefault`)
655+
2. Instantiates the specified TrashPolicy class
656+
3. Initializes the TrashPolicy with the given configuration
657+
4. Returns the initialized TrashPolicy
658+
659+
#### Implementation Notes
660+
661+
* FileSystem implementations MAY override this method to provide filesystem-specific
662+
TrashPolicy implementations. For example, Ozone `getTrashPolicy` can return its custom trash policy,
663+
while HDFS can still use `TrashPolicyDefault`.
664+
* The returned TrashPolicy should not be null.
665+
* FileSystem implementations with multiple child file systems (e.g. `ViewFileSystem`)
666+
should NOT implement this method since the Hadoop trash mechanism should resolve to the underlying filesystem
667+
before invoking `getTrashPolicy`.
668+
639669
## <a name="state_changing_operations"></a> State Changing Operations
640670

641671
### `boolean mkdirs(Path p, FsPermission permission)`

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestHarFileSystem.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ public BlockStoragePolicySpi getStoragePolicy(final Path src)
229229
public Collection<? extends BlockStoragePolicySpi> getAllStoragePolicies()
230230
throws IOException;
231231

232+
public TrashPolicy getTrashPolicy(Configuration conf);
233+
232234
public Path getTrashRoot(Path path) throws IOException;
233235

234236
public Collection<FileStatus> getTrashRoots(boolean allUsers) throws IOException;

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/TestTrash.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import org.junit.jupiter.api.Test;
4040
import org.slf4j.Logger;
4141
import org.slf4j.LoggerFactory;
42+
43+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
4244
import static org.junit.jupiter.api.Assertions.assertTrue;
4345
import static org.junit.jupiter.api.Assertions.assertFalse;
4446
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -629,10 +631,23 @@ public void testNonDefaultFS() throws IOException {
629631
public void testPluggableTrash() throws IOException {
630632
Configuration conf = new Configuration();
631633

632-
// Test plugged TrashPolicy
633-
conf.setClass("fs.trash.classname", TestTrashPolicy.class, TrashPolicy.class);
634-
Trash trash = new Trash(conf);
635-
assertTrue(trash.getTrashPolicy().getClass().equals(TestTrashPolicy.class));
634+
{
635+
// Test plugged TrashPolicy
636+
conf.setClass("fs.trash.classname", TestTrashPolicy.class, TrashPolicy.class);
637+
Trash trash = new Trash(conf);
638+
assertInstanceOf(TestTrashPolicy.class, trash.getTrashPolicy());
639+
}
640+
641+
{
642+
// Test FileSystem implementation that implements getTrashPolicy to return custom TrashPolicy
643+
// regardless of fs.trash.classname
644+
conf.setClass("fs.file.impl", TestLFSWithCustomTrashPolicy.class, FileSystem.class);
645+
conf.setBoolean("fs.file.impl.disable.cache", true);
646+
FileSystem fs = FileSystem.getLocal(conf);
647+
conf.set("fs.defaultFS", fs.getUri().toString());
648+
Trash trash = new Trash(fs, conf);
649+
assertInstanceOf(TestLFSWithCustomTrashPolicy.CustomTrashPolicy.class, trash.getTrashPolicy());
650+
}
636651
}
637652

638653
@Test
@@ -873,6 +888,24 @@ public void setUri(String uri){
873888
uriName = URI.create(uri);
874889
}
875890
}
891+
892+
public static class TestLFSWithCustomTrashPolicy extends TestLFS {
893+
894+
@Override
895+
public String getScheme() {
896+
return "testlfswithcustomtrashpolicy";
897+
}
898+
899+
@Override
900+
public TrashPolicy getTrashPolicy(Configuration conf) {
901+
return new CustomTrashPolicy();
902+
}
903+
904+
public static class CustomTrashPolicy extends TrashPolicyDefault {
905+
}
906+
}
907+
908+
876909

877910
/**
878911
* test same file deletion - multiple time

0 commit comments

Comments
 (0)