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

[chore](test) Add testing util sync point #28924

Merged
merged 2 commits into from
Dec 24, 2023
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
236 changes: 236 additions & 0 deletions be/src/common/sync_point.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// 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.

// Most code of this file is copied from rocksdb SyncPoint.
// https://github.com/facebook/rocksdb

// clang-format off
#include "sync_point.h"

#include <atomic>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <random>
#include <string>
#include <thread>
#include <unordered_map>
#include <unordered_set>

namespace doris {

struct SyncPoint::Data { // impl
public:
Data() : enabled_(false) { }
virtual ~Data() {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: use '= default' to define a trivial destructor [modernize-use-equals-default]

Suggested change
virtual ~Data() {}
virtual ~Data() = default;

void process(const std::string& point, std::vector<std::any>&& cb_args);
void load_dependency(const std::vector<SyncPointPair>& dependencies);
void load_dependency_and_markers(
const std::vector<SyncPointPair>& dependencies,
const std::vector<SyncPointPair>& markers);
bool predecessors_all_cleared(const std::string& point);
void set_call_back(const std::string& point,
const std::function<void(std::vector<std::any>&&)>& callback);
void clear_call_back(const std::string& point);
void clear_all_call_backs();
void enable_processing();
void disable_processing();
void clear_trace();
private:
bool disable_by_marker(const std::string& point, std::thread::id thread_id);
private:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: redundant access specifier has the same accessibility as the previous access specifier [readability-redundant-access-specifiers]

Suggested change
private:
Additional context

be/src/common/sync_point.cpp:53: previously declared here

private:
^

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: redundant access specifier has the same accessibility as the previous access specifier [readability-redundant-access-specifiers]

Suggested change
private:
Additional context

be/src/common/sync_point.cpp:52: previously declared here

private:
^

// successor/predecessor map loaded from load_dependency
std::unordered_map<std::string, std::vector<std::string>> successors_;
std::unordered_map<std::string, std::vector<std::string>> predecessors_;
std::unordered_map<std::string, std::function<void(std::vector<std::any>&&)>> callbacks_;
std::unordered_map<std::string, std::vector<std::string>> markers_;
std::unordered_map<std::string, std::thread::id> marked_thread_id_;
std::mutex mutex_;
std::condition_variable cv_;
// sync points that have been passed through
std::unordered_set<std::string> cleared_points_;
std::atomic<bool> enabled_;
int num_callbacks_running_ = 0;
};

SyncPoint* SyncPoint::get_instance() {
static SyncPoint sync_point;
return &sync_point;
}
SyncPoint::SyncPoint() :
impl_(new Data) {
}
SyncPoint:: ~SyncPoint() {
delete impl_;
}
void SyncPoint::load_dependency(const std::vector<SyncPointPair>& dependencies) {
impl_->load_dependency(dependencies);
}
void SyncPoint::load_dependency_and_markers(
const std::vector<SyncPointPair>& dependencies,
const std::vector<SyncPointPair>& markers) {
impl_->load_dependency_and_markers(dependencies, markers);
}
void SyncPoint::set_call_back(const std::string& point,
const std::function<void(std::vector<std::any>&&)>& callback) {
impl_->set_call_back(point, callback);
}
void SyncPoint::clear_call_back(const std::string& point) {
impl_->clear_call_back(point);
}
void SyncPoint::clear_all_call_backs() {
impl_->clear_all_call_backs();
}
void SyncPoint::enable_processing() {
impl_->enable_processing();
}
void SyncPoint::disable_processing() {
impl_->disable_processing();
}
void SyncPoint::clear_trace() {
impl_->clear_trace();
}
void SyncPoint::process(const std::string& point, std::vector<std::any>&& cb_arg) {
impl_->process(point, std::move(cb_arg));
}

// =============================================================================
// SyncPoint implementation
// =============================================================================

void SyncPoint::Data::load_dependency(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: method 'load_dependency' can be made static [readability-convert-member-functions-to-static]

Suggested change
void SyncPoint::Data::load_dependency(
static void SyncPoint::Data::load_dependency(

const std::vector<SyncPointPair>& dependencies) {
std::lock_guard lock(mutex_);
successors_.clear();
predecessors_.clear();
cleared_points_.clear();
for (const auto& dependency : dependencies) {
successors_[dependency.predecessor].push_back(dependency.successor);
predecessors_[dependency.successor].push_back(dependency.predecessor);
}
cv_.notify_all();
}

/**
* Markers are also dependency descriptions
*/
void SyncPoint::Data::load_dependency_and_markers(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: method 'load_dependency_and_markers' can be made static [readability-convert-member-functions-to-static]

Suggested change
void SyncPoint::Data::load_dependency_and_markers(
static void SyncPoint::Data::load_dependency_and_markers(

const std::vector<SyncPointPair>& dependencies,
const std::vector<SyncPointPair>& markers) {
std::lock_guard lock(mutex_);
successors_.clear();
predecessors_.clear();
cleared_points_.clear();
markers_.clear();
marked_thread_id_.clear();
for (const auto& dependency : dependencies) {
successors_[dependency.predecessor].push_back(dependency.successor);
predecessors_[dependency.successor].push_back(dependency.predecessor);
}
for (const auto& marker : markers) {
successors_[marker.predecessor].push_back(marker.successor);
predecessors_[marker.successor].push_back(marker.predecessor);
markers_[marker.predecessor].push_back(marker.successor);
}
cv_.notify_all();
}

bool SyncPoint::Data::predecessors_all_cleared(const std::string& point) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: method 'predecessors_all_cleared' can be made static [readability-convert-member-functions-to-static]

be/src/common/sync_point.cpp:45:

-   bool predecessors_all_cleared(const std::string& point);
+   static bool predecessors_all_cleared(const std::string& point);

for (const auto& pred : predecessors_[point]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: replace loop by 'std::ranges::all_of()' [readability-use-anyofallof]

  for (const auto& pred : predecessors_[point]) {
  ^

if (cleared_points_.count(pred) == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: use 'contains' to check for membership [readability-container-contains]

Suggested change
if (cleared_points_.count(pred) == 0) {
if (!cleared_points_.contains(pred)) {

return false;
}
}
return true;
}

void SyncPoint::Data::clear_call_back(const std::string& point) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: method 'clear_call_back' can be made static [readability-convert-member-functions-to-static]

be/src/common/sync_point.cpp:48:

-   void clear_call_back(const std::string& point);
+   static void clear_call_back(const std::string& point);

std::unique_lock lock(mutex_);
callbacks_.erase(point);
}

void SyncPoint::Data::clear_all_call_backs() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: method 'clear_all_call_backs' can be made static [readability-convert-member-functions-to-static]

be/src/common/sync_point.cpp:49:

-   void clear_all_call_backs();
+   static void clear_all_call_backs();

std::unique_lock lock(mutex_);
callbacks_.clear();
}

void SyncPoint::Data::process(const std::string& point, std::vector<std::any>&& cb_arg) {
if (!enabled_) {
return;
}
std::unique_lock lock(mutex_);
auto thread_id = std::this_thread::get_id();
auto marker_iter = markers_.find(point);
// if current sync point is a marker
// record it in marked_thread_id_ for all its successors
if (marker_iter != markers_.end()) {
for (auto& marked_point : marker_iter->second) {
marked_thread_id_.emplace(marked_point, thread_id);
}
}
// if current point is a marker's successor
if (disable_by_marker(point, thread_id)) {
return;
}
while (!predecessors_all_cleared(point)) {
cv_.wait(lock);
if (disable_by_marker(point, thread_id)) {
return;
}
}
auto callback_pair = callbacks_.find(point);
if (callback_pair != callbacks_.end()) {
num_callbacks_running_++;
auto callback = callback_pair->second;
mutex_.unlock();
callback(std::move(cb_arg));
mutex_.lock();
num_callbacks_running_--;
}
cleared_points_.insert(point);
cv_.notify_all();
}

bool SyncPoint::Data::disable_by_marker(const std::string& point,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: method 'disable_by_marker' can be made static [readability-convert-member-functions-to-static]

be/src/common/sync_point.cpp:54:

-   bool disable_by_marker(const std::string& point, std::thread::id thread_id);
+   static bool disable_by_marker(const std::string& point, std::thread::id thread_id);

std::thread::id thread_id) {
auto marked_point_iter = marked_thread_id_.find(point);
return marked_point_iter != marked_thread_id_.end() // is a successor
&& thread_id != marked_point_iter->second;
}

void SyncPoint::Data::set_call_back(const std::string& point,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: method 'set_call_back' can be made static [readability-convert-member-functions-to-static]

be/src/common/sync_point.cpp:46:

-   void set_call_back(const std::string& point,
+   static void set_call_back(const std::string& point,

const std::function<void(std::vector<std::any>&&)>& callback) {
std::lock_guard lock(mutex_);
callbacks_[point] = callback;
}

void SyncPoint::Data::clear_trace() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: method 'clear_trace' can be made static [readability-convert-member-functions-to-static]

be/src/common/sync_point.cpp:52:

-   void clear_trace();
+   static void clear_trace();

std::lock_guard lock(mutex_);
cleared_points_.clear();
}

void SyncPoint::Data::enable_processing() {
enabled_ = true;
}

void SyncPoint::Data::disable_processing() {
enabled_ = false;
}

} // namespace doris
// clang-format on
// vim: et tw=80 ts=2 sw=2 cc=80:
Loading
Loading