Search in sources :

Example 1 with PCmdRequest

use of com.navercorp.pinpoint.grpc.trace.PCmdRequest in project pinpoint by naver.

the class PinpointGrpcServerTest method requestTest.

@Test
public void requestTest() {
    RecordedStreamObserver<PCmdRequest> recordedStreamObserver = new RecordedStreamObserver<PCmdRequest>();
    PinpointGrpcServer pinpointGrpcServer = new PinpointGrpcServer(Mockito.mock(InetSocketAddress.class), agentInfo, new RequestManager(testTimer, 3000), Mockito.mock(ProfilerClusterManager.class), recordedStreamObserver);
    pinpointGrpcServer.connected();
    List<Integer> supportCommandList = Collections.singletonList(Short.toUnsignedInt(TCommandType.ECHO.getCode()));
    pinpointGrpcServer.handleHandshake(supportCommandList);
    Future<ResponseMessage> future = pinpointGrpcServer.request(this.request);
    Assert.assertEquals(1, recordedStreamObserver.getRequestCount());
    // timeout
    awaitAndAssert(future, false);
    future = pinpointGrpcServer.request(this.request);
    Assert.assertEquals(2, recordedStreamObserver.getRequestCount());
    PCmdRequest latestRequest = recordedStreamObserver.getLatestRequest();
    pinpointGrpcServer.handleMessage(latestRequest.getRequestId(), PCmdEchoResponse.newBuilder().setMessage(latestRequest.getCommandEcho().getMessage()).build());
    // success
    awaitAndAssert(future, true);
    future = pinpointGrpcServer.request(this.request);
    Assert.assertEquals(3, recordedStreamObserver.getRequestCount());
    latestRequest = recordedStreamObserver.getLatestRequest();
    PCmdResponse.Builder builder = PCmdResponse.newBuilder();
    PCmdResponse response = builder.setMessage(StringValue.of("fail")).setResponseId(latestRequest.getRequestId()).build();
    pinpointGrpcServer.handleFail(response);
    // fail
    awaitAndAssert(future, false);
    pinpointGrpcServer.close();
    assertCurrentState(SocketStateCode.CLOSED_BY_SERVER, pinpointGrpcServer);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ResponseMessage(com.navercorp.pinpoint.rpc.ResponseMessage) RequestManager(com.navercorp.pinpoint.rpc.client.RequestManager) PCmdRequest(com.navercorp.pinpoint.grpc.trace.PCmdRequest) ProfilerClusterManager(com.navercorp.pinpoint.collector.cluster.ProfilerClusterManager) PCmdResponse(com.navercorp.pinpoint.grpc.trace.PCmdResponse) Test(org.junit.Test)

Example 2 with PCmdRequest

use of com.navercorp.pinpoint.grpc.trace.PCmdRequest in project pinpoint by naver.

the class GrpcCommandServiceTest method oldVersionHandshakeFailTest.

@Test
public void oldVersionHandshakeFailTest() throws IOException, PinpointZookeeperException {
    ZookeeperProfilerClusterManager manager = creteMemoryClusterManager();
    ZookeeperClusterService mockClusterService = Mockito.mock(ZookeeperClusterService.class);
    Mockito.when(mockClusterService.getProfilerClusterManager()).thenReturn(manager);
    try (GrpcCommandService commandService = new GrpcCommandService(mockClusterService)) {
        TransportMetadata transportMetaData = createTransportMetaData(new InetSocketAddress("127.0.0.1", 61613), 10);
        attachContext(transportMetaData);
        attachContext(new Header("test", "agentId", "agentName", "applicationName", ServiceType.UNDEFINED.getCode(), System.currentTimeMillis(), Header.SOCKET_ID_NOT_EXIST, getCodeList()));
        final TempServerCallStreamObserver<PCmdRequest> requestObserver = new TempServerCallStreamObserver<>();
        StreamObserver<PCmdMessage> handleMessageObserver = commandService.handleCommand(requestObserver);
        Assert.assertThrows(ConditionTimeoutException.class, () -> {
            Awaitility.await("oldVersionHandshakeFailTest").timeout(400, TimeUnit.MILLISECONDS).until(manager::getClusterData, hasSize(1));
        });
        Assert.assertNotNull(requestObserver.getLatestException());
    }
}
Also used : PCmdMessage(com.navercorp.pinpoint.grpc.trace.PCmdMessage) ZookeeperClusterService(com.navercorp.pinpoint.collector.cluster.zookeeper.ZookeeperClusterService) ZookeeperProfilerClusterManager(com.navercorp.pinpoint.collector.cluster.zookeeper.ZookeeperProfilerClusterManager) Header(com.navercorp.pinpoint.grpc.Header) DefaultTransportMetadata(com.navercorp.pinpoint.grpc.server.DefaultTransportMetadata) TransportMetadata(com.navercorp.pinpoint.grpc.server.TransportMetadata) PCmdRequest(com.navercorp.pinpoint.grpc.trace.PCmdRequest) InetSocketAddress(java.net.InetSocketAddress) GrpcCommandService(com.navercorp.pinpoint.collector.receiver.grpc.service.command.GrpcCommandService) Test(org.junit.Test)

Example 3 with PCmdRequest

use of com.navercorp.pinpoint.grpc.trace.PCmdRequest in project pinpoint by naver.

the class PinpointGrpcServer method request.

public Future<ResponseMessage> request(GeneratedMessageV3 message) {
    if (!state.checkState(SocketStateCode.RUN_DUPLEX)) {
        return createFailedFuture(new IllegalStateException("failed to request message. caused:illegal State"));
    }
    PCmdRequest request = createRequest(message);
    if (request == null) {
        return createFailedFuture(new PinpointSocketException(TRouteResult.NOT_SUPPORTED_REQUEST.name()));
    }
    DefaultFuture<ResponseMessage> future = requestManager.register(request.getRequestId());
    requestObserver.onNext(request);
    return future;
}
Also used : PCmdRequest(com.navercorp.pinpoint.grpc.trace.PCmdRequest) PinpointSocketException(com.navercorp.pinpoint.rpc.PinpointSocketException) ResponseMessage(com.navercorp.pinpoint.rpc.ResponseMessage)

Example 4 with PCmdRequest

use of com.navercorp.pinpoint.grpc.trace.PCmdRequest in project pinpoint by naver.

the class PinpointGrpcServer method openStream.

// 1st message : client(collector) -> server(agent)
public ClientStreamChannel openStream(GeneratedMessageV3 message, ClientStreamChannelEventHandler streamChannelEventHandler) throws StreamException {
    if (!state.checkState(SocketStateCode.RUN_DUPLEX)) {
        throw new StreamException(StreamCode.STATE_NOT_CONNECTED);
    }
    PCmdRequest request = createRequest(message);
    if (request == null) {
        throw new StreamException(StreamCode.TYPE_UNSUPPORT);
    }
    GrpcClientStreamChannel grpcClientStreamChannel = new GrpcClientStreamChannel(remoteAddress, request.getRequestId(), streamChannelRepository, streamChannelEventHandler);
    try {
        grpcClientStreamChannel.init();
        grpcClientStreamChannel.connect(new Runnable() {

            @Override
            public void run() {
                requestObserver.onNext(request);
            }
        }, 1000);
    } catch (StreamException e) {
        grpcClientStreamChannel.close(e.getStreamCode());
        throw e;
    }
    return grpcClientStreamChannel;
}
Also used : PCmdRequest(com.navercorp.pinpoint.grpc.trace.PCmdRequest) StreamException(com.navercorp.pinpoint.rpc.stream.StreamException)

Example 5 with PCmdRequest

use of com.navercorp.pinpoint.grpc.trace.PCmdRequest in project pinpoint by naver.

the class GrpcCommandService method handleCommandV2.

@Override
public StreamObserver<PCmdMessage> handleCommandV2(StreamObserver<PCmdRequest> requestObserver) {
    final Long transportId = getTransportId();
    final AgentInfo agentInfo = getAgentInfo();
    final List<Integer> supportCommandCodeList = getSupportCommandCodeList();
    logger.info("{} => local. handleCommandV2(). transportId:{}, supportCommandCodeList{}", agentInfo, transportId, supportCommandCodeList);
    if (supportCommandCodeList == Header.SUPPORT_COMMAND_CODE_LIST_NOT_EXIST) {
        logger.warn("handleCommandV2() not allow empty Header:{}. Connection will be disconnected.", Header.SUPPORT_COMMAND_CODE.name());
        requestObserver.onError(new StatusException(Status.INVALID_ARGUMENT));
        return DisabledStreamObserver.instance();
    }
    final PinpointGrpcServer pinpointGrpcServer = registerNewPinpointGrpcServer(requestObserver, agentInfo, transportId);
    if (pinpointGrpcServer == null) {
        return handleServerRegistrationFailed(requestObserver, agentInfo, transportId);
    }
    final ServerCallStreamObserver<PCmdRequest> serverCallStreamObserver = (ServerCallStreamObserver<PCmdRequest>) requestObserver;
    serverCallStreamObserver.setOnReadyHandler(new Runnable() {

        public void run() {
            if (serverCallStreamObserver.isReady()) {
                logger.info("{} => local. ready() transportId:{}", agentInfo.getAgentKey(), transportId);
                pinpointGrpcServer.connected();
                registerAgentCommandList(pinpointGrpcServer, supportCommandCodeList);
            }
            pinpointGrpcServer.setOnCloseHandler(new Runnable() {

                @Override
                public void run() {
                    unregisterPinpointGrpcServer(transportId);
                }
            });
        }
    });
    final StreamObserver<PCmdMessage> responseObserver = new StreamObserver<PCmdMessage>() {

        @Override
        public void onNext(PCmdMessage value) {
            if (value.hasFailMessage()) {
                PCmdResponse failMessage = value.getFailMessage();
                pinpointGrpcServer.handleFail(failMessage);
            }
        }

        @Override
        public void onError(Throwable t) {
            handleOnError(t, pinpointGrpcServer, agentInfo);
        }

        @Override
        public void onCompleted() {
            handleOnCompleted(pinpointGrpcServer, agentInfo);
        }
    };
    return responseObserver;
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) PinpointGrpcServer(com.navercorp.pinpoint.collector.receiver.grpc.PinpointGrpcServer) StatusException(io.grpc.StatusException) PCmdMessage(com.navercorp.pinpoint.grpc.trace.PCmdMessage) PCmdRequest(com.navercorp.pinpoint.grpc.trace.PCmdRequest) AgentInfo(com.navercorp.pinpoint.collector.cluster.AgentInfo) PCmdResponse(com.navercorp.pinpoint.grpc.trace.PCmdResponse)

Aggregations

PCmdRequest (com.navercorp.pinpoint.grpc.trace.PCmdRequest)6 PCmdMessage (com.navercorp.pinpoint.grpc.trace.PCmdMessage)3 PCmdResponse (com.navercorp.pinpoint.grpc.trace.PCmdResponse)3 AgentInfo (com.navercorp.pinpoint.collector.cluster.AgentInfo)2 PinpointGrpcServer (com.navercorp.pinpoint.collector.receiver.grpc.PinpointGrpcServer)2 ResponseMessage (com.navercorp.pinpoint.rpc.ResponseMessage)2 StatusException (io.grpc.StatusException)2 ServerCallStreamObserver (io.grpc.stub.ServerCallStreamObserver)2 StreamObserver (io.grpc.stub.StreamObserver)2 InetSocketAddress (java.net.InetSocketAddress)2 Test (org.junit.Test)2 ProfilerClusterManager (com.navercorp.pinpoint.collector.cluster.ProfilerClusterManager)1 ZookeeperClusterService (com.navercorp.pinpoint.collector.cluster.zookeeper.ZookeeperClusterService)1 ZookeeperProfilerClusterManager (com.navercorp.pinpoint.collector.cluster.zookeeper.ZookeeperProfilerClusterManager)1 GrpcCommandService (com.navercorp.pinpoint.collector.receiver.grpc.service.command.GrpcCommandService)1 Header (com.navercorp.pinpoint.grpc.Header)1 DefaultTransportMetadata (com.navercorp.pinpoint.grpc.server.DefaultTransportMetadata)1 TransportMetadata (com.navercorp.pinpoint.grpc.server.TransportMetadata)1 PinpointSocketException (com.navercorp.pinpoint.rpc.PinpointSocketException)1 RequestManager (com.navercorp.pinpoint.rpc.client.RequestManager)1