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

bugfix collector can not auto reconnect when channel idle #1259

Merged
merged 4 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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 @@ -132,12 +132,16 @@ public void onChannelActive(Channel channel) {
scheduledExecutor = Executors.newSingleThreadScheduledExecutor(threadFactory);
// schedule send heartbeat message
scheduledExecutor.scheduleAtFixedRate(() -> {
ClusterMsg.Message heartbeat = ClusterMsg.Message.newBuilder()
.setIdentity(identity)
.setType(ClusterMsg.MessageType.HEARTBEAT)
.build();
CollectServer.this.sendMsg(heartbeat);
log.info("collector send cluster server heartbeat, time: {}.", System.currentTimeMillis());
try {
ClusterMsg.Message heartbeat = ClusterMsg.Message.newBuilder()
.setIdentity(identity)
.setType(ClusterMsg.MessageType.HEARTBEAT)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

发消息最好把Request加上

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的 setDirection(ClusterMsg.Direction.REQUEST)

.build();
CollectServer.this.sendMsg(heartbeat);
log.info("collector send cluster server heartbeat, time: {}.", System.currentTimeMillis());
} catch (Exception e) {
log.error(e.getMessage());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里log日志弄的详细一点把

}
}, 5, 5, TimeUnit.SECONDS);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private void init(final SchedulerProperties schedulerProperties, final CommonThr
this.remotingServer.registerHook(Lists.newArrayList(new NettyHook() {
@Override
public void doBeforeRequest(ChannelHandlerContext ctx, ClusterMsg.Message message) {
ManageServer.this.clientChannelTable.put(message.getIdentity(), ctx.channel());
// do something before processor list
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

去了的话直接删了好了,hook不用加了

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好 那接口还是留着以后用

}
}));

Expand All @@ -84,13 +84,17 @@ public void start() {
this.remotingServer.start();

this.channelSchedule.scheduleAtFixedRate(() -> {
ManageServer.this.clientChannelTable.forEach((collector, channel) -> {
if (!channel.isActive()) {
channel.closeFuture();
ManageServer.this.clientChannelTable.remove(collector);
ManageServer.this.collectorAndJobScheduler.collectorGoOffline(collector);
}
});
try {
this.clientChannelTable.forEach((collector, channel) -> {
if (!channel.isActive()) {
channel.closeFuture();
this.clientChannelTable.remove(collector);
this.collectorAndJobScheduler.collectorGoOffline(collector);
}
});
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}, 10, 3, TimeUnit.SECONDS);
}

Expand All @@ -113,6 +117,14 @@ public Channel getChannel(final String identity) {
return channel;
}

public void addChannel(final String identity, Channel channel) {
Channel preChannel = this.clientChannelTable.get(identity);
if (preChannel != null && channel.isActive()) {
preChannel.close();
}
this.clientChannelTable.put(identity, channel);
}

public void closeChannel(final String identity) {
Channel channel = this.getChannel(identity);
if (channel != null) {
Expand Down Expand Up @@ -165,6 +177,7 @@ public void onChannelIdle(Channel channel) {
if (identity != null) {
ManageServer.this.clientChannelTable.remove(identity);
ManageServer.this.collectorAndJobScheduler.collectorGoOffline(identity);
channel.close();
log.info("handle idle event triggered. the client {} is going offline.", identity);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public ClusterMsg.Message handle(ChannelHandlerContext ctx, ClusterMsg.Message m
String collector = message.getIdentity();
log.info("the collector {} actively requests to go online.", collector);
CollectorInfo collectorInfo = JsonUtil.fromJson(message.getMsg(), CollectorInfo.class);
this.manageServer.addChannel(collector, ctx.channel());
this.manageServer.getCollectorAndJobScheduler().collectorGoOnline(collector, collectorInfo);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ public ClusterMsg.Message handle(ChannelHandlerContext ctx, ClusterMsg.Message m
String identity = message.getIdentity();
boolean isChannelExist = this.manageServer.isChannelExist(identity);
if (!isChannelExist) {
log.info("the collector {} has reconnected and to go online.", identity);
this.manageServer.getCollectorAndJobScheduler().collectorGoOnline(identity, null);
log.info("the collector {} is not online.", identity);
}
if (log.isDebugEnabled()) {
log.debug("server receive collector heartbeat");
log.debug("server receive collector {} heartbeat", message.getIdentity());
}
return ClusterMsg.Message.newBuilder()
.setType(ClusterMsg.MessageType.HEARTBEAT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private void initChannel(final SocketChannel channel) {
pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
pipeline.addLast(new ProtobufEncoder());
// idle state
pipeline.addLast(new IdleStateHandler(0, 0, 30));
pipeline.addLast(new IdleStateHandler(0, 0, 100));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

放到ServerConfig里面怎么样?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

pipeline.addLast(new NettyServerHandler());
}

Expand Down
Loading