mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
= cold cleanup
* using lambda if possible (we are already building in Java8) * using diamond when new generic instance * remove used improt
This commit is contained in:
@@ -63,46 +63,42 @@ public class ClientAuthenticationHandler extends SimpleChannelHandler {
|
||||
}
|
||||
|
||||
byte[] ackBytes = AdminNettyUtils.ackPacket();
|
||||
AdminNettyUtils.write(ctx.getChannel(), ackBytes, new ChannelFutureListener() {
|
||||
AdminNettyUtils.write(ctx.getChannel(), ackBytes, future -> {
|
||||
logger.info("remove unused channel handlers after authentication is done successfully.");
|
||||
ctx.getPipeline().remove(HandshakeInitializationHandler.class.getName());
|
||||
ctx.getPipeline().remove(ClientAuthenticationHandler.class.getName());
|
||||
|
||||
public void operationComplete(ChannelFuture future) throws Exception {
|
||||
logger.info("remove unused channel handlers after authentication is done successfully.");
|
||||
ctx.getPipeline().remove(HandshakeInitializationHandler.class.getName());
|
||||
ctx.getPipeline().remove(ClientAuthenticationHandler.class.getName());
|
||||
|
||||
int readTimeout = defaultSubscriptorDisconnectIdleTimeout;
|
||||
int writeTimeout = defaultSubscriptorDisconnectIdleTimeout;
|
||||
if (clientAuth.getNetReadTimeout() > 0) {
|
||||
readTimeout = clientAuth.getNetReadTimeout();
|
||||
}
|
||||
if (clientAuth.getNetWriteTimeout() > 0) {
|
||||
writeTimeout = clientAuth.getNetWriteTimeout();
|
||||
}
|
||||
// fix bug: soTimeout parameter's unit from connector is
|
||||
// millseconds.
|
||||
IdleStateHandler idleStateHandler = new IdleStateHandler(NettyUtils.hashedWheelTimer,
|
||||
readTimeout,
|
||||
writeTimeout,
|
||||
0,
|
||||
TimeUnit.MILLISECONDS);
|
||||
ctx.getPipeline().addBefore(SessionHandler.class.getName(),
|
||||
IdleStateHandler.class.getName(),
|
||||
idleStateHandler);
|
||||
|
||||
IdleStateAwareChannelHandler idleStateAwareChannelHandler = new IdleStateAwareChannelHandler() {
|
||||
|
||||
public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception {
|
||||
logger.warn("channel:{} idle timeout exceeds, close channel to save server resources...",
|
||||
ctx.getChannel());
|
||||
ctx.getChannel().close();
|
||||
}
|
||||
|
||||
};
|
||||
ctx.getPipeline().addBefore(SessionHandler.class.getName(),
|
||||
IdleStateAwareChannelHandler.class.getName(),
|
||||
idleStateAwareChannelHandler);
|
||||
int readTimeout = defaultSubscriptorDisconnectIdleTimeout;
|
||||
int writeTimeout = defaultSubscriptorDisconnectIdleTimeout;
|
||||
if (clientAuth.getNetReadTimeout() > 0) {
|
||||
readTimeout = clientAuth.getNetReadTimeout();
|
||||
}
|
||||
if (clientAuth.getNetWriteTimeout() > 0) {
|
||||
writeTimeout = clientAuth.getNetWriteTimeout();
|
||||
}
|
||||
// fix bug: soTimeout parameter's unit from connector is
|
||||
// millseconds.
|
||||
IdleStateHandler idleStateHandler = new IdleStateHandler(NettyUtils.hashedWheelTimer,
|
||||
readTimeout,
|
||||
writeTimeout,
|
||||
0,
|
||||
TimeUnit.MILLISECONDS);
|
||||
ctx.getPipeline().addBefore(SessionHandler.class.getName(),
|
||||
IdleStateHandler.class.getName(),
|
||||
idleStateHandler);
|
||||
|
||||
IdleStateAwareChannelHandler idleStateAwareChannelHandler = new IdleStateAwareChannelHandler() {
|
||||
|
||||
public void channelIdle(ChannelHandlerContext ctx1, IdleStateEvent e1) throws Exception {
|
||||
logger.warn("channel:{} idle timeout exceeds, close channel to save server resources...",
|
||||
ctx1.getChannel());
|
||||
ctx1.getChannel().close();
|
||||
}
|
||||
|
||||
};
|
||||
ctx.getPipeline().addBefore(SessionHandler.class.getName(),
|
||||
IdleStateAwareChannelHandler.class.getName(),
|
||||
idleStateAwareChannelHandler);
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -46,16 +46,12 @@ public class HandshakeInitializationHandler extends SimpleChannelHandler {
|
||||
.build()
|
||||
.toByteArray();
|
||||
|
||||
AdminNettyUtils.write(ctx.getChannel(), body, new ChannelFutureListener() {
|
||||
|
||||
public void operationComplete(ChannelFuture future) throws Exception {
|
||||
logger.info("remove unused channel handlers after authentication is done successfully.");
|
||||
ctx.getPipeline().get(HandshakeInitializationHandler.class.getName());
|
||||
ClientAuthenticationHandler handler = (ClientAuthenticationHandler) ctx.getPipeline()
|
||||
.get(ClientAuthenticationHandler.class.getName());
|
||||
handler.setSeed(seed);
|
||||
}
|
||||
|
||||
AdminNettyUtils.write(ctx.getChannel(), body, future -> {
|
||||
logger.info("remove unused channel handlers after authentication is done successfully.");
|
||||
ctx.getPipeline().get(HandshakeInitializationHandler.class.getName());
|
||||
ClientAuthenticationHandler handler = (ClientAuthenticationHandler) ctx.getPipeline()
|
||||
.get(ClientAuthenticationHandler.class.getName());
|
||||
handler.setSeed(seed);
|
||||
});
|
||||
logger.info("send handshake initialization packet to : {}", ctx.getChannel());
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public class AdminNettyUtils {
|
||||
|
||||
public static void write(Channel channel, ByteBuffer body) {
|
||||
byte[] header = ByteBuffer.allocate(HEADER_LENGTH).order(ByteOrder.BIG_ENDIAN).putInt(body.limit()).array();
|
||||
List<ChannelBuffer> components = new ArrayList<ChannelBuffer>(2);
|
||||
List<ChannelBuffer> components = new ArrayList<>(2);
|
||||
components.add(ChannelBuffers.wrappedBuffer(ByteOrder.BIG_ENDIAN, header));
|
||||
components.add(ChannelBuffers.wrappedBuffer(body));
|
||||
Channels.write(channel, new CompositeChannelBuffer(ByteOrder.BIG_ENDIAN, components));
|
||||
|
||||
@@ -69,21 +69,18 @@ public class CanalAdminWithNetty extends AbstractCanalLifeCycle {
|
||||
bootstrap.setOption("child.tcpNoDelay", true);
|
||||
|
||||
// 构造对应的pipeline
|
||||
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
|
||||
bootstrap.setPipelineFactory(() -> {
|
||||
ChannelPipeline pipelines = Channels.pipeline();
|
||||
pipelines.addLast(FixedHeaderFrameDecoder.class.getName(), new FixedHeaderFrameDecoder());
|
||||
// support to maintain child socket channel.
|
||||
pipelines.addLast(HandshakeInitializationHandler.class.getName(),
|
||||
new HandshakeInitializationHandler(childGroups));
|
||||
pipelines.addLast(ClientAuthenticationHandler.class.getName(),
|
||||
new ClientAuthenticationHandler(canalAdmin));
|
||||
|
||||
public ChannelPipeline getPipeline() throws Exception {
|
||||
ChannelPipeline pipelines = Channels.pipeline();
|
||||
pipelines.addLast(FixedHeaderFrameDecoder.class.getName(), new FixedHeaderFrameDecoder());
|
||||
// support to maintain child socket channel.
|
||||
pipelines.addLast(HandshakeInitializationHandler.class.getName(),
|
||||
new HandshakeInitializationHandler(childGroups));
|
||||
pipelines.addLast(ClientAuthenticationHandler.class.getName(),
|
||||
new ClientAuthenticationHandler(canalAdmin));
|
||||
|
||||
SessionHandler sessionHandler = new SessionHandler(canalAdmin);
|
||||
pipelines.addLast(SessionHandler.class.getName(), sessionHandler);
|
||||
return pipelines;
|
||||
}
|
||||
SessionHandler sessionHandler = new SessionHandler(canalAdmin);
|
||||
pipelines.addLast(SessionHandler.class.getName(), sessionHandler);
|
||||
return pipelines;
|
||||
});
|
||||
|
||||
// 启动
|
||||
|
||||
@@ -72,22 +72,18 @@ public class CanalMQStarter {
|
||||
running = true;
|
||||
logger.info("## the MQ workers is running now ......");
|
||||
|
||||
shutdownThread = new Thread() {
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
logger.info("## stop the MQ workers");
|
||||
running = false;
|
||||
executorService.shutdown();
|
||||
canalMQProducer.stop();
|
||||
} catch (Throwable e) {
|
||||
logger.warn("##something goes wrong when stopping MQ workers:", e);
|
||||
} finally {
|
||||
logger.info("## canal MQ is down.");
|
||||
}
|
||||
shutdownThread = new Thread(() -> {
|
||||
try {
|
||||
logger.info("## stop the MQ workers");
|
||||
running = false;
|
||||
executorService.shutdown();
|
||||
canalMQProducer.stop();
|
||||
} catch (Throwable e) {
|
||||
logger.warn("##something goes wrong when stopping MQ workers:", e);
|
||||
} finally {
|
||||
logger.info("## canal MQ is down.");
|
||||
}
|
||||
|
||||
};
|
||||
});
|
||||
|
||||
Runtime.getRuntime().addShutdownHook(shutdownThread);
|
||||
} catch (Throwable e) {
|
||||
|
||||
@@ -78,12 +78,7 @@ public class CanalServerWithEmbedded extends AbstractCanalLifeCycle implements C
|
||||
loadCanalMetrics();
|
||||
metrics.setServerPort(metricsPort);
|
||||
metrics.initialize();
|
||||
canalInstances = MigrateMap.makeComputingMap(new Function<String, CanalInstance>() {
|
||||
|
||||
public CanalInstance apply(String destination) {
|
||||
return canalInstanceGenerator.generate(destination);
|
||||
}
|
||||
});
|
||||
canalInstances = MigrateMap.makeComputingMap(destination -> canalInstanceGenerator.generate(destination));
|
||||
|
||||
// lastRollbackPostions = new MapMaker().makeMap();
|
||||
}
|
||||
@@ -274,19 +269,9 @@ public class CanalServerWithEmbedded extends AbstractCanalLifeCycle implements C
|
||||
boolean raw = isRaw(canalInstance.getEventStore());
|
||||
List entrys = null;
|
||||
if (raw) {
|
||||
entrys = Lists.transform(events.getEvents(), new Function<Event, ByteString>() {
|
||||
|
||||
public ByteString apply(Event input) {
|
||||
return input.getRawEntry();
|
||||
}
|
||||
});
|
||||
entrys = Lists.transform(events.getEvents(), Event::getRawEntry);
|
||||
} else {
|
||||
entrys = Lists.transform(events.getEvents(), new Function<Event, CanalEntry.Entry>() {
|
||||
|
||||
public CanalEntry.Entry apply(Event input) {
|
||||
return input.getEntry();
|
||||
}
|
||||
});
|
||||
entrys = Lists.transform(events.getEvents(), Event::getEntry);
|
||||
}
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("get successfully, clientId:{} batchSize:{} real size is {} and result is [batchId:{} , position:{}]",
|
||||
@@ -366,19 +351,9 @@ public class CanalServerWithEmbedded extends AbstractCanalLifeCycle implements C
|
||||
boolean raw = isRaw(canalInstance.getEventStore());
|
||||
List entrys = null;
|
||||
if (raw) {
|
||||
entrys = Lists.transform(events.getEvents(), new Function<Event, ByteString>() {
|
||||
|
||||
public ByteString apply(Event input) {
|
||||
return input.getRawEntry();
|
||||
}
|
||||
});
|
||||
entrys = Lists.transform(events.getEvents(), Event::getRawEntry);
|
||||
} else {
|
||||
entrys = Lists.transform(events.getEvents(), new Function<Event, CanalEntry.Entry>() {
|
||||
|
||||
public CanalEntry.Entry apply(Event input) {
|
||||
return input.getEntry();
|
||||
}
|
||||
});
|
||||
entrys = Lists.transform(events.getEvents(), Event::getEntry);
|
||||
}
|
||||
if (logger.isInfoEnabled()) {
|
||||
logger.info("getWithoutAck successfully, clientId:{} batchSize:{} real size is {} and result is [batchId:{} , position:{}]",
|
||||
@@ -403,7 +378,7 @@ public class CanalServerWithEmbedded extends AbstractCanalLifeCycle implements C
|
||||
|
||||
CanalInstance canalInstance = canalInstances.get(clientIdentity.getDestination());
|
||||
Map<Long, PositionRange> batchs = canalInstance.getMetaManager().listAllBatchs(clientIdentity);
|
||||
List<Long> result = new ArrayList<Long>(batchs.keySet());
|
||||
List<Long> result = new ArrayList<>(batchs.keySet());
|
||||
Collections.sort(result);
|
||||
return result;
|
||||
}
|
||||
@@ -561,7 +536,7 @@ public class CanalServerWithEmbedded extends AbstractCanalLifeCycle implements C
|
||||
|
||||
private void loadCanalMetrics() {
|
||||
ServiceLoader<CanalMetricsProvider> providers = ServiceLoader.load(CanalMetricsProvider.class);
|
||||
List<CanalMetricsProvider> list = new ArrayList<CanalMetricsProvider>();
|
||||
List<CanalMetricsProvider> list = new ArrayList<>();
|
||||
for (CanalMetricsProvider provider : providers) {
|
||||
list.add(provider);
|
||||
}
|
||||
|
||||
@@ -75,21 +75,18 @@ public class CanalServerWithNetty extends AbstractCanalLifeCycle implements Cana
|
||||
bootstrap.setOption("child.tcpNoDelay", true);
|
||||
|
||||
// 构造对应的pipeline
|
||||
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
|
||||
bootstrap.setPipelineFactory(() -> {
|
||||
ChannelPipeline pipelines = Channels.pipeline();
|
||||
pipelines.addLast(FixedHeaderFrameDecoder.class.getName(), new FixedHeaderFrameDecoder());
|
||||
// support to maintain child socket channel.
|
||||
pipelines.addLast(HandshakeInitializationHandler.class.getName(),
|
||||
new HandshakeInitializationHandler(childGroups));
|
||||
pipelines.addLast(ClientAuthenticationHandler.class.getName(),
|
||||
new ClientAuthenticationHandler(embeddedServer));
|
||||
|
||||
public ChannelPipeline getPipeline() throws Exception {
|
||||
ChannelPipeline pipelines = Channels.pipeline();
|
||||
pipelines.addLast(FixedHeaderFrameDecoder.class.getName(), new FixedHeaderFrameDecoder());
|
||||
// support to maintain child socket channel.
|
||||
pipelines.addLast(HandshakeInitializationHandler.class.getName(),
|
||||
new HandshakeInitializationHandler(childGroups));
|
||||
pipelines.addLast(ClientAuthenticationHandler.class.getName(),
|
||||
new ClientAuthenticationHandler(embeddedServer));
|
||||
|
||||
SessionHandler sessionHandler = new SessionHandler(embeddedServer);
|
||||
pipelines.addLast(SessionHandler.class.getName(), sessionHandler);
|
||||
return pipelines;
|
||||
}
|
||||
SessionHandler sessionHandler = new SessionHandler(embeddedServer);
|
||||
pipelines.addLast(SessionHandler.class.getName(), sessionHandler);
|
||||
return pipelines;
|
||||
});
|
||||
|
||||
// 启动
|
||||
|
||||
@@ -29,7 +29,7 @@ public class NettyUtils {
|
||||
|
||||
public static void write(Channel channel, ByteBuffer body, ChannelFutureListener channelFutureListner) {
|
||||
byte[] header = ByteBuffer.allocate(HEADER_LENGTH).order(ByteOrder.BIG_ENDIAN).putInt(body.limit()).array();
|
||||
List<ChannelBuffer> components = new ArrayList<ChannelBuffer>(2);
|
||||
List<ChannelBuffer> components = new ArrayList<>(2);
|
||||
components.add(ChannelBuffers.wrappedBuffer(ByteOrder.BIG_ENDIAN, header));
|
||||
components.add(ChannelBuffers.wrappedBuffer(body));
|
||||
|
||||
|
||||
@@ -87,46 +87,42 @@ public class ClientAuthenticationHandler extends SimpleChannelHandler {
|
||||
}
|
||||
}
|
||||
// 鉴权一次性,暂不统计
|
||||
NettyUtils.ack(ctx.getChannel(), new ChannelFutureListener() {
|
||||
NettyUtils.ack(ctx.getChannel(), future -> {
|
||||
logger.info("remove unused channel handlers after authentication is done successfully.");
|
||||
ctx.getPipeline().remove(HandshakeInitializationHandler.class.getName());
|
||||
ctx.getPipeline().remove(ClientAuthenticationHandler.class.getName());
|
||||
|
||||
public void operationComplete(ChannelFuture future) throws Exception {
|
||||
logger.info("remove unused channel handlers after authentication is done successfully.");
|
||||
ctx.getPipeline().remove(HandshakeInitializationHandler.class.getName());
|
||||
ctx.getPipeline().remove(ClientAuthenticationHandler.class.getName());
|
||||
|
||||
int readTimeout = defaultSubscriptorDisconnectIdleTimeout;
|
||||
int writeTimeout = defaultSubscriptorDisconnectIdleTimeout;
|
||||
if (clientAuth.getNetReadTimeout() > 0) {
|
||||
readTimeout = clientAuth.getNetReadTimeout();
|
||||
}
|
||||
if (clientAuth.getNetWriteTimeout() > 0) {
|
||||
writeTimeout = clientAuth.getNetWriteTimeout();
|
||||
}
|
||||
// fix bug: soTimeout parameter's unit from connector is
|
||||
// millseconds.
|
||||
IdleStateHandler idleStateHandler = new IdleStateHandler(NettyUtils.hashedWheelTimer,
|
||||
readTimeout,
|
||||
writeTimeout,
|
||||
0,
|
||||
TimeUnit.MILLISECONDS);
|
||||
ctx.getPipeline().addBefore(SessionHandler.class.getName(),
|
||||
IdleStateHandler.class.getName(),
|
||||
idleStateHandler);
|
||||
|
||||
IdleStateAwareChannelHandler idleStateAwareChannelHandler = new IdleStateAwareChannelHandler() {
|
||||
|
||||
public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception {
|
||||
logger.warn("channel:{} idle timeout exceeds, close channel to save server resources...",
|
||||
ctx.getChannel());
|
||||
ctx.getChannel().close();
|
||||
}
|
||||
|
||||
};
|
||||
ctx.getPipeline().addBefore(SessionHandler.class.getName(),
|
||||
IdleStateAwareChannelHandler.class.getName(),
|
||||
idleStateAwareChannelHandler);
|
||||
int readTimeout = defaultSubscriptorDisconnectIdleTimeout;
|
||||
int writeTimeout = defaultSubscriptorDisconnectIdleTimeout;
|
||||
if (clientAuth.getNetReadTimeout() > 0) {
|
||||
readTimeout = clientAuth.getNetReadTimeout();
|
||||
}
|
||||
if (clientAuth.getNetWriteTimeout() > 0) {
|
||||
writeTimeout = clientAuth.getNetWriteTimeout();
|
||||
}
|
||||
// fix bug: soTimeout parameter's unit from connector is
|
||||
// millseconds.
|
||||
IdleStateHandler idleStateHandler = new IdleStateHandler(NettyUtils.hashedWheelTimer,
|
||||
readTimeout,
|
||||
writeTimeout,
|
||||
0,
|
||||
TimeUnit.MILLISECONDS);
|
||||
ctx.getPipeline().addBefore(SessionHandler.class.getName(),
|
||||
IdleStateHandler.class.getName(),
|
||||
idleStateHandler);
|
||||
|
||||
IdleStateAwareChannelHandler idleStateAwareChannelHandler = new IdleStateAwareChannelHandler() {
|
||||
|
||||
public void channelIdle(ChannelHandlerContext ctx1, IdleStateEvent e1) throws Exception {
|
||||
logger.warn("channel:{} idle timeout exceeds, close channel to save server resources...",
|
||||
ctx1.getChannel());
|
||||
ctx1.getChannel().close();
|
||||
}
|
||||
|
||||
};
|
||||
ctx.getPipeline().addBefore(SessionHandler.class.getName(),
|
||||
IdleStateAwareChannelHandler.class.getName(),
|
||||
idleStateAwareChannelHandler);
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -46,15 +46,11 @@ public class HandshakeInitializationHandler extends SimpleChannelHandler {
|
||||
.build()
|
||||
.toByteArray();
|
||||
|
||||
NettyUtils.write(ctx.getChannel(), body, new ChannelFutureListener() {
|
||||
|
||||
public void operationComplete(ChannelFuture future) throws Exception {
|
||||
ctx.getPipeline().get(HandshakeInitializationHandler.class.getName());
|
||||
ClientAuthenticationHandler handler = (ClientAuthenticationHandler) ctx.getPipeline()
|
||||
.get(ClientAuthenticationHandler.class.getName());
|
||||
handler.setSeed(seed);
|
||||
}
|
||||
|
||||
NettyUtils.write(ctx.getChannel(), body, future -> {
|
||||
ctx.getPipeline().get(HandshakeInitializationHandler.class.getName());
|
||||
ClientAuthenticationHandler handler = (ClientAuthenticationHandler) ctx.getPipeline()
|
||||
.get(ClientAuthenticationHandler.class.getName());
|
||||
handler.setSeed(seed);
|
||||
});
|
||||
logger.info("send handshake initialization packet to : {}", ctx.getChannel());
|
||||
}
|
||||
|
||||
@@ -53,12 +53,9 @@ public class CanalServerTest {
|
||||
@Before
|
||||
public void setUp() {
|
||||
CanalServerWithEmbedded embeddedServer = new CanalServerWithEmbedded();
|
||||
embeddedServer.setCanalInstanceGenerator(new CanalInstanceGenerator() {
|
||||
|
||||
public CanalInstance generate(String destination) {
|
||||
Canal canal = buildCanal();
|
||||
return new CanalInstanceWithManager(canal, FILTER);
|
||||
}
|
||||
embeddedServer.setCanalInstanceGenerator(destination -> {
|
||||
Canal canal = buildCanal();
|
||||
return new CanalInstanceWithManager(canal, FILTER);
|
||||
});
|
||||
|
||||
nettyServer = CanalServerWithNetty.instance();
|
||||
|
||||
@@ -33,12 +33,9 @@ public abstract class BaseCanalServerWithEmbededTest {
|
||||
@Before
|
||||
public void setUp() {
|
||||
server = CanalServerWithEmbedded.instance();
|
||||
server.setCanalInstanceGenerator(new CanalInstanceGenerator() {
|
||||
|
||||
public CanalInstance generate(String destination) {
|
||||
Canal canal = buildCanal();
|
||||
return new CanalInstanceWithManager(canal, FILTER);
|
||||
}
|
||||
server.setCanalInstanceGenerator(destination -> {
|
||||
Canal canal = buildCanal();
|
||||
return new CanalInstanceWithManager(canal, FILTER);
|
||||
});
|
||||
server.start();
|
||||
server.start(DESTINATION);
|
||||
|
||||
Reference in New Issue
Block a user