Log database deadlocks in EventIssue

This commit is contained in:
Eric
2020-04-23 15:25:12 +02:00
parent 58fd8709be
commit 75730eeea0
2 changed files with 56 additions and 3 deletions

View File

@@ -661,6 +661,7 @@ class CMDBSource
}
catch (mysqli_sql_exception $e)
{
self::LogDeadLock($e);
throw new MySQLException('Failed to issue SQL query', array('query' => $sSql, $e));
}
$oKPI->ComputeStats('Query exec (mySQL)', $sSql);
@@ -674,13 +675,65 @@ class CMDBSource
{
throw new MySQLHasGoneAwayException(self::GetError(), $aContext);
}
throw new MySQLException('Failed to issue SQL query', $aContext);
$e = new MySQLException('Failed to issue SQL query', $aContext);
self::LogDeadLock($e);
throw $e;
}
return $oResult;
}
private static function LogDeadLock(Exception $e)
{
// Deadlock detection
$iMySqlErrorNo = self::$m_oMysqli->errno;
if (($iMySqlErrorNo == 1213) || ($iMySqlErrorNo == 1205))
{
// Try to log the deadlock
$oError = self::$m_oMysqli->query("SHOW ENGINE INNODB STATUS");
$aData = array();
if ($oError !== false)
{
$aData = $oError->fetch_all(MYSQLI_ASSOC);
}
if (MetaModel::IsLogEnabledIssue())
{
if (MetaModel::IsValidClass('EventIssue'))
{
try
{
$bInTransaction = self::IsInsideTransaction();
if ($bInTransaction)
{
// In order to log the error, we have to rollback the current transaction
// Else the caller will rollback our log
self::DBQuery('ROLLBACK');
}
$oLog = new EventIssue();
$oLog->Set('message', $e->getMessage());
$oLog->Set('userinfo', UserRights::GetUser());
$oLog->Set('issue', 'Database DeadLock');
$oLog->Set('impact', 'Request execution failed');
$oLog->Set('callstack', $e->getTrace());
$oLog->Set('data', $aData[0]);
$oLog->DBInsertNoReload();
if ($bInTransaction)
{
self::DBQuery('START TRANSACTION');
}
}
catch(Exception $e1)
{
IssueLog::Error("Failed to log database deadlock issue into the DB\n".$e1->getMessage());
}
}
}
IssueLog::Error($e->getMessage());
IssueLog::Error(print_r($aData[0], true));
}
}
/**
* If nested transaction, we are not starting a new one : only one global transaction will exist.
*

View File

@@ -215,7 +215,7 @@ class EventIssue extends Event
MetaModel::Init_AddAttribute(new AttributePropertySet("data", array("allowed_values"=>null, "sql"=>"data", "default_value"=>null, "is_null_allowed"=>true, "depends_on"=>array())));
// Display lists
MetaModel::Init_SetZListItems('details', array('date', 'userinfo', 'issue', 'impact', 'page', 'arguments_post', 'arguments_get', 'callstack', 'data')); // Attributes to be displayed for the complete details
MetaModel::Init_SetZListItems('details', array('date', 'message', 'userinfo', 'issue', 'impact', 'page', 'arguments_post', 'arguments_get', 'callstack', 'data')); // Attributes to be displayed for the complete details
MetaModel::Init_SetZListItems('list', array('date', 'userinfo', 'issue', 'impact')); // Attributes to be displayed for a list
// Search criteria
// MetaModel::Init_SetZListItems('standard_search', array('name')); // Criteria of the std search form