Repository navigation
[SpannerIO] Add write results and write options to the Spanner write SchemaTransform - #41769
jonathansudhakar1 wants to merge 2 commits 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
There was a problem hiding this comment.
Thanks, good catch. I was wrong in the description: a nanosecond snapshot timestamp would have hit that AssertionError instead of being truncated. Fixed in 98152b6: toInstant now truncates to ChronoUnit.MICROS, the schema Javadoc and the emit_write_results description say so, and there are two new tests. testWriteResultsRowTruncatesTimestampsToMicros encodes the row with RowCoder, and testWriteEmitsWriteResults now uses a nanosecond snapshot timestamp end to end. Both failed with that AssertionError before the fix.
…onds Spanner snapshot timestamps can carry nanoseconds, and MicrosInstant rejects sub-microsecond values with an AssertionError when the row is encoded. Truncate both timestamps to microseconds before building the write_results row, document it, and test it with a nanosecond snapshot timestamp (directly and through a pipeline).
|
Thanks for the review! I kept After the change I reran spotlessCheck, compile (Checker on), checkstyle and javadoc, plus |
|
thanks for working on this. Feel free to work on follow-ups, I think it makes sense to expose similar knobs as legacy wrappers, to give possibility in the future to remove them/ invoke schematransform from those methods. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #41769 +/- ##
=============================================
+ Coverage 37.41% 58.95% +21.53%
Complexity 2336 2336
=============================================
Files 646 1136 +490
Lines 86550 171277 +84727
Branches 1552 1552
=============================================
+ Hits 32386 100975 +68589
- Misses 51608 67746 +16138
Partials 2556 2556
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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': 13/13, 46/46 and 22/22 passed (rerun after the review fix).FakeServiceFactory. They cover the write-results rows (microsecond timestamps round-trip exactly; a nanosecond snapshot timestamp is truncated to microseconds and the row encodes), 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).MicrosInstantkeepscommit_timestampexactly. Snapshot timestamps can carry nanoseconds. The first version of this PR was wrong to say those would be truncated:MicrosInstantthrows anAssertionErroron them. Since 98152b6, both timestamps are explicitly truncated to microseconds, and that's documented and tested.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.