|
|
|
@ -17,14 +17,6 @@
|
|
|
|
|
|
|
|
|
|
package org.apache.dolphinscheduler.remote; |
|
|
|
|
|
|
|
|
|
import io.netty.bootstrap.ServerBootstrap; |
|
|
|
|
import io.netty.channel.ChannelFuture; |
|
|
|
|
import io.netty.channel.ChannelInitializer; |
|
|
|
|
import io.netty.channel.ChannelOption; |
|
|
|
|
import io.netty.channel.ChannelPipeline; |
|
|
|
|
import io.netty.channel.nio.NioEventLoopGroup; |
|
|
|
|
import io.netty.channel.socket.nio.NioServerSocketChannel; |
|
|
|
|
import io.netty.channel.socket.nio.NioSocketChannel; |
|
|
|
|
import org.apache.dolphinscheduler.remote.codec.NettyDecoder; |
|
|
|
|
import org.apache.dolphinscheduler.remote.codec.NettyEncoder; |
|
|
|
|
import org.apache.dolphinscheduler.remote.command.CommandType; |
|
|
|
@ -32,8 +24,7 @@ import org.apache.dolphinscheduler.remote.config.NettyServerConfig;
|
|
|
|
|
import org.apache.dolphinscheduler.remote.handler.NettyServerHandler; |
|
|
|
|
import org.apache.dolphinscheduler.remote.processor.NettyRequestProcessor; |
|
|
|
|
import org.apache.dolphinscheduler.remote.utils.Constants; |
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
import org.apache.dolphinscheduler.remote.utils.NettyUtils; |
|
|
|
|
|
|
|
|
|
import java.util.concurrent.ExecutorService; |
|
|
|
|
import java.util.concurrent.Executors; |
|
|
|
@ -41,6 +32,19 @@ import java.util.concurrent.ThreadFactory;
|
|
|
|
|
import java.util.concurrent.atomic.AtomicBoolean; |
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger; |
|
|
|
|
|
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
import io.netty.bootstrap.ServerBootstrap; |
|
|
|
|
import io.netty.channel.ChannelFuture; |
|
|
|
|
import io.netty.channel.ChannelInitializer; |
|
|
|
|
import io.netty.channel.ChannelOption; |
|
|
|
|
import io.netty.channel.ChannelPipeline; |
|
|
|
|
import io.netty.channel.EventLoopGroup; |
|
|
|
|
import io.netty.channel.epoll.EpollEventLoopGroup; |
|
|
|
|
import io.netty.channel.nio.NioEventLoopGroup; |
|
|
|
|
import io.netty.channel.socket.SocketChannel; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* remoting netty server |
|
|
|
|
*/ |
|
|
|
@ -66,12 +70,12 @@ public class NettyRemotingServer {
|
|
|
|
|
/** |
|
|
|
|
* boss group |
|
|
|
|
*/ |
|
|
|
|
private final NioEventLoopGroup bossGroup; |
|
|
|
|
private final EventLoopGroup bossGroup; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* worker group |
|
|
|
|
*/ |
|
|
|
|
private final NioEventLoopGroup workGroup; |
|
|
|
|
private final EventLoopGroup workGroup; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* server config |
|
|
|
@ -95,7 +99,25 @@ public class NettyRemotingServer {
|
|
|
|
|
*/ |
|
|
|
|
public NettyRemotingServer(final NettyServerConfig serverConfig) { |
|
|
|
|
this.serverConfig = serverConfig; |
|
|
|
|
if (NettyUtils.useEpoll()) { |
|
|
|
|
this.bossGroup = new EpollEventLoopGroup(1, new ThreadFactory() { |
|
|
|
|
private AtomicInteger threadIndex = new AtomicInteger(0); |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Thread newThread(Runnable r) { |
|
|
|
|
return new Thread(r, String.format("NettyServerBossThread_%d", this.threadIndex.incrementAndGet())); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
this.workGroup = new EpollEventLoopGroup(serverConfig.getWorkerThread(), new ThreadFactory() { |
|
|
|
|
private AtomicInteger threadIndex = new AtomicInteger(0); |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Thread newThread(Runnable r) { |
|
|
|
|
return new Thread(r, String.format("NettyServerWorkerThread_%d", this.threadIndex.incrementAndGet())); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} else { |
|
|
|
|
this.bossGroup = new NioEventLoopGroup(1, new ThreadFactory() { |
|
|
|
|
private AtomicInteger threadIndex = new AtomicInteger(0); |
|
|
|
|
|
|
|
|
@ -114,29 +136,26 @@ public class NettyRemotingServer {
|
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* server start |
|
|
|
|
*/ |
|
|
|
|
public void start() { |
|
|
|
|
|
|
|
|
|
if(this.isStarted.get()){ |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (isStarted.compareAndSet(false, true)) { |
|
|
|
|
this.serverBootstrap |
|
|
|
|
.group(this.bossGroup, this.workGroup) |
|
|
|
|
.channel(NioServerSocketChannel.class) |
|
|
|
|
.channel(NettyUtils.getServerSocketChannelClass()) |
|
|
|
|
.option(ChannelOption.SO_REUSEADDR, true) |
|
|
|
|
.option(ChannelOption.SO_BACKLOG, serverConfig.getSoBacklog()) |
|
|
|
|
.childOption(ChannelOption.SO_KEEPALIVE, serverConfig.isSoKeepalive()) |
|
|
|
|
.childOption(ChannelOption.TCP_NODELAY, serverConfig.isTcpNoDelay()) |
|
|
|
|
.childOption(ChannelOption.SO_SNDBUF, serverConfig.getSendBufferSize()) |
|
|
|
|
.childOption(ChannelOption.SO_RCVBUF, serverConfig.getReceiveBufferSize()) |
|
|
|
|
.childHandler(new ChannelInitializer<NioSocketChannel>() { |
|
|
|
|
.childHandler(new ChannelInitializer<SocketChannel>() { |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
protected void initChannel(NioSocketChannel ch) throws Exception { |
|
|
|
|
protected void initChannel(SocketChannel ch) throws Exception { |
|
|
|
|
initNettyChannel(ch); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
@ -155,16 +174,15 @@ public class NettyRemotingServer {
|
|
|
|
|
} else { |
|
|
|
|
throw new RuntimeException(String.format("NettyRemotingServer bind %s fail", serverConfig.getListenPort())); |
|
|
|
|
} |
|
|
|
|
//
|
|
|
|
|
isStarted.compareAndSet(false, true); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* init netty channel |
|
|
|
|
* |
|
|
|
|
* @param ch socket channel |
|
|
|
|
* @throws Exception |
|
|
|
|
*/ |
|
|
|
|
private void initNettyChannel(NioSocketChannel ch) throws Exception{ |
|
|
|
|
private void initNettyChannel(SocketChannel ch) { |
|
|
|
|
ChannelPipeline pipeline = ch.pipeline(); |
|
|
|
|
pipeline.addLast("encoder", encoder); |
|
|
|
|
pipeline.addLast("decoder", new NettyDecoder()); |
|
|
|
@ -173,6 +191,7 @@ public class NettyRemotingServer {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* register processor |
|
|
|
|
* |
|
|
|
|
* @param commandType command type |
|
|
|
|
* @param processor processor |
|
|
|
|
*/ |
|
|
|
@ -193,6 +212,7 @@ public class NettyRemotingServer {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* get default thread executor |
|
|
|
|
* |
|
|
|
|
* @return thread executor |
|
|
|
|
*/ |
|
|
|
|
public ExecutorService getDefaultExecutor() { |
|
|
|
|