Skip to content

fix: race condition in table schema updates causing incorrect schema to be saved - #5756

Merged
shekhar-rudder merged 3 commits into
release/1.47.xfrom
war-565-schema-updates
Apr 23, 2025
Merged

shekhar-rudder merged 3 commits into
release/1.47.xfrom
war-565-schema-updates

Conversation

@shekhar-rudder

@shekhar-rudder shekhar-rudder commented Apr 23, 2025 •

Copy link
Copy Markdown
Member

Description

BUG
The UpdateTableSchema method can be invoked concurrently by multiple goroutines, each fetching its own copy of the schema using getSchema. This can result in a race condition where concurrent updates to the schema map may override each other, leading to incorrect data being saved.

FIX

  • Introduced a lock at the beginning of the UpdateTableSchema method to ensure only one goroutine can execute it at a time, preventing concurrent modifications.
  • Refactored getSchema to simplify mutex logic by moving schema fetching to the constructor. This required moving setting of schemaHandle in UploadJob struct from its constructor to run method
  • Updated the New method of schema to replace db with two new arguments schemaRepo and stagingFileRepo for easier testing

Security

  • The code changed/added as part of this pull request won't create any security issues with how the software is being used.

@shekhar-rudder
shekhar-rudder force-pushed the war-565-schema-updates branch 4 times, most recently from 2d6e725 to 7edff1b Compare April 23, 2025 07:31
@codecov

codecov Bot commented Apr 23, 2025 •

Copy link
Copy Markdown

Codecov Report

Attention: Patch coverage is 84.26966% with 14 lines in your changes missing coverage. Please review.

Project coverage is 77.04%. Comparing base (b5af4c2) to head (9589242).
Report is 1 commits behind head on release/1.47.x.

Files with missing lines Patch % Lines
warehouse/schema/schema.go 85.91% 7 Missing and 3 partials ⚠️
warehouse/router/upload.go 77.77% 3 Missing and 1 partial ⚠️
Additional details and impacted files
@@                Coverage Diff                 @@
##           release/1.47.x    #5756      +/-   ##
==================================================
+ Coverage           76.98%   77.04%   +0.06%     
==================================================
  Files                 487      487              
  Lines               66244    66228      -16     
==================================================
+ Hits                50997    51027      +30     
+ Misses              12475    12430      -45     
+ Partials             2772     2771       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread warehouse/schema/schema.go Resolved
Comment thread warehouse/schema/schema.go Resolved
Comment on lines +142 to +143
duration := math.Round((sh.now().Sub(start).Minutes() * 1000)) / 1000
sh.log.Infon("Fetched schema from warehouse", obskit.DestinationID(sh.warehouse.Destination.ID), obskit.Namespace(sh.warehouse.Type), logger.NewFloatField("timeTakenInMinutes", duration))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: Why minutes and not seconds? Would values of few seconds get rounded as 0 if using minutes?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why minutes and not seconds?

I believe we would be primarily interested in larger values, so I thought it would be better to use minutes instead of seconds.

Would values of few seconds get rounded as 0 if using minutes?

No. math.Round((sh.now().Sub(start).Minutes() * 1000)) / 1000 ensures value upto 3 decimals

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say can keep it seconds. As in using minutes do not give any advantage as such.

return tableSchemaDiff(tableName, sh.cachedSchema, tableSchema), nil
}

func (sh *schema) saveSchema(ctx context.Context, newSchema model.Schema) error {

@RanjeetMishra RanjeetMishra Apr 23, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: This we can discuss later while we start testing this fix. Should this func saveSchema(ctx context.Context, newSchema model.Schema) be thread safe instead of usage to be thread safe?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this func be thread safe

This function only executes a Postgres query and doesn't perform any other operations, so there's no need to use a lock. In the case of concurrent calls from multiple goroutines, the final DB value will be that of the last write.

@RanjeetMishra RanjeetMishra Apr 23, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. In our usage, we are taking lock to update DB and memory cache both which is right.
  2. the final DB value will be that of the last write -- I am still doubtful about this. Say three goroutines are planning to write value A, B, C. Last write can be any of these? Unless we are using it thread safe as in point 1.

Nvm about point 2. I understand what you are saying, let me think more 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By last write, I meant whoever finished last and not in the order which they invoked this function

Say three goroutines are planning to write value A, B, C. Last write can be any of these?

Yes

@shekhar-rudder
shekhar-rudder merged commit 5ec12a0 into release/1.47.x Apr 23, 2025
@shekhar-rudder
shekhar-rudder deleted the war-565-schema-updates branch April 23, 2025 10:37
shekhar-rudder pushed a commit that referenced this pull request Apr 23, 2025
🤖 I have created a release *beep* *boop*
---


##
[1.47.2](v1.47.1...v1.47.2)
(2025-04-23)


### Bug Fixes

* race condition in table schema updates causing incorrect schema to be
saved ([#5756](#5756))
([5ec12a0](5ec12a0))
* warehouse extracting messageId and receivedAt from event payload
([#5757](#5757))
([2532122](2532122))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
shekhar-rudder added a commit that referenced this pull request Apr 23, 2025
# Description

Syncing patch release v1.47.2 to main branch

**↓↓ Please review and edit commit overrides before merging ↓↓**

BEGIN_COMMIT_OVERRIDE
fix: race condition in table schema updates causing incorrect schema to
be saved (#5756)
fix: warehouse extracting messageId and receivedAt from event payload
(#5757)
END_COMMIT_OVERRIDE

---------

Co-authored-by: Akash Chetty <[email protected]>
Co-authored-by: shekhar-rudder <[email protected]>
Sidddddarth pushed a commit that referenced this pull request Apr 29, 2025
🤖 I have created a release *beep* *boop*
---


##
[1.48.0-rc.2](v1.47.0...v1.48.0-rc.2)
(2025-04-28)


### Features

* introduce common http client for reporting
([#5669](#5669))
([594bdce](594bdce))
* populate bytesPerTable for warehouse process API
([#5737](#5737))
([fd46379](fd46379))
* query load files using upload_id instead of staging_file_id
([#5700](#5700))
([3749ba9](3749ba9))
* support for creating upload v2 notifier jobs
([#5729](#5729))
([dbc830f](dbc830f))
* use header augmenter for router transformer client
([#5640](#5640))
([3749ba9](3749ba9))


### Bug Fixes

* httplb client doesn't respect connection limits
([#5773](#5773))
([ef0a15e](ef0a15e))
* jobsdb payload limit used compressed column sizes instead of actual
payload length
([#5723](#5723))
([c711b25](c711b25))
* race condition in table schema updates causing incorrect schema to be
saved ([#5756](#5756))
([f5ab51a](f5ab51a))
* use copy to load table in append mode for
redshift([#5743](#5743))
([877da25](877da25))
* use warehouse transformer event instead of using processor event
([#5764](#5764))
([f51e260](f51e260))
* version downgrade for databricks-sql-go to 1.6.1
([#5741](#5741))
([c711b25](c711b25))
* warehouse extracting messageId and receivedAt from event payload
([#5757](#5757))
([f5ab51a](f5ab51a))


### Miscellaneous

* add destination type in metrics
([#5727](#5727))
([fa38f34](fa38f34))
* **batchrouter:** limiter cleanup adjust stats and use harmonized
config
([#5783](#5783))
([96aba9d](96aba9d))
* change jobsdb payload column type default to text
([#5746](#5746))
([0f436e7](0f436e7))
* configure pubsub publish settings
([#5755](#5755))
([e481992](e481992))
* **deps:** bump google.golang.org/api from 0.228.0 to 0.229.0 in the
frequent group
([#5734](#5734))
([32854cc](32854cc))
* **deps:** bump the frequent group with 3 updates
([#5769](#5769))
([12b7d96](12b7d96))
* disable transformer compaction by default
([#5766](#5766))
([c504aa0](c504aa0))
* granular configuration for adaptive rate limiting
([#5739](#5739))
([c711b25](c711b25))
* improve BRT performance
([#5730](#5730))
([61642c8](61642c8))
* **jobsdb:** remove jobMinRowsMigrateThres configuration option
([#5763](#5763))
([5d2fae9](5d2fae9))
* processor UT mirroring
([#5710](#5710))
([f4f056c](f4f056c))
* remove payload logging
([#5447](#5447))
([8e08c11](8e08c11))
* trim down tracking plan validation payload
([#5749](#5749))
([e711579](e711579))
* use reloadable limiters
([#5744](#5744))
([d344d40](d344d40))
* use transformer compaction by default
([#5740](#5740))
([53792f2](53792f2))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Sidddddarth pushed a commit that referenced this pull request Apr 29, 2025
🤖 I have created a release *beep* *boop*
---


##
[1.48.0](v1.47.0...v1.48.0)
(2025-04-28)


### Features

* introduce common http client for reporting
([#5669](#5669))
([594bdce](594bdce))
* populate bytesPerTable for warehouse process API
([#5737](#5737))
([fd46379](fd46379))
* query load files using upload_id instead of staging_file_id
([#5700](#5700))
([3749ba9](3749ba9))
* support for creating upload v2 notifier jobs
([#5729](#5729))
([dbc830f](dbc830f))
* use header augmenter for router transformer client
([#5640](#5640))
([3749ba9](3749ba9))


### Bug Fixes

* httplb client doesn't respect connection limits
([#5773](#5773))
([ef0a15e](ef0a15e))
* jobsdb payload limit used compressed column sizes instead of actual
payload length
([#5723](#5723))
([c711b25](c711b25))
* race condition in table schema updates causing incorrect schema to be
saved ([#5756](#5756))
([f5ab51a](f5ab51a))
* use copy to load table in append mode for
redshift([#5743](#5743))
([877da25](877da25))
* use warehouse transformer event instead of using processor event
([#5764](#5764))
([f51e260](f51e260))
* version downgrade for databricks-sql-go to 1.6.1
([#5741](#5741))
([c711b25](c711b25))
* warehouse extracting messageId and receivedAt from event payload
([#5757](#5757))
([f5ab51a](f5ab51a))


### Miscellaneous

* add destination type in metrics
([#5727](#5727))
([fa38f34](fa38f34))
* **batchrouter:** limiter cleanup adjust stats and use harmonized
config
([#5783](#5783))
([96aba9d](96aba9d))
* change jobsdb payload column type default to text
([#5746](#5746))
([0f436e7](0f436e7))
* configure pubsub publish settings
([#5755](#5755))
([e481992](e481992))
* **deps:** bump google.golang.org/api from 0.228.0 to 0.229.0 in the
frequent group
([#5734](#5734))
([32854cc](32854cc))
* **deps:** bump the frequent group with 3 updates
([#5769](#5769))
([12b7d96](12b7d96))
* disable transformer compaction by default
([#5766](#5766))
([c504aa0](c504aa0))
* granular configuration for adaptive rate limiting
([#5739](#5739))
([c711b25](c711b25))
* improve BRT performance
([#5730](#5730))
([61642c8](61642c8))
* **jobsdb:** remove jobMinRowsMigrateThres configuration option
([#5763](#5763))
([5d2fae9](5d2fae9))
* processor UT mirroring
([#5710](#5710))
([f4f056c](f4f056c))
* remove payload logging
([#5447](#5447))
([8e08c11](8e08c11))
* trim down tracking plan validation payload
([#5749](#5749))
([e711579](e711579))
* use reloadable limiters
([#5744](#5744))
([d344d40](d344d40))
* use transformer compaction by default
([#5740](#5740))
([53792f2](53792f2))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants