-
Notifications
You must be signed in to change notification settings - Fork 129
Valkey #877
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
Open
hpopuri2
wants to merge
5
commits into
trinodb:main
Choose a base branch
from
hpopuri2:valkey
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Valkey #877
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b73c305
added valkey dependency as a distributed cach
hpopuri2 e6aaf7c
Add gateway.log to gitignore
hpopuri2 d01349a
Update config.yaml
hpopuri2 26f54f6
Remove gateway.log from repository
hpopuri2 345a7c4
Refactor caching infrastructure and address code review comments
hpopuri2 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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| gateway-ha/gateway.log |
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
30 changes: 30 additions & 0 deletions
30
gateway-ha/src/main/java/io/trino/gateway/ha/cache/Cache.java
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 |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.trino.gateway.ha.cache; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| public interface Cache | ||
| { | ||
| Optional<String> get(String key); | ||
|
|
||
| void set(String key, String value); | ||
|
|
||
| void invalidate(String key); | ||
|
|
||
| default boolean isEnabled() | ||
| { | ||
| return false; | ||
| } | ||
| } |
184 changes: 184 additions & 0 deletions
184
gateway-ha/src/main/java/io/trino/gateway/ha/cache/QueryCacheManager.java
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 |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.trino.gateway.ha.cache; | ||
|
|
||
| import com.google.common.cache.CacheBuilder; | ||
| import com.google.common.cache.CacheLoader; | ||
| import com.google.common.cache.LoadingCache; | ||
|
|
||
| import java.util.Optional; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.function.Function; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| /** | ||
| * Manages query-related caches including both L1 (in-memory LoadingCache) and L2 (distributed cache). | ||
| * This class encapsulates all cache operations to provide better separation of concerns. | ||
| */ | ||
| public class QueryCacheManager | ||
| { | ||
| private static final String BACKEND_KEY_PREFIX = "trino:query:backend:"; | ||
| private static final String ROUTING_GROUP_KEY_PREFIX = "trino:query:routing_group:"; | ||
| private static final String EXTERNAL_URL_KEY_PREFIX = "trino:query:external_url:"; | ||
|
|
||
| private final LoadingCache<String, String> queryIdBackendCache; | ||
| private final LoadingCache<String, String> queryIdRoutingGroupCache; | ||
| private final LoadingCache<String, String> queryIdExternalUrlCache; | ||
| private final Cache distributedCache; | ||
|
|
||
| public QueryCacheManager( | ||
| Function<String, String> backendLoader, | ||
| Function<String, String> routingGroupLoader, | ||
| Function<String, String> externalUrlLoader, | ||
| Cache distributedCache) | ||
| { | ||
| this.queryIdBackendCache = buildCache(backendLoader); | ||
| this.queryIdRoutingGroupCache = buildCache(routingGroupLoader); | ||
| this.queryIdExternalUrlCache = buildCache(externalUrlLoader); | ||
| this.distributedCache = requireNonNull(distributedCache, "distributedCache is null"); | ||
| } | ||
|
|
||
| private LoadingCache<String, String> buildCache(Function<String, String> loader) | ||
| { | ||
| return CacheBuilder.newBuilder() | ||
| .maximumSize(10000) | ||
| .expireAfterAccess(30, TimeUnit.MINUTES) | ||
| .build( | ||
| new CacheLoader<>() | ||
| { | ||
| @Override | ||
| public String load(String queryId) | ||
| { | ||
| return loader.apply(queryId); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // L1 Cache Operations | ||
|
|
||
| public void setBackendInL1(String queryId, String backend) | ||
| { | ||
| queryIdBackendCache.put(queryId, backend); | ||
| } | ||
|
|
||
| public void setRoutingGroupInL1(String queryId, String routingGroup) | ||
| { | ||
| queryIdRoutingGroupCache.put(queryId, routingGroup); | ||
| } | ||
|
|
||
| public void setExternalUrlInL1(String queryId, String externalUrl) | ||
| { | ||
| queryIdExternalUrlCache.put(queryId, externalUrl); | ||
| } | ||
|
|
||
| public String getBackendFromL1(String queryId) | ||
| throws ExecutionException | ||
| { | ||
| return queryIdBackendCache.get(queryId); | ||
| } | ||
|
|
||
| public String getRoutingGroupFromL1(String queryId) | ||
| throws ExecutionException | ||
| { | ||
| return queryIdRoutingGroupCache.get(queryId); | ||
| } | ||
|
|
||
| public String getExternalUrlFromL1(String queryId) | ||
| throws ExecutionException | ||
| { | ||
| return queryIdExternalUrlCache.get(queryId); | ||
| } | ||
|
|
||
| // L2 Cache Operations | ||
|
|
||
| public void cacheBackend(String queryId, String backend) | ||
| { | ||
| if (distributedCache.isEnabled()) { | ||
| distributedCache.set(BACKEND_KEY_PREFIX + queryId, backend); | ||
| } | ||
| } | ||
|
|
||
| public void cacheRoutingGroup(String queryId, String routingGroup) | ||
| { | ||
| if (distributedCache.isEnabled()) { | ||
| distributedCache.set(ROUTING_GROUP_KEY_PREFIX + queryId, routingGroup); | ||
| } | ||
| } | ||
|
|
||
| public void cacheExternalUrl(String queryId, String externalUrl) | ||
| { | ||
| if (distributedCache.isEnabled()) { | ||
| distributedCache.set(EXTERNAL_URL_KEY_PREFIX + queryId, externalUrl); | ||
| } | ||
| } | ||
|
|
||
| public Optional<String> getCachedBackend(String queryId) | ||
| { | ||
| if (distributedCache.isEnabled()) { | ||
| return distributedCache.get(BACKEND_KEY_PREFIX + queryId); | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| public Optional<String> getCachedRoutingGroup(String queryId) | ||
| { | ||
| if (distributedCache.isEnabled()) { | ||
| return distributedCache.get(ROUTING_GROUP_KEY_PREFIX + queryId); | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| public Optional<String> getCachedExternalUrl(String queryId) | ||
| { | ||
| if (distributedCache.isEnabled()) { | ||
| return distributedCache.get(EXTERNAL_URL_KEY_PREFIX + queryId); | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| // Combined Operations (L1 + L2) | ||
|
|
||
| public void setBackend(String queryId, String backend) | ||
| { | ||
| setBackendInL1(queryId, backend); | ||
| cacheBackend(queryId, backend); | ||
| } | ||
|
|
||
| public void setRoutingGroup(String queryId, String routingGroup) | ||
| { | ||
| setRoutingGroupInL1(queryId, routingGroup); | ||
| cacheRoutingGroup(queryId, routingGroup); | ||
| } | ||
|
|
||
| public void setExternalUrl(String queryId, String externalUrl) | ||
| { | ||
| setExternalUrlInL1(queryId, externalUrl); | ||
| cacheExternalUrl(queryId, externalUrl); | ||
| } | ||
|
|
||
| public void updateAllCaches(String queryId, String backend, String routingGroup, String externalUrl) | ||
| { | ||
| setBackendInL1(queryId, backend); | ||
| cacheBackend(queryId, backend); | ||
| cacheRoutingGroup(queryId, routingGroup); | ||
| cacheExternalUrl(queryId, externalUrl); | ||
| } | ||
|
|
||
| public boolean isDistributedCacheEnabled() | ||
| { | ||
| return distributedCache.isEnabled(); | ||
| } | ||
| } | ||
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 does L1 vs L2 matter here? imo, this file shouldn't care about specific keys.