BuxOrg / bux

Bitcoin UTXO & xPub Management Engine
https://getbux.io
Apache License 2.0
36 stars 6 forks source link

refactor(BUX-411): taskmanager simplification & tasq with redis fixes #510

Closed chris-4chain closed 10 months ago

chris-4chain commented 11 months ago

Pull Request Checklist

Initially, the goal of this task was to simplify the taskmanager by fixing taskq and localCron and make some refactorization & simplification changes. All are done:

After my changes, a lot of test files had to be adjusted - that's why so many files are modified.

I tested it locally running different examples and bux-server with backend/frontend. Tasks were correctly scheduled.

TL;DR

Additionally, I tested the bux, running several parallel instances with Redis configuration for taskq. Apparently, the codebase before my changes didn't allow to run it that way. So I fixed the issue, according to task-doc, adding:

if factoryType == FactoryRedis {
  if err := q.Consumer().Start(ctx); err != nil {
      return err
  }
}

After that, the taskq in different nodes (instances) started to communicate via Redis.

But another problem appeared. Number of cronJob executions in defined period was about number of active nodes.

For example, For cronJob with 30sec period and 5 active instances: The job executions among all nodes was 5.

In this PR I'm proposing a fix using distributed redis locks with TTL.

Diagrams for better understanding:

Previous solution

image

New solution

image

I tested it with 6 instances and collecting local metrics. My example task has 2sec period and every time it was executed it sent 1(one) to my local influxDB.

I analysed the results, grouping metrics in 2sec aggregation window and then summing the number of points per window.

Without cronlocks

We can see, that the number of tasks executed per period is not constant and definitely it is not one. image

With cronlocks

Regardless how many instances are active, only one cronJob handler is executed per time window. image

Note:

Of course, the solution can be developed, but, for now:

In the future we should re-think whether using taskq is a good idea for us:

Maybe a good alternative will be asynq.

mergify[bot] commented 11 months ago

Welcome to our open-source project @chris-4chain! πŸ’˜

codecov[bot] commented 11 months ago

Codecov Report

Attention: 44 lines in your changes are missing coverage. Please review.

Comparison is base (1d3bbb2) 53.18% compared to head (12a41f8) 53.04%. Report is 8 commits behind head on master.

Additional details and impacted files [![Impacted file tree graph](https://app.codecov.io/gh/BuxOrg/bux/pull/510/graphs/tree.svg?width=650&height=150&src=pr&token=gh8XH5lven&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BuxOrg)](https://app.codecov.io/gh/BuxOrg/bux/pull/510?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BuxOrg) ```diff @@ Coverage Diff @@ ## master #510 +/- ## ========================================== - Coverage 53.18% 53.04% -0.15% ========================================== Files 110 107 -3 Lines 11251 11146 -105 ========================================== - Hits 5984 5912 -72 + Misses 4815 4787 -28 + Partials 452 447 -5 ``` | [Flag](https://app.codecov.io/gh/BuxOrg/bux/pull/510/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BuxOrg) | Coverage Ξ” | | |---|---|---| | [unittests](https://app.codecov.io/gh/BuxOrg/bux/pull/510/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BuxOrg) | `53.04% <75.14%> (-0.15%)` | :arrow_down: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BuxOrg#carryforward-flags-in-the-pull-request-comment) to find out more. | [Files](https://app.codecov.io/gh/BuxOrg/bux/pull/510?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BuxOrg) | Coverage Ξ” | | |---|---|---| | [client.go](https://app.codecov.io/gh/BuxOrg/bux/pull/510?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BuxOrg#diff-Y2xpZW50Lmdv) | `60.50% <100.00%> (-0.59%)` | :arrow_down: | | [client\_internal.go](https://app.codecov.io/gh/BuxOrg/bux/pull/510?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BuxOrg#diff-Y2xpZW50X2ludGVybmFsLmdv) | `72.04% <100.00%> (ΓΈ)` | | | [client\_options.go](https://app.codecov.io/gh/BuxOrg/bux/pull/510?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BuxOrg#diff-Y2xpZW50X29wdGlvbnMuZ28=) | `72.85% <100.00%> (-0.46%)` | :arrow_down: | | [taskmanager/options.go](https://app.codecov.io/gh/BuxOrg/bux/pull/510?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BuxOrg#diff-dGFza21hbmFnZXIvb3B0aW9ucy5nbw==) | `100.00% <100.00%> (ΓΈ)` | | | [taskmanager/cron\_jobs.go](https://app.codecov.io/gh/BuxOrg/bux/pull/510?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BuxOrg#diff-dGFza21hbmFnZXIvY3Jvbl9qb2JzLmdv) | `51.85% <57.14%> (-4.82%)` | :arrow_down: | | [taskmanager/taskmanager.go](https://app.codecov.io/gh/BuxOrg/bux/pull/510?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BuxOrg#diff-dGFza21hbmFnZXIvdGFza21hbmFnZXIuZ28=) | `70.76% <70.76%> (ΓΈ)` | | | [taskmanager/taskq.go](https://app.codecov.io/gh/BuxOrg/bux/pull/510?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BuxOrg#diff-dGFza21hbmFnZXIvdGFza3EuZ28=) | `77.19% <73.17%> (+7.28%)` | :arrow_up: | ------ [Continue to review full report in Codecov by Sentry](https://app.codecov.io/gh/BuxOrg/bux/pull/510?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BuxOrg). > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BuxOrg) > `Ξ” = absolute (impact)`, `ΓΈ = not affected`, `? = missing data` > Powered by [Codecov](https://app.codecov.io/gh/BuxOrg/bux/pull/510?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BuxOrg). Last update [1d3bbb2...12a41f8](https://app.codecov.io/gh/BuxOrg/bux/pull/510?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BuxOrg). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=BuxOrg).