Skip to content

Commit

Permalink
Rename variables latest_data_id_→top_request_id_, current_data_id_→cu…
Browse files Browse the repository at this point in the history
…rrent_request_id_.

#codehealth

PiperOrigin-RevId: 618059466
  • Loading branch information
hiroyuki-komatsu committed Mar 22, 2024
1 parent ac2303b commit 0db4759
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
35 changes: 18 additions & 17 deletions src/engine/data_loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,16 @@ uint64_t DataLoader::RegisterRequest(const EngineReloadRequest &request) {

absl::WriterMutexLock lock(&mutex_);

// Already requesting latest id.
if (!requests_.empty() && requests_.front().id == id) {
latest_data_id_ = id;
return latest_data_id_;
// Already requesting the top priority request.
if (top_request_id_ == id) {
DCHECK(!requests_.empty() && requests_.front().id == id);
return top_request_id_;
}

// The request is invalid since it has already been unregistered.
if (unregistered_.contains(id)) {
latest_data_id_ = requests_.empty() ? 0 : requests_.front().id;
return latest_data_id_;
DCHECK_EQ(top_request_id_, requests_.empty() ? 0 : requests_.front().id);
return top_request_id_;
}

++sequence_id_;
Expand All @@ -109,8 +109,8 @@ uint64_t DataLoader::RegisterRequest(const EngineReloadRequest &request) {
lhs.sequence_id > rhs.sequence_id));
});

latest_data_id_ = requests_.front().id;
return latest_data_id_;
top_request_id_ = requests_.front().id;
return top_request_id_;
}

uint64_t DataLoader::ReportLoadFailure(uint64_t id) {
Expand All @@ -123,9 +123,10 @@ uint64_t DataLoader::ReportLoadFailure(uint64_t id) {
requests_.erase(it, requests_.end());
unregistered_.emplace(id);
}
const uint64_t rollback_id = requests_.empty() ? 0 : requests_.front().id;
latest_data_id_.compare_exchange_strong(id, rollback_id);
return rollback_id;

// Update the top request ID from the remaining sorted requests.
top_request_id_ = requests_.empty() ? 0 : requests_.front().id;
return top_request_id_;
}

namespace {
Expand Down Expand Up @@ -209,17 +210,17 @@ std::unique_ptr<DataLoader::ResponseFuture> DataLoader::Build(

void DataLoader::MaybeBuildNewData() {
// Checks if an existing build process, or already using the top request.
if (loader_response_future_ || current_data_id_ == latest_data_id_ ||
latest_data_id_ == 0) {
if (loader_response_future_ || current_request_id_ == top_request_id_ ||
top_request_id_ == 0) {
return;
}

LOG(INFO) << "Building a new module (current_data_id_=" << current_data_id_
<< ", latest_data_id_=" << latest_data_id_ << ")";
loader_response_future_ = Build(latest_data_id_);
LOG(INFO) << "Building a new module (current ID =" << current_request_id_
<< ", top ID =" << top_request_id_ << ")";
loader_response_future_ = Build(top_request_id_);

// Waits the engine if the no new engine is loaded so far.
if (current_data_id_ == 0 || always_wait_for_loader_response_future_) {
if (current_request_id_ == 0 || always_wait_for_loader_response_future_) {
loader_response_future_->Wait();
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/engine/data_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class DataLoader {
uint64_t ReportLoadFailure(uint64_t id);

// Sets the id of DataLoader::Response as the ID of the currently using data.
void ReportLoadSuccess(uint64_t id) { current_data_id_ = id; }
void ReportLoadSuccess(uint64_t id) { current_request_id_ = id; }

// Builds the new engine associated with `id`.
// This method returns the future object immediately.
Expand Down Expand Up @@ -122,9 +122,14 @@ class DataLoader {
absl::flat_hash_set<uint64_t> unregistered_ ABSL_GUARDED_BY(mutex_);
std::vector<RequestData> requests_ ABSL_GUARDED_BY(mutex_);

// Id of the highest priority request in the registered requests.
// 0 means that no request have been registered or valid yet.
std::atomic<uint64_t> top_request_id_ = 0;

// Id of the request for the current data.
// 0 means that no data has been updated yet.
std::atomic<uint64_t> latest_data_id_ = 0;
std::atomic<uint64_t> current_data_id_ = 0;
std::atomic<uint64_t> current_request_id_ = 0;

std::unique_ptr<DataLoader::ResponseFuture> loader_response_future_;
// used only in unittest to perform blocking behavior.
bool always_wait_for_loader_response_future_ = false;
Expand Down

0 comments on commit 0db4759

Please sign in to comment.