Skip to contentSkip to conversation

Executive DB: Eliminate SQL locking for read-only transactions

PATCH In review Ian Jackson 2 participants 6 messages Dec 11, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 Executive DB: Eliminate SQL locking for read-only transactions

Our transactions generally run with
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
(which, incidentally, does not mean that the transactions are necessarily serialisable!)

In SQL in general it is possible for a read-only transaction to fail and need to be retried because some writer has updated things.

However, in PostgreSQL this is not possible because Postgres uses multi-version concurrency control: it retains the old version of the data while the read transaction is open:
http://www.postgresql.org/docs/8.3/static/transaction-iso.html

(And, of course, SQLite uses MVCC too, and all transactions in SQLite are fully serialisable.)

So it is not necessary for these read-only operations to take out locks. When they do so they can unnecessarily block other important work for long periods of time.

With this change, we go further from the ability to support databases other than PostgreSQL and SQLite. However, such support was very distant anyway because of differences in SQL syntax and semantics, our reliance in Executive mode on sql's command line utilities, and so on.

We retain the db_retry framing because (a) although the retry loop is not necessary in these cases, the transaction framing is (b) it will make it slightly easier to reverse this decision in the future if we ever decide to do so (c) it is less code churn.

---
 Osstest/Executive.pm   |    6 ++++--
 cr-ensure-disk-space   |    2 +-
 ms-planner             |    3 ++-
 sg-report-flight       |    2 +-
 sg-report-host-history |    8 ++++----
 sg-report-job-history  |    2 +-
 6 files changed, 13 insertions(+), 10 deletions(-)
Osstest/Executive.pm +4 -2
index 84c7d46..d9c698a 100644
@@ -90,8 +90,10 @@ our (@all_lock_tables) = qw(flights resources);
 #
 # READS:
 #
-#  Nontransactional reads are also permitted
-#  Transactional reads must take out locks as if they were modifying
+#  Nontransactional and transactional reads are also permitted
+#  and do not need to take out any locks.  (We support only databases
+#  using multi-version concurrency control, which includes PostgreSQL
+#  and SQLite.)
 
 augmentconfigdefaults(
     ControlDaemonHost => 'control-daemons',
cr-ensure-disk-space +1 -1
index 138f3f4..a1f838f 100755
@@ -145,7 +145,7 @@ END
     return 1;
 }
 
-db_retry($dbh_tests,[qw(flights)], sub {
+db_retry($dbh_tests,[], sub {
     @flights = ();
     for (;;) {
 	iteration_continue()
ms-planner +2 -1
index cab32c9..fc65a5b 100755
@@ -57,7 +57,8 @@ my $fn= "data-$walker.pl";
 
 sub allocations ($$) {
     my ($init, $each) = @_;
-    db_retry($dbh_tests, \@all_lock_tables, sub {
+
+    db_retry($dbh_tests, [], sub {
 	$init->();
 	
 	our $resources_q ||= $dbh_tests->prepare(<<END);
sg-report-flight +1 -1
index bbe03fe..db0d922 100755
@@ -1284,7 +1284,7 @@ END
     rename "$htmlout.new", $htmlout or die $!;
 }
 
-db_begin_work($dbh_tests, [qw(flights)]);
+db_begin_work($dbh_tests, []);
 findspecflight();
 my $fi= examineflight($specflight);
 my @fails= justifyfailures($fi);
sg-report-host-history +4 -4
index 79dc519..9d39483 100755
@@ -259,7 +259,7 @@ END
     rename "$html_file.new", "$html_file" or die "$html_file $!";
 }
 
-db_retry($dbh_tests, [qw(flights resources)], sub {
+db_retry($dbh_tests, [], sub {
     computeflightsrange();
 });
 
@@ -271,7 +271,7 @@ $dbh_tests->do("SET LOCAL enable_seqscan=false");
 foreach my $host (@ARGV) {
     if ($host =~ m/^flight:/) {
 	my $flight=$'; #';
-	db_retry($dbh_tests, [qw(flights)], sub {
+	db_retry($dbh_tests, [], sub {
 	    our $hostsinflightq //= db_prepare(<<END);
 	        SELECT DISTINCT val
 		  FROM runvars
@@ -292,12 +292,12 @@ END
 
 exit 0 unless %hosts;
 
-db_retry($dbh_tests, [qw(flights)], sub {
+db_retry($dbh_tests, [], sub {
     mainquery();
 });
 
 foreach my $host (sort keys %hosts) {
-    db_retry($dbh_tests, [qw(flights)], sub {
+    db_retry($dbh_tests, [], sub {
 	reporthost $host;
     });
 }
sg-report-job-history +1 -1
index 0e2a3f9..0ca441c 100755
@@ -298,5 +298,5 @@ sub processjob ($) {
     processjobbranch($j,$_) foreach @branches;
 }
 
-db_begin_work($dbh_tests, [qw(flights)]);
+db_begin_work($dbh_tests, []);
 foreach my $j (@jobs) { processjob($j); }
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH 2/2 Executive DB: Reduce strength of DB locks

The purpose of these locks is partly to prevent transactions being aborted (which I'm not sure the existing code would in practice cope with, although this is a bug) and also to avoid bugs due to the fact that
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
not mean that the transactions are necessarily serialisable!
http://www.postgresql.org/docs/8.3/static/transaction-iso.html

In SQL in general it is possible for read-only transactions to conflict with writing transactions.

However, in PostgreSQL this is not a problem because Postgres uses multi-version concurrency control: it retains the old version of the data while the read transaction is open:
http://www.postgresql.org/docs/8.3/static/transaction-iso.html

So a read transaction cannot cause a write transaction to abort, nor vice versa. So there is no need to have the database explicit table locks prevent concurrent read access.

Preventing concurrent read access means that simple and urgent updates can be unnecessarily delayed by long-running reader transactions in the history reporters and archaelogists.

So, reduce the lock mode from ACCESS EXCLUSIVE to ACCESS. This still conflicts with all kinds of updates and prospective updates, but no longer with SELECT:
http://www.postgresql.org/docs/8.3/static/explicit-locking.html

---
 Osstest/JobDB/Executive.pm |    2 +-
 tcl/JobDB-Executive.tcl    |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
Osstest/JobDB/Executive.pm +1 -1
index 69cb276..124e7c0 100644
@@ -43,7 +43,7 @@ sub begin_work ($$$) { #method
     
     return if $ENV{'OSSTEST_DEBUG_NOSQLLOCK'};
     foreach my $tab (@$tables) {
-        $dbh->do("LOCK TABLE $tab IN ACCESS EXCLUSIVE MODE");
+        $dbh->do("LOCK TABLE $tab IN EXCLUSIVE MODE");
     }
 }
 
tcl/JobDB-Executive.tcl +1 -1
index f37bbaf..a3dbb9e 100644
@@ -133,7 +133,7 @@ proc lock-tables {tables} {
     # must be inside transaction
     foreach tab $tables {
         db-execute "
-		LOCK TABLE $tab IN ACCESS EXCLUSIVE MODE
+		LOCK TABLE $tab IN EXCLUSIVE MODE
         "
     }
 }
Signature · 1 line
1.7.10.4
Acked-by on v1 2/2
#2 lore raw

Re: [OSSTEST PATCH 2/2] Executive DB: Reduce strength of DB locks

Quoted 6 lines · Ian Jackson

The purpose of these locks is partly to prevent transactions being aborted (which I'm not sure the existing code would in practice cope with, although this is a bug) and also to avoid bugs due to the fact that
  SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
not mean that the transactions are necessarily serialisable!

 ^does not?

Quoted 10 lines

  http://www.postgresql.org/docs/8.3/static/transaction-iso.html

In SQL in general it is possible for read-only transactions to conflict with writing transactions.

However, in PostgreSQL this is not a problem because Postgres uses multi-version concurrency control: it retains the old version of the data while the read transaction is open:
  http://www.postgresql.org/docs/8.3/static/transaction-iso.html

Quoted 7 lines

So a read transaction cannot cause a write transaction to abort, nor vice versa.  So there is no need to have the database explicit table locks prevent concurrent read access.

Preventing concurrent read access means that simple and urgent updates can be unnecessarily delayed by long-running reader transactions in the history reporters and archaelogists.

                            archaeologists

Quoted 6 lines

So, reduce the lock mode from ACCESS EXCLUSIVE to ACCESS.  This still conflicts with all kinds of updates and prospective updates, but no longer with SELECT:
  http://www.postgresql.org/docs/8.3/static/explicit-locking.html

Quoted 28 lines

---
 Osstest/JobDB/Executive.pm |    2 +-
 tcl/JobDB-Executive.tcl    |    2 +-

 2 files changed, 2 insertions(+), 2 deletions(-)
Osstest/JobDB/Executive.pm +0 -0
index 69cb276..124e7c0 100644
@@ -43,7 +43,7 @@ sub begin_work ($$$) { #method


     return if $ENV{'OSSTEST_DEBUG_NOSQLLOCK'}; foreach my $tab (@$tables) {
-        $dbh->do("LOCK TABLE $tab IN ACCESS EXCLUSIVE MODE"); +        $dbh->do("LOCK TABLE $tab IN EXCLUSIVE MODE"); }
 }

tcl/JobDB-Executive.tcl +0 -0
index f37bbaf..a3dbb9e 100644
@@ -133,7 +133,7 @@ proc lock-tables {tables} {
      # must be inside transaction
      foreach tab $tables {

         db-execute "

tcl/JobDB-Executive.tcl +1 -1
-		LOCK TABLE $tab IN ACCESS EXCLUSIVE MODE
+		LOCK TABLE $tab IN EXCLUSIVE MODE
Quoted 3 lines

         "
     }
 }

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#3 lore raw

Re: [OSSTEST PATCH 2/2] Executive DB: Reduce strength of DB locks

Ian Campbell writes ("Re: [OSSTEST PATCH 2/2] Executive DB: Reduce strength of DB locks"):

Quoted 5 lines · Ian Jackson
Quoted 2 lines

  SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; not mean that the transactions are necessarily serialisable!

 ^does not?

Yes, thanks.

Quoted 5 lines
Quoted 3 lines

Preventing concurrent read access means that simple and urgent updates can be unnecessarily delayed by long-running reader transactions in the history reporters and archaelogists.

                            archaeologists

Fixed.

Quoted 8 lines
Quoted 6 lines

So, reduce the lock mode from ACCESS EXCLUSIVE to ACCESS.  This still conflicts with all kinds of updates and prospective updates, but no longer with SELECT:
  http://www.postgresql.org/docs/8.3/static/explicit-locking.html

Thanks,
Ian.

Acked-by on v1 1/2
#4 lore raw
In reply to Ian Jackson
Quoted 25 lines · Ian Jackson

Our transactions generally run with
  SET TRANSACTION ISOLATION LEVEL SERIALIZABLE; (which, incidentally, does not mean that the transactions are necessarily serialisable!)

In SQL in general it is possible for a read-only transaction to fail and need to be retried because some writer has updated things.

However, in PostgreSQL this is not possible because Postgres uses multi-version concurrency control: it retains the old version of the data while the read transaction is open:
  http://www.postgresql.org/docs/8.3/static/transaction-iso.html

(And, of course, SQLite uses MVCC too, and all transactions in SQLite are fully serialisable.)

So it is not necessary for these read-only operations to take out locks.  When they do so they can unnecessarily block other important work for long periods of time.

With this change, we go further from the ability to support databases other than PostgreSQL and SQLite.  However, such support was very distant anyway because of differences in SQL syntax and semantics, our reliance in Executive mode on sql's command line utilities, and so on.

Quoted 6 lines

We retain the db_retry framing because (a) although the retry loop is not necessary in these cases, the transaction framing is (b) it will make it slightly easier to reverse this decision in the future if we ever decide to do so (c) it is less code churn.

(I haven't actually checked that everywhere you have changed is actually r/o, nor whether you have found every r/o operation).

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#5 lore raw

Ian Campbell writes ("Re: [OSSTEST PATCH 1/2] Executive DB: Eliminate SQL locking for read-only transactions"):

Quoted 6 lines · Ian Jackson
Quoted 5 lines

We retain the db_retry framing because (a) although the retry loop is not necessary in these cases, the transaction framing is (b) it will make it slightly easier to reverse this decision in the future if we ever decide to do so (c) it is less code churn.

Quoted 6 lines
Quoted 1 line

(I haven't actually checked that everywhere you have changed is actually r/o

Justifying that claim, from the diffstat:

Comment only:

 Osstest/Executive.pm   |    6 ++++--

These two I have checked, but their function is indeed RO:

 cr-ensure-disk-space   |    2 +-
 ms-planner             |    3 ++-

(Note that `ms-planner' just manipulates data-plan.pl et al; it is left to the clients to actually obtain resources in the DB.)

These are clearly RO:

 sg-report-flight       |    2 +-
 sg-report-host-history |    8 ++++----
 sg-report-job-history  |    2 +-
Quoted 1 line

nor whether you have found every r/o operation).

I may have missed some, but the obvious potential remaining long-running lockholder would be cs-bisection-step. I have looked at cs-bisection step and:

- It does not seem to take any lock, or even begin a transaction, for its primary archaeology function. I think this is fine because if some test results or whatever show up later it ought not to break the algorithm.

- The flight construction _is_ within a lock, in populateflight, but it basically just clones a template and merges in information from the archaeologist part.

- There is the check in `checkrepetition' - the flail test. This is not a trivial transaction and it would be better if it didn't take the lock. It is only not read-only because it ends, in the error case, with an UPDATE to set the flight broken. This could be in a separate transaction given that no-one else is supposed to be messing about with the flight that cs-bisection-step is constructing. But this is not a trivial change.

Ian.