Skip to content

Adding more tests to system transaction, demonstrating #1571 - #1583

Merged
roji merged 3 commits into
npgsql:devfrom
fredericDelaporte:dtc
Jun 13, 2017
Merged

roji merged 3 commits into
npgsql:devfrom
fredericDelaporte:dtc

Conversation

@fredericDelaporte

Copy link
Copy Markdown
Contributor

This PR add some more tests to system transaction tests. It also has a test demonstrating #1571.

@fredericDelaporte fredericDelaporte left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intend to add more tests, but I already submit this for checking if my way of doing things suits you.

So currently this is not for being merged, but rather for some feedback as you see fit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added messages in a bunch of cases where the Assert failure could be ambiguous with another assert in the same test.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, no problem. I'd prefer the message to be on the same line though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For actually testing Enlist=false, checking a rollback does not prevent data to be changed seems to me the best way.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that checking that data is visible via another connection (conn2) before the scope is completed should be good enough as a check - your way seems to be just as good to me. I don't really have anything against doing both.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the Enlist=false does not work and enlist connections, both would be in same transaction and thus should see the same data. Conn2 would then read the uncommitted data from conn1. So it would not ascertain they are not enlisted.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like Enlist=false but the other way round, checking the rollback case with Enlist=true seems to me the best way to ascertain the enlistment.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This usage pattern is common with NHibernate, when a single transaction scope span many NHibernate session consecutively used. If in such case there is no transaction promotion to distributed, this is better.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, this is a common usage pattern regardless of NHibernate. I actually worked on 3.2 to make sure escalation doesn't occur in this scenario and was convinced I had this test already, but there was only ReuseConnection which is close but not exactly that. Thanks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause a rollback from the prepare phase of the enlisted test ressource.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. To me this test seems very similar to TwoConnectionsRollback - except in that the enlistment triggers the rollback (via ForceRollback()) and not the user (by not calling TransactionScope.Complete()). I'm not sure if there's supposed to be a different, but in any case it makes sense having this scenario as well.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the test demonstrating the race condition. I was not able of demonstrating it with the simpler TwoConnections test. It looks like enlisting with a durable ressource is required for triggering it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh. I'll try to find the time in the next few days to play around with this myself. If there really is a behavioral difference between volatile/durable on this I will have learned something new.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is for checking if supporting a null enlistment could be required for dodging some race condition. I should probably add a variation with a durable ressource enlisted, and some loops.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. In its current form this test doesn't seem very useful.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test was looking like a copy paste of the previous one, renamed but not changed. Changes done now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops... thanks for catching that and fixing!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Locally I have tried using that for adding a AssertHasPreparedTransactions in most distributed tests, but that assert was failing everywhere, finding nothing prepared.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that you were seeing 0 transactions in pg_prepared_xacts although you were expecting one to be there? Do you consider this as evidence of the race condition you were mentioning before (or another race condition)?

@fredericDelaporte fredericDelaporte May 30, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that you were seeing 0 transactions in pg_prepared_xacts although you were expecting one to be there?

Yes.

Do you consider this as evidence of the race condition you were mentioning before (or another race condition)?

No.

But maybe as an evidence that there is more differences between durable and volatile ressources. Enlisting many volatile ressources does not mandate promotion to distributed, provided there is no more than one durable ressource in the transaction.

As stated previously, a resource manager would make a volatile enlistment if it manages an in-memory, volatile resource. One of the benefits of using EnlistVolatile is that it does not force an unnecessary escalation of the transaction.

As long as the System.Transactions infrastructure handles volatile resources and at most one durable resource that supports single-phase notifications, the transaction remains in the ownership of the System.Transactions infrastructure.

The durable ressource used in the race test does not support single-phase, and with this one I see pg_prepared count raising. But for all the other tests, count stays at 0.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I now understand better how durable vs. volatile could induce this kind of behavior change (and possibly create race conditions)... The MSDTC may introduce some asynchronicity which doesn't exist when using only System.Transactions (i.e. volatile-only). The question then would be how other provider implementations deal with this kind of thing, if at all.

@fredericDelaporte fredericDelaporte May 30, 2017 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, on NHibernate side, all our concurrency issues occur only when the transaction is truly distributed (involving delegation to the external MsDtc process).

With NHibernate we do not have any "transaction commit delayed after scope disposal" issue with Sql-Server, be it through SqlConnection or OdbcConnection. But we have that with Oracle (managed driver, the others are not yet tested) and SQLite, in addition to PostgreSql. So I think Microsoft knows what should be done but has not documented it properly, causing some other vendors to have those kind of issues. My best hope is still using a durable ressource manager instead of a volatile one. But as I have already written, I have no evidence that it is either enough or necessary.

#1580 is related to those issues: with sql-server in some circumstances re-using a connection after a transaction fails because it has not yet un-enlist from the transaction. Usually it does un-enlist by itself, but in distributed cases, sometimes it does not (or does it too late). There, enlisting with null allows to un-enlist from the already completed transaction and allows using the connection without a bad hack like Thread.Sleep.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With commit 90f01ecc341fe11ab19464e4ad7cd795ae28249f, I have tried putting back that AssertHasPreparedTransactions in place for cases enlisting the test durable ressource, but it fails there too. Although I have seen that count raising in some circumstances when debugging...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I guess I misunderstand it. I think this counter can go-up only when the prepare phase has been executed, but is not yet committed or rollback. So we cannot see that count in most circumstances. I do not think it is a good indicator for knowing if a distributed transaction is ongoing. Better check Transaction.Current.TransactionInformation.DistributedIdentifier != Guid.Empty.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, looks like I have issued a ctrl+k ctrl+d.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, please remove these spaces

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fredericDelaporte, thanks for this - it's really great work. I spent considerable effort rewriting and improving Npgsql's TransactionScope/distributed tx support for 3.2, and I'm really happy someone with knowledge and experience is taking a deep look at it.

Take a look at my comments below - everything looks good to me apart a nitpick or two. As for the actual issue - the race condition - I'll try to run your race condition-producing test in the next few days and will report on findings. In the meantime you can definitely continue working on tests and also submit PRs on the other, unrelated things we discussed (#1580).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, no problem. I'd prefer the message to be on the same line though.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that checking that data is visible via another connection (conn2) before the scope is completed should be good enough as a check - your way seems to be just as good to me. I don't really have anything against doing both.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely, this is a common usage pattern regardless of NHibernate. I actually worked on 3.2 to make sure escalation doesn't occur in this scenario and was convinced I had this test already, but there was only ReuseConnection which is close but not exactly that. Thanks.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note - I've never actually confirmed for myself that there's a possible race condition here. If so several tests should have been failing every now and then.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess they do not fail because they are never truly distributed, since Npgsql connections are currently volatile ressources, which do not mandate promotion to distributed whatever their count in the same transaction.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. In its current form this test doesn't seem very useful.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops... thanks for catching that and fixing!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: remove space between curlies

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean that you were seeing 0 transactions in pg_prepared_xacts although you were expecting one to be there? Do you consider this as evidence of the race condition you were mentioning before (or another race condition)?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, please remove these spaces

@fredericDelaporte fredericDelaporte left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes done. The other tests I may add would be to check if there are differences between volatile callback timings and durable ones, in regards to the scope disposal end.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess they do not fail because they are never truly distributed, since Npgsql connections are currently volatile ressources, which do not mandate promotion to distributed whatever their count in the same transaction.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the test durable ressource enlisted instead of a second connection, now it fails with the error "connection already executing".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With commit 90f01ecc341fe11ab19464e4ad7cd795ae28249f, I have tried putting back that AssertHasPreparedTransactions in place for cases enlisting the test durable ressource, but it fails there too. Although I have seen that count raising in some circumstances when debugging...

@fredericDelaporte

Copy link
Copy Markdown
Contributor Author

Rebased, slightly squashed, and added some more test for comparing volatile vs durable, and checking distributed vs non-distributed.

The race tests now demonstrates that even the durable resource has its commit phase happening after the scope disposal... In such a situation, the only way I can think of for avoiding a race condition when re-using the connection after the scope, it to introduced some internal locking mechanism inside it. A bit like what I have done for NHibernate session here. Any session usage will be blocked if required by calling the WaitOne added in that IEnlistmentNotification class.

But that will not avoid race conditions when using another connection to get at the data supposedly already committed.

I do not think I will add more to this PR now.

@roji
roji merged this pull request into npgsql:dev Jun 13, 2017
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.

2 participants