Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APCng storage - avoid worst-case memory usage in buildPermutationTree #123

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
39 changes: 14 additions & 25 deletions src/Prometheus/Storage/APCng.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,33 +430,23 @@ private function metaData(array $data): array
* [9] => ['/private', 'get', 'fail'], [10] => ['/private', 'post', 'success'], [11] => ['/private', 'post', 'fail'],
* [12] => ['/metrics', 'put', 'success'], [13] => ['/metrics', 'put', 'fail'], [14] => ['/metrics', 'get', 'success'],
* [15] => ['/metrics', 'get', 'fail'], [16] => ['/metrics', 'post', 'success'], [17] => ['/metrics', 'post', 'fail']
* @param array<string> $labelNames
* @param array<array> $labelValues
* @return array<array>
* @return Generator<array>
*/
private function buildPermutationTree(array $labelNames, array $labelValues): array /** @phpstan-ignore-line */
{
$treeRowCount = count(array_keys($labelNames));
$numElements = 1;
$treeInfo = [];
for ($i = $treeRowCount - 1; $i >= 0; $i--) {
$treeInfo[$i]['numInRow'] = count($labelValues[$i]);
$numElements *= $treeInfo[$i]['numInRow'];
$treeInfo[$i]['numInTree'] = $numElements;
}

$map = array_fill(0, $numElements, []);
for ($row = 0; $row < $treeRowCount; $row++) {
$col = $i = 0;
while ($i < $numElements) {
$val = $labelValues[$row][$col];
$map[$i] = array_merge($map[$i], array($val));
if (++$i % ($treeInfo[$row]['numInTree'] / $treeInfo[$row]['numInRow']) == 0) {
$col = ++$col % $treeInfo[$row]['numInRow'];
}
private function buildPermutationTree(array $labelValues): \Generator /** @phpstan-ignore-line */
{
if($labelValues){
$lastIndex = array_key_last($labelValues);
if($currentValue = array_pop($labelValues)){
foreach($this->buildPermutationTree($labelValues) as $prefix){
foreach($currentValue as $value){
yield $prefix + [$lastIndex => $value];
}
}
}
} else {
yield [];
}
return $map;
}

/**
Expand Down Expand Up @@ -557,10 +547,9 @@ private function getValues(string $type, array $metaData): array /** @phpstan-ig
if (isset($metaData['buckets'])) {
$metaData['buckets'][] = 'sum';
$labels[] = $metaData['buckets'];
$metaData['labelNames'][] = '__histogram_buckets';
Copy link
Author

Choose a reason for hiding this comment

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

Since I don't use the number of label names anymore, I removed that parameter from buildPermutationTree and removed this line as it seemed unused. I don't see any change in the output for histograms but if this actually used then I can re-add it.

}

$labelValuesList = $this->buildPermutationTree($metaData['labelNames'], $labels);
$labelValuesList = $this->buildPermutationTree($labels);
unset($labels);
$histogramBucket = '';
foreach ($labelValuesList as $labelValues) {
Expand Down