Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: try to avoid memory exhaustion
Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
ChristophWurst committed Apr 16, 2025
commit 8a7d3cc7e74c30ca4cac6525ad80b072c5ef97ef
16 changes: 12 additions & 4 deletions lib/Db/LoginAddressAggregatedMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ private function findHistoricIpv4(int $threshold, int $maxAge): array {
$qb->expr()->like('ip', $qb->createNamedParameter('_%._%._%._%')),
$qb->expr()->gte('last_seen', $qb->createNamedParameter($maxAge)),
$qb->expr()->lte('first_seen', $qb->createNamedParameter($threshold))
));
))
->orderBy('last_seen', 'DESC') // Use most recent data in case of limiting
->setMaxResults(15_000); // More data will like exhaust memory

return $this->findEntities($query);
}
Expand All @@ -84,7 +86,9 @@ private function findRecentIpV4(int $threshold): array {
->where($qb->expr()->andX(
$qb->expr()->like('ip', $qb->createNamedParameter('_%._%._%._%')),
$qb->expr()->gt('last_seen', $qb->createNamedParameter($threshold))
));
))
->orderBy('last_seen', 'DESC') // Use most recent data in case of limiting
->setMaxResults(3_000); // More data will like exhaust memory;

return $this->findEntities($query);
}
Expand Down Expand Up @@ -148,7 +152,9 @@ private function findHistoricIpv6(int $threshold, int $maxAge): array {
$qb->expr()->notLike('ip', $qb->createNamedParameter('_%._%._%._%')),
$qb->expr()->gte('last_seen', $qb->createNamedParameter($maxAge)),
$qb->expr()->lte('first_seen', $qb->createNamedParameter($threshold))
));
))
->orderBy('last_seen', 'DESC') // Use most recent data in case of limiting
->setMaxResults(15_000); // More data will like exhaust memory;

return $this->findEntities($query);
}
Expand All @@ -162,7 +168,9 @@ private function findRecentIpV6(int $threshold): array {
->where($qb->expr()->andX(
$qb->expr()->notLike('ip', $qb->createNamedParameter('_%._%._%._%')),
$qb->expr()->gt('last_seen', $qb->createNamedParameter($threshold))
));
))
->orderBy('last_seen', 'DESC') // Use most recent data in case of limiting
->setMaxResults(3_000); // More data will like exhaust memory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL about this notation :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they took inspiration from Rust :)


return $this->findEntities($query);
}
Expand Down
11 changes: 11 additions & 0 deletions lib/Service/DataLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
use function floor;
use function log;
use function max;
use function random_int;

class DataLoader {
private const MAX_SAMPLES_POSITIVES = 15_000;
private const MAX_SAMPLES_VALIDATE_POSITIVES = 3_000;

/** @var LoginAddressAggregatedMapper */
private $loginAddressMapper;
Expand Down Expand Up @@ -65,6 +68,14 @@ public function loadTrainingAndValidationData(TrainingDataConfig $dataConfig,

$positives = $this->addressesToDataSet($historyRaw, $strategy);
$validationPositives = $this->addressesToDataSet($recentRaw, $strategy);
if ($positives->count() > self::MAX_SAMPLES_POSITIVES) {
$threshold = (self::MAX_SAMPLES_POSITIVES / $positives->count()) * 100;
$positives = $positives->filter(fn () => random_int(0, 100) <= $threshold);
}
if ($validationPositives->count() > self::MAX_SAMPLES_VALIDATE_POSITIVES) {
$threshold = (self::MAX_SAMPLES_VALIDATE_POSITIVES / $validationPositives->count()) * 100;
$validationPositives = $validationPositives->filter(fn () => random_int(0, 100) <= $threshold);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probabilistic limit, nice!


return new CollectedData(
$positives,
Expand Down
Loading