-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix: Add type safety to FindMutable calls #5886
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
Merged
+29
−27
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -285,9 +285,13 @@ OpResult<int64_t> OpIncrBy(const OpArgs& op_args, string_view key, int64_t incr, | |
auto& db_slice = op_args.GetDbSlice(); | ||
|
||
// we avoid using AddOrFind because of skip_on_missing option for memcache. | ||
auto res = db_slice.FindMutable(op_args.db_cntx, key); | ||
// Use type-safe FindMutable with OBJ_STRING (fixes #5316) | ||
auto res = db_slice.FindMutable(op_args.db_cntx, key, OBJ_STRING); | ||
|
||
if (!res) { | ||
if (res.status() == OpStatus::WRONG_TYPE) | ||
return res.status(); | ||
|
||
if (!IsValid(res.it)) { | ||
if (skip_on_missing) | ||
return OpStatus::KEY_NOTFOUND; | ||
|
||
|
@@ -300,11 +304,8 @@ OpResult<int64_t> OpIncrBy(const OpArgs& op_args, string_view key, int64_t incr, | |
return incr; | ||
} | ||
|
||
if (res.it->second.ObjType() != OBJ_STRING) { | ||
return OpStatus::WRONG_TYPE; | ||
} | ||
|
||
auto opt_prev = res.it->second.TryGetInt(); | ||
// Type is already checked by FindMutable (OBJ_STRING) | ||
auto opt_prev = res->it->second.TryGetInt(); | ||
if (!opt_prev) { | ||
return OpStatus::INVALID_VALUE; | ||
} | ||
|
@@ -316,8 +317,8 @@ OpResult<int64_t> OpIncrBy(const OpArgs& op_args, string_view key, int64_t incr, | |
} | ||
|
||
int64_t new_val = prev + incr; | ||
DCHECK(!res.it->second.IsExternal()); | ||
res.it->second.SetInt(new_val); | ||
DCHECK(!res->it->second.IsExternal()); | ||
res->it->second.SetInt(new_val); | ||
|
||
return new_val; | ||
} | ||
|
@@ -383,20 +384,20 @@ OpResult<array<int64_t, 5>> OpThrottle(const OpArgs& op_args, const string_view | |
// Cost of this request | ||
const int64_t increment_ns = emission_interval_ns * quantity; // should be nonnegative | ||
|
||
auto res = db_slice.FindMutable(op_args.db_cntx, key); | ||
// Use type-safe FindMutable with OBJ_STRING (fixes #5316) | ||
|
||
auto res = db_slice.FindMutable(op_args.db_cntx, key, OBJ_STRING); | ||
const int64_t now_ns = GetCurrentTimeNs(); | ||
|
||
int64_t tat_ns = now_ns; | ||
if (IsValid(res.it)) { | ||
if (res.it->second.ObjType() != OBJ_STRING) { | ||
return OpStatus::WRONG_TYPE; | ||
} | ||
|
||
auto opt_prev = res.it->second.TryGetInt(); | ||
if (res) { | ||
// Type is already checked by FindMutable (OBJ_STRING) | ||
auto opt_prev = res->it->second.TryGetInt(); | ||
if (!opt_prev) { | ||
return OpStatus::INVALID_VALUE; | ||
} | ||
tat_ns = *opt_prev; | ||
} else if (res.status() == OpStatus::WRONG_TYPE) { | ||
return res.status(); | ||
} | ||
|
||
int64_t new_tat_ns = max(tat_ns, now_ns); | ||
|
@@ -458,14 +459,14 @@ OpResult<array<int64_t, 5>> OpThrottle(const OpArgs& op_args, const string_view | |
// break behavior because the tat_ns value will be used to check for throttling. | ||
const int64_t new_tat_ms = | ||
(new_tat_ns + kMilliSecondToNanoSecond - 1) / kMilliSecondToNanoSecond; | ||
if (IsValid(res.it)) { | ||
if (IsValid(res.exp_it)) { | ||
res.exp_it->second = db_slice.FromAbsoluteTime(new_tat_ms); | ||
if (res) { | ||
if (IsValid(res->exp_it)) { | ||
res->exp_it->second = db_slice.FromAbsoluteTime(new_tat_ms); | ||
} else { | ||
db_slice.AddExpire(op_args.db_cntx.db_index, res.it, new_tat_ms); | ||
db_slice.AddExpire(op_args.db_cntx.db_index, res->it, new_tat_ms); | ||
} | ||
|
||
res.it->second.SetInt(new_tat_ns); | ||
res->it->second.SetInt(new_tat_ns); | ||
} else { | ||
CompactObj cobj; | ||
cobj.SetInt(new_tat_ns); | ||
|
Oops, something went wrong.
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.
why do you need to run it manually?
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.
AutoUpdater dtor automatically calls Run() on destruction, which has a DCHECK that verifies the key still exists in the database.
Since we call db_slice.Del() earlier in the code (which removes the key), we must manually call post_updater.Run() before deletion to update memory accounting while the key is still valid. Otherwise, when res_it goes out of scope, the destructor will call Run() on an already-deleted key and the DCHECK will fail.
This pattern is used throughout the codebase whenever a key is deleted after FindMutable().
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.
maybe we can unify somehow this code, I mean do delete in post_updater or call run during delete.
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.
This pattern (post_updater.Run() + Del()) appears many times across several files in the codebase, so unifying it makes sense.
Suggested approach:
Add a new method DelMutable() to DbSlice:
This would simplify code like:
However, I'd suggest doing this as a separate follow-up PR after this one, because:
Maybe better create a separate issue for this refactoring?