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

feat: decouple round tracker from API routes #292

Merged
merged 5 commits into from
May 15, 2024
Merged

Conversation

bajtos
Copy link
Member

@bajtos bajtos commented May 14, 2024

feat: decouple round tracker from API routes

Rework API handlers to get the current round from the database.

Benefits:

  • This commit fixes the long-standing bug where the REST API returned different current round depending on which worker process received the request. This was caused by different timing when each worker process detected a new RoundStarted event.

  • The new architecture allows us to further decouple the round tracker from the API handler. We can run it in background to not block process startup or even extract it into a standalone service.

Downsides:

  • More database queries. Before my changes, the current round was available in memory. Now we need to read it from the database. Affected endpoints:
    • POST /measurements
    • GET /rounds/current

Note: The next commit reworks the query recording a new measurement to obtain the current round number in a sub-query of the INSERT statement.

perf: inline getCurrentRound()

  • Rework the query inserting new measurements to fetch the current round as part of the single INSERT statement.

  • Inline getCurrentRound() implementation into the single remaining place calling it - the handler for GET /rounds/current.

Links:

Rework API handlers to get the current round from the database.

Benefits:

- This commit fixes the long-standing bug where the REST API returned
  different current round depending on which worker process received
  the request. This was caused by different timing when each worker
  process detected a new RoundStarted event.

- The new architecture allows us to further decouple the round tracker
  from the API handler. We can run it in background to not block
  process startup or even extract it into a standalone service.

Downsides:

- More database queries. Before my changes, the current round was
  available in memory. Now we need to read it from the database.
  Affected endpoints:
  - `POST /measurements`
  - `GET /rounds/current`

Note: it should be possible to rework the query recording a new
measurement to obtain the current round number in a sub-query
of the `INSERT` statement.

Signed-off-by: Miroslav Bajtoš <oss@bajtos.net>
- Rework the query inserting new measurements to fetch the current round
  as part of the single INSERT statement.

- Inline `getCurrentRound()` implementation into the single remaining
  place calling it - the handler for `GET /rounds/current`.

Signed-off-by: Miroslav Bajtoš <oss@bajtos.net>
Copy link
Member

@juliangruber juliangruber left a comment

Choose a reason for hiding this comment

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

I very much agree with the approach of this PR 👏 While I'm also concerned about the extra queries on POST /measurements, I think we can optimize this later if we identify that there's a problem here. Great clean up!

bin/spark.js Outdated Show resolved Hide resolved
bajtos and others added 2 commits May 15, 2024 15:08
Co-authored-by: Julian Gruber <julian@juliangruber.com>
@bajtos bajtos enabled auto-merge (squash) May 15, 2024 13:09
Signed-off-by: Miroslav Bajtoš <oss@bajtos.net>
@bajtos bajtos merged commit 574f02d into main May 15, 2024
6 checks passed
@bajtos bajtos deleted the current-round-from-db branch May 15, 2024 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: ✅ done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants