-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[ML] Implement CCMCache #137743
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
+583
−3
Merged
[ML] Implement CCMCache #137743
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9a654f3
[ML] Implement CCMCache
prwhelan f59ca21
export module-info
prwhelan ba3967e
Merge branch 'main' of github.com:elastic/elasticsearch into ml-1738
prwhelan 6eb5d34
Add non-operator permissions
prwhelan 069a46f
Merge branch 'main' into ml-1738
prwhelan 60f1cd0
rename ccmstorageservice
prwhelan 11182fd
address comments
prwhelan 95121fe
Merge branch 'main' into ml-1738
prwhelan 286dfa6
Merge branch 'main' into ml-1738
prwhelan dfa19b7
Merge branch 'main' into ml-1738
prwhelan 907769a
Merge branch 'main' into ml-1738
prwhelan 960d931
Merge branch 'main' into ml-1738
prwhelan 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
179 changes: 179 additions & 0 deletions
179
...lusterTest/java/org/elasticsearch/xpack/inference/services/elastic/ccm/CCMCacheTests.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,179 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.inference.services.elastic.ccm; | ||
|
|
||
| import org.elasticsearch.ResourceNotFoundException; | ||
| import org.elasticsearch.action.support.ActionTestUtils; | ||
| import org.elasticsearch.action.support.TestPlainActionFuture; | ||
| import org.elasticsearch.common.bytes.BytesArray; | ||
| import org.elasticsearch.core.TimeValue; | ||
| import org.elasticsearch.plugins.Plugin; | ||
| import org.elasticsearch.test.ESSingleNodeTestCase; | ||
| import org.elasticsearch.xpack.inference.LocalStateInferencePlugin; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.not; | ||
| import static org.hamcrest.Matchers.sameInstance; | ||
|
|
||
| public class CCMCacheTests extends ESSingleNodeTestCase { | ||
|
|
||
| private static final TimeValue TIMEOUT = TimeValue.THIRTY_SECONDS; | ||
|
|
||
| private CCMCache ccmCache; | ||
| private CCMPersistentStorageService ccmPersistentStorageService; | ||
|
|
||
| @Override | ||
| protected Collection<Class<? extends Plugin>> getPlugins() { | ||
| return List.of(LocalStateInferencePlugin.class); | ||
| } | ||
|
|
||
| @Before | ||
| public void createComponents() { | ||
| ccmCache = node().injector().getInstance(CCMCache.class); | ||
| ccmPersistentStorageService = node().injector().getInstance(CCMPersistentStorageService.class); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean resetNodeAfterTest() { | ||
| return true; | ||
| } | ||
|
|
||
| @After | ||
| public void clearCacheAndIndex() { | ||
| try { | ||
| indicesAdmin().prepareDelete(CCMIndex.INDEX_NAME).execute().actionGet(TIMEOUT); | ||
| } catch (ResourceNotFoundException e) { | ||
| // mission complete! | ||
| } | ||
| } | ||
|
|
||
| public void testCacheHit() throws IOException { | ||
| var expectedCcmModel = storeCcm(); | ||
| var actualCcmModel = getFromCache(); | ||
| assertThat(actualCcmModel, equalTo(expectedCcmModel)); | ||
| assertThat(ccmCache.stats().getHits(), equalTo(0L)); | ||
| assertThat(getFromCache(), sameInstance(actualCcmModel)); | ||
| assertThat(ccmCache.stats().getHits(), equalTo(1L)); | ||
| } | ||
|
|
||
| private CCMModel storeCcm() throws IOException { | ||
| var ccmModel = CCMModel.fromXContentBytes(new BytesArray(""" | ||
| { | ||
| "api_key": "test_key" | ||
| } | ||
| """)); | ||
| var listener = new TestPlainActionFuture<Void>(); | ||
| ccmPersistentStorageService.store(ccmModel, listener); | ||
| listener.actionGet(TIMEOUT); | ||
| return ccmModel; | ||
| } | ||
|
|
||
| private CCMModel getFromCache() { | ||
| var listener = new TestPlainActionFuture<CCMModel>(); | ||
| ccmCache.get(listener); | ||
| return listener.actionGet(TIMEOUT); | ||
| } | ||
|
|
||
| public void testCacheInvalidate() throws Exception { | ||
| var expectedCcmModel = storeCcm(); | ||
| var actualCcmModel = getFromCache(); | ||
| assertThat(actualCcmModel, equalTo(expectedCcmModel)); | ||
| assertThat(ccmCache.stats().getHits(), equalTo(0L)); | ||
| assertThat(ccmCache.stats().getMisses(), equalTo(1L)); | ||
| assertThat(ccmCache.cacheCount(), equalTo(1)); | ||
|
|
||
| var listener = new TestPlainActionFuture<Void>(); | ||
| ccmCache.invalidate(listener); | ||
| listener.actionGet(TIMEOUT); | ||
|
|
||
| assertThat(getFromCache(), not(sameInstance(actualCcmModel))); | ||
| assertThat(ccmCache.stats().getHits(), equalTo(0L)); | ||
| assertThat(ccmCache.stats().getMisses(), equalTo(2L)); | ||
| assertThat(ccmCache.stats().getEvictions(), equalTo(1L)); | ||
| assertThat(ccmCache.cacheCount(), equalTo(1)); | ||
| } | ||
|
|
||
| public void testEmptyInvalidate() throws InterruptedException { | ||
| var latch = new CountDownLatch(1); | ||
| ccmCache.invalidate(ActionTestUtils.assertNoFailureListener(success -> latch.countDown())); | ||
| assertTrue(latch.await(TIMEOUT.getSeconds(), TimeUnit.SECONDS)); | ||
|
|
||
| assertThat(ccmCache.stats().getEvictions(), equalTo(0L)); | ||
| assertThat(ccmCache.cacheCount(), equalTo(0)); | ||
| } | ||
|
|
||
| private boolean isPresent() { | ||
| var listener = new TestPlainActionFuture<Boolean>(); | ||
| ccmCache.isEnabled(listener); | ||
| return listener.actionGet(TIMEOUT); | ||
| } | ||
|
|
||
| public void testIsEnabled() throws IOException { | ||
| storeCcm(); | ||
|
|
||
| getFromCache(); | ||
| assertThat(ccmCache.stats().getHits(), equalTo(0L)); | ||
| assertThat(ccmCache.stats().getMisses(), equalTo(1L)); | ||
|
|
||
| assertTrue(isPresent()); | ||
| assertThat(ccmCache.stats().getHits(), equalTo(1L)); | ||
| assertThat(ccmCache.stats().getMisses(), equalTo(1L)); | ||
| } | ||
|
|
||
| public void testIsDisabledWithMissingIndex() { | ||
| assertFalse(isPresent()); | ||
| } | ||
|
|
||
| public void testIsDisabledWithPresentIndex() { | ||
| indicesAdmin().prepareCreate(CCMIndex.INDEX_NAME).execute().actionGet(TIMEOUT); | ||
| assertFalse(isPresent()); | ||
| } | ||
|
|
||
| public void testIsDisabledWithCacheHit() { | ||
| indicesAdmin().prepareCreate(CCMIndex.INDEX_NAME).execute().actionGet(TIMEOUT); | ||
|
|
||
| assertFalse(isPresent()); | ||
| assertThat(ccmCache.stats().getHits(), equalTo(0L)); | ||
| assertThat(ccmCache.stats().getMisses(), equalTo(1L)); | ||
|
|
||
| assertFalse(isPresent()); | ||
| assertThat(ccmCache.stats().getHits(), equalTo(1L)); | ||
| assertThat(ccmCache.stats().getMisses(), equalTo(1L)); | ||
| } | ||
|
|
||
| public void testIsDisabledRefreshedWithGet() throws IOException { | ||
| indicesAdmin().prepareCreate(CCMIndex.INDEX_NAME).execute().actionGet(TIMEOUT); | ||
|
|
||
| assertFalse(isPresent()); | ||
| assertThat(ccmCache.stats().getHits(), equalTo(0L)); | ||
| assertThat(ccmCache.stats().getMisses(), equalTo(1L)); | ||
|
|
||
| var expectedCcmModel = storeCcm(); | ||
|
|
||
| assertFalse(isPresent()); | ||
| assertThat(ccmCache.stats().getHits(), equalTo(1L)); | ||
| assertThat(ccmCache.stats().getMisses(), equalTo(1L)); | ||
|
|
||
| var actualCcmModel = getFromCache(); | ||
| assertThat(actualCcmModel, equalTo(expectedCcmModel)); | ||
| assertThat(ccmCache.stats().getHits(), equalTo(2L)); | ||
| assertThat(ccmCache.stats().getMisses(), equalTo(1L)); | ||
|
|
||
| assertTrue(isPresent()); | ||
| assertThat(ccmCache.stats().getHits(), equalTo(3L)); | ||
| assertThat(ccmCache.stats().getMisses(), equalTo(1L)); | ||
| } | ||
| } | ||
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
147 changes: 147 additions & 0 deletions
147
...erence/src/main/java/org/elasticsearch/xpack/inference/common/BroadcastMessageAction.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,147 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.inference.common; | ||
|
|
||
| import org.elasticsearch.action.FailedNodeException; | ||
| import org.elasticsearch.action.support.ActionFilters; | ||
| import org.elasticsearch.action.support.TransportAction; | ||
| import org.elasticsearch.action.support.nodes.BaseNodeResponse; | ||
| import org.elasticsearch.action.support.nodes.BaseNodesRequest; | ||
| import org.elasticsearch.action.support.nodes.BaseNodesResponse; | ||
| import org.elasticsearch.action.support.nodes.TransportNodesAction; | ||
| import org.elasticsearch.cluster.ClusterName; | ||
| import org.elasticsearch.cluster.node.DiscoveryNode; | ||
| import org.elasticsearch.cluster.service.ClusterService; | ||
| import org.elasticsearch.common.Strings; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.common.io.stream.Writeable; | ||
| import org.elasticsearch.core.TimeValue; | ||
| import org.elasticsearch.tasks.CancellableTask; | ||
| import org.elasticsearch.tasks.Task; | ||
| import org.elasticsearch.tasks.TaskId; | ||
| import org.elasticsearch.threadpool.ThreadPool; | ||
| import org.elasticsearch.transport.AbstractTransportRequest; | ||
| import org.elasticsearch.transport.TransportService; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Broadcasts a {@link Writeable} to all nodes and responds with an empty object. | ||
| * This is intended to be used as a fire-and-forget style, where responses and failures are logged and swallowed. | ||
| */ | ||
| public abstract class BroadcastMessageAction<Message extends Writeable> extends TransportNodesAction< | ||
| BroadcastMessageAction.Request<Message>, | ||
| BroadcastMessageAction.Response, | ||
| BroadcastMessageAction.NodeRequest<Message>, | ||
| BroadcastMessageAction.NodeResponse, | ||
| Void> { | ||
|
|
||
| protected BroadcastMessageAction( | ||
| String actionName, | ||
| ClusterService clusterService, | ||
| TransportService transportService, | ||
| ActionFilters actionFilters, | ||
| Writeable.Reader<Message> messageReader | ||
| ) { | ||
| super( | ||
| actionName, | ||
| clusterService, | ||
| transportService, | ||
| actionFilters, | ||
| in -> new NodeRequest<>(messageReader.read(in)), | ||
| clusterService.threadPool().executor(ThreadPool.Names.MANAGEMENT) | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| protected Response newResponse(Request<Message> request, List<NodeResponse> nodeResponses, List<FailedNodeException> failures) { | ||
| return new Response(clusterService.getClusterName(), nodeResponses, failures); | ||
| } | ||
|
|
||
| @Override | ||
| protected NodeRequest<Message> newNodeRequest(Request<Message> request) { | ||
| return new NodeRequest<>(request.message); | ||
| } | ||
|
|
||
| @Override | ||
| protected NodeResponse newNodeResponse(StreamInput in, DiscoveryNode node) throws IOException { | ||
| return new NodeResponse(in, node); | ||
| } | ||
|
|
||
| @Override | ||
| protected NodeResponse nodeOperation(NodeRequest<Message> request, Task task) { | ||
| receiveMessage(request.message); | ||
| return new NodeResponse(transportService.getLocalNode()); | ||
| } | ||
|
|
||
| /** | ||
| * This method is run on each node in the cluster. | ||
| */ | ||
| protected abstract void receiveMessage(Message message); | ||
|
|
||
| public static <T extends Writeable> Request<T> request(T message, TimeValue timeout) { | ||
| return new Request<>(message, timeout); | ||
| } | ||
|
|
||
| public static class Request<Message extends Writeable> extends BaseNodesRequest { | ||
| private final Message message; | ||
|
|
||
| protected Request(Message message, TimeValue timeout) { | ||
| super(Strings.EMPTY_ARRAY); | ||
| this.message = message; | ||
| setTimeout(timeout); | ||
| } | ||
| } | ||
|
|
||
| public static class Response extends BaseNodesResponse<NodeResponse> { | ||
|
|
||
| protected Response(ClusterName clusterName, List<NodeResponse> nodes, List<FailedNodeException> failures) { | ||
| super(clusterName, nodes, failures); | ||
| } | ||
|
|
||
| @Override | ||
| protected List<NodeResponse> readNodesFrom(StreamInput in) throws IOException { | ||
| return in.readCollectionAsList(NodeResponse::new); | ||
| } | ||
|
|
||
| @Override | ||
| protected void writeNodesTo(StreamOutput out, List<NodeResponse> nodes) { | ||
| TransportAction.localOnly(); | ||
| } | ||
| } | ||
|
|
||
| public static class NodeRequest<Message extends Writeable> extends AbstractTransportRequest { | ||
| private final Message message; | ||
|
|
||
| private NodeRequest(Message message) { | ||
| this.message = message; | ||
| } | ||
|
|
||
| @Override | ||
| public Task createTask(long id, String type, String action, TaskId parentTaskId, Map<String, String> headers) { | ||
| return new CancellableTask(id, type, action, "broadcasted message to an individual node", parentTaskId, headers); | ||
| } | ||
| } | ||
|
|
||
| public static class NodeResponse extends BaseNodeResponse { | ||
| protected NodeResponse(StreamInput in) throws IOException { | ||
| super(in); | ||
| } | ||
|
|
||
| protected NodeResponse(StreamInput in, DiscoveryNode node) throws IOException { | ||
| super(in, node); | ||
| } | ||
|
|
||
| protected NodeResponse(DiscoveryNode node) { | ||
| super(node); | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.
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 might have missed it but do we have a test for the
elsecase in this block:CCMCache::get
Could we add a test that when the internal entry is in the disabled state (but present and not null) that we get a cache miss aka hit the
elsecase?