Skip to content

Commit

Permalink
Eliminate wlocks when updating existing histograms, gauges (#116)
Browse files Browse the repository at this point in the history
apcu_add always takes a wlock in the current version 5.1.22, which
leads to lots of lock contention in high concurrency scenarios. Since
the most common operation ought to be updating an already existing
key, wrap apcu_add in an apcu_exists that only takes a rlock.

Signed-off-by: Tobias Bengtsson <[email protected]>
Co-authored-by: Lukas Kämmerling <[email protected]>
  • Loading branch information
TobiasBengtsson and LKaemmerling authored Apr 14, 2023
1 parent bf7d43e commit 144022e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
33 changes: 22 additions & 11 deletions src/Prometheus/Storage/APC.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ public function updateHistogram(array $data): void
{
// Initialize the sum
$sumKey = $this->histogramBucketValueKey($data, 'sum');
$new = apcu_add($sumKey, $this->toBinaryRepresentationAsInteger(0));
if (!apcu_exists($sumKey)) {
$new = apcu_add($sumKey, $this->toBinaryRepresentationAsInteger(0));

// If sum does not exist, assume a new histogram and store the metadata
if ($new) {
apcu_store($this->metaKey($data), json_encode($this->metaData($data)));
// If sum does not exist, assume a new histogram and store the metadata
if ($new) {
apcu_store($this->metaKey($data), json_encode($this->metaData($data)));
}
}

// Atomically increment the sum
Expand All @@ -83,8 +85,11 @@ public function updateHistogram(array $data): void
}

// Initialize and increment the bucket
apcu_add($this->histogramBucketValueKey($data, $bucketToIncrease), 0);
apcu_inc($this->histogramBucketValueKey($data, $bucketToIncrease));
$bucketKey = $this->histogramBucketValueKey($data, $bucketToIncrease);
if (!apcu_exists($bucketKey)) {
apcu_add($bucketKey, 0);
}
apcu_inc($bucketKey);
}

/**
Expand All @@ -94,11 +99,15 @@ public function updateSummary(array $data): void
{
// store meta
$metaKey = $this->metaKey($data);
apcu_add($metaKey, $this->metaData($data));
if (!apcu_exists($metaKey)) {
apcu_add($metaKey, $this->metaData($data));
}

// store value key
$valueKey = $this->valueKey($data);
apcu_add($valueKey, $this->encodeLabelValues($data['labelValues']));
if (!apcu_exists($valueKey)) {
apcu_add($valueKey, $this->encodeLabelValues($data['labelValues']));
}

// trick to handle uniqid collision
$done = false;
Expand All @@ -118,9 +127,11 @@ public function updateGauge(array $data): void
apcu_store($valueKey, $this->toBinaryRepresentationAsInteger($data['value']));
apcu_store($this->metaKey($data), json_encode($this->metaData($data)));
} else {
$new = apcu_add($valueKey, $this->toBinaryRepresentationAsInteger(0));
if ($new) {
apcu_store($this->metaKey($data), json_encode($this->metaData($data)));
if (!apcu_exists($valueKey)) {
$new = apcu_add($valueKey, $this->toBinaryRepresentationAsInteger(0));
if ($new) {
apcu_store($this->metaKey($data), json_encode($this->metaData($data)));
}
}
// Taken from https://github.com/prometheus/client_golang/blob/66058aac3a83021948e5fb12f1f408ff556b9037/prometheus/value.go#L91
$done = false;
Expand Down
7 changes: 5 additions & 2 deletions src/Prometheus/Storage/APCng.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,11 @@ public function updateHistogram(array $data): void
}

// Initialize and increment the bucket
apcu_add($this->histogramBucketValueKey($data, $bucketToIncrease), 0);
apcu_inc($this->histogramBucketValueKey($data, $bucketToIncrease));
$bucketKey = $this->histogramBucketValueKey($data, $bucketToIncrease);
if (!apcu_exists($bucketKey)) {
apcu_add($bucketKey, 0);
}
apcu_inc($bucketKey);
}

/**
Expand Down

0 comments on commit 144022e

Please sign in to comment.