-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add TTL values for client caches of key locations and auto remove expired entries #12514
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
base: main
Are you sure you want to change the base?
Changes from all commits
cf3480c
bc933ea
2967d31
18b1b4a
78ca1a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -948,39 +948,45 @@ ACTOR static Future<Void> monitorClientDBInfoChange(DatabaseContext* cx, | |
| } | ||
| } | ||
|
|
||
| void updateLocationCacheWithCaches(DatabaseContext* self, | ||
| const std::map<UID, StorageServerInterface>& removed, | ||
| const std::map<UID, StorageServerInterface>& added) { | ||
| // TODO: this needs to be more clever in the future | ||
| auto ranges = self->locationCache.ranges(); | ||
| for (auto iter = ranges.begin(); iter != ranges.end(); ++iter) { | ||
| if (iter->value() && iter->value()->hasCaches) { | ||
| auto& val = iter->value(); | ||
| std::vector<Reference<ReferencedInterface<StorageServerInterface>>> interfaces; | ||
| interfaces.reserve(val->size() - removed.size() + added.size()); | ||
| for (int i = 0; i < val->size(); ++i) { | ||
| const auto& interf = (*val)[i]; | ||
| if (removed.count(interf->interf.id()) == 0) { | ||
| interfaces.emplace_back(interf); | ||
| ACTOR static Future<Void> cleanupLocationCache(DatabaseContext* cx) { | ||
| // Only run cleanup if TTL is enabled | ||
| if (CLIENT_KNOBS->LOCATION_CACHE_ENTRY_TTL == 0.0) { | ||
| return Void(); | ||
| } | ||
|
|
||
| loop { | ||
| wait(delay(CLIENT_KNOBS->LOCATION_CACHE_EVICTION_INTERVAL)); | ||
|
|
||
| double currentTime = now(); | ||
| std::vector<KeyRangeRef> toRemove; | ||
| int totalCount = 0; | ||
|
|
||
| // Scan locationCache for expired entries | ||
| auto iter = cx->locationCache.randomRange(); | ||
| for (; iter != cx->locationCache.lastItem(); ++iter) { | ||
| if (iter->value()) { | ||
| // Check the expireTime of the first cache entry as a representative | ||
| // All entries in a range typically have similar expiration times | ||
| if (iter->value()->expireTime > 0.0 && iter->value()->expireTime <= currentTime) { | ||
| toRemove.push_back(iter->range()); | ||
| } | ||
| } | ||
| for (const auto& p : added) { | ||
| interfaces.push_back(makeReference<ReferencedInterface<StorageServerInterface>>(p.second)); | ||
| totalCount++; | ||
| if (totalCount > 1000 || toRemove.size() > 100) { | ||
| break; // Avoid long blocking scans | ||
| } | ||
| iter->value() = makeReference<LocationInfo>(interfaces, true); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Reference<LocationInfo> addCaches(const Reference<LocationInfo>& loc, | ||
| const std::vector<Reference<ReferencedInterface<StorageServerInterface>>>& other) { | ||
| std::vector<Reference<ReferencedInterface<StorageServerInterface>>> interfaces; | ||
| interfaces.reserve(loc->size() + other.size()); | ||
| for (int i = 0; i < loc->size(); ++i) { | ||
| interfaces.emplace_back((*loc)[i]); | ||
| // Remove expired entries | ||
| for (const auto& range : toRemove) { | ||
| cx->locationCache.insert(range, Reference<LocationInfo>()); | ||
| } | ||
|
|
||
| if (!toRemove.empty()) { | ||
| CODE_PROBE(true, "LocationCacheCleanup removed some entries"); | ||
| TraceEvent("LocationCacheCleanup").detail("RemovedRanges", toRemove.size()); | ||
| } | ||
| } | ||
| interfaces.insert(interfaces.end(), other.begin(), other.end()); | ||
| return makeReference<LocationInfo>(interfaces, true); | ||
| } | ||
|
|
||
| ACTOR static Future<Void> handleTssMismatches(DatabaseContext* cx) { | ||
|
|
@@ -1266,6 +1272,7 @@ DatabaseContext::DatabaseContext(Reference<AsyncVar<Reference<IClusterConnection | |
|
|
||
| clientDBInfoMonitor = monitorClientDBInfoChange(this, clientInfo, &proxiesChangeTrigger); | ||
| tssMismatchHandler = handleTssMismatches(this); | ||
| locationCacheCleanup = cleanupLocationCache(this); | ||
|
Contributor
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. Should we add a
Contributor
Author
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. We don't need to explicitly call |
||
| clientStatusUpdater.actor = clientStatusUpdateActor(this); | ||
|
|
||
| smoothMidShardSize.reset(CLIENT_KNOBS->INIT_MID_SHARD_BYTES); | ||
|
|
@@ -1574,6 +1581,7 @@ DatabaseContext::~DatabaseContext() { | |
| clientDBInfoMonitor.cancel(); | ||
| monitorTssInfoChange.cancel(); | ||
| tssMismatchHandler.cancel(); | ||
| locationCacheCleanup.cancel(); | ||
| storage = nullptr; | ||
|
|
||
| if (grvUpdateHandler.isValid()) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -64,15 +64,18 @@ class StorageServerInfo : public ReferencedInterface<StorageServerInterface> { | |
| struct LocationInfo : MultiInterface<ReferencedInterface<StorageServerInterface>>, FastAllocated<LocationInfo> { | ||
| using Locations = MultiInterface<ReferencedInterface<StorageServerInterface>>; | ||
| explicit LocationInfo(const std::vector<Reference<ReferencedInterface<StorageServerInterface>>>& v) | ||
| : Locations(v) {} | ||
| LocationInfo(const std::vector<Reference<ReferencedInterface<StorageServerInterface>>>& v, bool hasCaches) | ||
| : Locations(v), hasCaches(hasCaches) {} | ||
| : Locations(v), | ||
|
Contributor
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. Please correct me if I'm wrong, but do I understand the code correctly that the
Contributor
Author
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. that's correct. Each shard is stored on multiple storage servers, so |
||
| expireTime(CLIENT_KNOBS->LOCATION_CACHE_ENTRY_TTL > 0.0 ? now() + CLIENT_KNOBS->LOCATION_CACHE_ENTRY_TTL | ||
| : 0.0) {} | ||
|
|
||
| LocationInfo(const LocationInfo&) = delete; | ||
| LocationInfo(LocationInfo&&) = delete; | ||
| LocationInfo& operator=(const LocationInfo&) = delete; | ||
| LocationInfo& operator=(LocationInfo&&) = delete; | ||
| bool hasCaches = false; | ||
| Reference<Locations> locations() { return Reference<Locations>::addRef(this); } | ||
|
|
||
| // Absolute expiration time for this cache entry. 0 means no expiration (TTL disabled). | ||
| double expireTime = 0.0; | ||
| }; | ||
|
|
||
| using CommitProxyInfo = ModelInterface<CommitProxyInterface>; | ||
|
|
@@ -376,6 +379,7 @@ class DatabaseContext : public ReferenceCounted<DatabaseContext>, public FastAll | |
| Future<Void> tssMismatchHandler; | ||
| PromiseStream<std::pair<UID, std::vector<DetailedTSSMismatch>>> tssMismatchStream; | ||
| Future<Void> grvUpdateHandler; | ||
| Future<Void> locationCacheCleanup; | ||
| Reference<CommitProxyInfo> commitProxies; | ||
| Reference<GrvProxyInfo> grvProxies; | ||
| bool proxyProvisional; // Provisional commit proxy and grv proxy are used at the same time. | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.