|
20 | 20 | import org.opensearch.Version; |
21 | 21 | import org.opensearch.cluster.node.DiscoveryNode; |
22 | 22 | import org.opensearch.common.io.stream.BytesStreamOutput; |
| 23 | +import org.opensearch.common.util.concurrent.ThreadContext; |
23 | 24 | import org.opensearch.core.common.bytes.BytesReference; |
24 | 25 | import org.opensearch.core.transport.TransportResponse; |
25 | 26 | import org.opensearch.threadpool.ThreadPool; |
@@ -89,85 +90,195 @@ public void sendResponse( |
89 | 90 | ); |
90 | 91 | } |
91 | 92 |
|
92 | | - /** This needs to be synchronized for the cases when multiple batches are written concurrently, |
93 | | - * as VectorSchemaRoot is shared across batches **/ |
94 | | - public synchronized void sendResponseBatch( |
| 93 | + @Override |
| 94 | + public void sendErrorResponse( |
| 95 | + Version nodeVersion, |
| 96 | + Set<String> features, |
| 97 | + TcpChannel channel, |
| 98 | + long requestId, |
| 99 | + String action, |
| 100 | + Exception error |
| 101 | + ) throws IOException { |
| 102 | + throw new UnsupportedOperationException( |
| 103 | + "sendResponse() is not supported for streaming requests in FlightOutboundHandler; use sendResponseBatch()" |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + public void sendResponseBatch( |
95 | 108 | final Version nodeVersion, |
96 | 109 | final Set<String> features, |
97 | 110 | final TcpChannel channel, |
| 111 | + final FlightTransportChannel transportChannel, |
98 | 112 | final long requestId, |
99 | 113 | final String action, |
100 | 114 | final TransportResponse response, |
101 | 115 | final boolean compress, |
102 | 116 | final boolean isHandshake |
103 | 117 | ) throws IOException { |
104 | | - // TODO add support for compression |
| 118 | + ThreadContext.StoredContext storedContext = threadPool.getThreadContext().stashContext(); |
| 119 | + BatchTask task = new BatchTask( |
| 120 | + nodeVersion, |
| 121 | + features, |
| 122 | + channel, |
| 123 | + transportChannel, |
| 124 | + requestId, |
| 125 | + action, |
| 126 | + response, |
| 127 | + compress, |
| 128 | + isHandshake, |
| 129 | + false, |
| 130 | + false, |
| 131 | + null, |
| 132 | + storedContext |
| 133 | + ); |
| 134 | + |
105 | 135 | if (!(channel instanceof FlightServerChannel flightChannel)) { |
106 | | - throw new IllegalStateException("Expected FlightServerChannel, got " + channel.getClass().getName()); |
| 136 | + messageListener.onResponseSent(requestId, action, new IllegalStateException("Expected FlightServerChannel")); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + flightChannel.getExecutor().execute(() -> { |
| 141 | + try (BatchTask ignored = task) { |
| 142 | + processBatchTask(task); |
| 143 | + } catch (Exception e) { |
| 144 | + messageListener.onResponseSent(requestId, action, e); |
| 145 | + } |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + private void processBatchTask(BatchTask task) { |
| 150 | + task.storedContext().restore(); |
| 151 | + if (!(task.channel() instanceof FlightServerChannel flightChannel)) { |
| 152 | + Exception error = new IllegalStateException("Expected FlightServerChannel, got " + task.channel().getClass().getName()); |
| 153 | + messageListener.onResponseSent(task.requestId(), task.action(), error); |
| 154 | + return; |
107 | 155 | } |
| 156 | + |
108 | 157 | try { |
109 | 158 | try (VectorStreamOutput out = new VectorStreamOutput(flightChannel.getAllocator(), flightChannel.getRoot())) { |
110 | | - response.writeTo(out); |
111 | | - flightChannel.sendBatch(getHeaderBuffer(requestId, nodeVersion, features), out); |
112 | | - messageListener.onResponseSent(requestId, action, response); |
| 159 | + task.response().writeTo(out); |
| 160 | + flightChannel.sendBatch(getHeaderBuffer(task.requestId(), task.nodeVersion(), task.features()), out); |
| 161 | + messageListener.onResponseSent(task.requestId(), task.action(), task.response()); |
113 | 162 | } |
114 | | - } catch (StreamException e) { |
115 | | - messageListener.onResponseSent(requestId, action, e); |
116 | | - // Let StreamException propagate as is - it will be converted to FlightRuntimeException at a higher level |
117 | | - throw e; |
118 | 163 | } catch (FlightRuntimeException e) { |
119 | | - messageListener.onResponseSent(requestId, action, e); |
120 | | - throw FlightErrorMapper.fromFlightException(e); |
| 164 | + messageListener.onResponseSent(task.requestId(), task.action(), FlightErrorMapper.fromFlightException(e)); |
121 | 165 | } catch (Exception e) { |
122 | | - messageListener.onResponseSent(requestId, action, e); |
123 | | - throw e; |
| 166 | + messageListener.onResponseSent(task.requestId(), task.action(), e); |
124 | 167 | } |
125 | 168 | } |
126 | 169 |
|
127 | 170 | public void completeStream( |
128 | 171 | final Version nodeVersion, |
129 | 172 | final Set<String> features, |
130 | 173 | final TcpChannel channel, |
| 174 | + final FlightTransportChannel transportChannel, |
131 | 175 | final long requestId, |
132 | 176 | final String action |
133 | 177 | ) { |
| 178 | + ThreadContext.StoredContext storedContext = threadPool.getThreadContext().stashContext(); |
| 179 | + BatchTask completeTask = new BatchTask( |
| 180 | + nodeVersion, |
| 181 | + features, |
| 182 | + channel, |
| 183 | + transportChannel, |
| 184 | + requestId, |
| 185 | + action, |
| 186 | + TransportResponse.Empty.INSTANCE, |
| 187 | + false, |
| 188 | + false, |
| 189 | + true, |
| 190 | + false, |
| 191 | + null, |
| 192 | + storedContext |
| 193 | + ); |
| 194 | + |
134 | 195 | if (!(channel instanceof FlightServerChannel flightChannel)) { |
135 | | - throw new IllegalStateException("Expected FlightServerChannel, got " + channel.getClass().getName()); |
| 196 | + messageListener.onResponseSent(requestId, action, new IllegalStateException("Expected FlightServerChannel")); |
| 197 | + return; |
136 | 198 | } |
| 199 | + |
| 200 | + flightChannel.getExecutor().execute(() -> { |
| 201 | + try (BatchTask ignored = completeTask) { |
| 202 | + processCompleteTask(completeTask); |
| 203 | + } catch (Exception e) { |
| 204 | + messageListener.onResponseSent(requestId, action, e); |
| 205 | + } |
| 206 | + }); |
| 207 | + } |
| 208 | + |
| 209 | + private void processCompleteTask(BatchTask task) { |
| 210 | + task.storedContext().restore(); |
| 211 | + if (!(task.channel() instanceof FlightServerChannel flightChannel)) { |
| 212 | + Exception error = new IllegalStateException("Expected FlightServerChannel, got " + task.channel().getClass().getName()); |
| 213 | + messageListener.onResponseSent(task.requestId(), task.action(), error); |
| 214 | + return; |
| 215 | + } |
| 216 | + |
137 | 217 | try { |
138 | 218 | flightChannel.completeStream(); |
139 | | - messageListener.onResponseSent(requestId, action, TransportResponse.Empty.INSTANCE); |
140 | | - } catch (FlightRuntimeException e) { |
141 | | - messageListener.onResponseSent(requestId, action, e); |
142 | | - throw FlightErrorMapper.fromFlightException(e); |
| 219 | + messageListener.onResponseSent(task.requestId(), task.action(), TransportResponse.Empty.INSTANCE); |
143 | 220 | } catch (Exception e) { |
144 | | - messageListener.onResponseSent(requestId, action, e); |
145 | | - throw e; |
| 221 | + messageListener.onResponseSent(task.requestId(), task.action(), e); |
146 | 222 | } |
147 | 223 | } |
148 | 224 |
|
149 | | - @Override |
150 | 225 | public void sendErrorResponse( |
151 | 226 | final Version nodeVersion, |
152 | 227 | final Set<String> features, |
153 | 228 | final TcpChannel channel, |
| 229 | + final FlightTransportChannel transportChannel, |
154 | 230 | final long requestId, |
155 | 231 | final String action, |
156 | 232 | final Exception error |
157 | | - ) throws IOException { |
158 | | - if (!(channel instanceof FlightServerChannel flightServerChannel)) { |
159 | | - throw new IllegalStateException("Expected FlightServerChannel, got " + channel.getClass().getName()); |
| 233 | + ) { |
| 234 | + ThreadContext.StoredContext storedContext = threadPool.getThreadContext().stashContext(); |
| 235 | + BatchTask errorTask = new BatchTask( |
| 236 | + nodeVersion, |
| 237 | + features, |
| 238 | + channel, |
| 239 | + transportChannel, |
| 240 | + requestId, |
| 241 | + action, |
| 242 | + null, |
| 243 | + false, |
| 244 | + false, |
| 245 | + false, |
| 246 | + true, |
| 247 | + error, |
| 248 | + storedContext |
| 249 | + ); |
| 250 | + |
| 251 | + if (!(channel instanceof FlightServerChannel flightChannel)) { |
| 252 | + messageListener.onResponseSent(requestId, action, new IllegalStateException("Expected FlightServerChannel")); |
| 253 | + return; |
| 254 | + } |
| 255 | + |
| 256 | + flightChannel.getExecutor().execute(() -> { |
| 257 | + try (BatchTask ignored = errorTask) { |
| 258 | + processErrorTask(errorTask); |
| 259 | + } catch (Exception e) { |
| 260 | + messageListener.onResponseSent(requestId, action, e); |
| 261 | + } |
| 262 | + }); |
| 263 | + } |
| 264 | + |
| 265 | + private void processErrorTask(BatchTask task) { |
| 266 | + task.storedContext().restore(); |
| 267 | + if (!(task.channel() instanceof FlightServerChannel flightServerChannel)) { |
| 268 | + Exception error = new IllegalStateException("Expected FlightServerChannel, got " + task.channel().getClass().getName()); |
| 269 | + messageListener.onResponseSent(task.requestId(), task.action(), error); |
| 270 | + return; |
160 | 271 | } |
| 272 | + |
161 | 273 | try { |
162 | | - Exception flightError = error; |
163 | | - if (error instanceof StreamException) { |
164 | | - flightError = FlightErrorMapper.toFlightException((StreamException) error); |
| 274 | + Exception flightError = task.error(); |
| 275 | + if (task.error() instanceof StreamException) { |
| 276 | + flightError = FlightErrorMapper.toFlightException((StreamException) task.error()); |
165 | 277 | } |
166 | | - flightServerChannel.sendError(getHeaderBuffer(requestId, version, features), flightError); |
167 | | - messageListener.onResponseSent(requestId, action, error); |
| 278 | + flightServerChannel.sendError(getHeaderBuffer(task.requestId(), task.nodeVersion(), task.features()), flightError); |
| 279 | + messageListener.onResponseSent(task.requestId(), task.action(), task.error()); |
168 | 280 | } catch (Exception e) { |
169 | | - messageListener.onResponseSent(requestId, action, e); |
170 | | - throw e; |
| 281 | + messageListener.onResponseSent(task.requestId(), task.action(), e); |
171 | 282 | } |
172 | 283 | } |
173 | 284 |
|
@@ -197,4 +308,19 @@ private ByteBuffer getHeaderBuffer(long requestId, Version nodeVersion, Set<Stri |
197 | 308 | return ByteBuffer.wrap(headerBytes.toBytesRef().bytes); |
198 | 309 | } |
199 | 310 | } |
| 311 | + |
| 312 | + record BatchTask(Version nodeVersion, Set<String> features, TcpChannel channel, FlightTransportChannel transportChannel, long requestId, |
| 313 | + String action, TransportResponse response, boolean compress, boolean isHandshake, boolean isComplete, boolean isError, |
| 314 | + Exception error, ThreadContext.StoredContext storedContext) implements AutoCloseable { |
| 315 | + |
| 316 | + @Override |
| 317 | + public void close() { |
| 318 | + if (storedContext != null) { |
| 319 | + storedContext.close(); |
| 320 | + } |
| 321 | + if ((isComplete || isError) && transportChannel != null) { |
| 322 | + transportChannel.releaseChannel(isError); |
| 323 | + } |
| 324 | + } |
| 325 | + } |
200 | 326 | } |
0 commit comments