Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 2E19C200CC8 for ; Fri, 30 Jun 2017 01:02:53 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 25910160BF7; Thu, 29 Jun 2017 23:02:53 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 460AF160BED for ; Fri, 30 Jun 2017 01:02:52 +0200 (CEST) Received: (qmail 83532 invoked by uid 500); 29 Jun 2017 23:02:51 -0000 Mailing-List: contact common-commits-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list common-commits@hadoop.apache.org Received: (qmail 83521 invoked by uid 99); 29 Jun 2017 23:02:51 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 29 Jun 2017 23:02:51 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 49799DFBA2; Thu, 29 Jun 2017 23:02:51 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: liuml07@apache.org To: common-commits@hadoop.apache.org Message-Id: <6134f7ac0335414981bc61bdd4fded07@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: hadoop git commit: HADOOP-14423. S3Guard will set file length to -1 on a putObjectDirect(stream, -1) call. Contributed by Steve Loughran Date: Thu, 29 Jun 2017 23:02:51 +0000 (UTC) archived-at: Thu, 29 Jun 2017 23:02:53 -0000 Repository: hadoop Updated Branches: refs/heads/HADOOP-13345 14ac6ea57 -> 886d680e1 HADOOP-14423. S3Guard will set file length to -1 on a putObjectDirect(stream, -1) call. Contributed by Steve Loughran Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/886d680e Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/886d680e Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/886d680e Branch: refs/heads/HADOOP-13345 Commit: 886d680e1dd357bf31cdf774066d345c148c5f5a Parents: 14ac6ea Author: Mingliang Liu Authored: Thu Jun 29 16:02:03 2017 -0700 Committer: Mingliang Liu Committed: Thu Jun 29 16:02:22 2017 -0700 ---------------------------------------------------------------------- .../org/apache/hadoop/fs/s3a/S3AFileSystem.java | 44 +++++++++++++++----- .../hadoop/fs/s3a/ITestS3AMiscOperations.java | 20 +++++++++ 2 files changed, 54 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hadoop/blob/886d680e/hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/S3AFileSystem.java ---------------------------------------------------------------------- diff --git a/hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/S3AFileSystem.java b/hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/S3AFileSystem.java index 51543f8..878f2f7 100644 --- a/hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/S3AFileSystem.java +++ b/hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/S3AFileSystem.java @@ -1161,8 +1161,9 @@ public class S3AFileSystem extends FileSystem { * @param inputStream source data. * @return the request */ - private PutObjectRequest newPutObjectRequest(String key, - ObjectMetadata metadata, InputStream inputStream) { + PutObjectRequest newPutObjectRequest(String key, + ObjectMetadata metadata, + InputStream inputStream) { Preconditions.checkNotNull(inputStream); PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, key, inputStream, metadata); @@ -1240,14 +1241,10 @@ public class S3AFileSystem extends FileSystem { * @return the upload initiated * @throws AmazonClientException on problems */ - public PutObjectResult putObjectDirect(PutObjectRequest putObjectRequest) + PutObjectResult putObjectDirect(PutObjectRequest putObjectRequest) throws AmazonClientException { - long len; - if (putObjectRequest.getFile() != null) { - len = putObjectRequest.getFile().length(); - } else { - len = putObjectRequest.getMetadata().getContentLength(); - } + long len = getPutRequestLength(putObjectRequest); + LOG.debug("PUT {} bytes to {}", len, putObjectRequest.getKey()); incrementPutStartStatistics(len); try { PutObjectResult result = s3.putObject(putObjectRequest); @@ -1260,6 +1257,23 @@ public class S3AFileSystem extends FileSystem { } /** + * Get the length of the PUT, verifying that the length is known. + * @param putObjectRequest a request bound to a file or a stream. + * @return the request length + * @throws IllegalArgumentException if the length is negative + */ + private long getPutRequestLength(PutObjectRequest putObjectRequest) { + long len; + if (putObjectRequest.getFile() != null) { + len = putObjectRequest.getFile().length(); + } else { + len = putObjectRequest.getMetadata().getContentLength(); + } + Preconditions.checkState(len >= 0, "Cannot PUT object of unknown length"); + return len; + } + + /** * Upload part of a multi-partition file. * Increments the write and put counters. * Important: this call does not close any input stream in the request. @@ -2257,13 +2271,23 @@ public class S3AFileSystem extends FileSystem { /** * Perform post-write actions. + * This operation MUST be called after any PUT/multipart PUT completes + * successfully. + * This includes + *
    + *
  1. Calling {@link #deleteUnnecessaryFakeDirectories(Path)}
  2. + *
  3. Updating any metadata store with details on the newly created + * object.
  4. + *
* @param key key written to * @param length total length of file written */ - public void finishedWrite(String key, long length) { + @InterfaceAudience.Private + void finishedWrite(String key, long length) { LOG.debug("Finished write to {}, len {}", key, length); Path p = keyToQualifiedPath(key); deleteUnnecessaryFakeDirectories(p.getParent()); + Preconditions.checkArgument(length >= 0, "content length is negative"); // See note about failure semantics in s3guard.md doc. try { http://git-wip-us.apache.org/repos/asf/hadoop/blob/886d680e/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/ITestS3AMiscOperations.java ---------------------------------------------------------------------- diff --git a/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/ITestS3AMiscOperations.java b/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/ITestS3AMiscOperations.java index 59fcb05..dd20903 100644 --- a/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/ITestS3AMiscOperations.java +++ b/hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/ITestS3AMiscOperations.java @@ -22,8 +22,13 @@ import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileAlreadyExistsException; import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.contract.ContractTestUtils; +import org.apache.hadoop.test.LambdaTestUtils; + +import com.amazonaws.services.s3.model.ObjectMetadata; +import com.amazonaws.services.s3.model.PutObjectRequest; import org.junit.Test; +import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.IOException; @@ -55,6 +60,21 @@ public class ITestS3AMiscOperations extends AbstractS3ATestBase { createNonRecursive(new Path(parent, "fail")); } + @Test + public void testPutObjectDirect() throws Throwable { + S3AFileSystem fs = getFileSystem(); + ObjectMetadata metadata = fs.newObjectMetadata(-1); + metadata.setContentLength(-1); + Path path = path("putDirect"); + final PutObjectRequest put = new PutObjectRequest(fs.getBucket(), + path.toUri().getPath(), + new ByteArrayInputStream("PUT".getBytes()), + metadata); + LambdaTestUtils.intercept(IllegalStateException.class, + () -> fs.putObjectDirect(put)); + assertPathDoesNotExist("put object was created", path); + } + private FSDataOutputStream createNonRecursive(Path path) throws IOException { return getFileSystem().createNonRecursive(path, false, 4096, (short) 3, (short) 4096, --------------------------------------------------------------------- To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org For additional commands, e-mail: common-commits-help@hadoop.apache.org