0
# Network Utilities
1
2
Utility functions for buffer management, channel handling, and Netty 3 integration. Provides essential conversion and management utilities for working with Netty 3 networking components in Elasticsearch.
3
4
## Capabilities
5
6
### Netty3Utils Class
7
8
Core utility class providing essential functions for Netty 3 integration, buffer management, and system setup.
9
10
```java { .api }
11
/**
12
* Utility class for Netty 3 integration and buffer management
13
*/
14
public class Netty3Utils {
15
/**
16
* Default gathering flag for composite buffers to enable zero-copy operations
17
*/
18
public static final boolean DEFAULT_GATHERING = true;
19
20
/**
21
* Sets up Netty 3 environment including logging configuration and thread naming.
22
* Must be called before using any Netty 3 functionality.
23
*/
24
public static void setup();
25
26
/**
27
* Converts Elasticsearch BytesReference to Netty ChannelBuffer for network transmission
28
* @param bytes BytesReference containing data to convert
29
* @return ChannelBuffer suitable for Netty operations
30
*/
31
public static ChannelBuffer toChannelBuffer(BytesReference bytes);
32
33
/**
34
* Converts Netty ChannelBuffer to Elasticsearch BytesReference for internal processing
35
* @param buffer ChannelBuffer containing network data
36
* @return BytesReference for Elasticsearch internal use
37
*/
38
public static BytesReference toBytesReference(ChannelBuffer buffer);
39
40
/**
41
* Converts Netty ChannelBuffer to Elasticsearch BytesReference with specified size
42
* @param buffer ChannelBuffer containing network data
43
* @param size Number of bytes to include in the conversion
44
* @return BytesReference with the specified size
45
*/
46
public static BytesReference toBytesReference(ChannelBuffer buffer, int size);
47
48
/**
49
* Closes a collection of Netty channels gracefully
50
* @param channels Collection of Channel objects to close
51
*/
52
public static void closeChannels(Collection<Channel> channels);
53
54
/**
55
* Handles fatal errors by rethrowing them on a separate thread to prevent blocking
56
* @param t Throwable representing the fatal error
57
*/
58
public static void maybeDie(Throwable t);
59
}
60
```
61
62
**Usage Examples:**
63
64
```java
65
import org.elasticsearch.transport.netty3.Netty3Utils;
66
import org.elasticsearch.common.bytes.BytesReference;
67
import org.jboss.netty.buffer.ChannelBuffer;
68
69
// Setup Netty 3 environment (called automatically by plugin)
70
Netty3Utils.setup();
71
72
// Convert Elasticsearch bytes to Netty buffer for transmission
73
BytesReference elasticsearchData = // ... some data
74
ChannelBuffer nettyBuffer = Netty3Utils.toChannelBuffer(elasticsearchData);
75
76
// Convert received Netty buffer back to Elasticsearch bytes
77
ChannelBuffer receivedBuffer = // ... received from network
78
BytesReference elasticsearchBytes = Netty3Utils.toBytesReference(receivedBuffer);
79
80
// Convert with specific size limit
81
BytesReference limitedBytes = Netty3Utils.toBytesReference(receivedBuffer, 1024);
82
83
// Close multiple channels at once
84
List<Channel> channels = // ... collection of channels
85
Netty3Utils.closeChannels(channels);
86
```
87
88
### Channel Management Utilities
89
90
Additional utilities for managing Netty channel lifecycle and connection tracking.
91
92
```java { .api }
93
/**
94
* Handler for tracking and managing open Netty channels
95
*/
96
public class Netty3OpenChannelsHandler extends SimpleChannelUpstreamHandler implements Releasable {
97
/**
98
* Constructor for channel tracking handler
99
* @param logger Logger instance for debugging and monitoring
100
*/
101
public Netty3OpenChannelsHandler(Logger logger);
102
103
/**
104
* Handles upstream channel events for connection tracking
105
* @param ctx Channel handler context
106
* @param e Channel event to process
107
*/
108
public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e);
109
110
/**
111
* Returns the current number of open channels
112
* @return int count of currently open channels
113
*/
114
public int numberOfOpenChannels();
115
116
/**
117
* Returns the total number of channels that have been created
118
* @return long total channel count since startup
119
*/
120
public long totalChannels();
121
122
/**
123
* Closes all currently tracked open channels
124
*/
125
public void close();
126
}
127
```
128
129
### Buffer Reference Implementation
130
131
Specialized BytesReference implementation that wraps Netty ChannelBuffer for efficient memory management.
132
133
```java { .api }
134
/**
135
* BytesReference implementation that wraps a Netty ChannelBuffer
136
* Provides efficient access to network buffer data without copying
137
*/
138
public class ChannelBufferBytesReference implements BytesReference {
139
/**
140
* Constructor wrapping a ChannelBuffer with specified size
141
* @param buffer ChannelBuffer to wrap
142
* @param size Number of bytes to include from the buffer
143
*/
144
public ChannelBufferBytesReference(ChannelBuffer buffer, int size);
145
146
/**
147
* Gets a single byte at the specified index
148
* @param index Position to read from
149
* @return byte value at the index
150
*/
151
public byte get(int index);
152
153
/**
154
* Returns the total length of available data
155
* @return int length in bytes
156
*/
157
public int length();
158
159
/**
160
* Creates a slice of the buffer for the specified range
161
* @param from Starting position (inclusive)
162
* @param length Number of bytes to include
163
* @return BytesReference containing the slice
164
*/
165
public BytesReference slice(int from, int length);
166
167
/**
168
* Creates a stream input for reading the buffer data
169
* @return StreamInput for sequential reading
170
*/
171
public StreamInput streamInput();
172
173
/**
174
* Writes the buffer contents to an output stream
175
* @param out OutputStream to write to
176
*/
177
public void writeTo(OutputStream out);
178
}
179
```
180
181
### Stream Input Implementation
182
183
StreamInput implementation for reading from Netty ChannelBuffer data.
184
185
```java { .api }
186
/**
187
* StreamInput implementation for reading from ChannelBuffer data
188
*/
189
public class ChannelBufferStreamInput extends StreamInput {
190
/**
191
* Constructor for creating stream input from ChannelBuffer
192
* @param buffer ChannelBuffer containing data to read
193
* @param size Number of bytes available for reading
194
*/
195
public ChannelBufferStreamInput(ChannelBuffer buffer, int size);
196
197
/**
198
* Reads a single byte from the stream
199
* @return int byte value (0-255) or -1 if end of stream
200
*/
201
public int read();
202
203
/**
204
* Reads multiple bytes into a byte array
205
* @param b Destination byte array
206
* @param off Offset in the destination array
207
* @param len Maximum number of bytes to read
208
* @return int number of bytes actually read
209
*/
210
public int read(byte[] b, int off, int len);
211
212
/**
213
* Resets the stream position to the beginning
214
*/
215
public void reset();
216
217
/**
218
* Closes the stream and releases resources
219
*/
220
public void close();
221
}
222
```
223
224
### Error Handling and Logging
225
226
The utilities include comprehensive error handling and logging integration.
227
228
```java { .api }
229
/**
230
* Internal logger adapter for Netty 3 integration with Elasticsearch logging
231
*/
232
public class Netty3InternalESLogger implements InternalLogger {
233
// Provides bridge between Netty 3 logging and Elasticsearch logging system
234
// Handles log level mapping and message formatting
235
}
236
```
237
238
**Setup and Integration:**
239
240
```java
241
// The setup() method configures:
242
// - Netty 3 logging to use Elasticsearch's logging framework
243
// - Thread naming patterns for better debugging
244
// - System property configuration for optimal performance
245
// - Error handling integration
246
247
public static void setup() {
248
// Configure Netty logging to use ES logging
249
InternalLoggerFactory.setDefaultFactory(new Netty3InternalESLoggerFactory());
250
251
// Set thread naming pattern for Netty threads
252
System.setProperty("io.netty.threadLocalDirectBufferSize", /* optimized size */);
253
254
// Additional Netty 3 system optimizations
255
// ...
256
}
257
```
258
259
### Memory Management
260
261
The utilities provide efficient memory management for network operations:
262
263
- **Zero-Copy Operations**: ChannelBuffer conversions avoid unnecessary data copying
264
- **Composite Buffers**: Support for gathering multiple buffer segments efficiently
265
- **Buffer Pooling**: Integration with Netty's buffer pooling for memory efficiency
266
- **Resource Cleanup**: Automatic cleanup of network resources and buffer references