diff --git a/src/Appwrite/Platform/Workers/StatsResources.php b/src/Appwrite/Platform/Workers/StatsResources.php index da8c086bf4..0c8f11c07b 100644 --- a/src/Appwrite/Platform/Workers/StatsResources.php +++ b/src/Appwrite/Platform/Workers/StatsResources.php @@ -434,18 +434,43 @@ class StatsResources extends Action { $message = 'Stats writeDocuments project: ' . $project->getId() . '(' . $project->getSequence() . ')'; + /** + * sort by unique index key reduce locks/deadlocks + */ + usort($this->documents, function ($a, $b) { + // Metric DESC + $cmp = strcmp($b['metric'], $a['metric']); + if ($cmp !== 0) { + return $cmp; + } + + // Period ASC + $cmp = strcmp($a['period'], $b['period']); + if ($cmp !== 0) { + return $cmp; + } + + // Time ASC, NULLs first + if ($a['time'] === null) { + return ($b['time'] === null) ? 0 : -1; + } + if ($b['time'] === null) { + return 1; + } + + return strcmp($a['time'], $b['time']); + }); + try { $dbForLogs->createOrUpdateDocuments( 'stats', - $this->documents + $this->documents, ); Console::success($message . ' | Documents: ' . count($this->documents)); } catch (\Throwable $e) { Console::error('Error: ' . $message . ' | Exception: ' . $e->getMessage()); throw $e; - } finally { - $this->documents = []; } } } diff --git a/src/Appwrite/Platform/Workers/StatsUsage.php b/src/Appwrite/Platform/Workers/StatsUsage.php index 3610381d5a..bd34d53b00 100644 --- a/src/Appwrite/Platform/Workers/StatsUsage.php +++ b/src/Appwrite/Platform/Workers/StatsUsage.php @@ -424,12 +424,40 @@ class StatsUsage extends Action try { $dbForProject = $getProjectDB($projectStats['project']); Console::log('Processing batch with ' . count($projectStats['stats']) . ' stats'); + + /** + * Sort by unique index key reduce locks/deadlocks + */ + usort($projectStats['stats'], function ($a, $b) { + // Metric DESC + $cmp = strcmp($b['metric'], $a['metric']); + if ($cmp !== 0) { + return $cmp; + } + + // Period ASC + $cmp = strcmp($a['period'], $b['period']); + if ($cmp !== 0) { + return $cmp; + } + + // Time ASC, NULLs first + if ($a['time'] === null) { + return ($b['time'] === null) ? 0 : -1; + } + if ($b['time'] === null) { + return 1; + } + + return strcmp($a['time'], $b['time']); + }); + $dbForProject->createOrUpdateDocumentsWithIncrease('stats', 'value', $projectStats['stats']); Console::success('Batch successfully written to DB'); - - unset($this->projects[$sequence]); } catch (Throwable $e) { Console::error('Error processing stats: ' . $e->getMessage()); + } finally { + unset($this->projects[$sequence]); } } @@ -468,12 +496,53 @@ class StatsUsage extends Action try { Console::log('Processing batch with ' . count($this->statDocuments) . ' stats'); + + /** + * Sort by UNIQUE KEY "_key_metric_period_time" ("_tenant","metric" DESC,"period","time") + * Here we sort by _tenant as well because of setTenantPerDocument + */ + + usort($this->statDocuments, function ($a, $b) { + // Tenant ASC + $cmp = $a['$tenant'] <=> $b['$tenant']; + if ($cmp !== 0) { + return $cmp; + } + + // Metric DESC + $cmp = strcmp($b['metric'], $a['metric']); + if ($cmp !== 0) { + return $cmp; + } + + // Period ASC + $cmp = strcmp($a['period'], $b['period']); + if ($cmp !== 0) { + return $cmp; + } + + // Time ASC, NULLs first + if ($a['time'] === null) { + return ($b['time'] === null) ? 0 : -1; + } + if ($b['time'] === null) { + return 1; + } + + return strcmp($a['time'], $b['time']); + }); + $dbForLogs->createOrUpdateDocumentsWithIncrease( 'stats', 'value', $this->statDocuments ); Console::success('Usage logs pushed to Logs DB'); + + /** + * todo: Do we need to unset $this->statDocuments? + */ + } catch (Throwable $th) { Console::error($th->getMessage()); }