Skip to content

Commit 2869085

Browse files
committed
another test
1 parent 485232c commit 2869085

File tree

5 files changed

+68
-39
lines changed

5 files changed

+68
-39
lines changed

x-pack/plugin/inference/qa/inference-service-tests/src/javaRestTest/java/org/elasticsearch/xpack/inference/CreateFromDeploymentIT.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,25 @@
99

1010
import org.elasticsearch.client.Request;
1111
import org.elasticsearch.client.Response;
12+
import org.elasticsearch.client.ResponseException;
1213
import org.elasticsearch.core.Strings;
1314
import org.elasticsearch.inference.TaskType;
1415

1516
import java.io.IOException;
1617
import java.util.List;
1718
import java.util.Map;
1819

20+
import static org.hamcrest.Matchers.containsString;
1921
import static org.hamcrest.Matchers.is;
2022

21-
public class CreateFromDeploymentIT extends CustomElandModelIT {
23+
public class CreateFromDeploymentIT extends InferenceBaseRestTest {
2224

2325
@SuppressWarnings("unchecked")
2426
public void testAttachToDeployment() throws IOException {
2527
var modelId = "attach_to_deployment";
2628
var deploymentId = "existing_deployment";
2729

28-
createMlNodeTextExpansionModel(modelId);
30+
CustomElandModelIT.createMlNodeTextExpansionModel(modelId, client());
2931
var response = startMlNodeDeploymemnt(modelId, deploymentId);
3032
assertOkOrCreated(response);
3133

@@ -54,7 +56,7 @@ public void testAttachWithModelId() throws IOException {
5456
var modelId = "attach_with_model_id";
5557
var deploymentId = "existing_deployment_with_model_id";
5658

57-
createMlNodeTextExpansionModel(modelId);
59+
CustomElandModelIT.createMlNodeTextExpansionModel(modelId, client());
5860
var response = startMlNodeDeploymemnt(modelId, deploymentId);
5961
assertOkOrCreated(response);
6062

@@ -84,6 +86,29 @@ public void testAttachWithModelId() throws IOException {
8486
stopMlNodeDeployment(deploymentId);
8587
}
8688

89+
public void testModelIdDoesNotMatch() throws IOException {
90+
var modelId = "attach_with_model_id";
91+
var deploymentId = "existing_deployment_with_model_id";
92+
var aDifferentModelId = "not_the_same_as_the_one_used_in_the_deployment";
93+
94+
CustomElandModelIT.createMlNodeTextExpansionModel(modelId, client());
95+
var response = startMlNodeDeploymemnt(modelId, deploymentId);
96+
assertOkOrCreated(response);
97+
98+
var inferenceId = "inference_on_existing_deployment";
99+
var e = expectThrows(
100+
ResponseException.class,
101+
() -> putModel(inferenceId, endpointConfig(aDifferentModelId, deploymentId), TaskType.SPARSE_EMBEDDING)
102+
);
103+
assertThat(
104+
e.getMessage(),
105+
containsString(
106+
"Deployment [existing_deployment_with_model_id] uses model [attach_with_model_id] "
107+
+ "which does not match the model [not_the_same_as_the_one_used_in_the_deployment] in the request."
108+
)
109+
);
110+
}
111+
87112
private String endpointConfig(String deploymentId) {
88113
return Strings.format("""
89114
{

x-pack/plugin/inference/qa/inference-service-tests/src/javaRestTest/java/org/elasticsearch/xpack/inference/CustomElandModelIT.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
package org.elasticsearch.xpack.inference;
99

1010
import org.elasticsearch.client.Request;
11+
import org.elasticsearch.client.RestClient;
1112
import org.elasticsearch.core.Strings;
1213
import org.elasticsearch.inference.TaskType;
1314

@@ -65,11 +66,12 @@ public class CustomElandModelIT extends InferenceBaseRestTest {
6566
public void testSparse() throws IOException {
6667
String modelId = "custom-text-expansion-model";
6768

68-
createTextExpansionModel(modelId);
69-
putModelDefinition(modelId, BASE_64_ENCODED_MODEL, RAW_MODEL_SIZE);
69+
createTextExpansionModel(modelId, client());
70+
putModelDefinition(modelId, BASE_64_ENCODED_MODEL, RAW_MODEL_SIZE, client());
7071
putVocabulary(
7172
List.of("these", "are", "my", "words", "the", "washing", "machine", "is", "leaking", "octopus", "comforter", "smells"),
72-
modelId
73+
modelId,
74+
client()
7375
);
7476

7577
var inferenceConfig = """
@@ -90,7 +92,7 @@ public void testSparse() throws IOException {
9092
assertNotNull(results.get("sparse_embedding"));
9193
}
9294

93-
protected void createTextExpansionModel(String modelId) throws IOException {
95+
static void createTextExpansionModel(String modelId, RestClient client) throws IOException {
9496
// with_special_tokens: false for this test with limited vocab
9597
Request request = new Request("PUT", "/_ml/trained_models/" + modelId);
9698
request.setJsonEntity("""
@@ -107,10 +109,10 @@ protected void createTextExpansionModel(String modelId) throws IOException {
107109
}
108110
}
109111
}""");
110-
client().performRequest(request);
112+
client.performRequest(request);
111113
}
112114

113-
protected void putVocabulary(List<String> vocabulary, String modelId) throws IOException {
115+
static void putVocabulary(List<String> vocabulary, String modelId, RestClient client) throws IOException {
114116
List<String> vocabularyWithPad = new ArrayList<>();
115117
vocabularyWithPad.add("[PAD]");
116118
vocabularyWithPad.add("[UNK]");
@@ -121,24 +123,26 @@ protected void putVocabulary(List<String> vocabulary, String modelId) throws IOE
121123
request.setJsonEntity(Strings.format("""
122124
{ "vocabulary": [%s] }
123125
""", quotedWords));
124-
client().performRequest(request);
126+
client.performRequest(request);
125127
}
126128

127-
protected void putModelDefinition(String modelId, String base64EncodedModel, long unencodedModelSize) throws IOException {
129+
static void putModelDefinition(String modelId, String base64EncodedModel, long unencodedModelSize, RestClient client)
130+
throws IOException {
128131
Request request = new Request("PUT", "_ml/trained_models/" + modelId + "/definition/0");
129132
String body = Strings.format("""
130133
{"total_definition_length":%s,"definition": "%s","total_parts": 1}""", unencodedModelSize, base64EncodedModel);
131134
request.setJsonEntity(body);
132-
client().performRequest(request);
135+
client.performRequest(request);
133136
}
134137

135138
// Create the model including definition and vocab
136-
protected void createMlNodeTextExpansionModel(String modelId) throws IOException {
137-
createTextExpansionModel(modelId);
138-
putModelDefinition(modelId, BASE_64_ENCODED_MODEL, RAW_MODEL_SIZE);
139+
static void createMlNodeTextExpansionModel(String modelId, RestClient client) throws IOException {
140+
createTextExpansionModel(modelId, client);
141+
putModelDefinition(modelId, BASE_64_ENCODED_MODEL, RAW_MODEL_SIZE, client);
139142
putVocabulary(
140143
List.of("these", "are", "my", "words", "the", "washing", "machine", "is", "leaking", "octopus", "comforter", "smells"),
141-
modelId
144+
modelId,
145+
client
142146
);
143147
}
144148

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/elasticsearch/BaseElasticsearchInternalService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ public void start(Model model, ActionListener<Boolean> finalListener) {
9999
return;
100100
}
101101

102+
if (esModel.usesExistingDeployment()) {
103+
// don't start a deployment
104+
finalListener.onResponse(Boolean.TRUE);
105+
return;
106+
}
107+
102108
SubscribableListener.<Boolean>newForked(forkedListener -> { isBuiltinModelPut(model, forkedListener); })
103109
.<Boolean>andThen((l, modelConfigExists) -> {
104110
if (modelConfigExists == false) {
@@ -129,6 +135,7 @@ public void stop(UnparsedModel unparsedModel, ActionListener<Boolean> listener)
129135
if (serviceSettings.getDeploymentId() != null) {
130136
// configured with an existing deployment so do not stop it
131137
listener.onResponse(Boolean.TRUE);
138+
return;
132139
}
133140

134141
var request = new StopTrainedModelDeploymentAction.Request(esModel.mlNodeDeploymentId());

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/elasticsearch/ElasticDeployedModel.java

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77

88
package org.elasticsearch.xpack.inference.services.elasticsearch;
99

10-
import org.elasticsearch.ResourceNotFoundException;
1110
import org.elasticsearch.action.ActionListener;
1211
import org.elasticsearch.inference.ChunkingSettings;
1312
import org.elasticsearch.inference.Model;
1413
import org.elasticsearch.inference.TaskType;
1514
import org.elasticsearch.xpack.core.ml.action.CreateTrainedModelAssignmentAction;
16-
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
15+
import org.elasticsearch.xpack.core.ml.action.StartTrainedModelDeploymentAction;
1716

1817
public class ElasticDeployedModel extends ElasticsearchInternalModel {
1918
public ElasticDeployedModel(
@@ -26,31 +25,21 @@ public ElasticDeployedModel(
2625
super(inferenceEntityId, taskType, service, serviceSettings, chunkingSettings);
2726
}
2827

28+
@Override
29+
public boolean usesExistingDeployment() {
30+
return true;
31+
}
32+
33+
@Override
34+
public StartTrainedModelDeploymentAction.Request getStartTrainedModelDeploymentActionRequest() {
35+
throw new IllegalStateException("cannot start model that uses an existing deployment");
36+
}
37+
2938
@Override
3039
public ActionListener<CreateTrainedModelAssignmentAction.Response> getCreateTrainedModelAssignmentActionListener(
3140
Model model,
3241
ActionListener<Boolean> listener
3342
) {
34-
return new ActionListener<>() {
35-
@Override
36-
public void onResponse(CreateTrainedModelAssignmentAction.Response response) {
37-
listener.onResponse(Boolean.TRUE);
38-
}
39-
40-
@Override
41-
public void onFailure(Exception e) {
42-
if (ExceptionsHelper.unwrapCause(e) instanceof ResourceNotFoundException) {
43-
listener.onFailure(
44-
new ResourceNotFoundException(
45-
"Could not start the inference as the deploymend model [{0}] cannot be found."
46-
+ " Trained Models must be deployed before they can be started.",
47-
internalServiceSettings.modelId()
48-
)
49-
);
50-
return;
51-
}
52-
listener.onFailure(e);
53-
}
54-
};
43+
throw new IllegalStateException("cannot start model that uses an existing deployment");
5544
}
5645
}

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/services/elasticsearch/ElasticsearchInternalModel.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ public abstract ActionListener<CreateTrainedModelAssignmentAction.Response> getC
8282
ActionListener<Boolean> listener
8383
);
8484

85+
public boolean usesExistingDeployment() {
86+
return internalServiceSettings.getDeploymentId() != null;
87+
}
88+
8589
@Override
8690
public ElasticsearchInternalServiceSettings getServiceSettings() {
8791
return (ElasticsearchInternalServiceSettings) super.getServiceSettings();

0 commit comments

Comments
 (0)