Skip to content
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

fix(jobsdb): update cache after transaction completes #2567

Merged
merged 3 commits into from
Oct 14, 2022

Conversation

atzoum
Copy link
Contributor

@atzoum atzoum commented Oct 13, 2022

Description

By updating (invalidating) the cache entries before the transaction commits and for the period until the transaction actually commits, we are risking to have other select queries who arrive during this timeframe to populate the cache with incorrect values: until the transaction commits, changes are not visible to other queries, thus they may erroneously put "no result" entries in the cache.

The above can have as a side-effect to:

  1. Delay processing of whole datasets until cache entries expire (observed with proc_error tables)
  2. Cause out-of-order event delivery

This fix introduces a jobsdb.Tx that wraps a sql.Tx, adding support for registering success listeners. Now, all cache entries are updated after the transaction has been successfully committed.

Notion Ticket

Link

Security

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

@atzoum atzoum changed the base branch from master to release/1.2.x October 14, 2022 07:43
@atzoum atzoum changed the title fix(jobsdb): update cache after transaction completes fix(jobsdb): update cache after transaction completion Oct 14, 2022
@atzoum atzoum changed the title fix(jobsdb): update cache after transaction completion fix(jobsdb): update cache after transaction completes Oct 14, 2022
@codecov
Copy link

codecov bot commented Oct 14, 2022

Codecov Report

Base: 43.86% // Head: 43.86% // Increases project coverage by +0.00% 🎉

Coverage data is based on head (022d864) compared to base (8cba05e).
Patch coverage: 81.91% of modified lines in pull request are covered.

Additional details and impacted files
@@              Coverage Diff               @@
##           release/1.2.x    #2567   +/-   ##
==============================================
  Coverage          43.86%   43.86%           
==============================================
  Files                187      187           
  Lines              39090    39107   +17     
==============================================
+ Hits               17146    17156   +10     
- Misses             20860    20870   +10     
+ Partials            1084     1081    -3     
Impacted Files Coverage Δ
router/batchrouter/batchrouter.go 34.13% <25.00%> (ø)
router/router.go 66.76% <66.66%> (ø)
jobsdb/jobsdb.go 67.68% <83.75%> (+0.01%) ⬆️
gateway/gateway.go 55.76% <100.00%> (ø)
processor/processor.go 71.91% <100.00%> (ø)
enterprise/reporting/setup.go 38.09% <0.00%> (-14.29%) ⬇️
services/stats/stats.go 88.48% <0.00%> (-1.82%) ⬇️
enterprise/reporting/reporting.go 8.33% <0.00%> (-1.44%) ⬇️
services/rsources/handler.go 70.55% <0.00%> (+0.83%) ⬆️
... and 2 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@@ -668,7 +701,7 @@ func loadConfig() {
config.RegisterDurationConfigVariable(5, &addNewDSLoopSleepDuration, true, time.Second, []string{"JobsDB.addNewDSLoopSleepDuration", "JobsDB.addNewDSLoopSleepDurationInS"}...)
config.RegisterDurationConfigVariable(5, &refreshDSListLoopSleepDuration, true, time.Second, []string{"JobsDB.refreshDSListLoopSleepDuration", "JobsDB.refreshDSListLoopSleepDurationInS"}...)
config.RegisterDurationConfigVariable(5, &backupCheckSleepDuration, true, time.Second, []string{"JobsDB.backupCheckSleepDuration", "JobsDB.backupCheckSleepDurationIns"}...)
config.RegisterDurationConfigVariable(60, &cacheExpiration, true, time.Minute, []string{"JobsDB.cacheExpiration"}...)
config.RegisterDurationConfigVariable(5, &cacheExpiration, true, time.Minute, []string{"JobsDB.cacheExpiration"}...)
Copy link
Contributor Author

@atzoum atzoum Oct 14, 2022

Choose a reason for hiding this comment

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

5 minutes by default should be more than enough and could potentially alleviate the consequences of having an incorrectly populated cache for too long.

@atzoum atzoum marked this pull request as ready for review October 14, 2022 10:53
// Commit commits the transaction and executes all listeners.
func (tx *Tx) Commit() error {
err := tx.Tx.Commit()
if err == nil {
Copy link
Member

Choose a reason for hiding this comment

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

Any reason why we are not checking the error early and returning?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the future we might want to introduce other listeners as well, such as errorListeners and completionListeners. In such a case returning early wouldn't be relevant.
Now it can be done, but I preferred having one less loc instead :)

if err != nil {
	return err
}
for _, successListener := range tx.successListeners {
	successListener()
}
return nil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants