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

[extension/dbstorage] Add DB Transactions to dbstorage.Batch() method #37805

Conversation

Fiery-Fenix
Copy link
Contributor

Description

According to Storage API documentation 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 or here or here
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

Testing

Respective Unit Tests were added

@Fiery-Fenix Fiery-Fenix force-pushed the feat/dbstorage-transaction-support branch from 3129f39 to c6641c6 Compare February 10, 2025 13:48
if err != nil {
return err
}
//nolint:errcheck
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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()
Copy link
Contributor

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:

  1. when any error is encoutered
  2. 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?

Copy link
Contributor Author

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
@Fiery-Fenix Fiery-Fenix force-pushed the feat/dbstorage-transaction-support branch from 57c2d9d to d65307c Compare February 11, 2025 11:57
@atoulme atoulme added extension/storage/dbstorage ready to merge Code review completed; ready to merge by maintainers labels Feb 12, 2025
@github-actions github-actions bot requested a review from atoulme February 12, 2025 01:48
@MovieStoreGuy MovieStoreGuy merged commit ee2d165 into open-telemetry:main Feb 12, 2025
162 checks passed
@github-actions github-actions bot added this to the next release milestone Feb 12, 2025
khushijain21 pushed a commit to khushijain21/opentelemetry-collector-contrib that referenced this pull request Feb 14, 2025
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension/storage/dbstorage extension/storage ready to merge Code review completed; ready to merge by maintainers
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants