Skip to content

Rewrite of System.Transactions support - #1380

Merged
roji merged 11 commits into
npgsql:devfrom
roji:sys-tx-rewrite
Dec 12, 2016
Merged

roji merged 11 commits into
npgsql:devfrom
roji:sys-tx-rewrite

Conversation

@roji

@roji roji commented Dec 11, 2016

Copy link
Copy Markdown
Member
  • General rewrite/refactor, everything is much simpler now.
  • Although support for enlisting as durable/PSPE is present, it is currently commented out since it makes no sense without recovery (Durable distributed transaction recovery #1378).
  • Durable/PSPE support has been rewritten to use .NET 4.5.2's PromoteAndEnlistDurable to promote, this removes the need for the separate AppDomain which was complicating things.
  • Considerably simplified the scenario where a connection is closed before its transaction completes. Before we put the connection in a "fake closed" mode, we now properly close the connection and return the connector to a special list on the pool. The list is checked when opening a new connection with the same transaction.

Based on work and discussions with @omatrot, @amirtuval, @skikiker

Closes #122

* General rewrite/refactor, everything is much simpler now.
* Although support for enlisting as durable/PSPE is present, it is
  currently commented out since it makes no sense without recovery
  (npgsql#1378).
* Durable/PSPE support has been rewritten to use .NET 4.5.2's
  PromoteAndEnlistDurable to promote, this removes the
  need for the separate AppDomain which was complicating things.
* Considerably simplified the scenario where a connection is closed before
  its transaction completes. Before we put the connection in a "fake
  closed" mode, we now properly close the connection and return the
  connector to a special list on the pool. The list is checked when
  opening a new connection with the same transaction.

Based on work and discussions with @omatrot, @amirtuval, @skikiker

Closes npgsql#122
@omatrot

omatrot commented Dec 11, 2016

Copy link
Copy Markdown

@roji
A first wave of comments based on a quick review of NpgsqlResourceManager.cs (Two-Phase Operations):

  • That may sounds crazy, but Commit may be called without a previous call to Prepare. This case should be detected and a simple COMMIT command should be sent to the backend. In the previous implementation this also affected the way the connection is released with the postponing close logic. This is why I suggest that the state of the connection pool is logged each time a connector is used/released. This helped me fixing this leak.

I strongly suggest that the transaction name used in PREPARE and COMMIT/ROLLBACK PREPARED is composed of a GUID and the connector backend process id. This allows multiple connections to be enlisted a the same distributed transaction. This is one of my use case.

The code is indeed much more simple than before.

I will try your code tomorrow at work on my applications to see what its like.

@roji

roji commented Dec 11, 2016

Copy link
Copy Markdown
Member Author

A first wave of comments based on a quick review of NpgsqlResourceManager.cs (Two-Phase Operations):

Many thanks for spending time on this and sending your comments!

That may sounds crazy, but Commit may be called without a previous call to Prepare. This case should be detected and a simple COMMIT command should be sent to the backend. In the previous implementation this also affected the way the connection is released with the postponing close logic. This is why I suggest that the state of the connection pool is logged each time a connector is used/released. This helped me fixing this leak.

Can you provide more information on this scenario? As I understand it, it's possible that the system will decide to do single phase commit instead of two-phase commit, but in that case SinglePhaseCommit is supposed to be called instead of Commit... This is also why the resource manager implements ISinglePhaseNotification rather than IEnlistmentNotification.

Any info on when your scenario occurs, maybe even a test would help understand...

I strongly suggest that the transaction name used in PREPARE and COMMIT/ROLLBACK PREPARED is composed of a GUID and the connector backend process id. This allows multiple connections to be enlisted a the same distributed transaction. This is one of my use case.

But when multiple connections enlist in the same distributed transaction, each (currently) generates their own GUID - the transaction name is connection-specific and not shared across connections...

Do you mean that you use the distributed transaction GUID (i.e. Transaction.TransactionInformation.DistributedIdentifier) as the PostgreSQL prepared transaction name, in order to help debugging (and in that case appending the process ID is necessary)? If so, that's a pretty good idea...

From a random GUID to the distributed transaction's GUID + backend process
id.
@roji

roji commented Dec 11, 2016

Copy link
Copy Markdown
Member Author

FYI pushed a commit to change the transaction name.

@omatrot

omatrot commented Dec 11, 2016

Copy link
Copy Markdown

About this weird commit without bein prepared, I'm still in the dark right now. The transaction involves other resource managers. It is distributed because I have a valid distributed Guid. This is a single phase commit in the 2 phase code. Strange. The single phase code is never called. Other resources managers are volatile... May be this is why. I need to further investigate,and see the behavior with your code.

Yes, I'm using the Distributed Transaction Guid and the connector pid as the PostgreSQL transaction name. This helps track back an orphaned prepared connection in the application logs where this Guid is also present. It already allowed me to pinpoint an issue on a specific client in my n tier architecture. My customer was quickly reassured.

@roji

roji commented Dec 11, 2016

Copy link
Copy Markdown
Member Author

About this weird commit without bein prepared, I'm still in the dark right now. The transaction involves other resource managers. It is distributed because I have a valid distributed Guid. This is a single phase commit in the 2 phase code. Strange. The single phase code is never called. Other resources managers are volatile... May be this is why. I need to further investigate,and see the behavior with your code.

OK... let me know what you find out about that. I know that in some cases the systems opts for a single-phase commit, but then the appropriate method should be called. The documentation for IEnlistmentNotification.Commit starts with the following sentence: The transaction manager calls this method of an enlisted object during the second phase of a commitment, when it instructs all participants to commit the transaction.. This seems to unambiguously say that Prepare() has to be called first...

Who knows, maybe this is some bug in the current (pre-rewrite) implementation that caused the wrong method to be called? With all the extra codepaths it could be that...

Yes, I'm using the Distributed Transaction Guid and the connector pid as the PostgreSQL transaction name. This helps track back an orphaned prepared connection in the application logs where this Guid is also present. It already allowed me to pinpoint an issue on a specific client in my n tier architecture. My customer was quickly reassured.

That makes a lot of sense. I made that change and pushed it to this PR.

roji added 5 commits December 11, 2016 18:03
Mono is missing this method, which was added to Transaction in .NET 4.5.2.
Have submitted a PR to add it. In any case we're not calling it until
recovery is implemented.
@omatrot

omatrot commented Dec 12, 2016

Copy link
Copy Markdown

@roji I've recompile the provider from your sys-tx-rewrite branch, changed the version number and dropped it in place of my current 3.1.9 customized version in one of my application.

The behavior right now is that there seems to be no enlistment at all. This is difficult for me to say until I have have more logs available.

@roji

roji commented Dec 12, 2016

Copy link
Copy Markdown
Member Author

@omatrot thanks for testing, yes, I found and corrected this bug about an hour ago in commit 7d3fc4d. Try to update to the latest version of this PR.

@roji

roji commented Dec 12, 2016

Copy link
Copy Markdown
Member Author

FYI I just pushed a cleanup that leaves only volatile enlistment for now.

PSPE/Durable will be implemented in the future with recovery.
@omatrot

omatrot commented Dec 12, 2016

Copy link
Copy Markdown

This is much better ! Following is the Npgsql log produced within my application.
The good news is that this weird behavior of pseudo single phase commit I was taking about is gone.
We have a two phase commit as expected.

2016-12-12 11:07:35,207 [28] [(null)] Npgsql.NpgsqlConnection DEBUG Opening connnection
2016-12-12 11:07:35,207 [28] [(null)] Npgsql.NpgsqlConnector DEBUG Start user action
2016-12-12 11:07:35,207 [28] [(null)] Npgsql.NpgsqlConnection DEBUG Beginning transaction with isolation level Serializable on connector [5328]
2016-12-12 11:07:35,207 [28] [(null)] Npgsql.NpgsqlConnector DEBUG End user action
2016-12-12 11:07:35,207 [28] [(null)] Npgsql.NpgsqlConnection DEBUG Enlisted resource manager as volatile (local txid=d182e793-32d2-44e4-9234-4ec5416d2cd0:1) on connector [5328]
2016-12-12 11:07:35,223 [28] [(null)] Npgsql.NpgsqlConnector DEBUG Start user action
2016-12-12 11:07:35,223 [28] [(null)] Npgsql.NpgsqlCommand DEBUG ExecuteReader
2016-12-12 11:07:35,223 [28] [(null)] Npgsql.NpgsqlCommand DEBUG Executing statement(s):
2016-12-12 11:07:35,301 [20] [(null)] Npgsql.NpgsqlDataReader DEBUG Cleaning up reader
2016-12-12 11:07:35,301 [20] [(null)] Npgsql.NpgsqlConnector DEBUG End user action
2016-12-12 11:07:35,301 [20] [(null)] Npgsql.NpgsqlConnection DEBUG Closing connection
2016-12-12 11:07:35,301 [20] [(null)] Npgsql.NpgsqlConnection DEBUG Connection closed on connector [5328]
2016-12-12 11:07:35,707 [14] [(null)] Npgsql.NpgsqlResourceManager DEBUG Two-phase transaction prepare (localid=d182e793-32d2-44e4-9234-4ec5416d2cd0:1, distributed txid=ddfd4909-68c7-4cab-bde2-37ef2f7b44af on connector [5328]
2016-12-12 11:07:35,723 [13] [(null)] Npgsql.NpgsqlResourceManager DEBUG Two-phase transaction commit (localid=d182e793-32d2-44e4-9234-4ec5416d2cd0:1) on connector [5328]
2016-12-12 11:07:35,723 [13] [(null)] Npgsql.NpgsqlResourceManager DEBUG Cleaning up RM (localid=d182e793-32d2-44e4-9234-4ec5416d2cd0:1) on connector [5328]

I'll let this running as is for the day and report any problem.
Thanks for the good work.

@omatrot

omatrot commented Dec 12, 2016

Copy link
Copy Markdown

Whoopsy doopsy there is a connection leak...

2016-12-12 11:20:17,194 [132] [(null)] WebAPI.Utils.Log4NetExceptionLogger ERROR Npgsql.NpgsqlException (0x80004005): The connection pool has been exhausted, either raise MaxPoolSize (currently 10) or Timeout (currently 15 seconds)

@roji

roji commented Dec 12, 2016

Copy link
Copy Markdown
Member Author

Whoopsy doopsy there is a connection leak...

Uhh, can you share a code sample?

@omatrot

omatrot commented Dec 12, 2016 •

Copy link
Copy Markdown

I would say that the connector is not returned to the available pool when the 2PC ends. I'll try to prepare a sample to reproduce the problem,but not until the end of my work day now. In the meantime, I think you could raise the log level with the connector pool.

@roji

roji commented Dec 12, 2016

Copy link
Copy Markdown
Member Author

@omatrot I found the bug and fixed it, and added a test for a leak in that particular situation. Thanks for letting me know. Would be great if you could test again with the latest version and let me know.

@omatrot

omatrot commented Dec 12, 2016

Copy link
Copy Markdown

No more leak with the latest version.

@roji

roji commented Dec 12, 2016

Copy link
Copy Markdown
Member Author

OK great!

I think I'll merge this PR soon to be able to work on other things - we can always add more fixes later. Anything else you'd like to test/play around with/suggest before I do?

@omatrot

omatrot commented Dec 12, 2016

Copy link
Copy Markdown

I'll have to modify my EF code to try not to wait for the transaction to complete and dispose the context as soon as I have voted for transaction completion.
I'm also managing my own context/cnx dictionnary to be able to reuse the same connection between service calls. I should remove it to test the driver own logic.
I would like to see the transaction related statements dumped in the logs too.

But as you say, you probably can go with the merge.

@roji

roji commented Dec 12, 2016

Copy link
Copy Markdown
Member Author

I'll have to modify my EF code to try not to wait for the transaction to complete and dispose the context as soon as I have voted for transaction completion.

I don't understand that one... Does the new implementation trigger some sort of behavior change that impacts you? Can you provide more details?

I'm also managing my own context/cnx dictionnary to be able to reuse the same connection between service calls. I should remove it to test the driver own logic.

That doesn't seem like something directly related to System.Transactions etc., but more to your EF (or high level) usage, right? Let me know if there's anything that concerns Npgsql directly.

I would like to see the transaction related statements dumped in the logs too.

Are you referring to PREPARE STATEMENT, ROLLBACK PREPARED, etc? If so, I could add a logging trace ExecuteInternalCommand...

But as you say, you probably can go with the merge.

OK, I'll do that now. Feel free to continue commenting here or opening new issues.

@omatrot

omatrot commented Dec 12, 2016 •

Copy link
Copy Markdown

I don't understand that one... Does the new implementation trigger some sort of behavior change that impacts you? Can you provide more details?

I am referring to this comment on issue #122 and this other comment on #1208.
As I understand the new implementation, this can't happen any more. My code is working fine with the new driver, I just want to remove the need to wait on an event.

That doesn't seem like something directly related to System.Transactions etc., but more to your EF (or high level) usage, right? Let me know if there's anything that concerns Npgsql directly.

Again, as I understand the new implementation, there is a built-in mechanism in Npgsql that automatically reuse of a connection already enlisted in a distributed transaction if the tx distributed id is the same. I could test how it behaves by removing the similar code I have in the consumer. Unless I'm totaly wrong on that one.

Are you referring to PREPARE STATEMENT, ROLLBACK PREPARED, etc? If so, I could add a logging trace ExecuteInternalCommand...

Yes exactly.

@roji

roji commented Dec 12, 2016 •

Copy link
Copy Markdown
Member Author

I don't understand that one... Does the new implementation trigger some sort of behavior change that impacts you? Can you provide more details?

I am referring to this comment on issue #122 and this other comment on #1208.
As I understand the new implementation, this can't happen any more. My code is working fine with the new driver, I just want to remove the need to wait on an event.

I think I understand. Yes, it should now be totally safe to close a connection even though its transaction hasn't been committed/rolled back yet. Npgsql will even reuse that connection if you happen to open a connection within the same transaction - so as long as you don't open two connections at the same time you'll be reusing the same connector behind the scenes, and won't escalate to a distributed transaction. Of course I'd be very happy to get some real-world confirmation that the new implementation works well :)

Are you referring to PREPARE STATEMENT, ROLLBACK PREPARED, etc? If so, I could add a logging trace ExecuteInternalCommand...

Yes exactly.

Have just pushed this commit, which will log a message on all internal commands, which includes commit/rollbacks. You have to have level trace for it though.

@omatrot

omatrot commented Dec 13, 2016

Copy link
Copy Markdown

@roji

I think I understand. Yes, it should now be totally safe to close a connection even though its transaction hasn't been committed/rolled back yet. Npgsql will even reuse that connection if you happen to open a connection within the same transaction - so as long as you don't open two connections at the same time you'll be reusing the same connector behind the scenes, and won't escalate to a distributed transaction. Of course I'd be very happy to get some real-world confirmation that the new implementation works well :)

I am now detached from the TransactionCompletedEvent with the new driver and I can confirm that this is working well. I have a few scenarios where multipe connections are enlisted in the same distributed transaction though. So I think this will be unsupported right now.

@roji

roji commented Dec 13, 2016

Copy link
Copy Markdown
Member Author

I am now detached from the TransactionCompletedEvent with the new driver and I can confirm that this is working well. I have a few scenarios where multipe connections are enlisted in the same distributed transaction though. So I think this will be unsupported right now.

OK, good to hear and thanks for testing. But I didn't understand that last comment - why should multiple connections enlisting in the same distributed transaction not be supported?

@roji
roji deleted the sys-tx-rewrite branch December 13, 2016 14:03
@omatrot

omatrot commented Dec 13, 2016

Copy link
Copy Markdown

This was about the following statement:

Npgsql will even reuse that connection if you happen to open a connection within the same transaction - so as long as you don't open two connections at the same time you'll be reusing the same connector behind the scenes, and won't escalate to a distributed transaction

I have such a use case where 2 connections to the same database are opened during the same distributed transaction, hence the transaction name using the Tx Dist. Id and the connector Id.

@roji

roji commented Dec 13, 2016

Copy link
Copy Markdown
Member Author

Npgsql will even reuse that connection if you happen to open a connection within the same transaction - so as long as you don't open two connections at the same time you'll be reusing the same connector behind the scenes, and won't escalate to a distributed transaction

I have such a use case where 2 connections to the same database are opened during the same distributed transaction, hence the transaction name using the Tx Dist. Id and the connector Id.

OK, although there's nothing unsupported about it... As long as you don't have the two connections open simultaneously, Npgsql will reuse the same connector behind the scenes. If you do have them open at the same time, at the moment it won't escalate either, since we're using volatile enlistments (I think escalation only happens for two durable enlistments).

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.

Fully fix ambient and distributed transactions

2 participants