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

Track value category, staleness, and sync state separately for ActorCache::Entries #1033

Merged
merged 6 commits into from
Aug 30, 2023

Conversation

bcaimano
Copy link
Contributor

@bcaimano bcaimano commented Aug 18, 2023

This PR is paving the way for us to start coalescing reads/gets in the actor cache. The general plan looks like this:

  • Create an entry that admits it doesn't know what its value is (like we do for the end of list operations).
  • Give that entry some way to know that it is scheduled for a sync
  • Until that entry is replaced in the cache by one with a known value, any get requests that see that entry wait for a result to be available.

To that end, we're splitting the entry state into its three component parts:

  • Is the entry stale? If so, try to evict it eventually.
  • Is the entry present, absent, or unknown? If a get hits unknown, retrieve the value.
  • Is the entry dirty, flushing (synchronizing), or clean? If it is clean, we can sometimes avoid rpc altogether.

Sadly, this is a bit longer than I was hoping it would be. That said, the number of actual behavior changes should be relatively low. The biggest changes are around what is considered evictable (unknown entries now are) and how strict we are about tracking when something is in the cache.

@bcaimano bcaimano changed the title Convert ActorCache::Entry::state to track value category, staleness, and sync state separately Track value category, staleness, and sync state separately for ActorCache::Entries Aug 18, 2023
@bcaimano bcaimano marked this pull request as ready for review August 18, 2023 16:55
case EntrySyncStatus::NOT_IN_CACHE: {
KJ_FAIL_ASSERT("NOT_IN_CACHE entries should not be in the map or flushing");
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Non-blocking nit: good idea to throw a KJ_UNREACHABLE after this switch?

Copy link
Contributor

@MellowYarker MellowYarker left a comment

Choose a reason for hiding this comment

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

I've never read the actor-cache code before so I'd rather do a second pass in the morning before approving. Happy to see it get cleaned up, especially those switch statements!

src/workerd/io/actor-cache.c++ Outdated Show resolved Hide resolved
src/workerd/io/actor-cache.c++ Outdated Show resolved Hide resolved
src/workerd/io/actor-cache.c++ Outdated Show resolved Hide resolved
src/workerd/io/actor-cache.c++ Show resolved Hide resolved
src/workerd/io/actor-cache.h Outdated Show resolved Hide resolved
src/workerd/io/actor-cache.h Outdated Show resolved Hide resolved
@@ -1414,7 +1409,6 @@ kj::Own<ActorCache::Entry> ActorCache::findInCache(
if (iter != ordered.end() && iter->get()->key == key) {
// Found exact matching entry.
Entry& entry = **iter;
KJ_ASSERT(entry.state != NOT_IN_CACHE, "entry should have been removed from map");
Copy link
Contributor

Choose a reason for hiding this comment

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

I was a eyeing this assert but noticed it wasn't in the final diff so I'll ask here instead. Previously wondered the reasoning for checking NOT_IN_CACHE directly instead of UNKNOWN, but why delete this entirely?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could leave it in if you prefer! Basically, this particular assert doesn't have anything to do with this function, we have no reason why we cannot return the entry since we pulled it from the map just now. I'd also be willing to add KJ_IASSERTS to all the places we set syncStatus to make sure the previous state is correct.

@bcaimano bcaimano force-pushed the bcaimano/actor-cache-no-state branch from 658d4bb to 1f55c38 Compare August 23, 2023 20:47
@jasnell
Copy link
Member

jasnell commented Aug 25, 2023

doh, sorry, for some reason I thought this had already landed. I updated on the comment format which caused a merge conflict.

@bcaimano
Copy link
Contributor Author

doh, sorry, for some reason I thought this had already landed. I updated on the comment format which caused a merge conflict.

No worries, thanks for updating the comment style in general!

@bcaimano bcaimano force-pushed the bcaimano/actor-cache-no-state branch from 1f55c38 to 8582914 Compare August 28, 2023 22:54
This also fixes an inconsequential bug wherein we set dummy values to CLEAN instead of NOT_IN_CACHE.
Tracking these unknown values makes our logic around staleness and eviction simpler. It also prevents non-intersecting list calls (which add unknown entries for their end key) from filling up the cache with unevictable entries. Since both unknown (END_GAP) and known (CLEAN) entries can now be stale, entry staleness becomes its own boolean.
This commit introduces separate enum for if a value is present, absent, or unknown that is constant for the lifetime of an `Entry`. This makes our logic in several places clearer and provides the ground work for coalescing reads (since we'll need an entry in cache for them).
Since we have now moved staleness and value category to their own bytes, we can shrink down our sync status to its own byte and make it clearer that it indicates membership in various maps and lists.
@bcaimano bcaimano force-pushed the bcaimano/actor-cache-no-state branch from 8582914 to 18d864c Compare August 30, 2023 15:44
@bcaimano bcaimano merged commit 3a49006 into main Aug 30, 2023
6 checks passed
@bcaimano bcaimano deleted the bcaimano/actor-cache-no-state branch August 30, 2023 19:18
jp4a50 added a commit to jp4a50/workerd that referenced this pull request Sep 8, 2023
…ctor-cache-no-state"

This reverts commit 3a49006, reversing
changes made to 812f78b.
jp4a50 added a commit that referenced this pull request Sep 8, 2023
…-no-state"

This reverts commit 3a49006, reversing
changes made to 812f78b.
jp4a50 added a commit that referenced this pull request Sep 8, 2023
…-no-state"

This reverts commit 3a49006, reversing
changes made to 812f78b.
jp4a50 added a commit that referenced this pull request Sep 8, 2023
Revert "Merge pull request #1033 from cloudflare/bcaimano/actor-cache-no-state
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.

3 participants