Skip to content

Durable distributed transaction recovery #1378

Description

@roji

#122 tracks the rewrite of Npgsql's System.Transactions support, which will significantly simplify the implementation and fix long-standing bugs. As part of discussions there, the question of recovery has been raised - distributed transactions allow a durable resource manager is recover transactions in case of a crash between prepare and commit. This would be a new feature (there's never been an recovery support in Npgsql), and somewhat orthogonal to the rewrite in #122, so it's being moved here.

Options solutions discussed:

  1. Implement an out-of-process recovery manager, similar to what ODP.NET provides. The advantage is a stable process (probably Windows service) that would always be up. Npgsql would communicate with this service, and the service would maintain a persistent store of pending transactions. The main disadvantage here is an additional component which must be installed.
  2. Implement an in-process recovery manager, like @amirtuval did here (also see Distributed transaction in-process recovery pull request #634). This doesn't require an additional component to install and communicate with. However, In many scenarios it isn't appropriate to wait until the program starts up again, hence the need for a stable, always-up dedicated process.
  3. No recovery support whatsoever - simply implement a volatile resource manager. We're probably going to have to do this anyway for .NET Core, which won't support distributed transactions.
  4. Expose an API, allowing the user to provide a recovery provider. Options 1 and 2 above could be implemented in this way.

As a starting point, implementing 1 via 3 seems to make the most sense - it's a complete solution that also allows the implementation of option 2 if desired.

Activity

  1. added this to the 3.2 milestone on Dec 10, 2016
  2. self-assigned this
    on Dec 10, 2016
  3. omatrot commented on Dec 11, 2016

    @omatrot

    @roji I totally agree with the solutions above. We'll provide the user with a simple and/or more power full support of distributed transaction regarding the context. I think this is great.

  4. pruiz commented on Dec 11, 2016

    @pruiz

    Thinking on npgsql <=> recovery agent protocol, here are my initial thoughts on this:

    1. During transaction creation (or [auto]promotion), NPGSQL connects to recovery-agent and requests a transaction-id for the current connection string (which should be passed to recovery-agent too).
    2. recovery-agent generates a random transaction-id, and persists it to durable storage.
    3. recovery-agent starts polling for transaction-id state using "select pg_prepared_xacts()"
      ** Initially pg_prepared_xacts will not return this (not yet prepared) transaction. Nothing wrong here..

    In case of transaction success (ie. user acutally got to call commit):

    1. npgsql (during transaction's commit) executes 'PREPARE TRANSACTION transaction-id'.
    2. npgsql 'confirms' to recovery-agent the transaction was commited.
      ** Not really needed, as recovery-agent will keep polling PSQL directly for transaction's status. But may help somehow.. heh

    In case of transaction failure (ie. user-initiated or automatic rollback):

    1. npgsql (during transaction's rollback) executes rollback.
    2. npgsql 'notifies removal' of transaction to recovery-agent.
      ** Maybe we should switch order here (first notify recovery-agent, then try to rollback). This way, recovery-agent may rollback later on in case we were not able to actually invoke rollback.

    Recovery-Agents main logic for each 'registered' transaction-id

    1. Polls for transaction-id state using "select pg_prepared_xacts()"
    2. NOT SURE: If transaction has been prepared within a reasonable period, finish it?
      ** If so.. in which cases are we expected to recover by rolling back?

    NOTES:

    A) Regarding connection from recovery-agent to postgresql, I think we will face some (corner) case limitations with some authentication types like SSPI/kerberos/etc. as both processes (the npgsql one, and the recovery-agent) may not be running under the same local user account.
    B) The 'durable storage' where 'recovery-manager' writes it's state/data, may even be the same (or a different) postgresql instance. By passing recovery-agent a connection string of it's own.. Do you guys think this provides valuable advantage over locally storing such data? (maybe monitoring, or availability in case of local machine died).
    C) Instead of re-using credentials from npgsql-process' connectionString, we may choose to let recovery-agent connect to postgres server's using it's own set of credentials (with an independent user defined at db with required permissions). Or even have such 'recovery credentials' be set at npgsql-process' connectionString (so it flows to recovery-agent during transaction-id request)

  5. roji commented on Dec 11, 2016

    @roji
    MemberAuthor

    @pruiz, thanks for these notes. I understood things a bit differently, but I'm really new to the world of distributed transaction so I may be totally wrong.

    The way I understand it, once a transaction is prepared, it is the agent's responsibility to actually send the 2nd-phase command (commit or rollback), not application's. In other words, the agent isn't just a something that does recovery, it actually manages the second phase of the two-phase commit. So the flow would be something like the following:

    • Application creates a transaction (not yet promoted) with Npgsql
    • The transaction is promoted. At this point, Npgsql contacts the agent.
    • The agent creates a DTC transaction, enlists a durable resource manager to it and propagates the transaction back to the client. The client then returns this propagation token from the Promote() method. At this point the local pre-promoted transaction in Npgsql has been "replaced" by the full distributed transaction to which the agent's resource manager is enlisted. This means the agent will be getting notifications to commit/rollback.
    • One problematic point is preparation. It seems that in most cases the agent's resource manager is capable of preparing the transaction, but not in ours - the client (Npgsql) is the one holding the local PostgreSQL transaction, and it's the only capable of preparing. So while the agent's resource manager will be notified to prepare, it must pass that information back to Npgsql, which will actually send the preparation command to PostgreSQL.
    • Note that before requesting preparation from the client, the agent's durable resource manager will log to disk for recovery.
    • Once the client finished preparing, it is no longer concerned with the transaction - it can crash or whatever. The agent will be notified to either commit or roll back. As long as the agent is up, there will be no issues.

    Note that the flow without promotion (i.e. second connection opened, PSPE enlistment fails) is a bit different, but the same in essence.

    I hope what I wrote makes sense. Again, this is my understand as a newcomer to the world of distributed transactions...

  6. pruiz commented on Dec 11, 2016

    @pruiz

    @roji I probably explained myself in wrong wais.. I will try to clarify.

    1. The recovery-agent is the one issuing the final 'commit $transaction-id$' or 'rollback $transaction-id$'.
    2. However, the one who performs the 'prepare transaction' is the npgsql-process itself. And take should be care as which order do this two steps happen (prepare, then monitor tx for completion or in-doubt, then finally commit/rollback), as to avoid letting missing transactions unresolved.
    3. Regarding enlisting the transaction with DTC. This is an area I still have doubts, as the transaction maybe promoted outside of our own (ie. cause there's another resource at this same Tx.Scope, forcing transaction promotion). But yes, at some point the agent is the one receiving the signalling of commit/rollback from DTC. But this commit/rollback are regarding the whole distributed transaction, not just our (npgsql) resource's transaction.
    4. Regarding preparing, I think this should be done from npgsql-side. This is, if a transaction has been promoted, 'commiting' it (from npgsql-process point of view) becomes 'preparing it'. As recovery-agent will take charge of final (an real' commite later on.
  7. pruiz commented on Dec 11, 2016

    @pruiz

    Also, due to performance reasons, IMHO we should try to avoid (or defer) involving recovery-manager (and DTC) as much as possible. As simple transactions non-involving more than one resource do not benefit from recovery-agent/DTC, and contacting them will probably be an "expensive" operation.

  8. roji commented on Dec 12, 2016

    @roji
    MemberAuthor

    @pruiz I think we're going in the same direction... some comments:

    Regarding preparing, I think this should be done from npgsql-side

    Technically, there's definitely no other way. Preparing (i.e. sending PREPARE TRANSACTION) can only be done on the connection where the unpromoted (regular, classic) transaction has started, and that's the application with Npgsql.

    And as you say, care must be taken here. The agent must log the transaction to persistent storage before Npgsql prepares. Otherwise there's a chance the Npgsql process will disappear, leaving the prepared transaction in PostgreSQL without the agent knowing anything about it. So first the agent needs to log, then Npgsql can prepare.

    Also, due to performance reasons, IMHO we should try to avoid (or defer) involving recovery-manager (and DTC) as much as possible. As simple transactions non-involving more than one resource do not benefit from recovery-agent/DTC, and contacting them will probably be an "expensive" operation.

    Yes, that's the whole point behind the "promotable single phase enlistment optimization" (described here). As long as you enlist with a PSPE, System.Transactions will do its best not to promote unless it becomes necessary (i.e. another durable resource manager enlists).

    Regarding preparing, I think this should be done from npgsql-side. This is, if a transaction has been promoted, 'commiting' it (from npgsql-process point of view) becomes 'preparing it'. As recovery-agent will take charge of final (an real' commite later on.

    I think it's important to clearly distinguish between promotion and preparation... Promotion is relevant only to PSPE enlistments - when you start off with a single Npgsql connection and then add something else. It doesn't mean imply preparation, which happens at a later phase. Even if we enlist as a PSPE, it's entirely possible that we skip promotion and go directly to a durable resource manager, e.g. when the Npgsql connection isn't the first to enlist.

    Anyway, as I said we seem to have the same ideas here...

  9. changed the title [-]Distributed transaction recovery[/-] [+]Durable, out-of-process distributed transaction recovery[/+] on Dec 12, 2016
  10. 7 remaining items

  11. changed the title [-]Durable, out-of-process distributed transaction recovery[/-] [+]Durable distributed transaction recovery[/+] on Jun 17, 2017
  12. modified the milestones: 4.0, on Mar 14, 2018
  13. roji commented on Aug 26, 2019

    @roji
    MemberAuthor

    Although I've been wanting to implement this for a while, it simply seems less and less relevant as distributed transactions are a .NET Framework-only feature. It would help to receive feedback from the community on the desirability of this feature.

  14. modified the milestones: 4.1, Backlog on Aug 26, 2019
  15. schmitch commented on Aug 26, 2019

    @schmitch

    actually TransactionScope is not .net framework only (https://docs.microsoft.com/en-us/dotnet/api/system.transactions.transactionscope?view=netcore-2.0 and https://docs.microsoft.com/de-de/ef/core/saving/transactions#using-systemtransactions) and as far as I know it's impossible to get a NpgsqlTransaction from TransactionScope. thus you need to have some kind of "distributed" transaction even on a single node for some cases.
    Consider using Hangfire where you want to "create" a new task and also put something in your own database. since you can't directly access the Hangfire connection you need to co-ordinate two NpgsqlConnections.
    Well it's only a minor use case.

  16. roji commented on Aug 26, 2019

    @roji
    MemberAuthor

    TransactionScope is indeed supported on .NET Core, but distributed transactions are not - the two are very different things.

    as far as I know it's impossible to get a NpgsqlTransaction from TransactionScope. thus you need to have some kind of "distributed" transaction even on a single node for some cases.

    I don't really understand this, why would someone need to get an NpgsqlTransaction from a TransactionScope? I also don't understand the Hangfire scenario you describe - aren't there two databases involved there?

    To be extra clear, I'm not saying that distributed transactions don't have their uses - just that they're not supported in .NET Core. The moment you have two different connections, and both enlist in the same TransactionScope, this will fail. This has nothing to do with Npgsql - there's simply no cross-platform distributed transaction manager, and so distributed transactions do not work.

  17. YohDeadfall commented on Aug 26, 2019

    @YohDeadfall
    Contributor

    @roji There is agood example when TransactionScope is needed. For example, you have two repositories and you should commit changes done by them in a service. How would you do this without a scope if you're not using EF? There is no distributed transactions at all, but only two repositories with unknown implementation.

  18. roji commented on Aug 26, 2019

    @roji
    MemberAuthor

    @YohDeadfall I'm not sure I understand... You can have two TransactionScopes, one for each database, and commit to each one of them separately (or one NpgsqlTransaction for each database, it's the same). This indeed has nothing to do with distributed transactions, and should work just fine.
    This can be done simply by manually enlisting each connection in a specific TransactionScope instance, instead of relying on implicit, ambient enlistment. I don't really see what EF has to do with any of this...

    In any case, this issue really is only about implementing a durable resource manager (so recovery), as opposed to the current volatile resource manager that Npgsql currently implements. This is only related to distributed transactions, and not to the use of multiple TransactionScopes.

    Or am I missing something?

  19. roji commented on Oct 10, 2020

    @roji
    MemberAuthor

    For anyone looking at this, we're not going to do any work on distributed transactions since they aren't supported in .NET Core (dotnet/runtime#715).

  20. added theissue type on Jun 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions