Skip to content

Bunch of fixes on the distributed transaction management. - #781

Closed
omatrot wants to merge 1 commit into
npgsql:developfrom
omatrot:develop
Closed

omatrot wants to merge 1 commit into
npgsql:developfrom
omatrot:develop

Conversation

@omatrot

@omatrot omatrot commented Sep 13, 2015

Copy link
Copy Markdown

The multithreaded asynchronous nature of the transaction manager (MSDTC) makes it hard to deal with connections, timeouts and backend faults. All these fixes are the result of more than 2 years of debugging the provider running on medium to high traffic WCF Service based real world applications.
Note that it is nearly impossible to validate these by writing tests because they spans scenario ranging from invalid consumer code to poorly performing queries and backends.
They all have been fixed by the help of logs added in the provider to really understand what was going on.
Unfortunately, you can't mimmic production environment.

The multithreaded asynchronous nature of the transaction manager (MSDTC) makes it hard to deal with connections, timeouts and backend faults. All these fixes are the result of more than 2 years of debugging the provider running on medium to high traffic WCF Service based real world applications.
Note that it is nearly impossible to validate these by writing tests because they spans scenario ranging from invalid consumer code to poorly performing queries and backends.
They all have been fixed by the help of extending logging added in the provider to really understand what was going on.
Unfortunately, you can't mimmic production environment.
@roji roji added this to the 3.1 milestone Sep 13, 2015
@roji

roji commented Sep 13, 2015

Copy link
Copy Markdown
Member

Thanks for this patch @omatrot! Unfortunately I'm currently busy with other things (EF7, .NET Core support, and after that connection pool redo). A serious look and probably a redo is definitely something I want to do for 3.1 (or maybe 3.2), so I'll get around to it.

BTW, you can also take a look at #513 and #634 to see other PRs on distributed transactions.

@omatrot

omatrot commented Sep 13, 2015

Copy link
Copy Markdown
Author

No problem, it will allow me to push other fixes I've done in the resource manager and in other places as well. The goal for me was to have a rock solid transaction aware provider. Plenty of resources for you alongside other contributions to do a rewrite.

@roji
roji force-pushed the develop branch 4 times, most recently from 2321a43 to d1fbc1e Compare April 19, 2016 08:03
@roji roji modified the milestones: 3.2, 3.1 Apr 20, 2016
@roji
roji force-pushed the develop branch 10 times, most recently from beae0a3 to 968113d Compare May 10, 2016 16:14
@roji roji closed this May 12, 2016
@roji

roji commented May 12, 2016

Copy link
Copy Markdown
Member

@omatrot this PR has been closed because the develop branch has been renamed to dev.

All distributed transaction work (for 3.2) is tracked by #122.

@roji

roji commented Dec 11, 2016 •

Copy link
Copy Markdown
Member

@omatrot, I've had a close look at the issue of TransactionScope timeout triggering a rollback at random times, and your attempt to handle this. I think there are several issues, which to be honest aren't really solvable.

  • When RollbackTransaction is called, you check if the connector's state to see if it's currently busy, and proceed to call cancel if so. There's a race condition here where a connector may become busy right after your check.
  • Cancel works asynchronously and on a best-effort basis. In other words, after Cancel returns you have no guarantee that the currently-running command actually completed. This means it's still possible (even likely) to run get the "connection already use error" when you try to send the rollback

To be honest, I find the idea of a rollback-on-timeout pretty bad. Rollback is something that should be taken care of by the application itself, when it knows in what state things are, rather than at some random point from a timer. Since user threads may be performing arbitrary database operations when the timeout triggers, it's going to be very difficult (read impossible) to provide a 100% safe and predictable solution.

Here's what I did in my PR (#1380), it's similar to what you did, except that it simply attempts to send a rollback in a loop, catching NpgsqlOperationInProgressException exceptions to detect if the connection is in use, sending a cancel and sleeping for a bit.

@omatrot

omatrot commented Dec 11, 2016

Copy link
Copy Markdown
Author

@roji The connector is tied to the current distributed transaction so I think there is little chance that it becomes busy after the check. I may be wrong but the code you're talking about is only involved during a distributed transaction. I may have pushed code in a wrong place, I'm not used to Git. And I'm pretty sure it is incomplete because I did it again on the latest 3.1.9 source code.

Can I send you a zip file will al the latest code I'm using ?
May be there is simple way to create new branch from existing files ?

I faced another issue lately when a distributed transaction is commited without being first prepared. In the current implementation the connector is leaked.

Anyway I think your solution will work fine, provided you exit your loop after a max number of tentative.

@roji

roji commented Dec 11, 2016

Copy link
Copy Markdown
Member

@omatrot if you'd like, you can push your latest changes on 3.1.9 to some branch and simply post the link there - I think it'll be easier than exchanging ZIPs (although we can do that too).

Regardless, any comment you have on my PR would be greatly appreciated, I need feedback for people with actual experience with distributed transaction :)

At the moment, I didn't place a limit on the number of loops - that may be a good idea. I have a hard time imagining a scenario where we would loop forever: we can cancelling and reattempting to rollback. At some point it has to work, although to be extra safe you may be right.

@omatrot

omatrot commented Dec 11, 2016

Copy link
Copy Markdown
Author

You probably know that nothing can replace production. Everything will fail at some point for various reasons. Poorly managed virtualized environments are still giving me a hard time with PostgreSQL/Npgsql.
I'll have to compare the code. I have lot of comments in my current version that explains why I made the changes I made. Stay tuned.

@roji

roji commented Dec 11, 2016

Copy link
Copy Markdown
Member

OK. I'm especially interested in anything missing (or wrong) in #1380.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants