Skip to content

Commit

Permalink
[#9631] Refactor RequestResponseHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
emeroad committed Aug 17, 2023
1 parent 92986e3 commit a7a3928
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@
@Service
public interface RequestResponseHandler<REQ, RES> {

int type();

void handleRequest(ServerRequest<REQ> serverRequest, ServerResponse<RES> serverResponse);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
import com.navercorp.pinpoint.grpc.trace.PResult;
import com.navercorp.pinpoint.io.request.ServerRequest;
import com.navercorp.pinpoint.io.request.ServerResponse;
import com.navercorp.pinpoint.thrift.io.DefaultTBaseLocator;
import io.grpc.Status;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import java.util.Objects;
Expand All @@ -53,6 +54,11 @@ public GrpcAgentInfoHandler(AgentInfoService agentInfoService, GrpcAgentInfoBoMa
this.agentInfoBoMapper = Objects.requireNonNull(agentInfoBoMapper, "agentInfoBoMapper");
}

@Override
public int type() {
return DefaultTBaseLocator.APIMETADATA;
}

@Override
public void handleSimple(ServerRequest<GeneratedMessageV3> serverRequest) {
final GeneratedMessageV3 data = serverRequest.getData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.navercorp.pinpoint.grpc.trace.PResult;
import com.navercorp.pinpoint.io.request.ServerRequest;
import com.navercorp.pinpoint.io.request.ServerResponse;
import com.navercorp.pinpoint.thrift.io.DefaultTBaseLocator;
import io.grpc.Status;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -51,6 +52,11 @@ public GrpcApiMetaDataHandler(ApiMetaDataService apiMetaDataService) {
this.apiMetaDataService = Objects.requireNonNull(apiMetaDataService, "apiMetaDataService");
}

@Override
public int type() {
return DefaultTBaseLocator.APIMETADATA;
}

@Override
public void handleRequest(ServerRequest<GeneratedMessageV3> serverRequest, ServerResponse<GeneratedMessageV3> serverResponse) {
final GeneratedMessageV3 data = serverRequest.getData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.navercorp.pinpoint.grpc.trace.PTransactionId;
import com.navercorp.pinpoint.io.request.ServerRequest;
import com.navercorp.pinpoint.io.request.ServerResponse;
import com.navercorp.pinpoint.thrift.io.DefaultTBaseLocator;
import io.grpc.Status;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -58,6 +59,11 @@ public GrpcExceptionMetaDataHandler(ExceptionMetaDataService exceptionMetaDataSe
this.exceptionMetaDataService = Objects.requireNonNull(exceptionMetaDataService, "exceptionMetaDataService");
}

@Override
public int type() {
return DefaultTBaseLocator.EXCEPTIONMETADATA;
}

@Override
public void handleRequest(ServerRequest<GeneratedMessageV3> serverRequest, ServerResponse<GeneratedMessageV3> serverResponse) {
final GeneratedMessageV3 data = serverRequest.getData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.navercorp.pinpoint.grpc.trace.PSqlUidMetaData;
import com.navercorp.pinpoint.io.request.ServerRequest;
import com.navercorp.pinpoint.io.request.ServerResponse;
import com.navercorp.pinpoint.thrift.io.DefaultTBaseLocator;
import io.grpc.Status;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -57,6 +58,11 @@ public GrpcSqlMetaDataHandler(SqlMetaDataService[] sqlMetaDataServices, SqlUidMe
}


@Override
public int type() {
return DefaultTBaseLocator.SQLMETADATA;
}

@Override
public void handleRequest(ServerRequest<GeneratedMessageV3> serverRequest, ServerResponse<GeneratedMessageV3> serverResponse) {
final GeneratedMessageV3 data = serverRequest.getData();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2019 NAVER Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.navercorp.pinpoint.collector.handler.grpc;

import com.navercorp.pinpoint.collector.service.SqlMetaDataService;
import com.navercorp.pinpoint.collector.service.SqlUidMetaDataService;
import com.navercorp.pinpoint.thrift.io.DefaultTBaseLocator;
import org.springframework.stereotype.Service;

/**
* @author emeroad
*/
@Service
public class GrpcSqlUidMetaDataHandler extends GrpcSqlMetaDataHandler {

public GrpcSqlUidMetaDataHandler(SqlMetaDataService[] sqlMetaDataServices, SqlUidMetaDataService[] sqlUidMetaDataServices) {
super(sqlMetaDataServices, sqlUidMetaDataServices);
}

@Override
public int type() {
return DefaultTBaseLocator.SQLUIDMETADATA;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
import com.navercorp.pinpoint.grpc.trace.PStringMetaData;
import com.navercorp.pinpoint.io.request.ServerRequest;
import com.navercorp.pinpoint.io.request.ServerResponse;
import com.navercorp.pinpoint.thrift.io.DefaultTBaseLocator;
import io.grpc.Status;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import java.util.Objects;
Expand All @@ -48,6 +49,11 @@ public GrpcStringMetaDataHandler(StringMetaDataService stringMetaDataService) {
this.stringMetaDataService = Objects.requireNonNull(stringMetaDataService, "stringMetaDataService");
}

@Override
public int type() {
return DefaultTBaseLocator.STRINGMETADATA;
}

@Override
public void handleRequest(ServerRequest<GeneratedMessageV3> serverRequest, ServerResponse<GeneratedMessageV3> serverResponse) {
final GeneratedMessageV3 data = serverRequest.getData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
import com.navercorp.pinpoint.collector.handler.RequestResponseHandler;
import com.navercorp.pinpoint.collector.handler.SimpleAndRequestResponseHandler;
import com.navercorp.pinpoint.collector.handler.SimpleHandler;
import com.navercorp.pinpoint.common.util.apache.IntHashMap;
import com.navercorp.pinpoint.io.header.Header;
import com.navercorp.pinpoint.io.request.ServerRequest;
import com.navercorp.pinpoint.io.request.ServerResponse;
import com.navercorp.pinpoint.thrift.io.DefaultTBaseLocator;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;
import java.util.Objects;

/**
Expand All @@ -32,45 +36,37 @@
*/
public class AgentDispatchHandler<REQ, RES> implements DispatchHandler<REQ, RES> {

private final SimpleAndRequestResponseHandler<REQ, RES> agentInfoHandler;

private final RequestResponseHandler<REQ, RES> sqlMetaDataHandler;
private final Logger logger = LogManager.getLogger(getClass());

private final RequestResponseHandler<REQ, RES> apiMetaDataHandler;
private final SimpleAndRequestResponseHandler<REQ, RES> agentInfoHandler;

private final RequestResponseHandler<REQ, RES> stringMetaDataHandler;
private final IntHashMap<RequestResponseHandler<REQ, RES>> handlerMap;

private final RequestResponseHandler<REQ, RES> exceptionMetaDataHandler;

public AgentDispatchHandler(final SimpleAndRequestResponseHandler<REQ, RES> agentInfoHandler,
final RequestResponseHandler<REQ, RES> sqlMetaDataHandler,
final RequestResponseHandler<REQ, RES> apiMetaDataHandler,
final RequestResponseHandler<REQ, RES> stringMetaDataHandler,
final RequestResponseHandler<REQ, RES> exceptionMetaDataHandler) {
List<RequestResponseHandler<REQ, RES>> handlers) {
this.agentInfoHandler = Objects.requireNonNull(agentInfoHandler, "agentInfoHandler");
this.sqlMetaDataHandler = Objects.requireNonNull(sqlMetaDataHandler, "sqlMetaDataHandler");
this.apiMetaDataHandler = Objects.requireNonNull(apiMetaDataHandler, "apiMetaDataHandler");
this.stringMetaDataHandler = Objects.requireNonNull(stringMetaDataHandler, "stringMetaDataHandler");
this.exceptionMetaDataHandler = Objects.requireNonNull(exceptionMetaDataHandler, "exceptionMetaDataHandler");

handlers.forEach(handler -> logger.info("{} {}", handler.type(), handler.getClass()));

this.handlerMap = new IntHashMap<>();
for (RequestResponseHandler<REQ, RES> handler : handlers) {
RequestResponseHandler<REQ, RES> old = handlerMap.put(handler.type(), handler);
if (old != null) {
throw new IllegalArgumentException("Unexpected type new:" + handler + " old:" + old);
}
}

}

protected RequestResponseHandler<REQ, RES> getRequestResponseHandler(ServerRequest<? extends REQ> serverRequest) {
final Header header = serverRequest.getHeader();
final short type = header.getType();
switch (type) {
case DefaultTBaseLocator.SQLMETADATA:
case DefaultTBaseLocator.SQLUIDMETADATA:
return sqlMetaDataHandler;
case DefaultTBaseLocator.APIMETADATA:
return apiMetaDataHandler;
case DefaultTBaseLocator.STRINGMETADATA:
return stringMetaDataHandler;
case DefaultTBaseLocator.AGENT_INFO:
return agentInfoHandler;
case DefaultTBaseLocator.EXCEPTIONMETADATA:
return exceptionMetaDataHandler;
RequestResponseHandler<REQ, RES> handler = this.handlerMap.get(type);
if (handler == null) {
throw new UnsupportedOperationException("unsupported header:" + header);
}
throw new UnsupportedOperationException("unsupported header:" + header);
return handler;
}

private SimpleHandler<REQ> getSimpleHandler(Header header) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@

<bean id="grpcTcpDispatchHandler" class="com.navercorp.pinpoint.collector.receiver.AgentDispatchHandler">
<constructor-arg index="0" ref="grpcAgentInfoHandler"/>
<constructor-arg index="1" ref="grpcSqlMetaDataHandler"/>
<constructor-arg index="2" ref="grpcApiMetaDataHandler"/>
<constructor-arg index="3" ref="grpcStringMetaDataHandler"/>
<constructor-arg index="4" ref="grpcExceptionMetaDataHandler"/>
</bean>

<bean id="grpcDispatchHandlerFactoryBean" class="com.navercorp.pinpoint.collector.receiver.DispatchHandlerFactoryBean">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ public void handleRequest(ServerRequest<TBase<?, ?>> serverRequest, ServerRespon
serverResponse.write(new TResult());
}

@Override
public int type() {
return 0;
}

public int getExecutedCount() {
return executedCount;
Expand Down

0 comments on commit a7a3928

Please sign in to comment.