|
@@ -13,7 +13,7 @@ import io.netty.channel.Channel;
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.slf4j.MDC;
|
|
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
@@ -33,6 +33,7 @@ import java.util.concurrent.ScheduledFuture;
|
|
|
public class ClusterSlaveService {
|
|
|
|
|
|
private final ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
|
|
|
+ private ScheduledFuture<?> taskFuture;
|
|
|
|
|
|
private final ApplicationRepository repo;
|
|
|
private final HaClusterConfig clusterConfig;
|
|
@@ -41,8 +42,6 @@ public class ClusterSlaveService {
|
|
|
private final ExecutorService executorService= Executors.newFixedThreadPool(1);
|
|
|
private final List<ClusterSlave> clientTasks = Collections.synchronizedList(new ArrayList<>());
|
|
|
|
|
|
- private ScheduledFuture<?> taskFuture;
|
|
|
-
|
|
|
@PostConstruct
|
|
|
void init() {
|
|
|
this.bootstrapFactory = new ClusterSlaveBootstrapFactory(1, 5);
|
|
@@ -61,7 +60,7 @@ public class ClusterSlaveService {
|
|
|
if (cluster.getServerId() == this.clusterConfig.getServerId()) {
|
|
|
continue;
|
|
|
}
|
|
|
- ClusterSlave slaveClient = new ClusterSlave(repo, clusterConfig, cluster, this.bootstrapFactory);
|
|
|
+ ClusterSlave slaveClient = new ClusterSlave(this, repo, clusterConfig, cluster, this.bootstrapFactory);
|
|
|
this.clientTasks.add(slaveClient);
|
|
|
}
|
|
|
|
|
@@ -69,7 +68,7 @@ public class ClusterSlaveService {
|
|
|
List<Future<Object>> futures = this.executorService.invokeAll(this.clientTasks);
|
|
|
log.info("ClusterSlaveService.run: futures, {} EA.", (long)futures.size());
|
|
|
|
|
|
- startSyncSchedule();
|
|
|
+ dataSyncSchedule();
|
|
|
}
|
|
|
catch(InterruptedException e) {
|
|
|
log.error("ClusterSlaveService.run: Exception: InterruptedException");
|
|
@@ -89,7 +88,7 @@ public class ClusterSlaveService {
|
|
|
if (cluster.getServerId() == this.clusterConfig.getServerId()) {
|
|
|
continue;
|
|
|
}
|
|
|
- channelClose(cluster.getNetState().getChannel());
|
|
|
+ channelClose(cluster.getSyncState().getChannel());
|
|
|
}
|
|
|
try {
|
|
|
if (this.bootstrapFactory != null && this.bootstrapFactory.getEventLoopGroup() != null) {
|
|
@@ -129,8 +128,8 @@ public class ClusterSlaveService {
|
|
|
.build();
|
|
|
}
|
|
|
|
|
|
- private void startSyncSchedule() {
|
|
|
- log.info("ClusterSlaveService: startSyncSchedule: {} seconds.", this.clusterConfig.getSyncSeconds());
|
|
|
+ private void dataSyncSchedule() {
|
|
|
+ log.info("ClusterSlaveService:dataSyncSchedule: {} seconds.", this.clusterConfig.getSyncSeconds());
|
|
|
this.taskFuture = this.taskScheduler.scheduleAtFixedRate(() -> {
|
|
|
ClusterMessage clusterMsg = getClusterMessage();
|
|
|
for (Map.Entry<Integer, HaClusterConfig.HaCluster> entry : this.clusterConfig.getClusterMap().entrySet()) {
|
|
@@ -138,33 +137,39 @@ public class ClusterSlaveService {
|
|
|
if (cluster.getServerId() == this.clusterConfig.getServerId()) {
|
|
|
continue;
|
|
|
}
|
|
|
- if (cluster.getNetState().getState() != NET.CLOSED) {
|
|
|
- try {
|
|
|
- ChannelFuture f = cluster.getNetState().getChannel().writeAndFlush(clusterMsg);
|
|
|
- f.awaitUninterruptibly();
|
|
|
- if (f.isDone() || f.isSuccess()) {
|
|
|
- log.info("-ClusterSlaveHandler.sendData___: [{}], {}, [--TO: serverId: {}, serverTime: {}, infos: {}]",
|
|
|
- this.clusterConfig.getServerId(), NettyUtils.getTcpAddress(cluster.getNetState().getChannel()),
|
|
|
- clusterMsg.getServerId(), clusterMsg.getServerTime(), clusterMsg.getInfos().size());
|
|
|
- }
|
|
|
- }
|
|
|
- catch (Exception e) {
|
|
|
- log.info("-ClusterSlaveHandler.sendData___: [{}], {}, Failed: [--TO: serverId: {}, serverTime: {}, infos: {}]",
|
|
|
- this.clusterConfig.getServerId(), NettyUtils.getTcpAddress(cluster.getNetState().getChannel()),
|
|
|
- clusterMsg.getServerId(), clusterMsg.getServerTime(), clusterMsg.getInfos().size());
|
|
|
- log.info("-ClusterSlaveHandler.sendData___: [{}], {}, Failed: {}",
|
|
|
- this.clusterConfig.getServerId(), NettyUtils.getTcpAddress(cluster.getNetState().getChannel()), e.getMessage());
|
|
|
- }
|
|
|
+ if (cluster.getSyncState().getState() != NET.CLOSED) {
|
|
|
+ sendSyncData(cluster, cluster.getSyncState().getChannel(), clusterMsg);
|
|
|
}
|
|
|
}
|
|
|
}, this.clusterConfig.getSyncSeconds() * 1000L);
|
|
|
|
|
|
}
|
|
|
|
|
|
- @Scheduled(fixedRate = 5000)
|
|
|
- public void logPeriodicMessage() {
|
|
|
- log.info("This is a periodic log message every 5 seconds.");
|
|
|
+ public void sendSyncData(final HaClusterConfig.HaCluster cluster, final Channel channel, ClusterMessage clusterMsg) {
|
|
|
+ if (null == clusterMsg) {
|
|
|
+ clusterMsg = getClusterMessage();
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ MDC.put("id", cluster.getLogKey());
|
|
|
+ ChannelFuture f = channel.writeAndFlush(clusterMsg);
|
|
|
+ f.awaitUninterruptibly();
|
|
|
+ if (f.isDone() || f.isSuccess()) {
|
|
|
+ log.info("ClusterSlaveService.sendSyncData: [{}], {}, [--TO: serverId: {}, serverTime: {}, infos: {}]",
|
|
|
+ this.clusterConfig.getServerId(), NettyUtils.getTcpAddress(channel),
|
|
|
+ clusterMsg.getServerId(), clusterMsg.getServerTime(), clusterMsg.getInfos().size());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e) {
|
|
|
+ log.info("ClusterSlaveService.sendSyncData: [{}], {}, Failed: [--TO: serverId: {}, serverTime: {}, infos: {}]",
|
|
|
+ this.clusterConfig.getServerId(), NettyUtils.getTcpAddress(channel),
|
|
|
+ clusterMsg.getServerId(), clusterMsg.getServerTime(), clusterMsg.getInfos().size());
|
|
|
+ log.info("ClusterSlaveService.sendSyncData: [{}], {}, Failed: {}",
|
|
|
+ this.clusterConfig.getServerId(), NettyUtils.getTcpAddress(channel), e.getMessage());
|
|
|
+ }
|
|
|
+ finally {
|
|
|
+ MDC.remove(cluster.getLogKey());
|
|
|
+ MDC.clear();
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|