-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
[extension/dbstorage] Add DB Transactions to dbstorage.Batch() method #37805
[extension/dbstorage] Add DB Transactions to dbstorage.Batch() method #37805
Conversation
c57c90e
to
3129f39
Compare
3129f39
to
c6641c6
Compare
if err != nil { | ||
return err | ||
} | ||
//nolint:errcheck |
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.
do we not want to do something on error here?
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.
Agreed, I'd at least log the error.
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.
Added logging here
Unfortunately there was no logger instance in this object, so have to inject it, I believe it might be useful in this object for future improvements
return err | ||
} | ||
//nolint:errcheck | ||
defer tx.Rollback() |
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 might be confusing for other developers. We commit the tx
on return and a rollback call is also made due to this defer.
There are two scenarios:
- when any error is encoutered
- when no error is encountered
First scenario is straightforward. We return the error and a rollback is called.
For the second scenario, we commit the transaction and then rollback is called due to the defer. The rollback call in this scenario is a no-op because tx
is already committed.
Can we add a comment here to mention this nuance?
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.
Added few comments to clarify commit/rollback flow, as well as for tx.Rollback() error handling
Added few comments to clarify commit/rollback flow
57c2d9d
to
d65307c
Compare
…open-telemetry#37805) <!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description According to [Storage API documentation](https://github.com/open-telemetry/opentelemetry-collector/blob/main/extension/xextension/storage/README.md) `Batch` is expected to "execute several operations in a single transaction" Moreover, Persistent Queue in `exporterhelper` is actively using `storage.Batch()` with read+write/write+delete operations in single call, which is really needs to be in a single transaction to avoid accidental data inconsistency in Persistent Queue in case of unexpected service shutdown. For example [here](https://github.com/open-telemetry/opentelemetry-collector/blob/main/exporter/exporterqueue/persistent_queue.go#L140) or [here](https://github.com/open-telemetry/opentelemetry-collector/blob/main/exporter/exporterqueue/persistent_queue.go#L257) or [here](https://github.com/open-telemetry/opentelemetry-collector/blob/main/exporter/exporterqueue/persistent_queue.go#L463) As currently supported SQlite and PostgreSQL driver natively support Transaction - this PR implements single transaction inside `storage.Batch()` call Also, I've added bunch of unit tests that were missing to ensure that show Storage API works as expected in `dbstorage` extension <!--Describe what testing was performed and which tests were added.--> #### Testing Respective Unit Tests were added --------- Co-authored-by: Sean Marciniak <[email protected]>
Description
According to Storage API documentation
Batch
is expected to "execute several operations in a single transaction"Moreover, Persistent Queue in
exporterhelper
is actively usingstorage.Batch()
with read+write/write+delete operations in single call, which is really needs to be in a single transaction to avoid accidental data inconsistency in Persistent Queue in case of unexpected service shutdown. For example here or here or hereAs currently supported SQlite and PostgreSQL driver natively support Transaction - this PR implements single transaction inside
storage.Batch()
callAlso, I've added bunch of unit tests that were missing to ensure that show Storage API works as expected in
dbstorage
extensionTesting
Respective Unit Tests were added