Skip to content

DataShard: in-memory state migration on graceful restart #11561

Open

Description

@snaury

DataShards have some in-memory state (e.g. optimistic locks, inflight volatile transactions) that is not preserved on restarts. On the one hand it helped us with certain bugs in said in-memory state, with restarts healing stuck databases. On the other hand frequent tablet rebalancing causes pain for users due to TLI errors (as well as UNDETERMINED errors with volatile transactions in the future). We need to migrate as much in-memory state as possible so shards just continue where they left-off on a new node.

There's a custom state migration implemented in Coordinator tablets right now. One idea is to make some generic mechanism as part of LocalDB and then use it in DataShards. However state migration from an inaccessible node would cause latency spikes, and custom migration could be combined with the current mediator state restore running in parallel. Custom migration could also be easier to customise. However when state migration is part of LocalDB it could also be used to track which commits succeeded (even when log writes didn't due to blocked storage races). The latter is a long shot however.

The rough plan is as follows:

  • RFC: datashard_state_migration.md
  • Make locks part of some state that is streamed and restored on restart
  • Make prepared volatile transactions part of some state that is streamed and restored on restart
  • Make volatile variables (e.g. UnprotectedReadEdge) part of the state, which we could trust instead of worst-case mediator state
  • Make it possible to stream waiting (almost committed) volatile transactions on restart, with pending reply handoff
  • Make it possible to reattach to volatile transactions on restart (to reduce UNDETERMINED errors)

Activity

  1. self-assigned this
    on Nov 13, 2024
  2. snaury commented on Aug 26, 2025

    @snaury
    MemberAuthor

    Regarding volatile transactions there are actually two aspects:

    1. Volatile transactions which have been prepared, but not executed yet. Currently we just safely abort such transactions. Ideally we would like to transfer them to new instances, so they could finish planning and executing. This is only safe to do as long as there have been no side effects regarding these transactions (e.g. no storage changes, and no communication). Basically we need to serialize an in-memory hash table in a way that may be restored by the newer generation. This should use existing TEvProposeTransactionRestart and TEvProposeTransactionAttach events so executor could reattach to the transaction while it finishes planning.
    2. Volatile transactions which have been executed, but which are waiting for commits and/or other confimations. These are very tricky, since we don't even finish filling the reply event until the persistence transaction commits, and we cannot transfer a non-existent reply to a newer generation (especially since the reply is also transformed on failures). It may actually be easier to delay shutdown until a newer generation confirm the transaction status and then send the reply from the original generation. Client (executor) won't even get a delivery failure in that case, and wouldn't need to reattach.

    Executed transactions need to have a way of ensuring it doesn't hang indefinitely, and doesn't leave a large number of instances around when shard is restarting very often. There are the following aspects:

    1. We may start persisting the transaction, but lose the commit status due to blob storage blocking. Unless we find a way to precisely track which commits ended up included in the commit graph we just cannot safely wait or even know whether the transaction committed or aborted. We may only safely wait for transaction status when we know it has been persistent.
    2. There may be multiple restarts until the transaction commits or aborts, and we need to handle that somehow. One idea is to persist the original datashard actor id and notify it when newer generations commit or abort. Additionally we would need a way to ensure the transaction exists.
    3. Client (executor) may give up before the wait finishes. Ideally we should stop waiting in that case too, so we actually need to track request pipes.
    4. We may have commits or aborts that start before or during the shutdown. While commits are supposed to be repeatable (once we start committing all newer generations are supposed to commit), aborts are not. We may start aborting locally, but newer generation may actually commit, and we need to track that correctly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions