Skip to content

Commit e7ba9f4

Browse files
[ML] Fix loss of context in the inference API for streaming APIs (#118999) (#119223)
* Adding context preserving fix * Update docs/changelog/118999.yaml * Update docs/changelog/118999.yaml * Using a setonce and adding a test * Updating the changelog (cherry picked from commit 7ba3cb9) # Conflicts: # x-pack/plugin/inference/qa/inference-service-tests/src/javaRestTest/java/org/elasticsearch/xpack/inference/InferenceBaseRestTest.java # x-pack/plugin/inference/qa/inference-service-tests/src/javaRestTest/java/org/elasticsearch/xpack/inference/InferenceCrudIT.java # x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/InferencePlugin.java # x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/rest/RestUnifiedCompletionInferenceAction.java # x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/rest/RestUnifiedCompletionInferenceActionTests.java
1 parent 5db05ea commit e7ba9f4

File tree

8 files changed

+94
-16
lines changed

8 files changed

+94
-16
lines changed

docs/changelog/118999.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 118999
2+
summary: Fix loss of context in the inference API for streaming APIs
3+
area: Machine Learning
4+
type: bug
5+
issues:
6+
- 119000

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.Map;
3131
import java.util.concurrent.CountDownLatch;
3232
import java.util.concurrent.TimeUnit;
33+
import java.util.function.Consumer;
3334

3435
import static org.hamcrest.Matchers.anyOf;
3536
import static org.hamcrest.Matchers.equalTo;
@@ -318,20 +319,34 @@ protected Map<String, Object> infer(String modelId, List<String> input) throws I
318319
return inferInternal(endpoint, input, Map.of());
319320
}
320321

321-
protected Deque<ServerSentEvent> streamInferOnMockService(String modelId, TaskType taskType, List<String> input) throws Exception {
322+
protected Deque<ServerSentEvent> streamInferOnMockService(
323+
String modelId,
324+
TaskType taskType,
325+
List<String> input,
326+
@Nullable Consumer<Response> responseConsumerCallback
327+
) throws Exception {
322328
var endpoint = Strings.format("_inference/%s/%s/_stream", taskType, modelId);
323-
return callAsync(endpoint, input);
329+
return callAsync(endpoint, input, responseConsumerCallback);
324330
}
325331

326-
private Deque<ServerSentEvent> callAsync(String endpoint, List<String> input) throws Exception {
327-
var responseConsumer = new AsyncInferenceResponseConsumer();
332+
private Deque<ServerSentEvent> callAsync(String endpoint, List<String> input, @Nullable Consumer<Response> responseConsumerCallback)
333+
throws Exception {
328334
var request = new Request("POST", endpoint);
329335
request.setJsonEntity(jsonBody(input));
336+
337+
return execAsyncCall(request, responseConsumerCallback);
338+
}
339+
340+
private Deque<ServerSentEvent> execAsyncCall(Request request, @Nullable Consumer<Response> responseConsumerCallback) throws Exception {
341+
var responseConsumer = new AsyncInferenceResponseConsumer();
330342
request.setOptions(RequestOptions.DEFAULT.toBuilder().setHttpAsyncResponseConsumerFactory(() -> responseConsumer).build());
331343
var latch = new CountDownLatch(1);
332344
client().performRequestAsync(request, new ResponseListener() {
333345
@Override
334346
public void onSuccess(Response response) {
347+
if (responseConsumerCallback != null) {
348+
responseConsumerCallback.accept(response);
349+
}
335350
latch.countDown();
336351
}
337352

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
package org.elasticsearch.xpack.inference;
1111

1212
import org.apache.http.util.EntityUtils;
13+
import org.elasticsearch.client.Response;
1314
import org.elasticsearch.client.ResponseException;
1415
import org.elasticsearch.common.settings.Settings;
1516
import org.elasticsearch.inference.TaskType;
@@ -19,6 +20,7 @@
1920
import java.util.Map;
2021
import java.util.Objects;
2122
import java.util.Set;
23+
import java.util.function.Consumer;
2224
import java.util.function.Function;
2325
import java.util.stream.IntStream;
2426
import java.util.stream.Stream;
@@ -28,9 +30,15 @@
2830
import static org.hamcrest.Matchers.equalTo;
2931
import static org.hamcrest.Matchers.equalToIgnoringCase;
3032
import static org.hamcrest.Matchers.hasSize;
33+
import static org.hamcrest.Matchers.is;
3134

3235
public class InferenceCrudIT extends InferenceBaseRestTest {
3336

37+
private static final Consumer<Response> VALIDATE_ELASTIC_PRODUCT_HEADER_CONSUMER = (r) -> assertThat(
38+
r.getHeader("X-elastic-product"),
39+
is("Elasticsearch")
40+
);
41+
3442
@SuppressWarnings("unchecked")
3543
public void testCRUD() throws IOException {
3644
for (int i = 0; i < 5; i++) {
@@ -282,7 +290,7 @@ public void testUnsupportedStream() throws Exception {
282290
assertEquals(TaskType.SPARSE_EMBEDDING.toString(), singleModel.get("task_type"));
283291

284292
try {
285-
var events = streamInferOnMockService(modelId, TaskType.SPARSE_EMBEDDING, List.of(randomAlphaOfLength(10)));
293+
var events = streamInferOnMockService(modelId, TaskType.SPARSE_EMBEDDING, List.of(randomAlphaOfLength(10)), null);
286294
assertThat(events.size(), equalTo(2));
287295
events.forEach(event -> {
288296
switch (event.name()) {
@@ -310,7 +318,7 @@ public void testSupportedStream() throws Exception {
310318
var input = IntStream.range(1, randomInt(10)).mapToObj(i -> randomAlphaOfLength(10)).toList();
311319

312320
try {
313-
var events = streamInferOnMockService(modelId, TaskType.COMPLETION, input);
321+
var events = streamInferOnMockService(modelId, TaskType.COMPLETION, input, VALIDATE_ELASTIC_PRODUCT_HEADER_CONSUMER);
314322

315323
var expectedResponses = Stream.concat(
316324
input.stream().map(String::toUpperCase).map(str -> "{\"completion\":[{\"delta\":\"" + str + "\"}]}"),

x-pack/plugin/inference/src/internalClusterTest/java/org/elasticsearch/xpack/inference/rest/ServerSentEventsRestActionListenerTests.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.apache.http.nio.util.SimpleInputBuffer;
1818
import org.apache.http.protocol.HttpContext;
1919
import org.apache.http.util.EntityUtils;
20+
import org.apache.lucene.util.SetOnce;
2021
import org.elasticsearch.client.Request;
2122
import org.elasticsearch.client.RequestOptions;
2223
import org.elasticsearch.client.Response;
@@ -43,6 +44,7 @@
4344
import org.elasticsearch.rest.RestHandler;
4445
import org.elasticsearch.rest.RestRequest;
4546
import org.elasticsearch.test.ESIntegTestCase;
47+
import org.elasticsearch.threadpool.ThreadPool;
4648
import org.elasticsearch.xcontent.ToXContent;
4749
import org.elasticsearch.xpack.core.inference.action.InferenceAction;
4850
import org.elasticsearch.xpack.inference.external.response.streaming.ServerSentEvent;
@@ -52,6 +54,7 @@
5254
import java.io.IOException;
5355
import java.nio.charset.StandardCharsets;
5456
import java.util.Collection;
57+
import java.util.Collections;
5558
import java.util.Deque;
5659
import java.util.Iterator;
5760
import java.util.List;
@@ -97,6 +100,14 @@ protected Collection<Class<? extends Plugin>> nodePlugins() {
97100
}
98101

99102
public static class StreamingPlugin extends Plugin implements ActionPlugin {
103+
private final SetOnce<ThreadPool> threadPool = new SetOnce<>();
104+
105+
@Override
106+
public Collection<?> createComponents(PluginServices services) {
107+
threadPool.set(services.threadPool());
108+
return Collections.emptyList();
109+
}
110+
100111
@Override
101112
public Collection<RestHandler> getRestHandlers(
102113
Settings settings,
@@ -123,7 +134,7 @@ public void handleRequest(RestRequest request, RestChannel channel, NodeClient c
123134
var publisher = new RandomPublisher(requestCount, withError);
124135
var inferenceServiceResults = new StreamingInferenceServiceResults(publisher);
125136
var inferenceResponse = new InferenceAction.Response(inferenceServiceResults, inferenceServiceResults.publisher());
126-
new ServerSentEventsRestActionListener(channel).onResponse(inferenceResponse);
137+
new ServerSentEventsRestActionListener(channel, threadPool).onResponse(inferenceResponse);
127138
}
128139
}, new RestHandler() {
129140
@Override
@@ -133,7 +144,7 @@ public List<Route> routes() {
133144

134145
@Override
135146
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) {
136-
new ServerSentEventsRestActionListener(channel).onFailure(expectedException);
147+
new ServerSentEventsRestActionListener(channel, threadPool).onFailure(expectedException);
137148
}
138149
}, new RestHandler() {
139150
@Override
@@ -144,7 +155,7 @@ public List<Route> routes() {
144155
@Override
145156
public void handleRequest(RestRequest request, RestChannel channel, NodeClient client) {
146157
var inferenceResponse = new InferenceAction.Response(new SingleInferenceServiceResults());
147-
new ServerSentEventsRestActionListener(channel).onResponse(inferenceResponse);
158+
new ServerSentEventsRestActionListener(channel, threadPool).onResponse(inferenceResponse);
148159
}
149160
});
150161
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.elasticsearch.search.rank.RankDoc;
4040
import org.elasticsearch.threadpool.ExecutorBuilder;
4141
import org.elasticsearch.threadpool.ScalingExecutorBuilder;
42+
import org.elasticsearch.threadpool.ThreadPool;
4243
import org.elasticsearch.xcontent.ParseField;
4344
import org.elasticsearch.xpack.core.ClientHelper;
4445
import org.elasticsearch.xpack.core.action.XPackUsageFeatureAction;
@@ -138,6 +139,9 @@ public class InferencePlugin extends Plugin implements ActionPlugin, ExtensibleP
138139
private final SetOnce<AmazonBedrockRequestSender.Factory> amazonBedrockFactory = new SetOnce<>();
139140
private final SetOnce<ServiceComponents> serviceComponents = new SetOnce<>();
140141
private final SetOnce<ElasticInferenceServiceComponents> eisComponents = new SetOnce<>();
142+
// This is mainly so that the rest handlers can access the ThreadPool in a way that avoids potential null pointers from it
143+
// not being initialized yet
144+
private final SetOnce<ThreadPool> threadPoolSetOnce = new SetOnce<>();
141145
private final SetOnce<InferenceServiceRegistry> inferenceServiceRegistry = new SetOnce<>();
142146
private final SetOnce<ShardBulkInferenceActionFilter> shardBulkInferenceActionFilter = new SetOnce<>();
143147
private List<InferenceServiceExtension> inferenceServiceExtensions;
@@ -173,7 +177,7 @@ public List<RestHandler> getRestHandlers(
173177
) {
174178
return List.of(
175179
new RestInferenceAction(),
176-
new RestStreamInferenceAction(),
180+
new RestStreamInferenceAction(threadPoolSetOnce),
177181
new RestGetInferenceModelAction(),
178182
new RestPutInferenceModelAction(),
179183
new RestUpdateInferenceModelAction(),
@@ -187,6 +191,7 @@ public Collection<?> createComponents(PluginServices services) {
187191
var throttlerManager = new ThrottlerManager(settings, services.threadPool(), services.clusterService());
188192
var truncator = new Truncator(settings, services.clusterService());
189193
serviceComponents.set(new ServiceComponents(services.threadPool(), throttlerManager, settings, truncator));
194+
threadPoolSetOnce.set(services.threadPool());
190195

191196
var httpClientManager = HttpClientManager.create(settings, services.threadPool(), services.clusterService(), throttlerManager);
192197
var httpRequestSenderFactory = new HttpRequestSender.Factory(serviceComponents.get(), httpClientManager, services.clusterService());

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/rest/RestStreamInferenceAction.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,30 @@
77

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

10+
import org.apache.lucene.util.SetOnce;
1011
import org.elasticsearch.action.ActionListener;
1112
import org.elasticsearch.rest.RestChannel;
1213
import org.elasticsearch.rest.Scope;
1314
import org.elasticsearch.rest.ServerlessScope;
15+
import org.elasticsearch.threadpool.ThreadPool;
1416
import org.elasticsearch.xpack.core.inference.action.InferenceAction;
1517

1618
import java.util.List;
19+
import java.util.Objects;
1720

1821
import static org.elasticsearch.rest.RestRequest.Method.POST;
1922
import static org.elasticsearch.xpack.inference.rest.Paths.STREAM_INFERENCE_ID_PATH;
2023
import static org.elasticsearch.xpack.inference.rest.Paths.STREAM_TASK_TYPE_INFERENCE_ID_PATH;
2124

2225
@ServerlessScope(Scope.PUBLIC)
2326
public class RestStreamInferenceAction extends BaseInferenceAction {
27+
private final SetOnce<ThreadPool> threadPool;
28+
29+
public RestStreamInferenceAction(SetOnce<ThreadPool> threadPool) {
30+
super();
31+
this.threadPool = Objects.requireNonNull(threadPool);
32+
}
33+
2434
@Override
2535
public String getName() {
2636
return "stream_inference_action";
@@ -38,6 +48,6 @@ protected InferenceAction.Request prepareInferenceRequest(InferenceAction.Reques
3848

3949
@Override
4050
protected ActionListener<InferenceAction.Response> listener(RestChannel channel) {
41-
return new ServerSentEventsRestActionListener(channel);
51+
return new ServerSentEventsRestActionListener(channel, threadPool);
4252
}
4353
}

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/rest/ServerSentEventsRestActionListener.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
import org.apache.logging.log4j.LogManager;
1111
import org.apache.logging.log4j.Logger;
1212
import org.apache.lucene.util.BytesRef;
13+
import org.apache.lucene.util.SetOnce;
1314
import org.elasticsearch.ElasticsearchException;
1415
import org.elasticsearch.ExceptionsHelper;
1516
import org.elasticsearch.action.ActionListener;
17+
import org.elasticsearch.action.support.ContextPreservingActionListener;
1618
import org.elasticsearch.common.bytes.ReleasableBytesReference;
1719
import org.elasticsearch.common.collect.Iterators;
1820
import org.elasticsearch.common.io.stream.BytesStream;
@@ -30,6 +32,7 @@
3032
import org.elasticsearch.rest.RestResponse;
3133
import org.elasticsearch.rest.RestStatus;
3234
import org.elasticsearch.tasks.TaskCancelledException;
35+
import org.elasticsearch.threadpool.ThreadPool;
3336
import org.elasticsearch.xcontent.ToXContent;
3437
import org.elasticsearch.xcontent.XContentBuilder;
3538
import org.elasticsearch.xpack.core.inference.action.InferenceAction;
@@ -39,6 +42,7 @@
3942
import java.nio.charset.StandardCharsets;
4043
import java.util.Iterator;
4144
import java.util.Map;
45+
import java.util.Objects;
4246
import java.util.concurrent.Flow;
4347
import java.util.concurrent.atomic.AtomicBoolean;
4448

@@ -55,6 +59,7 @@ public class ServerSentEventsRestActionListener implements ActionListener<Infere
5559
private final AtomicBoolean isLastPart = new AtomicBoolean(false);
5660
private final RestChannel channel;
5761
private final ToXContent.Params params;
62+
private final SetOnce<ThreadPool> threadPool;
5863

5964
/**
6065
* A listener for the first part of the next entry to become available for transmission.
@@ -66,13 +71,14 @@ public class ServerSentEventsRestActionListener implements ActionListener<Infere
6671
*/
6772
private ActionListener<ChunkedRestResponseBodyPart> nextBodyPartListener;
6873

69-
public ServerSentEventsRestActionListener(RestChannel channel) {
70-
this(channel, channel.request());
74+
public ServerSentEventsRestActionListener(RestChannel channel, SetOnce<ThreadPool> threadPool) {
75+
this(channel, channel.request(), threadPool);
7176
}
7277

73-
public ServerSentEventsRestActionListener(RestChannel channel, ToXContent.Params params) {
78+
public ServerSentEventsRestActionListener(RestChannel channel, ToXContent.Params params, SetOnce<ThreadPool> threadPool) {
7479
this.channel = channel;
7580
this.params = params;
81+
this.threadPool = Objects.requireNonNull(threadPool);
7682
}
7783

7884
@Override
@@ -99,7 +105,7 @@ protected void ensureOpen() {
99105
}
100106

101107
private void initializeStream(InferenceAction.Response response) {
102-
nextBodyPartListener = ActionListener.wrap(bodyPart -> {
108+
ActionListener<ChunkedRestResponseBodyPart> chunkedResponseBodyActionListener = ActionListener.wrap(bodyPart -> {
103109
// this is the first response, so we need to send the RestResponse to open the stream
104110
// all subsequent bytes will be delivered through the nextBodyPartListener
105111
channel.sendResponse(RestResponse.chunked(RestStatus.OK, bodyPart, this::release));
@@ -115,6 +121,12 @@ private void initializeStream(InferenceAction.Response response) {
115121
)
116122
);
117123
});
124+
125+
nextBodyPartListener = ContextPreservingActionListener.wrapPreservingContext(
126+
chunkedResponseBodyActionListener,
127+
threadPool.get().getThreadContext()
128+
);
129+
118130
// subscribe will call onSubscribe, which requests the first chunk
119131
response.publisher().subscribe(subscriber);
120132
}

x-pack/plugin/inference/src/test/java/org/elasticsearch/xpack/inference/rest/RestStreamInferenceActionTests.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@
1212
import org.elasticsearch.rest.RestRequest;
1313
import org.elasticsearch.test.rest.FakeRestRequest;
1414
import org.elasticsearch.test.rest.RestActionTestCase;
15+
import org.elasticsearch.threadpool.TestThreadPool;
16+
import org.elasticsearch.threadpool.ThreadPool;
1517
import org.elasticsearch.xcontent.XContentType;
1618
import org.elasticsearch.xpack.core.inference.action.InferenceAction;
19+
import org.junit.After;
1720
import org.junit.Before;
1821

1922
import static org.elasticsearch.xpack.inference.rest.BaseInferenceActionTests.createResponse;
@@ -22,10 +25,18 @@
2225
import static org.hamcrest.Matchers.instanceOf;
2326

2427
public class RestStreamInferenceActionTests extends RestActionTestCase {
28+
private final SetOnce<ThreadPool> threadPool = new SetOnce<>();
2529

2630
@Before
2731
public void setUpAction() {
28-
controller().registerHandler(new RestStreamInferenceAction());
32+
threadPool.set(new TestThreadPool(getTestName()));
33+
controller().registerHandler(new RestStreamInferenceAction(threadPool));
34+
}
35+
36+
@After
37+
public void tearDownAction() {
38+
terminate(threadPool.get());
39+
2940
}
3041

3142
public void testStreamIsTrue() {

0 commit comments

Comments
 (0)