-
Notifications
You must be signed in to change notification settings - Fork 26.5k
merge thrift #3977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
merge thrift #3977
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d14d560
merge thrift
Leishunyu a495b9e
thrift version update
Leishunyu 5d17a95
test
Leishunyu b331582
test success
Leishunyu 849673d
test success
Leishunyu 654a529
Merge remote-tracking branch 'upstream/master' into merge-thrift
Leishunyu a04a32f
thrift and native thrift
Leishunyu 7aa6a70
Restore thrift compatibility issues
Leishunyu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <!-- | ||
| Licensed to the Apache Software Foundation (ASF) under one or more | ||
| contributor license agreements. See the NOTICE file distributed with | ||
| this work for additional information regarding copyright ownership. | ||
| The ASF licenses this file to You 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. | ||
| --> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>org.apache.dubbo</groupId> | ||
| <artifactId>dubbo-rpc</artifactId> | ||
| <version>${revision}</version> | ||
| </parent> | ||
| <artifactId>dubbo-rpc-native-thrift</artifactId> | ||
| <packaging>jar</packaging> | ||
| <name>${project.artifactId}</name> | ||
| <description>The thrift rpc module of dubbo project</description> | ||
| <properties> | ||
| <skip_maven_deploy>false</skip_maven_deploy> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.apache.dubbo</groupId> | ||
| <artifactId>dubbo-rpc-api</artifactId> | ||
| <version>${project.parent.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.thrift</groupId> | ||
| <artifactId>libthrift</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.apache.dubbo</groupId> | ||
| <artifactId>dubbo-serialization-jdk</artifactId> | ||
| <version>${revision}</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
| </project> |
179 changes: 179 additions & 0 deletions
179
...ative-thrift/src/main/java/org/apache/dubbo/rpc/protocol/nativethrift/ThriftProtocol.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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 org.apache.dubbo.rpc.protocol.nativethrift; | ||
|
|
||
|
|
||
| import org.apache.dubbo.common.URL; | ||
| import org.apache.dubbo.rpc.RpcException; | ||
| import org.apache.dubbo.rpc.protocol.AbstractProxyProtocol; | ||
| import org.apache.thrift.TException; | ||
| import org.apache.thrift.TMultiplexedProcessor; | ||
| import org.apache.thrift.TProcessor; | ||
| import org.apache.thrift.protocol.TCompactProtocol; | ||
| import org.apache.thrift.protocol.TMultiplexedProtocol; | ||
| import org.apache.thrift.protocol.TProtocol; | ||
| import org.apache.thrift.server.TServer; | ||
| import org.apache.thrift.server.TThreadedSelectorServer; | ||
| import org.apache.thrift.transport.TFramedTransport; | ||
| import org.apache.thrift.transport.TNonblockingServerSocket; | ||
| import org.apache.thrift.transport.TSocket; | ||
| import org.apache.thrift.transport.TTransport; | ||
|
|
||
| import java.lang.reflect.Constructor; | ||
| import java.net.InetSocketAddress; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * native thrift protocol | ||
| */ | ||
| public class ThriftProtocol extends AbstractProxyProtocol { | ||
|
|
||
| public static final int DEFAULT_PORT = 40880; | ||
|
|
||
| public static final String NAME = "nthrift"; | ||
| public static final String THRIFT_IFACE = "$Iface"; | ||
| public static final String THRIFT_PROCESSOR = "$Processor"; | ||
| public static final String THRIFT_CLIENT = "$Client"; | ||
|
|
||
| private static final Map<String, TServer> serverMap = new HashMap<>(); | ||
| private TMultiplexedProcessor processor = new TMultiplexedProcessor(); | ||
|
|
||
| @Override | ||
| public int getDefaultPort() { | ||
| return DEFAULT_PORT; | ||
| } | ||
|
|
||
| public ThriftProtocol() { | ||
| super(TException.class, RpcException.class); | ||
| } | ||
|
|
||
| @Override | ||
| protected <T> Runnable doExport(T impl, Class<T> type, URL url) throws RpcException { | ||
| return exportThreadedSelectorServer(impl, type, url); | ||
| } | ||
|
|
||
| @Override | ||
| protected <T> T doRefer(Class<T> type, URL url) throws RpcException { | ||
| return doReferFrameAndCompact(type, url); | ||
| } | ||
|
|
||
| public ThriftProtocol(Class<?>... exceptions) { | ||
| super(exceptions); | ||
| } | ||
|
|
||
| private <T> Runnable exportThreadedSelectorServer(T impl, Class<T> type, URL url) throws RpcException { | ||
|
|
||
| TThreadedSelectorServer.Args tArgs = null; | ||
| String typeName = type.getName(); | ||
|
|
||
| TServer tserver = null; | ||
| if (typeName.endsWith(THRIFT_IFACE)) { | ||
| String processorClsName = typeName.substring(0, typeName.indexOf(THRIFT_IFACE)) + THRIFT_PROCESSOR; | ||
| try { | ||
| Class<?> clazz = Class.forName(processorClsName); | ||
| Constructor constructor = clazz.getConstructor(type); | ||
| try { | ||
| TProcessor tprocessor = (TProcessor) constructor.newInstance(impl); | ||
| processor.registerProcessor(typeName,tprocessor); | ||
|
|
||
| tserver = serverMap.get(url.getAddress()); | ||
| if(tserver == null) { | ||
|
|
||
| /**Solve the problem of only 50 of the default number of concurrent connections*/ | ||
| TNonblockingServerSocket.NonblockingAbstractServerSocketArgs args = new TNonblockingServerSocket.NonblockingAbstractServerSocketArgs(); | ||
| /**1000 connections*/ | ||
| args.backlog(1000); | ||
| args.bindAddr(new InetSocketAddress(url.getHost(), url.getPort())); | ||
| /**timeout: 10s */ | ||
| args.clientTimeout(10000); | ||
|
|
||
| TNonblockingServerSocket transport = new TNonblockingServerSocket(args); | ||
|
|
||
| tArgs = new TThreadedSelectorServer.Args(transport); | ||
| tArgs.workerThreads(200); | ||
| tArgs.selectorThreads(4); | ||
| tArgs.acceptQueueSizePerThread(256); | ||
| tArgs.processor(processor); | ||
| tArgs.transportFactory(new TFramedTransport.Factory()); | ||
| tArgs.protocolFactory(new TCompactProtocol.Factory()); | ||
| }else{ | ||
| return null; // if server is starting, return and do nothing here | ||
| } | ||
| } catch (Exception e) { | ||
| logger.error(e.getMessage(), e); | ||
| throw new RpcException("Fail to create nativethrift server(" + url + ") : " + e.getMessage(), e); | ||
| } | ||
| } catch (Exception e) { | ||
| logger.error(e.getMessage(), e); | ||
| throw new RpcException("Fail to create nativethrift server(" + url + ") : " + e.getMessage(), e); | ||
| } | ||
| } | ||
|
|
||
| if (tserver == null && tArgs == null) { | ||
| logger.error("Fail to create nativethrift server(" + url + ") due to null args"); | ||
| throw new RpcException("Fail to create nativethrift server(" + url + ") due to null args"); | ||
| } | ||
| final TServer thriftServer = new TThreadedSelectorServer(tArgs); | ||
| serverMap.put(url.getAddress(),thriftServer); | ||
|
|
||
| new Thread(() -> { | ||
| logger.info("Start Thrift ThreadedSelectorServer"); | ||
| thriftServer.serve(); | ||
| logger.info("Thrift ThreadedSelectorServer started."); | ||
| }).start(); | ||
|
|
||
| return () -> { | ||
| try { | ||
| logger.info("Close Thrift NonblockingServer"); | ||
| thriftServer.stop(); | ||
| } catch (Throwable e) { | ||
| logger.warn(e.getMessage(), e); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private <T> T doReferFrameAndCompact(Class<T> type, URL url) throws RpcException { | ||
|
|
||
| try { | ||
| T thriftClient = null; | ||
| String typeName = type.getName(); | ||
| if (typeName.endsWith(THRIFT_IFACE)) { | ||
| String clientClsName = typeName.substring(0, typeName.indexOf(THRIFT_IFACE)) + THRIFT_CLIENT; | ||
| Class<?> clazz = Class.forName(clientClsName); | ||
| Constructor constructor = clazz.getConstructor(TProtocol.class); | ||
| try { | ||
| TSocket tSocket = new TSocket(url.getHost(), url.getPort()); | ||
| TTransport transport = new TFramedTransport(tSocket); | ||
| TProtocol tprotocol = new TCompactProtocol(transport); | ||
| TMultiplexedProtocol protocol = new TMultiplexedProtocol(tprotocol,typeName); | ||
| thriftClient = (T) constructor.newInstance(protocol); | ||
| transport.open(); | ||
| logger.info("nativethrift client opened for service(" + url + ")"); | ||
| } catch (Exception e) { | ||
| logger.error(e.getMessage(), e); | ||
| throw new RpcException("Fail to create remote client:" + e.getMessage(), e); | ||
| } | ||
| } | ||
| return thriftClient; | ||
| } catch (Exception e) { | ||
| logger.error(e.getMessage(), e); | ||
| throw new RpcException("Fail to create remote client for service(" + url + "): " + e.getMessage(), e); | ||
| } | ||
| } | ||
|
|
||
| } |
1 change: 1 addition & 0 deletions
1
...pc-native-thrift/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.Protocol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| nthrift=org.apache.dubbo.rpc.protocol.nativethrift.ThriftProtocol | ||
|
Leishunyu marked this conversation as resolved.
Outdated
|
||
17 changes: 17 additions & 0 deletions
17
dubbo-rpc/dubbo-rpc-native-thrift/src/test/idls/DemoService.thrift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| namespace java org.apache.dubbo.rpc.protocol.nativethrift | ||
| namespace go demo | ||
| /*Demo service define file,can be generated to inteface files*/ | ||
| /*Here test the 7 kind of data type*/ | ||
| service DemoService { | ||
| string sayHello(1:required string name); | ||
|
|
||
| bool hasName( 1:required bool hasName); | ||
|
|
||
| string sayHelloTimes(1:required string name, 2:required i32 times); | ||
|
|
||
| void timeOut(1:required i32 millis); | ||
|
|
||
| string customException(); | ||
|
|
||
| string context(1:required string name); | ||
| } |
6 changes: 6 additions & 0 deletions
6
dubbo-rpc/dubbo-rpc-native-thrift/src/test/idls/UserService.thrift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| namespace java org.apache.dubbo.rpc.protocol.nativethrift | ||
| namespace go demo | ||
|
|
||
| service UserService { | ||
| string find(1:required i32 id); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.