|
| 1 | +/* |
| 2 | + * Copyright 2022 NAVER Corp. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.navercorp.pinpoint.plugin.thrift.common.server; |
| 18 | + |
| 19 | +import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier; |
| 20 | +import com.navercorp.pinpoint.bootstrap.plugin.util.SocketAddressUtils; |
| 21 | +import com.navercorp.pinpoint.common.plugin.util.HostAndPort; |
| 22 | +import com.navercorp.pinpoint.plugin.thrift.common.TestEnvironment; |
| 23 | +import com.navercorp.pinpoint.plugin.thrift.common.client.AsyncEchoTestClient; |
| 24 | +import com.navercorp.pinpoint.plugin.thrift.common.client.EchoTestClient; |
| 25 | +import com.navercorp.pinpoint.plugin.thrift.common.client.SyncEchoTestClient; |
| 26 | +import com.navercorp.pinpoint.plugin.thrift.common.client.SyncEchoTestClient014; |
| 27 | +import com.navercorp.pinpoint.plugin.thrift.dto.EchoService; |
| 28 | +import org.apache.thrift.TException; |
| 29 | +import org.apache.thrift.TProcessor; |
| 30 | +import org.apache.thrift.async.AsyncMethodCallback; |
| 31 | +import org.apache.thrift.protocol.TBinaryProtocol; |
| 32 | +import org.apache.thrift.server.AbstractNonblockingServer; |
| 33 | +import org.apache.thrift.server.THsHaServer; |
| 34 | +import org.apache.thrift.server.TNonblockingServer; |
| 35 | +import org.apache.thrift.server.TThreadedSelectorServer; |
| 36 | +import org.apache.thrift.transport.TNonblockingServerSocket; |
| 37 | +import org.apache.thrift.transport.TTransportException; |
| 38 | + |
| 39 | +import java.lang.reflect.Method; |
| 40 | +import java.net.InetSocketAddress; |
| 41 | + |
| 42 | +import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.annotation; |
| 43 | +import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.event; |
| 44 | +import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.root; |
| 45 | + |
| 46 | +/** |
| 47 | + * @author HyunGil Jeong |
| 48 | + */ |
| 49 | +public abstract class AsyncEchoTestServer014<T extends AbstractNonblockingServer> extends ThriftEchoTestServer<T> { |
| 50 | + |
| 51 | + protected AsyncEchoTestServer014(T server, TestEnvironment environment) { |
| 52 | + super(server, environment); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void verifyServerTraces(PluginTestVerifier verifier) throws Exception { |
| 57 | + final InetSocketAddress socketAddress = super.environment.getServerAddress(); |
| 58 | + final String address = SocketAddressUtils.getAddressFirst(socketAddress); |
| 59 | + verifier.verifyTraceCount(2); |
| 60 | + // Method process = TBaseAsyncProcessor.class.getDeclaredMethod("process", AsyncFrameBuffer.class); |
| 61 | + Method process = TBinaryProtocol.class.getDeclaredMethod("readMessageEnd"); |
| 62 | + // RootSpan |
| 63 | + verifier.verifyTrace(root("THRIFT_SERVER", // ServiceType, |
| 64 | + "Thrift Server Invocation", // Method |
| 65 | + "com/navercorp/pinpoint/plugin/thrift/dto/EchoService/echo", // rpc |
| 66 | + HostAndPort.toHostAndPortString(address, socketAddress.getPort()), // endPoint |
| 67 | + address // remoteAddress |
| 68 | + )); |
| 69 | + // SpanEvent - TBaseAsyncProcessor.process |
| 70 | + verifier.verifyTrace(event("THRIFT_SERVER_INTERNAL", process, annotation("thrift.url", "com/navercorp/pinpoint/plugin/thrift/dto/EchoService/echo"))); |
| 71 | + } |
| 72 | + |
| 73 | + public static class AsyncEchoTestServerFactory { |
| 74 | + |
| 75 | + private static TProcessor getAsyncProcessor() { |
| 76 | + return new EchoService.AsyncProcessor<EchoService.AsyncIface>(new EchoService.AsyncIface() { |
| 77 | + @Override |
| 78 | + public void echo(String message, AsyncMethodCallback<String> resultHandler) throws TException { |
| 79 | + resultHandler.onComplete(message); |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + public static AsyncEchoTestServer014<TThreadedSelectorServer> threadedSelectorServer( |
| 85 | + final TestEnvironment environment) throws TTransportException { |
| 86 | + TThreadedSelectorServer server = new TThreadedSelectorServer(new TThreadedSelectorServer.Args( |
| 87 | + new TNonblockingServerSocket(environment.getPort())).processor(getAsyncProcessor()) |
| 88 | + .inputProtocolFactory(environment.getProtocolFactory()) |
| 89 | + .outputProtocolFactory(environment.getProtocolFactory())); |
| 90 | + return new AsyncEchoTestServer014<TThreadedSelectorServer>(server, environment) { |
| 91 | + @Override |
| 92 | + public EchoTestClient getSynchronousClient() throws Exception { |
| 93 | + return new SyncEchoTestClient014.ClientForNonblockingServer(environment); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public EchoTestClient getAsynchronousClient() throws Exception { |
| 98 | + return new AsyncEchoTestClient.Client(environment); |
| 99 | + } |
| 100 | + }; |
| 101 | + } |
| 102 | + |
| 103 | + public static AsyncEchoTestServer014<TNonblockingServer> nonblockingServer(final TestEnvironment environment) |
| 104 | + throws TTransportException { |
| 105 | + TNonblockingServer server = new TNonblockingServer(new TNonblockingServer.Args( |
| 106 | + new TNonblockingServerSocket(environment.getPort())).processor(getAsyncProcessor()) |
| 107 | + .inputProtocolFactory(environment.getProtocolFactory()) |
| 108 | + .outputProtocolFactory(environment.getProtocolFactory())); |
| 109 | + return new AsyncEchoTestServer014<TNonblockingServer>(server, environment) { |
| 110 | + @Override |
| 111 | + public EchoTestClient getSynchronousClient() throws Exception { |
| 112 | + return new SyncEchoTestClient014.ClientForNonblockingServer(environment); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public EchoTestClient getAsynchronousClient() throws Exception { |
| 117 | + return new AsyncEchoTestClient.Client(environment); |
| 118 | + } |
| 119 | + }; |
| 120 | + } |
| 121 | + |
| 122 | + public static AsyncEchoTestServer014<THsHaServer> halfSyncHalfAsyncServer(final TestEnvironment environment) |
| 123 | + throws TTransportException { |
| 124 | + THsHaServer server = new THsHaServer(new THsHaServer.Args(new TNonblockingServerSocket( |
| 125 | + environment.getPort())).processor(getAsyncProcessor()) |
| 126 | + .inputProtocolFactory(environment.getProtocolFactory()) |
| 127 | + .outputProtocolFactory(environment.getProtocolFactory())); |
| 128 | + return new AsyncEchoTestServer014<THsHaServer>(server, environment) { |
| 129 | + @Override |
| 130 | + public EchoTestClient getSynchronousClient() throws Exception { |
| 131 | + return new SyncEchoTestClient014.ClientForNonblockingServer(environment); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public EchoTestClient getAsynchronousClient() throws Exception { |
| 136 | + return new AsyncEchoTestClient.Client(environment); |
| 137 | + } |
| 138 | + }; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments