|
18 | 18 | package org.apache.beam.sdk.extensions.gcp.options; |
19 | 19 |
|
20 | 20 | import com.fasterxml.jackson.annotation.JsonIgnore; |
| 21 | +import com.fasterxml.jackson.core.JsonGenerator; |
| 22 | +import com.fasterxml.jackson.core.JsonParser; |
| 23 | +import com.fasterxml.jackson.databind.DeserializationContext; |
| 24 | +import com.fasterxml.jackson.databind.JsonDeserializer; |
| 25 | +import com.fasterxml.jackson.databind.JsonNode; |
| 26 | +import com.fasterxml.jackson.databind.JsonSerializer; |
| 27 | +import com.fasterxml.jackson.databind.SerializerProvider; |
| 28 | +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; |
| 29 | +import com.fasterxml.jackson.databind.annotation.JsonSerialize; |
21 | 30 | import com.google.cloud.hadoop.gcsio.GoogleCloudStorageReadOptions; |
22 | 31 | import com.google.cloud.hadoop.util.AsyncWriteChannelOptions; |
23 | 32 | import java.util.HashMap; |
@@ -49,12 +58,16 @@ public interface GcsOptions extends ApplicationNameOptions, GcpOptions, Pipeline |
49 | 58 | class GcsReadOptionsFactory implements DefaultValueFactory<GoogleCloudStorageReadOptions> { |
50 | 59 | @Override |
51 | 60 | public GoogleCloudStorageReadOptions create(PipelineOptions options) { |
52 | | - return GoogleCloudStorageReadOptions.DEFAULT; |
| 61 | + // In gcs-connector v3, GoogleCloudStorageReadOptions.DEFAULT changed fadvise from SEQUENTIAL |
| 62 | + // to AUTO. Beam workloads default to SEQUENTIAL to preserve expected sequential read |
| 63 | + // throughput and caching behavior. |
| 64 | + return GcsReadOptionsSerializer.DEFAULT_OPTIONS; |
53 | 65 | } |
54 | 66 | } |
55 | 67 |
|
56 | 68 | /** @deprecated This option will be removed in a future release. */ |
57 | | - @JsonIgnore |
| 69 | + @JsonSerialize(using = GcsReadOptionsSerializer.class) |
| 70 | + @JsonDeserialize(using = GcsReadOptionsDeserializer.class) |
58 | 71 | @Description( |
59 | 72 | "The GoogleCloudStorageReadOptions instance that should be used to read from Google Cloud Storage.") |
60 | 73 | @Default.InstanceFactory(GcsReadOptionsFactory.class) |
@@ -286,3 +299,71 @@ boolean exceedsEntryLimit() { |
286 | 299 | } |
287 | 300 | } |
288 | 301 | } |
| 302 | + |
| 303 | +class GcsReadOptionsSerializer extends JsonSerializer<GoogleCloudStorageReadOptions> { |
| 304 | + static final GoogleCloudStorageReadOptions DEFAULT_OPTIONS = |
| 305 | + GoogleCloudStorageReadOptions.DEFAULT |
| 306 | + .toBuilder() |
| 307 | + .setFadvise(GoogleCloudStorageReadOptions.Fadvise.SEQUENTIAL) |
| 308 | + .build(); |
| 309 | + |
| 310 | + @Override |
| 311 | + public void serialize( |
| 312 | + GoogleCloudStorageReadOptions value, JsonGenerator gen, SerializerProvider serializers) |
| 313 | + throws java.io.IOException { |
| 314 | + // Note: We only support a partial set of options to propagate to remote |
| 315 | + // workers. Setting the unsupported ones will not have any effect and will fall |
| 316 | + // back to default in remote workers. Support for additional options can be |
| 317 | + // added on a need basis. The full list of options can be seen in |
| 318 | + // com.google.cloud.hadoop.gcsio.GoogleCloudStorageReadOptions. |
| 319 | + gen.writeStartObject(); |
| 320 | + if (value.getFadvise() != null && value.getFadvise() != DEFAULT_OPTIONS.getFadvise()) { |
| 321 | + gen.writeStringField("fadvise", value.getFadvise().name()); |
| 322 | + } |
| 323 | + if (value.isFastFailOnNotFoundEnabled() != DEFAULT_OPTIONS.isFastFailOnNotFoundEnabled()) { |
| 324 | + gen.writeBooleanField("fastFailOnNotFoundEnabled", value.isFastFailOnNotFoundEnabled()); |
| 325 | + } |
| 326 | + if (value.getMinRangeRequestSize() != DEFAULT_OPTIONS.getMinRangeRequestSize()) { |
| 327 | + gen.writeNumberField("minRangeRequestSize", value.getMinRangeRequestSize()); |
| 328 | + } |
| 329 | + if (value.getInplaceSeekLimit() != DEFAULT_OPTIONS.getInplaceSeekLimit()) { |
| 330 | + gen.writeNumberField("inplaceSeekLimit", value.getInplaceSeekLimit()); |
| 331 | + } |
| 332 | + if (value.isGrpcReadZeroCopyEnabled() != DEFAULT_OPTIONS.isGrpcReadZeroCopyEnabled()) { |
| 333 | + gen.writeBooleanField("grpcReadZeroCopyEnabled", value.isGrpcReadZeroCopyEnabled()); |
| 334 | + } |
| 335 | + gen.writeEndObject(); |
| 336 | + } |
| 337 | +} |
| 338 | + |
| 339 | +class GcsReadOptionsDeserializer extends JsonDeserializer<GoogleCloudStorageReadOptions> { |
| 340 | + @Override |
| 341 | + public GoogleCloudStorageReadOptions deserialize(JsonParser p, DeserializationContext ctxt) |
| 342 | + throws java.io.IOException { |
| 343 | + JsonNode root = p.readValueAsTree(); |
| 344 | + GoogleCloudStorageReadOptions.Builder builder = |
| 345 | + GcsReadOptionsSerializer.DEFAULT_OPTIONS.toBuilder(); |
| 346 | + |
| 347 | + if (root != null && root.isObject()) { |
| 348 | + if (root.hasNonNull("fadvise")) { |
| 349 | + builder.setFadvise( |
| 350 | + GoogleCloudStorageReadOptions.Fadvise.valueOf(root.get("fadvise").asText())); |
| 351 | + } |
| 352 | + if (root.hasNonNull("fastFailOnNotFoundEnabled")) { |
| 353 | + builder.setFastFailOnNotFoundEnabled(root.get("fastFailOnNotFoundEnabled").asBoolean()); |
| 354 | + } else if (root.hasNonNull("fastFailOnNotFound")) { |
| 355 | + builder.setFastFailOnNotFoundEnabled(root.get("fastFailOnNotFound").asBoolean()); |
| 356 | + } |
| 357 | + if (root.hasNonNull("minRangeRequestSize")) { |
| 358 | + builder.setMinRangeRequestSize(root.get("minRangeRequestSize").asLong()); |
| 359 | + } |
| 360 | + if (root.hasNonNull("inplaceSeekLimit")) { |
| 361 | + builder.setInplaceSeekLimit(root.get("inplaceSeekLimit").asLong()); |
| 362 | + } |
| 363 | + if (root.hasNonNull("grpcReadZeroCopyEnabled")) { |
| 364 | + builder.setGrpcReadZeroCopyEnabled(root.get("grpcReadZeroCopyEnabled").asBoolean()); |
| 365 | + } |
| 366 | + } |
| 367 | + return builder.build(); |
| 368 | + } |
| 369 | +} |
0 commit comments