Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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,43 @@
/*
* 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 org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability;

import java.io.IOException;

/**
* Interface filesystems MAY implement to offer a batched operations.
*/

@InterfaceAudience.Public
@InterfaceStability.Unstable
public interface BatchOperations {
Copy link
Contributor

Choose a reason for hiding this comment

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

BatchRename

Copy link
Author

Choose a reason for hiding this comment

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

I change to BatchRename, I thought we may add other batch method in this interface in the future.


/**
* Batched rename API that rename a batch of files.
*
* @param srcs source file list.
* @param dsts target file list.
* @throws IOException failure exception.
*/
void batchRename(String[] srcs, String[] dsts, Options.Rename... options)
Copy link
Contributor

Choose a reason for hiding this comment

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

Prefer list or array of pairs. We don't have any pair type in hadoop here and can't use commons-lang as we don't want that in our public API. Maybe we should add one to org.apache.hadoop.common.utils and use it here amongst other places. I could certainly use it (I may be able to add this to HADOOP-16830 for you to pick up)

Return a future where we define RenameResult as something (class/interface) which implements IOStatisticsSource.

Copy link
Author

Choose a reason for hiding this comment

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

OK, I'll integrate this later.

throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,11 @@ private CommonPathCapabilities() {
public static final String FS_MULTIPART_UPLOADER =
"fs.capability.multipart.uploader";

/**
* Does the store support multipart uploading?
Copy link
Contributor

Choose a reason for hiding this comment

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

fix

Copy link
Author

Choose a reason for hiding this comment

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

Done

* Value: {@value}.
*/
public static final String FS_BATCH_RENAME =
"fs.capability.batch.rename";

}
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.BatchOpsException;
import org.apache.hadoop.hdfs.protocol.BatchedDirectoryListing;
import org.apache.hadoop.hdfs.protocol.BlockStoragePolicy;
import org.apache.hadoop.hdfs.protocol.CacheDirectiveEntry;
Expand Down Expand Up @@ -1609,6 +1610,26 @@ public void rename(String src, String dst, Options.Rename... options)
}
}

/**
* Rename a batch files or directories.
* @see ClientProtocol#batchRename(String[] , String[], Options.Rename...)
*/
public void batchRename(String[] srcs, String[] dsts,
Copy link
Contributor

Choose a reason for hiding this comment

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

better as a list of <src, dest> pairs, so it's obvious about the mapping. Add javadocs

Copy link
Author

Choose a reason for hiding this comment

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

OK.

Options.Rename... options) throws IOException {
checkOpen();
try {
namenode.batchRename(srcs, dsts, options);
} catch(RemoteException re) {
throw re.unwrapRemoteException(AccessControlException.class,
NSQuotaExceededException.class,
DSQuotaExceededException.class,
UnresolvedPathException.class,
SnapshotAccessControlException.class,
BatchOpsException.class);
}
}


/**
* Truncate a file to an indicated size
* See {@link ClientProtocol#truncate}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public enum OpType {
GET_SNAPSHOT_LIST("op_get_snapshot_list"),
TRUNCATE(CommonStatisticNames.OP_TRUNCATE),
UNSET_EC_POLICY("op_unset_ec_policy"),
UNSET_STORAGE_POLICY("op_unset_storage_policy");
UNSET_STORAGE_POLICY("op_unset_storage_policy"),
BATCH_RENAME("op_batch_rename");

private static final Map<String, OpType> SYMBOL_MAP =
new HashMap<>(OpType.values().length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.hadoop.crypto.key.KeyProvider;
import org.apache.hadoop.crypto.key.KeyProviderTokenIssuer;
import org.apache.hadoop.fs.BatchListingOperations;
import org.apache.hadoop.fs.BatchOperations;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.BlockStoragePolicySpi;
import org.apache.hadoop.fs.CacheFlag;
Expand All @@ -50,6 +51,7 @@
import org.apache.hadoop.fs.FsStatus;
import org.apache.hadoop.fs.GlobalStorageStatistics;
import org.apache.hadoop.fs.GlobalStorageStatistics.StorageStatisticsProvider;
import org.apache.hadoop.fs.InvalidPathException;
import org.apache.hadoop.fs.InvalidPathHandleException;
import org.apache.hadoop.fs.PartialListing;
import org.apache.hadoop.fs.MultipartUploaderBuilder;
Expand Down Expand Up @@ -147,7 +149,7 @@
@InterfaceAudience.LimitedPrivate({ "MapReduce", "HBase" })
@InterfaceStability.Unstable
public class DistributedFileSystem extends FileSystem
implements KeyProviderTokenIssuer, BatchListingOperations {
implements KeyProviderTokenIssuer, BatchListingOperations, BatchOperations{
private Path workingDir;
private URI uri;

Expand Down Expand Up @@ -965,6 +967,25 @@ public Void next(final FileSystem fs, final Path p)
}
}

protected String[] getBatchPathName(String[] files) {
List<String> ret = new ArrayList<>();
for(String f : files) {
ret.add(getPathName(new Path(f)));
}
return ret.toArray(new String[ret.size()]);
}

@Override
public void batchRename(final String[] srcs, final String[] dsts,
final Options.Rename... options) throws IOException {
if (srcs.length != dsts.length) {
throw new InvalidPathException("mismatch batch path src: " +
Arrays.toString(srcs) + " dst: " + Arrays.toString(dsts));
}
statistics.incrementWriteOps(1);
dfs.batchRename(getBatchPathName(srcs), getBatchPathName(dsts));
}

@Override
public boolean truncate(Path f, final long newLength) throws IOException {
statistics.incrementWriteOps(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static Optional<Boolean> hasPathCapability(final Path path,
case CommonPathCapabilities.FS_SNAPSHOTS:
case CommonPathCapabilities.FS_STORAGEPOLICY:
case CommonPathCapabilities.FS_XATTRS:
case CommonPathCapabilities.FS_BATCH_RENAME:
return Optional.of(true);
case CommonPathCapabilities.FS_SYMLINKS:
return Optional.of(FileSystem.areSymlinksEnabled());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* 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.hdfs.protocol;

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

import java.io.IOException;

/**
* Thrown when break during a batch operation .
*/
@InterfaceAudience.Private
@InterfaceStability.Evolving
public final class BatchOpsException extends IOException {
private static final long serialVersionUID = 1L;
Copy link
Contributor

Choose a reason for hiding this comment

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

needs a real serial version ID; your IDE can help there

Copy link
Author

Choose a reason for hiding this comment

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

Done, Thanks!

private static final String TAG_INDEX = "index";
private static final String TAG_TOTAL = "total";
private static final String TAG_REASON = "reason";

/**
* Used by RemoteException to instantiate an BatchOpsException.
*/
public BatchOpsException(String msg) {
super(msg);
}

public BatchOpsException(long index, long total, Throwable cause) {
this(index, total,
cause.getClass().getName() + ": " + cause.getMessage());
Copy link
Contributor

Choose a reason for hiding this comment

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

cause.toString(); message may be null

Copy link
Author

Choose a reason for hiding this comment

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

add a more check.

}

public BatchOpsException(long index, long total,
String cause) {
super("Batch operation break! " +
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure about "break!"

Copy link
Author

Choose a reason for hiding this comment

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

change to "Batch operation partial success"

getTagHeader(TAG_INDEX) + index + getTagTailer(TAG_INDEX) +
getTagHeader(TAG_TOTAL) + total + getTagTailer(TAG_TOTAL) +
getTagHeader(TAG_REASON) + cause + getTagTailer(TAG_REASON));
}

public long getIndex() {
return Long.parseLong(getValue(TAG_INDEX));
}

public long getTotal() {
return Long.parseLong(getValue(TAG_TOTAL));
}

public String getReason() {
return getValue(TAG_REASON);
}

private static String getTagHeader(String tag) {
return "<"+tag + ">";
}

private static String getTagTailer(String tag) {
return "</"+tag + ">";
}

private String getValue(String target) {
String msg = getMessage();
String header = getTagHeader(target);
String tailer = getTagTailer(target);
int pos1 = msg.indexOf(header) + header.length();
int pos2 = msg.indexOf(tailer, pos1);

assert pos2 > pos1;
return msg.substring(pos1, pos2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,19 @@ void concat(String trg, String[] srcs)
void rename2(String src, String dst, Options.Rename... options)
throws IOException;


/**
* Rename an batch items in the file system namespace.
* @param srcs existing files or directories name.216
* @param dsts new names.
* @param options Rename options
*
* @throws IOException an I/O error occurred
*/
@AtMostOnce
void batchRename(String[] srcs, String[] dsts,
Options.Rename... options) throws IOException;

/**
* Truncate file src to new size.
* <ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
import org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.AllowSnapshotRequestProto;
import org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.AppendRequestProto;
import org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.AppendResponseProto;
import org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.BatchRenameRequestProto;
import org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.CachePoolEntryProto;
import org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.CheckAccessRequestProto;
import org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.CompleteRequestProto;
Expand Down Expand Up @@ -637,6 +638,33 @@ public void rename2(String src, String dst, Rename... options)

}

@Override
public void batchRename(String[] srcs, String[] dsts, Rename... options)
throws IOException {
boolean overwrite = false;
boolean toTrash = false;
if (options != null) {
for (Rename option : options) {
if (option == Rename.OVERWRITE) {
overwrite = true;
}
if (option == Rename.TO_TRASH) {
toTrash = true;
}
}
}
BatchRenameRequestProto req = BatchRenameRequestProto.newBuilder()
.addAllSrcs(Arrays.asList(srcs))
.addAllDsts(Arrays.asList(dsts))
.setOverwriteDest(overwrite)
.setMoveToTrash(toTrash).build();
try {
rpcProxy.batchRename(null, req);
} catch (ServiceException e) {
throw ProtobufHelper.getRemoteException(e);
}
}

@Override
public void concat(String trg, String[] srcs) throws IOException {
ConcatRequestProto req = ConcatRequestProto.newBuilder().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.nio.charset.StandardCharsets;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Collection;
Expand All @@ -61,9 +62,9 @@
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.crypto.key.KeyProvider;
import org.apache.hadoop.crypto.key.KeyProviderTokenIssuer;
import org.apache.hadoop.fs.BatchOperations;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.CommonConfigurationKeys;
import org.apache.hadoop.fs.CommonPathCapabilities;
import org.apache.hadoop.fs.ContentSummary;
import org.apache.hadoop.fs.CreateFlag;
import org.apache.hadoop.fs.DelegationTokenRenewer;
Expand All @@ -76,9 +77,9 @@
import org.apache.hadoop.fs.FsServerDefaults;
import org.apache.hadoop.fs.GlobalStorageStatistics;
import org.apache.hadoop.fs.GlobalStorageStatistics.StorageStatisticsProvider;
import org.apache.hadoop.fs.InvalidPathException;
import org.apache.hadoop.fs.MultipartUploaderBuilder;
import org.apache.hadoop.fs.QuotaUsage;
import org.apache.hadoop.fs.PathCapabilities;
import org.apache.hadoop.fs.StorageStatistics;
import org.apache.hadoop.fs.StorageType;
import org.apache.hadoop.fs.impl.FileSystemMultipartUploaderBuilder;
Expand Down Expand Up @@ -144,7 +145,8 @@
/** A FileSystem for HDFS over the web. */
public class WebHdfsFileSystem extends FileSystem
implements DelegationTokenRenewer.Renewable,
TokenAspect.TokenManagementDelegator, KeyProviderTokenIssuer {
TokenAspect.TokenManagementDelegator, KeyProviderTokenIssuer,
BatchOperations {
public static final Logger LOG = LoggerFactory
.getLogger(WebHdfsFileSystem.class);
/** WebHdfs version. */
Expand Down Expand Up @@ -1184,6 +1186,34 @@ public void rename(final Path src, final Path dst,
).run();
}

protected String[] getBatchPathName(String[] files) throws IOException{
List<String> ret = new ArrayList<>();
for(String f : files) {
if(!f.startsWith(Path.SEPARATOR)) {
throw new InvalidPathException("Path is not absolute! " + f);
}
ret.add(makeQualified(new Path(f)).toUri().getPath());
}
return ret.toArray(new String[ret.size()]);
}

@Override
public void batchRename(final String[] srcs, final String[] dsts,
final Options.Rename... options) throws IOException {
statistics.incrementWriteOps(1);
storageStatistics.incrementOpCounter(OpType.BATCH_RENAME);
final HttpOpParam.Op op = PutOpParam.Op.BATCH_RENAME;
if (srcs.length != dsts.length) {
throw new InvalidPathException("mismatch batch path src: " +
Arrays.toString(srcs) + " dst: " + Arrays.toString(dsts));
}
new FsPathRunner(op,
new Path(StringUtils.join(":", getBatchPathName(srcs))),
new DestinationParam(StringUtils.join(":", getBatchPathName(dsts))),
new RenameOptionSetParam(options)
).run();
}

@Override
public void setXAttr(Path p, String name, byte[] value,
EnumSet<XAttrSetFlag> flag) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public enum Op implements HttpOpParam.Op {
SETQUOTA(false, HttpURLConnection.HTTP_OK),
SETQUOTABYSTORAGETYPE(false, HttpURLConnection.HTTP_OK),

BATCH_RENAME(false, HttpURLConnection.HTTP_OK),
NULL(false, HttpURLConnection.HTTP_NOT_IMPLEMENTED);

final boolean doOutputAndRedirect;
Expand Down
Loading