jonathansudhakar1 opened a new pull request, #41953:
URL: https://github.com/apache/beam/pull/41953

   Addresses #22480.
   
   A follow-up to #41769, as discussed there: @stankiewicz suggested exposing 
the same knobs on the SchemaTransforms as the legacy Python wrappers have, so 
the legacy wrappers could later be removed or delegate to the SchemaTransforms. 
This PR does the read side. The write side can follow once #41769 is merged, 
since it touches the same provider.
   
   The Spanner read SchemaTransform (`spanner_read:v1`, YAML `ReadFromSpanner`) 
only does strong reads and can't point at another Spanner host or an emulator. 
The legacy Python `ReadFromSpanner` (`SpannerTransformRegistrar.ReadBuilder`) 
supports all of these, so users moving to the SchemaTransform lose 
point-in-time and stale reads.
   
   ## Changes
   
   `SpannerReadSchemaTransformProvider`, with the legacy wrapper's names and 
meaning:
   - **`timestamp_bound_mode`**: `STRONG` (the default, unchanged), 
`READ_TIMESTAMP` (with **`read_timestamp`**, RFC 3339) or `EXACT_STALENESS` 
(with **`staleness`** and **`time_unit`**). Mode and unit names are 
case-insensitive.
   - **`host`** and **`emulator_host`**, passed to `SpannerIO.Read#withHost` / 
`#withEmulatorHost`.
   - **Validation** (in `validate()`, which the transform already calls when 
it's built): each mode needs its own fields and rejects the others, `staleness` 
can't be negative, and `read_timestamp`/`time_unit` must parse. `MAX_STALENESS` 
and `MIN_READ_TIMESTAMP` are rejected with an explanation: SpannerIO reads in a 
batch read-only transaction (`CreateTransactionFn` → 
`BatchClient#batchReadOnlyTransaction`), and the Spanner client's 
`MultiUseReadOnlyTransaction` only allows those two modes for single-use reads.
   - `buildRead()` (package-private) builds the `SpannerIO.Read`, and a 
package-private `setServiceFactory` lets tests use `FakeServiceFactory`, as 
#41769 does for the write side.
   - `description()` gets a short stale-read example.
   
   Also:
   - `standard_io.yaml` maps the six new fields for YAML `ReadFromSpanner`.
   - CHANGES.md has a bullet under I/Os.
   
   With none of the new fields set, the transform builds the same 
`SpannerIO.Read` as before.
   
   ## 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 --rerun --tests 
'…SpannerReadSchemaTransformProviderTest' --tests 
'…SpannerSchemaTransformProviderTest' --tests '…SpannerIOReadTest' --tests 
'…SpannerTransformRegistrarTest'`: 8/8, 7/7, 33/33 and 22/22 passed.
     - The new `SpannerReadSchemaTransformProviderTest` checks the bound, host 
and emulator host on the built `SpannerIO.Read`, the unchanged defaults, and 
every validation error. One test runs the transform in a pipeline against 
`FakeServiceFactory`, where the fake batch client only returns a transaction 
for the expected read timestamp, so the read fails if the bound doesn't reach 
the transaction.
     - It's a new test class so it doesn't conflict with #41769, which changes 
most of `SpannerSchemaTransformProviderTest`. That file only gets the six new 
names in `testReadFindTransformAndMakeItWork`.
     - Mutation check: with `withTimestampBound` removed and the emulator host 
replaced, 4 of the 8 new tests failed, including the pipeline test.
   - YAML, through a locally built 
`:sdks:java:io:google-cloud-platform:expansion-service:shadowJar` (no Spanner 
connection):
     - The YAML `ReadFromSpanner` config schema lists the six fields (strings, 
and an `INT64` `staleness`, all nullable).
     - `timestamp_bound_mode: MAX_STALENESS` fails at expansion with the new 
message.
     - With `EXACT_STALENESS`, `staleness: 15`, `time_unit: SECONDS` and 
`emulator_host: 'localhost:59999'`, expansion reaches schema inference, which 
fails with `The Spanner emulator host has been set to localhost:59999, but no 
running emulator could be found at that address.` So the options pass 
validation and the emulator host reaches `SpannerIO`.
   - `pytest apache_beam/yaml/yaml_provider_unit_test.py 
apache_beam/yaml/yaml_io_test.py`: 49 passed, 23 skipped. `pre-commit` on the 
changed files passed.
   - **Not run:** `SpannerReadIT` and the YAML Spanner extended tests, which 
need GCP or the Docker emulator.
   
   ## Open questions
   
   - **Names:** I kept the legacy wrapper's `timestamp_bound_mode` / 
`read_timestamp` / `staleness` / `time_unit` so the legacy `ReadFromSpanner` 
could pass its arguments straight through later. Happy to rename (for example 
to `read_timestamp` plus `exact_staleness_seconds`) if you'd prefer a smaller 
YAML surface.
   - **Legacy wrapper:** it accepts `MAX_STALENESS` and `MIN_READ_TIMESTAMP`, 
and its docstring example uses `MAX_STALENESS`. From reading the code (not run 
against Spanner), those would fail on the workers when the transaction is 
created. I left the legacy wrapper alone, since it's out of scope here, but can 
send a small docs fix if that's useful.
   - **CHANGES.md:** this and #41769 both add an I/Os bullet, so whichever 
merges second needs a rebase. I'll take care of that.
   


-- 
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]

Reply via email to