Repository navigation
[SpannerIO] Add write results and write options to the Spanner write SchemaTransform - #41769
jonathansudhakar1 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
📋 Review Summary
This PR cleanly adds emit_write_results and additional write configuration options (priority, max_commit_delay_millis, transaction_tag, and exclude_txn_from_change_streams) to SpannerWriteSchemaTransformProvider and exposes them in Beam YAML's WriteToSpanner transform. The implementation properly preserves compatibility with error_handling by enabling commit stats directly on SpannerIO.Write, and includes thorough unit test coverage.
🔍 General Feedback
- Enabling
commitStatson theSpannerIO.Writebuilder rather than callingwithWriteResults()is a clean approach that allowswrite_resultsanderror_handlingoutputs to coexist on the sameSpannerWriteResult. - Configuration validation, schema logical types (
MicrosInstant), and YAML mappings align well with existing Beam SchemaTransform conventions.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
|
Assigning reviewers: R: @nielm for label spanner. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
…SchemaTransform The Spanner write SchemaTransform (spanner_write:v1, YAML WriteToSpanner) is the recommended way to write to Spanner from Python and YAML, but it can't return SpannerIO.Write#withWriteResults() commit metadata, and it exposes none of the newer write options. Add an opt-in emit_write_results flag. When it's set, a new write_results output gets one row per committed batch: mutation_count (INT64) plus commit_timestamp and snapshot_timestamp (nullable MicrosInstant; Spanner commit timestamps have microsecond granularity). post-write and the error_handling output don't change. The transform turns on commit stats on SpannerIO.Write itself instead of using withWriteResults(), so the failed mutations stay available and error_handling works together with the write results (see apache#41764). Also add the optional priority (LOW/MEDIUM/HIGH), max_commit_delay_millis, transaction_tag and exclude_txn_from_change_streams options, map all new fields in standard_io.yaml, and validate the configuration when the transform is built, as the read SchemaTransform does.
c10a399 to
09d3c7b
Compare
|
naming looks fine. |
| private static @Nullable Instant toInstant(@Nullable Timestamp timestamp) { | ||
| return timestamp == null | ||
| ? null | ||
| : Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos()); |
There was a problem hiding this comment.
Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos())
.truncatedTo(ChronoUnit.MICROS);
otherwise MicrosInstant will throw AssertionError
Addresses #22480.
This follows the plan @stankiewicz laid out on the issue: expose
SpannerIO.Write#withWriteResults()and the missing write options on the Spanner write SchemaTransform (spanner_write:v1, YAMLWriteToSpanner), since Abacn noted it's the recommended Spanner write path for Python. The legacySpannerTransformRegistrartransforms inapache_beam.io.gcp.spannerdon't change.Changes
SpannerWriteSchemaTransformProvider:emit_write_results(optional, defaultfalse) adds awrite_resultsoutput with one row per batch committed to Spanner:mutation_count(INT64)commit_timestampandsnapshot_timestamp(nullableMicrosInstant)post-writeand theerror_handlingoutput don't change. With the flag off, the transform expands the same way as before.Works together with
error_handling([Bug]: SpannerIO.Write#withWriteResults() silently drops failed mutations when FailureMode.REPORT_FAILURES is set #41764). The provider turns on commit stats onSpannerIO.Writeitself (it's in the same package) instead of going throughwithWriteResults(), which returns only theWriteResults. So the sameSpannerWriteResultstill has the failed mutations, and Reject REPORT_FAILURES with SpannerIO withWriteResults #41765's new check, which only guardsWriteWithResults/WriteGroupedWithResults, doesn't apply.New optional options:
priority(LOW/MEDIUM/HIGH, any case; the default staysMEDIUM),max_commit_delay_millis,transaction_tagandexclude_txn_from_change_streams. Each maps to the matchingSpannerIO.Write/SpannerConfigsetting and is left alone when unset.Validation at construction: the transform now calls
configuration.validate()when it's built, asSpannerReadSchemaTransformProvideralready does.validate()also rejects an unknownpriorityand a negativemax_commit_delay_millis.Docs: the field descriptions and
description()explain the new output, andoutputCollectionNames()listswrite_results.Also:
standard_io.yamlmaps the five new fields for YAMLWriteToSpanner.Testing
All with JDK 21. Checker Framework was on, as in CI.
./gradlew :sdks:java:io:google-cloud-platform:spotlessCheck :sdks:java:io:google-cloud-platform:compileJava :sdks:java:io:google-cloud-platform:compileTestJava :sdks:java:io:google-cloud-platform:checkstyleMain :sdks:java:io:google-cloud-platform:checkstyleTest :sdks:java:io:google-cloud-platform:javadoc: passed../gradlew :sdks:java:io:google-cloud-platform:test --tests '…SpannerSchemaTransformProviderTest' --tests '…SpannerIOWriteTest' --tests '…SpannerTransformRegistrarTest': 12/12, 46/46 and 22/22 passed.FakeServiceFactory. They cover the write-results rows (microsecond timestamps round-trip exactly), write results anderror_handlingtogether (one key fails, one commits), the options reachingSpannerIO.Write, the unchanged defaults (onlypost-write, no commit stats), and validation.:sdks:java:io:google-cloud-platform:expansion-service:shadowJarand expanded a YAMLWriteToSpannerthrough it, with all five new fields pluserror_handling.post-write,write_resultsand the error output, and Python sees the timestamps asOptional[Timestamp].priority: urgentfails at expansion withpriority must be one of LOW, MEDIUM or HIGH, but was 'urgent'.pytest apache_beam/yaml/yaml_provider_unit_test.py apache_beam/yaml/yaml_io_test.py: 49 passed, 23 skipped.pre-commiton the changed files passed.SpannerWriteITand the YAML Spanner extended tests. They need GCP or the Docker emulator.Open questions
max_commit_delay_millisrather than themax_commit_delayI wrote on the issue, to match other SchemaTransform configs that put the unit in the name (poll_interval_millis,triggering_frequency_seconds). Happy to rename.MicrosInstantkeepscommit_timestampexactly. I didn't find the same statement for snapshot timestamps. Any sub-microsecond part ofsnapshot_timestampwould be truncated.max_batch_size_bytes,max_number_mutations,max_number_rows,commit_deadline), and a write operation other than insert-or-update. Both would be separate PRs, if they're wanted.