Package org.elasticsearch.test.transport
Class MockTransportService
- java.lang.Object
-
- org.elasticsearch.common.component.AbstractLifecycleComponent
-
- org.elasticsearch.transport.TransportService
-
- org.elasticsearch.test.transport.MockTransportService
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
,LifecycleComponent
,Releasable
,TransportConnectionListener
,TransportMessageListener
public final class MockTransportService extends TransportService
A mock delegate service that allows to simulate different network topology failures. Internally it maps TransportAddress objects to rules that inject failures. Adding rules for a node is done by adding rules for all bound addresses of a node (and the publish address, if different). Matching requests to rules is based on the delegate address associated with the discovery node of the request, namely by DiscoveryNode.getAddress(). This address is usually the publish address of the node but can also be a different one (for example, @see org.elasticsearch.discovery.HandshakingTransportAddressConnector, which constructs fake DiscoveryNode instances where the publish address is one of the bound addresses).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
MockTransportService.TestPlugin
static class
MockTransportService.Tracer
-
Nested classes/interfaces inherited from class org.elasticsearch.transport.TransportService
TransportService.ContextRestoreResponseHandler<T extends TransportResponse>, TransportService.HandshakeResponse
-
-
Field Summary
-
Fields inherited from class org.elasticsearch.transport.TransportService
clusterName, connectionManager, DIRECT_RESPONSE_PROFILE, HANDSHAKE_ACTION_NAME, NOOP_TRANSPORT_INTERCEPTOR, taskManager, threadPool, transport, VALID_ACTION_PREFIXES
-
Fields inherited from class org.elasticsearch.common.component.AbstractLifecycleComponent
lifecycle
-
-
Constructor Summary
Constructors Constructor Description MockTransportService(Settings settings, Transport transport, ThreadPool threadPool, TransportInterceptor interceptor, java.util.function.Function<BoundTransportAddress,DiscoveryNode> localNodeFactory, ClusterSettings clusterSettings, java.util.Set<java.lang.String> taskHeaders)
Build the service.MockTransportService(Settings settings, Transport transport, ThreadPool threadPool, TransportInterceptor interceptor, ClusterSettings clusterSettings)
Build the service.
-
Method Summary
Modifier and Type Method Description boolean
addConnectBehavior(TransportAddress transportAddress, StubbableTransport.OpenConnectionBehavior connectBehavior)
Adds a new connect behavior that is used for creating connections with the given delegate address.boolean
addConnectBehavior(TransportService transportService, StubbableTransport.OpenConnectionBehavior connectBehavior)
Adds a new connect behavior that is used for creating connections with the given delegate service.void
addFailToSendNoConnectRule(TransportAddress transportAddress)
Adds a rule that will cause every send request to fail, and each new connect since the rule is added to fail as well.void
addFailToSendNoConnectRule(TransportAddress transportAddress, java.util.Set<java.lang.String> blockedActions)
Adds a rule that will cause matching operations to throw ConnectTransportExceptionsvoid
addFailToSendNoConnectRule(TransportService transportService)
Adds a rule that will cause every send request to fail, and each new connect since the rule is added to fail as well.void
addFailToSendNoConnectRule(TransportService transportService, java.lang.String... blockedActions)
Adds a rule that will cause matching operations to throw ConnectTransportExceptionsvoid
addFailToSendNoConnectRule(TransportService transportService, java.util.Set<java.lang.String> blockedActions)
Adds a rule that will cause matching operations to throw ConnectTransportExceptionsboolean
addGetConnectionBehavior(TransportAddress transportAddress, StubbableConnectionManager.GetConnectionBehavior behavior)
Adds a get connection behavior that is used for communication with the given delegate address.boolean
addGetConnectionBehavior(StubbableConnectionManager.GetConnectionBehavior behavior)
Adds a get connection behavior that is the default get connection behavior.boolean
addNodeConnectedBehavior(TransportAddress transportAddress, StubbableConnectionManager.NodeConnectedBehavior behavior)
Adds a node connected behavior that is used for the given delegate address.boolean
addNodeConnectedBehavior(StubbableConnectionManager.NodeConnectedBehavior behavior)
Adds a node connected behavior that is the default node connected behavior.boolean
addSendBehavior(TransportAddress transportAddress, StubbableTransport.SendRequestBehavior sendBehavior)
Adds a new send behavior that is used for communication with the given delegate address.boolean
addSendBehavior(StubbableTransport.SendRequestBehavior behavior)
Adds a send behavior that is the default send behavior.boolean
addSendBehavior(TransportService transportService, StubbableTransport.SendRequestBehavior sendBehavior)
Adds a new send behavior that is used for communication with the given delegate service.void
addTracer(MockTransportService.Tracer tracer)
void
addUnresponsiveRule(TransportAddress transportAddress)
Adds a rule that will cause ignores each send request, simulating an unresponsive node and failing to connect once the rule was added.void
addUnresponsiveRule(TransportAddress transportAddress, TimeValue duration)
Adds a rule that will cause ignores each send request, simulating an unresponsive node and failing to connect once the rule was added.void
addUnresponsiveRule(TransportService transportService)
Adds a rule that will cause ignores each send request, simulating an unresponsive node and failing to connect once the rule was added.void
addUnresponsiveRule(TransportService transportService, TimeValue duration)
Adds a rule that will cause ignores each send request, simulating an unresponsive node and failing to connect once the rule was added.void
clearAllRules()
Clears all the registered rules.void
clearRule(TransportAddress transportAddress)
Clears the rule associated with the provided delegate address.void
clearRule(TransportService transportService)
Clears the rule associated with the provided delegate service.void
clearTracers()
StubbableConnectionManager
connectionManager()
static MockTransportService
createNewService(Settings settings, Transport transport, Version version, ThreadPool threadPool, ClusterSettings clusterSettings, java.util.Set<java.lang.String> taskHeaders)
static MockTransportService
createNewService(Settings settings, Version version, ThreadPool threadPool, ClusterSettings clusterSettings)
protected TaskManager
createTaskManager(Settings settings, ThreadPool threadPool, java.util.Set<java.lang.String> taskHeaders)
protected void
doClose()
DiscoveryNode
getLocalDiscoNode()
Transport
getOriginalTransport()
static MockNioTransport
newMockTransport(Settings settings, Version version, ThreadPool threadPool)
Transport.Connection
openConnection(DiscoveryNode node, ConnectionProfile profile)
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)
StubbableTransport
transport()
-
Methods inherited from class org.elasticsearch.transport.TransportService
acceptIncomingRequests, addConnectionListener, addressesFromString, boundAddress, connectionValidator, connectToNode, connectToNode, disconnectFromNode, doStart, doStop, getConnection, getConnectionManager, getLocalAddresses, getLocalNode, getRemoteClusterService, getRequestHandler, getTaskManager, getThreadPool, handshake, handshake, info, isValidActionName, nodeConnected, onConnectionClosed, onRequestReceived, onRequestSent, onResponseReceived, onResponseSent, onResponseSent, registerRequestHandler, registerRequestHandler, registerRequestHandler, registerRequestHandler, removeConnectionListener, sendChildRequest, sendChildRequest, sendChildRequest, sendRequest, sendRequest, sendRequest, stats, submitRequest, submitRequest, traceUnresolvedResponse
-
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
-
-
-
-
Constructor Detail
-
MockTransportService
public MockTransportService(Settings settings, Transport transport, ThreadPool threadPool, TransportInterceptor interceptor, @Nullable ClusterSettings clusterSettings)
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
.
-
MockTransportService
public MockTransportService(Settings settings, Transport transport, ThreadPool threadPool, TransportInterceptor interceptor, 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
.
-
-
Method Detail
-
createNewService
public static MockTransportService createNewService(Settings settings, Version version, ThreadPool threadPool, @Nullable ClusterSettings clusterSettings)
-
newMockTransport
public static MockNioTransport newMockTransport(Settings settings, Version version, ThreadPool threadPool)
-
createNewService
public static MockTransportService createNewService(Settings settings, Transport transport, Version version, ThreadPool threadPool, @Nullable ClusterSettings clusterSettings, java.util.Set<java.lang.String> taskHeaders)
-
createTaskManager
protected TaskManager createTaskManager(Settings settings, ThreadPool threadPool, java.util.Set<java.lang.String> taskHeaders)
- Overrides:
createTaskManager
in classTransportService
-
clearAllRules
public void clearAllRules()
Clears all the registered rules.
-
clearRule
public void clearRule(TransportService transportService)
Clears the rule associated with the provided delegate service.
-
clearRule
public void clearRule(TransportAddress transportAddress)
Clears the rule associated with the provided delegate address.
-
addFailToSendNoConnectRule
public void addFailToSendNoConnectRule(TransportService transportService)
Adds a rule that will cause every send request to fail, and each new connect since the rule is added to fail as well.
-
addFailToSendNoConnectRule
public void addFailToSendNoConnectRule(TransportAddress transportAddress)
Adds a rule that will cause every send request to fail, and each new connect since the rule is added to fail as well.
-
addFailToSendNoConnectRule
public void addFailToSendNoConnectRule(TransportService transportService, java.lang.String... blockedActions)
Adds a rule that will cause matching operations to throw ConnectTransportExceptions
-
addFailToSendNoConnectRule
public void addFailToSendNoConnectRule(TransportService transportService, java.util.Set<java.lang.String> blockedActions)
Adds a rule that will cause matching operations to throw ConnectTransportExceptions
-
addFailToSendNoConnectRule
public void addFailToSendNoConnectRule(TransportAddress transportAddress, java.util.Set<java.lang.String> blockedActions)
Adds a rule that will cause matching operations to throw ConnectTransportExceptions
-
addUnresponsiveRule
public void addUnresponsiveRule(TransportService transportService)
Adds a rule that will cause ignores each send request, simulating an unresponsive node and failing to connect once the rule was added.
-
addUnresponsiveRule
public void addUnresponsiveRule(TransportAddress transportAddress)
Adds a rule that will cause ignores each send request, simulating an unresponsive node and failing to connect once the rule was added.
-
addUnresponsiveRule
public void addUnresponsiveRule(TransportService transportService, TimeValue duration)
Adds a rule that will cause ignores each send request, simulating an unresponsive node and failing to connect once the rule was added.- Parameters:
duration
- the amount of time to delay sending and connecting.
-
addUnresponsiveRule
public void addUnresponsiveRule(TransportAddress transportAddress, TimeValue duration)
Adds a rule that will cause ignores each send request, simulating an unresponsive node and failing to connect once the rule was added.- Parameters:
duration
- the amount of time to delay sending and connecting.
-
addSendBehavior
public boolean addSendBehavior(TransportService transportService, StubbableTransport.SendRequestBehavior sendBehavior)
Adds a new send behavior that is used for communication with the given delegate service.- Returns:
true
if no other send behavior was registered for any of the addresses bound by delegate service.
-
addSendBehavior
public boolean addSendBehavior(TransportAddress transportAddress, StubbableTransport.SendRequestBehavior sendBehavior)
Adds a new send behavior that is used for communication with the given delegate address.- Returns:
true
if no other send behavior was registered for this address before.
-
addSendBehavior
public boolean addSendBehavior(StubbableTransport.SendRequestBehavior behavior)
Adds a send behavior that is the default send behavior.- Returns:
true
if no default send behavior was registered
-
addConnectBehavior
public boolean addConnectBehavior(TransportService transportService, StubbableTransport.OpenConnectionBehavior connectBehavior)
Adds a new connect behavior that is used for creating connections with the given delegate service.- Returns:
true
if no other send behavior was registered for any of the addresses bound by delegate service.
-
addConnectBehavior
public boolean addConnectBehavior(TransportAddress transportAddress, StubbableTransport.OpenConnectionBehavior connectBehavior)
Adds a new connect behavior that is used for creating connections with the given delegate address.- Returns:
true
if no other send behavior was registered for this address before.
-
addGetConnectionBehavior
public boolean addGetConnectionBehavior(TransportAddress transportAddress, StubbableConnectionManager.GetConnectionBehavior behavior)
Adds a get connection behavior that is used for communication with the given delegate address.- Returns:
true
if no other get connection behavior was registered for this address before.
-
addGetConnectionBehavior
public boolean addGetConnectionBehavior(StubbableConnectionManager.GetConnectionBehavior behavior)
Adds a get connection behavior that is the default get connection behavior.- Returns:
true
if no default get connection behavior was registered.
-
addNodeConnectedBehavior
public boolean addNodeConnectedBehavior(TransportAddress transportAddress, StubbableConnectionManager.NodeConnectedBehavior behavior)
Adds a node connected behavior that is used for the given delegate address.- Returns:
true
if no other node connected behavior was registered for this address before.
-
addNodeConnectedBehavior
public boolean addNodeConnectedBehavior(StubbableConnectionManager.NodeConnectedBehavior behavior)
Adds a node connected behavior that is the default node connected behavior.- Returns:
true
if no default node connected behavior was registered.
-
transport
public StubbableTransport transport()
-
connectionManager
public StubbableConnectionManager connectionManager()
-
addTracer
public void addTracer(MockTransportService.Tracer tracer)
-
clearTracers
public void clearTracers()
-
traceEnabled
protected boolean traceEnabled()
- Overrides:
traceEnabled
in classTransportService
-
traceReceivedRequest
protected void traceReceivedRequest(long requestId, java.lang.String action)
- Overrides:
traceReceivedRequest
in classTransportService
-
traceResponseSent
protected void traceResponseSent(long requestId, java.lang.String action)
- Overrides:
traceResponseSent
in classTransportService
-
traceResponseSent
protected void traceResponseSent(long requestId, java.lang.String action, java.lang.Exception e)
- Overrides:
traceResponseSent
in classTransportService
-
traceReceivedResponse
protected void traceReceivedResponse(long requestId, DiscoveryNode sourceNode, java.lang.String action)
- Overrides:
traceReceivedResponse
in classTransportService
-
traceRequestSent
protected void traceRequestSent(DiscoveryNode node, long requestId, java.lang.String action, TransportRequestOptions options)
- Overrides:
traceRequestSent
in classTransportService
-
getOriginalTransport
public Transport getOriginalTransport()
-
openConnection
public Transport.Connection openConnection(DiscoveryNode node, ConnectionProfile profile) throws java.io.IOException
- Overrides:
openConnection
in classTransportService
- Throws:
java.io.IOException
-
doClose
protected void doClose() throws java.io.IOException
- Overrides:
doClose
in classTransportService
- Throws:
java.io.IOException
-
getLocalDiscoNode
public DiscoveryNode getLocalDiscoNode()
-
-