Skip to content

refactor(storage): introduce transaction runner, move restart workunit logic out of the spanner storage - #950

Merged
andrzejfudro merged 8 commits into
mainfrom
refactor/restart-workunit-executions
Sep 28, 2026
Merged

andrzejfudro merged 8 commits into
mainfrom
refactor/restart-workunit-executions

Conversation

@andrzejfudro

@andrzejfudro andrzejfudro commented Sep 22, 2026 •

Copy link
Copy Markdown
Collaborator

This PR is one step towards a storage refactor. There are a couple of other methods that need to be refactored in a similar way. For now I just want to show the direction in which I want to go and the rest of the PRs will follow.

Current problem with existing storage split:

We have storage methods that orchestrate a lot of business logic. For example, if we would now want to add support for another database, the new repository class would need to replicate all the business logic (validation, processing data, orchestrating multiple db operations) within its restartWorkUnitExecutions implementation.

Goal of this and the follow-up PRs:

The end goal of this and few follow up PRs is to refactor the most complex and most business-logic-heavy methods out of the storage and move it to the service/business-logic layer. This will make it easier to provide another database repository implementations and e.g. in case of changes in the restart logic it won't require cascade changes in all the other repositories.

To achieve this, I introduce abstraction over transactions so the service layer can own its boundaries without leaking db details. That allows to move problematic function out of storage and make repository functions thinner and more reusable.

What is not the goal at this moment:

  • doing any unrelated refactors (like improving the existing logic, moving things to the domain layer etc)
  • removing spanner emulator from unit tests - we first need proper separation of layers first

Main decisions being made in this PR:

  • Moving restartworkunitexecutions out of storage into the service layer. For now I've moved it to the restart handler in the ESS as only ESS uses it but I'm open to moving it to a different package if requested.
  • Abstracting a transaction runner so the service layer can own transaction boundaries without leaking db details. This is very similar to the unit-of-work pattern although I stick to the transaction runner name as it seems to be more common in golang world and is easier to understand.
  • For the transaction-bound closure I construct new repository instances with the transaction inside. I was considering injecting it into context or passing it as an explicit parameter but bounding it to specific repository seemed to be cleaner (and more in line with unit-of-work pattern + some libraries I've looked into like GORM also handle it in similar way).

Next steps:

  1. Address the golangci issue on this restart file as a follow up PR. I skipped it here to keep this PR more focused and easier to review.
  2. Refactor the rest of logic-heavy repository methods and move them to the service layer
  3. Provide in-memory mocks once methods are thinner

TAG=agy
CONV=915fe59e-48a0-43ba-b44f-367b95338b58

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the RestartWorkUnitExecutions logic out of the Spanner repository layer and into the server package as a private method on WorkUnitHandler. It introduces a TransactionRunner interface and context-propagated read-write transactions to decouple the storage layer from business logic. Multiple new storage methods are added to handle saving and updating executions, status history, and experiment statuses within these transactions. Feedback is provided regarding a potential nil pointer dereference panic in restartWorkUnitExecutions when calling wu.GetExecutable() if the executable is not configured.

@andrzejfudro
andrzejfudro force-pushed the refactor/restart-workunit-executions branch from 2ddc0b9 to 787cd2f Compare September 23, 2026 08:03
@andrzejfudro andrzejfudro changed the title refactor(storage): separate restart workunit business logic from spanner storage refactor(storage): introduce transaction runner, separate restart workunit business logic from spanner storage Sep 23, 2026
…ner storage

Move RestartWorkUnitExecutions orchestration logic from internal/storage/spanner/workunit
to cmd/experiment_state_server/server to decouple domain orchestration from Spanner.
Introduce TransactionRunner in storage.Storage and granular persistence write methods
requiring an active ambient read-write transaction.

TAG=agy
CONV=915fe59e-48a0-43ba-b44f-367b95338b58
Replace ambient context transaction propagation with explicit repository-scoped Transaction struct (tx.WorkUnits, tx.Experiments, tx.StatusMessages, tx.StatusHistory) constructed via repository WithTx methods.

TAG=agy

CONV=915fe59e-48a0-43ba-b44f-367b95338b58
…mentStatus

Merge SaveExperimentStatus and UpdateExperimentStatus into a single UpdateExperimentStatus method using r.tx, and document ExperimentTxStore removal TODO.

TAG=agy

CONV=915fe59e-48a0-43ba-b44f-367b95338b58
@andrzejfudro
andrzejfudro force-pushed the refactor/restart-workunit-executions branch 2 times, most recently from 90f62a4 to 0a3622b Compare September 23, 2026 10:46
@andrzejfudro
andrzejfudro force-pushed the refactor/restart-workunit-executions branch from 0a3622b to ac9d39d Compare September 23, 2026 11:00
@andrzejfudro
andrzejfudro marked this pull request as ready for review September 23, 2026 11:04
@andrzejfudro andrzejfudro changed the title refactor(storage): introduce transaction runner, separate restart workunit business logic from spanner storage refactor(storage): introduce transaction runner, move restart workunit logic out of the spanner storage Sep 23, 2026

@AntonTyb AntonTyb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

  1. there is a Todo left - is it still valid?
  2. logs? debug/info pleeeeeease.

Comment thread internal/storage/spanner/spanner.go Resolved
Comment thread internal/storage/storage.go Resolved
Comment thread internal/storage/test_utils.go Resolved
- Remove in-place proto defaulting from SaveWorkUnitExecution and UpdateCurrentWorkUnitExecution

- Document TransactionRunner retry invariants

- Convert restartWorkUnitExecutions to a package-level function accepting TransactionRunner

- Guard against nil current executions returning status.ErrInternal

- Clean up unused MockStorage error fields and clone protos in mock queries

- Add TestRestartWorkUnitExecutions_RetryIsIdempotent test
@andrzejfudro
andrzejfudro force-pushed the refactor/restart-workunit-executions branch from aad9208 to b3dcecc Compare September 25, 2026 10:27
…ner transactions

- Add dedicated TxRepository wrappers embedding *Repository and non-nil tx across workunit, experiment, statusmessage, and statushistory

- Hang transactional write methods on *TxRepository to convert runtime r.tx == nil guards into compile-time type guarantees

- Override FetchWorkUnit and FetchExperiment on *TxRepository to ensure transactional reads are served from r.tx

- Remove obsolete runtime precondition checks and their unit tests

- Export TxRepository aliases on spanner package and wire WithTx symmetrically in RunInTransaction
@andrzejfudro
andrzejfudro force-pushed the refactor/restart-workunit-executions branch from b3dcecc to e415db4 Compare September 25, 2026 10:37
@andrzejfudro

Copy link
Copy Markdown
Collaborator Author

@VLukiantsev
I've addressed most of the comments. Some of them that were unrelated to the refactor I deferred or skipped. Commits:

  1. 8790367 - addressing most of the comments apart of TxRepository interface split
  2. 5481e18 - TxRepository interface split

@AntonTyb

  1. I added a few logs to the restart in e415db4. If you meant adding logs in different places let me know.
  2. TODOs are left intentionally - those will be addressed in the follow up PRs

Comment thread internal/storage/spanner/spanner.go Resolved
VLukiantsev
VLukiantsev previously approved these changes Sep 25, 2026

@VLukiantsev VLukiantsev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM — all review threads verified against e415db4 (fixes applied or deferrals agreed as out of Step-1 scope). TxRepository wrapper design looks solid for the follow-up PRs. One optional non-blocking nit left on spanner.go:42.

@AntonTyb AntonTyb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

  1. repeated definition of TxRepository
  2. can you add some docs about the new setup?

…aliases

- Add internal/storage/README.md detailing storage architecture,
  TransactionRunner, *storage.Transaction, Repository vs TxRepository,
  retry guidelines, and hermetic testing.
- Update cmd/experiment_state_server/README.md with high-level storage
  overview and link to internal/storage documentation.
- Drop unused *TxRepository type aliases in internal/storage/spanner/spanner.go.

TAG=agy
CONV=915fe59e-48a0-43ba-b44f-367b95338b58
@andrzejfudro

Copy link
Copy Markdown
Collaborator Author

@AntonTyb

  1. Answered
  2. Added README

@andrzejfudro
andrzejfudro added this pull request to the merge queue Sep 28, 2026
Merged via the queue into main with commit e97d618 Sep 28, 2026
11 checks passed
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.

3 participants