Skip to content

Commit 47be1ab

Browse files
HADOOP-18679. Add API for bulk/paged delete of files (#6726)
Applications can create a BulkDelete instance from a BulkDeleteSource; the BulkDelete interface provides the pageSize(): the maximum number of entries which can be deleted, and a bulkDelete(Collection paths) method which can take a collection up to pageSize() long. This is optimized for object stores with bulk delete APIs; the S3A connector will offer the page size of fs.s3a.bulk.delete.page.size unless bulk delete has been disabled. Even with a page size of 1, the S3A implementation is more efficient than delete(path) as there are no safety checks for the path being a directory or probes for the need to recreate directories. The interface BulkDeleteSource is implemented by all FileSystem implementations, with a page size of 1 and mapped to delete(pathToDelete, false). This means that callers do not need to have special case handling for object stores versus classic filesystems. To aid use through reflection APIs, the class org.apache.hadoop.io.wrappedio.WrappedIO has been created with "reflection friendly" methods. Contributed by Mukund Thakur and Steve Loughran
1 parent 41eacf4 commit 47be1ab

File tree

35 files changed

+2679
-119
lines changed

35 files changed

+2679
-119
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
19+
package org.apache.hadoop.fs;
20+
21+
import java.io.Closeable;
22+
import java.io.IOException;
23+
import java.util.Collection;
24+
import java.util.List;
25+
import java.util.Map;
26+
27+
import org.apache.hadoop.classification.InterfaceAudience;
28+
import org.apache.hadoop.classification.InterfaceStability;
29+
import org.apache.hadoop.fs.statistics.IOStatisticsSource;
30+
31+
import static java.util.Objects.requireNonNull;
32+
33+
/**
34+
* API for bulk deletion of objects/files,
35+
* <i>but not directories</i>.
36+
* After use, call {@code close()} to release any resources and
37+
* to guarantee store IOStatistics are updated.
38+
* <p>
39+
* Callers MUST have no expectation that parent directories will exist after the
40+
* operation completes; if an object store needs to explicitly look for and create
41+
* directory markers, that step will be omitted.
42+
* <p>
43+
* Be aware that on some stores (AWS S3) each object listed in a bulk delete counts
44+
* against the write IOPS limit; large page sizes are counterproductive here, as
45+
* are attempts at parallel submissions across multiple threads.
46+
* @see <a href="https://issues.apache.org/jira/browse/HADOOP-16823">HADOOP-16823.
47+
* Large DeleteObject requests are their own Thundering Herd</a>
48+
*/
49+
@InterfaceAudience.Public
50+
@InterfaceStability.Unstable
51+
public interface BulkDelete extends IOStatisticsSource, Closeable {
52+
53+
/**
54+
* The maximum number of objects/files to delete in a single request.
55+
* @return a number greater than zero.
56+
*/
57+
int pageSize();
58+
59+
/**
60+
* Base path of a bulk delete operation.
61+
* All paths submitted in {@link #bulkDelete(Collection)} must be under this path.
62+
* @return base path of a bulk delete operation.
63+
*/
64+
Path basePath();
65+
66+
/**
67+
* Delete a list of files/objects.
68+
* <ul>
69+
* <li>Files must be under the path provided in {@link #basePath()}.</li>
70+
* <li>The size of the list must be equal to or less than the page size
71+
* declared in {@link #pageSize()}.</li>
72+
* <li>Directories are not supported; the outcome of attempting to delete
73+
* directories is undefined (ignored; undetected, listed as failures...).</li>
74+
* <li>The operation is not atomic.</li>
75+
* <li>The operation is treated as idempotent: network failures may
76+
* trigger resubmission of the request -any new objects created under a
77+
* path in the list may then be deleted.</li>
78+
* <li>There is no guarantee that any parent directories exist after this call.
79+
* </li>
80+
* </ul>
81+
* @param paths list of paths which must be absolute and under the base path.
82+
* provided in {@link #basePath()}.
83+
* @return a list of paths which failed to delete, with the exception message.
84+
* @throws IOException IO problems including networking, authentication and more.
85+
* @throws IllegalArgumentException if a path argument is invalid.
86+
*/
87+
List<Map.Entry<Path, String>> bulkDelete(Collection<Path> paths)
88+
throws IOException, IllegalArgumentException;
89+
90+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
19+
package org.apache.hadoop.fs;
20+
21+
import java.io.IOException;
22+
23+
import org.apache.hadoop.classification.InterfaceAudience;
24+
import org.apache.hadoop.classification.InterfaceStability;
25+
26+
/**
27+
* Interface for bulk deletion.
28+
* Filesystems which support bulk deletion should implement this interface
29+
* and MUST also declare their support in the path capability
30+
* {@link CommonPathCapabilities#BULK_DELETE}.
31+
* Exporting the interface does not guarantee that the operation is supported;
32+
* returning a {@link BulkDelete} object from the call {@link #createBulkDelete(Path)}
33+
* is.
34+
*/
35+
@InterfaceAudience.Public
36+
@InterfaceStability.Unstable
37+
public interface BulkDeleteSource {
38+
39+
/**
40+
* Create a bulk delete operation.
41+
* There is no network IO at this point, simply the creation of
42+
* a bulk delete object.
43+
* A path must be supplied to assist in link resolution.
44+
* @param path path to delete under.
45+
* @return the bulk delete.
46+
* @throws UnsupportedOperationException bulk delete under that path is not supported.
47+
* @throws IllegalArgumentException path not valid.
48+
* @throws IOException problems resolving paths
49+
*/
50+
BulkDelete createBulkDelete(Path path)
51+
throws UnsupportedOperationException, IllegalArgumentException, IOException;
52+
53+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
* <p>
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
* <p>
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.fs;
19+
20+
import java.util.Collection;
21+
22+
import static java.util.Objects.requireNonNull;
23+
import static org.apache.hadoop.util.Preconditions.checkArgument;
24+
25+
/**
26+
* Utility class for bulk delete operations.
27+
*/
28+
public final class BulkDeleteUtils {
29+
30+
private BulkDeleteUtils() {
31+
}
32+
33+
/**
34+
* Preconditions for bulk delete paths.
35+
* @param paths paths to delete.
36+
* @param pageSize maximum number of paths to delete in a single operation.
37+
* @param basePath base path for the delete operation.
38+
*/
39+
public static void validateBulkDeletePaths(Collection<Path> paths, int pageSize, Path basePath) {
40+
requireNonNull(paths);
41+
checkArgument(paths.size() <= pageSize,
42+
"Number of paths (%d) is larger than the page size (%d)", paths.size(), pageSize);
43+
paths.forEach(p -> {
44+
checkArgument(p.isAbsolute(), "Path %s is not absolute", p);
45+
checkArgument(validatePathIsUnderParent(p, basePath),
46+
"Path %s is not under the base path %s", p, basePath);
47+
});
48+
}
49+
50+
/**
51+
* Check if a path is under a base path.
52+
* @param p path to check.
53+
* @param basePath base path.
54+
* @return true if the path is under the base path.
55+
*/
56+
public static boolean validatePathIsUnderParent(Path p, Path basePath) {
57+
while (p.getParent() != null) {
58+
if (p.getParent().equals(basePath)) {
59+
return true;
60+
}
61+
p = p.getParent();
62+
}
63+
return false;
64+
}
65+
66+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,10 @@ private CommonPathCapabilities() {
181181
*/
182182
public static final String DIRECTORY_LISTING_INCONSISTENT =
183183
"fs.capability.directory.listing.inconsistent";
184+
185+
/**
186+
* Capability string to probe for bulk delete: {@value}.
187+
*/
188+
public static final String BULK_DELETE = "fs.capability.bulk.delete";
189+
184190
}

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

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.apache.hadoop.fs.Options.HandleOpt;
5757
import org.apache.hadoop.fs.Options.Rename;
5858
import org.apache.hadoop.fs.impl.AbstractFSBuilderImpl;
59+
import org.apache.hadoop.fs.impl.DefaultBulkDeleteOperation;
5960
import org.apache.hadoop.fs.impl.FutureDataInputStreamBuilderImpl;
6061
import org.apache.hadoop.fs.impl.OpenFileParameters;
6162
import org.apache.hadoop.fs.permission.AclEntry;
@@ -169,7 +170,8 @@
169170
@InterfaceAudience.Public
170171
@InterfaceStability.Stable
171172
public abstract class FileSystem extends Configured
172-
implements Closeable, DelegationTokenIssuer, PathCapabilities {
173+
implements Closeable, DelegationTokenIssuer,
174+
PathCapabilities, BulkDeleteSource {
173175
public static final String FS_DEFAULT_NAME_KEY =
174176
CommonConfigurationKeys.FS_DEFAULT_NAME_KEY;
175177
public static final String DEFAULT_FS =
@@ -3485,12 +3487,16 @@ public Collection<FileStatus> getTrashRoots(boolean allUsers) {
34853487
public boolean hasPathCapability(final Path path, final String capability)
34863488
throws IOException {
34873489
switch (validatePathCapabilityArgs(makeQualified(path), capability)) {
3488-
case CommonPathCapabilities.FS_SYMLINKS:
3489-
// delegate to the existing supportsSymlinks() call.
3490-
return supportsSymlinks() && areSymlinksEnabled();
3491-
default:
3492-
// the feature is not implemented.
3493-
return false;
3490+
case CommonPathCapabilities.BULK_DELETE:
3491+
// bulk delete has default implementation which
3492+
// can called on any FileSystem.
3493+
return true;
3494+
case CommonPathCapabilities.FS_SYMLINKS:
3495+
// delegate to the existing supportsSymlinks() call.
3496+
return supportsSymlinks() && areSymlinksEnabled();
3497+
default:
3498+
// the feature is not implemented.
3499+
return false;
34943500
}
34953501
}
34963502

@@ -4976,4 +4982,18 @@ public MultipartUploaderBuilder createMultipartUploader(Path basePath)
49764982
methodNotSupported();
49774983
return null;
49784984
}
4985+
4986+
/**
4987+
* Create a bulk delete operation.
4988+
* The default implementation returns an instance of {@link DefaultBulkDeleteOperation}.
4989+
* @param path base path for the operation.
4990+
* @return an instance of the bulk delete.
4991+
* @throws IllegalArgumentException any argument is invalid.
4992+
* @throws IOException if there is an IO problem.
4993+
*/
4994+
@Override
4995+
public BulkDelete createBulkDelete(Path path)
4996+
throws IllegalArgumentException, IOException {
4997+
return new DefaultBulkDeleteOperation(path, this);
4998+
}
49794999
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.fs.impl;
19+
20+
import java.io.IOException;
21+
import java.util.ArrayList;
22+
import java.util.Collection;
23+
import java.util.List;
24+
import java.util.Map;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
import org.apache.hadoop.fs.BulkDelete;
30+
import org.apache.hadoop.fs.FileSystem;
31+
import org.apache.hadoop.fs.Path;
32+
import org.apache.hadoop.util.functional.Tuples;
33+
34+
import static java.util.Objects.requireNonNull;
35+
import static org.apache.hadoop.fs.BulkDeleteUtils.validateBulkDeletePaths;
36+
37+
/**
38+
* Default implementation of the {@link BulkDelete} interface.
39+
*/
40+
public class DefaultBulkDeleteOperation implements BulkDelete {
41+
42+
private static Logger LOG = LoggerFactory.getLogger(DefaultBulkDeleteOperation.class);
43+
44+
/** Default page size for bulk delete. */
45+
private static final int DEFAULT_PAGE_SIZE = 1;
46+
47+
/** Base path for the bulk delete operation. */
48+
private final Path basePath;
49+
50+
/** Delegate File system make actual delete calls. */
51+
private final FileSystem fs;
52+
53+
public DefaultBulkDeleteOperation(Path basePath,
54+
FileSystem fs) {
55+
this.basePath = requireNonNull(basePath);
56+
this.fs = fs;
57+
}
58+
59+
@Override
60+
public int pageSize() {
61+
return DEFAULT_PAGE_SIZE;
62+
}
63+
64+
@Override
65+
public Path basePath() {
66+
return basePath;
67+
}
68+
69+
/**
70+
* {@inheritDoc}.
71+
* The default impl just calls {@code FileSystem.delete(path, false)}
72+
* on the single path in the list.
73+
*/
74+
@Override
75+
public List<Map.Entry<Path, String>> bulkDelete(Collection<Path> paths)
76+
throws IOException, IllegalArgumentException {
77+
validateBulkDeletePaths(paths, DEFAULT_PAGE_SIZE, basePath);
78+
List<Map.Entry<Path, String>> result = new ArrayList<>();
79+
if (!paths.isEmpty()) {
80+
// As the page size is always 1, this should be the only one
81+
// path in the collection.
82+
Path pathToDelete = paths.iterator().next();
83+
try {
84+
fs.delete(pathToDelete, false);
85+
} catch (IOException ex) {
86+
LOG.debug("Couldn't delete {} - exception occurred: {}", pathToDelete, ex);
87+
result.add(Tuples.pair(pathToDelete, ex.toString()));
88+
}
89+
}
90+
return result;
91+
}
92+
93+
@Override
94+
public void close() throws IOException {
95+
96+
}
97+
}

0 commit comments

Comments
 (0)