Skip to contentSkip to conversation

db retry: fixes and workarounds

PATCH New Ian Jackson 1 participant 10 messages Dec 20, 2016

lore
Share & tools
lore
#0 lore raw

cover db retry: fixes and workarounds

We have here:
* Two patches to improve mg-schema-test-database a bit.
* Fixes to cs-bisection-step, to survive and give right answers on db retry.
* An attempt at a workarounds for a strange DBD::Pg behaviour.

The workaround seems to WFM in my ad-hoc tests and I propose to deploy it in the hope that it will work in production too.

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

#1 lore raw

PATCH 1/9 mg-schema-test-database: Revamp sequence handling

The initial value (at creation time) of a sequence appears in the schema, but is not of any consequence. To avoid the schema diff check failing in databases created in a slightly different way, it is necessary to copy the actual original initial sequence value for each sequence.

Replace the sequence handling code with a setup which, for each sequence, copies the START WITH and calculates a fresh RESTART WITH.

This replaces both the unconditional copy (done with pgdump) and the special calculation of the next flight number. Now all sequences have the "bump the number somewhat" treatment, which seems nice.

---
 mg-schema-test-database | 43 ++++++++++++++++++++++---------------------
 1 file changed, 22 insertions(+), 21 deletions(-)
mg-schema-test-database +22 -21
index 5ebba39..a82c044 100755
@@ -413,6 +413,28 @@ END
 
 	withtest ./mg-schema-update -q apply $wantupdates
 
+        printf " (seqs)"
+        seq_alters=""
+        for seq in $sequences; do
+        	orgseqval=$(psql_query <<END
+			SELECT start_value || ' ' || last_value FROM $seq
+END
+                )
+                read orgstart orglast <<END
+$orgseqval
+END
+		newlast=$(( 10000 * (2 + $orglast / 10000) ))
+                seq_alters+="
+			ALTER SEQUENCE $seq
+				START WITH $orgstart
+				RESTART WITH $newlast;
+"
+        done
+	(withtest psql_do <<END
+$seq_alters
+END
+        )
+
 	printf ".\n"
 
 	# Schema should now be identical to main DB
@@ -444,14 +466,6 @@ END
 		SET CONSTRAINTS ALL DEFERRED;
 END
 
-	$(get_pgdump_cmd) -a -O -x ${sequences// / -t } >$t.sequences-import
-	perl <$t.sequences-import >>$t.import -ne '
-		next if m/^--/;
-		next if m/^SET /;
-		next unless m/\S/;
-		print or die $!;
-	'
-
 	for table in $tables; do
 		case " $ftables " in
 		*" $table "*)	condition="flight >= $minflight" ;;
@@ -490,19 +504,6 @@ END
 
 	rm -f $t.tabledata.*
 
-	printf "flightseq..."
-
-	lastflight=$(psql_query <<END
-		SELECT last_value FROM flights_flight_seq
-END
-		)
-	newlastflight=$(( 10000 * (2 + $lastflight / 10000) ))
-
-	withtest psql_do <<END
-		ALTER SEQUENCE flights_flight_seq
-			RESTART WITH $newlastflight;
-END
-
 	#---------- actually borrow resources ----------
 
 	printf "borrow..."
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#2 lore raw

PATCH 2/9 mg-schema-test-database: Wrap some withtest psql_do in subshells

In reply to Ian Jackson

Otherwise it takes effect for the rest of the script, which is not what is wanted ! As it happens, there are no accesses to the real db after this point, so this bug is latent.

---
 mg-schema-test-database | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
mg-schema-test-database +4 -3
index a82c044..0517ca3 100755
@@ -509,19 +509,20 @@ END
 	printf "borrow..."
 
 	for task in $tasks; do
-		withtest psql_do <<END
+		(withtest psql_do <<END
 			BEGIN;
 			UPDATE resources
 				SET owntaskid = $(taskid magic idle)
 				WHERE owntaskid = $(borrowtaskid $task);
 			COMMIT;
 END
+                )
 	done
-	withtest psql_do <<END
+	(withtest psql_do <<END
 		DELETE FROM tasks
 			WHERE type='xdbref' AND refkey='$dbname';
 END
-
+        )
 	printf "\n"
 
 	cat <<END
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#3 lore raw

PATCH 3/9 cs-bisection-step: Do not acquire the repo lock

In reply to Ian Jackson

This is only required to avoid a lock inversion between the repo lock and database table locks, but we have no explicit database table locks any more.

We do not want to hold the repo lock for an extended period, particularly when we are running a database retry loop.

In practice, currently, cs-bisection-step always fails with a database serialisation error.

---
 cs-bisection-step | 1 -
 1 file changed, 1 deletion(-)
cs-bisection-step +0 -1
index a0595b2..4c71587 100755
@@ -1405,7 +1405,6 @@ sub compute_exitstatus () {
 }
 
 csreadconfig();
-grabrepolock_reexec(@org_argv);
 reportcmdline();
 findbasics();
 digraph_whole();
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#4 lore raw

PATCH 4/9 db retry, bisection: Reset %jobs_created on db retry

In reply to Ian Jackson

%jobs_created is used for memoisation while populating the destination flight. We need to reset it when we restart flight construction, because those jobs were created in the discarded transaction.

Otherwise we could create a flight with missing jobs.

---
 cs-bisection-step | 1 +
 1 file changed, 1 insertion(+)
cs-bisection-step +1 -0
index 4c71587..1d1962a 100755
@@ -1286,6 +1286,7 @@ END
 
     db_retry($popflight,'constructing', $dbh_tests,[qw(flights)], sub {
         print STDERR "Populating $popflight...\n";
+	undef %jobs_created;
         preparejob($job, $latest_flight, 0);
         foreach my $hostspec (split /,/, $hosts) { # /
             $hostspec =~ m/=/;
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#5 lore raw

PATCH 5/9 db retry, bisect: Cache build reuse investigations

In reply to Ian Jackson

If we previously searched for builds to reuse, trust our previous answers. We will only have seen data from committed transactions and we will only have looked at jobs in completed flights, which won't have changed.

So any previously reuseable build is still reuseable. (Unless its stash check failed, in which case we might want to search again if we do a db retry.) We don't care much about missing any recently-finished jobs - there is a much bigger and unavoidable race there anyway, where multiple bisections on different branches may choose to pointlessly rebuild the same thing at the same time.

Not doing this search over and over again is important because it is a very wide ranging search, which will often cause database transaction serialisation errors. Without some caching here, we may never converge.

In principle we could do this another way: we could make a readonly transaction which did all the searching. But that's a more awkward way to organise the code because our search uses a temporary table which we then construct the flight from.

---
 cs-bisection-step | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
cs-bisection-step +10 -1
index 1d1962a..819e519 100755
@@ -1121,6 +1121,7 @@ END
 }
 
 our %jobs_created;
+our %builds_investigated; # $builds_investigated{$popjob} = 0, or {..row..}
 
 sub preparejob ($$$);
 sub preparejob ($$$) {
@@ -1194,7 +1195,11 @@ END
     my $usejob;
 
     if ($cache_option and $cacheok and $recipe =~ m/^build/ and !@$subjobs) {
-        my $reusejob= $dbh_tests->selectrow_hashref(<<END,{}, $popjob,$popjob);
+	my $reusejob= $builds_investigated{$popjob};
+	if (!defined $reusejob) {
+	    print STDERR "Searching for $popflight.$popjob to reuse...\n";
+	    $reusejob =
+		$dbh_tests->selectrow_hashref(<<END,{}, $popjob,$popjob);
             SELECT  *
             FROM    flights JOIN jobs j USING (flight)
             WHERE   j.job=?
@@ -1216,6 +1221,9 @@ END
             ORDER BY flights.started desc
             LIMIT 1
 END
+	    $reusejob //= 0; # defined but falseish
+	    $builds_investigated{$popjob}= $reusejob;
+	}
 
         if ($reusejob) {
 	    my $wantusejob= "$reusejob->{flight}.$reusejob->{job}";
@@ -1226,6 +1234,7 @@ END
 	    } else {
 		print STDERR "Can NOT reuse $recipe $wantusejob:".
 		    " $stashcheck: $!\n";
+		undef $builds_investigated{$popjob};
 	    }
         }
     }
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#6 lore raw

PATCH 6/9 db retry: Document $@ as an implicit parameter to need_retry

In reply to Ian Jackson

There are only two call sites and neither trashes $@ right now. We are going to use a more exception-friendly style.

---
 Osstest/JobDB/Executive.pm | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Osstest/JobDB/Executive.pm +1 -1
index 7eeaf52..ebafb4c 100644
@@ -59,7 +59,7 @@ sub begin_work ($$$) { #method
 }
 
 sub need_retry ($$$) {
-    my ($jd, $dbh,$committing) = @_;
+    my ($jd, $dbh,$committing) = @_; # implicitly, $@ is an argument too
     return
 	($dbh_tests->err() // 0)==7 &&
 	($dbh_tests->state =~ m/^(?:40P01|40001|23|40002)/);
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#7 lore raw

PATCH 7/9 db retry: Break out Osstest::Executive::JobDB::_need_retry

In reply to Ian Jackson

We are going to want to reorganise this. As prep work, break the $dbh state checking (and the corresponding comment) into a separate sub.

No functional changel.

(There is still an anomaly: need_retry passes it $dbh_tests, not the $dbh it got from the caller. This will go away shortly.)

---
 Osstest/JobDB/Executive.pm | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
Osstest/JobDB/Executive.pm +9 -4
index ebafb4c..5f2aac9 100644
@@ -58,11 +58,11 @@ sub begin_work ($$$) { #method
     }
 }
 
-sub need_retry ($$$) {
-    my ($jd, $dbh,$committing) = @_; # implicitly, $@ is an argument too
+sub _need_retry ($) {
+    my ($dbh) = @_;
     return
-	($dbh_tests->err() // 0)==7 &&
-	($dbh_tests->state =~ m/^(?:40P01|40001|23|40002)/);
+	($dbh->err() // 0)==7 &&
+	($dbh->state =~ m/^(?:40P01|40001|23|40002)/);
     # DEADLOCK DETECTED or SERIALIZATION FAILURE
     # or any Integrity Constraint Violation including
     # TRANSACTION_INTEGRITY_CONSTRAINT_VIOLATION.
@@ -109,6 +109,11 @@ sub need_retry ($$$) {
     # https://www.postgresql.org/message-id/flat/D960CB61B694CF459DCFB4B0128514C203937E44%40exadv11.host.magwien.gv.at
 }
 
+sub need_retry ($$$) {
+    my ($jd, $dbh,$committing) = @_; # implicitly, $@ is an argument too
+    return _need_retry($dbh_tests);
+}
+
 sub readonly_report ($$) { #method
     my ($jd, $dbh) = @_;
     
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#8 lore raw

PATCH 8/9 db retry: Use HandleError and exceptions to detect when to retry

In reply to Ian Jackson

It appears that sometimes, $dbh->state could be overwritten before $mjobdb->need_retry got to run. $dbh->err and $@ would still be right. I have not been able to explain this; I suspect that there is something exciting going on in the eval exception trapping.

To try to isolate the problem, I developed this different approach: we use the HandleError database handle feature, to cause all the retriable errors to be thrown as a dedicated exception class. We can then simply test ref $@. (We don't care about subclassing here. And we don't want isa because $@ might just be a string.) This is, in general, more reliable, as it cannot treat any other failures as db retry failures.

Osstest::Executive and Osstest::JobDB::Executive become slightly more intertwined with this patch.

Sadly this does not seem to completely eliminate the problem. It does allow us to present clearer evidence of problems in the underlying DBI, DBD or PostgreSQL layers...

---
 Osstest/Executive.pm       | 14 ++++++++++++++
 Osstest/JobDB/Executive.pm |  2 +-
 2 files changed, 15 insertions(+), 1 deletion(-)
Osstest/Executive.pm +14 -0
index c423570..635e5dd 100644
@@ -245,6 +245,8 @@ sub db_pg_dsn ($) {
     return $pg;
 }
 
+use Exception::Class ( 'Osstest::JobDB::Executive::RetryException' );
+
 sub opendb ($) {
     my ($dbname) = @_;
 
@@ -252,6 +254,18 @@ sub opendb ($) {
 
     my $dbh= DBI->connect("dbi:Pg:$pg", '','', {
         AutoCommit => 1,
+        HandleError => sub {
+            my ($emsg,$edbh,$firstval) = @_;
+            if (Osstest::JobDB::Executive::_need_retry($edbh)) {
+                chomp $emsg;
+                printf STDERR "DB confict (err=%s state=%s): %s\n",
+                    ($edbh->err // 'undef'),
+                    $edbh->state,
+                    $emsg;
+                die new Osstest::JobDB::Executive::RetryException $emsg;
+            }
+            undef;
+        },
         RaiseError => 1,
         ShowErrorStatement => 1,
         })
Osstest/JobDB/Executive.pm +1 -1
index 5f2aac9..5545849 100644
@@ -111,7 +111,7 @@ sub _need_retry ($) {
 
 sub need_retry ($$$) {
     my ($jd, $dbh,$committing) = @_; # implicitly, $@ is an argument too
-    return _need_retry($dbh_tests);
+    return ref($@) eq 'Osstest::JobDB::Executive::RetryException';
 }
 
 sub readonly_report ($$) { #method
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#9 lore raw

PATCH 9/9 db retry: Retry on $dbh->state eq ''

In reply to Ian Jackson

This is supposed to represent success. But now that _need_retry is only called within a HandleError hook, we know there has been a failure.

Retry such failures, in the hope that they are stochastic. If they aren't, we will fail eventually when we run out of retries.

---
 Osstest/JobDB/Executive.pm | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Osstest/JobDB/Executive.pm +4 -1
index 5545849..83e19e4 100644
@@ -62,11 +62,14 @@ sub _need_retry ($) {
     my ($dbh) = @_;
     return
 	($dbh->err() // 0)==7 &&
-	($dbh->state =~ m/^(?:40P01|40001|23|40002)/);
+	($dbh->state =~ m/^(?:40P01|40001|23|40002|$)/);
     # DEADLOCK DETECTED or SERIALIZATION FAILURE
     # or any Integrity Constraint Violation including
     # TRANSACTION_INTEGRITY_CONSTRAINT_VIOLATION.
     #
+    # Or the empty string, which it seems that we sometimes get on
+    # serialisation errors.
+    #
     # An Integrity Constraint Violation ought not to occur with
     # serialisable transactions, so it is aways a bug.  These bugs
     # should not be retried.  However, there is a longstanding bug in
Signature · 7 lines
2.1.4


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