Package org.elasticsearch.transport
Class TransportService
- java.lang.Object
-
- org.elasticsearch.common.component.AbstractLifecycleComponent
-
- org.elasticsearch.transport.TransportService
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,LifecycleComponent
,Releasable
,TransportConnectionListener
,TransportMessageListener
public class TransportService extends AbstractLifecycleComponent implements TransportMessageListener, TransportConnectionListener
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
TransportService.ContextRestoreResponseHandler<T extends TransportResponse>
This handler wrapper ensures that the response thread executes with the correct thread context.static class
TransportService.HandshakeResponse
-
Field Summary
Fields Modifier and Type Field Description protected ClusterName
clusterName
protected ConnectionManager
connectionManager
static java.lang.String
DIRECT_RESPONSE_PROFILE
static java.lang.String
HANDSHAKE_ACTION_NAME
static TransportInterceptor
NOOP_TRANSPORT_INTERCEPTOR
protected TaskManager
taskManager
protected ThreadPool
threadPool
protected Transport
transport
static java.util.Set<java.lang.String>
VALID_ACTION_PREFIXES
A set of all valid action prefixes.-
Fields inherited from class org.elasticsearch.common.component.AbstractLifecycleComponent
lifecycle
-
-
Constructor Summary
Constructors Constructor Description TransportService(Settings settings, Transport transport, ThreadPool threadPool, TransportInterceptor transportInterceptor, java.util.function.Function<BoundTransportAddress,DiscoveryNode> localNodeFactory, ClusterSettings clusterSettings, java.util.Set<java.lang.String> taskHeaders)
Build the service.TransportService(Settings settings, Transport transport, ThreadPool threadPool, TransportInterceptor transportInterceptor, java.util.function.Function<BoundTransportAddress,DiscoveryNode> localNodeFactory, ClusterSettings clusterSettings, java.util.Set<java.lang.String> taskHeaders, ConnectionManager connectionManager)
-
Method Summary
Modifier and Type Method Description void
acceptIncomingRequests()
start accepting incoming requests.void
addConnectionListener(TransportConnectionListener listener)
TransportAddress[]
addressesFromString(java.lang.String address, int perAddressLimit)
BoundTransportAddress
boundAddress()
CheckedBiConsumer<Transport.Connection,ConnectionProfile,java.io.IOException>
connectionValidator(DiscoveryNode node)
void
connectToNode(DiscoveryNode node)
Connect to the specified node with the default connection profilevoid
connectToNode(DiscoveryNode node, ConnectionProfile connectionProfile)
Connect to the specified node with the given connection profileprotected TaskManager
createTaskManager(Settings settings, ThreadPool threadPool, java.util.Set<java.lang.String> taskHeaders)
void
disconnectFromNode(DiscoveryNode node)
protected void
doClose()
protected void
doStart()
protected void
doStop()
Transport.Connection
getConnection(DiscoveryNode node)
Returns either a real transport connection or a local node connection if we are using the local node optimization.ConnectionManager
getConnectionManager()
java.util.List<java.lang.String>
getLocalAddresses()
DiscoveryNode
getLocalNode()
RemoteClusterService
getRemoteClusterService()
RequestHandlerRegistry<? extends TransportRequest>
getRequestHandler(java.lang.String action)
TaskManager
getTaskManager()
ThreadPool
getThreadPool()
Returns the internal thread poolDiscoveryNode
handshake(Transport.Connection connection, long handshakeTimeout)
Executes a high-level handshake using the given connection and returns the discovery node of the node the connection was established with.TransportService.HandshakeResponse
handshake(Transport.Connection connection, long handshakeTimeout, java.util.function.Predicate<ClusterName> clusterNamePredicate)
Executes a high-level handshake using the given connection and returns the discovery node of the node the connection was established with.TransportInfo
info()
static boolean
isValidActionName(java.lang.String actionName)
Returnstrue
iff the action name starts with a valid prefix.boolean
nodeConnected(DiscoveryNode node)
Returnstrue
iff the given node is already connected.void
onConnectionClosed(Transport.Connection connection)
Called once a connection ws closed.void
onRequestReceived(long requestId, java.lang.String action)
called by theTransport
implementation when an incoming request arrives but before any parsing of it has happened (with the exception of the requestId and action)void
onRequestSent(DiscoveryNode node, long requestId, java.lang.String action, TransportRequest request, TransportRequestOptions options)
called by theTransport
implementation once a request has been sentvoid
onResponseReceived(long requestId, Transport.ResponseContext holder)
Called for every response receivedvoid
onResponseSent(long requestId, java.lang.String action, java.lang.Exception e)
called by theTransport
implementation after an exception was sent as a response to an incoming requestvoid
onResponseSent(long requestId, java.lang.String action, TransportResponse response)
called by theTransport
implementation once a response was sent to calling nodeTransport.Connection
openConnection(DiscoveryNode node, ConnectionProfile connectionProfile)
Establishes and returns a new connection to the given node.<Request extends TransportRequest>
voidregisterRequestHandler(java.lang.String action, java.lang.String executor, boolean forceExecution, boolean canTripCircuitBreaker, Writeable.Reader<Request> requestReader, TransportRequestHandler<Request> handler)
Registers a new request handler<Request extends TransportRequest>
voidregisterRequestHandler(java.lang.String action, java.lang.String executor, Writeable.Reader<Request> requestReader, TransportRequestHandler<Request> handler)
Registers a new request handler<Request extends TransportRequest>
voidregisterRequestHandler(java.lang.String action, java.util.function.Supplier<Request> request, java.lang.String executor, boolean forceExecution, boolean canTripCircuitBreaker, TransportRequestHandler<Request> handler)
Registers a new request handler<Request extends TransportRequest>
voidregisterRequestHandler(java.lang.String action, java.util.function.Supplier<Request> requestFactory, java.lang.String executor, TransportRequestHandler<Request> handler)
Registers a new request handlervoid
removeConnectionListener(TransportConnectionListener listener)
<T extends TransportResponse>
voidsendChildRequest(DiscoveryNode node, java.lang.String action, TransportRequest request, Task parentTask, TransportRequestOptions options, TransportResponseHandler<T> handler)
<T extends TransportResponse>
voidsendChildRequest(Transport.Connection connection, java.lang.String action, TransportRequest request, Task parentTask, TransportRequestOptions options, TransportResponseHandler<T> handler)
<T extends TransportResponse>
voidsendChildRequest(Transport.Connection connection, java.lang.String action, TransportRequest request, Task parentTask, TransportResponseHandler<T> handler)
<T extends TransportResponse>
voidsendRequest(DiscoveryNode node, java.lang.String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T> handler)
<T extends TransportResponse>
voidsendRequest(DiscoveryNode node, java.lang.String action, TransportRequest request, TransportResponseHandler<T> handler)
<T extends TransportResponse>
voidsendRequest(Transport.Connection connection, java.lang.String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T> handler)
Sends a request on the specified connection.TransportStats
stats()
<T extends TransportResponse>
TransportFuture<T>submitRequest(DiscoveryNode node, java.lang.String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T> handler)
<T extends TransportResponse>
TransportFuture<T>submitRequest(DiscoveryNode node, java.lang.String action, TransportRequest request, TransportResponseHandler<T> handler)
protected boolean
traceEnabled()
protected void
traceReceivedRequest(long requestId, java.lang.String action)
protected void
traceReceivedResponse(long requestId, DiscoveryNode sourceNode, java.lang.String action)
protected void
traceRequestSent(DiscoveryNode node, long requestId, java.lang.String action, TransportRequestOptions options)
protected void
traceResponseSent(long requestId, java.lang.String action)
protected void
traceResponseSent(long requestId, java.lang.String action, java.lang.Exception e)
protected void
traceUnresolvedResponse(long requestId)
-
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.transport.TransportConnectionListener
onConnectionOpened, onNodeConnected, onNodeDisconnected
-
-
-
-
Field Detail
-
DIRECT_RESPONSE_PROFILE
public static final java.lang.String DIRECT_RESPONSE_PROFILE
- See Also:
- Constant Field Values
-
HANDSHAKE_ACTION_NAME
public static final java.lang.String HANDSHAKE_ACTION_NAME
- See Also:
- Constant Field Values
-
transport
protected final Transport transport
-
connectionManager
protected final ConnectionManager connectionManager
-
threadPool
protected final ThreadPool threadPool
-
clusterName
protected final ClusterName clusterName
-
taskManager
protected final TaskManager taskManager
-
NOOP_TRANSPORT_INTERCEPTOR
public static final TransportInterceptor NOOP_TRANSPORT_INTERCEPTOR
-
VALID_ACTION_PREFIXES
public static final java.util.Set<java.lang.String> VALID_ACTION_PREFIXES
A set of all valid action prefixes.
-
-
Constructor Detail
-
TransportService
public TransportService(Settings settings, Transport transport, ThreadPool threadPool, TransportInterceptor transportInterceptor, java.util.function.Function<BoundTransportAddress,DiscoveryNode> localNodeFactory, @Nullable ClusterSettings clusterSettings, java.util.Set<java.lang.String> taskHeaders)
Build the service.- Parameters:
clusterSettings
- if non null, the TransportService will register with theClusterSettings
for settings * updates forTransportSettings.TRACE_LOG_EXCLUDE_SETTING
andTransportSettings.TRACE_LOG_INCLUDE_SETTING
.
-
TransportService
public TransportService(Settings settings, Transport transport, ThreadPool threadPool, TransportInterceptor transportInterceptor, java.util.function.Function<BoundTransportAddress,DiscoveryNode> localNodeFactory, @Nullable ClusterSettings clusterSettings, java.util.Set<java.lang.String> taskHeaders, ConnectionManager connectionManager)
-
-
Method Detail
-
getRemoteClusterService
public RemoteClusterService getRemoteClusterService()
-
getLocalNode
public DiscoveryNode getLocalNode()
-
getTaskManager
public TaskManager getTaskManager()
-
createTaskManager
protected TaskManager createTaskManager(Settings settings, ThreadPool threadPool, java.util.Set<java.lang.String> taskHeaders)
-
doStart
protected void doStart()
- Specified by:
doStart
in classAbstractLifecycleComponent
-
doStop
protected void doStop()
- Specified by:
doStop
in classAbstractLifecycleComponent
-
doClose
protected void doClose() throws java.io.IOException
- Specified by:
doClose
in classAbstractLifecycleComponent
- Throws:
java.io.IOException
-
acceptIncomingRequests
public final void acceptIncomingRequests()
start accepting incoming requests. when the transport layer starts up it will block any incoming requests until this method is called
-
info
public TransportInfo info()
-
stats
public TransportStats stats()
-
boundAddress
public BoundTransportAddress boundAddress()
-
getLocalAddresses
public java.util.List<java.lang.String> getLocalAddresses()
-
nodeConnected
public boolean nodeConnected(DiscoveryNode node)
Returnstrue
iff the given node is already connected.
-
connectToNode
public void connectToNode(DiscoveryNode node) throws ConnectTransportException
Connect to the specified node with the default connection profile- Parameters:
node
- the node to connect to- Throws:
ConnectTransportException
-
connectToNode
public void connectToNode(DiscoveryNode node, ConnectionProfile connectionProfile)
Connect to the specified node with the given connection profile- Parameters:
node
- the node to connect toconnectionProfile
- the connection profile to use when connecting to this node
-
connectionValidator
public CheckedBiConsumer<Transport.Connection,ConnectionProfile,java.io.IOException> connectionValidator(DiscoveryNode node)
-
openConnection
public Transport.Connection openConnection(DiscoveryNode node, ConnectionProfile connectionProfile) throws java.io.IOException
Establishes and returns a new connection to the given node. The connection is NOT maintained by this service, it's the callers responsibility to close the connection once it goes out of scope.- Parameters:
node
- the node to connect toconnectionProfile
- the connection profile to use- Throws:
java.io.IOException
-
handshake
public DiscoveryNode handshake(Transport.Connection connection, long handshakeTimeout) throws ConnectTransportException
Executes a high-level handshake using the given connection and returns the discovery node of the node the connection was established with. The handshake will fail if the cluster name on the target node mismatches the local cluster name.- Parameters:
connection
- the connection to a specific nodehandshakeTimeout
- handshake timeout- Returns:
- the connected node
- Throws:
ConnectTransportException
- if the connection failedjava.lang.IllegalStateException
- if the handshake failed
-
handshake
public TransportService.HandshakeResponse handshake(Transport.Connection connection, long handshakeTimeout, java.util.function.Predicate<ClusterName> clusterNamePredicate)
Executes a high-level handshake using the given connection and returns the discovery node of the node the connection was established with. The handshake will fail if the cluster name on the target node doesn't match the local cluster name.- Parameters:
connection
- the connection to a specific nodehandshakeTimeout
- handshake timeoutclusterNamePredicate
- cluster name validation predicate- Returns:
- the handshake response
- Throws:
java.lang.IllegalStateException
- if the handshake failed
-
getConnectionManager
public ConnectionManager getConnectionManager()
-
disconnectFromNode
public void disconnectFromNode(DiscoveryNode node)
-
addConnectionListener
public void addConnectionListener(TransportConnectionListener listener)
-
removeConnectionListener
public void removeConnectionListener(TransportConnectionListener listener)
-
submitRequest
public <T extends TransportResponse> TransportFuture<T> submitRequest(DiscoveryNode node, java.lang.String action, TransportRequest request, TransportResponseHandler<T> handler) throws TransportException
- Throws:
TransportException
-
submitRequest
public <T extends TransportResponse> TransportFuture<T> submitRequest(DiscoveryNode node, java.lang.String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T> handler) throws TransportException
- Throws:
TransportException
-
sendRequest
public <T extends TransportResponse> void sendRequest(DiscoveryNode node, java.lang.String action, TransportRequest request, TransportResponseHandler<T> handler)
-
sendRequest
public final <T extends TransportResponse> void sendRequest(DiscoveryNode node, java.lang.String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T> handler)
-
sendRequest
public final <T extends TransportResponse> void sendRequest(Transport.Connection connection, java.lang.String action, TransportRequest request, TransportRequestOptions options, TransportResponseHandler<T> handler)
Sends a request on the specified connection. If there is a failure sending the request, the specified handler is invoked.- Type Parameters:
T
- the type of the transport response- Parameters:
connection
- the connection to send the request onaction
- the name of the actionrequest
- the requestoptions
- the options for this requesthandler
- the response handler
-
getConnection
public Transport.Connection getConnection(DiscoveryNode node)
Returns either a real transport connection or a local node connection if we are using the local node optimization.- Throws:
NodeNotConnectedException
- if the given node is not connected
-
sendChildRequest
public final <T extends TransportResponse> void sendChildRequest(DiscoveryNode node, java.lang.String action, TransportRequest request, Task parentTask, TransportRequestOptions options, TransportResponseHandler<T> handler)
-
sendChildRequest
public <T extends TransportResponse> void sendChildRequest(Transport.Connection connection, java.lang.String action, TransportRequest request, Task parentTask, TransportResponseHandler<T> handler)
-
sendChildRequest
public <T extends TransportResponse> void sendChildRequest(Transport.Connection connection, java.lang.String action, TransportRequest request, Task parentTask, TransportRequestOptions options, TransportResponseHandler<T> handler)
-
addressesFromString
public TransportAddress[] addressesFromString(java.lang.String address, int perAddressLimit) throws java.net.UnknownHostException
- Throws:
java.net.UnknownHostException
-
isValidActionName
public static boolean isValidActionName(java.lang.String actionName)
Returnstrue
iff the action name starts with a valid prefix.- See Also:
VALID_ACTION_PREFIXES
-
registerRequestHandler
public <Request extends TransportRequest> void registerRequestHandler(java.lang.String action, java.util.function.Supplier<Request> requestFactory, java.lang.String executor, TransportRequestHandler<Request> handler)
Registers a new request handler- Parameters:
action
- The action the request handler is associated withrequestFactory
- a callable to be used construct new instances for streamingexecutor
- The executor the request handling will be executed onhandler
- The handler itself that implements the request handling
-
registerRequestHandler
public <Request extends TransportRequest> void registerRequestHandler(java.lang.String action, java.lang.String executor, Writeable.Reader<Request> requestReader, TransportRequestHandler<Request> handler)
Registers a new request handler- Parameters:
action
- The action the request handler is associated withrequestReader
- a callable to be used construct new instances for streamingexecutor
- The executor the request handling will be executed onhandler
- The handler itself that implements the request handling
-
registerRequestHandler
public <Request extends TransportRequest> void registerRequestHandler(java.lang.String action, java.util.function.Supplier<Request> request, java.lang.String executor, boolean forceExecution, boolean canTripCircuitBreaker, TransportRequestHandler<Request> handler)
Registers a new request handler- Parameters:
action
- The action the request handler is associated withrequest
- The request class that will be used to construct new instances for streamingexecutor
- The executor the request handling will be executed onforceExecution
- Force execution on the executor queue and never reject itcanTripCircuitBreaker
- Check the request size and raise an exception in case the limit is breached.handler
- The handler itself that implements the request handling
-
registerRequestHandler
public <Request extends TransportRequest> void registerRequestHandler(java.lang.String action, java.lang.String executor, boolean forceExecution, boolean canTripCircuitBreaker, Writeable.Reader<Request> requestReader, TransportRequestHandler<Request> handler)
Registers a new request handler- Parameters:
action
- The action the request handler is associated withrequestReader
- The request class that will be used to construct new instances for streamingexecutor
- The executor the request handling will be executed onforceExecution
- Force execution on the executor queue and never reject itcanTripCircuitBreaker
- Check the request size and raise an exception in case the limit is breached.handler
- The handler itself that implements the request handling
-
onRequestSent
public void onRequestSent(DiscoveryNode node, long requestId, java.lang.String action, TransportRequest request, TransportRequestOptions options)
called by theTransport
implementation once a request has been sent- Specified by:
onRequestSent
in interfaceTransportMessageListener
- Parameters:
node
- the node the request was sent torequestId
- the internal request idaction
- the action namerequest
- the actual requestoptions
- the request options
-
traceEnabled
protected boolean traceEnabled()
-
onResponseSent
public void onResponseSent(long requestId, java.lang.String action, TransportResponse response)
called by theTransport
implementation once a response was sent to calling node- Specified by:
onResponseSent
in interfaceTransportMessageListener
- Parameters:
requestId
- the request ID (unique per client)action
- the request actionresponse
- the response send
-
onResponseSent
public void onResponseSent(long requestId, java.lang.String action, java.lang.Exception e)
called by theTransport
implementation after an exception was sent as a response to an incoming request- Specified by:
onResponseSent
in interfaceTransportMessageListener
- Parameters:
requestId
- the request ID (unique per client)action
- the request actione
- the error sent back to the caller
-
traceResponseSent
protected void traceResponseSent(long requestId, java.lang.String action, java.lang.Exception e)
-
onRequestReceived
public void onRequestReceived(long requestId, java.lang.String action)
called by theTransport
implementation when an incoming request arrives but before any parsing of it has happened (with the exception of the requestId and action)- Specified by:
onRequestReceived
in interfaceTransportMessageListener
- Parameters:
requestId
- the internal request IDaction
- the request action
-
getRequestHandler
public RequestHandlerRegistry<? extends TransportRequest> getRequestHandler(java.lang.String action)
-
onResponseReceived
public void onResponseReceived(long requestId, Transport.ResponseContext holder)
Description copied from interface:TransportMessageListener
Called for every response received- Specified by:
onResponseReceived
in interfaceTransportMessageListener
- Parameters:
requestId
- the request id for this reponseholder
- the response context or null if the context was already processed ie. due to a timeout.
-
onConnectionClosed
public void onConnectionClosed(Transport.Connection connection)
Description copied from interface:TransportConnectionListener
Called once a connection ws closed.- Specified by:
onConnectionClosed
in interfaceTransportConnectionListener
- Parameters:
connection
- the closed connection
-
traceReceivedRequest
protected void traceReceivedRequest(long requestId, java.lang.String action)
-
traceResponseSent
protected void traceResponseSent(long requestId, java.lang.String action)
-
traceReceivedResponse
protected void traceReceivedResponse(long requestId, DiscoveryNode sourceNode, java.lang.String action)
-
traceUnresolvedResponse
protected void traceUnresolvedResponse(long requestId)
-
traceRequestSent
protected void traceRequestSent(DiscoveryNode node, long requestId, java.lang.String action, TransportRequestOptions options)
-
getThreadPool
public ThreadPool getThreadPool()
Returns the internal thread pool
-
-