Package org.elasticsearch.transport
Class TcpTransport
- java.lang.Object
-
- org.elasticsearch.common.component.AbstractComponent
-
- org.elasticsearch.common.component.AbstractLifecycleComponent
-
- org.elasticsearch.transport.TcpTransport
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,LifecycleComponent
,Releasable
,Transport
public abstract class TcpTransport extends AbstractLifecycleComponent implements Transport
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TcpTransport.HttpOnTransportException
A helper exception to mark an incoming connection as potentially being HTTP so an appropriate error code can be returnedclass
TcpTransport.NodeChannels
static class
TcpTransport.ProfileSettings
Representation of a transport profile settings for atransport.profiles.$profilename.*
class
TcpTransport.ScheduledPing
-
Nested classes/interfaces inherited from interface org.elasticsearch.transport.Transport
Transport.Connection, Transport.ResponseContext<T extends TransportResponse>, Transport.ResponseHandlers
-
-
Field Summary
-
Fields inherited from class org.elasticsearch.common.component.AbstractComponent
deprecationLogger, logger, settings
-
Fields inherited from class org.elasticsearch.common.component.AbstractLifecycleComponent
lifecycle
-
Fields inherited from interface org.elasticsearch.transport.Transport
TRANSPORT_TCP_COMPRESS
-
-
Constructor Summary
Constructors Constructor Description TcpTransport(java.lang.String transportName, Settings settings, ThreadPool threadPool, BigArrays bigArrays, CircuitBreakerService circuitBreakerService, NamedWriteableRegistry namedWriteableRegistry, NetworkService networkService)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addConnectionListener(TransportConnectionListener listener)
Adds a new event listenerTransportAddress[]
addressesFromString(java.lang.String address, int perAddressLimit)
Returns an address from its string representation.protected abstract TcpChannel
bind(java.lang.String name, java.net.InetSocketAddress address)
Binds to the givenInetSocketAddress
protected void
bindServer(TcpTransport.ProfileSettings profileSettings)
protected java.net.InetSocketAddress
bindToPort(java.lang.String name, java.net.InetAddress hostAddress, java.lang.String port)
BoundTransportAddress
boundAddress()
The address the transport is bound on.boolean
canCompress(TransportRequest request)
void
connectToNode(DiscoveryNode node, ConnectionProfile connectionProfile, CheckedBiConsumer<Transport.Connection,ConnectionProfile,java.io.IOException> connectionValidator)
Connects to a node with the given connection profile.void
disconnectFromNode(DiscoveryNode node)
Disconnected from the given node, if not connected, will do nothing.protected void
doClose()
protected void
doStart()
protected void
doStop()
protected void
ensureOpen()
Ensures this transport is still started / openprotected Version
executeHandshake(DiscoveryNode node, TcpChannel channel, TimeValue timeout)
TcpTransport.NodeChannels
getConnection(DiscoveryNode node)
Returns a connection for the given node if the node is connected.protected Version
getCurrentVersion()
CircuitBreaker
getInFlightRequestBreaker()
java.util.List<java.lang.String>
getLocalAddresses()
Returns a list of all local adresses for this transportstatic java.util.Set<TcpTransport.ProfileSettings>
getProfileSettings(Settings settings)
Returns all profile settings for the given settings objectRequestHandlerRegistry
getRequestHandler(java.lang.String action)
Returns the registered request handler registry for the given action ornull
if it's not registeredTransport.ResponseHandlers
getResponseHandlers()
TransportStats
getStats()
protected java.lang.String
handleRequest(TcpChannel channel, java.lang.String profileName, StreamInput stream, long requestId, int messageLengthBytes, Version version, java.net.InetSocketAddress remoteAddress, byte status)
protected abstract TcpChannel
initiateChannel(DiscoveryNode node, TimeValue connectTimeout, ActionListener<java.lang.Void> connectListener)
Initiate a single tcp socket channel to a node.void
messageReceived(BytesReference reference, TcpChannel channel, java.lang.String profileName, java.net.InetSocketAddress remoteAddress, int messageLengthBytes)
This method handles the message receive part for both request and responsesboolean
nodeConnected(DiscoveryNode node)
Returnstrue
if the node is connected.protected void
onException(TcpChannel channel, java.lang.Exception e)
TcpTransport.NodeChannels
openConnection(DiscoveryNode node, ConnectionProfile connectionProfile)
Opens a new connection to the given node and returns it.java.util.Map<java.lang.String,BoundTransportAddress>
profileBoundAddresses()
Further profile bound addresses<Request extends TransportRequest>
voidregisterRequestHandler(RequestHandlerRegistry<Request> reg)
Registers a new request handlerboolean
removeConnectionListener(TransportConnectionListener listener)
Removes an event listenerprotected ConnectionProfile
resolveConnectionProfile(ConnectionProfile connectionProfile)
static int
resolvePublishPort(TcpTransport.ProfileSettings profileSettings, java.util.List<java.net.InetSocketAddress> boundAddresses, java.net.InetAddress publishInetAddress)
void
sendErrorResponse(Version nodeVersion, java.util.Set<java.lang.String> features, TcpChannel channel, java.lang.Exception error, long requestId, java.lang.String action)
Sends back an error response to the caller via the given channelvoid
sendResponse(Version nodeVersion, java.util.Set<java.lang.String> features, TcpChannel channel, TransportResponse response, long requestId, java.lang.String action, TransportResponseOptions options)
Sends the response to the given channel.protected void
serverAcceptedChannel(TcpChannel channel)
protected void
stopInternal()
Called to tear down internal resourcesstatic boolean
validateMessageHeader(BytesReference buffer)
Validates the first N bytes of the message header and returnsfalse
if the message is a ping message and has no payload ie.protected void
validateRequest(StreamInput stream, long requestId, java.lang.String action)
-
Methods inherited from class org.elasticsearch.common.component.AbstractComponent
logDeprecatedSetting, logRemovedSetting, nodeName
-
Methods inherited from class org.elasticsearch.common.component.AbstractLifecycleComponent
addLifecycleListener, close, lifecycleState, removeLifecycleListener, start, stop
-
Methods inherited from interface org.elasticsearch.common.component.LifecycleComponent
addLifecycleListener, lifecycleState, removeLifecycleListener, start, stop
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.elasticsearch.common.lease.Releasable
close
-
-
-
-
Field Detail
-
TRANSPORT_SERVER_WORKER_THREAD_NAME_PREFIX
public static final java.lang.String TRANSPORT_SERVER_WORKER_THREAD_NAME_PREFIX
- See Also:
- Constant Field Values
-
TRANSPORT_CLIENT_BOSS_THREAD_NAME_PREFIX
public static final java.lang.String TRANSPORT_CLIENT_BOSS_THREAD_NAME_PREFIX
- See Also:
- Constant Field Values
-
HOST
public static final Setting<java.util.List<java.lang.String>> HOST
-
BIND_HOST
public static final Setting<java.util.List<java.lang.String>> BIND_HOST
-
PUBLISH_HOST
public static final Setting<java.util.List<java.lang.String>> PUBLISH_HOST
-
PORT
public static final Setting<java.lang.String> PORT
-
PUBLISH_PORT
public static final Setting<java.lang.Integer> PUBLISH_PORT
-
DEFAULT_PROFILE
public static final java.lang.String DEFAULT_PROFILE
- See Also:
- Constant Field Values
-
CONNECTIONS_PER_NODE_RECOVERY
public static final Setting<java.lang.Integer> CONNECTIONS_PER_NODE_RECOVERY
-
CONNECTIONS_PER_NODE_BULK
public static final Setting<java.lang.Integer> CONNECTIONS_PER_NODE_BULK
-
CONNECTIONS_PER_NODE_REG
public static final Setting<java.lang.Integer> CONNECTIONS_PER_NODE_REG
-
CONNECTIONS_PER_NODE_STATE
public static final Setting<java.lang.Integer> CONNECTIONS_PER_NODE_STATE
-
CONNECTIONS_PER_NODE_PING
public static final Setting<java.lang.Integer> CONNECTIONS_PER_NODE_PING
-
TCP_NO_DELAY
public static final Setting<java.lang.Boolean> TCP_NO_DELAY
-
TCP_KEEP_ALIVE
public static final Setting<java.lang.Boolean> TCP_KEEP_ALIVE
-
TCP_REUSE_ADDRESS
public static final Setting<java.lang.Boolean> TCP_REUSE_ADDRESS
-
TCP_SEND_BUFFER_SIZE
public static final Setting<ByteSizeValue> TCP_SEND_BUFFER_SIZE
-
TCP_RECEIVE_BUFFER_SIZE
public static final Setting<ByteSizeValue> TCP_RECEIVE_BUFFER_SIZE
-
TCP_NO_DELAY_PROFILE
public static final Setting.AffixSetting<java.lang.Boolean> TCP_NO_DELAY_PROFILE
-
TCP_KEEP_ALIVE_PROFILE
public static final Setting.AffixSetting<java.lang.Boolean> TCP_KEEP_ALIVE_PROFILE
-
TCP_REUSE_ADDRESS_PROFILE
public static final Setting.AffixSetting<java.lang.Boolean> TCP_REUSE_ADDRESS_PROFILE
-
TCP_SEND_BUFFER_SIZE_PROFILE
public static final Setting.AffixSetting<ByteSizeValue> TCP_SEND_BUFFER_SIZE_PROFILE
-
TCP_RECEIVE_BUFFER_SIZE_PROFILE
public static final Setting.AffixSetting<ByteSizeValue> TCP_RECEIVE_BUFFER_SIZE_PROFILE
-
BIND_HOST_PROFILE
public static final Setting.AffixSetting<java.util.List<java.lang.String>> BIND_HOST_PROFILE
-
PUBLISH_HOST_PROFILE
public static final Setting.AffixSetting<java.util.List<java.lang.String>> PUBLISH_HOST_PROFILE
-
PORT_PROFILE
public static final Setting.AffixSetting<java.lang.String> PORT_PROFILE
-
PUBLISH_PORT_PROFILE
public static final Setting.AffixSetting<java.lang.Integer> PUBLISH_PORT_PROFILE
-
PING_DATA_SIZE
public static final int PING_DATA_SIZE
- See Also:
- Constant Field Values
-
FEATURE_PREFIX
public static final java.lang.String FEATURE_PREFIX
- See Also:
- Constant Field Values
-
scheduledPing
protected final TcpTransport.ScheduledPing scheduledPing
-
threadPool
protected final ThreadPool threadPool
-
networkService
protected final NetworkService networkService
-
profileSettings
protected final java.util.Set<TcpTransport.ProfileSettings> profileSettings
-
compress
protected final boolean compress
-
defaultConnectionProfile
protected final ConnectionProfile defaultConnectionProfile
-
-
Constructor Detail
-
TcpTransport
public TcpTransport(java.lang.String transportName, Settings settings, ThreadPool threadPool, BigArrays bigArrays, CircuitBreakerService circuitBreakerService, NamedWriteableRegistry namedWriteableRegistry, NetworkService networkService)
-
-
Method Detail
-
doStart
protected void doStart()
- Specified by:
doStart
in classAbstractLifecycleComponent
-
addConnectionListener
public void addConnectionListener(TransportConnectionListener listener)
Description copied from interface:Transport
Adds a new event listener- Specified by:
addConnectionListener
in interfaceTransport
- Parameters:
listener
- the listener to add
-
removeConnectionListener
public boolean removeConnectionListener(TransportConnectionListener listener)
Description copied from interface:Transport
Removes an event listener- Specified by:
removeConnectionListener
in interfaceTransport
- Parameters:
listener
- the listener to remove- Returns:
true
iff the listener was removed otherwisefalse
-
getInFlightRequestBreaker
public CircuitBreaker getInFlightRequestBreaker()
- Specified by:
getInFlightRequestBreaker
in interfaceTransport
-
registerRequestHandler
public <Request extends TransportRequest> void registerRequestHandler(RequestHandlerRegistry<Request> reg)
Description copied from interface:Transport
Registers a new request handler- Specified by:
registerRequestHandler
in interfaceTransport
-
nodeConnected
public boolean nodeConnected(DiscoveryNode node)
Description copied from interface:Transport
Returnstrue
if the node is connected.- Specified by:
nodeConnected
in interfaceTransport
-
connectToNode
public void connectToNode(DiscoveryNode node, ConnectionProfile connectionProfile, CheckedBiConsumer<Transport.Connection,ConnectionProfile,java.io.IOException> connectionValidator) throws ConnectTransportException
Description copied from interface:Transport
Connects to a node with the given connection profile. If the node is already connected this method has no effect. Once a successful is established, it can be validated before being exposed.- Specified by:
connectToNode
in interfaceTransport
- Throws:
ConnectTransportException
-
resolveConnectionProfile
protected ConnectionProfile resolveConnectionProfile(ConnectionProfile connectionProfile)
-
openConnection
public final TcpTransport.NodeChannels openConnection(DiscoveryNode node, ConnectionProfile connectionProfile)
Description copied from interface:Transport
Opens a new connection to the given node and returns it. In contrast toTransport.connectToNode(DiscoveryNode, ConnectionProfile, CheckedBiConsumer)
the returned connection is not managed by the transport implementation. This connection must be closed once it's not needed anymore. This connection type can be used to execute a handshake between two nodes before the node will be published viaTransport.connectToNode(DiscoveryNode, ConnectionProfile, CheckedBiConsumer)
.- Specified by:
openConnection
in interfaceTransport
-
getConnection
public TcpTransport.NodeChannels getConnection(DiscoveryNode node)
Description copied from interface:Transport
Returns a connection for the given node if the node is connected. Connections returned from this method must not be closed. The lifecycle of this connection is maintained by the Transport implementation.- Specified by:
getConnection
in interfaceTransport
- See Also:
Transport.connectToNode(DiscoveryNode, ConnectionProfile, CheckedBiConsumer)
-
disconnectFromNode
public void disconnectFromNode(DiscoveryNode node)
Description copied from interface:Transport
Disconnected from the given node, if not connected, will do nothing.- Specified by:
disconnectFromNode
in interfaceTransport
-
getCurrentVersion
protected Version getCurrentVersion()
-
boundAddress
public BoundTransportAddress boundAddress()
Description copied from interface:Transport
The address the transport is bound on.- Specified by:
boundAddress
in interfaceTransport
-
profileBoundAddresses
public java.util.Map<java.lang.String,BoundTransportAddress> profileBoundAddresses()
Description copied from interface:Transport
Further profile bound addresses- Specified by:
profileBoundAddresses
in interfaceTransport
- Returns:
null
iff profiles are unsupported, otherwise a map with name of profile and its bound transport address
-
getLocalAddresses
public java.util.List<java.lang.String> getLocalAddresses()
Description copied from interface:Transport
Returns a list of all local adresses for this transport- Specified by:
getLocalAddresses
in interfaceTransport
-
bindServer
protected void bindServer(TcpTransport.ProfileSettings profileSettings)
-
bindToPort
protected java.net.InetSocketAddress bindToPort(java.lang.String name, java.net.InetAddress hostAddress, java.lang.String port)
-
resolvePublishPort
public static int resolvePublishPort(TcpTransport.ProfileSettings profileSettings, java.util.List<java.net.InetSocketAddress> boundAddresses, java.net.InetAddress publishInetAddress)
-
addressesFromString
public TransportAddress[] addressesFromString(java.lang.String address, int perAddressLimit) throws java.net.UnknownHostException
Description copied from interface:Transport
Returns an address from its string representation.- Specified by:
addressesFromString
in interfaceTransport
- Throws:
java.net.UnknownHostException
-
doClose
protected final void doClose()
- Specified by:
doClose
in classAbstractLifecycleComponent
-
doStop
protected final void doStop()
- Specified by:
doStop
in classAbstractLifecycleComponent
-
onException
protected void onException(TcpChannel channel, java.lang.Exception e)
-
serverAcceptedChannel
protected void serverAcceptedChannel(TcpChannel channel)
-
bind
protected abstract TcpChannel bind(java.lang.String name, java.net.InetSocketAddress address) throws java.io.IOException
Binds to the givenInetSocketAddress
- Parameters:
name
- the profile nameaddress
- the address to bind to- Throws:
java.io.IOException
-
initiateChannel
protected abstract TcpChannel initiateChannel(DiscoveryNode node, TimeValue connectTimeout, ActionListener<java.lang.Void> connectListener) throws java.io.IOException
Initiate a single tcp socket channel to a node. Implementations do not have to observe the connectTimeout. It is provided for synchronous connection implementations.- Parameters:
node
- the nodeconnectTimeout
- the connection timeoutconnectListener
- listener to be called when connection complete- Returns:
- the pending connection
- Throws:
java.io.IOException
- if an I/O exception occurs while opening the channel
-
stopInternal
protected void stopInternal()
Called to tear down internal resources
-
canCompress
public boolean canCompress(TransportRequest request)
-
sendErrorResponse
public void sendErrorResponse(Version nodeVersion, java.util.Set<java.lang.String> features, TcpChannel channel, java.lang.Exception error, long requestId, java.lang.String action) throws java.io.IOException
Sends back an error response to the caller via the given channel- Parameters:
nodeVersion
- the caller node versionfeatures
- the caller featureschannel
- the channel to send the response toerror
- the error to returnrequestId
- the request ID this response replies toaction
- the action this response replies to- Throws:
java.io.IOException
-
sendResponse
public void sendResponse(Version nodeVersion, java.util.Set<java.lang.String> features, TcpChannel channel, TransportResponse response, long requestId, java.lang.String action, TransportResponseOptions options) throws java.io.IOException
Sends the response to the given channel. This method should be used to sendTransportResponse
objects back to the caller.- Throws:
java.io.IOException
- See Also:
for sending back errors to the caller
-
validateMessageHeader
public static boolean validateMessageHeader(BytesReference buffer) throws java.io.IOException
Validates the first N bytes of the message header and returnsfalse
if the message is a ping message and has no payload ie. isn't a real user level message.- Throws:
java.lang.IllegalStateException
- if the message is too short, less than the header or less that the header plus the message sizeTcpTransport.HttpOnTransportException
- if the message has no valid header and appears to be a HTTP messagejava.lang.IllegalArgumentException
- if the message is greater that the maximum allowed frame size. This is dependent on the available memory.java.io.IOException
-
messageReceived
public final void messageReceived(BytesReference reference, TcpChannel channel, java.lang.String profileName, java.net.InetSocketAddress remoteAddress, int messageLengthBytes) throws java.io.IOException
This method handles the message receive part for both request and responses- Throws:
java.io.IOException
-
handleRequest
protected java.lang.String handleRequest(TcpChannel channel, java.lang.String profileName, StreamInput stream, long requestId, int messageLengthBytes, Version version, java.net.InetSocketAddress remoteAddress, byte status) throws java.io.IOException
- Throws:
java.io.IOException
-
validateRequest
protected void validateRequest(StreamInput stream, long requestId, java.lang.String action) throws java.io.IOException
- Throws:
java.io.IOException
-
executeHandshake
protected Version executeHandshake(DiscoveryNode node, TcpChannel channel, TimeValue timeout) throws java.io.IOException, java.lang.InterruptedException
- Throws:
java.io.IOException
java.lang.InterruptedException
-
ensureOpen
protected final void ensureOpen()
Ensures this transport is still started / open- Throws:
java.lang.IllegalStateException
- if the transport is not started / open
-
getStats
public final TransportStats getStats()
-
getProfileSettings
public static java.util.Set<TcpTransport.ProfileSettings> getProfileSettings(Settings settings)
Returns all profile settings for the given settings object
-
getResponseHandlers
public final Transport.ResponseHandlers getResponseHandlers()
- Specified by:
getResponseHandlers
in interfaceTransport
-
getRequestHandler
public final RequestHandlerRegistry getRequestHandler(java.lang.String action)
Description copied from interface:Transport
Returns the registered request handler registry for the given action ornull
if it's not registered- Specified by:
getRequestHandler
in interfaceTransport
- Parameters:
action
- the action to look up
-
-