Skip to content
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

Grpc support #3

Merged
merged 53 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
e96ea17
grpc protocol
PleaseGiveMeTheCoke Aug 14, 2024
2e9d232
grpc protocol
PleaseGiveMeTheCoke Aug 16, 2024
be0448a
grpc protocol
PleaseGiveMeTheCoke Aug 16, 2024
6cbbac0
grpc protocol
PleaseGiveMeTheCoke Aug 16, 2024
bea5592
grpc protocol
PleaseGiveMeTheCoke Aug 16, 2024
c970d20
Merge branch '2.x' into grpc_support
PleaseGiveMeTheCoke Aug 22, 2024
899f490
delete no need change
PleaseGiveMeTheCoke Aug 22, 2024
44675d9
Merge remote-tracking branch 'origin/grpc_support' into grpc_support
PleaseGiveMeTheCoke Aug 22, 2024
5bf5038
cr
PleaseGiveMeTheCoke Aug 22, 2024
548fa42
1. mock test
PleaseGiveMeTheCoke Aug 25, 2024
1e9a0c2
cr
PleaseGiveMeTheCoke Aug 25, 2024
c4e0bc4
cr
PleaseGiveMeTheCoke Aug 25, 2024
24c0d23
Delete seata-test-grpc/src/main/java/org/apache/seata/grpc/generated/…
PleaseGiveMeTheCoke Aug 25, 2024
4e2fe65
Delete seata-test-grpc/src/main/java/org/apache/seata/grpc/generated/…
PleaseGiveMeTheCoke Aug 25, 2024
022b9cb
Delete seata-test-grpc/src/main/java/org/apache/seata/grpc/generated/…
PleaseGiveMeTheCoke Aug 25, 2024
a7762a2
Delete serializer/seata-serializer-protobuf/src/main/java/org/apache/…
PleaseGiveMeTheCoke Aug 25, 2024
0ba622c
Delete seata-test-grpc/src/main/java/org/apache/seata/grpc/generated/…
PleaseGiveMeTheCoke Aug 25, 2024
397b90c
cr
PleaseGiveMeTheCoke Aug 25, 2024
4d36883
Merge remote-tracking branch 'origin/grpc_support' into grpc_support
PleaseGiveMeTheCoke Aug 25, 2024
f71391b
Merge branch '2.x' into grpc_support
funky-eyes Aug 28, 2024
05552c0
cr
PleaseGiveMeTheCoke Aug 31, 2024
6dbefc3
Merge remote-tracking branch 'origin/grpc_support' into grpc_support
PleaseGiveMeTheCoke Aug 31, 2024
c7da0ff
cr
PleaseGiveMeTheCoke Aug 31, 2024
6b04158
Merge branch '2.x' into grpc_support
PleaseGiveMeTheCoke Aug 31, 2024
b93add1
cr
PleaseGiveMeTheCoke Aug 31, 2024
e1b7409
Merge remote-tracking branch 'origin/grpc_support' into grpc_support
PleaseGiveMeTheCoke Aug 31, 2024
e45049c
cr
PleaseGiveMeTheCoke Aug 31, 2024
0621acd
cr
PleaseGiveMeTheCoke Aug 31, 2024
06dfe34
cr
PleaseGiveMeTheCoke Aug 31, 2024
c701a79
cr
PleaseGiveMeTheCoke Aug 31, 2024
ad8bf75
cr
PleaseGiveMeTheCoke Aug 31, 2024
706eb0b
cr
PleaseGiveMeTheCoke Aug 31, 2024
ebbf0f5
cr
PleaseGiveMeTheCoke Aug 31, 2024
29da916
cr
PleaseGiveMeTheCoke Aug 31, 2024
8a51486
cr
PleaseGiveMeTheCoke Aug 31, 2024
725d4b2
cr
PleaseGiveMeTheCoke Aug 31, 2024
c35f31d
cr
PleaseGiveMeTheCoke Aug 31, 2024
1e41528
cr
PleaseGiveMeTheCoke Aug 31, 2024
894f24e
Merge branch '2.x' into grpc_support
funky-eyes Sep 2, 2024
c0f4f9a
Merge branch '2.x' into grpc_support
funky-eyes Sep 21, 2024
3979c9f
optimize: update grpc and protocol version
funky-eyes Sep 22, 2024
3ab99f1
Merge branch '2.x' into grpc_support
funky-eyes Sep 22, 2024
bc88621
Before casting, check if the value is within the range of the target…
funky-eyes Sep 22, 2024
bf7604a
Before casting, check if the value is within the range of the target…
funky-eyes Sep 22, 2024
2797768
Remove duplicate code
funky-eyes Sep 22, 2024
87a2354
import grpc-core
funky-eyes Sep 22, 2024
f1f4e42
import grpc-core
funky-eyes Sep 22, 2024
0a5b999
bugfix
funky-eyes Sep 22, 2024
4af6b69
bugfix
funky-eyes Sep 22, 2024
e0dc2f9
Merge branch '2.x' into grpc_support
funky-eyes Sep 23, 2024
bc77190
bugfix: context release
funky-eyes Sep 23, 2024
1c1a42d
bugfix: release byteBuf
funky-eyes Sep 23, 2024
844c1d8
opt
funky-eyes Sep 23, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,8 @@ public interface ConfigurationKeys {
@Deprecated
String ENABLE_CLIENT_BATCH_SEND_REQUEST = TRANSPORT_PREFIX + "enableClientBatchSendRequest";

String TRANSPORT_PROTOCOL = TRANSPORT_PREFIX + "protocol";

/**
* The constant ENABLE_TM_CLIENT_BATCH_SEND_REQUEST
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public interface DefaultValues {
String DEFAULT_BOSS_THREAD_PREFIX = "NettyBoss";
String DEFAULT_NIO_WORKER_THREAD_PREFIX = "NettyServerNIOWorker";
String DEFAULT_EXECUTOR_THREAD_PREFIX = "NettyServerBizHandler";
String DEFAULT_PROTOCOL = "seata";

boolean DEFAULT_TRANSPORT_HEARTBEAT = true;
boolean DEFAULT_TRANSACTION_UNDO_DATA_VALIDATION = true;
Expand Down
21 changes: 21 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@
<artifactId>fastjson</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
</dependency>
</dependencies>

<build>
Expand All @@ -90,6 +94,23 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<configuration>
<protoSourceRoot>${project.basedir}/src/main/resources/protobuf/org/apache/seata/protocol/transcation/</protoSourceRoot>
<protocArtifact>
com.google.protobuf:protoc:3.25.4:exe:${os.detected.classifier}
</protocArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
39 changes: 39 additions & 0 deletions core/src/main/java/org/apache/seata/core/protocol/Protocol.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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.seata.core.protocol;

/**
* seata transport protocol
*/
public enum Protocol {

/**
* grpc
*/
GPRC("grpc"),

/**
* seata
*/
SEATA("seata");

public final String value;

Protocol(String value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.seata.core.protocol.detector;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http2.Http2FrameCodecBuilder;
import io.netty.handler.codec.http2.Http2MultiplexHandler;
import io.netty.handler.codec.http2.Http2StreamChannel;
import io.netty.util.CharsetUtil;
import org.apache.seata.core.rpc.netty.grpc.GrpcDecoder;
import org.apache.seata.core.rpc.netty.grpc.GrpcEncoder;

public class Http2Detector implements ProtocolDetector {
private static final byte[] HTTP2_PREFIX_BYTES = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n".getBytes(CharsetUtil.UTF_8);
private ChannelHandler[] serverHandlers;

public Http2Detector(ChannelHandler[] serverHandlers) {
this.serverHandlers = serverHandlers;
}

@Override
public boolean detect(ByteBuf in) {
if (in.readableBytes() < HTTP2_PREFIX_BYTES.length) {
return false;
}
for (int i = 0; i < HTTP2_PREFIX_BYTES.length; i++) {
if (in.getByte(i) != HTTP2_PREFIX_BYTES[i]) {
return false;
}
}
return true;
}

@Override
public ChannelHandler[] getHandlers() {
return new ChannelHandler[]{
Http2FrameCodecBuilder.forServer().build(),
new Http2MultiplexHandler(new ChannelInitializer<Http2StreamChannel>() {
@Override
protected void initChannel(Http2StreamChannel ch) {
final ChannelPipeline p = ch.pipeline();
p.addLast(new GrpcDecoder());
p.addLast(new GrpcEncoder());
p.addLast(serverHandlers);
}
})
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.seata.core.protocol.detector;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;

public interface ProtocolDetector {
boolean detect(ByteBuf in);

ChannelHandler[] getHandlers();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.seata.core.protocol.detector;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandler;
import org.apache.seata.core.rpc.netty.MultiProtocolDecoder;

public class SeataDetector implements ProtocolDetector {
private static final byte[] MAGIC_CODE_BYTES = {(byte) 0xda, (byte) 0xda};
private ChannelHandler[] serverHandlers;

public SeataDetector(ChannelHandler[] serverHandlers) {
this.serverHandlers = serverHandlers;
}

@Override
public boolean detect(ByteBuf in) {
if (in.readableBytes() < MAGIC_CODE_BYTES.length) {
return false;
}
for (int i = 0; i < MAGIC_CODE_BYTES.length; i++) {
if (in.getByte(i) != MAGIC_CODE_BYTES[i]) {
return false;
}
}
return true;
}

@Override
public ChannelHandler[] getHandlers() {
MultiProtocolDecoder multiProtocolDecoder = new MultiProtocolDecoder(serverHandlers);

return new ChannelHandler[]{multiProtocolDecoder};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
Expand All @@ -28,13 +31,19 @@
import io.netty.channel.epoll.EpollMode;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http2.Http2FrameCodecBuilder;
import io.netty.handler.codec.http2.Http2MultiplexHandler;
import io.netty.handler.codec.http2.Http2StreamChannelBootstrap;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;
import io.netty.util.internal.PlatformDependent;
import org.apache.seata.common.exception.FrameworkException;
import org.apache.seata.common.thread.NamedThreadFactory;
import org.apache.seata.core.protocol.Protocol;
import org.apache.seata.core.rpc.RemotingBootstrap;
import org.apache.seata.core.rpc.netty.grpc.GrpcDecoder;
import org.apache.seata.core.rpc.netty.grpc.GrpcEncoder;
import org.apache.seata.core.rpc.netty.v1.ProtocolDecoderV1;
import org.apache.seata.core.rpc.netty.v1.ProtocolEncoderV1;
import org.slf4j.Logger;
Expand Down Expand Up @@ -130,12 +139,19 @@ public void start() {
@Override
public void initChannel(SocketChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
pipeline
.addLast(new IdleStateHandler(nettyClientConfig.getChannelMaxReadIdleSeconds(),
pipeline.addLast(new IdleStateHandler(nettyClientConfig.getChannelMaxReadIdleSeconds(),
nettyClientConfig.getChannelMaxWriteIdleSeconds(),
nettyClientConfig.getChannelMaxAllIdleSeconds()))
.addLast(new ProtocolDecoderV1())
.addLast(new ProtocolEncoderV1());
nettyClientConfig.getChannelMaxAllIdleSeconds()));
if (nettyClientConfig.getProtocol().equals(Protocol.GPRC.value)) {
pipeline.addLast(Http2FrameCodecBuilder.forClient().build())
.addLast(new Http2MultiplexHandler(new ChannelDuplexHandler()))
.addLast(new GrpcDecoder())
.addLast(new GrpcEncoder());
} else {
pipeline.addLast(new ProtocolDecoderV1())
.addLast(new ProtocolEncoderV1());
}

if (channelHandlers != null) {
addChannelPipelineLast(ch, channelHandlers);
}
Expand Down Expand Up @@ -177,9 +193,28 @@ public Channel getNewChannel(InetSocketAddress address) {
} else {
channel = f.channel();
}

// TODO tmp only for grpc
if (nettyClientConfig.getProtocol().equals(Protocol.GPRC.value)) {
Http2StreamChannelBootstrap bootstrap = new Http2StreamChannelBootstrap(channel);
bootstrap.handler(new ChannelInboundHandlerAdapter() {
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
Channel channel = ctx.channel();
channel.pipeline().addLast(new GrpcDecoder());
channel.pipeline().addLast(new GrpcEncoder());
if (channelHandlers != null) {
addChannelPipelineLast(channel, channelHandlers);
}
}
});
channel = bootstrap.open().get();
}

} catch (Exception e) {
throw new FrameworkException(e, "can not connect to services-server.");
}

return channel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.seata.core.rpc.TransportServerType;

import static org.apache.seata.common.DefaultValues.DEFAULT_ENABLE_CLIENT_BATCH_SEND_REQUEST;
import static org.apache.seata.common.DefaultValues.DEFAULT_PROTOCOL;
import static org.apache.seata.common.DefaultValues.DEFAULT_RPC_RM_REQUEST_TIMEOUT;
import static org.apache.seata.common.DefaultValues.DEFAULT_RPC_TM_REQUEST_TIMEOUT;
import static org.apache.seata.common.DefaultValues.DEFAULT_SELECTOR_THREAD_PREFIX;
Expand Down Expand Up @@ -451,6 +452,10 @@ public String getRmDispatchThreadPrefix() {
return RPC_DISPATCH_THREAD_PREFIX + "_" + NettyPoolKey.TransactionRole.RMROLE.name();
}

public String getProtocol() {
return CONFIG.getConfig(org.apache.seata.common.ConfigurationKeys.TRANSPORT_PROTOCOL, DEFAULT_PROTOCOL);
}

@Deprecated
public static boolean isEnableClientBatchSendRequest() {
return ENABLE_CLIENT_BATCH_SEND_REQUEST;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ protected void setChannelHandlers(final ChannelHandler... handlers) {
}
}

protected ChannelHandler[] getChannelHandlers() {
return channelHandlers;
}

/**
* Add channel pipeline last.
*
Expand Down Expand Up @@ -158,10 +162,8 @@ public void start() {
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
MultiProtocolDecoder multiProtocolDecoder = new MultiProtocolDecoder(channelHandlers);
ch.pipeline()
.addLast(new IdleStateHandler(nettyServerConfig.getChannelMaxReadIdleSeconds(), 0, 0))
.addLast(multiProtocolDecoder);
ch.pipeline().addLast(new IdleStateHandler(nettyServerConfig.getChannelMaxReadIdleSeconds(), 0, 0))
.addLast(new ProtocolDetectHandler(NettyServerBootstrap.this));
}
});

Expand Down
Loading