RFC - Store inflight state in-memory and flush to sqlite periodically #487
+1,328
−413
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently all state changes are made in sqlite, and because of the way taskbroker's logic works out the application is almost entirely write operations. With sqlite only having a single write lock on the database, we often see both gRPC, upkeep and consumer latency increases at the same time as contention piles up in sqlite.
These changes move much of the activation state-machine into a set of in-memory heaps/sets that are wrapped with a mutex. This allows gRPC operations to become detached from SQLite writes which should reduce contention on the write lock. As activations are mutated by grpc, and upkeep, modified records are added to the
dirty_ids
set, and periodically flushed to SQLite during ingest and upkeep.These changes mean that inflight state is no-longer fully durable. Instead, state changes can be lost between
commit
calls. This could lead to tasks being executed multiple times, but shouldn't result in tasks being lost or dropped. We already have the opportunity for duplicate execution (through processing deadlines), and we'd be expanding the scope of that problem but not creating new durability or data-loss scenarios (that I'm aware of).I've also separated the 'blob storage' and 'metadata storage' into separate tables. We have tried this in #369 and didn't move forward then as we weren't able to see noticeable improvements. My hope is that by separating the tables again, and removing write traffic we can reduce fragmentation in the database as rows containing activation blobs will not be mutated anymore. Splitting storage in Sqlite is also step towards storing large activations on the filesystem (which is also on our future plans).
Next steps
I'd like to get this onto sandboxes and validate:
If this prototype succeeds, I'll put together a more complete plan on how we could incrementally and safely ship these changes.