8000 [R2] Log Multiplexer by maierlars · Pull Request #14667 · arangodb/arangodb · GitHub
[go: up one dir, main page]

Skip to content

[R2] Log Multiplexer #14667

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix resolvePromise code wth bug in clang.
  • Loading branch information
maierlars committed Aug 23, 2021
commit d7e8e615a018f72124119353211aaf1a728219d0
31 changes: 25 additions & 6 deletions arangod/Replication2/Streams/LogMultiplexer.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,32 @@
namespace arangodb::replication2::streams {

namespace {

template <typename Descriptor, typename Queue, typename Result, typename Block = StreamInformationBlock<Descriptor>>
auto allUnresolved(std::pair<Queue, Result>& q) {
return std::all_of(std::begin(q.first), std::end(q.first),
[&](auto const& pair) { return !pair.second.isFulfilled(); });
}
template <typename Descriptor, typename Queue, typename Result, typename Block = StreamInformationBlock<Descriptor>>
auto resolvePromiseSet(std::pair<Queue, Result>& q) {
std::for_each(std::begin(q.first), std::end(q.first),
[&](auto& pair) { pair.second.setValue(q.second); });
TRI_ASSERT(allUnresolved<Descriptor>(q));
std::for_each(std::begin(q.first), std::end(q.first), [&](auto& pair) {
TRI_ASSERT(!pair.second.isFulfilled());
if (!pair.second.isFulfilled()) {
pair.second.setValue(q.second);
}
});
}

template <typename... Descriptors, typename... Pairs, std::size_t... Idxs>
auto resolvePromiseSets(stream_descriptor_set<Descriptors...>, std::index_sequence<Idxs...>, std::tuple<Pairs...>& pairs) {
(resolvePromiseSet<Descriptors>(std::get<Idxs>(pairs)), ...);
}

template <typename... Descriptors, typename... Pairs>
auto resolvePromiseSets(stream_descriptor_set<Descriptors...>, std::tuple<Pairs...>& pairs) {
(resolvePromiseSet<Descriptors>(std::get<Pairs>(pairs)), ...);
TRI_ASSERT((allUnresolved<Descriptors>(std::get<Pairs>(pairs)) && ...));
resolvePromiseSets(stream_descriptor_set<Descriptors...>{}, std::index_sequence_for<Descriptors...>{}, pairs);

}
} // namespace

Expand Down Expand Up @@ -234,7 +250,9 @@ struct LogMultiplexerImplementation
return futures::Future<W>{std::in_place};
}
auto& block = std::get<StreamInformationBlock<StreamDescriptor>>(self._blocks);
return block.registerWaitFor(index);
return block.registerWaitFor(index).thenValue([index](auto&& value) {
return std::forward<decltype(value)>(value);
});
});
}

Expand Down Expand Up @@ -275,7 +293,8 @@ struct LogMultiplexerImplementation

void triggerWaitForIndex(LogIndex waitForIndex) {
auto f = _interface->waitFor(waitForIndex);
std::move(f).thenValue([weak = this->weak_from_this()](replicated_log::WaitForResult&& result) {
std::move(f).thenValue([weak = this->weak_from_this()](
replicated_log::WaitForResult&& result) noexcept {
// First lock the shared pointer
if (auto locked = weak.lock(); locked) {
auto that = std::static_pointer_cast<SelfClass>(locked);
Expand Down
41 changes: 34 additions & 7 deletions tests/Replication2/Streams/MultiplexerConcurrencyTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <gtest/gtest.h>

#include <Basics/VelocyPackHelper.h>
#include <Replication2/Mocks/AsyncFollower.h>
#include <utility>

Expand Down Expand Up @@ -56,7 +57,7 @@ struct LogMultiplexerConcurrencyTest : LogMultiplexerTestBase {
auto [start, stop] = iter->range();
TRI_ASSERT(start != stop);
while (auto memtry = iter->next()) {
self->_state.emplace(*memtry);
self->_observedLog.emplace(*memtry);
}
self->waitForStream(stop);
} else {
Expand All @@ -65,7 +66,7 @@ struct LogMultiplexerConcurrencyTest : LogMultiplexerTestBase {
});
}

std::map<LogIndex, ValueType> _state;
std::map<LogIndex, ValueType> _observedLog;
std::shared_ptr<streams::Stream<ValueType>> _stream;
};

Expand Down Expand Up @@ -122,11 +123,37 @@ TEST_F(LogMultiplexerConcurrencyTest, test) {
auto leaderInstance = std::make_shared<LeaderInstance>(leader);

auto producer = leaderInstance->_mux->getStreamById<test::my_int_stream_id>();
auto index = LogIndex{0};
for (std::size_t i = 0; i < 100000; i++) {
index = producer->insert(i);
}

leader->waitFor(index).wait();
constexpr std::size_t num_threads = 5;
constexpr std::size_t num_inserts_per_thread = 10000;

std::vector<std::thread> threads;
std::generate_n(std::back_inserter(threads), num_threads, [&]{
return std::thread([producer]{
auto index = LogIndex{0};
for (std::size_t i = 0; i < num_inserts_per_thread; i++) {
index = producer->insert(i);
}
producer->waitFor(index).wait();
});
});

std::for_each(std::begin(threads), std::end(threads), [](std::thread& t) {
t.join();
});
asyncFollower->waitFor(LogIndex{num_threads * num_inserts_per_thread + 1}).wait();
asyncFollower->stop();

auto iterA = follower->waitForIterator(LogIndex{1}).get();
auto iterB = leader->waitForIterator(LogIndex{1}).get();

EXPECT_EQ(iterA->range(), iterB->range());
while (auto A = iterA->next()) {
auto B = iterB->next();
ASSERT_TRUE(B.has_value());
EXPECT_EQ(A->logIndex(), B->logIndex());
bool equal = basics::VelocyPackHelper::equal(A->logPayload(), B->logPayload(), true);
EXPECT_TRUE(equal) << A->logPayload().toJson() << " " << B->logPayload().toJson();
}
EXPECT_FALSE(iterB->next().has_value());
}
0