Skip to content
Closed
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 @@ -1439,6 +1439,13 @@ private Constants() {
*/
public static final String FS_S3A_CREATE_PERFORMANCE = "fs.s3a.create.performance";

/**
* Flag for commit if none match.
* This can be set in the {code createFile()} builder.
* Value {@value}.
*/
public static final String FS_S3A_CONDITIONAL_FILE_CREATE = "fs.s3a.conditional.file.create";

/**
* Default value for create performance in an S3A FS.
* Value {@value}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@

import static java.util.Objects.requireNonNull;
import static org.apache.hadoop.fs.s3a.Statistic.*;
import static org.apache.hadoop.fs.s3a.impl.AWSHeaders.IF_NONE_MATCH;
import static org.apache.hadoop.fs.s3a.impl.HeaderProcessing.CONTENT_TYPE_OCTET_STREAM;
import static org.apache.hadoop.fs.s3a.impl.ProgressListenerEvent.*;
import static org.apache.hadoop.fs.s3a.statistics.impl.EmptyS3AStatisticsContext.EMPTY_BLOCK_OUTPUT_STREAM_STATISTICS;
Expand Down Expand Up @@ -690,20 +691,28 @@ private long putObject() throws IOException {
final S3ADataBlocks.DataBlock block = getActiveBlock();
final long size = block.dataSize();
final S3ADataBlocks.BlockUploadData uploadData = block.startUpload();
final PutObjectRequest putObjectRequest =
PutObjectRequest putObjectRequest =
writeOperationHelper.createPutObjectRequest(
key,
uploadData.getSize(),
builder.putOptions);
clearActiveBlock();

PutObjectRequest.Builder maybeModifiedPutIfAbsentRequest = putObjectRequest.toBuilder();
Map<String, String> optionHeaders = builder.putOptions.getHeaders();
if (builder.isConditionalPutEnabled){
maybeModifiedPutIfAbsentRequest.overrideConfiguration(
Copy link
Contributor

Choose a reason for hiding this comment

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

as mentioned below as well, think we should upgrade SDK and then use the new .ifNoneMatch().

Also I would recommend you move all of this logic into a new private method in this class.

Copy link
Contributor

Choose a reason for hiding this comment

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

actually, I would move this logic to RequestFactoryImpl. But you will need to add in a flag to the method signature, "isConditonalPutEnabled", and only add the if-none-match header in when sConditonalPutEnabled is true.

Basically, you only want to add if-none-match when the PUT is coming from S3ABlockOutputStream.close() and not from anywhere else.

override -> override.putHeader(IF_NONE_MATCH, optionHeaders.get(IF_NONE_MATCH)));
}
final PutObjectRequest finalizedRequest = maybeModifiedPutIfAbsentRequest.build();

BlockUploadProgress progressCallback =
new BlockUploadProgress(block, progressListener, now());
statistics.blockUploadQueued(size);
try {
progressCallback.progressChanged(PUT_STARTED_EVENT);
// the putObject call automatically closes the upload data
writeOperationHelper.putObject(putObjectRequest,
writeOperationHelper.putObject(finalizedRequest,
builder.putOptions,
uploadData,
statistics);
Expand Down Expand Up @@ -1389,6 +1398,11 @@ public static final class BlockOutputStreamBuilder {
*/
private boolean isMultipartUploadEnabled;

/**
* Is conditional create enabled.
*/
private boolean isConditionalPutEnabled;

private BlockOutputStreamBuilder() {
}

Expand Down Expand Up @@ -1550,5 +1564,11 @@ public BlockOutputStreamBuilder withMultipartEnabled(
isMultipartUploadEnabled = value;
return this;
}

public BlockOutputStreamBuilder withConditionalPutEnabled(
final boolean value){
isConditionalPutEnabled = value;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2194,8 +2194,9 @@ private FSDataOutputStream innerCreateFile(
// put options are derived from the path and the
// option builder.
boolean keep = options.isPerformance() || keepDirectoryMarkers(path);
boolean conditionalCreate = options.isConditionalCreate();
final PutObjectOptions putOptions =
new PutObjectOptions(keep, null, options.getHeaders());
new PutObjectOptions(keep, false, null, options.getHeaders());

validateOutputStreamConfiguration(path, getConf());

Expand Down Expand Up @@ -2223,7 +2224,8 @@ private FSDataOutputStream innerCreateFile(
.withPutOptions(putOptions)
.withIOStatisticsAggregator(
IOStatisticsContext.getCurrentIOStatisticsContext().getAggregator())
.withMultipartEnabled(isMultipartUploadEnabled);
.withMultipartEnabled(isMultipartUploadEnabled)
.withConditionalPutEnabled(conditionalCreate);
return new FSDataOutputStream(
new S3ABlockOutputStream(builder),
null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,7 @@ private CompleteMultipartUploadResponse finalizeMultipartUpload(
retrying,
() -> {
final CompleteMultipartUploadRequest.Builder requestBuilder =
getRequestFactory().newCompleteMultipartUploadRequestBuilder(
destKey, uploadId, partETags);
getRequestFactory().newCompleteMultipartUploadRequestBuilder(destKey, uploadId, partETags, putOptions);
return writeOperationHelperCallbacks.completeMultipartUpload(requestBuilder.build());
});
writeOperationHelperCallbacks.finishedWrite(destKey, length,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,14 @@ CreateMultipartUploadRequest.Builder newMultipartUploadRequestBuilder(
* @param destKey destination object key
* @param uploadId ID of initiated upload
* @param partETags ordered list of etags
* @param putOptions options for the request
* @return the request builder.
*/
CompleteMultipartUploadRequest.Builder newCompleteMultipartUploadRequestBuilder(
String destKey,
String uploadId,
List<CompletedPart> partETags);
List<CompletedPart> partETags,
PutObjectOptions putOptions);

/**
* Create a HEAD object request builder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public boolean aboutToComplete(String uploadId,
PutObjectRequest originalDestPut = getWriter().createPutObjectRequest(
getOriginalDestKey(),
0,
new PutObjectOptions(true, null, headers));
new PutObjectOptions(true,false, null, headers));
upload(originalDestPut, EMPTY);

// build the commit summary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public interface AWSHeaders {
String DATE = "Date";
String ETAG = "ETag";
String LAST_MODIFIED = "Last-Modified";
String IF_NONE_MATCH = "If-None-Match";

/*
* Amazon HTTP Headers used by S3A.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ public class CreateFileBuilder extends
* Classic create file option set: overwriting.
*/
public static final CreateFileOptions OPTIONS_CREATE_FILE_OVERWRITE =
new CreateFileOptions(CREATE_OVERWRITE_FLAGS, true, false, null);
new CreateFileOptions(CREATE_OVERWRITE_FLAGS, true, false, false, null);

/**
* Classic create file option set: no overwrite.
*/
public static final CreateFileOptions OPTIONS_CREATE_FILE_NO_OVERWRITE =
new CreateFileOptions(CREATE_NO_OVERWRITE_FLAGS, true, false, null);
new CreateFileOptions(CREATE_NO_OVERWRITE_FLAGS, true, false, false, null);

/**
* Performance create options.
*/
public static final CreateFileOptions OPTIONS_CREATE_FILE_PERFORMANCE =
new CreateFileOptions(CREATE_OVERWRITE_FLAGS, true, true, null);
new CreateFileOptions(CREATE_OVERWRITE_FLAGS, true, true, false, null);

/**
* Callback interface.
Expand Down Expand Up @@ -144,10 +144,12 @@ public FSDataOutputStream build() throws IOException {

final boolean performance =
options.getBoolean(Constants.FS_S3A_CREATE_PERFORMANCE, false);
final boolean conditionalCreate =
options.getBoolean(Constants.FS_S3A_CONDITIONAL_FILE_CREATE, false);
return callbacks.createFileFromBuilder(
path,
getProgress(),
new CreateFileOptions(flags, isRecursive(), performance, headers));
new CreateFileOptions(flags, isRecursive(), performance, conditionalCreate, headers));

}

Expand Down Expand Up @@ -218,6 +220,11 @@ public static final class CreateFileOptions {
*/
private final boolean performance;

/**
* conditional flag.
*/
private final boolean conditionalCreate;

/**
* Headers; may be null.
*/
Expand All @@ -227,16 +234,19 @@ public static final class CreateFileOptions {
* @param flags creation flags
* @param recursive create parent dirs?
* @param performance performance flag
* @param conditionalCreate conditional flag
* @param headers nullable header map.
*/
public CreateFileOptions(
final EnumSet<CreateFlag> flags,
final boolean recursive,
final boolean performance,
final boolean conditionalCreate,
final Map<String, String> headers) {
this.flags = flags;
this.recursive = recursive;
this.performance = performance;
this.conditionalCreate = conditionalCreate;
this.headers = headers;
}

Expand All @@ -246,6 +256,7 @@ public String toString() {
"flags=" + flags +
", recursive=" + recursive +
", performance=" + performance +
", conditionalCreate=" + conditionalCreate +
", headers=" + headers +
'}';
}
Expand All @@ -262,6 +273,10 @@ public boolean isPerformance() {
return performance;
}

public boolean isConditionalCreate() {
return conditionalCreate;
}

public Map<String, String> getHeaders() {
return headers;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import static org.apache.hadoop.fs.s3a.Constants.DIRECTORY_OPERATIONS_PURGE_UPLOADS;
import static org.apache.hadoop.fs.s3a.Constants.ENABLE_MULTI_DELETE;
import static org.apache.hadoop.fs.s3a.Constants.FIPS_ENDPOINT;
import static org.apache.hadoop.fs.s3a.Constants.FS_S3A_CONDITIONAL_FILE_CREATE;
import static org.apache.hadoop.fs.s3a.Constants.FS_S3A_CREATE_PERFORMANCE;
import static org.apache.hadoop.fs.s3a.Constants.FS_S3A_CREATE_PERFORMANCE_ENABLED;
import static org.apache.hadoop.fs.s3a.Constants.STORE_CAPABILITY_AWS_V2;
Expand Down Expand Up @@ -260,7 +261,7 @@ private InternalConstants() {
*/
public static final Set<String> CREATE_FILE_KEYS =
Collections.unmodifiableSet(
new HashSet<>(Arrays.asList(FS_S3A_CREATE_PERFORMANCE)));
new HashSet<>(Arrays.asList(FS_S3A_CREATE_PERFORMANCE, FS_S3A_CONDITIONAL_FILE_CREATE)));

/**
* Dynamic Path capabilities to be evaluated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public final class PutObjectOptions {
*/
private final boolean keepMarkers;

/**
* Is this a conditional PUT operation
*/
private final boolean conditionalPutEnabled;

/**
* Storage class, if not null.
*/
Expand All @@ -45,13 +50,16 @@ public final class PutObjectOptions {
* Constructor.
* @param keepMarkers Can the PUT operation skip marker deletion?
* @param storageClass Storage class, if not null.
* @param conditionalPutEnabled Is this a conditional Put?
* @param headers Headers; may be null.
*/
public PutObjectOptions(
final boolean keepMarkers,
final boolean conditionalPutEnabled,
@Nullable final String storageClass,
@Nullable final Map<String, String> headers) {
this.keepMarkers = keepMarkers;
this.conditionalPutEnabled = conditionalPutEnabled;
this.storageClass = storageClass;
this.headers = headers;
}
Expand All @@ -64,6 +72,14 @@ public boolean isKeepMarkers() {
return keepMarkers;
}

/**
* Get the conditional put flag.
* @return true if it's a conditional put
*/
public boolean isconditionalPutEnabled() {
return conditionalPutEnabled;
}

/**
* Headers for the put/post request.
* @return headers or null.
Expand All @@ -80,9 +96,9 @@ public String toString() {
'}';
}

private static final PutObjectOptions KEEP_DIRS = new PutObjectOptions(true,
private static final PutObjectOptions KEEP_DIRS = new PutObjectOptions(true, false,
null, null);
private static final PutObjectOptions DELETE_DIRS = new PutObjectOptions(false,
private static final PutObjectOptions DELETE_DIRS = new PutObjectOptions(false, false,
null, null);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import org.apache.hadoop.fs.s3a.auth.delegation.EncryptionSecrets;

import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.apache.hadoop.fs.s3a.impl.AWSHeaders.IF_NONE_MATCH;
import static org.apache.hadoop.fs.s3a.Constants.DEFAULT_PART_UPLOAD_TIMEOUT;
import static org.apache.hadoop.fs.s3a.S3AEncryptionMethods.UNKNOWN_ALGORITHM;
import static org.apache.hadoop.fs.s3a.impl.AWSClientConfig.setRequestTimeout;
Expand Down Expand Up @@ -532,12 +533,20 @@ public CreateMultipartUploadRequest.Builder newMultipartUploadRequestBuilder(
public CompleteMultipartUploadRequest.Builder newCompleteMultipartUploadRequestBuilder(
String destKey,
String uploadId,
List<CompletedPart> partETags) {
List<CompletedPart> partETags,
PutObjectOptions putOptions) {

// a copy of the list is required, so that the AWS SDK doesn't
// attempt to sort an unmodifiable list.
CompleteMultipartUploadRequest.Builder requestBuilder =
CompleteMultipartUploadRequest.builder().bucket(bucket).key(destKey).uploadId(uploadId)
CompleteMultipartUploadRequest.Builder requestBuilder;
Map<String, String> optionHeaders = putOptions.getHeaders();
requestBuilder = CompleteMultipartUploadRequest.builder().bucket(bucket).key(destKey).uploadId(uploadId)
.multipartUpload(CompletedMultipartUpload.builder().parts(partETags).build());
if (putOptions.isconditionalPutEnabled()){
requestBuilder = CompleteMultipartUploadRequest.builder().overrideConfiguration(
override ->override.putHeader(IF_NONE_MATCH, optionHeaders.get(IF_NONE_MATCH)));

}

return prepareRequest(requestBuilder);
}
Expand Down
Loading
Loading