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

MF-703 - Reliably Publish Event Messages to Redis #1836

Merged
merged 13 commits into from
Jul 31, 2023

Conversation

rodneyosodo
Copy link
Member

What does this do?

I have introduced a mechanism for storing unpublished events and implementing retries. We use memory to save unpublished events when the Redis server is unavailable. Then, we periodically attempt to publish the stored events when the Redis server becomes available again.

  1. When an event needs to be published, check if the Redis server is reachable. Added a 100ms timeout so stop retrying to connect to Redis when it is down
  2. If the Redis server is reachable, publish the event.
  3. If the Redis server is unreachable, store the event in memory using slice.
  4. Periodically check the connection to the Redis server, after every minute, and retry publishing the stored events when the connection is re-established.
  5. Once an event is successfully published, remove it from memory.

Which issue(s) does this PR fix/relate to?

Resolves #703

List any changes that modify/break current functionality

added 100ms timeout to check if Redis is reachable

Have you included tests for your changes?

No

Did you document any new/modified functionality?

No

Notes

Tested by pausing the es-redis container and resuming it after doing some operations using the e2e tool.

@rodneyosodo rodneyosodo requested a review from a team as a code owner June 28, 2023 17:52
@codecov
Copy link

codecov bot commented Jun 28, 2023

Codecov Report

Merging #1836 (d5c598c) into master (3e7bac4) will increase coverage by 0.56%.
The diff coverage is 80.00%.

@@            Coverage Diff             @@
##           master    #1836      +/-   ##
==========================================
+ Coverage   67.12%   67.69%   +0.56%     
==========================================
  Files         118      118              
  Lines        9438     9434       -4     
==========================================
+ Hits         6335     6386      +51     
+ Misses       2436     2374      -62     
- Partials      667      674       +7     
Files Changed Coverage Δ
bootstrap/redis/producer/streams.go 80.53% <75.00%> (+24.12%) ⬆️
bootstrap/redis/producer/events.go 78.68% <100.00%> (+24.59%) ⬆️

... and 1 file with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@rodneyosodo rodneyosodo force-pushed the MF-703 branch 4 times, most recently from e3cc95a to 6f0b3c2 Compare July 10, 2023 12:25

type eventStore struct {
client *redis.Client
unpublishedEvents []*redis.XAddArgs
Copy link
Contributor

Choose a reason for hiding this comment

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

this can grow unexpectedly large sizes. I think a different approach is required for memory management. Perhaps a channel or some external queue like nats might serve better

Copy link
Member Author

Choose a reason for hiding this comment

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

Currently, nats is configured as fire and forget unless we move to Jetstream. I have implemented a channel with a max of 1M capacity.

Copy link
Member Author

Choose a reason for hiding this comment

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

Before publishing the memory footprint of the container was 17MB after publishing before resuming Redis it was 49MB

Copy link
Collaborator

@dborovcanin dborovcanin left a comment

Choose a reason for hiding this comment

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

I'm afraid that reliable publishing needs a little bit more than what we have. The buffer mechanism is not bad in itself, but what it effectively introduce is - the reordering of events. This topic is very advanced and out of the scope of this PR. What we can do is - provide a mechanism to mark these events "dirty" or "out of order" maybe by adding a field such as event_time that will represent the time event occurred and will be different from the time the event is published on Redis.

Copy link
Collaborator

@dborovcanin dborovcanin left a comment

Choose a reason for hiding this comment

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

Please fix tests and test locally.

@rodneyosodo rodneyosodo force-pushed the MF-703 branch 3 times, most recently from 93525f1 to b84c8d2 Compare July 12, 2023 10:15
@drasko
Copy link
Contributor

drasko commented Jul 28, 2023

@dborovcanin please review this one

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
@SammyOina SammyOina self-requested a review July 31, 2023 14:24
Copy link
Contributor

@SammyOina SammyOina left a comment

Choose a reason for hiding this comment

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

LGTM

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
Copy link
Contributor

@drasko drasko left a comment

Choose a reason for hiding this comment

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

LGTM

@drasko drasko merged commit abc1e3d into absmach:master Jul 31, 2023
3 checks passed
rodneyosodo added a commit to rodneyosodo/magistrala that referenced this pull request Aug 3, 2023
* Reliably Publish Event Messages to Redis

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Make Redis Producer Safe For Concurrent Use

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Combine Redis Publishing

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Add defer statement

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Use Channel Instead of Array

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Adding `occurred_at`

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Fix Check `occurred_at`

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Remove Unused Const

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Check For Non NIL Error on Publishing

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Add More Tests

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Hanndle When Channel Is Full

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Fix Issue After Rebase

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Fix Tests

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

---------

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
WashingtonKK pushed a commit to WashingtonKK/magistrala that referenced this pull request Aug 4, 2023
* Reliably Publish Event Messages to Redis

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Make Redis Producer Safe For Concurrent Use

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Combine Redis Publishing

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Add defer statement

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Use Channel Instead of Array

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Adding `occurred_at`

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Fix Check `occurred_at`

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Remove Unused Const

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Check For Non NIL Error on Publishing

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Add More Tests

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Hanndle When Channel Is Full

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Fix Issue After Rebase

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

* Fix Tests

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>

---------

Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
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.

Enable reliable publishing of event messages to Redis Streams
4 participants