2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* Notified.h
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-07-15 07:46:08 +08:00
|
|
|
#ifndef FDBCLIENT_NOTIFIED_H
|
|
|
|
#define FDBCLIENT_NOTIFIED_H
|
2017-05-26 04:48:44 +08:00
|
|
|
#pragma once
|
|
|
|
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "fdbclient/FDBTypes.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
#include "flow/TDMetric.actor.h"
|
|
|
|
|
2019-11-14 05:30:34 +08:00
|
|
|
template <class T>
|
|
|
|
struct IsMetricHandle : std::false_type {};
|
|
|
|
template <class T>
|
|
|
|
struct IsMetricHandle<MetricHandle<T>> : std::true_type {};
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-11-14 05:30:34 +08:00
|
|
|
template <class T, class ValueType = T>
|
|
|
|
struct Notified {
|
|
|
|
explicit Notified(ValueType v = 0) { val = v; }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-11-14 05:30:34 +08:00
|
|
|
[[nodiscard]] Future<Void> whenAtLeast(const ValueType& limit) {
|
|
|
|
if (val >= limit) return Void();
|
2017-05-26 04:48:44 +08:00
|
|
|
Promise<Void> p;
|
2019-11-14 05:30:34 +08:00
|
|
|
waiting.push(std::make_pair(limit, p));
|
2017-05-26 04:48:44 +08:00
|
|
|
return p.getFuture();
|
|
|
|
}
|
|
|
|
|
2019-11-14 05:30:34 +08:00
|
|
|
[[nodiscard]] ValueType get() const { return val; }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-11-14 05:30:34 +08:00
|
|
|
void initMetric(const StringRef& name, const StringRef& id) {
|
|
|
|
if constexpr (IsMetricHandle<T>::value) {
|
2019-11-14 07:32:52 +08:00
|
|
|
ValueType v = val;
|
2019-11-14 05:30:34 +08:00
|
|
|
val.init(name, id);
|
2019-11-14 07:32:52 +08:00
|
|
|
val = v;
|
2019-11-14 05:30:34 +08:00
|
|
|
} else {
|
|
|
|
TraceEvent(SevError, "InvalidNotifiedOperation")
|
|
|
|
.detail("Reason", "Notified<T> where T is not a metric: Can't use initMetric");
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 05:30:34 +08:00
|
|
|
void set(const ValueType& v) {
|
|
|
|
ASSERT(v >= val);
|
2019-07-13 04:10:21 +08:00
|
|
|
if (v != val) {
|
|
|
|
val = v;
|
|
|
|
|
|
|
|
std::vector<Promise<Void>> toSend;
|
2019-11-14 05:30:34 +08:00
|
|
|
while (waiting.size() && v >= waiting.top().first) {
|
2019-07-13 04:10:21 +08:00
|
|
|
Promise<Void> p = std::move(waiting.top().second);
|
|
|
|
waiting.pop();
|
|
|
|
toSend.push_back(p);
|
|
|
|
}
|
2019-11-14 05:30:34 +08:00
|
|
|
for (auto& p : toSend) {
|
2019-07-13 04:10:21 +08:00
|
|
|
p.send(Void());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 05:30:34 +08:00
|
|
|
void operator=(const ValueType& v) { set(v); }
|
2019-07-13 04:10:21 +08:00
|
|
|
|
2020-06-10 08:33:41 +08:00
|
|
|
Notified(Notified&& r) noexcept : waiting(std::move(r.waiting)), val(std::move(r.val)) {}
|
|
|
|
void operator=(Notified&& r) noexcept {
|
2019-11-14 05:30:34 +08:00
|
|
|
waiting = std::move(r.waiting);
|
|
|
|
val = std::move(r.val);
|
|
|
|
}
|
2019-07-13 04:10:21 +08:00
|
|
|
|
|
|
|
private:
|
2019-11-14 05:30:34 +08:00
|
|
|
using Item = std::pair<ValueType, Promise<Void>>;
|
2019-07-13 04:10:21 +08:00
|
|
|
struct ItemCompare {
|
|
|
|
bool operator()(const Item& a, const Item& b) { return a.first > b.first; }
|
|
|
|
};
|
|
|
|
std::priority_queue<Item, std::vector<Item>, ItemCompare> waiting;
|
2019-11-14 05:30:34 +08:00
|
|
|
T val;
|
2019-07-13 04:10:21 +08:00
|
|
|
};
|
|
|
|
|
2019-11-14 05:30:34 +08:00
|
|
|
using NotifiedVersion = Notified<VersionMetricHandle, VersionMetricHandle::ValueType>;
|
|
|
|
using NotifiedDouble = Notified<double>;
|
|
|
|
|
2017-07-15 06:49:30 +08:00
|
|
|
#endif
|