Skip to content

Commit c6eb9cf

Browse files
committed
[#9025] Update thrift plugin for 0.14 or later
1 parent 27790dc commit c6eb9cf

28 files changed

+1062
-77
lines changed

plugins-it/thrift-it/pom.xml

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
<dependency>
3838
<groupId>org.apache.thrift</groupId>
3939
<artifactId>libthrift</artifactId>
40-
<version>0.12.0</version>
4140
<scope>test</scope>
4241
</dependency>
4342
<dependency>

plugins-it/thrift-it/src/test/java/com/navercorp/pinpoint/plugin/thrift/common/client/AsyncEchoTestClient.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class AsyncEchoTestClient implements EchoTestClient {
5252
private final EchoService.AsyncClient asyncClient;
5353
private final TAsyncClientManager asyncClientManager = new TAsyncClientManager();
5454

55-
private AsyncEchoTestClient(TestEnvironment environment) throws IOException {
55+
private AsyncEchoTestClient(TestEnvironment environment) throws Exception {
5656
this.environment = environment;
5757
this.transport = new TNonblockingSocket(this.environment.getServerIp(), this.environment.getPort());
5858
this.asyncClient = new EchoService.AsyncClient(this.environment.getProtocolFactory(), this.asyncClientManager, this.transport);
@@ -168,7 +168,7 @@ public void onError(Exception exception) {
168168
}
169169

170170
public static class Client extends AsyncEchoTestClient {
171-
public Client(TestEnvironment environment) throws IOException {
171+
public Client(TestEnvironment environment) throws Exception {
172172
super(environment);
173173
}
174174
}

plugins-it/thrift-it/src/test/java/com/navercorp/pinpoint/plugin/thrift/common/client/SyncEchoTestClient.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.apache.thrift.TException;
2828
import org.apache.thrift.TServiceClient;
2929
import org.apache.thrift.protocol.TProtocol;
30-
import org.apache.thrift.transport.TFramedTransport;
3130
import org.apache.thrift.transport.TSocket;
3231
import org.apache.thrift.transport.TTransport;
3332
import org.apache.thrift.transport.TTransportException;
@@ -106,8 +105,8 @@ public Client(TestEnvironment environment) throws TTransportException {
106105
}
107106

108107
public static class ClientForNonblockingServer extends SyncEchoTestClient {
109-
public ClientForNonblockingServer(TestEnvironment environment) throws TTransportException {
110-
super(environment, new TFramedTransport(new TSocket(environment.getServerIp(), environment.getPort())));
108+
public ClientForNonblockingServer(TestEnvironment environment) throws Exception {
109+
super(environment, TTransportInstanceCreator.create(SyncEchoTestClient014.class.getClassLoader(), "org.apache.thrift.transport.TFramedTransport", new TSocket(environment.getServerIp(), environment.getPort())));
111110
}
112111
}
113112
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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.client;
18+
19+
import com.navercorp.pinpoint.bootstrap.plugin.test.Expectations;
20+
import com.navercorp.pinpoint.bootstrap.plugin.test.ExpectedAnnotation;
21+
import com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier;
22+
import com.navercorp.pinpoint.bootstrap.plugin.util.SocketAddressUtils;
23+
import com.navercorp.pinpoint.common.plugin.util.HostAndPort;
24+
import com.navercorp.pinpoint.plugin.thrift.common.TestEnvironment;
25+
import com.navercorp.pinpoint.plugin.thrift.dto.EchoService;
26+
import org.apache.thrift.TBase;
27+
import org.apache.thrift.TException;
28+
import org.apache.thrift.TServiceClient;
29+
import org.apache.thrift.protocol.TProtocol;
30+
import org.apache.thrift.transport.TSocket;
31+
import org.apache.thrift.transport.TTransport;
32+
import org.apache.thrift.transport.TTransportException;
33+
34+
import java.lang.reflect.Method;
35+
import java.net.InetSocketAddress;
36+
37+
import static com.navercorp.pinpoint.bootstrap.plugin.test.Expectations.event;
38+
39+
/**
40+
* @author HyunGil Jeong
41+
*/
42+
public abstract class SyncEchoTestClient014 implements EchoTestClient {
43+
44+
private final TestEnvironment environment;
45+
private final TTransport transport;
46+
47+
private SyncEchoTestClient014(TestEnvironment environment, TTransport transport) throws TTransportException {
48+
this.environment = environment;
49+
this.transport = transport;
50+
this.transport.open();
51+
}
52+
53+
@Override
54+
public final String echo(String message) throws TException {
55+
TProtocol protocol = this.environment.getProtocolFactory().getProtocol(transport);
56+
EchoService.Client client = new EchoService.Client(protocol);
57+
return client.echo(message);
58+
}
59+
60+
@Override
61+
public void verifyTraces(PluginTestVerifier verifier, String expectedMessage) throws Exception {
62+
// refer to TServiceClientSendBaseInterceptor.getRemoteAddress(...)
63+
final InetSocketAddress socketAddress = this.environment.getServerAddress();
64+
final String hostName = SocketAddressUtils.getHostNameFirst(socketAddress);
65+
final String remoteAddress = HostAndPort.toHostAndPortString(hostName, socketAddress.getPort());
66+
67+
// SpanEvent - TServiceClient.sendBase
68+
Method sendBase = TServiceClient.class.getDeclaredMethod("sendBase", String.class, TBase.class);
69+
70+
ExpectedAnnotation thriftUrl = Expectations.annotation("thrift.url",
71+
remoteAddress + "/com/navercorp/pinpoint/plugin/thrift/dto/EchoService/echo");
72+
ExpectedAnnotation thriftArgs = Expectations.annotation("thrift.args",
73+
"echo_args(message:" + expectedMessage + ")");
74+
75+
// SpanEvent - TServiceClient.receiveBase
76+
Method receiveBase = TServiceClient.class.getDeclaredMethod("receiveBase", TBase.class, String.class);
77+
ExpectedAnnotation thriftResult = Expectations.annotation("thrift.result", "echo_result(success:"
78+
+ expectedMessage + ")");
79+
80+
verifier.verifyDiscreteTrace(event("THRIFT_CLIENT", // ServiceType
81+
sendBase, // Method
82+
null, // rpc
83+
null, // endPoint
84+
remoteAddress, // destinationId
85+
thriftUrl, // Annotation("thrift.url")
86+
thriftArgs), // Annotation("thrift.args")
87+
event("THRIFT_CLIENT_INTERNAL", // ServiceType
88+
receiveBase, // Method
89+
thriftResult // Annotation("thrift.result")
90+
));
91+
}
92+
93+
@Override
94+
public void close() {
95+
if (this.transport.isOpen()) {
96+
this.transport.close();
97+
}
98+
}
99+
100+
public static class Client extends SyncEchoTestClient014 {
101+
public Client(TestEnvironment environment) throws TTransportException {
102+
super(environment, new TSocket(environment.getServerIp(), environment.getPort()));
103+
}
104+
}
105+
106+
public static class ClientForNonblockingServer extends SyncEchoTestClient014 {
107+
public ClientForNonblockingServer(TestEnvironment environment) throws Exception {
108+
super(environment, TTransportInstanceCreator.create(SyncEchoTestClient014.class.getClassLoader(), "org.apache.thrift.transport.layered.TFramedTransport", new TSocket(environment.getServerIp(), environment.getPort())));
109+
}
110+
}
111+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.client;
18+
19+
import org.apache.thrift.transport.TTransport;
20+
21+
import java.lang.reflect.Constructor;
22+
23+
public class TTransportInstanceCreator {
24+
25+
public static TTransport create(ClassLoader classLoader, String className, TTransport transport) throws Exception {
26+
Class<?> clazz = classLoader.loadClass(className);
27+
Constructor<?> constructor = clazz.getConstructor(TTransport.class);
28+
return (TTransport) constructor.newInstance(transport);
29+
}
30+
}

plugins-it/thrift-it/src/test/java/com/navercorp/pinpoint/plugin/thrift/common/server/AsyncEchoTestServer.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ public static AsyncEchoTestServer<TThreadedSelectorServer> threadedSelectorServe
8989
.outputProtocolFactory(environment.getProtocolFactory()));
9090
return new AsyncEchoTestServer<TThreadedSelectorServer>(server, environment) {
9191
@Override
92-
public SyncEchoTestClient getSynchronousClient() throws TTransportException {
92+
public SyncEchoTestClient getSynchronousClient() throws Exception {
9393
return new SyncEchoTestClient.ClientForNonblockingServer(environment);
9494
}
9595

9696
@Override
97-
public AsyncEchoTestClient getAsynchronousClient() throws IOException {
97+
public AsyncEchoTestClient getAsynchronousClient() throws Exception {
9898
return new AsyncEchoTestClient.Client(environment);
9999
}
100100
};
@@ -108,12 +108,12 @@ public static AsyncEchoTestServer<TNonblockingServer> nonblockingServer(final Te
108108
.outputProtocolFactory(environment.getProtocolFactory()));
109109
return new AsyncEchoTestServer<TNonblockingServer>(server, environment) {
110110
@Override
111-
public SyncEchoTestClient getSynchronousClient() throws TTransportException {
111+
public SyncEchoTestClient getSynchronousClient() throws Exception {
112112
return new SyncEchoTestClient.ClientForNonblockingServer(environment);
113113
}
114114

115115
@Override
116-
public AsyncEchoTestClient getAsynchronousClient() throws IOException {
116+
public AsyncEchoTestClient getAsynchronousClient() throws Exception {
117117
return new AsyncEchoTestClient.Client(environment);
118118
}
119119
};
@@ -127,12 +127,12 @@ public static AsyncEchoTestServer<THsHaServer> halfSyncHalfAsyncServer(final Tes
127127
.outputProtocolFactory(environment.getProtocolFactory()));
128128
return new AsyncEchoTestServer<THsHaServer>(server, environment) {
129129
@Override
130-
public SyncEchoTestClient getSynchronousClient() throws TTransportException {
130+
public SyncEchoTestClient getSynchronousClient() throws Exception {
131131
return new SyncEchoTestClient.ClientForNonblockingServer(environment);
132132
}
133133

134134
@Override
135-
public AsyncEchoTestClient getAsynchronousClient() throws IOException {
135+
public AsyncEchoTestClient getAsynchronousClient() throws Exception {
136136
return new AsyncEchoTestClient.Client(environment);
137137
}
138138
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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

Comments
 (0)