2828import java .io .IOException ;
2929import java .util .Objects ;
3030import java .util .concurrent .CancellationException ;
31- import java .util .concurrent .atomic .AtomicReference ;
3231
3332import static org .elasticsearch .core .Strings .format ;
3433import static org .elasticsearch .xpack .inference .InferencePlugin .INFERENCE_RESPONSE_THREAD_POOL_NAME ;
3938public class HttpClient implements Closeable {
4039 private static final Logger logger = LogManager .getLogger (HttpClient .class );
4140
42- enum Status {
43- CREATED ,
44- STARTED ,
45- STOPPED
46- }
47-
4841 private final CloseableHttpAsyncClient client ;
49- private final AtomicReference <Status > status = new AtomicReference <>(Status .CREATED );
5042 private final ThreadPool threadPool ;
5143 private final HttpSettings settings ;
5244 private final ThrottlerManager throttlerManager ;
@@ -127,15 +119,10 @@ private static CloseableHttpAsyncClient createAsyncClient(
127119 }
128120
129121 public void start () {
130- if (status .compareAndSet (Status .CREATED , Status .STARTED )) {
131- client .start ();
132- }
122+ client .start ();
133123 }
134124
135125 public void send (HttpRequest request , HttpClientContext context , ActionListener <HttpResult > listener ) throws IOException {
136- // The caller must call start() first before attempting to send a request
137- assert status .get () == Status .STARTED : "call start() before attempting to send a request" ;
138-
139126 SocketAccess .doPrivileged (() -> client .execute (request .httpRequestBase (), context , new FutureCallback <>() {
140127 @ Override
141128 public void completed (HttpResponse response ) {
@@ -145,7 +132,7 @@ public void completed(HttpResponse response) {
145132 @ Override
146133 public void failed (Exception ex ) {
147134 throttlerManager .warn (logger , format ("Request from inference entity id [%s] failed" , request .inferenceEntityId ()), ex );
148- failUsingResponseThread (ex , listener );
135+ failUsingResponseThread (getException ( ex ) , listener );
149136 }
150137
151138 @ Override
@@ -179,10 +166,22 @@ private void failUsingResponseThread(Exception exception, ActionListener<?> list
179166 threadPool .executor (INFERENCE_RESPONSE_THREAD_POOL_NAME ).execute (() -> listener .onFailure (exception ));
180167 }
181168
182- public void stream (HttpRequest request , HttpContext context , ActionListener <StreamingHttpResult > listener ) throws IOException {
183- // The caller must call start() first before attempting to send a request
184- assert status .get () == Status .STARTED : "call start() before attempting to send a request" ;
169+ private static Exception getException (Exception e ) {
170+ if (e instanceof CancellationException cancellationException ) {
171+ return createNotRunningException (cancellationException );
172+ }
185173
174+ return e ;
175+ }
176+
177+ private static IllegalStateException createNotRunningException (Exception exception ) {
178+ // If the http client isn't running, it is either not started yet, in which case we have a bug somewhere because
179+ // it should always be started as part of the inference plugin startup, or it is stopped meaning the node is shutting down.
180+ // If we're shutting down, the user should retry the request, and hopefully it'll hit a node that isn't shutting down.
181+ return new IllegalStateException ("Http client is not running, please retry the request" , exception );
182+ }
183+
184+ public void stream (HttpRequest request , HttpContext context , ActionListener <StreamingHttpResult > listener ) throws IOException {
186185 var streamingProcessor = new StreamingHttpResultPublisher (threadPool , settings , listener );
187186
188187 SocketAccess .doPrivileged (() -> client .execute (request .requestProducer (), streamingProcessor , context , new FutureCallback <>() {
@@ -193,7 +192,7 @@ public void completed(Void response) {
193192
194193 @ Override
195194 public void failed (Exception ex ) {
196- threadPool .executor (INFERENCE_RESPONSE_THREAD_POOL_NAME ).execute (() -> streamingProcessor .failed (ex ));
195+ threadPool .executor (INFERENCE_RESPONSE_THREAD_POOL_NAME ).execute (() -> streamingProcessor .failed (getException ( ex ) ));
197196 }
198197
199198 @ Override
@@ -212,7 +211,6 @@ public void cancelled() {
212211
213212 @ Override
214213 public void close () throws IOException {
215- status .set (Status .STOPPED );
216214 client .close ();
217215 }
218216}
0 commit comments