1.回顾 SearchTransportService
public class SearchTransportService extends AbstractComponent
// 转发分片,连接信息,节点信息,分片信息,任务信息
public void sendExecuteQuery(Transport.Connection connection, final ShardSearchTransportRequest request, SearchTask task,
final SearchActionListener<SearchPhaseResult> listener) {
// we optimize this and expect a QueryFetchSearchResult if we only have a single shard in the search request
// this used to be the QUERY_AND_FETCH which doesn't exist anymore
final boolean fetchDocuments = request.numberOfShards() == 1;
Supplier<SearchPhaseResult> supplier = fetchDocuments ? QueryFetchSearchResult::new : QuerySearchResult::new;
final ActionListener handler = responseWrapper.apply(connection, listener);//比如 SearchExecutionStatsCollector
transportService.sendChildRequest(connection, QUERY_ACTION_NAME, request, task,
new ActionListenerResponseHandler<>(handler, supplier));
}
2.TransportService
public class TransportService extends AbstractLifecycleComponent
TransportService :方法:1
public <T extends TransportResponse> void sendChildRequest(final Transport.Connection connection, final String action,
final TransportRequest request, final Task parentTask,
final TransportResponseHandler<T> handler) {
sendChildRequest(connection, action, request, parentTask, TransportRequestOptions.EMPTY, handler);
}
TransportService :方法:2
public <T extends TransportResponse> void sendChildRequest(final Transport.Connection connection, final String action,
final TransportRequest request, final Task parentTask,
final TransportRequestOptions options,
final TransportResponseHandler<T> handler) {
request.setParentTask(localNode.getId(), parentTask.getId());
try {
sendRequest(connection, action, request, options, handler);
} catch (TaskCancelledException ex) {
}
}
TransportService :方法:3
异步发送请求
asyncSender$TransportService
public final <T extends TransportResponse> void sendRequest(final Transport.Connection connection, final String action,
final TransportRequest request,
final TransportRequestOptions options,
TransportResponseHandler<T> handler) {
asyncSender.sendRequest(connection, action, request, options, handler);
}
下回衔接:思考,asyncSender.sendRequest异步请求到sendRequestInternal的经过
构造方法触发
public TransportService(Settings settings, Transport transport, ThreadPool threadPool, TransportInterceptor transportInterceptor, Function<BoundTransportAddress, DiscoveryNode> localNodeFactory, @Nullable ClusterSettings clusterSettings) {
super(settings);
this.asyncSender = interceptor.interceptSender(this::sendRequestInternal);
}
TransportService :方法:4
private <T extends TransportResponse> void sendRequestInternal(final Transport.Connection connection, final String action,
final TransportRequest request,
final TransportRequestOptions options,
TransportResponseHandler<T> handler) {
DiscoveryNode node = connection.getNode();
final long requestId = transport.newRequestId();
final TimeoutHandler timeoutHandler;
try {
if (options.timeout() == null) {
timeoutHandler = null;
} else {
timeoutHandler = new TimeoutHandler(requestId);
}
Supplier<ThreadContext.StoredContext> storedContextSupplier = threadPool.getThreadContext().newRestorableContext(true);
TransportResponseHandler<T> responseHandler = new ContextRestoreResponseHandler<>(storedContextSupplier, handler);
// map 存储requestId请求ID
clientHandlers.put(requestId, new RequestHolder<>(responseHandler, connection, action, timeoutHandler));
connection.sendRequest(requestId, action, request, options); // local node optimization happens upstream
} catch (final Exception e) {
}
}
TransportService :方法:5
@Override
public void sendRequest(long requestId, String action, TransportRequest request, TransportRequestOptions options)
throws IOException, TransportException {
//内部的RPC请求通过TransportService#sendRequest发送
//sendRequest会检查目的节点是否是本节点,如果是本节点,
//则在sendLocalRequest方法中直接通过action 获取对应的Handler,调用对应的处理函数
//TransportService#sendRequest
sendLocalRequest(requestId, action, request, options);
}
TransportService :方法:6
执行本地任务
reg.processMessageReceived(request, channel)
//ShardSearchTransportRequest
private void sendLocalRequest(long requestId, final String action, final TransportRequest request, TransportRequestOptions options) {
if (request instanceof ShardSearchTransportRequest) {
ShardSearchTransportRequest shardSearchTransportRequest = (ShardSearchTransportRequest) request;
if (!Objects.equals(shardSearchTransportRequest.shardId().getIndexName(), ".kibana")) {
System.out.println("请求本地分片索引:" + shardSearchTransportRequest.shardId().getIndexName());
}
}
final DirectResponseChannel channel = new DirectResponseChannel(logger, localNode, action, requestId, adapter, threadPool);
try {
adapter.onRequestSent(localNode, requestId, action, request, options);
adapter.onRequestReceived(requestId, action);
// sendLocalRequest方法中直接通过action 获取对应的Handler,调用对应的处理函
final RequestHandlerRegistry reg = adapter.getRequestHandler(action);// 获取action对应的注册信息
final String executor = reg.getExecutor();// 获取执行器
if (ThreadPool.Names.SAME.equals(executor)) {
//noinspection unchecked
reg.processMessageReceived(request, channel);// 处理task任务
} else {
}
} catch (Exception e) {
}
}
3.RequestHandlerRegistry 注册任务
public class RequestHandlerRegistry
private final TransportRequestHandler<Request> handler;
public void processMessageReceived(Request request, TransportChannel channel) throws Exception {
final Task task = taskManager.register(channel.getChannelType(), action, request);// 创建任务
if (task == null) {
handler.messageReceived(request, channel);// 启动会走这里
} else {
boolean success = false;
try {
// $SearchTransportService#messageReceived
handler.messageReceived(request, new TransportChannelWrapper(taskManager, task, channel), task);
success = true;
} finally {
if (success == false) {
taskManager.unregister(task);
}
}
}
}
不同类的流程子请求发送
getSearchTransport().sendExecuteQuery(getConnection(shardIt.getClusterAlias(), shard.currentNodeId()),
buildShardSearchRequest(shardIt), getTask(), listener)
transportService.sendChildRequest(connection, QUERY_ACTION_NAME, request, task,
new ActionListenerResponseHandler<>(handler, supplier))
asyncSender.sendRequest(connection, action, request, options, handler);
connection.sendRequest(requestId, action, request, options);