Handle deadlocks centrally

This commit is contained in:
Michael 2017-06-13 05:52:59 +00:00
parent 228993596a
commit 97be344a4a
2 changed files with 44 additions and 20 deletions

View file

@ -625,8 +625,21 @@ class dba {
} }
if (self::$dbo->errorno != 0) { if (self::$dbo->errorno != 0) {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1);
$called_from = array_shift($trace);
// We are having an own error logging in the function "p"
if ($called_from['function'] != 'p') {
// We have to preserve the error code, somewhere in the logging it get lost
$error = self::$dbo->error;
$errorno = self::$dbo->errorno;
logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n". logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n".
$a->callstack(8))."\n".self::replace_parameters($sql, $args); $a->callstack(8))."\n".self::replace_parameters($sql, $args);
self::$dbo->error = $error;
self::$dbo->errorno = $errorno;
}
} }
$a->save_timestamp($stamp1, 'database'); $a->save_timestamp($stamp1, 'database');
@ -662,6 +675,10 @@ class dba {
$args = func_get_args(); $args = func_get_args();
// In a case of a deadlock we are repeating the query 10 times
$timeout = 10;
do {
$stmt = call_user_func_array('self::p', $args); $stmt = call_user_func_array('self::p', $args);
if (is_bool($stmt)) { if (is_bool($stmt)) {
@ -674,6 +691,20 @@ class dba {
self::close($stmt); self::close($stmt);
} while ((self::$dbo->errorno = 1213) && (--$timeout > 0));
if (self::$dbo->errorno != 0) {
// We have to preserve the error code, somewhere in the logging it get lost
$error = self::$dbo->error;
$errorno = self::$dbo->errorno;
logger('DB Error '.self::$dbo->errorno.': '.self::$dbo->error."\n".
$a->callstack(8))."\n".self::replace_parameters($sql, $args);
self::$dbo->error = $error;
self::$dbo->errorno = $errorno;
}
$a->save_timestamp($stamp, "database_write"); $a->save_timestamp($stamp, "database_write");
return $retval; return $retval;

View file

@ -194,10 +194,7 @@ function poller_execute($queue) {
if (!validate_include($include)) { if (!validate_include($include)) {
logger("Include file ".$argv[0]." is not valid!"); logger("Include file ".$argv[0]." is not valid!");
$timeout = 10; dba::delete('workerqueue', array('id' => $queue["id"]));
while (!dba::delete('workerqueue', array('id' => $queue["id"])) && (--$timeout > 0)) {
sleep(1);
}
return true; return true;
} }
@ -207,11 +204,7 @@ function poller_execute($queue) {
if (function_exists($funcname)) { if (function_exists($funcname)) {
poller_exec_function($queue, $funcname, $argv); poller_exec_function($queue, $funcname, $argv);
$timeout = 10; dba::delete('workerqueue', array('id' => $queue["id"]));
while (!dba::delete('workerqueue', array('id' => $queue["id"])) && (--$timeout > 0)) {
logger('Delete ID '.$queue["id"], LOGGER_DEBUG);
sleep(1);
}
} else { } else {
logger("Function ".$funcname." does not exist"); logger("Function ".$funcname." does not exist");
} }