|
|
@ -42,47 +42,78 @@ import java.util.Collections; |
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.Set; |
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* netty executor manager |
|
|
|
|
|
|
|
*/ |
|
|
|
@Service |
|
|
|
@Service |
|
|
|
public class NettyExecutorManager extends AbstractExecutorManager{ |
|
|
|
public class NettyExecutorManager extends AbstractExecutorManager{ |
|
|
|
|
|
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(NettyExecutorManager.class); |
|
|
|
private final Logger logger = LoggerFactory.getLogger(NettyExecutorManager.class); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* zookeeper node manager |
|
|
|
|
|
|
|
*/ |
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
private ZookeeperNodeManager zookeeperNodeManager; |
|
|
|
private ZookeeperNodeManager zookeeperNodeManager; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* netty remote client |
|
|
|
|
|
|
|
*/ |
|
|
|
private final NettyRemotingClient nettyRemotingClient; |
|
|
|
private final NettyRemotingClient nettyRemotingClient; |
|
|
|
|
|
|
|
|
|
|
|
public NettyExecutorManager(){ |
|
|
|
public NettyExecutorManager(){ |
|
|
|
final NettyClientConfig clientConfig = new NettyClientConfig(); |
|
|
|
final NettyClientConfig clientConfig = new NettyClientConfig(); |
|
|
|
this.nettyRemotingClient = new NettyRemotingClient(clientConfig); |
|
|
|
this.nettyRemotingClient = new NettyRemotingClient(clientConfig); |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* register EXECUTE_TASK_RESPONSE command type TaskResponseProcessor |
|
|
|
|
|
|
|
* register EXECUTE_TASK_ACK command type TaskAckProcessor |
|
|
|
|
|
|
|
*/ |
|
|
|
this.nettyRemotingClient.registerProcessor(CommandType.EXECUTE_TASK_RESPONSE, new TaskResponseProcessor()); |
|
|
|
this.nettyRemotingClient.registerProcessor(CommandType.EXECUTE_TASK_RESPONSE, new TaskResponseProcessor()); |
|
|
|
this.nettyRemotingClient.registerProcessor(CommandType.EXECUTE_TASK_ACK, new TaskAckProcessor()); |
|
|
|
this.nettyRemotingClient.registerProcessor(CommandType.EXECUTE_TASK_ACK, new TaskAckProcessor()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* execute logic |
|
|
|
|
|
|
|
* @param context context |
|
|
|
|
|
|
|
* @throws ExecuteException |
|
|
|
|
|
|
|
*/ |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void execute(ExecutionContext executeContext) throws ExecuteException { |
|
|
|
public void execute(ExecutionContext context) throws ExecuteException { |
|
|
|
Set<String> allNodes = getAllNodes(executeContext); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* all nodes |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
Set<String> allNodes = getAllNodes(context); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* fail nodes |
|
|
|
|
|
|
|
*/ |
|
|
|
Set<String> failNodeSet = new HashSet<>(); |
|
|
|
Set<String> failNodeSet = new HashSet<>(); |
|
|
|
//
|
|
|
|
|
|
|
|
Command command = buildCommand(executeContext); |
|
|
|
/** |
|
|
|
Host host = executeContext.getHost(); |
|
|
|
* build command accord executeContext |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
Command command = buildCommand(context); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* execute task host |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
Host host = context.getHost(); |
|
|
|
boolean success = false; |
|
|
|
boolean success = false; |
|
|
|
//
|
|
|
|
|
|
|
|
while (!success) { |
|
|
|
while (!success) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
doExecute(host, command); |
|
|
|
doExecute(host,command); |
|
|
|
success = true; |
|
|
|
success = true; |
|
|
|
executeContext.setHost(host); |
|
|
|
context.setHost(host); |
|
|
|
} catch (ExecuteException ex) { |
|
|
|
} catch (ExecuteException ex) { |
|
|
|
logger.error(String.format("execute context : %s error", executeContext.getContext()), ex); |
|
|
|
logger.error(String.format("execute context : %s error", context.getContext()), ex); |
|
|
|
try { |
|
|
|
try { |
|
|
|
failNodeSet.add(host.getAddress()); |
|
|
|
failNodeSet.add(host.getAddress()); |
|
|
|
Set<String> tmpAllIps = new HashSet<>(allNodes); |
|
|
|
Set<String> tmpAllIps = new HashSet<>(allNodes); |
|
|
|
Collection<String> remained = CollectionUtils.subtract(tmpAllIps, failNodeSet); |
|
|
|
Collection<String> remained = CollectionUtils.subtract(tmpAllIps, failNodeSet); |
|
|
|
if (remained != null && remained.size() > 0) { |
|
|
|
if (remained != null && remained.size() > 0) { |
|
|
|
host = Host.of(remained.iterator().next()); |
|
|
|
host = Host.of(remained.iterator().next()); |
|
|
|
logger.error("retry execute context : {} host : {}", executeContext.getContext(), host); |
|
|
|
logger.error("retry execute context : {} host : {}", context.getContext(), host); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
throw new ExecuteException("fail after try all nodes"); |
|
|
|
throw new ExecuteException("fail after try all nodes"); |
|
|
|
} |
|
|
|
} |
|
|
@ -93,6 +124,11 @@ public class NettyExecutorManager extends AbstractExecutorManager{ |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* build command |
|
|
|
|
|
|
|
* @param context context |
|
|
|
|
|
|
|
* @return command |
|
|
|
|
|
|
|
*/ |
|
|
|
private Command buildCommand(ExecutionContext context) { |
|
|
|
private Command buildCommand(ExecutionContext context) { |
|
|
|
ExecuteTaskRequestCommand requestCommand = new ExecuteTaskRequestCommand(); |
|
|
|
ExecuteTaskRequestCommand requestCommand = new ExecuteTaskRequestCommand(); |
|
|
|
ExecutorType executorType = context.getExecutorType(); |
|
|
|
ExecutorType executorType = context.getExecutorType(); |
|
|
@ -110,7 +146,16 @@ public class NettyExecutorManager extends AbstractExecutorManager{ |
|
|
|
return requestCommand.convert2Command(); |
|
|
|
return requestCommand.convert2Command(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* execute logic |
|
|
|
|
|
|
|
* @param host host |
|
|
|
|
|
|
|
* @param command command |
|
|
|
|
|
|
|
* @throws ExecuteException |
|
|
|
|
|
|
|
*/ |
|
|
|
private void doExecute(final Host host, final Command command) throws ExecuteException { |
|
|
|
private void doExecute(final Host host, final Command command) throws ExecuteException { |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* retry count,default retry 3 |
|
|
|
|
|
|
|
*/ |
|
|
|
int retryCount = 3; |
|
|
|
int retryCount = 3; |
|
|
|
boolean success = false; |
|
|
|
boolean success = false; |
|
|
|
do { |
|
|
|
do { |
|
|
@ -131,8 +176,16 @@ public class NettyExecutorManager extends AbstractExecutorManager{ |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* get all nodes |
|
|
|
|
|
|
|
* @param context context |
|
|
|
|
|
|
|
* @return nodes |
|
|
|
|
|
|
|
*/ |
|
|
|
private Set<String> getAllNodes(ExecutionContext context){ |
|
|
|
private Set<String> getAllNodes(ExecutionContext context){ |
|
|
|
Set<String> nodes = Collections.EMPTY_SET; |
|
|
|
Set<String> nodes = Collections.EMPTY_SET; |
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* executor type |
|
|
|
|
|
|
|
*/ |
|
|
|
ExecutorType executorType = context.getExecutorType(); |
|
|
|
ExecutorType executorType = context.getExecutorType(); |
|
|
|
switch (executorType){ |
|
|
|
switch (executorType){ |
|
|
|
case WORKER: |
|
|
|
case WORKER: |
|
|
|