35
35
import org .apache .logging .log4j .LogManager ;
36
36
import org .apache .logging .log4j .Logger ;
37
37
import org .apache .logging .log4j .message .ParameterizedMessage ;
38
- import org .opensearch .Version ;
39
- import org .opensearch .cluster .node .DiscoveryNode ;
40
38
import org .opensearch .common .CheckedSupplier ;
41
- import org .opensearch .common .io .stream .ReleasableBytesStreamOutput ;
42
39
import org .opensearch .common .lease .Releasable ;
43
40
import org .opensearch .common .lease .Releasables ;
44
41
import org .opensearch .common .network .CloseableChannel ;
45
42
import org .opensearch .common .transport .NetworkExceptionHelper ;
46
- import org .opensearch .common .util .BigArrays ;
47
43
import org .opensearch .common .util .concurrent .ThreadContext ;
48
- import org .opensearch .common .util .io .IOUtils ;
49
44
import org .opensearch .core .action .ActionListener ;
50
45
import org .opensearch .core .action .NotifyOnceListener ;
51
46
import org .opensearch .core .common .bytes .BytesReference ;
52
- import org .opensearch .core .common .transport .TransportAddress ;
53
- import org .opensearch .core .transport .TransportResponse ;
54
47
import org .opensearch .threadpool .ThreadPool ;
55
48
56
49
import java .io .IOException ;
57
- import java .util .Set ;
58
50
59
51
/**
60
52
* Outbound data handler
61
53
*
62
54
* @opensearch.internal
63
55
*/
64
- final class OutboundHandler {
56
+ public final class OutboundHandler {
65
57
66
58
private static final Logger logger = LogManager .getLogger (OutboundHandler .class );
67
59
68
- private final String nodeName ;
69
- private final Version version ;
70
- private final String [] features ;
71
60
private final StatsTracker statsTracker ;
72
61
private final ThreadPool threadPool ;
73
- private final BigArrays bigArrays ;
74
- private volatile TransportMessageListener messageListener = TransportMessageListener .NOOP_LISTENER ;
75
62
76
- OutboundHandler (
77
- String nodeName ,
78
- Version version ,
79
- String [] features ,
80
- StatsTracker statsTracker ,
81
- ThreadPool threadPool ,
82
- BigArrays bigArrays
83
- ) {
84
- this .nodeName = nodeName ;
85
- this .version = version ;
86
- this .features = features ;
63
+ public OutboundHandler (StatsTracker statsTracker , ThreadPool threadPool ) {
87
64
this .statsTracker = statsTracker ;
88
65
this .threadPool = threadPool ;
89
- this .bigArrays = bigArrays ;
90
66
}
91
67
92
68
void sendBytes (TcpChannel channel , BytesReference bytes , ActionListener <Void > listener ) {
93
- SendContext sendContext = new SendContext (channel , () -> bytes , listener );
69
+ SendContext sendContext = new SendContext (statsTracker , channel , () -> bytes , listener );
94
70
try {
95
- internalSend (channel , sendContext );
71
+ sendBytes (channel , sendContext );
96
72
} catch (IOException e ) {
97
73
// This should not happen as the bytes are already serialized
98
74
throw new AssertionError (e );
99
75
}
100
76
}
101
77
102
- /**
103
- * Sends the request to the given channel. This method should be used to send {@link TransportRequest}
104
- * objects back to the caller.
105
- */
106
- void sendRequest (
107
- final DiscoveryNode node ,
108
- final TcpChannel channel ,
109
- final long requestId ,
110
- final String action ,
111
- final TransportRequest request ,
112
- final TransportRequestOptions options ,
113
- final Version channelVersion ,
114
- final boolean compressRequest ,
115
- final boolean isHandshake
116
- ) throws IOException , TransportException {
117
- Version version = Version .min (this .version , channelVersion );
118
- OutboundMessage .Request message = new OutboundMessage .Request (
119
- threadPool .getThreadContext (),
120
- features ,
121
- request ,
122
- version ,
123
- action ,
124
- requestId ,
125
- isHandshake ,
126
- compressRequest
127
- );
128
- ActionListener <Void > listener = ActionListener .wrap (() -> messageListener .onRequestSent (node , requestId , action , request , options ));
129
- sendMessage (channel , message , listener );
130
- }
131
-
132
- /**
133
- * Sends the response to the given channel. This method should be used to send {@link TransportResponse}
134
- * objects back to the caller.
135
- *
136
- * @see #sendErrorResponse(Version, Set, TcpChannel, long, String, Exception) for sending error responses
137
- */
138
- void sendResponse (
139
- final Version nodeVersion ,
140
- final Set <String > features ,
141
- final TcpChannel channel ,
142
- final long requestId ,
143
- final String action ,
144
- final TransportResponse response ,
145
- final boolean compress ,
146
- final boolean isHandshake
147
- ) throws IOException {
148
- Version version = Version .min (this .version , nodeVersion );
149
- OutboundMessage .Response message = new OutboundMessage .Response (
150
- threadPool .getThreadContext (),
151
- features ,
152
- response ,
153
- version ,
154
- requestId ,
155
- isHandshake ,
156
- compress
157
- );
158
- ActionListener <Void > listener = ActionListener .wrap (() -> messageListener .onResponseSent (requestId , action , response ));
159
- sendMessage (channel , message , listener );
160
- }
161
-
162
- /**
163
- * Sends back an error response to the caller via the given channel
164
- */
165
- void sendErrorResponse (
166
- final Version nodeVersion ,
167
- final Set <String > features ,
168
- final TcpChannel channel ,
169
- final long requestId ,
170
- final String action ,
171
- final Exception error
172
- ) throws IOException {
173
- Version version = Version .min (this .version , nodeVersion );
174
- TransportAddress address = new TransportAddress (channel .getLocalAddress ());
175
- RemoteTransportException tx = new RemoteTransportException (nodeName , address , action , error );
176
- OutboundMessage .Response message = new OutboundMessage .Response (
177
- threadPool .getThreadContext (),
178
- features ,
179
- tx ,
180
- version ,
181
- requestId ,
182
- false ,
183
- false
184
- );
185
- ActionListener <Void > listener = ActionListener .wrap (() -> messageListener .onResponseSent (requestId , action , error ));
186
- sendMessage (channel , message , listener );
187
- }
188
-
189
- private void sendMessage (TcpChannel channel , OutboundMessage networkMessage , ActionListener <Void > listener ) throws IOException {
190
- MessageSerializer serializer = new MessageSerializer (networkMessage , bigArrays );
191
- SendContext sendContext = new SendContext (channel , serializer , listener , serializer );
192
- internalSend (channel , sendContext );
193
- }
194
-
195
- private void internalSend (TcpChannel channel , SendContext sendContext ) throws IOException {
78
+ public void sendBytes (TcpChannel channel , SendContext sendContext ) throws IOException {
196
79
channel .getChannelStats ().markAccessed (threadPool .relativeTimeInMillis ());
197
80
BytesReference reference = sendContext .get ();
198
81
// stash thread context so that channel event loop is not polluted by thread context
@@ -205,59 +88,30 @@ private void internalSend(TcpChannel channel, SendContext sendContext) throws IO
205
88
}
206
89
}
207
90
208
- void setMessageListener (TransportMessageListener listener ) {
209
- if (messageListener == TransportMessageListener .NOOP_LISTENER ) {
210
- messageListener = listener ;
211
- } else {
212
- throw new IllegalStateException ("Cannot set message listener twice" );
213
- }
214
- }
215
-
216
91
/**
217
92
* Internal message serializer
218
93
*
219
94
* @opensearch.internal
220
95
*/
221
- private static class MessageSerializer implements CheckedSupplier <BytesReference , IOException >, Releasable {
222
-
223
- private final OutboundMessage message ;
224
- private final BigArrays bigArrays ;
225
- private volatile ReleasableBytesStreamOutput bytesStreamOutput ;
226
-
227
- private MessageSerializer (OutboundMessage message , BigArrays bigArrays ) {
228
- this .message = message ;
229
- this .bigArrays = bigArrays ;
230
- }
231
-
232
- @ Override
233
- public BytesReference get () throws IOException {
234
- bytesStreamOutput = new ReleasableBytesStreamOutput (bigArrays );
235
- return message .serialize (bytesStreamOutput );
236
- }
237
-
238
- @ Override
239
- public void close () {
240
- IOUtils .closeWhileHandlingException (bytesStreamOutput );
241
- }
242
- }
243
-
244
- private class SendContext extends NotifyOnceListener <Void > implements CheckedSupplier <BytesReference , IOException > {
245
-
96
+ public static class SendContext extends NotifyOnceListener <Void > implements CheckedSupplier <BytesReference , IOException > {
97
+ private final StatsTracker statsTracker ;
246
98
private final TcpChannel channel ;
247
99
private final CheckedSupplier <BytesReference , IOException > messageSupplier ;
248
100
private final ActionListener <Void > listener ;
249
101
private final Releasable optionalReleasable ;
250
102
private long messageSize = -1 ;
251
103
252
- private SendContext (
104
+ SendContext (
105
+ StatsTracker statsTracker ,
253
106
TcpChannel channel ,
254
107
CheckedSupplier <BytesReference , IOException > messageSupplier ,
255
108
ActionListener <Void > listener
256
109
) {
257
- this (channel , messageSupplier , listener , null );
110
+ this (statsTracker , channel , messageSupplier , listener , null );
258
111
}
259
112
260
- private SendContext (
113
+ public SendContext (
114
+ StatsTracker statsTracker ,
261
115
TcpChannel channel ,
262
116
CheckedSupplier <BytesReference , IOException > messageSupplier ,
263
117
ActionListener <Void > listener ,
@@ -267,6 +121,7 @@ private SendContext(
267
121
this .messageSupplier = messageSupplier ;
268
122
this .listener = listener ;
269
123
this .optionalReleasable = optionalReleasable ;
124
+ this .statsTracker = statsTracker ;
270
125
}
271
126
272
127
public BytesReference get () throws IOException {
0 commit comments