|
|
@ -1,31 +1,13 @@ |
|
|
|
package org.apache.dolphinscheduler.remote.rpc.remote; |
|
|
|
package org.apache.dolphinscheduler.remote.rpc.remote; |
|
|
|
|
|
|
|
|
|
|
|
import io.netty.bootstrap.Bootstrap; |
|
|
|
|
|
|
|
import io.netty.buffer.Unpooled; |
|
|
|
|
|
|
|
import io.netty.channel.Channel; |
|
|
|
|
|
|
|
import io.netty.channel.ChannelFuture; |
|
|
|
|
|
|
|
import io.netty.channel.ChannelInitializer; |
|
|
|
|
|
|
|
import io.netty.channel.ChannelOption; |
|
|
|
|
|
|
|
import io.netty.channel.EventLoopGroup; |
|
|
|
|
|
|
|
import io.netty.channel.epoll.EpollEventLoopGroup; |
|
|
|
|
|
|
|
import io.netty.channel.nio.NioEventLoopGroup; |
|
|
|
|
|
|
|
import io.netty.channel.socket.SocketChannel; |
|
|
|
|
|
|
|
import io.netty.handler.logging.LogLevel; |
|
|
|
|
|
|
|
import io.netty.handler.logging.LoggingHandler; |
|
|
|
|
|
|
|
import io.netty.handler.timeout.IdleStateHandler; |
|
|
|
|
|
|
|
import io.netty.util.CharsetUtil; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.remote.decoder.NettyDecoder; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.remote.config.NettyClientConfig; |
|
|
|
import org.apache.dolphinscheduler.remote.config.NettyClientConfig; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.remote.decoder.NettyDecoder; |
|
|
|
import org.apache.dolphinscheduler.remote.decoder.NettyEncoder; |
|
|
|
import org.apache.dolphinscheduler.remote.decoder.NettyEncoder; |
|
|
|
import org.apache.dolphinscheduler.remote.future.ResponseFuture; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.remote.rpc.client.RpcRequestCache; |
|
|
|
import org.apache.dolphinscheduler.remote.rpc.client.RpcRequestCache; |
|
|
|
import org.apache.dolphinscheduler.remote.rpc.client.RpcRequestTable; |
|
|
|
import org.apache.dolphinscheduler.remote.rpc.client.RpcRequestTable; |
|
|
|
import org.apache.dolphinscheduler.remote.rpc.common.RpcRequest; |
|
|
|
import org.apache.dolphinscheduler.remote.rpc.common.RpcRequest; |
|
|
|
import org.apache.dolphinscheduler.remote.rpc.common.RpcResponse; |
|
|
|
import org.apache.dolphinscheduler.remote.rpc.common.RpcResponse; |
|
|
|
import org.apache.dolphinscheduler.remote.rpc.future.RpcFuture; |
|
|
|
import org.apache.dolphinscheduler.remote.rpc.future.RpcFuture; |
|
|
|
import org.apache.dolphinscheduler.remote.serialize.ProtoStuffUtils; |
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.remote.utils.Constants; |
|
|
|
import org.apache.dolphinscheduler.remote.utils.Constants; |
|
|
|
import org.apache.dolphinscheduler.remote.utils.Host; |
|
|
|
import org.apache.dolphinscheduler.remote.utils.Host; |
|
|
|
import org.apache.dolphinscheduler.remote.utils.NettyUtils; |
|
|
|
import org.apache.dolphinscheduler.remote.utils.NettyUtils; |
|
|
@ -41,11 +23,33 @@ import java.util.concurrent.atomic.AtomicInteger; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import io.netty.bootstrap.Bootstrap; |
|
|
|
|
|
|
|
import io.netty.channel.Channel; |
|
|
|
|
|
|
|
import io.netty.channel.ChannelFuture; |
|
|
|
|
|
|
|
import io.netty.channel.ChannelInitializer; |
|
|
|
|
|
|
|
import io.netty.channel.ChannelOption; |
|
|
|
|
|
|
|
import io.netty.channel.EventLoopGroup; |
|
|
|
|
|
|
|
import io.netty.channel.epoll.EpollEventLoopGroup; |
|
|
|
|
|
|
|
import io.netty.channel.nio.NioEventLoopGroup; |
|
|
|
|
|
|
|
import io.netty.channel.socket.SocketChannel; |
|
|
|
|
|
|
|
import io.netty.handler.logging.LogLevel; |
|
|
|
|
|
|
|
import io.netty.handler.logging.LoggingHandler; |
|
|
|
|
|
|
|
import io.netty.handler.timeout.IdleStateHandler; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* NettyClient |
|
|
|
* NettyClient |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public class NettyClient { |
|
|
|
public class NettyClient { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static NettyClient getInstance() { |
|
|
|
|
|
|
|
return NettyClient.NettyClientInner.INSTANCE; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class NettyClientInner { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final NettyClient INSTANCE = new NettyClient(new NettyClientConfig()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(NettyClient.class); |
|
|
|
private final Logger logger = LoggerFactory.getLogger(NettyClient.class); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -160,38 +164,43 @@ public class NettyClient { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void initChannel(SocketChannel ch) { |
|
|
|
public void initChannel(SocketChannel ch) { |
|
|
|
ch.pipeline() |
|
|
|
ch.pipeline() |
|
|
|
.addLast(new NettyEncoder(RpcRequest.class)) //OUT - 1
|
|
|
|
.addLast(new NettyEncoder(RpcRequest.class)) |
|
|
|
.addLast(new NettyDecoder(RpcResponse.class)) |
|
|
|
.addLast(new NettyDecoder(RpcResponse.class)) |
|
|
|
.addLast("client-idle-handler", new IdleStateHandler(Constants.NETTY_CLIENT_HEART_BEAT_TIME, 0, 0, TimeUnit.MILLISECONDS)) |
|
|
|
.addLast("client-idle-handler", new IdleStateHandler(Constants.NETTY_CLIENT_HEART_BEAT_TIME, 0, 0, TimeUnit.MILLISECONDS)) |
|
|
|
|
|
|
|
|
|
|
|
.addLast(new NettyClientHandler()); |
|
|
|
.addLast(new NettyClientHandler()); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
isStarted.compareAndSet(false, true); |
|
|
|
isStarted.compareAndSet(false, true); |
|
|
|
System.out.println("netty client start"); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public Object sendMsg(Host host, RpcRequest request, Boolean async) { |
|
|
|
public RpcResponse sendMsg(Host host, RpcRequest request, Boolean async) { |
|
|
|
|
|
|
|
|
|
|
|
System.out.println("这个不是异步"+async); |
|
|
|
|
|
|
|
Channel channel = getChannel(host); |
|
|
|
Channel channel = getChannel(host); |
|
|
|
assert channel != null; |
|
|
|
assert channel != null; |
|
|
|
RpcRequestCache rpcRequestCache = new RpcRequestCache(); |
|
|
|
RpcRequestCache rpcRequestCache = new RpcRequestCache(); |
|
|
|
rpcRequestCache.setServiceName(request.getClassName() + request.getMethodName()); |
|
|
|
rpcRequestCache.setServiceName(request.getClassName() + request.getMethodName()); |
|
|
|
RpcFuture future = new RpcFuture(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RpcFuture future = null; |
|
|
|
|
|
|
|
if (!async) { |
|
|
|
|
|
|
|
future = new RpcFuture(); |
|
|
|
rpcRequestCache.setRpcFuture(future); |
|
|
|
rpcRequestCache.setRpcFuture(future); |
|
|
|
|
|
|
|
} |
|
|
|
RpcRequestTable.put(request.getRequestId(), rpcRequestCache); |
|
|
|
RpcRequestTable.put(request.getRequestId(), rpcRequestCache); |
|
|
|
channel.writeAndFlush(request); |
|
|
|
channel.writeAndFlush(request); |
|
|
|
|
|
|
|
|
|
|
|
Object result = null; |
|
|
|
RpcResponse result = null; |
|
|
|
if (async) { |
|
|
|
if (async) { |
|
|
|
return true; |
|
|
|
result=new RpcResponse(); |
|
|
|
|
|
|
|
result.setStatus((byte)0); |
|
|
|
|
|
|
|
result.setResult(true); |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
try { |
|
|
|
try { |
|
|
|
result = future.get(); |
|
|
|
result = future.get(); |
|
|
|
} catch (InterruptedException | ExecutionException e) { |
|
|
|
} catch (InterruptedException | ExecutionException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
logger.error("send msg error",e); |
|
|
|
} |
|
|
|
} |
|
|
|
return result; |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|