-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BigQueryIO] fetch updated schema for newly created Storage API stream writers #33231
[BigQueryIO] fetch updated schema for newly created Storage API stream writers #33231
Conversation
R: @Abacn |
Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment |
…api_schemaupdate_dynamicdest
...e-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryServicesImpl.java
Outdated
Show resolved
Hide resolved
@@ -531,6 +532,30 @@ public void process( | |||
element.getKey().getKey(), dynamicDestinations, datasetService); | |||
tableSchema = converter.getTableSchema(); | |||
descriptor = converter.getDescriptor(false); | |||
|
|||
if (autoUpdateSchema) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure this is the ideal place to put this. getAppendClientInfo is called whenever the static cache is populated, meaning that on any worker restart, range move, etc. we'll be forced to call this API again. However we have persistent state in this DoFn, so we know if it's a "new" key or not. Can we use that to gate calling this method instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should always perform this check before creating a new StreamWriter , regardless of the reason for its creation. The only exception is if we already have an updated schema stored in state (see first if
block above). If I'm following correctly, this method (getAppendClientInfo
) will always create a new stream writer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also note that the updated schema is ignored when the StreamWriter object's creation time is later than the updated schema's.
i.e. it doesn't matter when the WriteStream itself was created
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so this doesn't quite solve the following race condition:
- schema S is updated to S' before creating stream writer, we detect it in this codepath and store it in state.
- StreamWriter is destroyed (either because it is idle and evicted from the cache, or because the worker crashes).
- We later recreate the StreamWriter. Because we have an updated schema in cache, we don't execute this codepath.
In this case, I think we'll completely miss the new schema. IIUC the best way to address this race would be for BigQuery to provide us with a version of StreamWriter that returns schemas with new fields - i.e. not basing it off of creation time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, this if
block would only be executed if we have not detected S' yet.
But if S' is detected and stored in state, we would execute the first if
block (line 520 here) and use that stored value. We still end up using the updated S', no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless:
- S is updated to S'
- current writer recognizes it and stores S' in state
- writer crashes for whatever reason
- S' is updated to S'' (before new writer is created)
- new writer is created, using the schema stored in state: S'
This new writer would have no chance to recognize S'' because it was created after that update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a much narrower edge-case though. It's a small subset of the bigger problem we currently have, which involves essentially any new StreamWriter.
Are we okay with merging this solution and continue to pursue BigQuery to improve things on their side?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that´s the race condition I had in mind. Is BigQuery planning on improving things on their side_
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will follow up with them
@Abacn can you take a look? Reuven is OOO for some time |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I saw the existing comments mainly concerned on additional API calls.
-
how often will the additional call will be made. If it's not significant I think this LGTM
-
does this PR introduces new API call if schema update feature isn't enabled (reading the change I believe no, but good to confirm)
...platform/src/test/java/org/apache/beam/sdk/io/gcp/bigquery/StorageApiSinkSchemaUpdateIT.java
Show resolved
Hide resolved
There's a call made for every new StreamWriter, which normally would be every time a new stream is created. In other words, it's roughly equal to the number of concurrent connections created at a given time. I checked in with BigQuery folks and they aren't worried about this quota.
No this is strictly for when autoUpdateSchema is enabled |
The test passed on Dataflow V2 postcommits |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
...e-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigquery/BigQueryServicesImpl.java
Outdated
Show resolved
Hide resolved
@@ -531,6 +532,30 @@ public void process( | |||
element.getKey().getKey(), dynamicDestinations, datasetService); | |||
tableSchema = converter.getTableSchema(); | |||
descriptor = converter.getDescriptor(false); | |||
|
|||
if (autoUpdateSchema) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so this doesn't quite solve the following race condition:
- schema S is updated to S' before creating stream writer, we detect it in this codepath and store it in state.
- StreamWriter is destroyed (either because it is idle and evicted from the cache, or because the worker crashes).
- We later recreate the StreamWriter. Because we have an updated schema in cache, we don't execute this codepath.
In this case, I think we'll completely miss the new schema. IIUC the best way to address this race would be for BigQuery to provide us with a version of StreamWriter that returns schemas with new fields - i.e. not basing it off of creation time.
LGTM |
…m writers (apache#33231) * add dynamic dest test * fix and add some tests * add to changes.md * fix whitespace * trigger postcommits * address comments
…m writers (apache#33231) * add dynamic dest test * fix and add some tests * add to changes.md * fix whitespace * trigger postcommits * address comments
…h ([BigQueryIO] fetch updated schema for newly created Storage API stream writers)
Fixes #33238
This problem happens whenever a StreamWriter is created after the table's schema is updated. The new StreamWriter ignores the previously updated schema and ends up using the base schema, dropping the new columns.
Such a case can happen when a pipeline decides to increase its parallelism (e.g. autosharding of threads or autoscaling of workers) after a schema update happens. The increased parallelism creates new stream writers that continue using the base schema, ignoring the updated schema.
This PR fixes this by fetching the stream's schema whenever we create a new append client. Existing tests are modified to cover this case, and new tests are added for schema update with dynamic destinations.