foundationdb/flow/genericactors.actor.cpp

86 lines
2.5 KiB
C++
Raw Normal View History

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
*
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
*
2017-05-26 04:48:44 +08:00
* http://www.apache.org/licenses/LICENSE-2.0
*
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.
*/
#include "flow/flow.h"
#include "flow/actorcompiler.h" // This must be the last #include.
2017-05-26 04:48:44 +08:00
ACTOR Future<bool> allTrue( std::vector<Future<bool>> all ) {
state int i=0;
while (i != all.size()) {
bool r = wait( all[i] );
if (!r) return false;
i++;
}
return true;
}
ACTOR Future<Void> anyTrue( std::vector<Reference<AsyncVar<bool>>> input, Reference<AsyncVar<bool>> output ) {
loop {
bool oneTrue = false;
std::vector<Future<Void>> changes;
for(auto it : input) {
if( it->get() ) oneTrue = true;
changes.push_back( it->onChange() );
}
output->set( oneTrue );
wait( waitForAny(changes) );
}
}
2017-05-26 04:48:44 +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();
}
ACTOR Future<Void> timeoutWarningCollector( FutureStream<Void> input, double logDelay, const char* context, UID id ) {
state uint64_t counter = 0;
state Future<Void> end = delay( logDelay );
loop choose {
when ( waitNext( input ) ) {
2017-05-26 04:48:44 +08:00
counter++;
}
when ( wait( end ) ) {
2017-05-26 04:48:44 +08:00
if( counter )
TraceEvent(SevWarn, context, id).detail("LateProcessCount", counter).detail("LoggingDelay", logDelay);
end = delay( logDelay );
counter = 0;
}
}
}
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;
for(int i=0; i<futures.size(); i++) {
true_futures.push_back( onEqual( futures[i], true ) );
false_futures.push_back( onEqual( futures[i], false ) );
}
choose {
when( wait( quorum( true_futures, required ) ) ) {
2017-05-26 04:48:44 +08:00
return true;
}
when( wait( quorum( false_futures, futures.size() - required + 1 ) ) ) {
2017-05-26 04:48:44 +08:00
return false;
}
}
}