fixed format & import

This commit is contained in:
agapple
2017-07-05 12:41:11 +08:00
parent 780b4f715b
commit 989a69e7be
9 changed files with 117 additions and 96 deletions

View File

@@ -1,7 +1,6 @@
package com.alibaba.otter.canal.parse.driver.mysql; package com.alibaba.otter.canal.parse.driver.mysql;
import java.io.IOException; import java.io.IOException;
import com.alibaba.otter.canal.parse.driver.mysql.socket.SocketChannel;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -31,9 +30,9 @@ public class MysqlUpdateExecutor {
this.connector = connector; this.connector = connector;
} }
/* public MysqlUpdateExecutor(SocketChannel ch){ /*
this.channel = ch; * public MysqlUpdateExecutor(SocketChannel ch){ this.channel = ch; }
}*/ */
public OKPacket update(String updateString) throws IOException { public OKPacket update(String updateString) throws IOException {
QueryCommandPacket cmd = new QueryCommandPacket(); QueryCommandPacket cmd = new QueryCommandPacket();
@@ -42,7 +41,8 @@ public class MysqlUpdateExecutor {
PacketManager.writeBody(connector.getChannel(), bodyBytes); PacketManager.writeBody(connector.getChannel(), bodyBytes);
logger.debug("read update result..."); logger.debug("read update result...");
byte[] body = PacketManager.readBytes(connector.getChannel(), PacketManager.readHeader(connector.getChannel(), 4).getPacketBodyLength()); byte[] body = PacketManager.readBytes(connector.getChannel(),
PacketManager.readHeader(connector.getChannel(), 4).getPacketBodyLength());
if (body[0] < 0) { if (body[0] < 0) {
ErrorPacket packet = new ErrorPacket(); ErrorPacket packet = new ErrorPacket();
packet.fromBytes(body); packet.fromBytes(body);

View File

@@ -32,14 +32,18 @@ public abstract class SocketChannelPool {
private static Map<Channel, SocketChannel> chManager = new ConcurrentHashMap<Channel, SocketChannel>(); private static Map<Channel, SocketChannel> chManager = new ConcurrentHashMap<Channel, SocketChannel>();
static { static {
boot.group(group).channel(NioSocketChannel.class) boot.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_RCVBUF, 32 * 1024) .option(ChannelOption.SO_RCVBUF, 32 * 1024)
.option(ChannelOption.SO_SNDBUF, 32 * 1024) .option(ChannelOption.SO_SNDBUF, 32 * 1024)
.option(ChannelOption.TCP_NODELAY, true)//如果是延时敏感型应用,建议关闭Nagle算法 .option(ChannelOption.TCP_NODELAY, true)
// 如果是延时敏感型应用,建议关闭Nagle算法
.option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.SO_KEEPALIVE, true)
.option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT) .option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT)
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) // .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
//
.handler(new ChannelInitializer() { .handler(new ChannelInitializer() {
@Override @Override
protected void initChannel(Channel arg0) throws Exception { protected void initChannel(Channel arg0) throws Exception {
arg0.pipeline().addLast(new BusinessHandler());// 命令过滤和handler添加管理 arg0.pipeline().addLast(new BusinessHandler());// 命令过滤和handler添加管理
@@ -50,14 +54,15 @@ public abstract class SocketChannelPool {
public static SocketChannel open(SocketAddress address) throws Exception { public static SocketChannel open(SocketAddress address) throws Exception {
final SocketChannel socket = new SocketChannel(); final SocketChannel socket = new SocketChannel();
boot.connect(address).addListener(new ChannelFutureListener() { boot.connect(address).addListener(new ChannelFutureListener() {
@Override @Override
public void operationComplete(ChannelFuture arg0) throws Exception { public void operationComplete(ChannelFuture arg0) throws Exception {
if(arg0.isSuccess()) if (arg0.isSuccess()) socket.setChannel(arg0.channel());
socket.setChannel(arg0.channel());
synchronized (socket) { synchronized (socket) {
socket.notify(); socket.notify();
} }
}}); }
});
synchronized (socket) { synchronized (socket) {
socket.wait(); socket.wait();
} }
@@ -69,25 +74,26 @@ public abstract class SocketChannelPool {
} }
public static class BusinessHandler extends ChannelInboundHandlerAdapter { public static class BusinessHandler extends ChannelInboundHandlerAdapter {
private SocketChannel socket = null; private SocketChannel socket = null;
@Override @Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception { public void channelInactive(ChannelHandlerContext ctx) throws Exception {
socket.setChannel(null); socket.setChannel(null);
chManager.remove(ctx.channel());// 移除 chManager.remove(ctx.channel());// 移除
} }
@Override @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
throws Exception { if (null == socket) socket = chManager.get(ctx.channel());
if(null == socket)
socket = chManager.get(ctx.channel());
if (socket != null) { if (socket != null) {
socket.writeCache((ByteBuf) msg); socket.writeCache((ByteBuf) msg);
} }
ReferenceCountUtil.release(msg);// 添加防止内存泄漏的 ReferenceCountUtil.release(msg);// 添加防止内存泄漏的
} }
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
throws Exception {
ctx.close(); ctx.close();
} }
} }

View File

@@ -2,7 +2,6 @@ package com.alibaba.otter.canal.instance.core;
import java.util.List; import java.util.List;
import com.alibaba.otter.canal.parse.index.CanalLogPositionManager;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -17,6 +16,7 @@ import com.alibaba.otter.canal.parse.ha.HeartBeatHAController;
import com.alibaba.otter.canal.parse.inbound.AbstractEventParser; import com.alibaba.otter.canal.parse.inbound.AbstractEventParser;
import com.alibaba.otter.canal.parse.inbound.group.GroupEventParser; import com.alibaba.otter.canal.parse.inbound.group.GroupEventParser;
import com.alibaba.otter.canal.parse.inbound.mysql.MysqlEventParser; import com.alibaba.otter.canal.parse.inbound.mysql.MysqlEventParser;
import com.alibaba.otter.canal.parse.index.CanalLogPositionManager;
import com.alibaba.otter.canal.protocol.CanalEntry; import com.alibaba.otter.canal.protocol.CanalEntry;
import com.alibaba.otter.canal.protocol.ClientIdentity; import com.alibaba.otter.canal.protocol.ClientIdentity;
import com.alibaba.otter.canal.sink.CanalEventSink; import com.alibaba.otter.canal.sink.CanalEventSink;

View File

@@ -6,7 +6,6 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import com.alibaba.otter.canal.parse.index.*;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -38,6 +37,12 @@ import com.alibaba.otter.canal.parse.inbound.AbstractEventParser;
import com.alibaba.otter.canal.parse.inbound.group.GroupEventParser; import com.alibaba.otter.canal.parse.inbound.group.GroupEventParser;
import com.alibaba.otter.canal.parse.inbound.mysql.LocalBinlogEventParser; import com.alibaba.otter.canal.parse.inbound.mysql.LocalBinlogEventParser;
import com.alibaba.otter.canal.parse.inbound.mysql.MysqlEventParser; import com.alibaba.otter.canal.parse.inbound.mysql.MysqlEventParser;
import com.alibaba.otter.canal.parse.index.CanalLogPositionManager;
import com.alibaba.otter.canal.parse.index.FailbackLogPositionManager;
import com.alibaba.otter.canal.parse.index.MemoryLogPositionManager;
import com.alibaba.otter.canal.parse.index.MetaLogPositionManager;
import com.alibaba.otter.canal.parse.index.PeriodMixedLogPositionManager;
import com.alibaba.otter.canal.parse.index.ZooKeeperLogPositionManager;
import com.alibaba.otter.canal.parse.support.AuthenticationInfo; import com.alibaba.otter.canal.parse.support.AuthenticationInfo;
import com.alibaba.otter.canal.protocol.position.EntryPosition; import com.alibaba.otter.canal.protocol.position.EntryPosition;
import com.alibaba.otter.canal.sink.entry.EntryEventSink; import com.alibaba.otter.canal.sink.entry.EntryEventSink;

View File

@@ -4,11 +4,11 @@ import java.io.IOException;
import java.io.InterruptedIOException; import java.io.InterruptedIOException;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.nio.channels.ClosedByInterruptException; import java.nio.channels.ClosedByInterruptException;
import com.alibaba.otter.canal.parse.driver.mysql.socket.SocketChannel;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.alibaba.otter.canal.parse.driver.mysql.socket.SocketChannel;
import com.taobao.tddl.dbsync.binlog.LogFetcher; import com.taobao.tddl.dbsync.binlog.LogFetcher;
/** /**

View File

@@ -30,14 +30,18 @@ public class PeriodMixedLogPositionManagerTest extends AbstractLogPositionManage
MemoryLogPositionManager memoryLogPositionManager = new MemoryLogPositionManager(); MemoryLogPositionManager memoryLogPositionManager = new MemoryLogPositionManager();
ZooKeeperLogPositionManager zookeeperLogPositionManager = new ZooKeeperLogPositionManager(zkclientx); ZooKeeperLogPositionManager zookeeperLogPositionManager = new ZooKeeperLogPositionManager(zkclientx);
PeriodMixedLogPositionManager logPositionManager = new PeriodMixedLogPositionManager(memoryLogPositionManager, zookeeperLogPositionManager, 1000L); PeriodMixedLogPositionManager logPositionManager = new PeriodMixedLogPositionManager(memoryLogPositionManager,
zookeeperLogPositionManager,
1000L);
logPositionManager.start(); logPositionManager.start();
LogPosition position2 = doTest(logPositionManager); LogPosition position2 = doTest(logPositionManager);
sleep(1500); sleep(1500);
PeriodMixedLogPositionManager logPositionManager2 = new PeriodMixedLogPositionManager(memoryLogPositionManager, zookeeperLogPositionManager, 1000L); PeriodMixedLogPositionManager logPositionManager2 = new PeriodMixedLogPositionManager(memoryLogPositionManager,
zookeeperLogPositionManager,
1000L);
logPositionManager2.start(); logPositionManager2.start();
LogPosition getPosition2 = logPositionManager2.getLatestIndexBy(destination); LogPosition getPosition2 = logPositionManager2.getLatestIndexBy(destination);

View File

@@ -34,7 +34,10 @@ public class CanalServerWithNetty extends AbstractCanalLifeCycle implements Cana
private int port; private int port;
private Channel serverChannel = null; private Channel serverChannel = null;
private ServerBootstrap bootstrap = null; private ServerBootstrap bootstrap = null;
private ChannelGroup childGroups = null; //socket channel container, used to close sockets explicitly. private ChannelGroup childGroups = null; // socket channel
// container, used to
// close sockets
// explicitly.
private static class SingletonHolder { private static class SingletonHolder {
@@ -60,12 +63,10 @@ public class CanalServerWithNetty extends AbstractCanalLifeCycle implements Cana
this.bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), this.bootstrap = new ServerBootstrap(new NioServerSocketChannelFactory(Executors.newCachedThreadPool(),
Executors.newCachedThreadPool())); Executors.newCachedThreadPool()));
/* /*
* enable keep-alive mechanism, handle abnormal network connection scenarios on OS level. * enable keep-alive mechanism, handle abnormal network connection
* the threshold parameters are depended on OS. * scenarios on OS level. the threshold parameters are depended on OS.
* e.g. On Linux: * e.g. On Linux: net.ipv4.tcp_keepalive_time = 300
* net.ipv4.tcp_keepalive_time = 300 * net.ipv4.tcp_keepalive_probes = 2 net.ipv4.tcp_keepalive_intvl = 30
* net.ipv4.tcp_keepalive_probes = 2
* net.ipv4.tcp_keepalive_intvl = 30
*/ */
bootstrap.setOption("child.keepAlive", true); bootstrap.setOption("child.keepAlive", true);
/* /*
@@ -80,7 +81,8 @@ public class CanalServerWithNetty extends AbstractCanalLifeCycle implements Cana
ChannelPipeline pipelines = Channels.pipeline(); ChannelPipeline pipelines = Channels.pipeline();
pipelines.addLast(FixedHeaderFrameDecoder.class.getName(), new FixedHeaderFrameDecoder()); pipelines.addLast(FixedHeaderFrameDecoder.class.getName(), new FixedHeaderFrameDecoder());
// support to maintain child socket channel. // support to maintain child socket channel.
pipelines.addLast(HandshakeInitializationHandler.class.getName(), new HandshakeInitializationHandler(childGroups)); pipelines.addLast(HandshakeInitializationHandler.class.getName(),
new HandshakeInitializationHandler(childGroups));
pipelines.addLast(ClientAuthenticationHandler.class.getName(), pipelines.addLast(ClientAuthenticationHandler.class.getName(),
new ClientAuthenticationHandler(embeddedServer)); new ClientAuthenticationHandler(embeddedServer));
@@ -105,7 +107,8 @@ public class CanalServerWithNetty extends AbstractCanalLifeCycle implements Cana
this.serverChannel.close().awaitUninterruptibly(1000); this.serverChannel.close().awaitUninterruptibly(1000);
} }
//close sockets explicitly to reduce socket channel hung in complicated network environment. // close sockets explicitly to reduce socket channel hung in complicated
// network environment.
if (this.childGroups != null) { if (this.childGroups != null) {
this.childGroups.close().awaitUninterruptibly(5000); this.childGroups.close().awaitUninterruptibly(5000);
} }

View File

@@ -89,11 +89,13 @@ public class ClientAuthenticationHandler extends SimpleChannelHandler {
if (clientAuth.getNetWriteTimeout() > 0) { if (clientAuth.getNetWriteTimeout() > 0) {
writeTimeout = clientAuth.getNetWriteTimeout(); writeTimeout = clientAuth.getNetWriteTimeout();
} }
//fix bug: soTimeout parameter's unit from connector is millseconds. // fix bug: soTimeout parameter's unit from connector is
// millseconds.
IdleStateHandler idleStateHandler = new IdleStateHandler(NettyUtils.hashedWheelTimer, IdleStateHandler idleStateHandler = new IdleStateHandler(NettyUtils.hashedWheelTimer,
readTimeout, readTimeout,
writeTimeout, writeTimeout,
0, TimeUnit.MILLISECONDS); 0,
TimeUnit.MILLISECONDS);
ctx.getPipeline().addBefore(SessionHandler.class.getName(), ctx.getPipeline().addBefore(SessionHandler.class.getName(),
IdleStateHandler.class.getName(), IdleStateHandler.class.getName(),
idleStateHandler); idleStateHandler);

View File

@@ -19,6 +19,7 @@ import com.alibaba.otter.canal.server.netty.NettyUtils;
* @version 1.0.0 * @version 1.0.0
*/ */
public class HandshakeInitializationHandler extends SimpleChannelHandler { public class HandshakeInitializationHandler extends SimpleChannelHandler {
// support to maintain socket channel. // support to maintain socket channel.
private ChannelGroup childGroups; private ChannelGroup childGroups;