-
Notifications
You must be signed in to change notification settings - Fork 25.7k
[ML] Creating CCM feature flag, index, and storage logic #137582
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
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5c08cb2
Starting CCM index
jonathan-buttner 5935f72
Renaming
jonathan-buttner c4a0ede
Merge branch 'main' of github.com:elastic/elasticsearch into ml-ccm-i…
jonathan-buttner 6e2efe6
Building request
jonathan-buttner 371a086
Adding feature flag and tests
jonathan-buttner 3b14514
Merge branch 'main' of github.com:elastic/elasticsearch into ml-ccm-i…
jonathan-buttner e691bd2
[CI] Auto commit changes from spotless
6df47f8
Adding alias
jonathan-buttner 217a1af
Merge branch 'ml-ccm-index' of github.com:jonathan-buttner/elasticsea…
jonathan-buttner 499da1e
Using search instead of get in case of rollover
jonathan-buttner 59df6b1
Merge branch 'main' of github.com:elastic/elasticsearch into ml-ccm-i…
jonathan-buttner 80d2433
[CI] Auto commit changes from spotless
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
104 changes: 104 additions & 0 deletions
104
...alClusterTest/java/org/elasticsearch/xpack/inference/integration/CCMStorageServiceIT.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,104 @@ | ||
| /* | ||
| * 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.integration; | ||
|
|
||
| import org.elasticsearch.ElasticsearchException; | ||
| import org.elasticsearch.ResourceNotFoundException; | ||
| import org.elasticsearch.action.DocWriteResponse; | ||
| import org.elasticsearch.action.support.PlainActionFuture; | ||
| import org.elasticsearch.action.support.WriteRequest; | ||
| import org.elasticsearch.common.settings.SecureString; | ||
| import org.elasticsearch.core.TimeValue; | ||
| import org.elasticsearch.plugins.Plugin; | ||
| import org.elasticsearch.test.ESSingleNodeTestCase; | ||
| import org.elasticsearch.xcontent.XContentType; | ||
| import org.elasticsearch.xpack.inference.LocalStateInferencePlugin; | ||
| import org.elasticsearch.xpack.inference.services.elastic.ccm.CCMIndex; | ||
| import org.elasticsearch.xpack.inference.services.elastic.ccm.CCMModel; | ||
| import org.elasticsearch.xpack.inference.services.elastic.ccm.CCMStorageService; | ||
| import org.junit.Before; | ||
|
|
||
| import java.util.Collection; | ||
|
|
||
| import static org.hamcrest.Matchers.containsString; | ||
| import static org.hamcrest.Matchers.is; | ||
|
|
||
| public class CCMStorageServiceIT extends ESSingleNodeTestCase { | ||
| private CCMStorageService ccmStorageService; | ||
|
|
||
| @Before | ||
| public void createComponents() { | ||
| ccmStorageService = node().injector().getInstance(CCMStorageService.class); | ||
| } | ||
|
|
||
| @Override | ||
| protected Collection<Class<? extends Plugin>> getPlugins() { | ||
| return pluginList(LocalStateInferencePlugin.class); | ||
| } | ||
|
|
||
| public void testStoreAndGetCCMModel() { | ||
| var ccmModel = new CCMModel(new SecureString("secret".toCharArray())); | ||
| var storeListener = new PlainActionFuture<Void>(); | ||
| ccmStorageService.store(ccmModel, storeListener); | ||
|
|
||
| assertNull(storeListener.actionGet(TimeValue.THIRTY_SECONDS)); | ||
|
|
||
| var getListener = new PlainActionFuture<CCMModel>(); | ||
| ccmStorageService.get(getListener); | ||
|
|
||
| assertThat(getListener.actionGet(TimeValue.THIRTY_SECONDS), is(ccmModel)); | ||
| } | ||
|
|
||
| public void testGet_ThrowsResourceNotFoundException_WhenCCMIndexDoesNotExist() { | ||
| var getListener = new PlainActionFuture<CCMModel>(); | ||
| ccmStorageService.get(getListener); | ||
|
|
||
| var exception = expectThrows(ResourceNotFoundException.class, () -> getListener.actionGet(TimeValue.THIRTY_SECONDS)); | ||
| assertThat(exception.getMessage(), is("CCM configuration not found")); | ||
| } | ||
|
|
||
| public void testGet_ThrowsResourceNotFoundException_WhenCCMConfigurationDocumentDoesNotExist() { | ||
| storeCorruptCCMModel("id"); | ||
|
|
||
| var getListener = new PlainActionFuture<CCMModel>(); | ||
| ccmStorageService.get(getListener); | ||
|
|
||
| var exception = expectThrows(ResourceNotFoundException.class, () -> getListener.actionGet(TimeValue.THIRTY_SECONDS)); | ||
| assertThat(exception.getMessage(), is("CCM configuration not found")); | ||
| } | ||
|
|
||
| public void testGetCCMModel_ThrowsException_WhenStoredModelIsCorrupted() { | ||
| storeCorruptCCMModel(CCMStorageService.CCM_DOC_ID); | ||
|
|
||
| var getListener = new PlainActionFuture<CCMModel>(); | ||
| ccmStorageService.get(getListener); | ||
|
|
||
| var exception = expectThrows(ElasticsearchException.class, () -> getListener.actionGet(TimeValue.THIRTY_SECONDS)); | ||
| assertThat(exception.getMessage(), containsString("Failed to retrieve CCM configuration")); | ||
| assertThat(exception.getCause().getMessage(), containsString("Required [api_key]")); | ||
| } | ||
|
|
||
| private void storeCorruptCCMModel(String id) { | ||
| var corruptedSource = """ | ||
| { | ||
|
|
||
| } | ||
| """; | ||
|
|
||
| var response = client().prepareIndex() | ||
| .setSource(corruptedSource, XContentType.JSON) | ||
| .setIndex(CCMIndex.INDEX_NAME) | ||
| .setId(id) | ||
| .setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE) | ||
| .execute() | ||
| .actionGet(TimeValue.THIRTY_SECONDS); | ||
|
|
||
| assertThat(response.getResult(), is(DocWriteResponse.Result.CREATED)); | ||
| } | ||
|
|
||
| } |
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
22 changes: 22 additions & 0 deletions
22
.../src/main/java/org/elasticsearch/xpack/inference/services/elastic/ccm/CCMFeatureFlag.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,22 @@ | ||
| /* | ||
| * 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.common.util.FeatureFlag; | ||
|
|
||
| public class CCMFeatureFlag { | ||
|
|
||
| /** | ||
| * {@link org.elasticsearch.xpack.inference.services.custom.CustomService} feature flag. When the feature is complete, | ||
| * this flag will be removed. | ||
| * Enable feature via JVM option: `-Des.inference_api_ccm_feature_flag_enabled=true`. | ||
| */ | ||
| public static final FeatureFlag FEATURE_FLAG = new FeatureFlag("inference_api_ccm"); | ||
|
|
||
| private CCMFeatureFlag() {} | ||
| } |
59 changes: 59 additions & 0 deletions
59
...erence/src/main/java/org/elasticsearch/xpack/inference/services/elastic/ccm/CCMIndex.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,59 @@ | ||
| /* | ||
| * 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.cluster.metadata.IndexMetadata; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.indices.SystemIndexDescriptor; | ||
| import org.elasticsearch.xcontent.XContentBuilder; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
|
|
||
| import static org.elasticsearch.index.mapper.MapperService.SINGLE_MAPPING_NAME; | ||
| import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; | ||
|
|
||
| public class CCMIndex { | ||
|
|
||
| private CCMIndex() {} | ||
|
|
||
| public static final String INDEX_NAME = ".ccm-inference"; | ||
| public static final String INDEX_PATTERN = INDEX_NAME + "*"; | ||
|
|
||
| // Increment this version number when the mappings change | ||
| private static final int INDEX_MAPPING_VERSION = 1; | ||
|
|
||
| public static Settings settings() { | ||
| return builder().build(); | ||
| } | ||
|
|
||
| // Public to allow tests to create the index with custom settings | ||
| public static Settings.Builder builder() { | ||
| return Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_AUTO_EXPAND_REPLICAS, "0-1"); | ||
| } | ||
|
|
||
| public static XContentBuilder mappings() { | ||
| try { | ||
| return jsonBuilder().startObject() | ||
| .startObject(SINGLE_MAPPING_NAME) | ||
| .startObject("_meta") | ||
| .field(SystemIndexDescriptor.VERSION_META_KEY, INDEX_MAPPING_VERSION) | ||
| .endObject() | ||
| .field("dynamic", "strict") | ||
| .startObject("properties") | ||
| .startObject("api_key") | ||
| .field("type", "keyword") | ||
| .endObject() | ||
| .endObject() | ||
| .endObject() | ||
| .endObject(); | ||
| } catch (IOException e) { | ||
| throw new UncheckedIOException("Failed to build mappings for index " + INDEX_NAME, e); | ||
| } | ||
| } | ||
| } | ||
72 changes: 72 additions & 0 deletions
72
...erence/src/main/java/org/elasticsearch/xpack/inference/services/elastic/ccm/CCMModel.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,72 @@ | ||
| /* | ||
| * 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.common.bytes.BytesReference; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.io.stream.StreamOutput; | ||
| import org.elasticsearch.common.io.stream.Writeable; | ||
| import org.elasticsearch.common.settings.SecureString; | ||
| import org.elasticsearch.common.xcontent.XContentHelper; | ||
| import org.elasticsearch.xcontent.ConstructingObjectParser; | ||
| import org.elasticsearch.xcontent.ParseField; | ||
| import org.elasticsearch.xcontent.ToXContent; | ||
| import org.elasticsearch.xcontent.ToXContentObject; | ||
| import org.elasticsearch.xcontent.XContentBuilder; | ||
| import org.elasticsearch.xcontent.XContentParserConfiguration; | ||
| import org.elasticsearch.xcontent.XContentType; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Objects; | ||
|
|
||
| import static org.elasticsearch.xcontent.ConstructingObjectParser.constructorArg; | ||
|
|
||
| public record CCMModel(SecureString apiKey) implements Writeable, ToXContentObject { | ||
|
|
||
| private static final String API_KEY_FIELD = "api_key"; | ||
| private static final ConstructingObjectParser<CCMModel, Void> PARSER = new ConstructingObjectParser<>( | ||
| CCMModel.class.getSimpleName(), | ||
| true, | ||
| args -> new CCMModel(new SecureString(((String) args[0]).toCharArray())) | ||
| ); | ||
|
|
||
| static { | ||
| PARSER.declareString(constructorArg(), new ParseField(API_KEY_FIELD)); | ||
| } | ||
|
|
||
| public static CCMModel parse(org.elasticsearch.xcontent.XContentParser parser) throws IOException { | ||
| return PARSER.parse(parser, null); | ||
| } | ||
|
|
||
| public CCMModel { | ||
| Objects.requireNonNull(apiKey); | ||
| } | ||
|
|
||
| public CCMModel(StreamInput in) throws IOException { | ||
| this(in.readSecureString()); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(StreamOutput out) throws IOException { | ||
| out.writeSecureString(apiKey); | ||
| } | ||
|
|
||
| @Override | ||
| public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
| builder.startObject(); | ||
| builder.field(API_KEY_FIELD, apiKey.toString()); | ||
| builder.endObject(); | ||
| return builder; | ||
| } | ||
|
|
||
| public static CCMModel fromXContentBytes(BytesReference bytes) throws IOException { | ||
| try (var parser = XContentHelper.createParserNotCompressed(XContentParserConfiguration.EMPTY, bytes, XContentType.JSON)) { | ||
| return parse(parser); | ||
| } | ||
| } | ||
| } |
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.
Can we also start with an alias, like what we do for InferenceIndex, in case we ever need to migrate to
.ccm-inference-00002or whatever