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

HDFS-13616. Batch listing of multiple directories #1725

Merged
merged 4 commits into from
Jan 16, 2020
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
Expand Up @@ -2207,6 +2207,33 @@ public RemoteIterator<FileStatus> listStatusIterator(final Path p)
return new DirListingIterator<>(p);
}

/**
* Batched listing API that returns {@link PartialListing}s for the
* passed Paths.
*
* @param paths List of paths to list.
* @return RemoteIterator that returns corresponding PartialListings.
* @throws IOException
*/
public RemoteIterator<PartialListing<FileStatus>> batchedListStatusIterator(
final List<Path> paths) throws IOException {
throw new UnsupportedOperationException("Not implemented");
}

/**
* Batched listing API that returns {@link PartialListing}s for the passed
* Paths. The PartialListing will contain {@link LocatedFileStatus} entries
* with locations.
*
* @param paths List of paths to list.
* @return RemoteIterator that returns corresponding PartialListings.
* @throws IOException
*/
public RemoteIterator<PartialListing<LocatedFileStatus>> batchedListLocatedStatusIterator(
final List<Path> paths) throws IOException {
throw new UnsupportedOperationException("Not implemented");
}

/**
* List the statuses and block locations of the files in the given path.
* Does not guarantee to return the iterator that traverses statuses
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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 com.google.common.base.Preconditions;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;
import org.apache.hadoop.ipc.RemoteException;

import java.io.IOException;
import java.util.List;

/**
* A partial listing of the children of a parent directory. Since it is a
* partial listing, multiple PartialListing may need to be combined to obtain
* the full listing of a parent directory.
* <p/>
* ListingBatch behaves similar to a Future, in that getting the result via
* {@link #get()} will throw an Exception if there was a failure.
*/
@InterfaceAudience.Public
@InterfaceStability.Stable
public class PartialListing<T extends FileStatus> {
private final Path listedPath;
private final List<T> partialListing;
private final RemoteException exception;

public PartialListing(Path listedPath, List<T> partialListing) {
this(listedPath, partialListing, null);
}

public PartialListing(Path listedPath, RemoteException exception) {
this(listedPath, null, exception);
}

private PartialListing(Path listedPath, List<T> partialListing,
RemoteException exception) {
Preconditions.checkArgument(partialListing == null ^ exception == null);
this.partialListing = partialListing;
this.listedPath = listedPath;
this.exception = exception;
}

/**
* Partial listing of the path being listed. In the case where the path is
* a file. The list will be a singleton with the file itself.
*
* @return Partial listing of the path being listed.
* @throws IOException if there was an exception getting the listing.
*/
public List<T> get() throws IOException {
if (exception != null) {
throw exception.unwrapRemoteException();
}
return partialListing;
}

/**
* Path being listed.
*
* @return the path being listed.
*/
public Path getListedPath() {
return listedPath;
}

@Override
public String toString() {
return new ToStringBuilder(this)
.append("listedPath", listedPath)
.append("partialListing", partialListing)
.append("exception", exception)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.net.URI;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.hadoop.conf.Configuration;
Expand Down Expand Up @@ -105,6 +106,10 @@ public FSDataOutputStream create(Path f, FsPermission permission,
public FileStatus[] listStatusBatch(Path f, byte[] token);
public FileStatus[] listStatus(Path[] files);
public FileStatus[] listStatus(Path[] files, PathFilter filter);
public RemoteIterator<PartialListing<LocatedFileStatus>> batchedListLocatedStatusIterator(
final List<Path> paths) throws IOException;
public RemoteIterator<PartialListing<FileStatus>> batchedListStatusIterator(
final List<Path> paths) throws IOException;
public FileStatus[] globStatus(Path pathPattern);
public FileStatus[] globStatus(Path pathPattern, PathFilter filter);
public Iterator<LocatedFileStatus> listFiles(Path path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ public FSDataOutputStream create(Path f, FsPermission permission,
public FileStatus[] listStatusBatch(Path f, byte[] token);
public FileStatus[] listStatus(Path[] files);
public FileStatus[] listStatus(Path[] files, PathFilter filter);
public RemoteIterator<PartialListing<LocatedFileStatus>> batchedListLocatedStatusIterator(
final List<Path> paths) throws IOException;
public RemoteIterator<PartialListing<FileStatus>> batchedListStatusIterator(
final List<Path> paths) throws IOException;
public FileStatus[] globStatus(Path pathPattern);
public FileStatus[] globStatus(Path pathPattern, PathFilter filter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<Class name="org.apache.hadoop.hdfs.inotify.EventBatch"/>
<Class name="org.apache.hadoop.hdfs.protocol.HdfsFileStatus"/>
<Class name="org.apache.hadoop.hdfs.protocol.LocatedBlock"/>
<Class name="org.apache.hadoop.hdfs.protocol.BatchedDirectoryListing" />
<Class name="org.apache.hadoop.hdfs.protocol.BlockStoragePolicy"/>
<Class name="org.apache.hadoop.hdfs.protocol.CorruptFileBlocks"/>
<Class name="org.apache.hadoop.hdfs.protocol.StripedBlockInfo"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
import org.apache.hadoop.hdfs.net.Peer;
import org.apache.hadoop.hdfs.protocol.AclException;
import org.apache.hadoop.hdfs.protocol.AddErasureCodingPolicyResponse;
import org.apache.hadoop.hdfs.protocol.BatchedDirectoryListing;
import org.apache.hadoop.hdfs.protocol.BlockStoragePolicy;
import org.apache.hadoop.hdfs.protocol.CacheDirectiveEntry;
import org.apache.hadoop.hdfs.protocol.CacheDirectiveInfo;
Expand Down Expand Up @@ -1674,6 +1675,24 @@ public DirectoryListing listPaths(String src, byte[] startAfter,
}
}

/**
* Get a batched listing for the indicated directories
*
* @see ClientProtocol#getBatchedListing(String[], byte[], boolean)
*/
public BatchedDirectoryListing batchedListPaths(
String[] srcs, byte[] startAfter, boolean needLocation)
throws IOException {
checkOpen();
try {
return namenode.getBatchedListing(srcs, startAfter, needLocation);
} catch(RemoteException re) {
throw re.unwrapRemoteException(AccessControlException.class,
FileNotFoundException.class,
UnresolvedPathException.class);
}
}

/**
* Get the file info for a specific file or directory.
* @param src The string representation of the path to the file
Expand All @@ -1693,7 +1712,7 @@ public HdfsFileStatus getFileInfo(String src) throws IOException {
}
}

/**
/**
* Get the file info for a specific file or directory.
* @param src The string representation of the path to the file
* @param needBlockToken Include block tokens in {@link LocatedBlocks}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import org.apache.commons.collections.list.TreeList;
import org.apache.hadoop.HadoopIllegalArgumentException;
import org.apache.hadoop.classification.InterfaceAudience;
Expand Down Expand Up @@ -49,6 +50,7 @@
import org.apache.hadoop.fs.GlobalStorageStatistics;
import org.apache.hadoop.fs.GlobalStorageStatistics.StorageStatisticsProvider;
import org.apache.hadoop.fs.InvalidPathHandleException;
import org.apache.hadoop.fs.PartialListing;
import org.apache.hadoop.fs.PathHandle;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Options;
Expand All @@ -73,6 +75,7 @@
import org.apache.hadoop.hdfs.client.HdfsDataOutputStream;
import org.apache.hadoop.hdfs.client.impl.CorruptFileBlockIterator;
import org.apache.hadoop.hdfs.protocol.AddErasureCodingPolicyResponse;
import org.apache.hadoop.hdfs.protocol.BatchedDirectoryListing;
import org.apache.hadoop.hdfs.protocol.BlockStoragePolicy;
import org.apache.hadoop.hdfs.protocol.CacheDirectiveEntry;
import org.apache.hadoop.hdfs.protocol.CacheDirectiveInfo;
Expand All @@ -81,6 +84,7 @@
import org.apache.hadoop.hdfs.protocol.ClientProtocol;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
import org.apache.hadoop.hdfs.protocol.DirectoryListing;
import org.apache.hadoop.hdfs.protocol.HdfsPartialListing;
import org.apache.hadoop.hdfs.protocol.EncryptionZone;
import org.apache.hadoop.hdfs.protocol.ErasureCodingPolicy;
import org.apache.hadoop.hdfs.protocol.ErasureCodingPolicyInfo;
Expand Down Expand Up @@ -108,6 +112,8 @@
import org.apache.hadoop.security.token.DelegationTokenIssuer;
import org.apache.hadoop.util.ChunkedArrayList;
import org.apache.hadoop.util.Progressable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import java.io.FileNotFoundException;
Expand All @@ -120,6 +126,7 @@
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;

import static org.apache.hadoop.fs.impl.PathCapabilitiesSupport.validatePathCapabilityArgs;
Expand Down Expand Up @@ -1292,6 +1299,110 @@ public T next() throws IOException {
}
}

@Override
public RemoteIterator<PartialListing<FileStatus>> batchedListStatusIterator(
final List<Path> paths) throws IOException {
List<Path> absPaths = Lists.newArrayListWithCapacity(paths.size());
for (Path p : paths) {
absPaths.add(fixRelativePart(p));
}
return new PartialListingIterator<>(absPaths, false);
}

@Override
public RemoteIterator<PartialListing<LocatedFileStatus>> batchedListLocatedStatusIterator(
final List<Path> paths) throws IOException {
List<Path> absPaths = Lists.newArrayListWithCapacity(paths.size());
for (Path p : paths) {
absPaths.add(fixRelativePart(p));
}
return new PartialListingIterator<>(absPaths, true);
}

private static final Logger LBI_LOG =
LoggerFactory.getLogger(PartialListingIterator.class);

private class PartialListingIterator<T extends FileStatus>
implements RemoteIterator<PartialListing<T>> {

private List<Path> paths;
private String[] srcs;
private boolean needLocation;
private BatchedDirectoryListing batchedListing;
private int listingIdx = 0;

PartialListingIterator(List<Path> paths, boolean needLocation)
throws IOException {
this.paths = paths;
this.srcs = new String[paths.size()];
for (int i = 0; i < paths.size(); i++) {
this.srcs[i] = getPathName(paths.get(i));
}
this.needLocation = needLocation;

// Do the first listing
statistics.incrementReadOps(1);
storageStatistics.incrementOpCounter(OpType.LIST_LOCATED_STATUS);
batchedListing = dfs.batchedListPaths(
srcs, HdfsFileStatus.EMPTY_NAME, needLocation);
LBI_LOG.trace("Got batchedListing: {}", batchedListing);
if (batchedListing == null) { // the directory does not exist
throw new FileNotFoundException("One or more paths do not exist.");
}
}

@Override
public boolean hasNext() throws IOException {
if (batchedListing == null) {
return false;
}
// If we're done with the current batch, try to get the next batch
if (listingIdx >= batchedListing.getListings().length) {
if (!batchedListing.hasMore()) {
LBI_LOG.trace("No more elements");
return false;
}
batchedListing = dfs.batchedListPaths(
srcs, batchedListing.getStartAfter(), needLocation);
LBI_LOG.trace("Got batchedListing: {}", batchedListing);
listingIdx = 0;
}
return listingIdx < batchedListing.getListings().length;
}

@Override
@SuppressWarnings("unchecked")
public PartialListing<T> next() throws IOException {
if (!hasNext()) {
throw new NoSuchElementException("No more entries");
}
HdfsPartialListing listing = batchedListing.getListings()[listingIdx];
listingIdx++;

Path parent = paths.get(listing.getParentIdx());

if (listing.getException() != null) {
return new PartialListing<>(parent, listing.getException());
}

// Qualify paths for the client.
List<HdfsFileStatus> statuses = listing.getPartialListing();
List<T> qualifiedStatuses =
Lists.newArrayListWithCapacity(statuses.size());

for (HdfsFileStatus status : statuses) {
if (needLocation) {
qualifiedStatuses.add((T)((HdfsLocatedFileStatus) status)
.makeQualifiedLocated(getUri(), parent));
} else {
qualifiedStatuses.add((T)status.makeQualified(getUri(), parent));
}
}

return new PartialListing<>(parent, qualifiedStatuses);
}
}

/**
* Create a directory, only when the parent directories exist.
*
Expand Down
Loading