Repository navigation
Adding more tests to system transaction, demonstrating #1571 - #1583
Conversation
fredericDelaporte
left a comment
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I have added messages in a bunch of cases where the Assert failure could be ambiguous with another assert in the same test.
There was a problem hiding this comment.
Sure thing, no problem. I'd prefer the message to be on the same line though.
There was a problem hiding this comment.
For actually testing Enlist=false, checking a rollback does not prevent data to be changed seems to me the best way.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
This will cause a rollback from the prepare phase of the enlisted test ressource.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
OK. In its current form this test doesn't seem very useful.
There was a problem hiding this comment.
This test was looking like a copy paste of the previous one, renamed but not changed. Changes done now.
There was a problem hiding this comment.
Oops... thanks for catching that and fixing!
There was a problem hiding this comment.
Locally I have tried using that for adding a AssertHasPreparedTransactions in most distributed tests, but that assert was failing everywhere, finding nothing prepared.
There was a problem hiding this comment.
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)?
There was a problem hiding this comment.
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.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Hum, looks like I have issued a ctrl+k ctrl+d.
roji
left a comment
There was a problem hiding this comment.
@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).
There was a problem hiding this comment.
Sure thing, no problem. I'd prefer the message to be on the same line though.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
OK. In its current form this test doesn't seem very useful.
There was a problem hiding this comment.
Oops... thanks for catching that and fixing!
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
With the test durable ressource enlisted instead of a second connection, now it fails with the error "connection already executing".
There was a problem hiding this comment.
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...
|
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 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. |
This PR add some more tests to system transaction tests. It also has a test demonstrating #1571.