diff --git a/fdbserver/StorageMetrics.actor.cpp b/fdbserver/StorageMetrics.actor.cpp index 1e33a12e2c..08c7b9a8ab 100644 --- a/fdbserver/StorageMetrics.actor.cpp +++ b/fdbserver/StorageMetrics.actor.cpp @@ -549,9 +549,9 @@ void StorageServerMetrics::add(KeyRangeMap& map, KeyRangeRef const& keys, i // Returns the sampled metric value (possibly 0, possibly increased by the sampling factor) int64_t TransientStorageMetricSample::addAndExpire(const Key& key, int64_t metric, double expiration) { - int64_t x = add(key, metric); + auto x = add(key, metric); if (x) - queue.emplace_back(expiration, std::make_pair(*sample.find(key), -x)); + queue.emplace_back(expiration, std::make_pair(key, -x)); return x; } @@ -575,17 +575,18 @@ bool TransientStorageMetricSample::roll(int64_t metric) const { } void TransientStorageMetricSample::poll(KeyRangeMap>>& waitMap, - StorageMetrics m) { + StorageMetrics metrics) { double now = ::now(); while (queue.size() && queue.front().first <= now) { KeyRef key = queue.front().second.first; int64_t delta = queue.front().second.second; ASSERT(delta != 0); - if (sample.addMetric(key, delta) == 0) - sample.erase(key); + auto [m, it] = sample.addMetric(key, delta); + if (m == 0) + sample.erase(it); - StorageMetrics deltaM = m * delta; + StorageMetrics deltaM = metrics * delta; auto v = waitMap[key]; for (int i = 0; i < v.size(); i++) { CODE_PROBE(true, "TransientStorageMetricSample poll update"); @@ -603,8 +604,9 @@ void TransientStorageMetricSample::poll() { int64_t delta = queue.front().second.second; ASSERT(delta != 0); - if (sample.addMetric(key, delta) == 0) - sample.erase(key); + auto [m, it] = sample.addMetric(key, delta); + if (m == 0) + sample.erase(it); queue.pop_front(); } @@ -621,8 +623,9 @@ int64_t TransientStorageMetricSample::add(const Key& key, int64_t metric) { metric = metric < 0 ? -metricUnitsPerSample : metricUnitsPerSample; } - if (sample.addMetric(key, metric) == 0) - sample.erase(key); + auto [m, it] = sample.addMetric(key, metric); + if (m == 0) + sample.erase(it); return metric; } diff --git a/fdbserver/include/fdbserver/StorageMetrics.actor.h b/fdbserver/include/fdbserver/StorageMetrics.actor.h index 69ebee5c90..88cde8f35f 100644 --- a/fdbserver/include/fdbserver/StorageMetrics.actor.h +++ b/fdbserver/include/fdbserver/StorageMetrics.actor.h @@ -74,6 +74,8 @@ struct TransientStorageMetricSample : StorageMetricSample { private: bool roll(int64_t metric) const; + + // return the sampled metric delta int64_t add(const Key& key, int64_t metric); }; diff --git a/flow/include/flow/IndexedSet.h b/flow/include/flow/IndexedSet.h index 197fe2acf6..f266916c02 100644 --- a/flow/include/flow/IndexedSet.h +++ b/flow/include/flow/IndexedSet.h @@ -193,9 +193,9 @@ public: int insert(const std::vector>& data, bool replaceExisting = true); // Increase the metric for the given item by the given amount. Inserts data into the set if it - // doesn't exist. Returns the new sum. + // doesn't exist. Returns the pair of new sum and inserted iterator. template - Metric addMetric(T_&& data, Metric_&& metric); + std::pair addMetric(T_&& data, Metric_&& metric); // Remove the data item, if any, which is equal to key template @@ -820,15 +820,16 @@ typename IndexedSet::template Impl::IteratorT IndexedSet template -Metric IndexedSet::addMetric(T_&& data, Metric_&& metric) { +std::pair::iterator> IndexedSet::addMetric(T_&& data, + Metric_&& metric) { auto i = find(data); if (i == end()) { - insert(std::forward(data), std::forward(metric)); - return metric; + auto it = insert(std::forward(data), std::forward(metric)); + return { metric, it }; } else { Metric m = metric + getMetric(i); - insert(std::forward(data), m); - return m; + auto it = insert(std::forward(data), m); + return { m, it }; } } diff --git a/flow/include/flow/MetricSample.h b/flow/include/flow/MetricSample.h index f8f9250a61..527da724f6 100644 --- a/flow/include/flow/MetricSample.h +++ b/flow/include/flow/MetricSample.h @@ -127,14 +127,14 @@ struct TransientThresholdMetricSample : MetricSample { int64_t delta = std::get<2>(queue.front()); ASSERT(delta != 0); - int64_t val = this->sample.addMetric(T(key), delta); + auto [val, it] = this->sample.addMetric(T(key), delta); if (val < thresholdLimit && (val + std::abs(delta)) >= thresholdLimit) { auto iter = thresholdCrossedSet.find(key); ASSERT(iter != thresholdCrossedSet.end()); thresholdCrossedSet.erase(iter); } if (val == 0) - this->sample.erase(key); + this->sample.erase(it); queue.pop_front(); } @@ -159,7 +159,7 @@ private: metric = metric < 0 ? -this->metricUnitsPerSample : this->metricUnitsPerSample; } - int64_t val = this->sample.addMetric(T(key), metric); + auto [val, it] = this->sample.addMetric(T(key), metric); if (val >= thresholdLimit) { ASSERT((val - metric) < thresholdLimit ? thresholdCrossedSet.find(key) == thresholdCrossedSet.end() : thresholdCrossedSet.find(key) != thresholdCrossedSet.end()); @@ -167,7 +167,7 @@ private: } if (val == 0) - this->sample.erase(key); + this->sample.erase(it); return metric; }