mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
format & import
This commit is contained in:
@@ -140,7 +140,7 @@ public class MysqlConnector {
|
||||
HeaderPacket quitHeader = new HeaderPacket();
|
||||
quitHeader.setPacketBodyLength(cmdBody.length);
|
||||
quitHeader.setPacketSequenceNumber((byte) 0x00);
|
||||
PacketManager.write(channel,quitHeader.toBytes(),cmdBody);
|
||||
PacketManager.write(channel, quitHeader.toBytes(), cmdBody);
|
||||
}
|
||||
|
||||
private void negotiate(SocketChannel channel) throws IOException {
|
||||
@@ -177,7 +177,7 @@ public class MysqlConnector {
|
||||
h.setPacketBodyLength(clientAuthPkgBody.length);
|
||||
h.setPacketSequenceNumber((byte) (header.getPacketSequenceNumber() + 1));
|
||||
|
||||
PacketManager.write(channel,h.toBytes(), clientAuthPkgBody);
|
||||
PacketManager.write(channel, h.toBytes(), clientAuthPkgBody);
|
||||
logger.info("client authentication packet is sent out.");
|
||||
|
||||
// check auth result
|
||||
|
||||
@@ -82,9 +82,9 @@ public class MysqlQueryExecutor {
|
||||
rowDataPacket.fromBytes(body);
|
||||
rowData.add(rowDataPacket);
|
||||
}
|
||||
//未知,不知道是否需要锁定
|
||||
//channel.lock();//锁定读
|
||||
|
||||
// 未知,不知道是否需要锁定
|
||||
// channel.lock();//锁定读
|
||||
|
||||
ResultSetPacket resultSet = new ResultSetPacket();
|
||||
resultSet.getFieldDescriptors().addAll(fields);
|
||||
for (RowDataPacket r : rowData) {
|
||||
|
||||
@@ -48,7 +48,7 @@ public class MysqlUpdateExecutor {
|
||||
packet.fromBytes(body);
|
||||
throw new IOException(packet + "\n with command: " + updateString);
|
||||
}
|
||||
//channel.lock();//锁定读
|
||||
// channel.lock();//锁定读
|
||||
|
||||
OKPacket packet = new OKPacket();
|
||||
packet.fromBytes(body);
|
||||
|
||||
@@ -74,7 +74,7 @@ public class FieldPacket extends PacketWithHeaderPacket {
|
||||
this.decimals = data[index];
|
||||
index++;
|
||||
//
|
||||
index += 2;//skip filter
|
||||
index += 2;// skip filter
|
||||
//
|
||||
if (index < data.length) {
|
||||
reader.setIndex(index);
|
||||
|
||||
@@ -10,69 +10,77 @@ import java.net.SocketAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* @author luoyaogui
|
||||
* 封装netty的通信channel和数据接收缓存,实现读、写、连接校验的功能。
|
||||
* 2016-12-28
|
||||
* @author luoyaogui 封装netty的通信channel和数据接收缓存,实现读、写、连接校验的功能。 2016-12-28
|
||||
*/
|
||||
public class SocketChannel {
|
||||
private Channel channel = null;
|
||||
private Object lock = new Object();
|
||||
private ByteBuf cache = PooledByteBufAllocator.DEFAULT.directBuffer(1024*1024*5);//缓存大小
|
||||
|
||||
public Channel getChannel() {
|
||||
return channel;
|
||||
}
|
||||
public void setChannel(Channel channel,boolean notify) {
|
||||
this.channel = channel;
|
||||
if(notify){//是否需要通知,主要时channel不可用时
|
||||
synchronized(this){
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
private Channel channel = null;
|
||||
private Object lock = new Object();
|
||||
private ByteBuf cache = PooledByteBufAllocator.DEFAULT.directBuffer(1024 * 1024 * 5); // 缓存大小
|
||||
|
||||
public void writeCache(ByteBuf buf){
|
||||
synchronized (lock) {
|
||||
cache.discardReadBytes();//回收内存
|
||||
cache.writeBytes(buf);
|
||||
}
|
||||
synchronized(this){
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
public void writeChannel(byte[]... buf) throws IOException {
|
||||
if(channel != null && channel.isWritable())
|
||||
channel.writeAndFlush(Unpooled.copiedBuffer(buf));
|
||||
else
|
||||
throw new IOException("write failed ! please checking !");
|
||||
}
|
||||
public int read(ByteBuffer buffer) throws IOException {
|
||||
if(null == channel)
|
||||
throw new IOException("socket has Interrupted !");
|
||||
if(cache.readableBytes() < buffer.remaining()){
|
||||
synchronized(this){
|
||||
try {wait();} catch (InterruptedException e) {}
|
||||
}
|
||||
}else{
|
||||
synchronized (lock){
|
||||
cache.readBytes(buffer);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
public boolean isConnected() {
|
||||
return channel!=null?true:false;
|
||||
}
|
||||
public SocketAddress getRemoteSocketAddress(){
|
||||
return channel!=null?channel.remoteAddress():null;
|
||||
}
|
||||
public void close(){
|
||||
if(channel != null){
|
||||
channel.close();
|
||||
}
|
||||
channel = null;
|
||||
cache.discardReadBytes();//回收已占用的内存
|
||||
cache.release();//释放整个内存
|
||||
cache = null;
|
||||
}
|
||||
public Channel getChannel() {
|
||||
return channel;
|
||||
}
|
||||
|
||||
public void setChannel(Channel channel, boolean notify) {
|
||||
this.channel = channel;
|
||||
if (notify) {// 是否需要通知,主要时channel不可用时
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void writeCache(ByteBuf buf) {
|
||||
synchronized (lock) {
|
||||
cache.discardReadBytes();// 回收内存
|
||||
cache.writeBytes(buf);
|
||||
}
|
||||
synchronized (this) {
|
||||
notifyAll();
|
||||
}
|
||||
}
|
||||
|
||||
public void writeChannel(byte[]... buf) throws IOException {
|
||||
if (channel != null && channel.isWritable()) {
|
||||
channel.writeAndFlush(Unpooled.copiedBuffer(buf));
|
||||
} else {
|
||||
throw new IOException("write failed ! please checking !");
|
||||
}
|
||||
}
|
||||
|
||||
public int read(ByteBuffer buffer) throws IOException {
|
||||
if (null == channel) throw new IOException("socket has Interrupted !");
|
||||
if (cache.readableBytes() < buffer.remaining()) {
|
||||
synchronized (this) {
|
||||
try {
|
||||
wait();
|
||||
} catch (InterruptedException e) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
synchronized (lock) {
|
||||
cache.readBytes(buffer);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public boolean isConnected() {
|
||||
return channel != null ? true : false;
|
||||
}
|
||||
|
||||
public SocketAddress getRemoteSocketAddress() {
|
||||
return channel != null ? channel.remoteAddress() : null;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
if (channel != null) {
|
||||
channel.close();
|
||||
}
|
||||
channel = null;
|
||||
cache.discardReadBytes();// 回收已占用的内存
|
||||
cache.release();// 释放整个内存
|
||||
cache = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,76 +20,80 @@ import java.io.IOException;
|
||||
import java.net.SocketAddress;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* @author luoyaogui
|
||||
* 实现channel的管理(监听连接、读数据、回收)
|
||||
* 2016-12-28
|
||||
* @author luoyaogui 实现channel的管理(监听连接、读数据、回收) 2016-12-28
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public abstract class SocketChannelPool {
|
||||
|
||||
private static EventLoopGroup group = new NioEventLoopGroup();//非阻塞IO线程组
|
||||
private static Bootstrap boot = new Bootstrap();//主
|
||||
private static Map<Channel,SocketChannel> chManager = new ConcurrentHashMap<Channel,SocketChannel>();
|
||||
|
||||
static{
|
||||
boot.group(group).channel(NioSocketChannel.class)
|
||||
.option(ChannelOption.SO_RCVBUF, 32*1024)
|
||||
.option(ChannelOption.SO_SNDBUF, 32*1024)
|
||||
.option(ChannelOption.TCP_NODELAY, true)//如果是延时敏感型应用,建议关闭Nagle算法
|
||||
.option(ChannelOption.SO_KEEPALIVE, true)
|
||||
.option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT)
|
||||
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) //
|
||||
.handler(new ChannelInitializer(){
|
||||
@Override
|
||||
protected void initChannel(Channel arg0) throws Exception {
|
||||
arg0.pipeline().addLast(new BusinessHandler());//命令过滤和handler添加管理
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static SocketChannel open(SocketAddress address) throws Exception {
|
||||
final SocketChannel socket = new SocketChannel();
|
||||
boot.connect(address).addListener(new ChannelFutureListener(){
|
||||
@Override
|
||||
public void operationComplete(ChannelFuture arg0) throws Exception {
|
||||
if(arg0.isSuccess())
|
||||
socket.setChannel(arg0.channel(),false);
|
||||
synchronized (socket) {
|
||||
socket.notify();
|
||||
}
|
||||
}});
|
||||
synchronized (socket) {
|
||||
socket.wait();
|
||||
}
|
||||
if(null == socket.getChannel()){
|
||||
throw new IOException("can't create socket!");
|
||||
}
|
||||
chManager.put(socket.getChannel(), socket);
|
||||
return socket;
|
||||
}
|
||||
private static EventLoopGroup group = new NioEventLoopGroup(); // 非阻塞IO线程组
|
||||
private static Bootstrap boot = new Bootstrap(); // 主
|
||||
private static Map<Channel, SocketChannel> chManager = new ConcurrentHashMap<Channel, SocketChannel>();
|
||||
|
||||
public static class BusinessHandler extends ChannelInboundHandlerAdapter {
|
||||
private SocketChannel socket=null;
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
socket.setChannel(null,true);
|
||||
chManager.remove(ctx.channel());//移除
|
||||
}
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg)
|
||||
throws Exception {
|
||||
if(null == socket)
|
||||
socket = chManager.get(ctx.channel());
|
||||
if(socket != null){
|
||||
socket.writeCache((ByteBuf) msg);
|
||||
}
|
||||
ReferenceCountUtil.release(msg);//添加防止内存泄漏的
|
||||
}
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
|
||||
throws Exception {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
static {
|
||||
boot.group(group)
|
||||
.channel(NioSocketChannel.class)
|
||||
.option(ChannelOption.SO_RCVBUF, 32 * 1024)
|
||||
.option(ChannelOption.SO_SNDBUF, 32 * 1024)
|
||||
.option(ChannelOption.TCP_NODELAY, true)
|
||||
// 如果是延时敏感型应用,建议关闭Nagle算法
|
||||
.option(ChannelOption.SO_KEEPALIVE, true)
|
||||
.option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT)
|
||||
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
|
||||
//
|
||||
.handler(new ChannelInitializer() {
|
||||
|
||||
@Override
|
||||
protected void initChannel(Channel arg0) throws Exception {
|
||||
arg0.pipeline().addLast(new BusinessHandler());// 命令过滤和handler添加管理
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static SocketChannel open(SocketAddress address) throws Exception {
|
||||
final SocketChannel socket = new SocketChannel();
|
||||
boot.connect(address).addListener(new ChannelFutureListener() {
|
||||
|
||||
@Override
|
||||
public void operationComplete(ChannelFuture arg0) throws Exception {
|
||||
if (arg0.isSuccess()) socket.setChannel(arg0.channel(), false);
|
||||
synchronized (socket) {
|
||||
socket.notify();
|
||||
}
|
||||
}
|
||||
});
|
||||
synchronized (socket) {
|
||||
socket.wait();
|
||||
}
|
||||
if (null == socket.getChannel()) {
|
||||
throw new IOException("can't create socket!");
|
||||
}
|
||||
chManager.put(socket.getChannel(), socket);
|
||||
return socket;
|
||||
}
|
||||
|
||||
public static class BusinessHandler extends ChannelInboundHandlerAdapter {
|
||||
|
||||
private SocketChannel socket = null;
|
||||
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
socket.setChannel(null, true);
|
||||
chManager.remove(ctx.channel());// 移除
|
||||
}
|
||||
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
if (null == socket) {
|
||||
socket = chManager.get(ctx.channel());
|
||||
}
|
||||
if (socket != null) {
|
||||
socket.writeCache((ByteBuf) msg);
|
||||
}
|
||||
ReferenceCountUtil.release(msg);// 添加防止内存泄漏的
|
||||
}
|
||||
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
ctx.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +52,6 @@ public abstract class PacketManager {
|
||||
HeaderPacket header = new HeaderPacket();
|
||||
header.setPacketBodyLength(body.length);
|
||||
header.setPacketSequenceNumber(packetSeqNumber);
|
||||
write(ch, header.toBytes(),body);
|
||||
write(ch, header.toBytes(), body);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket;
|
||||
|
||||
Reference in New Issue
Block a user