2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* genericactors.actor.cpp
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-08-11 07:05:40 +08:00
|
|
|
#include "flow/flow.h"
|
2021-05-13 15:51:19 +08:00
|
|
|
#include "flow/UnitTest.h"
|
2021-03-11 02:06:03 +08:00
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<bool> allTrue(std::vector<Future<bool>> all) {
|
|
|
|
state int i = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
while (i != all.size()) {
|
2021-03-11 02:06:03 +08:00
|
|
|
bool r = wait(all[i]);
|
|
|
|
if (!r)
|
|
|
|
return false;
|
2017-05-26 04:48:44 +08:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<Void> anyTrue(std::vector<Reference<AsyncVar<bool>>> input, Reference<AsyncVar<bool>> output) {
|
2018-02-03 03:46:04 +08:00
|
|
|
loop {
|
|
|
|
bool oneTrue = false;
|
|
|
|
std::vector<Future<Void>> changes;
|
2021-03-11 02:06:03 +08:00
|
|
|
for (auto it : input) {
|
|
|
|
if (it->get())
|
|
|
|
oneTrue = true;
|
|
|
|
changes.push_back(it->onChange());
|
2018-02-03 03:46:04 +08:00
|
|
|
}
|
2021-03-11 02:06:03 +08:00
|
|
|
output->set(oneTrue);
|
|
|
|
wait(waitForAny(changes));
|
2018-02-03 03:46:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<Void> cancelOnly(std::vector<Future<Void>> futures) {
|
|
|
|
// We don't do anything with futures except hold them, we never return, but if we are cancelled we (naturally) drop
|
|
|
|
// the futures
|
|
|
|
wait(Never());
|
2017-05-26 04:48:44 +08:00
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<Void> timeoutWarningCollector(FutureStream<Void> input, double logDelay, const char* context, UID id) {
|
2017-05-26 04:48:44 +08:00
|
|
|
state uint64_t counter = 0;
|
2021-03-11 02:06:03 +08:00
|
|
|
state Future<Void> end = delay(logDelay);
|
2017-05-26 04:48:44 +08:00
|
|
|
loop choose {
|
2021-03-11 02:06:03 +08:00
|
|
|
when(waitNext(input)) { counter++; }
|
|
|
|
when(wait(end)) {
|
|
|
|
if (counter)
|
2017-05-26 04:48:44 +08:00
|
|
|
TraceEvent(SevWarn, context, id).detail("LateProcessCount", counter).detail("LoggingDelay", logDelay);
|
2021-03-11 02:06:03 +08:00
|
|
|
end = delay(logDelay);
|
2017-05-26 04:48:44 +08:00
|
|
|
counter = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<bool> quorumEqualsTrue(std::vector<Future<bool>> futures, int required) {
|
|
|
|
state std::vector<Future<Void>> true_futures;
|
|
|
|
state std::vector<Future<Void>> false_futures;
|
2020-04-25 03:21:35 +08:00
|
|
|
true_futures.reserve(futures.size());
|
|
|
|
false_futures.reserve(futures.size());
|
2021-03-11 02:06:03 +08:00
|
|
|
for (int i = 0; i < futures.size(); i++) {
|
|
|
|
true_futures.push_back(onEqual(futures[i], true));
|
|
|
|
false_futures.push_back(onEqual(futures[i], false));
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
choose {
|
2021-03-11 02:06:03 +08:00
|
|
|
when(wait(quorum(true_futures, required))) { return true; }
|
|
|
|
when(wait(quorum(false_futures, futures.size() - required + 1))) { return false; }
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
}
|
2019-06-21 00:29:01 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<bool> shortCircuitAny(std::vector<Future<bool>> f) {
|
2019-06-21 00:29:01 +08:00
|
|
|
std::vector<Future<Void>> sc;
|
2020-04-25 03:21:35 +08:00
|
|
|
sc.reserve(f.size());
|
2021-03-11 02:06:03 +08:00
|
|
|
for (Future<bool> fut : f) {
|
2019-06-21 00:29:01 +08:00
|
|
|
sc.push_back(returnIfTrue(fut));
|
|
|
|
}
|
|
|
|
|
|
|
|
choose {
|
2021-03-11 02:06:03 +08:00
|
|
|
when(wait(waitForAll(f))) {
|
2019-06-21 00:29:01 +08:00
|
|
|
// Handle a possible race condition? If the _last_ term to
|
|
|
|
// be evaluated triggers the waitForAll before bubbling
|
|
|
|
// out of the returnIfTrue quorum
|
2020-04-25 03:21:35 +08:00
|
|
|
for (const auto& fut : f) {
|
2021-03-11 02:06:03 +08:00
|
|
|
if (fut.get()) {
|
2019-06-21 00:29:01 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-11 02:06:03 +08:00
|
|
|
when(wait(waitForAny(sc))) { return true; }
|
2019-06-21 00:29:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
Future<Void> orYield(Future<Void> f) {
|
|
|
|
if (f.isReady()) {
|
|
|
|
if (f.isError())
|
2019-06-21 00:29:01 +08:00
|
|
|
return tagError<Void>(yield(), f.getError());
|
|
|
|
else
|
|
|
|
return yield();
|
2021-03-11 02:06:03 +08:00
|
|
|
} else
|
2019-06-21 00:29:01 +08:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<Void> returnIfTrue(Future<bool> f) {
|
|
|
|
bool b = wait(f);
|
|
|
|
if (b) {
|
2019-06-21 00:29:01 +08:00
|
|
|
return Void();
|
|
|
|
}
|
2021-03-11 02:06:03 +08:00
|
|
|
wait(Never());
|
2019-06-21 00:29:01 +08:00
|
|
|
throw internal_error();
|
|
|
|
}
|
2020-02-22 11:09:16 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<Void> lowPriorityDelay(double waitTime) {
|
2020-02-20 08:48:30 +08:00
|
|
|
state int loopCount = 0;
|
2021-03-11 02:06:03 +08:00
|
|
|
state int totalLoops =
|
|
|
|
std::max<int>(waitTime / FLOW_KNOBS->LOW_PRIORITY_MAX_DELAY, FLOW_KNOBS->LOW_PRIORITY_DELAY_COUNT);
|
2021-01-28 03:50:42 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
while (loopCount < totalLoops) {
|
|
|
|
wait(delay(waitTime / totalLoops, TaskPriority::Low));
|
2020-02-20 08:48:30 +08:00
|
|
|
loopCount++;
|
|
|
|
}
|
|
|
|
return Void();
|
|
|
|
}
|
2021-05-13 15:51:19 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct DummyState {
|
|
|
|
int changed{ 0 };
|
|
|
|
int unchanged{ 0 };
|
|
|
|
bool operator==(DummyState const& rhs) const { return changed == rhs.changed && unchanged == rhs.unchanged; }
|
|
|
|
bool operator!=(DummyState const& rhs) const { return !(*this == rhs); }
|
|
|
|
};
|
|
|
|
|
|
|
|
ACTOR Future<Void> testPublisher(Reference<AsyncVar<DummyState>> input) {
|
|
|
|
state int i = 0;
|
|
|
|
for (; i < 100; ++i) {
|
|
|
|
wait(delay(deterministicRandom()->random01()));
|
|
|
|
auto var = input->get();
|
|
|
|
++var.changed;
|
|
|
|
input->set(var);
|
|
|
|
}
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
|
|
|
ACTOR Future<Void> testSubscriber(Reference<IDependentAsyncVar<int>> output, Optional<int> expected) {
|
|
|
|
loop {
|
|
|
|
wait(output->onChange());
|
|
|
|
ASSERT(expected.present());
|
|
|
|
if (output->get() == expected.get()) {
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST_CASE("/flow/genericactors/DependentAsyncVar") {
|
|
|
|
auto input = makeReference<AsyncVar<DummyState>>();
|
|
|
|
state Future<Void> subscriber1 =
|
|
|
|
testSubscriber(IDependentAsyncVar<int>::create(input, [](auto const& var) { return var.changed; }), 100);
|
|
|
|
state Future<Void> subscriber2 =
|
|
|
|
testSubscriber(IDependentAsyncVar<int>::create(input, [](auto const& var) { return var.unchanged; }), {});
|
|
|
|
wait(subscriber1 && testPublisher(input));
|
2021-06-17 02:34:20 +08:00
|
|
|
ASSERT(!subscriber2.isReady());
|
2021-05-13 15:51:19 +08:00
|
|
|
return Void();
|
|
|
|
}
|