-
Notifications
You must be signed in to change notification settings - Fork 331
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
Use a shared_ptr and thread_local to store credentials for use #202
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7d4b310
Remove the use of put_time
pfifer 1701e4c
Change to straight shared_ptr
pfifer 1c591df
Sleep for 20ms between updates
pfifer c3fc559
Increase reader threads to 128 for the test
pfifer 1cb07d3
Re-enable all tests
pfifer 4f2d27f
Removed Debugging Stuff
pfifer 556e004
Trying a thread local version instead.
pfifer 23e4215
Remove unused headers
pfifer adbfd9b
Have the producer thread sleeps for 20ms between updates
pfifer 14be698
Add comments explaining the thread local approach.
pfifer 376ed8a
Added comments around how versioning is handled.
pfifer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,137 +15,36 @@ | |
#include <limits> | ||
|
||
namespace { | ||
#ifdef DEBUG | ||
thread_local aws::auth::DebugStats debug_stats; | ||
|
||
void update_step(std::uint64_t& step, std::uint64_t& outcome) { | ||
step++; | ||
outcome++; | ||
debug_stats.attempts_++; | ||
} | ||
|
||
void update_step(std::uint64_t& step) { | ||
std::uint64_t ignored = 0; | ||
update_step(step, ignored); | ||
} | ||
#else | ||
#define update_step(...) | ||
#endif | ||
thread_local aws::auth::VersionedCredentials current_creds; | ||
} | ||
|
||
using namespace aws::auth; | ||
|
||
VersionedCredentials::VersionedCredentials(std::uint64_t version, const std::string& akid, const std::string& sk, const std::string& token) : | ||
version_(version), creds_(Aws::Auth::AWSCredentials(akid, sk, token)) { | ||
} | ||
|
||
MutableStaticCredentialsProvider::MutableStaticCredentialsProvider(const std::string& akid, | ||
const std::string& sk, | ||
std::string token) { | ||
Aws::Auth::AWSCredentials creds(akid, sk, token); | ||
current_.creds_ = creds; | ||
std::string token) : | ||
creds_(std::make_shared<VersionedCredentials>(1, akid, sk, token)), version_(1) { | ||
} | ||
|
||
void MutableStaticCredentialsProvider::set_credentials(const std::string& akid, const std::string& sk, std::string token) { | ||
std::lock_guard<std::mutex> lock(update_mutex_); | ||
|
||
// | ||
// The ordering of stores here is important. current_.updating_, and current_.version_ are atomics. | ||
// Between the two of them they create an interlocked state change that allows consumers | ||
// to detect that the credentials have changed during the process of copying the values. | ||
// | ||
// Specifically current_.version_ must be incremented before current_.updating_ is set to false. | ||
// This ensures that consumers will either see a version mismatch or see the updating state change. | ||
// | ||
std::uint64_t next_version = version_ + 1; | ||
|
||
|
||
current_.updating_ = true; | ||
current_.creds_.SetAWSAccessKeyId(akid); | ||
current_.creds_.SetAWSSecretKey(sk); | ||
current_.creds_.SetSessionToken(token); | ||
current_.version_++; | ||
current_.updating_ = false; | ||
} | ||
|
||
bool MutableStaticCredentialsProvider::try_optimistic_read(Aws::Auth::AWSCredentials& destination) { | ||
// | ||
// This is an attempt to do an optimistic read. We assume that the contents of the | ||
// credentials may change in while copying to the result. | ||
// | ||
// 1. To handle this we first check if an update is in progress, and if it is we bounce out | ||
// and retry. | ||
// | ||
// 2. If the credential isn't being updated we go ahead and load the current version of | ||
// the credential. | ||
// | ||
// 3. At this point we go ahead and trigger a copy of the credential to the result. This | ||
// is what we will return if our remaining checks indicate everything is still ok. | ||
// | ||
// 4. We check again to see if the credential has entered updating, if it has it's possible | ||
// the version of the credential we copied was split between the two updates. So we | ||
// discard our copy, and try again. | ||
// | ||
// 5. Finally we make check to see that the version hasn't changed since we started. This | ||
// ensures that the credential didn't enter and exit updates between our update checks. | ||
// | ||
// If everything is ok we are safe to return the credential, while it may be a nanosecnds | ||
// out date it should be fine. | ||
// | ||
|
||
if (current_.updating_) { | ||
// | ||
// The credentials are currently being updated. It's not safe to read so | ||
// spin while we wait for the update to complete. | ||
// | ||
update_step(debug_stats.update_before_load_, debug_stats.retried_); | ||
return false; | ||
} | ||
std::uint64_t starting_version = current_.version_; | ||
|
||
// | ||
// Trigger a trivial copy to the result | ||
// | ||
destination = current_.creds_; | ||
|
||
if (current_.updating_) { | ||
// | ||
// The credentials object is being updated and the update may have started while we were | ||
// copying it. For safety we discard what we have and try again. | ||
// | ||
update_step(debug_stats.update_after_load_, debug_stats.retried_); | ||
return false; | ||
} | ||
|
||
std::uint64_t ending_version = current_.version_; | ||
|
||
if (starting_version != ending_version) { | ||
// | ||
// The version changed in between the start of the copy, and the end | ||
// of the copy. We can no longer trust that the resulting copy is | ||
// correct. So we discard what we have and try again. | ||
// | ||
update_step(debug_stats.version_mismatch_, debug_stats.retried_); | ||
return false; | ||
} | ||
update_step(debug_stats.success_); | ||
return true; | ||
std::shared_ptr<VersionedCredentials> new_credentials = std::make_shared<VersionedCredentials>(next_version, akid, sk, token); | ||
std::atomic_store(&creds_, new_credentials); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it be good to add a comment that this is updating the global credentials ? |
||
version_ = next_version; | ||
} | ||
|
||
Aws::Auth::AWSCredentials MutableStaticCredentialsProvider::GetAWSCredentials() { | ||
|
||
Aws::Auth::AWSCredentials result; | ||
|
||
if (!try_optimistic_read(result)) { | ||
// | ||
// The optimistic read failed, so just give up and use the lock to acquire the credentials. | ||
// | ||
std::lock_guard<std::mutex> lock(update_mutex_); | ||
update_step(debug_stats.used_lock_, debug_stats.success_); | ||
result = current_.creds_; | ||
if (current_creds.version_ != version_) { | ||
std::shared_ptr<VersionedCredentials> updated = std::atomic_load(&creds_); | ||
current_creds = *updated; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
DebugStats MutableStaticCredentialsProvider::get_debug_stats() { | ||
#ifdef DEBUG | ||
return debug_stats; | ||
#else | ||
return DebugStats(); | ||
#endif | ||
return current_creds.creds_; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a short comment about the role of the thread local current creds and how its version is compared against the global version would make it easier to follow.