Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HADOOP-18679. Add API for bulk/paged delete of files #6726

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.fs;

import java.io.Closeable;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.fs.statistics.IOStatisticsSource;

import static java.util.Objects.requireNonNull;

/**
* API for bulk deletion of objects/files,
* <i>but not directories</i>.
* After use, call {@code close()} to release any resources and
* to guarantee store IOStatistics are updated.
* <p>
* Callers MUST have no expectation that parent directories will exist after the
* operation completes; if an object store needs to explicitly look for and create
* directory markers, that step will be omitted.
* <p>
* Be aware that on some stores (AWS S3) each object listed in a bulk delete counts
* against the write IOPS limit; large page sizes are counterproductive here, as
* are attempts at parallel submissions across multiple threads.
* @see <a href="https://issues.apache.org/jira/browse/HADOOP-16823">HADOOP-16823.
* Large DeleteObject requests are their own Thundering Herd</a>
*/
@InterfaceAudience.Public
@InterfaceStability.Unstable
public interface BulkDelete extends IOStatisticsSource, Closeable {

/**
* The maximum number of objects/files to delete in a single request.
* @return a number greater than zero.
*/
int pageSize();

/**
* Base path of a bulk delete operation.
* All paths submitted in {@link #bulkDelete(Collection)} must be under this path.
* @return base path of a bulk delete operation.
*/
Path basePath();

/**
* Delete a list of files/objects.
* <ul>
* <li>Files must be under the path provided in {@link #basePath()}.</li>
* <li>The size of the list must be equal to or less than the page size
* declared in {@link #pageSize()}.</li>
* <li>Directories are not supported; the outcome of attempting to delete
* directories is undefined (ignored; undetected, listed as failures...).</li>
* <li>The operation is not atomic.</li>
* <li>The operation is treated as idempotent: network failures may
* trigger resubmission of the request -any new objects created under a
* path in the list may then be deleted.</li>
Comment on lines +75 to +77
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is great that we call this out, but can we do better at the API level?

Had the API took Collection<FileStatus>, we could make the operation idempotent in most clouds by passing the version/generationid/etag in the request.

* <li>There is no guarantee that any parent directories exist after this call.
* </li>
* </ul>
* @param paths list of paths which must be absolute and under the base path.
* provided in {@link #basePath()}.
* @return a list of paths which failed to delete, with the exception message.
* @throws IOException IO problems including networking, authentication and more.
* @throws IllegalArgumentException if a path argument is invalid.
*/
List<Map.Entry<Path, String>> bulkDelete(Collection<Path> paths)
throws IOException, IllegalArgumentException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.fs;

import java.io.IOException;

import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;

/**
* Interface for bulk deletion.
* Filesystems which support bulk deletion should implement this interface
* and MUST also declare their support in the path capability
* {@link CommonPathCapabilities#BULK_DELETE}.
* Exporting the interface does not guarantee that the operation is supported;
* returning a {@link BulkDelete} object from the call {@link #createBulkDelete(Path)}
* is.
*/
@InterfaceAudience.Public
@InterfaceStability.Unstable
public interface BulkDeleteSource {

/**
* Create a bulk delete operation.
* There is no network IO at this point, simply the creation of
* a bulk delete object.
* A path must be supplied to assist in link resolution.
* @param path path to delete under.
* @return the bulk delete.
* @throws UnsupportedOperationException bulk delete under that path is not supported.
* @throws IllegalArgumentException path not valid.
* @throws IOException problems resolving paths
*/
BulkDelete createBulkDelete(Path path)
throws UnsupportedOperationException, IllegalArgumentException, IOException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.fs;

import java.util.Collection;

import static java.util.Objects.requireNonNull;
import static org.apache.hadoop.util.Preconditions.checkArgument;

/**
* Utility class for bulk delete operations.
*/
public final class BulkDeleteUtils {

private BulkDeleteUtils() {
}

/**
* Preconditions for bulk delete paths.
* @param paths paths to delete.
* @param pageSize maximum number of paths to delete in a single operation.
* @param basePath base path for the delete operation.
*/
public static void validateBulkDeletePaths(Collection<Path> paths, int pageSize, Path basePath) {
requireNonNull(paths);
checkArgument(paths.size() <= pageSize,
"Number of paths (%d) is larger than the page size (%d)", paths.size(), pageSize);
paths.forEach(p -> {
checkArgument(p.isAbsolute(), "Path %s is not absolute", p);
checkArgument(validatePathIsUnderParent(p, basePath),
"Path %s is not under the base path %s", p, basePath);
});
}

/**
* Check if a path is under a base path.
* @param p path to check.
* @param basePath base path.
* @return true if the path is under the base path.
*/
public static boolean validatePathIsUnderParent(Path p, Path basePath) {
while (p.getParent() != null) {
if (p.getParent().equals(basePath)) {
return true;
}
p = p.getParent();
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,10 @@ private CommonPathCapabilities() {
*/
public static final String DIRECTORY_LISTING_INCONSISTENT =
"fs.capability.directory.listing.inconsistent";

/**
* Capability string to probe for bulk delete: {@value}.
*/
public static final String BULK_DELETE = "fs.capability.bulk.delete";

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.apache.hadoop.fs.Options.HandleOpt;
import org.apache.hadoop.fs.Options.Rename;
import org.apache.hadoop.fs.impl.AbstractFSBuilderImpl;
import org.apache.hadoop.fs.impl.DefaultBulkDeleteOperation;
import org.apache.hadoop.fs.impl.FutureDataInputStreamBuilderImpl;
import org.apache.hadoop.fs.impl.OpenFileParameters;
import org.apache.hadoop.fs.permission.AclEntry;
Expand Down Expand Up @@ -169,7 +170,8 @@
@InterfaceAudience.Public
@InterfaceStability.Stable
public abstract class FileSystem extends Configured
implements Closeable, DelegationTokenIssuer, PathCapabilities {
implements Closeable, DelegationTokenIssuer,
PathCapabilities, BulkDeleteSource {
public static final String FS_DEFAULT_NAME_KEY =
CommonConfigurationKeys.FS_DEFAULT_NAME_KEY;
public static final String DEFAULT_FS =
Expand Down Expand Up @@ -3485,12 +3487,16 @@ public Collection<FileStatus> getTrashRoots(boolean allUsers) {
public boolean hasPathCapability(final Path path, final String capability)
throws IOException {
switch (validatePathCapabilityArgs(makeQualified(path), capability)) {
case CommonPathCapabilities.FS_SYMLINKS:
// delegate to the existing supportsSymlinks() call.
return supportsSymlinks() && areSymlinksEnabled();
default:
// the feature is not implemented.
return false;
case CommonPathCapabilities.BULK_DELETE:
// bulk delete has default implementation which
// can called on any FileSystem.
return true;
case CommonPathCapabilities.FS_SYMLINKS:
// delegate to the existing supportsSymlinks() call.
return supportsSymlinks() && areSymlinksEnabled();
default:
// the feature is not implemented.
return false;
}
}

Expand Down Expand Up @@ -4976,4 +4982,18 @@ public MultipartUploaderBuilder createMultipartUploader(Path basePath)
methodNotSupported();
return null;
}

/**
* Create a bulk delete operation.
* The default implementation returns an instance of {@link DefaultBulkDeleteOperation}.
* @param path base path for the operation.
* @return an instance of the bulk delete.
* @throws IllegalArgumentException any argument is invalid.
* @throws IOException if there is an IO problem.
*/
@Override
public BulkDelete createBulkDelete(Path path)
throws IllegalArgumentException, IOException {
return new DefaultBulkDeleteOperation(path, this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.fs.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.hadoop.fs.BulkDelete;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.util.functional.Tuples;

import static java.util.Objects.requireNonNull;
import static org.apache.hadoop.fs.BulkDeleteUtils.validateBulkDeletePaths;

/**
* Default implementation of the {@link BulkDelete} interface.
*/
public class DefaultBulkDeleteOperation implements BulkDelete {

private static Logger LOG = LoggerFactory.getLogger(DefaultBulkDeleteOperation.class);

/** Default page size for bulk delete. */
private static final int DEFAULT_PAGE_SIZE = 1;

/** Base path for the bulk delete operation. */
private final Path basePath;

/** Delegate File system make actual delete calls. */
private final FileSystem fs;

public DefaultBulkDeleteOperation(Path basePath,
FileSystem fs) {
this.basePath = requireNonNull(basePath);
this.fs = fs;
}

@Override
public int pageSize() {
return DEFAULT_PAGE_SIZE;
}

@Override
public Path basePath() {
return basePath;
}

/**
* {@inheritDoc}.
* The default impl just calls {@code FileSystem.delete(path, false)}
* on the single path in the list.
*/
@Override
public List<Map.Entry<Path, String>> bulkDelete(Collection<Path> paths)
throws IOException, IllegalArgumentException {
validateBulkDeletePaths(paths, DEFAULT_PAGE_SIZE, basePath);
List<Map.Entry<Path, String>> result = new ArrayList<>();
if (!paths.isEmpty()) {
// As the page size is always 1, this should be the only one
// path in the collection.
Path pathToDelete = paths.iterator().next();
try {
fs.delete(pathToDelete, false);
} catch (IOException ex) {
LOG.debug("Couldn't delete {} - exception occurred: {}", pathToDelete, ex);
result.add(Tuples.pair(pathToDelete, ex.toString()));
}
}
return result;
}

@Override
public void close() throws IOException {

}
}
Loading
Loading