MartijnVisser commented on code in PR #29823:
URL: https://github.com/apache/flink/pull/29823#discussion_r4628157285
##########
flink-filesystems/flink-s3-fs-hadoop/src/main/java/org/apache/flink/fs/s3hadoop/HadoopS3AccessHelper.java:
##########
@@ -18,91 +18,142 @@
package org.apache.flink.fs.s3hadoop;
+import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.fs.s3.common.writer.S3AccessHelper;
-import org.apache.flink.util.MathUtils;
-
-import com.amazonaws.SdkBaseException;
-import com.amazonaws.services.s3.model.CompleteMultipartUploadResult;
-import com.amazonaws.services.s3.model.ObjectMetadata;
-import com.amazonaws.services.s3.model.PartETag;
-import com.amazonaws.services.s3.model.PutObjectRequest;
-import com.amazonaws.services.s3.model.PutObjectResult;
-import com.amazonaws.services.s3.model.UploadPartRequest;
-import com.amazonaws.services.s3.model.UploadPartResult;
+
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.fs.s3a.S3ADataBlocks;
import org.apache.hadoop.fs.s3a.S3AFileSystem;
-import org.apache.hadoop.fs.s3a.S3AUtils;
import org.apache.hadoop.fs.s3a.WriteOperationHelper;
-import org.apache.hadoop.fs.s3a.statistics.S3AStatisticsContext;
-import org.apache.hadoop.fs.store.audit.AuditSpan;
-import org.apache.hadoop.fs.store.audit.AuditSpanSource;
+import org.apache.hadoop.fs.s3a.impl.PutObjectOptions;
+import org.apache.hadoop.io.MD5Hash;
+import software.amazon.awssdk.core.sync.RequestBody;
+import
software.amazon.awssdk.services.s3.model.CompleteMultipartUploadResponse;
+import software.amazon.awssdk.services.s3.model.CompletedPart;
+import software.amazon.awssdk.services.s3.model.HeadObjectResponse;
+import software.amazon.awssdk.services.s3.model.PutObjectResponse;
+import software.amazon.awssdk.services.s3.model.UploadPartRequest;
+import software.amazon.awssdk.services.s3.model.UploadPartResponse;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.OutputStream;
+import java.util.Base64;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import static org.apache.flink.util.Preconditions.checkNotNull;
+import static org.apache.hadoop.fs.s3a.Constants.CHECKSUM_ALGORITHM;
+import static org.apache.hadoop.fs.s3a.Constants.CHECKSUM_GENERATION;
+import static org.apache.hadoop.fs.s3a.Constants.DEFAULT_CHECKSUM_ALGORITHM;
+import static org.apache.hadoop.fs.s3a.Constants.DEFAULT_CHECKSUM_GENERATION;
-/** An implementation of the {@link S3AccessHelper} for the Hadoop S3A
filesystem. */
+/**
+ * An implementation of the {@link S3AccessHelper} for the Hadoop S3A
filesystem.
+ *
+ * <p>This implementation uses Hadoop's {@link WriteOperationHelper} to
perform S3 operations,
+ * similar to the SDK v1 implementation. This provides retry logic, error
handling, S3A statistics
+ * integration, and auditing support.
+ */
public class HadoopS3AccessHelper implements S3AccessHelper {
private final S3AFileSystem s3a;
- private final InternalWriteOperationHelper s3accessHelper;
+ private final WriteOperationHelper writeHelper;
+
+ private final boolean partChecksumsEnabled;
public HadoopS3AccessHelper(S3AFileSystem s3a, Configuration conf) {
- checkNotNull(s3a);
- this.s3accessHelper =
- new InternalWriteOperationHelper(
- s3a,
- checkNotNull(conf),
- s3a.createStoreContext().getInstrumentation(),
- s3a.getAuditSpanSource(),
- s3a.getActiveAuditSpan());
- this.s3a = s3a;
+ this.s3a = checkNotNull(s3a);
+ this.writeHelper = s3a.getWriteOperationHelper();
+ this.partChecksumsEnabled = partChecksumsEnabled(conf);
+ }
+
+ /**
+ * Whether S3A sends per-part checksums. A multipart upload with part
checksums can only be
+ * completed with those checksums, and the recoverable state does not
store them.
+ */
+ @VisibleForTesting
+ static boolean partChecksumsEnabled(Configuration conf) {
+ final String algorithm = conf.getTrimmed(CHECKSUM_ALGORITHM,
DEFAULT_CHECKSUM_ALGORITHM);
+ return conf.getBoolean(CHECKSUM_GENERATION,
DEFAULT_CHECKSUM_GENERATION)
+ || !(algorithm.isEmpty() ||
"NONE".equalsIgnoreCase(algorithm));
+ }
+
+ private void checkPartChecksumsDisabled() throws IOException {
+ if (partChecksumsEnabled) {
+ throw new IOException(
+ String.format(
+ "The S3 recoverable writer does not support part
checksums. Unset '%s' and '%s'.",
+ CHECKSUM_ALGORITHM, CHECKSUM_GENERATION));
+ }
+ }
+
+ /**
+ * Content-MD5 for an upload, so that S3 rejects bytes that arrive
corrupted. The SDK v2 client
+ * neither sends nor checks one, and the writer cannot use S3A's part
checksums.
+ */
+ private static String contentMd5(File file) throws IOException {
+ try (InputStream in = new FileInputStream(file)) {
+ return
Base64.getEncoder().encodeToString(MD5Hash.digest(in).getDigest());
+ }
+ }
+
+ /** Follows the directory marker policy for the key, like {@link
S3AFileSystem} does. */
+ private PutObjectOptions putOptions(String key) {
+ return
s3a.getDirectoryMarkerPolicy().keepDirectoryMarkers(s3a.keyToQualifiedPath(key))
+ ? PutObjectOptions.keepingDirs()
+ : PutObjectOptions.deletingDirs();
}
@Override
public String startMultiPartUpload(String key) throws IOException {
- return s3accessHelper.initiateMultiPartUpload(key);
+ checkPartChecksumsDisabled();
+ return writeHelper.initiateMultiPartUpload(key, putOptions(key));
}
@Override
- public UploadPartResult uploadPart(
+ public UploadPartResponse uploadPart(
String key, String uploadId, int partNumber, File inputFile, long
length)
throws IOException {
- final UploadPartRequest uploadRequest =
- s3accessHelper.newUploadPartRequest(
- key,
- uploadId,
- partNumber,
- MathUtils.checkedDownCast(length),
- null,
- inputFile,
- 0L);
- return s3accessHelper.uploadPart(uploadRequest);
+ checkPartChecksumsDisabled();
+ UploadPartRequest request =
+ writeHelper
+ .newUploadPartRequestBuilder(key, uploadId,
partNumber, false, length)
+ .contentMD5(contentMd5(inputFile))
+ .build();
+ RequestBody body = RequestBody.fromFile(inputFile);
Review Comment:
Content-MD5 is a header, so S3 needs it before the body. S3A's part
checksums avoid that, but then the state has to store them. The second read
costs 0.2 ms per 5 MiB part (4.3 ms per 64 MiB), the MD5 itself 6 ms (80 ms),
so I would keep it.
##########
flink-filesystems/flink-s3-fs-base/src/main/java/org/apache/flink/fs/s3/common/writer/MultiPartUploadInfo.java:
##########
@@ -101,7 +101,7 @@ void registerNewPart(long length) {
this.numberOfRegisteredParts++;
}
- void registerCompletePart(PartETag eTag) {
+ void registerCompletePart(CompletedPart eTag) {
Review Comment:
The type is `PartETag` again, Flink's own now, so `eTag` fits.
##########
flink-filesystems/flink-s3-fs-base/src/main/java/org/apache/flink/fs/s3/common/writer/MultiPartUploadInfo.java:
##########
@@ -92,7 +92,7 @@ Optional<File> getIncompletePart() {
return incompletePart;
}
- List<PartETag> getCopyOfEtagsOfCompleteParts() {
+ List<CompletedPart> getCopyOfEtagsOfCompleteParts() {
Review Comment:
Same here: it returns `List<PartETag>` again, as on master.
##########
docs/content.zh/docs/deployment/filesystems/s3.md:
##########
@@ -230,6 +230,36 @@ cp ./opt/flink-s3-fs-hadoop-{{< version >}}.jar
./plugins/s3-fs-hadoop/
The [common configuration](#common-configuration) options apply. In addition,
[Hadoop's s3a configuration
keys](https://hadoop.apache.org/docs/stable/hadoop-aws/tools/hadoop-aws/index.html#S3A)
are supported. Hadoop configuration keys are automatically translated — for
example, `fs.s3a.connection.maximum` becomes `s3.connection.maximum`.
+**Input stream type:** hadoop-aws 3.4.3 changes its default input stream to
the [S3 Analytics
Accelerator](https://hadoop.apache.org/docs/stable/hadoop-aws/tools/hadoop-aws/stream_architecture.html)
(`fs.s3a.input.stream.type: analytics`), which alters read patterns,
per-stream memory usage, and the volume of S3 GET requests. To keep upgrades
behavior-preserving, Flink pins the classic input stream by default. To opt in
to the analytics stream, set `s3.input.stream.type: analytics`; setting the
legacy `s3.prefetch.enabled: true` toggle is also respected and selects
Hadoop's prefetching stream.
+
+**AWS SDK v1 credentials providers:** the Hadoop S3 FileSystem uses AWS SDK
v2. Hadoop maps common SDK v1 provider names in
`fs.s3a.aws.credentials.provider` to their SDK v2 equivalents, and Flink adds
mappings for `com.amazonaws.auth.DefaultAWSCredentialsProviderChain`,
`com.amazonaws.auth.WebIdentityTokenCredentialsProvider`,
`com.amazonaws.auth.SystemPropertiesCredentialsProvider` and
`com.amazonaws.auth.ContainerCredentialsProvider`. A custom provider must
implement `software.amazon.awssdk.auth.credentials.AwsCredentialsProvider`;
other SDK v1 names can be mapped with `s3.aws.credentials.provider.mapping`.
+
+**Part checksums:** the `FileSink` doesn't support S3 part checksums with the
Hadoop S3 FileSystem. Leave `s3.create.checksum.algorithm` and
`s3.checksum.generation` unset. Instead, it sends a `Content-MD5` header with
each upload, so S3 rejects data that arrives corrupted.
+
+#### Changed defaults from the Hadoop 3.4 upgrade
+
+The Hadoop S3 FileSystem passes Hadoop's s3a defaults through. Hadoop 3.4
changed several of them, and Flink keeps the new values, except for the input
stream type above.
+
+| Key | Hadoop 3.3.4 default | Hadoop 3.4.3 default |
Review Comment:
Yes: the release note on FLINK-30975 points to this table, and users need
the old values to restore them. I added the two defaults it missed.
##########
flink-filesystems/flink-s3-fs-presto/src/main/java/org/apache/flink/fs/s3presto/token/S3PrestoDelegationTokenProvider.java:
##########
@@ -19,13 +19,54 @@
package org.apache.flink.fs.s3presto.token;
import org.apache.flink.annotation.Internal;
+import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.fs.s3.common.token.AbstractS3DelegationTokenProvider;
+import org.apache.flink.fs.s3.common.token.S3SessionCredentials;
-/** Delegation token provider for S3 Presto filesystems. */
+import com.amazonaws.auth.AWSStaticCredentialsProvider;
+import com.amazonaws.auth.BasicAWSCredentials;
+import com.amazonaws.services.securitytoken.AWSSecurityTokenService;
+import
com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClientBuilder;
+import com.amazonaws.services.securitytoken.model.Credentials;
+
+/** Delegation token provider for S3 Presto filesystems, based on AWS SDK v1.
*/
@Internal
public class S3PrestoDelegationTokenProvider extends
AbstractS3DelegationTokenProvider {
+
@Override
public String serviceName() {
return "s3-presto";
}
+
+ @Override
+ protected SessionTokenClient createSessionTokenClient(
+ String region, String accessKey, String secretKey) {
+ final AWSSecurityTokenService stsClient = createStsClient(region,
accessKey, secretKey);
+ return new SessionTokenClient() {
+ @Override
+ public S3SessionCredentials getSessionCredentials() {
+ final Credentials credentials =
stsClient.getSessionToken().getCredentials();
+ return new S3SessionCredentials(
+ credentials.getAccessKeyId(),
+ credentials.getSecretAccessKey(),
+ credentials.getSessionToken(),
+ credentials.getExpiration().getTime());
+ }
+
+ @Override
+ public void close() {
+ stsClient.shutdown();
Review Comment:
Best effort: `obtainDelegationTokens` logs a failed close and keeps the
tokens, see `obtainDelegationTokensShouldReturnTokensWhenShutdownFails`.
##########
flink-filesystems/flink-s3-fs-base/src/main/java/org/apache/flink/fs/s3/common/token/AbstractS3DelegationTokenReceiver.java:
##########
@@ -79,24 +134,25 @@ public void init(Configuration configuration) {
DelegationTokenProvider.CONFIG_PREFIX,
serviceName()),
null);
if (!StringUtils.isNullOrWhitespaceOnly(region)) {
- LOG.debug("Region: " + region);
+ LOG.debug("Region: {}", region);
}
}
@Override
public void onNewTokensObtained(byte[] tokens) throws Exception {
LOG.info("Updating session credentials");
- credentials =
+ final S3SessionCredentials newCredentials =
InstantiationUtil.deserializeObject(
tokens,
AbstractS3DelegationTokenReceiver.class.getClassLoader());
+ credentials = newCredentials;
LOG.info(
"Session credentials updated successfully with access key: {}
expiration: {}",
- credentials.getAccessKeyId(),
- credentials.getExpiration());
+ newCredentials.getAccessKeyId(),
+
Instant.ofEpochMilli(newCredentials.getExpirationEpochMilli()));
Review Comment:
`formatWithHighestUnit` picks the largest unit that divides exactly, so the
remaining lifetime of a token prints as `43199873 ms`. I would keep the
timestamp.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]