|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.inference.integration; |
| 9 | + |
| 10 | +import org.elasticsearch.ElasticsearchException; |
| 11 | +import org.elasticsearch.action.ActionFuture; |
| 12 | +import org.elasticsearch.action.search.SearchPhaseExecutionException; |
| 13 | +import org.elasticsearch.common.bytes.BytesReference; |
| 14 | +import org.elasticsearch.common.settings.Settings; |
| 15 | +import org.elasticsearch.core.TimeValue; |
| 16 | +import org.elasticsearch.inference.InferenceServiceExtension; |
| 17 | +import org.elasticsearch.inference.TaskType; |
| 18 | +import org.elasticsearch.license.LicenseSettings; |
| 19 | +import org.elasticsearch.license.XPackLicenseState; |
| 20 | +import org.elasticsearch.plugins.Plugin; |
| 21 | +import org.elasticsearch.test.ESIntegTestCase; |
| 22 | +import org.elasticsearch.test.ESTestCase; |
| 23 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 24 | +import org.elasticsearch.xcontent.XContentFactory; |
| 25 | +import org.elasticsearch.xcontent.XContentType; |
| 26 | +import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; |
| 27 | +import org.elasticsearch.xpack.core.inference.InferenceContext; |
| 28 | +import org.elasticsearch.xpack.core.inference.action.GetInferenceModelAction; |
| 29 | +import org.elasticsearch.xpack.core.inference.action.InferenceAction; |
| 30 | +import org.elasticsearch.xpack.core.inference.action.InferenceActionProxy; |
| 31 | +import org.elasticsearch.xpack.core.inference.action.PutInferenceModelAction; |
| 32 | +import org.elasticsearch.xpack.core.ssl.SSLService; |
| 33 | +import org.elasticsearch.xpack.inference.InferenceIndex; |
| 34 | +import org.elasticsearch.xpack.inference.InferencePlugin; |
| 35 | +import org.elasticsearch.xpack.inference.InferenceSecretsIndex; |
| 36 | +import org.elasticsearch.xpack.inference.mock.TestDenseInferenceServiceExtension; |
| 37 | +import org.elasticsearch.xpack.inference.mock.TestInferenceServicePlugin; |
| 38 | +import org.elasticsearch.xpack.inference.mock.TestSparseInferenceServiceExtension; |
| 39 | + |
| 40 | +import java.io.IOException; |
| 41 | +import java.nio.file.Path; |
| 42 | +import java.util.Collection; |
| 43 | +import java.util.List; |
| 44 | +import java.util.Map; |
| 45 | + |
| 46 | +import static org.hamcrest.CoreMatchers.containsString; |
| 47 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 48 | +import static org.hamcrest.Matchers.instanceOf; |
| 49 | + |
| 50 | +@ESTestCase.WithoutEntitlements // due to dependency issue ES-12435 |
| 51 | +public class InferenceIndicesIT extends ESIntegTestCase { |
| 52 | + |
| 53 | + private static final String INDEX_ROUTER_ATTRIBUTE = "node.attr.index_router"; |
| 54 | + private static final String CONFIG_ROUTER = "config"; |
| 55 | + private static final String SECRETS_ROUTER = "secrets"; |
| 56 | + |
| 57 | + private static final Map<String, Object> TEST_SERVICE_SETTINGS = Map.of( |
| 58 | + "model", |
| 59 | + "my_model", |
| 60 | + "dimensions", |
| 61 | + 256, |
| 62 | + "similarity", |
| 63 | + "cosine", |
| 64 | + "api_key", |
| 65 | + "my_api_key" |
| 66 | + ); |
| 67 | + |
| 68 | + public static class LocalStateIndexSettingsInferencePlugin extends LocalStateCompositeXPackPlugin { |
| 69 | + private final InferencePlugin inferencePlugin; |
| 70 | + |
| 71 | + public LocalStateIndexSettingsInferencePlugin(final Settings settings, final Path configPath) throws Exception { |
| 72 | + super(settings, configPath); |
| 73 | + var thisVar = this; |
| 74 | + this.inferencePlugin = new InferencePlugin(settings) { |
| 75 | + @Override |
| 76 | + protected SSLService getSslService() { |
| 77 | + return thisVar.getSslService(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected XPackLicenseState getLicenseState() { |
| 82 | + return thisVar.getLicenseState(); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public List<InferenceServiceExtension.Factory> getInferenceServiceFactories() { |
| 87 | + return List.of( |
| 88 | + TestSparseInferenceServiceExtension.TestInferenceService::new, |
| 89 | + TestDenseInferenceServiceExtension.TestInferenceService::new |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public Settings getIndexSettings() { |
| 95 | + return InferenceIndex.builder() |
| 96 | + .put(Settings.builder().put("index.routing.allocation.require.index_router", "config").build()) |
| 97 | + .build(); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public Settings getSecretsIndexSettings() { |
| 102 | + return InferenceSecretsIndex.builder() |
| 103 | + .put(Settings.builder().put("index.routing.allocation.require.index_router", "secrets").build()) |
| 104 | + .build(); |
| 105 | + } |
| 106 | + }; |
| 107 | + plugins.add(inferencePlugin); |
| 108 | + } |
| 109 | + |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + protected Settings nodeSettings(int nodeOrdinal, Settings otherSettings) { |
| 114 | + return Settings.builder().put(LicenseSettings.SELF_GENERATED_LICENSE_TYPE.getKey(), "trial").build(); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 119 | + return List.of(LocalStateIndexSettingsInferencePlugin.class, TestInferenceServicePlugin.class); |
| 120 | + } |
| 121 | + |
| 122 | + public void testRetrievingInferenceEndpoint_ThrowsException_WhenIndexNodeIsNotAvailable() throws Exception { |
| 123 | + final var configIndexNodeAttributes = Settings.builder().put(INDEX_ROUTER_ATTRIBUTE, CONFIG_ROUTER).build(); |
| 124 | + |
| 125 | + internalCluster().startMasterOnlyNode(configIndexNodeAttributes); |
| 126 | + final var configIndexDataNodes = internalCluster().startDataOnlyNode(configIndexNodeAttributes); |
| 127 | + |
| 128 | + internalCluster().startDataOnlyNode(Settings.builder().put(INDEX_ROUTER_ATTRIBUTE, SECRETS_ROUTER).build()); |
| 129 | + |
| 130 | + final var inferenceId = "test-index-id"; |
| 131 | + createInferenceEndpoint(TaskType.TEXT_EMBEDDING, inferenceId, TEST_SERVICE_SETTINGS); |
| 132 | + |
| 133 | + // Ensure the inference indices are created and we can retrieve the inference endpoint |
| 134 | + var getInferenceEndpointRequest = new GetInferenceModelAction.Request(inferenceId, TaskType.TEXT_EMBEDDING, true); |
| 135 | + var responseFuture = client().execute(GetInferenceModelAction.INSTANCE, getInferenceEndpointRequest); |
| 136 | + assertThat(responseFuture.actionGet(TEST_REQUEST_TIMEOUT).getEndpoints().get(0).getInferenceEntityId(), equalTo(inferenceId)); |
| 137 | + |
| 138 | + // stop the node that holds the inference index |
| 139 | + internalCluster().stopNode(configIndexDataNodes); |
| 140 | + |
| 141 | + var responseFailureFuture = client().execute(GetInferenceModelAction.INSTANCE, getInferenceEndpointRequest); |
| 142 | + var exception = expectThrows(ElasticsearchException.class, () -> responseFailureFuture.actionGet(TEST_REQUEST_TIMEOUT)); |
| 143 | + assertThat(exception.toString(), containsString("Failed to load inference endpoint [test-index-id]")); |
| 144 | + |
| 145 | + var causeException = exception.getCause(); |
| 146 | + assertThat(causeException, instanceOf(SearchPhaseExecutionException.class)); |
| 147 | + } |
| 148 | + |
| 149 | + public void testRetrievingInferenceEndpoint_ThrowsException_WhenIndexNodeIsNotAvailable_ForInferenceAction() throws Exception { |
| 150 | + final var configIndexNodeAttributes = Settings.builder().put(INDEX_ROUTER_ATTRIBUTE, CONFIG_ROUTER).build(); |
| 151 | + |
| 152 | + internalCluster().startMasterOnlyNode(configIndexNodeAttributes); |
| 153 | + final var configIndexDataNodes = internalCluster().startDataOnlyNode(configIndexNodeAttributes); |
| 154 | + |
| 155 | + internalCluster().startDataOnlyNode(Settings.builder().put(INDEX_ROUTER_ATTRIBUTE, SECRETS_ROUTER).build()); |
| 156 | + |
| 157 | + final var inferenceId = "test-index-id-2"; |
| 158 | + createInferenceEndpoint(TaskType.TEXT_EMBEDDING, inferenceId, TEST_SERVICE_SETTINGS); |
| 159 | + |
| 160 | + // Ensure the inference indices are created and we can retrieve the inference endpoint |
| 161 | + var getInferenceEndpointRequest = new GetInferenceModelAction.Request(inferenceId, TaskType.TEXT_EMBEDDING, true); |
| 162 | + var responseFuture = client().execute(GetInferenceModelAction.INSTANCE, getInferenceEndpointRequest); |
| 163 | + assertThat(responseFuture.actionGet(TEST_REQUEST_TIMEOUT).getEndpoints().get(0).getInferenceEntityId(), equalTo(inferenceId)); |
| 164 | + |
| 165 | + // stop the node that holds the inference index |
| 166 | + internalCluster().stopNode(configIndexDataNodes); |
| 167 | + |
| 168 | + var proxyResponse = sendInferenceProxyRequest(inferenceId); |
| 169 | + var exception = expectThrows(ElasticsearchException.class, () -> proxyResponse.actionGet(TEST_REQUEST_TIMEOUT)); |
| 170 | + assertThat(exception.toString(), containsString("Failed to load inference endpoint with secrets [test-index-id-2]")); |
| 171 | + |
| 172 | + var causeException = exception.getCause(); |
| 173 | + assertThat(causeException, instanceOf(SearchPhaseExecutionException.class)); |
| 174 | + } |
| 175 | + |
| 176 | + public void testRetrievingInferenceEndpoint_ThrowsException_WhenSecretsIndexNodeIsNotAvailable() throws Exception { |
| 177 | + final var configIndexNodeAttributes = Settings.builder().put(INDEX_ROUTER_ATTRIBUTE, CONFIG_ROUTER).build(); |
| 178 | + internalCluster().startMasterOnlyNode(configIndexNodeAttributes); |
| 179 | + internalCluster().startDataOnlyNode(configIndexNodeAttributes); |
| 180 | + |
| 181 | + var secretIndexDataNodes = internalCluster().startDataOnlyNode( |
| 182 | + Settings.builder().put(INDEX_ROUTER_ATTRIBUTE, SECRETS_ROUTER).build() |
| 183 | + ); |
| 184 | + |
| 185 | + final var inferenceId = "test-secrets-index-id"; |
| 186 | + createInferenceEndpoint(TaskType.TEXT_EMBEDDING, inferenceId, TEST_SERVICE_SETTINGS); |
| 187 | + |
| 188 | + // Ensure the inference indices are created and we can retrieve the inference endpoint |
| 189 | + var getInferenceEndpointRequest = new GetInferenceModelAction.Request(inferenceId, TaskType.TEXT_EMBEDDING, true); |
| 190 | + var responseFuture = client().execute(GetInferenceModelAction.INSTANCE, getInferenceEndpointRequest); |
| 191 | + assertThat(responseFuture.actionGet(TEST_REQUEST_TIMEOUT).getEndpoints().get(0).getInferenceEntityId(), equalTo(inferenceId)); |
| 192 | + |
| 193 | + // stop the node that holds the inference secrets index |
| 194 | + internalCluster().stopNode(secretIndexDataNodes); |
| 195 | + |
| 196 | + var proxyResponse = sendInferenceProxyRequest(inferenceId); |
| 197 | + |
| 198 | + var exception = expectThrows(ElasticsearchException.class, () -> proxyResponse.actionGet(TEST_REQUEST_TIMEOUT)); |
| 199 | + assertThat(exception.toString(), containsString("Failed to load inference endpoint with secrets [test-secrets-index-id]")); |
| 200 | + |
| 201 | + var causeException = exception.getCause(); |
| 202 | + |
| 203 | + assertThat(causeException, instanceOf(SearchPhaseExecutionException.class)); |
| 204 | + } |
| 205 | + |
| 206 | + private ActionFuture<InferenceAction.Response> sendInferenceProxyRequest(String inferenceId) throws IOException { |
| 207 | + final BytesReference content; |
| 208 | + try (XContentBuilder builder = XContentFactory.jsonBuilder()) { |
| 209 | + builder.startObject(); |
| 210 | + builder.field("input", List.of("test input")); |
| 211 | + builder.endObject(); |
| 212 | + |
| 213 | + content = BytesReference.bytes(builder); |
| 214 | + } |
| 215 | + |
| 216 | + var inferenceRequest = new InferenceActionProxy.Request( |
| 217 | + TaskType.TEXT_EMBEDDING, |
| 218 | + inferenceId, |
| 219 | + content, |
| 220 | + XContentType.JSON, |
| 221 | + TimeValue.THIRTY_SECONDS, |
| 222 | + false, |
| 223 | + InferenceContext.EMPTY_INSTANCE |
| 224 | + ); |
| 225 | + |
| 226 | + return client().execute(InferenceActionProxy.INSTANCE, inferenceRequest); |
| 227 | + } |
| 228 | + |
| 229 | + private void createInferenceEndpoint(TaskType taskType, String inferenceId, Map<String, Object> serviceSettings) throws IOException { |
| 230 | + var responseFuture = createInferenceEndpointAsync(taskType, inferenceId, serviceSettings); |
| 231 | + assertThat(responseFuture.actionGet(TEST_REQUEST_TIMEOUT).getModel().getInferenceEntityId(), equalTo(inferenceId)); |
| 232 | + } |
| 233 | + |
| 234 | + private ActionFuture<PutInferenceModelAction.Response> createInferenceEndpointAsync( |
| 235 | + TaskType taskType, |
| 236 | + String inferenceId, |
| 237 | + Map<String, Object> serviceSettings |
| 238 | + ) throws IOException { |
| 239 | + final BytesReference content; |
| 240 | + try (XContentBuilder builder = XContentFactory.jsonBuilder()) { |
| 241 | + builder.startObject(); |
| 242 | + builder.field("service", TestDenseInferenceServiceExtension.TestInferenceService.NAME); |
| 243 | + builder.field("service_settings", serviceSettings); |
| 244 | + builder.endObject(); |
| 245 | + |
| 246 | + content = BytesReference.bytes(builder); |
| 247 | + } |
| 248 | + |
| 249 | + var request = new PutInferenceModelAction.Request(taskType, inferenceId, content, XContentType.JSON, TEST_REQUEST_TIMEOUT); |
| 250 | + return client().execute(PutInferenceModelAction.INSTANCE, request); |
| 251 | + } |
| 252 | +} |
0 commit comments