Skip to content

Commit 22032c6

Browse files
mbasmanovafacebook-github-bot
authored andcommitted
Replace std::mutex with folly::Synchronized (facebookincubator#83)
Summary: Pull Request resolved: facebookincubator#83 Reviewed By: pedroerp Differential Revision: D30436398 Pulled By: mbasmanova fbshipit-source-id: 6fa473db871cd4f78961eb56bc9c5f8460758c70
1 parent d4d18cf commit 22032c6

File tree

2 files changed

+63
-57
lines changed

2 files changed

+63
-57
lines changed

velox/exec/PartitionedOutputBufferManager.cpp

+60-55
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,12 @@ PartitionedOutputBufferManager::getInstance(const std::string& host) {
423423

424424
std::shared_ptr<PartitionedOutputBuffer>
425425
PartitionedOutputBufferManager::getBuffer(const std::string& taskId) {
426-
std::lock_guard<std::mutex> l(mutex_);
427-
auto it = buffers_.find(taskId);
428-
VELOX_CHECK(
429-
it != buffers_.end(), "Output buffers for task not found: {}", taskId);
430-
return it->second;
426+
return buffers_.withLock([&](auto& buffers) {
427+
auto it = buffers.find(taskId);
428+
VELOX_CHECK(
429+
it != buffers.end(), "Output buffers for task not found: {}", taskId);
430+
return it->second;
431+
});
431432
}
432433

433434
BlockingReason PartitionedOutputBufferManager::enqueue(
@@ -450,35 +451,34 @@ void PartitionedOutputBufferManager::acknowledge(
450451
const std::string& taskId,
451452
int destination,
452453
int64_t sequence) {
453-
std::shared_ptr<PartitionedOutputBuffer> buffer;
454-
{
455-
std::lock_guard<std::mutex> l(mutex_);
456-
auto it = buffers_.find(taskId);
457-
if (it == buffers_.end()) {
458-
VLOG(1) << "Receiving ack for non-existent task " << taskId
459-
<< " destination " << destination << " sequence " << sequence;
460-
return;
461-
}
462-
buffer = it->second;
454+
auto buffer = buffers_.withLock(
455+
[&](auto& buffers) -> std::shared_ptr<PartitionedOutputBuffer> {
456+
auto it = buffers.find(taskId);
457+
if (it == buffers.end()) {
458+
VLOG(1) << "Receiving ack for non-existent task " << taskId
459+
<< " destination " << destination << " sequence " << sequence;
460+
return nullptr;
461+
}
462+
return it->second;
463+
});
464+
if (buffer) {
465+
buffer->acknowledge(destination, sequence);
463466
}
464-
buffer->acknowledge(destination, sequence);
465467
}
466468

467469
void PartitionedOutputBufferManager::deleteResults(
468470
const std::string& taskId,
469471
int destination) {
470-
std::shared_ptr<PartitionedOutputBuffer> buffer;
471-
{
472-
std::lock_guard<std::mutex> l(mutex_);
473-
auto it = buffers_.find(taskId);
474-
if (it == buffers_.end()) {
475-
return;
476-
}
477-
buffer = it->second;
478-
}
479-
if (buffer->deleteResults(destination)) {
480-
std::lock_guard<std::mutex> l(mutex_);
481-
buffers_.erase(taskId);
472+
auto buffer = buffers_.withLock(
473+
[&](auto& buffers) -> std::shared_ptr<PartitionedOutputBuffer> {
474+
auto it = buffers.find(taskId);
475+
if (it == buffers.end()) {
476+
return nullptr;
477+
}
478+
return it->second;
479+
});
480+
if (buffer && buffer->deleteResults(destination)) {
481+
buffers_.withLock([&](auto& buffers) { buffers.erase(taskId); });
482482
}
483483
}
484484

@@ -498,15 +498,16 @@ void PartitionedOutputBufferManager::initializeTask(
498498
int numDrivers) {
499499
const auto& taskId = task->taskId();
500500

501-
std::lock_guard<std::mutex> l(mutex_);
502-
auto it = buffers_.find(taskId);
503-
if (it == buffers_.end()) {
504-
buffers_[taskId] = std::make_shared<PartitionedOutputBuffer>(
505-
std::move(task), broadcast, numDestinations, numDrivers);
506-
} else {
507-
VELOX_FAIL(
508-
"Registering an output buffer for pre-existing taskId {}", taskId);
509-
}
501+
buffers_.withLock([&](auto& buffers) {
502+
auto it = buffers.find(taskId);
503+
if (it == buffers.end()) {
504+
buffers[taskId] = std::make_shared<PartitionedOutputBuffer>(
505+
std::move(task), broadcast, numDestinations, numDrivers);
506+
} else {
507+
VELOX_FAIL(
508+
"Registering an output buffer for pre-existing taskId {}", taskId);
509+
}
510+
});
510511
}
511512

512513
void PartitionedOutputBufferManager::updateBroadcastOutputBuffers(
@@ -517,28 +518,32 @@ void PartitionedOutputBufferManager::updateBroadcastOutputBuffers(
517518
}
518519

519520
void PartitionedOutputBufferManager::removeTask(const std::string& taskId) {
520-
std::shared_ptr<PartitionedOutputBuffer> buffer;
521-
{
522-
std::lock_guard<std::mutex> l(mutex_);
523-
auto iter = buffers_.find(taskId);
524-
if (iter == buffers_.end()) {
525-
// Already removed.
526-
return;
527-
}
528-
buffer = iter->second;
529-
buffers_.erase(taskId);
521+
auto buffer = buffers_.withLock(
522+
[&](auto& buffers) -> std::shared_ptr<PartitionedOutputBuffer> {
523+
auto it = buffers.find(taskId);
524+
if (it == buffers.end()) {
525+
// Already removed.
526+
return nullptr;
527+
}
528+
auto taskBuffer = it->second;
529+
buffers.erase(taskId);
530+
return taskBuffer;
531+
});
532+
if (buffer) {
533+
buffer->terminate();
530534
}
531-
buffer->terminate();
532535
}
533536

534537
std::string PartitionedOutputBufferManager::toString() {
535-
std::stringstream out;
536-
out << "[BufferManager:" << std::endl;
537-
for (auto& pair : buffers_) {
538-
out << pair.first << ": " << pair.second->toString() << std::endl;
539-
}
540-
out << "]";
541-
return out.str();
538+
return buffers_.withLock([](const auto& buffers) {
539+
std::stringstream out;
540+
out << "[BufferManager:" << std::endl;
541+
for (const auto& pair : buffers) {
542+
out << pair.first << ": " << pair.second->toString() << std::endl;
543+
}
544+
out << "]";
545+
return out.str();
546+
});
542547
}
543548

544549
} // namespace facebook::velox::exec

velox/exec/PartitionedOutputBufferManager.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,9 @@ class PartitionedOutputBufferManager {
249249
// Retrieves the set of buffers for a query.
250250
std::shared_ptr<PartitionedOutputBuffer> getBuffer(const std::string& taskId);
251251

252-
std::mutex mutex_;
253-
std::unordered_map<std::string, std::shared_ptr<PartitionedOutputBuffer>>
252+
folly::Synchronized<
253+
std::unordered_map<std::string, std::shared_ptr<PartitionedOutputBuffer>>,
254+
std::mutex>
254255
buffers_;
255256
};
256257
} // namespace facebook::velox::exec

0 commit comments

Comments
 (0)