Skip to content
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

BTS-519: avoid holding 2 mutexes at the same time #14511

Merged
merged 2 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions arangod/Pregel/Conductor.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ class Conductor : public std::enable_shared_from_this<Conductor> {
}

bool canBeGarbageCollected() const;

uint64_t executionNumber() const { return _executionNumber; }

private:
void cancelNoLock();
Expand Down
52 changes: 38 additions & 14 deletions arangod/Pregel/PregelFeature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,19 +335,34 @@ std::shared_ptr<Conductor> PregelFeature::conductor(uint64_t executionNumber) {

void PregelFeature::garbageCollectConductors() try {
// iterate over all conductors and remove the ones which can be garbage-collected
MUTEX_LOCKER(guard, _mutex);
for (auto it = _conductors.begin(); it != _conductors.end(); /*no hoisting*/) {
if (it->second.conductor->canBeGarbageCollected()) {
uint64_t executionNumber = it->first;

it->second.conductor->cancel();
it = _conductors.erase(it);

_workers.erase(executionNumber);
} else {
++it;
std::vector<std::shared_ptr<Conductor>> conductors;

// copy out shared-ptrs of Conductors under the mutex
{
MUTEX_LOCKER(guard, _mutex);
for (auto const& it : _conductors) {
if (it.second.conductor->canBeGarbageCollected()) {
if (conductors.empty()) {
conductors.reserve(8);
}
conductors.emplace_back(it.second.conductor);
}
}
}

// cancel and kill conductors without holding the mutex
// permanently
for (auto& c : conductors) {
c->cancel();
}

MUTEX_LOCKER(guard, _mutex);
for (auto& c : conductors) {
uint64_t executionNumber = c->executionNumber();

_conductors.erase(executionNumber);
_workers.erase(executionNumber);
}
} catch (...) {}

void PregelFeature::addWorker(std::shared_ptr<IWorker>&& w, uint64_t executionNumber) {
Expand Down Expand Up @@ -521,21 +536,30 @@ uint64_t PregelFeature::numberOfActiveConductors() const {
Result PregelFeature::toVelocyPack(TRI_vocbase_t& vocbase,
arangodb::velocypack::Builder& result,
bool allDatabases, bool fanout) const {
Result res;
std::vector<std::shared_ptr<Conductor>> conductors;

result.openArray();
// make a copy of all conductor shared-ptrs under the mutex
{
MUTEX_LOCKER(guard, _mutex);
conductors.reserve(_conductors.size());

for (auto const& p : _conductors) {
auto const& ce = p.second;
if (!::authorized(ce.user)) {
continue;
}

ce.conductor->toVelocyPack(result);
conductors.emplace_back(ce.conductor);
}
}

// release lock, and now velocypackify all conductors
result.openArray();
for (auto const& c : conductors) {
c->toVelocyPack(result);
}

Result res;

if (ServerState::instance()->isCoordinator() && fanout) {
// coordinator case, fan out to other coordinators!
Expand Down