Skip to content

Fix metrics upload race condition - #638

Merged
zachjhum merged 2 commits into
masterfrom
metrics-upload-fix
May 15, 2025
Merged

Fix metrics upload race condition#638
zachjhum merged 2 commits into
masterfrom
metrics-upload-fix

Conversation

@zachjhum

@zachjhum zachjhum commented May 13, 2025

Copy link
Copy Markdown
Contributor

Issue #, if available: #188

Description of changes: Fixes a race condition between the metrics manager and the metrics accumulator that could cause missed data points. If these were the only data points in the last 60 seconds, this could cause CloudWatch to fail the PutMetricData request as it would try to upload the default values in C++ Boost accumulators.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Comment thread aws/metrics/metrics_manager.cc Outdated
Comment on lines +140 to +145

for (auto& m : uploads) {
m->accumulator().flush(end);
}

upload_checkpoint_ = end;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

They should be outside of the for loop

std::vector<std::shared_ptr<Metric>> uploads;

TimePoint begin = upload_checkpoint_;
TimePoint end = Clock::now() - std::chrono::seconds(1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's start adding comments for the changes. It will help readers

Comment thread aws/metrics/accumulator.h
@@ -136,20 +152,20 @@ class AccumulatorList {
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

buckets_between is not used so it can be removed

Comment thread aws/metrics/accumulator.h
return std::pair<TimePoint, TimePoint>(end - BucketSize(buckets), end);
}

static inline TimePoint current_time() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe combine buckets_since_epoch with current_time. Please add some comments for the newly added comments

Comment thread aws/metrics/accumulator.h
Comment on lines +132 to +138
static inline std::pair<TimePoint, TimePoint> range(size_t buckets) {
return range(buckets, current_time());
}

static inline std::pair<TimePoint, TimePoint> range(size_t buckets, TimePoint end) {
return std::pair<TimePoint, TimePoint>(end - BucketSize(buckets), end);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These methods shouldn't be needed once we remove the original usage of BucketSize based timepoint calculation. Please add a TODO here to clean these up.

Comment thread aws/metrics/accumulator.h
}
}

void flush(TimePoint checkpoint) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can we have a test for this?

…load fails or severely stalls and add flush accumulator unit test

std::shared_ptr<aws::utils::ScheduledCallback> scheduled_upload_;
TimePoint upload_checkpoint_;
TimePoint next_run_;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit : better name for this variable

@zachjhum
zachjhum marked this pull request as ready for review May 15, 2025 17:06
@zachjhum
zachjhum merged commit 8267d92 into master May 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants