Netty 3 based transport implementation for Elasticsearch providing TCP and HTTP transport layers
—
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.
Core utility class providing essential functions for Netty 3 integration, buffer management, and system setup.
/**
* Utility class for Netty 3 integration and buffer management
*/
public class Netty3Utils {
/**
* Default gathering flag for composite buffers to enable zero-copy operations
*/
public static final boolean DEFAULT_GATHERING = true;
/**
* Sets up Netty 3 environment including logging configuration and thread naming.
* Must be called before using any Netty 3 functionality.
*/
public static void setup();
/**
* Converts Elasticsearch BytesReference to Netty ChannelBuffer for network transmission
* @param bytes BytesReference containing data to convert
* @return ChannelBuffer suitable for Netty operations
*/
public static ChannelBuffer toChannelBuffer(BytesReference bytes);
/**
* Converts Netty ChannelBuffer to Elasticsearch BytesReference for internal processing
* @param buffer ChannelBuffer containing network data
* @return BytesReference for Elasticsearch internal use
*/
public static BytesReference toBytesReference(ChannelBuffer buffer);
/**
* Converts Netty ChannelBuffer to Elasticsearch BytesReference with specified size
* @param buffer ChannelBuffer containing network data
* @param size Number of bytes to include in the conversion
* @return BytesReference with the specified size
*/
public static BytesReference toBytesReference(ChannelBuffer buffer, int size);
/**
* Closes a collection of Netty channels gracefully
* @param channels Collection of Channel objects to close
*/
public static void closeChannels(Collection<Channel> channels);
/**
* Handles fatal errors by rethrowing them on a separate thread to prevent blocking
* @param t Throwable representing the fatal error
*/
public static void maybeDie(Throwable t);
}Usage Examples:
import org.elasticsearch.transport.netty3.Netty3Utils;
import org.elasticsearch.common.bytes.BytesReference;
import org.jboss.netty.buffer.ChannelBuffer;
// Setup Netty 3 environment (called automatically by plugin)
Netty3Utils.setup();
// Convert Elasticsearch bytes to Netty buffer for transmission
BytesReference elasticsearchData = // ... some data
ChannelBuffer nettyBuffer = Netty3Utils.toChannelBuffer(elasticsearchData);
// Convert received Netty buffer back to Elasticsearch bytes
ChannelBuffer receivedBuffer = // ... received from network
BytesReference elasticsearchBytes = Netty3Utils.toBytesReference(receivedBuffer);
// Convert with specific size limit
BytesReference limitedBytes = Netty3Utils.toBytesReference(receivedBuffer, 1024);
// Close multiple channels at once
List<Channel> channels = // ... collection of channels
Netty3Utils.closeChannels(channels);Additional utilities for managing Netty channel lifecycle and connection tracking.
/**
* Handler for tracking and managing open Netty channels
*/
public class Netty3OpenChannelsHandler extends SimpleChannelUpstreamHandler implements Releasable {
/**
* Constructor for channel tracking handler
* @param logger Logger instance for debugging and monitoring
*/
public Netty3OpenChannelsHandler(Logger logger);
/**
* Handles upstream channel events for connection tracking
* @param ctx Channel handler context
* @param e Channel event to process
*/
public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e);
/**
* Returns the current number of open channels
* @return int count of currently open channels
*/
public int numberOfOpenChannels();
/**
* Returns the total number of channels that have been created
* @return long total channel count since startup
*/
public long totalChannels();
/**
* Closes all currently tracked open channels
*/
public void close();
}Specialized BytesReference implementation that wraps Netty ChannelBuffer for efficient memory management.
/**
* BytesReference implementation that wraps a Netty ChannelBuffer
* Provides efficient access to network buffer data without copying
*/
public class ChannelBufferBytesReference implements BytesReference {
/**
* Constructor wrapping a ChannelBuffer with specified size
* @param buffer ChannelBuffer to wrap
* @param size Number of bytes to include from the buffer
*/
public ChannelBufferBytesReference(ChannelBuffer buffer, int size);
/**
* Gets a single byte at the specified index
* @param index Position to read from
* @return byte value at the index
*/
public byte get(int index);
/**
* Returns the total length of available data
* @return int length in bytes
*/
public int length();
/**
* Creates a slice of the buffer for the specified range
* @param from Starting position (inclusive)
* @param length Number of bytes to include
* @return BytesReference containing the slice
*/
public BytesReference slice(int from, int length);
/**
* Creates a stream input for reading the buffer data
* @return StreamInput for sequential reading
*/
public StreamInput streamInput();
/**
* Writes the buffer contents to an output stream
* @param out OutputStream to write to
*/
public void writeTo(OutputStream out);
}StreamInput implementation for reading from Netty ChannelBuffer data.
/**
* StreamInput implementation for reading from ChannelBuffer data
*/
public class ChannelBufferStreamInput extends StreamInput {
/**
* Constructor for creating stream input from ChannelBuffer
* @param buffer ChannelBuffer containing data to read
* @param size Number of bytes available for reading
*/
public ChannelBufferStreamInput(ChannelBuffer buffer, int size);
/**
* Reads a single byte from the stream
* @return int byte value (0-255) or -1 if end of stream
*/
public int read();
/**
* Reads multiple bytes into a byte array
* @param b Destination byte array
* @param off Offset in the destination array
* @param len Maximum number of bytes to read
* @return int number of bytes actually read
*/
public int read(byte[] b, int off, int len);
/**
* Resets the stream position to the beginning
*/
public void reset();
/**
* Closes the stream and releases resources
*/
public void close();
}The utilities include comprehensive error handling and logging integration.
/**
* Internal logger adapter for Netty 3 integration with Elasticsearch logging
*/
public class Netty3InternalESLogger implements InternalLogger {
// Provides bridge between Netty 3 logging and Elasticsearch logging system
// Handles log level mapping and message formatting
}Setup and Integration:
// The setup() method configures:
// - Netty 3 logging to use Elasticsearch's logging framework
// - Thread naming patterns for better debugging
// - System property configuration for optimal performance
// - Error handling integration
public static void setup() {
// Configure Netty logging to use ES logging
InternalLoggerFactory.setDefaultFactory(new Netty3InternalESLoggerFactory());
// Set thread naming pattern for Netty threads
System.setProperty("io.netty.threadLocalDirectBufferSize", /* optimized size */);
// Additional Netty 3 system optimizations
// ...
}The utilities provide efficient memory management for network operations:
Install with Tessl CLI
npx tessl i tessl/maven-org-elasticsearch-plugin--transport-netty3-client