Package org.elasticsearch.transport
Class TcpTransport
- java.lang.Object
-
- 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.*
-
Nested classes/interfaces inherited from interface org.elasticsearch.transport.Transport
Transport.Connection, Transport.ResponseContext<T extends TransportResponse>, Transport.ResponseHandlers
-
-
Field Summary
Fields Modifier and Type Field Description protected BigArrays
bigArrays
protected NetworkService
networkService
protected PageCacheRecycler
pageCacheRecycler
protected java.util.Set<TcpTransport.ProfileSettings>
profileSettings
protected Settings
settings
protected ThreadPool
threadPool
static java.lang.String
TRANSPORT_WORKER_THREAD_NAME_PREFIX
-
Fields inherited from class org.elasticsearch.common.component.AbstractLifecycleComponent
lifecycle
-
-
Constructor Summary
Constructors Constructor Description TcpTransport(java.lang.String transportName, Settings settings, Version version, ThreadPool threadPool, PageCacheRecycler pageCacheRecycler, CircuitBreakerService circuitBreakerService, NamedWriteableRegistry namedWriteableRegistry, NetworkService networkService)
-
Method Summary
Modifier and Type Method Description void
addMessageListener(TransportMessageListener listener)
TransportAddress[]
addressesFromString(java.lang.String address, int perAddressLimit)
Returns an address from its string representation.protected abstract TcpServerChannel
bind(java.lang.String name, java.net.InetSocketAddress address)
Binds to the givenInetSocketAddress
protected void
bindServer(TcpTransport.ProfileSettings profileSettings)
BoundTransportAddress
boundAddress()
The address the transport is bound on.int
consumeNetworkReads(TcpChannel channel, BytesReference bytesReference)
Consumes bytes that are available from network reads.protected void
doClose()
protected void
doStart()
protected void
doStop()
protected void
ensureOpen()
Ensures this transport is still started / openvoid
executeHandshake(DiscoveryNode node, TcpChannel channel, ConnectionProfile profile, ActionListener<Version> listener)
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<? extends TransportRequest>
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 void
handleRequest(TcpChannel channel, InboundMessage.RequestMessage message, int messageLengthBytes)
void
inboundMessage(TcpChannel channel, BytesReference message)
Handles inbound message that has been decoded.protected abstract TcpChannel
initiateChannel(DiscoveryNode node)
Initiate a single tcp socket channel.protected ConnectionProfile
maybeOverrideConnectionProfile(ConnectionProfile connectionProfile)
void
messageReceived(BytesReference reference, TcpChannel channel)
This method handles the message receive part for both request and responsesvoid
onException(TcpChannel channel, java.lang.Exception e)
protected void
onNonChannelException(java.lang.Exception exception)
Exception handler for exceptions that are not associated with a specific channel.protected void
onServerException(TcpServerChannel channel, java.lang.Exception e)
Releasable
openConnection(DiscoveryNode node, ConnectionProfile profile, ActionListener<Transport.Connection> listener)
Opens a new connection to the given node.java.util.Map<java.lang.String,BoundTransportAddress>
profileBoundAddresses()
Further profile bound addressesstatic int
readMessageLength(BytesReference networkBytes)
Validates the first 6 bytes of the message header and returns the length of the message.<Request extends TransportRequest>
voidregisterRequestHandler(RequestHandlerRegistry<Request> reg)
Registers a new request handlerboolean
removeMessageListener(TransportMessageListener listener)
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, boolean compress)
Sends the response to the given channel.protected void
serverAcceptedChannel(TcpChannel channel)
protected abstract void
stopInternal()
Called to tear down internal resourcesprotected void
validateRequest(StreamInput stream, long requestId, java.lang.String action)
-
Methods inherited from class org.elasticsearch.common.component.AbstractLifecycleComponent
addLifecycleListener, close, 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.component.LifecycleComponent
addLifecycleListener, lifecycleState, removeLifecycleListener, start, stop
-
Methods inherited from interface org.elasticsearch.common.lease.Releasable
close
-
-
-
-
Field Detail
-
TRANSPORT_WORKER_THREAD_NAME_PREFIX
public static final java.lang.String TRANSPORT_WORKER_THREAD_NAME_PREFIX
- See Also:
- Constant Field Values
-
settings
protected final Settings settings
-
threadPool
protected final ThreadPool threadPool
-
bigArrays
protected final BigArrays bigArrays
-
pageCacheRecycler
protected final PageCacheRecycler pageCacheRecycler
-
networkService
protected final NetworkService networkService
-
profileSettings
protected final java.util.Set<TcpTransport.ProfileSettings> profileSettings
-
-
Constructor Detail
-
TcpTransport
public TcpTransport(java.lang.String transportName, Settings settings, Version version, ThreadPool threadPool, PageCacheRecycler pageCacheRecycler, CircuitBreakerService circuitBreakerService, NamedWriteableRegistry namedWriteableRegistry, NetworkService networkService)
-
-
Method Detail
-
doStart
protected void doStart()
- Specified by:
doStart
in classAbstractLifecycleComponent
-
addMessageListener
public void addMessageListener(TransportMessageListener listener)
- Specified by:
addMessageListener
in interfaceTransport
-
removeMessageListener
public boolean removeMessageListener(TransportMessageListener listener)
- Specified by:
removeMessageListener
in interfaceTransport
-
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
-
maybeOverrideConnectionProfile
protected ConnectionProfile maybeOverrideConnectionProfile(ConnectionProfile connectionProfile)
-
openConnection
public Releasable openConnection(DiscoveryNode node, ConnectionProfile profile, ActionListener<Transport.Connection> listener)
Description copied from interface:Transport
Opens a new connection to the given node. When the connection is fully connected, the listener is called. AReleasable
is returned representing the pending connection. If the caller of this method decides to move on before the listener is called with the completed connection, they should release the pending connection to prevent hanging connections.- Specified by:
openConnection
in interfaceTransport
-
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)
-
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
public void onException(TcpChannel channel, java.lang.Exception e)
-
onServerException
protected void onServerException(TcpServerChannel channel, java.lang.Exception e)
-
onNonChannelException
protected void onNonChannelException(java.lang.Exception exception)
Exception handler for exceptions that are not associated with a specific channel.- Parameters:
exception
- the exception
-
serverAcceptedChannel
protected void serverAcceptedChannel(TcpChannel channel)
-
bind
protected abstract TcpServerChannel 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) throws java.io.IOException
Initiate a single tcp socket channel.- Parameters:
node
- for the initiated connection- Returns:
- the pending connection
- Throws:
java.io.IOException
- if an I/O exception occurs while opening the channel
-
stopInternal
protected abstract void stopInternal()
Called to tear down internal resources
-
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, boolean compress) 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
-
inboundMessage
public void inboundMessage(TcpChannel channel, BytesReference message)
Handles inbound message that has been decoded.- Parameters:
channel
- the channel the message is frommessage
- the message
-
consumeNetworkReads
public int consumeNetworkReads(TcpChannel channel, BytesReference bytesReference) throws java.io.IOException
Consumes bytes that are available from network reads. This method returns the number of bytes consumed in this call.- Parameters:
channel
- the channel read frombytesReference
- the bytes available to consume- Returns:
- the number of bytes consumed
- Throws:
java.io.StreamCorruptedException
- if the message header format is not recognizedTcpTransport.HttpOnTransportException
- if the message header appears to be an HTTP messagejava.lang.IllegalArgumentException
- if the message length is greater that the maximum allowed frame size. This is dependent on the available memory.java.io.IOException
-
readMessageLength
public static int readMessageLength(BytesReference networkBytes) throws java.io.IOException
Validates the first 6 bytes of the message header and returns the length of the message. If 6 bytes are not available, it returns -1.- Parameters:
networkBytes
- the will be read- Returns:
- the length of the message
- Throws:
java.io.StreamCorruptedException
- if the message header format is not recognizedTcpTransport.HttpOnTransportException
- if the message header appears to be an HTTP messagejava.lang.IllegalArgumentException
- if the message length 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) throws java.io.IOException
This method handles the message receive part for both request and responses- Throws:
java.io.IOException
-
handleRequest
protected void handleRequest(TcpChannel channel, InboundMessage.RequestMessage message, int messageLengthBytes) 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
public void executeHandshake(DiscoveryNode node, TcpChannel channel, ConnectionProfile profile, ActionListener<Version> listener)
-
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<? extends TransportRequest> 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
-
-