mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
Compare commits
108 Commits
canal-1.0.
...
canal-1.0.
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
89eb3d6078 | ||
|
|
7b52568575 | ||
|
|
d520260fe8 | ||
|
|
77c1c65b83 | ||
|
|
fe38bc16d2 | ||
|
|
b4c6490c94 | ||
|
|
67f2e55d38 | ||
|
|
b9d7f0b6d0 | ||
|
|
b880d96a86 | ||
|
|
b9156936ae | ||
|
|
617f9bbc62 | ||
|
|
b19eaeb89a | ||
|
|
5582a38930 | ||
|
|
c6eee47af6 | ||
|
|
10bb800f85 | ||
|
|
89726a6365 | ||
|
|
8e856acac5 | ||
|
|
df8fe4a9eb | ||
|
|
33a2a6121a | ||
|
|
441d2d2449 | ||
|
|
bef6a66eaf | ||
|
|
c1805a4831 | ||
|
|
38eecd9aef | ||
|
|
cef431c8af | ||
|
|
3efd589cb1 | ||
|
|
92c570e967 | ||
|
|
cf7af17aec | ||
|
|
3308bf0662 | ||
|
|
be605617c3 | ||
|
|
bb8fec963b | ||
|
|
da8bf59d5b | ||
|
|
54168af790 | ||
|
|
f0bed4c762 | ||
|
|
4d4d97419c | ||
|
|
41eef9a090 | ||
|
|
9ae6c0c863 | ||
|
|
5f47809c8e | ||
|
|
f2e3ff2dae | ||
|
|
92778be5be | ||
|
|
5da8d391f8 | ||
|
|
f867b183ab | ||
|
|
ea555df315 | ||
|
|
4af1e5fb84 | ||
|
|
bf02db4cbc | ||
|
|
967f8d6b0f | ||
|
|
4f4b016da3 | ||
|
|
a4255a6377 | ||
|
|
dca1b20043 | ||
|
|
025d36604b | ||
|
|
e23a19cd85 | ||
|
|
593902d349 | ||
|
|
d014167226 | ||
|
|
df668a6f72 | ||
|
|
cc5eb1075b | ||
|
|
19f4c5ae1b | ||
|
|
4e41ebf746 | ||
|
|
31cbdb1824 | ||
|
|
1b6efdf463 | ||
|
|
cb6c7ef3e8 | ||
|
|
4bd2a79905 | ||
|
|
345e22bd2c | ||
|
|
9964bb75b1 | ||
|
|
ec072a45d8 | ||
|
|
efb7b32694 | ||
|
|
74bc6368f2 | ||
|
|
b96e4dd23a | ||
|
|
d991e6be60 | ||
|
|
e7846085fc | ||
|
|
6faa3fd586 | ||
|
|
fa34eb0e59 | ||
|
|
3e97e6b771 | ||
|
|
330da50a5e | ||
|
|
e35a06743c | ||
|
|
0d6757c257 | ||
|
|
e4cde2f6a1 | ||
|
|
9b7d62717e | ||
|
|
b306bde3ec | ||
|
|
31f72e1aac | ||
|
|
ad201660a6 | ||
|
|
77f1dfd324 | ||
|
|
11467fca2a | ||
|
|
cc8d5b4263 | ||
|
|
4022aa8b5a | ||
|
|
187b98fbda | ||
|
|
0f33c8c89c | ||
|
|
b6967dc0dc | ||
|
|
b734a32ae6 | ||
|
|
1f16310b7d | ||
|
|
5349a5c49b | ||
|
|
a2241e2521 | ||
|
|
fedc07352b | ||
|
|
e251de79f3 | ||
|
|
7076e3124d | ||
|
|
fca7da6bba | ||
|
|
57c2cc4c4e | ||
|
|
3c913d1e22 | ||
|
|
4141049bb9 | ||
|
|
912a7a12be | ||
|
|
5c2ed38d2f | ||
|
|
c8f1d54fa1 | ||
|
|
e0fa8e0f72 | ||
|
|
7eb6fb3e8a | ||
|
|
debfdb8873 | ||
|
|
eda7e4ca2b | ||
|
|
70e6bdde02 | ||
|
|
21f3f8c973 | ||
|
|
f46133d116 | ||
|
|
9b7de00efb |
@@ -5,6 +5,7 @@
|
|||||||
<ol>
|
<ol>
|
||||||
<li>canal QQ讨论群已经建立,群号:161559791 ,欢迎加入进行技术讨论。</li>
|
<li>canal QQ讨论群已经建立,群号:161559791 ,欢迎加入进行技术讨论。</li>
|
||||||
<li>canal消费端项目开源: Otter(分布式数据库同步系统),地址:<a href="https://github.com/alibaba/otter">https://github.com/alibaba/otter</a></li>
|
<li>canal消费端项目开源: Otter(分布式数据库同步系统),地址:<a href="https://github.com/alibaba/otter">https://github.com/alibaba/otter</a></li>
|
||||||
|
<li>Canal已在阿里云推出商业化版本 <a href="https://www.aliyun.com/product/dts?spm=a2c4g.11186623.cloudEssentials.80.srdwr7">数据传输服务DTS</a>, 开通即用,免去部署维护的昂贵使用成本。DTS针对阿里云RDS、DRDS等产品进行了适配,解决了Binlog日志回收,主备切换、VPC网络切换等场景下的订阅高可用问题。同时,针对RDS进行了针对性的性能优化。出于稳定性、性能及成本的考虑,强烈推荐阿里云用户使用DTS产品。<a href="https://help.aliyun.com/document_detail/26592.html?spm=a2c4g.11174283.6.539.t1Y91E">DTS产品使用文档</a></li>
|
||||||
</ol>
|
</ol>
|
||||||
|
|
||||||
<h1>背景</h1>
|
<h1>背景</h1>
|
||||||
@@ -73,6 +74,14 @@ See the wiki page for : <a href="https://github.com/alibaba/canal/wiki" >wiki文
|
|||||||
<li>阿里巴巴去Oracle数据迁移同步工具(目标支持MySQL/DRDS):<a href="http://github.com/alibaba/yugong">http://github.com/alibaba/yugong</a></li>
|
<li>阿里巴巴去Oracle数据迁移同步工具(目标支持MySQL/DRDS):<a href="http://github.com/alibaba/yugong">http://github.com/alibaba/yugong</a></li>
|
||||||
</ol>
|
</ol>
|
||||||
|
|
||||||
|
<h1>相关产品</h1>
|
||||||
|
<ol>
|
||||||
|
<li><a href="https://www.aliyun.com/product/drds?spm=5176.55326.cloudEssentials.71.69fd227dRPZj9K">阿里云分布式数据库DRDS</a></li>
|
||||||
|
<li><a href="https://www.aliyun.com/product/dts?spm=5176.7947010.cloudEssentials.80.33f734f4JOAxSP">阿里云数据传输服务DTS</a></li>
|
||||||
|
<li><a href="https://www.aliyun.com/product/dbs?spm=5176.54487.cloudEssentials.83.34b851a8GmVZg6">阿里云数据库备份服务DBS</a></li>
|
||||||
|
<li><a href="https://www.aliyun.com/product/dms?spm=5176.169464.cloudEssentials.81.2e1066feC1sBBL">阿里云数据管理服务DMS</a></li>
|
||||||
|
</ol>
|
||||||
|
|
||||||
<h1>问题反馈</h1>
|
<h1>问题反馈</h1>
|
||||||
<ol>
|
<ol>
|
||||||
<li>qq交流群: 161559791 </li>
|
<li>qq交流群: 161559791 </li>
|
||||||
|
|||||||
@@ -150,4 +150,10 @@ public interface CanalConnector {
|
|||||||
*/
|
*/
|
||||||
void rollback() throws CanalClientException;
|
void rollback() throws CanalClientException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 中断的阻塞,用于优雅停止client
|
||||||
|
*
|
||||||
|
* @throws CanalClientException
|
||||||
|
*/
|
||||||
|
void stopRunning() throws CanalClientException;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,8 @@ public class CanalConnectors {
|
|||||||
public static CanalConnector newSingleConnector(SocketAddress address, String destination, String username,
|
public static CanalConnector newSingleConnector(SocketAddress address, String destination, String username,
|
||||||
String password) {
|
String password) {
|
||||||
SimpleCanalConnector canalConnector = new SimpleCanalConnector(address, username, password, destination);
|
SimpleCanalConnector canalConnector = new SimpleCanalConnector(address, username, password, destination);
|
||||||
canalConnector.setSoTimeout(30 * 1000);
|
canalConnector.setSoTimeout(60 * 1000);
|
||||||
|
canalConnector.setIdleTimeout(60 * 60 * 1000);
|
||||||
return canalConnector;
|
return canalConnector;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +49,8 @@ public class CanalConnectors {
|
|||||||
password,
|
password,
|
||||||
destination,
|
destination,
|
||||||
new SimpleNodeAccessStrategy(addresses));
|
new SimpleNodeAccessStrategy(addresses));
|
||||||
canalConnector.setSoTimeout(30 * 1000);
|
canalConnector.setSoTimeout(60 * 1000);
|
||||||
|
canalConnector.setIdleTimeout(60 * 60 * 1000);
|
||||||
return canalConnector;
|
return canalConnector;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,7 +69,8 @@ public class CanalConnectors {
|
|||||||
password,
|
password,
|
||||||
destination,
|
destination,
|
||||||
new ClusterNodeAccessStrategy(destination, ZkClientx.getZkClient(zkServers)));
|
new ClusterNodeAccessStrategy(destination, ZkClientx.getZkClient(zkServers)));
|
||||||
canalConnector.setSoTimeout(30 * 1000);
|
canalConnector.setSoTimeout(60 * 1000);
|
||||||
|
canalConnector.setIdleTimeout(60 * 60 * 1000);
|
||||||
return canalConnector;
|
return canalConnector;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,8 +22,9 @@ public class ClusterCanalConnector implements CanalConnector {
|
|||||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||||
private String username;
|
private String username;
|
||||||
private String password;
|
private String password;
|
||||||
private int soTimeout = 10000;
|
private int soTimeout = 60000;
|
||||||
private int retryTimes = 3;
|
private int idleTimeout = 60 * 60 * 1000;
|
||||||
|
private int retryTimes = 3; // 设置-1时可以subscribe阻塞等待时优雅停机
|
||||||
private int retryInterval = 5000; // 重试的时间间隔,默认5秒
|
private int retryInterval = 5000; // 重试的时间间隔,默认5秒
|
||||||
private CanalNodeAccessStrategy accessStrategy;
|
private CanalNodeAccessStrategy accessStrategy;
|
||||||
private SimpleCanalConnector currentConnector;
|
private SimpleCanalConnector currentConnector;
|
||||||
@@ -52,6 +53,7 @@ public class ClusterCanalConnector implements CanalConnector {
|
|||||||
|
|
||||||
};
|
};
|
||||||
currentConnector.setSoTimeout(soTimeout);
|
currentConnector.setSoTimeout(soTimeout);
|
||||||
|
currentConnector.setIdleTimeout(idleTimeout);
|
||||||
if (filter != null) {
|
if (filter != null) {
|
||||||
currentConnector.setFilter(filter);
|
currentConnector.setFilter(filter);
|
||||||
}
|
}
|
||||||
@@ -106,14 +108,18 @@ public class ClusterCanalConnector implements CanalConnector {
|
|||||||
this.filter = filter;
|
this.filter = filter;
|
||||||
return;
|
return;
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
logger.warn(String.format(
|
if (retryTimes == -1 && t.getCause() instanceof InterruptedException) {
|
||||||
"something goes wrong when subscribing from server: %s",
|
logger.info("block waiting interrupted by other thread.");
|
||||||
currentConnector != null ? currentConnector.getAddress() : "null"),
|
return;
|
||||||
t);
|
} else {
|
||||||
|
logger.warn(String.format("something goes wrong when subscribing from server: %s",
|
||||||
|
currentConnector != null ? currentConnector.getAddress() : "null"), t);
|
||||||
times++;
|
times++;
|
||||||
restart();
|
restart();
|
||||||
logger.info("restart the connector for next round retry.");
|
logger.info("restart the connector for next round retry.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new CanalClientException("failed to subscribe after " + times + " times retry.");
|
throw new CanalClientException("failed to subscribe after " + times + " times retry.");
|
||||||
@@ -178,7 +184,7 @@ public class ClusterCanalConnector implements CanalConnector {
|
|||||||
return msg;
|
return msg;
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
logger.warn(String.format("something goes wrong when getWithoutAck data from server:%s",
|
logger.warn(String.format("something goes wrong when getWithoutAck data from server:%s",
|
||||||
currentConnector.getAddress()), t);
|
currentConnector != null ? currentConnector.getAddress() : "null"), t);
|
||||||
times++;
|
times++;
|
||||||
restart();
|
restart();
|
||||||
logger.info("restart the connector for next round retry.");
|
logger.info("restart the connector for next round retry.");
|
||||||
@@ -195,7 +201,7 @@ public class ClusterCanalConnector implements CanalConnector {
|
|||||||
return msg;
|
return msg;
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
logger.warn(String.format("something goes wrong when getWithoutAck data from server:%s",
|
logger.warn(String.format("something goes wrong when getWithoutAck data from server:%s",
|
||||||
currentConnector.getAddress()), t);
|
currentConnector != null ? currentConnector.getAddress() : "null"), t);
|
||||||
times++;
|
times++;
|
||||||
restart();
|
restart();
|
||||||
logger.info("restart the connector for next round retry.");
|
logger.info("restart the connector for next round retry.");
|
||||||
@@ -212,7 +218,8 @@ public class ClusterCanalConnector implements CanalConnector {
|
|||||||
return;
|
return;
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
logger.warn(String.format("something goes wrong when rollbacking data from server:%s",
|
logger.warn(String.format("something goes wrong when rollbacking data from server:%s",
|
||||||
currentConnector.getAddress()), t);
|
currentConnector != null ? currentConnector.getAddress() : "null"),
|
||||||
|
t);
|
||||||
times++;
|
times++;
|
||||||
restart();
|
restart();
|
||||||
logger.info("restart the connector for next round retry.");
|
logger.info("restart the connector for next round retry.");
|
||||||
@@ -229,7 +236,8 @@ public class ClusterCanalConnector implements CanalConnector {
|
|||||||
return;
|
return;
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
logger.warn(String.format("something goes wrong when rollbacking data from server:%s",
|
logger.warn(String.format("something goes wrong when rollbacking data from server:%s",
|
||||||
currentConnector.getAddress()), t);
|
currentConnector != null ? currentConnector.getAddress() : "null"),
|
||||||
|
t);
|
||||||
times++;
|
times++;
|
||||||
restart();
|
restart();
|
||||||
logger.info("restart the connector for next round retry.");
|
logger.info("restart the connector for next round retry.");
|
||||||
@@ -247,7 +255,8 @@ public class ClusterCanalConnector implements CanalConnector {
|
|||||||
return;
|
return;
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
logger.warn(String.format("something goes wrong when acking data from server:%s",
|
logger.warn(String.format("something goes wrong when acking data from server:%s",
|
||||||
currentConnector.getAddress()), t);
|
currentConnector != null ? currentConnector.getAddress() : "null"),
|
||||||
|
t);
|
||||||
times++;
|
times++;
|
||||||
restart();
|
restart();
|
||||||
logger.info("restart the connector for next round retry.");
|
logger.info("restart the connector for next round retry.");
|
||||||
@@ -294,6 +303,14 @@ public class ClusterCanalConnector implements CanalConnector {
|
|||||||
this.soTimeout = soTimeout;
|
this.soTimeout = soTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getIdleTimeout() {
|
||||||
|
return idleTimeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIdleTimeout(int idleTimeout) {
|
||||||
|
this.idleTimeout = idleTimeout;
|
||||||
|
}
|
||||||
|
|
||||||
public int getRetryTimes() {
|
public int getRetryTimes() {
|
||||||
return retryTimes;
|
return retryTimes;
|
||||||
}
|
}
|
||||||
@@ -322,4 +339,8 @@ public class ClusterCanalConnector implements CanalConnector {
|
|||||||
return currentConnector;
|
return currentConnector;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void stopRunning() {
|
||||||
|
currentConnector.stopRunning();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ public class SimpleCanalConnector implements CanalConnector {
|
|||||||
private String username;
|
private String username;
|
||||||
private String password;
|
private String password;
|
||||||
private int soTimeout = 60000; // milliseconds
|
private int soTimeout = 60000; // milliseconds
|
||||||
|
private int idleTimeout = 60 * 60 * 1000; // client和server之间的空闲链接超时的时间,默认为1小时
|
||||||
private String filter; // 记录上一次的filter提交值,便于自动重试时提交
|
private String filter; // 记录上一次的filter提交值,便于自动重试时提交
|
||||||
|
|
||||||
private final ByteBuffer readHeader = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN);
|
private final ByteBuffer readHeader = ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN);
|
||||||
@@ -70,21 +71,29 @@ public class SimpleCanalConnector implements CanalConnector {
|
|||||||
private volatile boolean connected = false; // 代表connected是否已正常执行,因为有HA,不代表在工作中
|
private volatile boolean connected = false; // 代表connected是否已正常执行,因为有HA,不代表在工作中
|
||||||
private boolean rollbackOnConnect = true; // 是否在connect链接成功后,自动执行rollback操作
|
private boolean rollbackOnConnect = true; // 是否在connect链接成功后,自动执行rollback操作
|
||||||
private boolean rollbackOnDisConnect = false; // 是否在connect链接成功后,自动执行rollback操作
|
private boolean rollbackOnDisConnect = false; // 是否在connect链接成功后,自动执行rollback操作
|
||||||
|
private boolean lazyParseEntry = false; // 是否自动化解析Entry对象,如果考虑最大化性能可以延后解析
|
||||||
// 读写数据分别使用不同的锁进行控制,减小锁粒度,读也需要排他锁,并发度容易造成数据包混乱,反序列化失败
|
// 读写数据分别使用不同的锁进行控制,减小锁粒度,读也需要排他锁,并发度容易造成数据包混乱,反序列化失败
|
||||||
private Object readDataLock = new Object();
|
private Object readDataLock = new Object();
|
||||||
private Object writeDataLock = new Object();
|
private Object writeDataLock = new Object();
|
||||||
|
|
||||||
|
private volatile boolean running = false;
|
||||||
|
|
||||||
public SimpleCanalConnector(SocketAddress address, String username, String password, String destination){
|
public SimpleCanalConnector(SocketAddress address, String username, String password, String destination){
|
||||||
this(address, username, password, destination, 60000);
|
this(address, username, password, destination, 60000, 60 * 60 * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SimpleCanalConnector(SocketAddress address, String username, String password, String destination,
|
public SimpleCanalConnector(SocketAddress address, String username, String password, String destination,
|
||||||
int soTimeout){
|
int soTimeout){
|
||||||
|
this(address, username, password, destination, soTimeout, 60 * 60 * 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
public SimpleCanalConnector(SocketAddress address, String username, String password, String destination,
|
||||||
|
int soTimeout, int idleTimeout){
|
||||||
this.address = address;
|
this.address = address;
|
||||||
this.username = username;
|
this.username = username;
|
||||||
this.password = password;
|
this.password = password;
|
||||||
this.soTimeout = soTimeout;
|
this.soTimeout = soTimeout;
|
||||||
|
this.idleTimeout = idleTimeout;
|
||||||
this.clientIdentity = new ClientIdentity(destination, (short) 1001);
|
this.clientIdentity = new ClientIdentity(destination, (short) 1001);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,6 +108,9 @@ public class SimpleCanalConnector implements CanalConnector {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
waitClientRunning();
|
waitClientRunning();
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
doConnect();
|
doConnect();
|
||||||
if (filter != null) { // 如果存在条件,说明是自动切换,基于上一次的条件订阅一次
|
if (filter != null) { // 如果存在条件,说明是自动切换,基于上一次的条件订阅一次
|
||||||
subscribe(filter);
|
subscribe(filter);
|
||||||
@@ -152,8 +164,8 @@ public class SimpleCanalConnector implements CanalConnector {
|
|||||||
ClientAuth ca = ClientAuth.newBuilder()
|
ClientAuth ca = ClientAuth.newBuilder()
|
||||||
.setUsername(username != null ? username : "")
|
.setUsername(username != null ? username : "")
|
||||||
.setPassword(ByteString.copyFromUtf8(password != null ? password : ""))
|
.setPassword(ByteString.copyFromUtf8(password != null ? password : ""))
|
||||||
.setNetReadTimeout(soTimeout)
|
.setNetReadTimeout(idleTimeout)
|
||||||
.setNetWriteTimeout(soTimeout)
|
.setNetWriteTimeout(idleTimeout)
|
||||||
.build();
|
.build();
|
||||||
writeWithHeader(Packet.newBuilder()
|
writeWithHeader(Packet.newBuilder()
|
||||||
.setType(PacketType.CLIENTAUTHENTICATION)
|
.setType(PacketType.CLIENTAUTHENTICATION)
|
||||||
@@ -208,6 +220,9 @@ public class SimpleCanalConnector implements CanalConnector {
|
|||||||
|
|
||||||
public void subscribe(String filter) throws CanalClientException {
|
public void subscribe(String filter) throws CanalClientException {
|
||||||
waitClientRunning();
|
waitClientRunning();
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
writeWithHeader(Packet.newBuilder()
|
writeWithHeader(Packet.newBuilder()
|
||||||
.setType(PacketType.SUBSCRIPTION)
|
.setType(PacketType.SUBSCRIPTION)
|
||||||
@@ -234,6 +249,9 @@ public class SimpleCanalConnector implements CanalConnector {
|
|||||||
|
|
||||||
public void unsubscribe() throws CanalClientException {
|
public void unsubscribe() throws CanalClientException {
|
||||||
waitClientRunning();
|
waitClientRunning();
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
writeWithHeader(Packet.newBuilder()
|
writeWithHeader(Packet.newBuilder()
|
||||||
.setType(PacketType.UNSUBSCRIPTION)
|
.setType(PacketType.UNSUBSCRIPTION)
|
||||||
@@ -271,6 +289,9 @@ public class SimpleCanalConnector implements CanalConnector {
|
|||||||
|
|
||||||
public Message getWithoutAck(int batchSize, Long timeout, TimeUnit unit) throws CanalClientException {
|
public Message getWithoutAck(int batchSize, Long timeout, TimeUnit unit) throws CanalClientException {
|
||||||
waitClientRunning();
|
waitClientRunning();
|
||||||
|
if (!running) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
int size = (batchSize <= 0) ? 1000 : batchSize;
|
int size = (batchSize <= 0) ? 1000 : batchSize;
|
||||||
long time = (timeout == null || timeout < 0) ? -1 : timeout; // -1代表不做timeout控制
|
long time = (timeout == null || timeout < 0) ? -1 : timeout; // -1代表不做timeout控制
|
||||||
@@ -298,7 +319,8 @@ public class SimpleCanalConnector implements CanalConnector {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Message receiveMessages() throws IOException {
|
private Message receiveMessages() throws IOException {
|
||||||
Packet p = Packet.parseFrom(readNextPacket());
|
byte[] data = readNextPacket();
|
||||||
|
Packet p = Packet.parseFrom(data);
|
||||||
switch (p.getType()) {
|
switch (p.getType()) {
|
||||||
case MESSAGES: {
|
case MESSAGES: {
|
||||||
if (!p.getCompression().equals(Compression.NONE)) {
|
if (!p.getCompression().equals(Compression.NONE)) {
|
||||||
@@ -307,9 +329,14 @@ public class SimpleCanalConnector implements CanalConnector {
|
|||||||
|
|
||||||
Messages messages = Messages.parseFrom(p.getBody());
|
Messages messages = Messages.parseFrom(p.getBody());
|
||||||
Message result = new Message(messages.getBatchId());
|
Message result = new Message(messages.getBatchId());
|
||||||
|
if (lazyParseEntry) {
|
||||||
|
// byteString
|
||||||
|
result.setRawEntries(messages.getMessagesList());
|
||||||
|
} else {
|
||||||
for (ByteString byteString : messages.getMessagesList()) {
|
for (ByteString byteString : messages.getMessagesList()) {
|
||||||
result.addEntry(Entry.parseFrom(byteString));
|
result.addEntry(Entry.parseFrom(byteString));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
case ACK: {
|
case ACK: {
|
||||||
@@ -324,6 +351,9 @@ public class SimpleCanalConnector implements CanalConnector {
|
|||||||
|
|
||||||
public void ack(long batchId) throws CanalClientException {
|
public void ack(long batchId) throws CanalClientException {
|
||||||
waitClientRunning();
|
waitClientRunning();
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
ClientAck ca = ClientAck.newBuilder()
|
ClientAck ca = ClientAck.newBuilder()
|
||||||
.setDestination(clientIdentity.getDestination())
|
.setDestination(clientIdentity.getDestination())
|
||||||
.setClientId(String.valueOf(clientIdentity.getClientId()))
|
.setClientId(String.valueOf(clientIdentity.getClientId()))
|
||||||
@@ -445,7 +475,11 @@ public class SimpleCanalConnector implements CanalConnector {
|
|||||||
throw new CanalClientException("should connect first");
|
throw new CanalClientException("should connect first");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
running = true;
|
||||||
mutex.get();// 阻塞等待
|
mutex.get();// 阻塞等待
|
||||||
|
} else {
|
||||||
|
// 单机模式直接设置为running
|
||||||
|
running = true;
|
||||||
}
|
}
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
@@ -485,6 +519,14 @@ public class SimpleCanalConnector implements CanalConnector {
|
|||||||
this.soTimeout = soTimeout;
|
this.soTimeout = soTimeout;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getIdleTimeout() {
|
||||||
|
return idleTimeout;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIdleTimeout(int idleTimeout) {
|
||||||
|
this.idleTimeout = idleTimeout;
|
||||||
|
}
|
||||||
|
|
||||||
public void setZkClientx(ZkClientx zkClientx) {
|
public void setZkClientx(ZkClientx zkClientx) {
|
||||||
this.zkClientx = zkClientx;
|
this.zkClientx = zkClientx;
|
||||||
initClientRunningMonitor(this.clientIdentity);
|
initClientRunningMonitor(this.clientIdentity);
|
||||||
@@ -502,4 +544,21 @@ public class SimpleCanalConnector implements CanalConnector {
|
|||||||
this.filter = filter;
|
this.filter = filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isLazyParseEntry() {
|
||||||
|
return lazyParseEntry;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLazyParseEntry(boolean lazyParseEntry) {
|
||||||
|
this.lazyParseEntry = lazyParseEntry;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stopRunning() {
|
||||||
|
if (running) {
|
||||||
|
running = false; // 设置为非running状态
|
||||||
|
if (!mutex.state()) {
|
||||||
|
mutex.set(true); // 中断阻塞
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,6 +98,10 @@ public class ClientRunningMonitor extends AbstractCanalLifeCycle {
|
|||||||
String path = ZookeeperPathUtils.getDestinationClientRunning(this.destination, clientData.getClientId());
|
String path = ZookeeperPathUtils.getDestinationClientRunning(this.destination, clientData.getClientId());
|
||||||
zkClient.unsubscribeDataChanges(path, dataListener);
|
zkClient.unsubscribeDataChanges(path, dataListener);
|
||||||
releaseRunning(); // 尝试一下release
|
releaseRunning(); // 尝试一下release
|
||||||
|
//Fix issue #697
|
||||||
|
if (delayExector != null) {
|
||||||
|
delayExector.shutdown();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 改动记录:
|
// 改动记录:
|
||||||
|
|||||||
@@ -23,6 +23,11 @@ public class NamedThreadFactory implements ThreadFactory {
|
|||||||
|
|
||||||
public void uncaughtException(Thread t,
|
public void uncaughtException(Thread t,
|
||||||
Throwable e) {
|
Throwable e) {
|
||||||
|
if (e instanceof InterruptedException
|
||||||
|
|| (e.getCause() != null && e.getCause() instanceof InterruptedException)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
logger.error("from " + t.getName(), e);
|
logger.error("from " + t.getName(), e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import java.util.concurrent.Executors;
|
|||||||
import java.util.concurrent.ScheduledExecutorService;
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import com.alibaba.otter.canal.common.CanalException;
|
|
||||||
import org.I0Itec.zkclient.IZkDataListener;
|
import org.I0Itec.zkclient.IZkDataListener;
|
||||||
import org.I0Itec.zkclient.exception.ZkException;
|
import org.I0Itec.zkclient.exception.ZkException;
|
||||||
import org.I0Itec.zkclient.exception.ZkInterruptedException;
|
import org.I0Itec.zkclient.exception.ZkInterruptedException;
|
||||||
|
|||||||
@@ -103,8 +103,8 @@ public final class CharsetConversion {
|
|||||||
putEntry(42, "latin7", "latin7_general_cs", "ISO8859_7");
|
putEntry(42, "latin7", "latin7_general_cs", "ISO8859_7");
|
||||||
putEntry(43, "macce", "macce_bin", "MacCentralEurope");
|
putEntry(43, "macce", "macce_bin", "MacCentralEurope");
|
||||||
putEntry(44, "cp1250", "cp1250_croatian_ci", "Cp1250");
|
putEntry(44, "cp1250", "cp1250_croatian_ci", "Cp1250");
|
||||||
putEntry(45, "utf8mb4", "utf8mb4_general_ci", "MacCentralEurope");
|
putEntry(45, "utf8mb4", "utf8mb4_general_ci", "UTF-8");
|
||||||
putEntry(46, "utf8mb4", "utf8mb4_bin", "MacCentralEurope");
|
putEntry(46, "utf8mb4", "utf8mb4_bin", "UTF-8");
|
||||||
putEntry(47, "latin1", "latin1_bin", "ISO8859_1");
|
putEntry(47, "latin1", "latin1_bin", "ISO8859_1");
|
||||||
putEntry(48, "latin1", "latin1_general_ci", "ISO8859_1");
|
putEntry(48, "latin1", "latin1_general_ci", "ISO8859_1");
|
||||||
putEntry(49, "latin1", "latin1_general_cs", "ISO8859_1");
|
putEntry(49, "latin1", "latin1_general_cs", "ISO8859_1");
|
||||||
|
|||||||
@@ -75,7 +75,9 @@ public final class FileLogFetcher extends LogFetcher {
|
|||||||
fin = new FileInputStream(file);
|
fin = new FileInputStream(file);
|
||||||
|
|
||||||
ensureCapacity(BIN_LOG_HEADER_SIZE);
|
ensureCapacity(BIN_LOG_HEADER_SIZE);
|
||||||
if (BIN_LOG_HEADER_SIZE != fin.read(buffer, 0, BIN_LOG_HEADER_SIZE)) throw new IOException("No binlog file header");
|
if (BIN_LOG_HEADER_SIZE != fin.read(buffer, 0, BIN_LOG_HEADER_SIZE)) {
|
||||||
|
throw new IOException("No binlog file header");
|
||||||
|
}
|
||||||
|
|
||||||
if (buffer[0] != BINLOG_MAGIC[0] || buffer[1] != BINLOG_MAGIC[1] || buffer[2] != BINLOG_MAGIC[2]
|
if (buffer[0] != BINLOG_MAGIC[0] || buffer[1] != BINLOG_MAGIC[1] || buffer[2] != BINLOG_MAGIC[2]
|
||||||
|| buffer[3] != BINLOG_MAGIC[3]) {
|
|| buffer[3] != BINLOG_MAGIC[3]) {
|
||||||
@@ -128,6 +130,19 @@ public final class FileLogFetcher extends LogFetcher {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} else if (limit > 0) {
|
} else if (limit > 0) {
|
||||||
|
if (limit >= FormatDescriptionLogEvent.LOG_EVENT_HEADER_LEN) {
|
||||||
|
int lenPosition = position + 4 + 1 + 4;
|
||||||
|
long eventLen = ((long) (0xff & buffer[lenPosition++])) | ((long) (0xff & buffer[lenPosition++]) << 8)
|
||||||
|
| ((long) (0xff & buffer[lenPosition++]) << 16)
|
||||||
|
| ((long) (0xff & buffer[lenPosition++]) << 24);
|
||||||
|
|
||||||
|
if (limit >= eventLen) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
ensureCapacity((int) eventLen);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
System.arraycopy(buffer, origin, buffer, 0, limit);
|
System.arraycopy(buffer, origin, buffer, 0, limit);
|
||||||
position -= origin;
|
position -= origin;
|
||||||
origin = 0;
|
origin = 0;
|
||||||
|
|||||||
@@ -58,7 +58,6 @@ public final class LogContext {
|
|||||||
|
|
||||||
public void reset() {
|
public void reset() {
|
||||||
formatDescription = FormatDescriptionLogEvent.FORMAT_DESCRIPTION_EVENT_5_x;
|
formatDescription = FormatDescriptionLogEvent.FORMAT_DESCRIPTION_EVENT_5_x;
|
||||||
|
|
||||||
mapOfTable.clear();
|
mapOfTable.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,15 +30,19 @@ import com.taobao.tddl.dbsync.binlog.event.RowsQueryLogEvent;
|
|||||||
import com.taobao.tddl.dbsync.binlog.event.StartLogEventV3;
|
import com.taobao.tddl.dbsync.binlog.event.StartLogEventV3;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.StopLogEvent;
|
import com.taobao.tddl.dbsync.binlog.event.StopLogEvent;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.TableMapLogEvent;
|
import com.taobao.tddl.dbsync.binlog.event.TableMapLogEvent;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.event.TransactionContextLogEvent;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.UnknownLogEvent;
|
import com.taobao.tddl.dbsync.binlog.event.UnknownLogEvent;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.UpdateRowsLogEvent;
|
import com.taobao.tddl.dbsync.binlog.event.UpdateRowsLogEvent;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.UserVarLogEvent;
|
import com.taobao.tddl.dbsync.binlog.event.UserVarLogEvent;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.event.ViewChangeEvent;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.WriteRowsLogEvent;
|
import com.taobao.tddl.dbsync.binlog.event.WriteRowsLogEvent;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.event.XaPrepareLogEvent;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.XidLogEvent;
|
import com.taobao.tddl.dbsync.binlog.event.XidLogEvent;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.mariadb.AnnotateRowsEvent;
|
import com.taobao.tddl.dbsync.binlog.event.mariadb.AnnotateRowsEvent;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.mariadb.BinlogCheckPointLogEvent;
|
import com.taobao.tddl.dbsync.binlog.event.mariadb.BinlogCheckPointLogEvent;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.mariadb.MariaGtidListLogEvent;
|
import com.taobao.tddl.dbsync.binlog.event.mariadb.MariaGtidListLogEvent;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.mariadb.MariaGtidLogEvent;
|
import com.taobao.tddl.dbsync.binlog.event.mariadb.MariaGtidLogEvent;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.event.mariadb.StartEncryptionLogEvent;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements a binary-log decoder.
|
* Implements a binary-log decoder.
|
||||||
@@ -105,6 +109,8 @@ public final class LogDecoder {
|
|||||||
/* Decoding binary-log to event */
|
/* Decoding binary-log to event */
|
||||||
event = decode(buffer, header, context);
|
event = decode(buffer, header, context);
|
||||||
if (event != null) {
|
if (event != null) {
|
||||||
|
// set logFileName
|
||||||
|
event.getHeader().setLogFileName(context.getLogPosition().getFileName());
|
||||||
event.setSemival(buffer.semival);
|
event.setSemival(buffer.semival);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@@ -366,6 +372,24 @@ public final class LogDecoder {
|
|||||||
logPosition.position = header.getLogPos();
|
logPosition.position = header.getLogPos();
|
||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
case LogEvent.TRANSACTION_CONTEXT_EVENT: {
|
||||||
|
TransactionContextLogEvent event = new TransactionContextLogEvent(header, buffer, descriptionEvent);
|
||||||
|
/* updating position in context */
|
||||||
|
logPosition.position = header.getLogPos();
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
case LogEvent.VIEW_CHANGE_EVENT: {
|
||||||
|
ViewChangeEvent event = new ViewChangeEvent(header, buffer, descriptionEvent);
|
||||||
|
/* updating position in context */
|
||||||
|
logPosition.position = header.getLogPos();
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
case LogEvent.XA_PREPARE_LOG_EVENT: {
|
||||||
|
XaPrepareLogEvent event = new XaPrepareLogEvent(header, buffer, descriptionEvent);
|
||||||
|
/* updating position in context */
|
||||||
|
logPosition.position = header.getLogPos();
|
||||||
|
return event;
|
||||||
|
}
|
||||||
case LogEvent.ANNOTATE_ROWS_EVENT: {
|
case LogEvent.ANNOTATE_ROWS_EVENT: {
|
||||||
AnnotateRowsEvent event = new AnnotateRowsEvent(header, buffer, descriptionEvent);
|
AnnotateRowsEvent event = new AnnotateRowsEvent(header, buffer, descriptionEvent);
|
||||||
/* updating position in context */
|
/* updating position in context */
|
||||||
@@ -390,6 +414,12 @@ public final class LogDecoder {
|
|||||||
logPosition.position = header.getLogPos();
|
logPosition.position = header.getLogPos();
|
||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
case LogEvent.START_ENCRYPTION_EVENT: {
|
||||||
|
StartEncryptionLogEvent event = new StartEncryptionLogEvent(header, buffer, descriptionEvent);
|
||||||
|
/* updating position in context */
|
||||||
|
logPosition.position = header.getLogPos();
|
||||||
|
return event;
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
/*
|
/*
|
||||||
* Create an object of Ignorable_log_event for unrecognized
|
* Create an object of Ignorable_log_event for unrecognized
|
||||||
@@ -402,9 +432,10 @@ public final class LogDecoder {
|
|||||||
logPosition.position = header.getLogPos();
|
logPosition.position = header.getLogPos();
|
||||||
return event;
|
return event;
|
||||||
} else {
|
} else {
|
||||||
if (logger.isWarnEnabled()) logger.warn("Skipping unrecognized binlog event "
|
if (logger.isWarnEnabled()) {
|
||||||
+ LogEvent.getTypeName(header.getType()) + " from: "
|
logger.warn("Skipping unrecognized binlog event " + LogEvent.getTypeName(header.getType())
|
||||||
+ context.getLogPosition());
|
+ " from: " + context.getLogPosition());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -161,9 +161,17 @@ public abstract class LogEvent {
|
|||||||
|
|
||||||
public static final int PREVIOUS_GTIDS_LOG_EVENT = 35;
|
public static final int PREVIOUS_GTIDS_LOG_EVENT = 35;
|
||||||
|
|
||||||
|
/* MySQL 5.7 events */
|
||||||
|
public static final int TRANSACTION_CONTEXT_EVENT = 36;
|
||||||
|
|
||||||
|
public static final int VIEW_CHANGE_EVENT = 37;
|
||||||
|
|
||||||
|
/* Prepared XA transaction terminal event similar to Xid */
|
||||||
|
public static final int XA_PREPARE_LOG_EVENT = 38;
|
||||||
|
|
||||||
// mariaDb 5.5.34
|
// mariaDb 5.5.34
|
||||||
/* New MySQL/Sun events are to be added right above this comment */
|
/* New MySQL/Sun events are to be added right above this comment */
|
||||||
public static final int MYSQL_EVENTS_END = 36;
|
public static final int MYSQL_EVENTS_END = 39;
|
||||||
|
|
||||||
public static final int MARIA_EVENTS_BEGIN = 160;
|
public static final int MARIA_EVENTS_BEGIN = 160;
|
||||||
/* New Maria event numbers start from here */
|
/* New Maria event numbers start from here */
|
||||||
@@ -189,8 +197,10 @@ public abstract class LogEvent {
|
|||||||
*/
|
*/
|
||||||
public static final int GTID_LIST_EVENT = 163;
|
public static final int GTID_LIST_EVENT = 163;
|
||||||
|
|
||||||
|
public static final int START_ENCRYPTION_EVENT = 164;
|
||||||
|
|
||||||
/** end marker */
|
/** end marker */
|
||||||
public static final int ENUM_END_EVENT = 164;
|
public static final int ENUM_END_EVENT = 165;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 1 byte length, 1 byte format Length is total length in bytes, including 2
|
* 1 byte length, 1 byte format Length is total length in bytes, including 2
|
||||||
@@ -370,8 +380,6 @@ public abstract class LogEvent {
|
|||||||
*/
|
*/
|
||||||
protected int semival;
|
protected int semival;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public int getSemival() {
|
public int getSemival() {
|
||||||
return semival;
|
return semival;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,10 +59,15 @@ public final class FormatDescriptionLogEvent extends StartLogEventV3 {
|
|||||||
public static final int HEARTBEAT_HEADER_LEN = 0;
|
public static final int HEARTBEAT_HEADER_LEN = 0;
|
||||||
public static final int IGNORABLE_HEADER_LEN = 0;
|
public static final int IGNORABLE_HEADER_LEN = 0;
|
||||||
public static final int ROWS_HEADER_LEN_V2 = 10;
|
public static final int ROWS_HEADER_LEN_V2 = 10;
|
||||||
|
public static final int TRANSACTION_CONTEXT_HEADER_LEN = 18;
|
||||||
|
public static final int VIEW_CHANGE_HEADER_LEN = 52;
|
||||||
|
public static final int XA_PREPARE_HEADER_LEN = 0;
|
||||||
|
|
||||||
public static final int ANNOTATE_ROWS_HEADER_LEN = 0;
|
public static final int ANNOTATE_ROWS_HEADER_LEN = 0;
|
||||||
public static final int BINLOG_CHECKPOINT_HEADER_LEN = 4;
|
public static final int BINLOG_CHECKPOINT_HEADER_LEN = 4;
|
||||||
public static final int GTID_HEADER_LEN = 19;
|
public static final int GTID_HEADER_LEN = 19;
|
||||||
public static final int GTID_LIST_HEADER_LEN = 4;
|
public static final int GTID_LIST_HEADER_LEN = 4;
|
||||||
|
public static final int START_ENCRYPTION_HEADER_LEN = 0;
|
||||||
|
|
||||||
public static final int POST_HEADER_LENGTH = 11;
|
public static final int POST_HEADER_LENGTH = 11;
|
||||||
|
|
||||||
@@ -202,11 +207,17 @@ public final class FormatDescriptionLogEvent extends StartLogEventV3 {
|
|||||||
postHeaderLen[GTID_LOG_EVENT - 1] = POST_HEADER_LENGTH;
|
postHeaderLen[GTID_LOG_EVENT - 1] = POST_HEADER_LENGTH;
|
||||||
postHeaderLen[ANONYMOUS_GTID_LOG_EVENT - 1] = POST_HEADER_LENGTH;
|
postHeaderLen[ANONYMOUS_GTID_LOG_EVENT - 1] = POST_HEADER_LENGTH;
|
||||||
postHeaderLen[PREVIOUS_GTIDS_LOG_EVENT - 1] = IGNORABLE_HEADER_LEN;
|
postHeaderLen[PREVIOUS_GTIDS_LOG_EVENT - 1] = IGNORABLE_HEADER_LEN;
|
||||||
|
|
||||||
|
postHeaderLen[TRANSACTION_CONTEXT_EVENT - 1] = TRANSACTION_CONTEXT_HEADER_LEN;
|
||||||
|
postHeaderLen[VIEW_CHANGE_EVENT - 1] = VIEW_CHANGE_HEADER_LEN;
|
||||||
|
postHeaderLen[XA_PREPARE_LOG_EVENT - 1] = XA_PREPARE_HEADER_LEN;
|
||||||
|
|
||||||
// mariadb 10
|
// mariadb 10
|
||||||
postHeaderLen[ANNOTATE_ROWS_EVENT - 1] = ANNOTATE_ROWS_HEADER_LEN;
|
postHeaderLen[ANNOTATE_ROWS_EVENT - 1] = ANNOTATE_ROWS_HEADER_LEN;
|
||||||
postHeaderLen[BINLOG_CHECKPOINT_EVENT - 1] = BINLOG_CHECKPOINT_HEADER_LEN;
|
postHeaderLen[BINLOG_CHECKPOINT_EVENT - 1] = BINLOG_CHECKPOINT_HEADER_LEN;
|
||||||
postHeaderLen[GTID_EVENT - 1] = GTID_HEADER_LEN;
|
postHeaderLen[GTID_EVENT - 1] = GTID_HEADER_LEN;
|
||||||
postHeaderLen[GTID_LIST_EVENT - 1] = GTID_LIST_HEADER_LEN;
|
postHeaderLen[GTID_LIST_EVENT - 1] = GTID_LIST_HEADER_LEN;
|
||||||
|
postHeaderLen[START_ENCRYPTION_EVENT - 1] = START_ENCRYPTION_HEADER_LEN;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 3: /* 4.0.x x>=2 */
|
case 3: /* 4.0.x x>=2 */
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package com.taobao.tddl.dbsync.binlog.event;
|
package com.taobao.tddl.dbsync.binlog.event;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
import com.taobao.tddl.dbsync.binlog.LogBuffer;
|
import com.taobao.tddl.dbsync.binlog.LogBuffer;
|
||||||
import com.taobao.tddl.dbsync.binlog.LogEvent;
|
import com.taobao.tddl.dbsync.binlog.LogEvent;
|
||||||
|
|
||||||
@@ -14,8 +17,13 @@ public class GtidLogEvent extends LogEvent {
|
|||||||
public static final int ENCODED_FLAG_LENGTH = 1;
|
public static final int ENCODED_FLAG_LENGTH = 1;
|
||||||
// / Length of SID in event encoding
|
// / Length of SID in event encoding
|
||||||
public static final int ENCODED_SID_LENGTH = 16;
|
public static final int ENCODED_SID_LENGTH = 16;
|
||||||
|
public static final int LOGICAL_TIMESTAMP_TYPE_CODE = 2;
|
||||||
|
|
||||||
private boolean commitFlag;
|
private boolean commitFlag;
|
||||||
|
private UUID sid;
|
||||||
|
private long gno;
|
||||||
|
private Long lastCommitted;
|
||||||
|
private Long sequenceNumber;
|
||||||
|
|
||||||
public GtidLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent){
|
public GtidLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent){
|
||||||
super(header);
|
super(header);
|
||||||
@@ -27,6 +35,22 @@ public class GtidLogEvent extends LogEvent {
|
|||||||
buffer.position(commonHeaderLen);
|
buffer.position(commonHeaderLen);
|
||||||
commitFlag = (buffer.getUint8() != 0); // ENCODED_FLAG_LENGTH
|
commitFlag = (buffer.getUint8() != 0); // ENCODED_FLAG_LENGTH
|
||||||
|
|
||||||
|
byte[] bs = buffer.getData(ENCODED_SID_LENGTH);
|
||||||
|
ByteBuffer bb = ByteBuffer.wrap(bs);
|
||||||
|
long high = bb.getLong();
|
||||||
|
long low = bb.getLong();
|
||||||
|
sid = new UUID(high, low);
|
||||||
|
|
||||||
|
gno = buffer.getLong64();
|
||||||
|
|
||||||
|
// support gtid lastCommitted and sequenceNumber
|
||||||
|
// 42 = 1+16+8+1+8+8
|
||||||
|
if (buffer.capacity() > 42 && buffer.getUint8() == LOGICAL_TIMESTAMP_TYPE_CODE) {
|
||||||
|
lastCommitted = buffer.getLong64();
|
||||||
|
sequenceNumber = buffer.getLong64();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// ignore gtid info read
|
// ignore gtid info read
|
||||||
// sid.copy_from((uchar *)ptr_buffer);
|
// sid.copy_from((uchar *)ptr_buffer);
|
||||||
// ptr_buffer+= ENCODED_SID_LENGTH;
|
// ptr_buffer+= ENCODED_SID_LENGTH;
|
||||||
@@ -42,4 +66,19 @@ public class GtidLogEvent extends LogEvent {
|
|||||||
return commitFlag;
|
return commitFlag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public UUID getSid() {
|
||||||
|
return sid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getGno() {
|
||||||
|
return gno;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getLastCommitted() {
|
||||||
|
return lastCommitted;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getSequenceNumber() {
|
||||||
|
return sequenceNumber;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -115,6 +115,11 @@ public final class LogHeader {
|
|||||||
*/
|
*/
|
||||||
protected long crc; // ha_checksum
|
protected long crc; // ha_checksum
|
||||||
|
|
||||||
|
/**
|
||||||
|
* binlog fileName
|
||||||
|
*/
|
||||||
|
protected String logFileName;
|
||||||
|
|
||||||
/* for Start_event_v3 */
|
/* for Start_event_v3 */
|
||||||
public LogHeader(final int type){
|
public LogHeader(final int type){
|
||||||
this.type = type;
|
this.type = type;
|
||||||
@@ -270,6 +275,14 @@ public final class LogHeader {
|
|||||||
return checksumAlg;
|
return checksumAlg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getLogFileName() {
|
||||||
|
return logFileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLogFileName(String logFileName) {
|
||||||
|
this.logFileName = logFileName;
|
||||||
|
}
|
||||||
|
|
||||||
private void processCheckSum(LogBuffer buffer) {
|
private void processCheckSum(LogBuffer buffer) {
|
||||||
if (checksumAlg != LogEvent.BINLOG_CHECKSUM_ALG_OFF && checksumAlg != LogEvent.BINLOG_CHECKSUM_ALG_UNDEF) {
|
if (checksumAlg != LogEvent.BINLOG_CHECKSUM_ALG_OFF && checksumAlg != LogEvent.BINLOG_CHECKSUM_ALG_UNDEF) {
|
||||||
crc = buffer.getUint32(eventLen - LogEvent.BINLOG_CHECKSUM_LEN);
|
crc = buffer.getUint32(eventLen - LogEvent.BINLOG_CHECKSUM_LEN);
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ public final class RowsLogBuffer {
|
|||||||
public static final long DATETIMEF_INT_OFS = 0x8000000000L;
|
public static final long DATETIMEF_INT_OFS = 0x8000000000L;
|
||||||
public static final long TIMEF_INT_OFS = 0x800000L;
|
public static final long TIMEF_INT_OFS = 0x800000L;
|
||||||
public static final long TIMEF_OFS = 0x800000000000L;
|
public static final long TIMEF_OFS = 0x800000000000L;
|
||||||
|
private static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
|
||||||
|
|
||||||
private final LogBuffer buffer;
|
private final LogBuffer buffer;
|
||||||
private final int columnLen;
|
private final int columnLen;
|
||||||
@@ -480,13 +481,28 @@ public final class RowsLogBuffer {
|
|||||||
// cal.set(d / 10000, (d % 10000) / 100 - 1, d % 100, t /
|
// cal.set(d / 10000, (d % 10000) / 100 - 1, d % 100, t /
|
||||||
// 10000, (t % 10000) / 100, t % 100);
|
// 10000, (t % 10000) / 100, t % 100);
|
||||||
// value = new Timestamp(cal.getTimeInMillis());
|
// value = new Timestamp(cal.getTimeInMillis());
|
||||||
value = String.format("%04d-%02d-%02d %02d:%02d:%02d",
|
// value = String.format("%04d-%02d-%02d %02d:%02d:%02d",
|
||||||
d / 10000,
|
// d / 10000,
|
||||||
(d % 10000) / 100,
|
// (d % 10000) / 100,
|
||||||
d % 100,
|
// d % 100,
|
||||||
t / 10000,
|
// t / 10000,
|
||||||
(t % 10000) / 100,
|
// (t % 10000) / 100,
|
||||||
t % 100);
|
// t % 100);
|
||||||
|
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
builder.append(26);
|
||||||
|
appendNumber4(builder, d / 10000);
|
||||||
|
builder.append('-');
|
||||||
|
appendNumber2(builder, (d % 10000) / 100);
|
||||||
|
builder.append('-');
|
||||||
|
appendNumber2(builder, d % 100);
|
||||||
|
builder.append(' ');
|
||||||
|
appendNumber2(builder, t / 10000);
|
||||||
|
builder.append(':');
|
||||||
|
appendNumber2(builder, (t % 10000) / 100);
|
||||||
|
builder.append(':');
|
||||||
|
appendNumber2(builder, t % 100);
|
||||||
|
value = builder.toString();
|
||||||
}
|
}
|
||||||
javaType = Types.TIMESTAMP;
|
javaType = Types.TIMESTAMP;
|
||||||
length = 8;
|
length = 8;
|
||||||
@@ -540,13 +556,27 @@ public final class RowsLogBuffer {
|
|||||||
// % (1 << 5)), (int) (hms >> 12),
|
// % (1 << 5)), (int) (hms >> 12),
|
||||||
// (int) ((hms >> 6) % (1 << 6)), (int) (hms % (1 << 6)));
|
// (int) ((hms >> 6) % (1 << 6)), (int) (hms % (1 << 6)));
|
||||||
// value = new Timestamp(cal.getTimeInMillis());
|
// value = new Timestamp(cal.getTimeInMillis());
|
||||||
second = String.format("%04d-%02d-%02d %02d:%02d:%02d",
|
// second = String.format("%04d-%02d-%02d %02d:%02d:%02d",
|
||||||
(int) (ym / 13),
|
// (int) (ym / 13),
|
||||||
(int) (ym % 13),
|
// (int) (ym % 13),
|
||||||
(int) (ymd % (1 << 5)),
|
// (int) (ymd % (1 << 5)),
|
||||||
(int) (hms >> 12),
|
// (int) (hms >> 12),
|
||||||
(int) ((hms >> 6) % (1 << 6)),
|
// (int) ((hms >> 6) % (1 << 6)),
|
||||||
(int) (hms % (1 << 6)));
|
// (int) (hms % (1 << 6)));
|
||||||
|
|
||||||
|
StringBuilder builder = new StringBuilder(26);
|
||||||
|
appendNumber4(builder, (int) (ym / 13));
|
||||||
|
builder.append('-');
|
||||||
|
appendNumber2(builder, (int) (ym % 13));
|
||||||
|
builder.append('-');
|
||||||
|
appendNumber2(builder, (int) (ymd % (1 << 5)));
|
||||||
|
builder.append(' ');
|
||||||
|
appendNumber2(builder, (int) (hms >> 12));
|
||||||
|
builder.append(':');
|
||||||
|
appendNumber2(builder, (int) ((hms >> 6) % (1 << 6)));
|
||||||
|
builder.append(':');
|
||||||
|
appendNumber2(builder, (int) (hms % (1 << 6)));
|
||||||
|
second = builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (meta >= 1) {
|
if (meta >= 1) {
|
||||||
@@ -575,11 +605,22 @@ public final class RowsLogBuffer {
|
|||||||
// cal.set(70, 0, 1, i32 / 10000, (i32 % 10000) / 100, i32 %
|
// cal.set(70, 0, 1, i32 / 10000, (i32 % 10000) / 100, i32 %
|
||||||
// 100);
|
// 100);
|
||||||
// value = new Time(cal.getTimeInMillis());
|
// value = new Time(cal.getTimeInMillis());
|
||||||
value = String.format("%s%02d:%02d:%02d",
|
// value = String.format("%s%02d:%02d:%02d",
|
||||||
(i32 >= 0) ? "" : "-",
|
// (i32 >= 0) ? "" : "-",
|
||||||
u32 / 10000,
|
// u32 / 10000,
|
||||||
(u32 % 10000) / 100,
|
// (u32 % 10000) / 100,
|
||||||
u32 % 100);
|
// u32 % 100);
|
||||||
|
|
||||||
|
StringBuilder builder = new StringBuilder(12);
|
||||||
|
if (i32 < 0) {
|
||||||
|
builder.append('-');
|
||||||
|
}
|
||||||
|
appendNumber2(builder, u32 / 10000);
|
||||||
|
builder.append(':');
|
||||||
|
appendNumber2(builder, (u32 % 10000) / 100);
|
||||||
|
builder.append(':');
|
||||||
|
appendNumber2(builder, u32 % 100);
|
||||||
|
value = builder.toString();
|
||||||
}
|
}
|
||||||
javaType = Types.TIME;
|
javaType = Types.TIME;
|
||||||
length = 3;
|
length = 3;
|
||||||
@@ -673,11 +714,22 @@ public final class RowsLogBuffer {
|
|||||||
// value = new Time(cal.getTimeInMillis());
|
// value = new Time(cal.getTimeInMillis());
|
||||||
long ultime = Math.abs(ltime);
|
long ultime = Math.abs(ltime);
|
||||||
intpart = ultime >> 24;
|
intpart = ultime >> 24;
|
||||||
second = String.format("%s%02d:%02d:%02d",
|
// second = String.format("%s%02d:%02d:%02d",
|
||||||
ltime >= 0 ? "" : "-",
|
// ltime >= 0 ? "" : "-",
|
||||||
(int) ((intpart >> 12) % (1 << 10)),
|
// (int) ((intpart >> 12) % (1 << 10)),
|
||||||
(int) ((intpart >> 6) % (1 << 6)),
|
// (int) ((intpart >> 6) % (1 << 6)),
|
||||||
(int) (intpart % (1 << 6)));
|
// (int) (intpart % (1 << 6)));
|
||||||
|
|
||||||
|
StringBuilder builder = new StringBuilder(12);
|
||||||
|
if (ltime < 0) {
|
||||||
|
builder.append('-');
|
||||||
|
}
|
||||||
|
appendNumber2(builder, (int) ((intpart >> 12) % (1 << 10)));
|
||||||
|
builder.append(':');
|
||||||
|
appendNumber2(builder, (int) ((intpart >> 6) % (1 << 6)));
|
||||||
|
builder.append(':');
|
||||||
|
appendNumber2(builder, (int) (intpart % (1 << 6)));
|
||||||
|
second = builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (meta >= 1) {
|
if (meta >= 1) {
|
||||||
@@ -717,7 +769,16 @@ public final class RowsLogBuffer {
|
|||||||
// cal.set((i32 / (16 * 32)), (i32 / 32 % 16) - 1, (i32 %
|
// cal.set((i32 / (16 * 32)), (i32 / 32 % 16) - 1, (i32 %
|
||||||
// 32));
|
// 32));
|
||||||
// value = new java.sql.Date(cal.getTimeInMillis());
|
// value = new java.sql.Date(cal.getTimeInMillis());
|
||||||
value = String.format("%04d-%02d-%02d", i32 / (16 * 32), i32 / 32 % 16, i32 % 32);
|
// value = String.format("%04d-%02d-%02d", i32 / (16 * 32),
|
||||||
|
// i32 / 32 % 16, i32 % 32);
|
||||||
|
|
||||||
|
StringBuilder builder = new StringBuilder(12);
|
||||||
|
appendNumber4(builder, i32 / (16 * 32));
|
||||||
|
builder.append('-');
|
||||||
|
appendNumber2(builder, i32 / 32 % 16);
|
||||||
|
builder.append('-');
|
||||||
|
appendNumber2(builder, i32 % 32);
|
||||||
|
value = builder.toString();
|
||||||
}
|
}
|
||||||
javaType = Types.DATE;
|
javaType = Types.DATE;
|
||||||
length = 3;
|
length = 3;
|
||||||
@@ -970,7 +1031,8 @@ public final class RowsLogBuffer {
|
|||||||
throw new IllegalArgumentException("!! Unknown JSON packlen = " + meta);
|
throw new IllegalArgumentException("!! Unknown JSON packlen = " + meta);
|
||||||
}
|
}
|
||||||
if (0 == len) {
|
if (0 == len) {
|
||||||
// fixed issue #1 by lava, json column of zero length has no value, value parsing should be skipped
|
// fixed issue #1 by lava, json column of zero length has no
|
||||||
|
// value, value parsing should be skipped
|
||||||
value = "";
|
value = "";
|
||||||
} else {
|
} else {
|
||||||
int position = buffer.position();
|
int position = buffer.position();
|
||||||
@@ -1066,4 +1128,34 @@ public final class RowsLogBuffer {
|
|||||||
return sec.substring(0, meta);
|
return sec.substring(0, meta);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void appendNumber4(StringBuilder builder, int d) {
|
||||||
|
if (d >= 1000) {
|
||||||
|
builder.append(digits[d / 1000])
|
||||||
|
.append(digits[(d / 100) % 10])
|
||||||
|
.append(digits[(d / 10) % 10])
|
||||||
|
.append(digits[d % 10]);
|
||||||
|
} else if (d >= 100) {
|
||||||
|
builder.append('0');
|
||||||
|
appendNumber3(builder, d);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void appendNumber3(StringBuilder builder, int d) {
|
||||||
|
if (d >= 100) {
|
||||||
|
builder.append(digits[d / 100])
|
||||||
|
.append(digits[(d / 10) % 10])
|
||||||
|
.append(digits[d % 10]);
|
||||||
|
} else {
|
||||||
|
builder.append('0');
|
||||||
|
appendNumber2(builder, d);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void appendNumber2(StringBuilder builder, int d) {
|
||||||
|
if (d >= 10) {
|
||||||
|
builder.append(digits[(d / 10) % 10]).append(digits[d % 10]);
|
||||||
|
} else {
|
||||||
|
builder.append('0').append(digits[d]);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -194,7 +194,7 @@ public abstract class RowsLogEvent extends LogEvent {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public final RowsLogBuffer getRowsBuf(String charsetName) {
|
public final RowsLogBuffer getRowsBuf(String charsetName) {
|
||||||
return new RowsLogBuffer(rowsBuf.duplicate(), columnLen, charsetName);
|
return new RowsLogBuffer(rowsBuf, columnLen, charsetName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public final int getFlags(final int flags) {
|
public final int getFlags(final int flags) {
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.taobao.tddl.dbsync.binlog.event;
|
||||||
|
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogBuffer;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author agapple 2018年5月7日 下午7:05:39
|
||||||
|
* @version 1.0.26
|
||||||
|
* @since mysql 5.7
|
||||||
|
*/
|
||||||
|
public class TransactionContextLogEvent extends LogEvent {
|
||||||
|
|
||||||
|
public TransactionContextLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent){
|
||||||
|
super(header);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.taobao.tddl.dbsync.binlog.event;
|
||||||
|
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogBuffer;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author agapple 2018年5月7日 下午7:05:39
|
||||||
|
* @version 1.0.26
|
||||||
|
* @since mysql 5.7
|
||||||
|
*/
|
||||||
|
public class ViewChangeEvent extends LogEvent {
|
||||||
|
|
||||||
|
public ViewChangeEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent){
|
||||||
|
super(header);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,65 @@
|
|||||||
|
package com.taobao.tddl.dbsync.binlog.event;
|
||||||
|
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogBuffer;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author agapple 2018年5月7日 下午7:05:39
|
||||||
|
* @version 1.0.26
|
||||||
|
* @since mysql 5.7
|
||||||
|
*/
|
||||||
|
public class XaPrepareLogEvent extends LogEvent {
|
||||||
|
|
||||||
|
private boolean onePhase;
|
||||||
|
private int formatId;
|
||||||
|
private int gtridLength;
|
||||||
|
private int bqualLength;
|
||||||
|
private byte[] data;
|
||||||
|
|
||||||
|
public XaPrepareLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent){
|
||||||
|
super(header);
|
||||||
|
|
||||||
|
final int commonHeaderLen = descriptionEvent.getCommonHeaderLen();
|
||||||
|
final int postHeaderLen = descriptionEvent.getPostHeaderLen()[header.getType() - 1];
|
||||||
|
|
||||||
|
int offset = commonHeaderLen + postHeaderLen;
|
||||||
|
buffer.position(offset);
|
||||||
|
|
||||||
|
onePhase = (buffer.getInt8() == 0x00 ? false : true);
|
||||||
|
|
||||||
|
formatId = buffer.getInt32();
|
||||||
|
gtridLength = buffer.getInt32();
|
||||||
|
bqualLength = buffer.getInt32();
|
||||||
|
|
||||||
|
int MY_XIDDATASIZE = 128;
|
||||||
|
if (MY_XIDDATASIZE >= gtridLength + bqualLength && gtridLength >= 0 && gtridLength <= 64 && bqualLength >= 0
|
||||||
|
&& bqualLength <= 64) {
|
||||||
|
data = buffer.getData(gtridLength + bqualLength);
|
||||||
|
} else {
|
||||||
|
formatId = -1;
|
||||||
|
gtridLength = 0;
|
||||||
|
bqualLength = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isOnePhase() {
|
||||||
|
return onePhase;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getFormatId() {
|
||||||
|
return formatId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getGtridLength() {
|
||||||
|
return gtridLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getBqualLength() {
|
||||||
|
return bqualLength;
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getData() {
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.taobao.tddl.dbsync.binlog.event.mariadb;
|
||||||
|
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogBuffer;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogEvent;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.event.FormatDescriptionLogEvent;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.event.LogHeader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* mariadb的Start_encryption_log_event
|
||||||
|
*
|
||||||
|
* @author agapple 2018年5月7日 下午7:23:02
|
||||||
|
* @version 1.0.26
|
||||||
|
*/
|
||||||
|
public class StartEncryptionLogEvent extends LogEvent {
|
||||||
|
|
||||||
|
public StartEncryptionLogEvent(LogHeader header, LogBuffer buffer, FormatDescriptionLogEvent descriptionEvent){
|
||||||
|
super(header);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
package com.taobao.tddl.dbsync;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
|
import com.taobao.tddl.dbsync.binlog.DirectLogFetcher;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogEvent;
|
||||||
|
|
||||||
|
public class FetcherPerformanceTest {
|
||||||
|
|
||||||
|
public static void main(String args[]) {
|
||||||
|
DirectLogFetcher fetcher = new DirectLogFetcher();
|
||||||
|
try {
|
||||||
|
Class.forName("com.mysql.jdbc.Driver");
|
||||||
|
Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306",
|
||||||
|
"root",
|
||||||
|
"hello");
|
||||||
|
Statement statement = connection.createStatement();
|
||||||
|
statement.execute("SET @master_binlog_checksum='@@global.binlog_checksum'");
|
||||||
|
statement.execute("SET @mariadb_slave_capability='" + LogEvent.MARIA_SLAVE_CAPABILITY_MINE + "'");
|
||||||
|
|
||||||
|
fetcher.open(connection, "mysql-bin.000006", 120L, 2);
|
||||||
|
|
||||||
|
AtomicLong sum = new AtomicLong(0);
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
long last = 0;
|
||||||
|
long end = 0;
|
||||||
|
|
||||||
|
while (fetcher.fetch()) {
|
||||||
|
sum.incrementAndGet();
|
||||||
|
long current = sum.get();
|
||||||
|
if (current - last >= 100000) {
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
long tps = ((current - last) * 1000) / (end - start);
|
||||||
|
System.out.println(" total : " + sum + " , cost : " + (end - start) + " , tps : " + tps);
|
||||||
|
last = current;
|
||||||
|
start = end;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
fetcher.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,15 +8,6 @@ import java.sql.Statement;
|
|||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.taobao.tddl.dbsync.binlog.event.DeleteRowsLogEvent;
|
|
||||||
import com.taobao.tddl.dbsync.binlog.event.QueryLogEvent;
|
|
||||||
import com.taobao.tddl.dbsync.binlog.event.RotateLogEvent;
|
|
||||||
import com.taobao.tddl.dbsync.binlog.event.RowsQueryLogEvent;
|
|
||||||
import com.taobao.tddl.dbsync.binlog.event.UpdateRowsLogEvent;
|
|
||||||
import com.taobao.tddl.dbsync.binlog.event.WriteRowsLogEvent;
|
|
||||||
import com.taobao.tddl.dbsync.binlog.event.XidLogEvent;
|
|
||||||
import com.taobao.tddl.dbsync.binlog.event.mariadb.AnnotateRowsEvent;
|
|
||||||
|
|
||||||
public class DirectLogFetcherTest extends BaseLogFetcherTest {
|
public class DirectLogFetcherTest extends BaseLogFetcherTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -24,56 +15,54 @@ public class DirectLogFetcherTest extends BaseLogFetcherTest {
|
|||||||
DirectLogFetcher fecther = new DirectLogFetcher();
|
DirectLogFetcher fecther = new DirectLogFetcher();
|
||||||
try {
|
try {
|
||||||
Class.forName("com.mysql.jdbc.Driver");
|
Class.forName("com.mysql.jdbc.Driver");
|
||||||
Connection connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306", "root", "hello");
|
Connection connection = DriverManager.getConnection("jdbc:mysql://100.81.154.142:3306", "root", "hello");
|
||||||
Statement statement = connection.createStatement();
|
Statement statement = connection.createStatement();
|
||||||
statement.execute("SET @master_binlog_checksum='@@global.binlog_checksum'");
|
statement.execute("SET @master_binlog_checksum='@@global.binlog_checksum'");
|
||||||
statement.execute("SET @mariadb_slave_capability='" + LogEvent.MARIA_SLAVE_CAPABILITY_MINE + "'");
|
statement.execute("SET @mariadb_slave_capability='" + LogEvent.MARIA_SLAVE_CAPABILITY_MINE + "'");
|
||||||
|
|
||||||
fecther.open(connection, "mysql-bin.000001", 4L, 2);
|
fecther.open(connection, "mysql-bin.000006", 120L, 2);
|
||||||
|
|
||||||
LogDecoder decoder = new LogDecoder(LogEvent.UNKNOWN_EVENT, LogEvent.ENUM_END_EVENT);
|
LogDecoder decoder = new LogDecoder(LogEvent.UNKNOWN_EVENT, LogEvent.UNKNOWN_EVENT);
|
||||||
LogContext context = new LogContext();
|
LogContext context = new LogContext();
|
||||||
while (fecther.fetch()) {
|
while (fecther.fetch()) {
|
||||||
LogEvent event = null;
|
decoder.decode(fecther, context);
|
||||||
event = decoder.decode(fecther, context);
|
|
||||||
|
|
||||||
if (event == null) {
|
|
||||||
continue;
|
continue;
|
||||||
// throw new RuntimeException("parse failed");
|
// if (event == null) {
|
||||||
}
|
// continue;
|
||||||
|
// }
|
||||||
int eventType = event.getHeader().getType();
|
//
|
||||||
switch (eventType) {
|
// int eventType = event.getHeader().getType();
|
||||||
case LogEvent.ROTATE_EVENT:
|
// switch (eventType) {
|
||||||
binlogFileName = ((RotateLogEvent) event).getFilename();
|
// case LogEvent.ROTATE_EVENT:
|
||||||
break;
|
// binlogFileName = ((RotateLogEvent) event).getFilename();
|
||||||
case LogEvent.WRITE_ROWS_EVENT_V1:
|
// break;
|
||||||
case LogEvent.WRITE_ROWS_EVENT:
|
// case LogEvent.WRITE_ROWS_EVENT_V1:
|
||||||
parseRowsEvent((WriteRowsLogEvent) event);
|
// case LogEvent.WRITE_ROWS_EVENT:
|
||||||
break;
|
// parseRowsEvent((WriteRowsLogEvent) event);
|
||||||
case LogEvent.UPDATE_ROWS_EVENT_V1:
|
// break;
|
||||||
case LogEvent.UPDATE_ROWS_EVENT:
|
// case LogEvent.UPDATE_ROWS_EVENT_V1:
|
||||||
parseRowsEvent((UpdateRowsLogEvent) event);
|
// case LogEvent.UPDATE_ROWS_EVENT:
|
||||||
break;
|
// parseRowsEvent((UpdateRowsLogEvent) event);
|
||||||
case LogEvent.DELETE_ROWS_EVENT_V1:
|
// break;
|
||||||
case LogEvent.DELETE_ROWS_EVENT:
|
// case LogEvent.DELETE_ROWS_EVENT_V1:
|
||||||
parseRowsEvent((DeleteRowsLogEvent) event);
|
// case LogEvent.DELETE_ROWS_EVENT:
|
||||||
break;
|
// parseRowsEvent((DeleteRowsLogEvent) event);
|
||||||
case LogEvent.QUERY_EVENT:
|
// break;
|
||||||
parseQueryEvent((QueryLogEvent) event);
|
// case LogEvent.QUERY_EVENT:
|
||||||
break;
|
// parseQueryEvent((QueryLogEvent) event);
|
||||||
case LogEvent.ROWS_QUERY_LOG_EVENT:
|
// break;
|
||||||
parseRowsQueryEvent((RowsQueryLogEvent) event);
|
// case LogEvent.ROWS_QUERY_LOG_EVENT:
|
||||||
break;
|
// parseRowsQueryEvent((RowsQueryLogEvent) event);
|
||||||
case LogEvent.ANNOTATE_ROWS_EVENT:
|
// break;
|
||||||
parseAnnotateRowsEvent((AnnotateRowsEvent) event);
|
// case LogEvent.ANNOTATE_ROWS_EVENT:
|
||||||
break;
|
// parseAnnotateRowsEvent((AnnotateRowsEvent) event);
|
||||||
case LogEvent.XID_EVENT:
|
// break;
|
||||||
parseXidEvent((XidLogEvent) event);
|
// case LogEvent.XID_EVENT:
|
||||||
break;
|
// parseXidEvent((XidLogEvent) event);
|
||||||
default:
|
// break;
|
||||||
break;
|
// default:
|
||||||
}
|
// break;
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|||||||
5
deployer/src/main/bin/restart.sh
Normal file
5
deployer/src/main/bin/restart.sh
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
sh stop.sh
|
||||||
|
|
||||||
|
sh startup.sh
|
||||||
@@ -16,6 +16,7 @@ public class CanalConstants {
|
|||||||
public static final String CANAL_IP = ROOT + "." + "ip";
|
public static final String CANAL_IP = ROOT + "." + "ip";
|
||||||
public static final String CANAL_PORT = ROOT + "." + "port";
|
public static final String CANAL_PORT = ROOT + "." + "port";
|
||||||
public static final String CANAL_ZKSERVERS = ROOT + "." + "zkServers";
|
public static final String CANAL_ZKSERVERS = ROOT + "." + "zkServers";
|
||||||
|
public static final String CANAL_WITHOUT_NETTY = ROOT + "." + "withoutNetty";
|
||||||
|
|
||||||
public static final String CANAL_DESTINATIONS = ROOT + "." + "destinations";
|
public static final String CANAL_DESTINATIONS = ROOT + "." + "destinations";
|
||||||
public static final String CANAL_AUTO_SCAN = ROOT + "." + "auto.scan";
|
public static final String CANAL_AUTO_SCAN = ROOT + "." + "auto.scan";
|
||||||
|
|||||||
@@ -97,9 +97,12 @@ public class CanalController {
|
|||||||
port = Integer.valueOf(getProperty(properties, CanalConstants.CANAL_PORT));
|
port = Integer.valueOf(getProperty(properties, CanalConstants.CANAL_PORT));
|
||||||
embededCanalServer = CanalServerWithEmbedded.instance();
|
embededCanalServer = CanalServerWithEmbedded.instance();
|
||||||
embededCanalServer.setCanalInstanceGenerator(instanceGenerator);// 设置自定义的instanceGenerator
|
embededCanalServer.setCanalInstanceGenerator(instanceGenerator);// 设置自定义的instanceGenerator
|
||||||
|
String canalWithoutNetty = getProperty(properties, CanalConstants.CANAL_WITHOUT_NETTY);
|
||||||
|
if (canalWithoutNetty == null || "false".equals(canalWithoutNetty)) {
|
||||||
canalServer = CanalServerWithNetty.instance();
|
canalServer = CanalServerWithNetty.instance();
|
||||||
canalServer.setIp(ip);
|
canalServer.setIp(ip);
|
||||||
canalServer.setPort(port);
|
canalServer.setPort(port);
|
||||||
|
}
|
||||||
|
|
||||||
// 处理下ip为空,默认使用hostIp暴露到zk中
|
// 处理下ip为空,默认使用hostIp暴露到zk中
|
||||||
if (StringUtils.isEmpty(ip)) {
|
if (StringUtils.isEmpty(ip)) {
|
||||||
@@ -431,11 +434,15 @@ public class CanalController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 启动网络接口
|
// 启动网络接口
|
||||||
|
if (canalServer != null) {
|
||||||
canalServer.start();
|
canalServer.start();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void stop() throws Throwable {
|
public void stop() throws Throwable {
|
||||||
|
if (canalServer != null) {
|
||||||
canalServer.stop();
|
canalServer.stop();
|
||||||
|
}
|
||||||
|
|
||||||
if (autoScan) {
|
if (autoScan) {
|
||||||
for (InstanceConfigMonitor monitor : instanceConfigMonitors.values()) {
|
for (InstanceConfigMonitor monitor : instanceConfigMonitors.values()) {
|
||||||
@@ -454,6 +461,10 @@ public class CanalController {
|
|||||||
// 释放canal的工作节点
|
// 释放canal的工作节点
|
||||||
releaseCid(ZookeeperPathUtils.getCanalClusterNode(ip + ":" + port));
|
releaseCid(ZookeeperPathUtils.getCanalClusterNode(ip + ":" + port));
|
||||||
logger.info("## stop the canal server[{}:{}]", ip, port);
|
logger.info("## stop the canal server[{}:{}]", ip, port);
|
||||||
|
|
||||||
|
if (zkclientx != null) {
|
||||||
|
zkclientx.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initCid(String path) {
|
private void initCid(String path) {
|
||||||
|
|||||||
@@ -59,6 +59,7 @@ public class CanalLauncher {
|
|||||||
|
|
||||||
private static void setGlobalUncaughtExceptionHandler() {
|
private static void setGlobalUncaughtExceptionHandler() {
|
||||||
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
|
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void uncaughtException(Thread t, Throwable e) {
|
public void uncaughtException(Thread t, Throwable e) {
|
||||||
logger.error("UnCaughtException", e);
|
logger.error("UnCaughtException", e);
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ canal.port= 11111
|
|||||||
canal.zkServers=
|
canal.zkServers=
|
||||||
# flush data to zk
|
# flush data to zk
|
||||||
canal.zookeeper.flush.period = 1000
|
canal.zookeeper.flush.period = 1000
|
||||||
|
canal.withoutNetty = false
|
||||||
# flush meta cursor/parse position to file
|
# flush meta cursor/parse position to file
|
||||||
canal.file.data.dir = ${canal.conf.dir}
|
canal.file.data.dir = ${canal.conf.dir}
|
||||||
canal.file.flush.period = 1000
|
canal.file.flush.period = 1000
|
||||||
@@ -50,6 +51,13 @@ canal.instance.binlog.image = FULL,MINIMAL,NOBLOB
|
|||||||
# binlog ddl isolation
|
# binlog ddl isolation
|
||||||
canal.instance.get.ddl.isolation = false
|
canal.instance.get.ddl.isolation = false
|
||||||
|
|
||||||
|
# parallel parser config
|
||||||
|
canal.instance.parser.parallel = true
|
||||||
|
## concurrent thread number, default 60% available processors, suggest not to exceed Runtime.getRuntime().availableProcessors()
|
||||||
|
#canal.instance.parser.parallelThreadSize = 16
|
||||||
|
## disruptor ringbuffer size, must be power of 2
|
||||||
|
canal.instance.parser.parallelBufferSize = 256
|
||||||
|
|
||||||
#################################################
|
#################################################
|
||||||
######### destinations #############
|
######### destinations #############
|
||||||
#################################################
|
#################################################
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
#################################################
|
#################################################
|
||||||
## mysql serverId
|
## mysql serverId
|
||||||
canal.instance.mysql.slaveId=0
|
canal.instance.mysql.slaveId=0
|
||||||
|
|
||||||
# position info
|
# position info
|
||||||
canal.instance.master.address=127.0.0.1:3306
|
canal.instance.master.address=127.0.0.1:3306
|
||||||
|
# enable gtid use true/false
|
||||||
|
canal.instance.gtidon=false
|
||||||
canal.instance.master.journal.name=
|
canal.instance.master.journal.name=
|
||||||
canal.instance.master.position=
|
canal.instance.master.position=
|
||||||
canal.instance.master.timestamp=
|
canal.instance.master.timestamp=
|
||||||
|
canal.instance.master.gtid=
|
||||||
|
|
||||||
# table meta tsdb info
|
# table meta tsdb info
|
||||||
canal.instance.tsdb.enable=true
|
canal.instance.tsdb.enable=true
|
||||||
@@ -21,6 +24,7 @@ canal.instance.tsdb.dbPassword=canal
|
|||||||
#canal.instance.standby.journal.name =
|
#canal.instance.standby.journal.name =
|
||||||
#canal.instance.standby.position =
|
#canal.instance.standby.position =
|
||||||
#canal.instance.standby.timestamp =
|
#canal.instance.standby.timestamp =
|
||||||
|
#canal.instance.standby.gtid=
|
||||||
# username/password
|
# username/password
|
||||||
canal.instance.dbUsername=canal
|
canal.instance.dbUsername=canal
|
||||||
canal.instance.dbPassword=canal
|
canal.instance.dbPassword=canal
|
||||||
|
|||||||
@@ -73,6 +73,10 @@
|
|||||||
<level value="INFO" />
|
<level value="INFO" />
|
||||||
<appender-ref ref="CANAL-META" />
|
<appender-ref ref="CANAL-META" />
|
||||||
</logger>
|
</logger>
|
||||||
|
<logger name="com.alibaba.otter.canal.kafka" additivity="false">
|
||||||
|
<level value="INFO" />
|
||||||
|
<appender-ref ref="CANAL-ROOT" />
|
||||||
|
</logger>
|
||||||
|
|
||||||
<root level="WARN">
|
<root level="WARN">
|
||||||
<appender-ref ref="STDOUT"/>
|
<appender-ref ref="STDOUT"/>
|
||||||
|
|||||||
@@ -166,6 +166,7 @@
|
|||||||
<property name="journalName" value="${canal.instance.master.journal.name}" />
|
<property name="journalName" value="${canal.instance.master.journal.name}" />
|
||||||
<property name="position" value="${canal.instance.master.position}" />
|
<property name="position" value="${canal.instance.master.position}" />
|
||||||
<property name="timestamp" value="${canal.instance.master.timestamp}" />
|
<property name="timestamp" value="${canal.instance.master.timestamp}" />
|
||||||
|
<property name="gtid" value="${canal.instance.master.gtid}" />
|
||||||
</bean>
|
</bean>
|
||||||
</property>
|
</property>
|
||||||
<property name="standbyPosition">
|
<property name="standbyPosition">
|
||||||
@@ -173,6 +174,7 @@
|
|||||||
<property name="journalName" value="${canal.instance.standby.journal.name}" />
|
<property name="journalName" value="${canal.instance.standby.journal.name}" />
|
||||||
<property name="position" value="${canal.instance.standby.position}" />
|
<property name="position" value="${canal.instance.standby.position}" />
|
||||||
<property name="timestamp" value="${canal.instance.standby.timestamp}" />
|
<property name="timestamp" value="${canal.instance.standby.timestamp}" />
|
||||||
|
<property name="gtid" value="${canal.instance.standby.gtid}" />
|
||||||
</bean>
|
</bean>
|
||||||
</property>
|
</property>
|
||||||
<property name="filterQueryDml" value="${canal.instance.filter.query.dml:false}" />
|
<property name="filterQueryDml" value="${canal.instance.filter.query.dml:false}" />
|
||||||
@@ -187,5 +189,13 @@
|
|||||||
<!--表结构相关-->
|
<!--表结构相关-->
|
||||||
<property name="enableTsdb" value="${canal.instance.tsdb.enable:true}"/>
|
<property name="enableTsdb" value="${canal.instance.tsdb.enable:true}"/>
|
||||||
<property name="tsdbSpringXml" value="${canal.instance.tsdb.spring.xml:}"/>
|
<property name="tsdbSpringXml" value="${canal.instance.tsdb.spring.xml:}"/>
|
||||||
|
|
||||||
|
<!--是否启用GTID模式-->
|
||||||
|
<property name="isGTIDMode" value="${canal.instance.gtidon:false}"/>
|
||||||
|
|
||||||
|
<!-- parallel parser -->
|
||||||
|
<property name="parallel" value="${canal.instance.parser.parallel:true}" />
|
||||||
|
<property name="parallelThreadSize" value="${canal.instance.parser.parallelThreadSize}" />
|
||||||
|
<property name="parallelBufferSize" value="${canal.instance.parser.parallelBufferSize:256}" />
|
||||||
</bean>
|
</bean>
|
||||||
</beans>
|
</beans>
|
||||||
|
|||||||
@@ -151,6 +151,7 @@
|
|||||||
<property name="journalName" value="${canal.instance.master.journal.name}" />
|
<property name="journalName" value="${canal.instance.master.journal.name}" />
|
||||||
<property name="position" value="${canal.instance.master.position}" />
|
<property name="position" value="${canal.instance.master.position}" />
|
||||||
<property name="timestamp" value="${canal.instance.master.timestamp}" />
|
<property name="timestamp" value="${canal.instance.master.timestamp}" />
|
||||||
|
<property name="gtid" value="${canal.instance.master.gtid}" />
|
||||||
</bean>
|
</bean>
|
||||||
</property>
|
</property>
|
||||||
<property name="standbyPosition">
|
<property name="standbyPosition">
|
||||||
@@ -158,6 +159,7 @@
|
|||||||
<property name="journalName" value="${canal.instance.standby.journal.name}" />
|
<property name="journalName" value="${canal.instance.standby.journal.name}" />
|
||||||
<property name="position" value="${canal.instance.standby.position}" />
|
<property name="position" value="${canal.instance.standby.position}" />
|
||||||
<property name="timestamp" value="${canal.instance.standby.timestamp}" />
|
<property name="timestamp" value="${canal.instance.standby.timestamp}" />
|
||||||
|
<property name="gtid" value="${canal.instance.standby.gtid}" />
|
||||||
</bean>
|
</bean>
|
||||||
</property>
|
</property>
|
||||||
<property name="filterQueryDml" value="${canal.instance.filter.query.dml:false}" />
|
<property name="filterQueryDml" value="${canal.instance.filter.query.dml:false}" />
|
||||||
@@ -172,5 +174,13 @@
|
|||||||
<!--表结构相关-->
|
<!--表结构相关-->
|
||||||
<property name="enableTsdb" value="${canal.instance.tsdb.enable:true}"/>
|
<property name="enableTsdb" value="${canal.instance.tsdb.enable:true}"/>
|
||||||
<property name="tsdbSpringXml" value="${canal.instance.tsdb.spring.xml:}"/>
|
<property name="tsdbSpringXml" value="${canal.instance.tsdb.spring.xml:}"/>
|
||||||
|
|
||||||
|
<!--是否启用GTID模式-->
|
||||||
|
<property name="isGTIDMode" value="${canal.instance.gtidon:false}"/>
|
||||||
|
|
||||||
|
<!-- parallel parser -->
|
||||||
|
<property name="parallel" value="${canal.instance.parser.parallel:true}" />
|
||||||
|
<property name="parallelThreadSize" value="${canal.instance.parser.parallelThreadSize}" />
|
||||||
|
<property name="parallelBufferSize" value="${canal.instance.parser.parallelBufferSize:256}" />
|
||||||
</bean>
|
</bean>
|
||||||
</beans>
|
</beans>
|
||||||
|
|||||||
@@ -148,6 +148,7 @@
|
|||||||
<property name="journalName" value="${canal.instance.master1.journal.name}" />
|
<property name="journalName" value="${canal.instance.master1.journal.name}" />
|
||||||
<property name="position" value="${canal.instance.master1.position}" />
|
<property name="position" value="${canal.instance.master1.position}" />
|
||||||
<property name="timestamp" value="${canal.instance.master1.timestamp}" />
|
<property name="timestamp" value="${canal.instance.master1.timestamp}" />
|
||||||
|
<property name="gtid" value="${canal.instance.master1.gtid}" />
|
||||||
</bean>
|
</bean>
|
||||||
</property>
|
</property>
|
||||||
<property name="standbyPosition">
|
<property name="standbyPosition">
|
||||||
@@ -155,6 +156,7 @@
|
|||||||
<property name="journalName" value="${canal.instance.standby1.journal.name}" />
|
<property name="journalName" value="${canal.instance.standby1.journal.name}" />
|
||||||
<property name="position" value="${canal.instance.standby1.position}" />
|
<property name="position" value="${canal.instance.standby1.position}" />
|
||||||
<property name="timestamp" value="${canal.instance.standby1.timestamp}" />
|
<property name="timestamp" value="${canal.instance.standby1.timestamp}" />
|
||||||
|
<property name="gtid" value="${canal.instance.standby1.gtid}" />
|
||||||
</bean>
|
</bean>
|
||||||
</property>
|
</property>
|
||||||
<property name="filterQueryDml" value="${canal.instance.filter.query.dml:false}" />
|
<property name="filterQueryDml" value="${canal.instance.filter.query.dml:false}" />
|
||||||
@@ -164,6 +166,11 @@
|
|||||||
<property name="filterTableError" value="${canal.instance.filter.table.error:false}" />
|
<property name="filterTableError" value="${canal.instance.filter.table.error:false}" />
|
||||||
<property name="supportBinlogFormats" value="${canal.instance.binlog.format}" />
|
<property name="supportBinlogFormats" value="${canal.instance.binlog.format}" />
|
||||||
<property name="supportBinlogImages" value="${canal.instance.binlog.image}" />
|
<property name="supportBinlogImages" value="${canal.instance.binlog.image}" />
|
||||||
|
|
||||||
|
<!-- parallel parser -->
|
||||||
|
<property name="parallel" value="${canal.instance.parser.parallel:true}" />
|
||||||
|
<property name="parallelThreadSize" value="${canal.instance.parser.parallelThreadSize}" />
|
||||||
|
<property name="parallelBufferSize" value="${canal.instance.parser.parallelBufferSize:256}" />
|
||||||
</bean>
|
</bean>
|
||||||
|
|
||||||
<bean id="eventParser2" class="com.alibaba.otter.canal.parse.inbound.mysql.MysqlEventParser">
|
<bean id="eventParser2" class="com.alibaba.otter.canal.parse.inbound.mysql.MysqlEventParser">
|
||||||
@@ -239,6 +246,7 @@
|
|||||||
<property name="journalName" value="${canal.instance.master2.journal.name}" />
|
<property name="journalName" value="${canal.instance.master2.journal.name}" />
|
||||||
<property name="position" value="${canal.instance.master2.position}" />
|
<property name="position" value="${canal.instance.master2.position}" />
|
||||||
<property name="timestamp" value="${canal.instance.master2.timestamp}" />
|
<property name="timestamp" value="${canal.instance.master2.timestamp}" />
|
||||||
|
<property name="gtid" value="${canal.instance.master2.gtid}" />
|
||||||
</bean>
|
</bean>
|
||||||
</property>
|
</property>
|
||||||
<property name="standbyPosition">
|
<property name="standbyPosition">
|
||||||
@@ -246,6 +254,7 @@
|
|||||||
<property name="journalName" value="${canal.instance.standby2.journal.name}" />
|
<property name="journalName" value="${canal.instance.standby2.journal.name}" />
|
||||||
<property name="position" value="${canal.instance.standby2.position}" />
|
<property name="position" value="${canal.instance.standby2.position}" />
|
||||||
<property name="timestamp" value="${canal.instance.standby2.timestamp}" />
|
<property name="timestamp" value="${canal.instance.standby2.timestamp}" />
|
||||||
|
<property name="gtid" value="${canal.instance.standby2.gtid}" />
|
||||||
</bean>
|
</bean>
|
||||||
</property>
|
</property>
|
||||||
<property name="filterQueryDml" value="${canal.instance.filter.query.dml:false}" />
|
<property name="filterQueryDml" value="${canal.instance.filter.query.dml:false}" />
|
||||||
@@ -256,5 +265,10 @@
|
|||||||
<property name="filterTableError" value="${canal.instance.filter.table.error:false}" />
|
<property name="filterTableError" value="${canal.instance.filter.table.error:false}" />
|
||||||
<property name="supportBinlogFormats" value="${canal.instance.binlog.format}" />
|
<property name="supportBinlogFormats" value="${canal.instance.binlog.format}" />
|
||||||
<property name="supportBinlogImages" value="${canal.instance.binlog.image}" />
|
<property name="supportBinlogImages" value="${canal.instance.binlog.image}" />
|
||||||
|
|
||||||
|
<!-- parallel parser -->
|
||||||
|
<property name="parallel" value="${canal.instance.parser.parallel:true}" />
|
||||||
|
<property name="parallelThreadSize" value="${canal.instance.parser.parallelThreadSize}" />
|
||||||
|
<property name="parallelBufferSize" value="${canal.instance.parser.parallelBufferSize:256}" />
|
||||||
</bean>
|
</bean>
|
||||||
</beans>
|
</beans>
|
||||||
|
|||||||
@@ -137,5 +137,10 @@
|
|||||||
<!--表结构相关-->
|
<!--表结构相关-->
|
||||||
<property name="enableTsdb" value="${canal.instance.tsdb.enable:true}"/>
|
<property name="enableTsdb" value="${canal.instance.tsdb.enable:true}"/>
|
||||||
<property name="tsdbSpringXml" value="${canal.instance.tsdb.spring.xml:}"/>
|
<property name="tsdbSpringXml" value="${canal.instance.tsdb.spring.xml:}"/>
|
||||||
|
|
||||||
|
<!-- parallel parser -->
|
||||||
|
<property name="parallel" value="${canal.instance.parser.parallel:true}" />
|
||||||
|
<property name="parallelThreadSize" value="${canal.instance.parser.parallelThreadSize}" />
|
||||||
|
<property name="parallelBufferSize" value="${canal.instance.parser.parallelBufferSize:256}" />
|
||||||
</bean>
|
</bean>
|
||||||
</beans>
|
</beans>
|
||||||
@@ -139,6 +139,7 @@
|
|||||||
<property name="journalName" value="${canal.instance.master.journal.name}" />
|
<property name="journalName" value="${canal.instance.master.journal.name}" />
|
||||||
<property name="position" value="${canal.instance.master.position}" />
|
<property name="position" value="${canal.instance.master.position}" />
|
||||||
<property name="timestamp" value="${canal.instance.master.timestamp}" />
|
<property name="timestamp" value="${canal.instance.master.timestamp}" />
|
||||||
|
<property name="gtid" value="${canal.instance.master.gtid}" />
|
||||||
</bean>
|
</bean>
|
||||||
</property>
|
</property>
|
||||||
<property name="standbyPosition">
|
<property name="standbyPosition">
|
||||||
@@ -146,6 +147,7 @@
|
|||||||
<property name="journalName" value="${canal.instance.standby.journal.name}" />
|
<property name="journalName" value="${canal.instance.standby.journal.name}" />
|
||||||
<property name="position" value="${canal.instance.standby.position}" />
|
<property name="position" value="${canal.instance.standby.position}" />
|
||||||
<property name="timestamp" value="${canal.instance.standby.timestamp}" />
|
<property name="timestamp" value="${canal.instance.standby.timestamp}" />
|
||||||
|
<property name="gtid" value="${canal.instance.standby.gtid}" />
|
||||||
</bean>
|
</bean>
|
||||||
</property>
|
</property>
|
||||||
<property name="filterQueryDml" value="${canal.instance.filter.query.dml:false}" />
|
<property name="filterQueryDml" value="${canal.instance.filter.query.dml:false}" />
|
||||||
@@ -160,5 +162,13 @@
|
|||||||
<!--表结构相关-->
|
<!--表结构相关-->
|
||||||
<property name="enableTsdb" value="${canal.instance.tsdb.enable:false}"/>
|
<property name="enableTsdb" value="${canal.instance.tsdb.enable:false}"/>
|
||||||
<property name="tsdbSpringXml" value="${canal.instance.tsdb.spring.xml:}"/>
|
<property name="tsdbSpringXml" value="${canal.instance.tsdb.spring.xml:}"/>
|
||||||
|
|
||||||
|
<!--是否启用GTID模式-->
|
||||||
|
<property name="isGTIDMode" value="${canal.instance.gtidon:false}"/>
|
||||||
|
|
||||||
|
<!-- parallel parser -->
|
||||||
|
<property name="parallel" value="${canal.instance.parser.parallel:true}" />
|
||||||
|
<property name="parallelThreadSize" value="${canal.instance.parser.parallelThreadSize}" />
|
||||||
|
<property name="parallelBufferSize" value="${canal.instance.parser.parallelBufferSize:256}" />
|
||||||
</bean>
|
</bean>
|
||||||
</beans>
|
</beans>
|
||||||
|
|||||||
@@ -31,8 +31,8 @@
|
|||||||
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
|
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
|
||||||
<property name="driverClassName" value="org.h2.Driver" />
|
<property name="driverClassName" value="org.h2.Driver" />
|
||||||
<property name="url" value="${canal.instance.tsdb.url:}" />
|
<property name="url" value="${canal.instance.tsdb.url:}" />
|
||||||
<property name="username" value="${canal.instance.tsdb.dbUsername:canal}" />
|
<property name="username" value="${canal.instance.tsdb.dbUsername:}" />
|
||||||
<property name="password" value="${canal.instance.tsdb.dbPassword:canal}" />
|
<property name="password" value="${canal.instance.tsdb.dbPassword:}" />
|
||||||
<property name="maxActive" value="30" />
|
<property name="maxActive" value="30" />
|
||||||
<property name="initialSize" value="0" />
|
<property name="initialSize" value="0" />
|
||||||
<property name="minIdle" value="1" />
|
<property name="minIdle" value="1" />
|
||||||
|
|||||||
@@ -31,8 +31,8 @@
|
|||||||
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
|
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
|
||||||
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
|
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
|
||||||
<property name="url" value="${canal.instance.tsdb.url:}" />
|
<property name="url" value="${canal.instance.tsdb.url:}" />
|
||||||
<property name="username" value="${canal.instance.tsdb.dbUsername:canal}" />
|
<property name="username" value="${canal.instance.tsdb.dbUsername:}" />
|
||||||
<property name="password" value="${canal.instance.tsdb.dbPassword:canal}" />
|
<property name="password" value="${canal.instance.tsdb.dbPassword:}" />
|
||||||
<property name="maxActive" value="30" />
|
<property name="maxActive" value="30" />
|
||||||
<property name="initialSize" value="0" />
|
<property name="initialSize" value="0" />
|
||||||
<property name="minIdle" value="1" />
|
<property name="minIdle" value="1" />
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package com.alibaba.otter.canal.parse.driver.mysql.packets;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by hiwjd on 2018/4/23. hiwjd0@gmail.com
|
||||||
|
*/
|
||||||
|
public interface GTIDSet {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 序列化成字节数组
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
byte[] encode() throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更新当前实例
|
||||||
|
*
|
||||||
|
* @param str
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
void update(String str);
|
||||||
|
}
|
||||||
@@ -0,0 +1,116 @@
|
|||||||
|
package com.alibaba.otter.canal.parse.driver.mysql.packets;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.utils.ByteHelper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by hiwjd on 2018/4/23. hiwjd0@gmail.com
|
||||||
|
*/
|
||||||
|
public class MysqlGTIDSet implements GTIDSet {
|
||||||
|
|
||||||
|
public Map<String, UUIDSet> sets;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] encode() throws IOException {
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
ByteHelper.writeUnsignedInt64LittleEndian(sets.size(), out);
|
||||||
|
|
||||||
|
for (Map.Entry<String, UUIDSet> entry : sets.entrySet()) {
|
||||||
|
out.write(entry.getValue().encode());
|
||||||
|
}
|
||||||
|
|
||||||
|
return out.toByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void update(String str) {
|
||||||
|
UUIDSet us = UUIDSet.parse(str);
|
||||||
|
String sid = us.SID.toString();
|
||||||
|
if (sets.containsKey(sid)) {
|
||||||
|
sets.get(sid).intervals.addAll(us.intervals);
|
||||||
|
sets.get(sid).intervals = UUIDSet.combine(sets.get(sid).intervals);
|
||||||
|
} else {
|
||||||
|
sets.put(sid, us);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (o == null) return false;
|
||||||
|
if (this == o) return true;
|
||||||
|
|
||||||
|
MysqlGTIDSet gs = (MysqlGTIDSet) o;
|
||||||
|
if (gs.sets == null) return false;
|
||||||
|
|
||||||
|
for (Map.Entry<String, UUIDSet> entry : sets.entrySet()) {
|
||||||
|
if (!entry.getValue().equals(gs.sets.get(entry.getKey()))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析如下格式的字符串为MysqlGTIDSet: 726757ad-4455-11e8-ae04-0242ac110002:1 =>
|
||||||
|
* MysqlGTIDSet{ sets: { 726757ad-4455-11e8-ae04-0242ac110002: UUIDSet{ SID:
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110002, intervals: [{start:1, stop:2}] } }
|
||||||
|
* } 726757ad-4455-11e8-ae04-0242ac110002:1-3 => MysqlGTIDSet{ sets: {
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110002: UUIDSet{ SID:
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110002, intervals: [{start:1, stop:4}] } }
|
||||||
|
* } 726757ad-4455-11e8-ae04-0242ac110002:1-3:4 => MysqlGTIDSet{ sets: {
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110002: UUIDSet{ SID:
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110002, intervals: [{start:1, stop:5}] } }
|
||||||
|
* } 726757ad-4455-11e8-ae04-0242ac110002:1-3:7-9 => MysqlGTIDSet{ sets: {
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110002: UUIDSet{ SID:
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110002, intervals: [{start:1, stop:4},
|
||||||
|
* {start:7, stop: 10}] } } }
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110002:1-3,726757
|
||||||
|
* ad-4455-11e8-ae04-0242ac110003:4 => MysqlGTIDSet{ sets: {
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110002: UUIDSet{ SID:
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110002, intervals: [{start:1, stop:4}] },
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110003: UUIDSet{ SID:
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110002, intervals: [{start:4, stop:5}] } }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* @param gtidData
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static MysqlGTIDSet parse(String gtidData) {
|
||||||
|
Map<String, UUIDSet> m;
|
||||||
|
|
||||||
|
if (gtidData == null || gtidData.length() < 1) {
|
||||||
|
m = new HashMap<String, UUIDSet>();
|
||||||
|
} else {
|
||||||
|
String[] uuidStrs = gtidData.split(",");
|
||||||
|
m = new HashMap<String, UUIDSet>(uuidStrs.length);
|
||||||
|
for (int i = 0; i < uuidStrs.length; i++) {
|
||||||
|
UUIDSet uuidSet = UUIDSet.parse(uuidStrs[i]);
|
||||||
|
m.put(uuidSet.SID.toString(), uuidSet);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MysqlGTIDSet gs = new MysqlGTIDSet();
|
||||||
|
gs.sets = m;
|
||||||
|
|
||||||
|
return gs;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
for (Map.Entry<String, UUIDSet> entry : sets.entrySet()) {
|
||||||
|
if (sb.length() > 0) {
|
||||||
|
sb.append(",");
|
||||||
|
}
|
||||||
|
sb.append(entry.getValue().toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,194 @@
|
|||||||
|
package com.alibaba.otter.canal.parse.driver.mysql.packets;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.utils.ByteHelper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by hiwjd on 2018/4/23. hiwjd0@gmail.com
|
||||||
|
*/
|
||||||
|
public class UUIDSet {
|
||||||
|
|
||||||
|
public UUID SID;
|
||||||
|
public List<Interval> intervals;
|
||||||
|
|
||||||
|
public byte[] encode() throws IOException {
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
|
||||||
|
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
|
||||||
|
bb.putLong(SID.getMostSignificantBits());
|
||||||
|
bb.putLong(SID.getLeastSignificantBits());
|
||||||
|
|
||||||
|
out.write(bb.array());
|
||||||
|
|
||||||
|
ByteHelper.writeUnsignedInt64LittleEndian(intervals.size(), out);
|
||||||
|
|
||||||
|
for (Interval interval : intervals) {
|
||||||
|
ByteHelper.writeUnsignedInt64LittleEndian(interval.start, out);
|
||||||
|
ByteHelper.writeUnsignedInt64LittleEndian(interval.stop, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
return out.toByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (o == null) return false;
|
||||||
|
if (this == o) return true;
|
||||||
|
|
||||||
|
UUIDSet us = (UUIDSet) o;
|
||||||
|
Collections.sort(intervals);
|
||||||
|
Collections.sort(us.intervals);
|
||||||
|
if (SID.equals(us.SID) && intervals.equals(us.intervals)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Interval implements Comparable<Interval> {
|
||||||
|
|
||||||
|
public long start;
|
||||||
|
public long stop;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
|
Interval interval = (Interval) o;
|
||||||
|
|
||||||
|
if (start != interval.start) return false;
|
||||||
|
return stop == interval.stop;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int result = (int) (start ^ (start >>> 32));
|
||||||
|
result = 31 * result + (int) (stop ^ (stop >>> 32));
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Interval o) {
|
||||||
|
if (equals(o)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return start > o.start ? 1 : (start == o.start ? 0 : -1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析如下格式字符串为UUIDSet: 726757ad-4455-11e8-ae04-0242ac110002:1 => UUIDSet{SID:
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110002, intervals: [{start:1, stop:2}]}
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110002:1-3 => UUIDSet{SID:
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110002, intervals: [{start:1, stop:4}]}
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110002:1-3:4 UUIDSet{SID:
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110002, intervals: [{start:1, stop:5}]}
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110002:1-3:7-9 UUIDSet{SID:
|
||||||
|
* 726757ad-4455-11e8-ae04-0242ac110002, intervals: [{start:1, stop:4},
|
||||||
|
* {start:7, stop:10}]}
|
||||||
|
*
|
||||||
|
* @param str
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static UUIDSet parse(String str) {
|
||||||
|
String[] ss = str.split(":");
|
||||||
|
|
||||||
|
if (ss.length < 2) {
|
||||||
|
throw new RuntimeException(String.format("parseUUIDSet failed due to wrong format: %s", str));
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Interval> intervals = new ArrayList<Interval>();
|
||||||
|
for (int i = 1; i < ss.length; i++) {
|
||||||
|
intervals.add(parseInterval(ss[i]));
|
||||||
|
}
|
||||||
|
|
||||||
|
UUIDSet uuidSet = new UUIDSet();
|
||||||
|
uuidSet.SID = UUID.fromString(ss[0]);
|
||||||
|
uuidSet.intervals = combine(intervals);
|
||||||
|
|
||||||
|
return uuidSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
|
||||||
|
sb.append(SID.toString());
|
||||||
|
for (Interval interval : intervals) {
|
||||||
|
if (interval.start == interval.stop - 1) {
|
||||||
|
sb.append(":");
|
||||||
|
sb.append(interval.start);
|
||||||
|
} else {
|
||||||
|
sb.append(":");
|
||||||
|
sb.append(interval.start);
|
||||||
|
sb.append("-");
|
||||||
|
sb.append(interval.stop - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析如下格式字符串为Interval: 1 => Interval{start:1, stop:2} 1-3 =>
|
||||||
|
* Interval{start:1, stop:4} 注意!字符串格式表达时[n,m]是两侧都包含的,Interval表达时[n,m)右侧开
|
||||||
|
*
|
||||||
|
* @param str
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static Interval parseInterval(String str) {
|
||||||
|
String[] ss = str.split("-");
|
||||||
|
|
||||||
|
Interval interval = new Interval();
|
||||||
|
switch (ss.length) {
|
||||||
|
case 1:
|
||||||
|
interval.start = Long.parseLong(ss[0]);
|
||||||
|
interval.stop = interval.start + 1;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
interval.start = Long.parseLong(ss[0]);
|
||||||
|
interval.stop = Long.parseLong(ss[1]) + 1;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new RuntimeException(String.format("parseInterval failed due to wrong format: %s", str));
|
||||||
|
}
|
||||||
|
|
||||||
|
return interval;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 把{start,stop}连续的合并掉: [{start:1, stop:4},{start:4, stop:5}] => [{start:1,
|
||||||
|
* stop:5}]
|
||||||
|
*
|
||||||
|
* @param intervals
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static List<Interval> combine(List<Interval> intervals) {
|
||||||
|
List<Interval> combined = new ArrayList<Interval>();
|
||||||
|
Collections.sort(intervals);
|
||||||
|
int len = intervals.size();
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
combined.add(intervals.get(i));
|
||||||
|
|
||||||
|
int j;
|
||||||
|
for (j = i + 1; j < len; j++) {
|
||||||
|
if (intervals.get(i).stop >= intervals.get(j).start) {
|
||||||
|
intervals.get(i).stop = intervals.get(j).stop;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i = j - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return combined;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -54,7 +54,7 @@ public class BinlogDumpCommandPacket extends CommandPacket {
|
|||||||
// 1. write 4 bytes bin-log position to start at
|
// 1. write 4 bytes bin-log position to start at
|
||||||
ByteHelper.writeUnsignedIntLittleEndian(binlogPosition, out);
|
ByteHelper.writeUnsignedIntLittleEndian(binlogPosition, out);
|
||||||
// 2. write 2 bytes bin-log flags
|
// 2. write 2 bytes bin-log flags
|
||||||
int binlog_flags = 0;
|
int binlog_flags = BINLOG_DUMP_NON_BLOCK;
|
||||||
binlog_flags |= BINLOG_SEND_ANNOTATE_ROWS_EVENT;
|
binlog_flags |= BINLOG_SEND_ANNOTATE_ROWS_EVENT;
|
||||||
out.write(binlog_flags);
|
out.write(binlog_flags);
|
||||||
out.write(0x00);
|
out.write(0x00);
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
package com.alibaba.otter.canal.parse.driver.mysql.packets.client;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.CommandPacket;
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.GTIDSet;
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.utils.ByteHelper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by hiwjd on 2018/4/24. hiwjd0@gmail.com
|
||||||
|
* https://dev.mysql.com/doc/internals/en/com-binlog-dump-gtid.html
|
||||||
|
*/
|
||||||
|
public class BinlogDumpGTIDCommandPacket extends CommandPacket {
|
||||||
|
|
||||||
|
public static final int BINLOG_DUMP_NON_BLOCK = 0x01;
|
||||||
|
public static final int BINLOG_THROUGH_POSITION = 0x02;
|
||||||
|
public static final int BINLOG_THROUGH_GTID = 0x04;
|
||||||
|
|
||||||
|
public long slaveServerId;
|
||||||
|
public GTIDSet gtidSet;
|
||||||
|
|
||||||
|
public BinlogDumpGTIDCommandPacket(){
|
||||||
|
setCommand((byte) 0x1e);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fromBytes(byte[] data) throws IOException {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] toBytes() throws IOException {
|
||||||
|
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||||
|
|
||||||
|
// 0. [1] write command number
|
||||||
|
out.write(getCommand());
|
||||||
|
// 1. [2] flags
|
||||||
|
ByteHelper.writeUnsignedShortLittleEndian(BINLOG_THROUGH_GTID, out);
|
||||||
|
// 2. [4] server-id
|
||||||
|
ByteHelper.writeUnsignedIntLittleEndian(slaveServerId, out);
|
||||||
|
// 3. [4] binlog-filename-len
|
||||||
|
ByteHelper.writeUnsignedIntLittleEndian(0, out);
|
||||||
|
// 4. [] binlog-filename
|
||||||
|
// skip
|
||||||
|
// 5. [8] binlog-pos
|
||||||
|
ByteHelper.writeUnsignedInt64LittleEndian(4, out);
|
||||||
|
// if flags & BINLOG_THROUGH_GTID {
|
||||||
|
byte[] bs = gtidSet.encode();
|
||||||
|
// 6. [4] data-size
|
||||||
|
ByteHelper.writeUnsignedIntLittleEndian(bs.length, out);
|
||||||
|
// 7, [] data
|
||||||
|
// [8] n_sids // 文档写的是4个字节,其实是8个字节
|
||||||
|
// for n_sids {
|
||||||
|
// [16] SID
|
||||||
|
// [8] n_intervals
|
||||||
|
// for n_intervals {
|
||||||
|
// [8] start (signed)
|
||||||
|
// [8] end (signed)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
out.write(bs);
|
||||||
|
// }
|
||||||
|
|
||||||
|
return out.toByteArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.alibaba.otter.canal.parse.driver.mysql.socket;
|
package com.alibaba.otter.canal.parse.driver.mysql.socket;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
@@ -24,7 +25,7 @@ public class BioSocketChannel implements SocketChannel {
|
|||||||
|
|
||||||
BioSocketChannel(Socket socket) throws IOException{
|
BioSocketChannel(Socket socket) throws IOException{
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
this.input = socket.getInputStream();
|
this.input = new BufferedInputStream(socket.getInputStream(), 16384);
|
||||||
this.output = socket.getOutputStream();
|
this.output = socket.getOutputStream();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,6 +94,37 @@ public class BioSocketChannel implements SocketChannel {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(byte[] data, int off, int len, int timeout) throws IOException {
|
||||||
|
InputStream input = this.input;
|
||||||
|
int accTimeout = 0;
|
||||||
|
if (input == null) {
|
||||||
|
throw new SocketException("Socket already closed.");
|
||||||
|
}
|
||||||
|
|
||||||
|
int n = 0;
|
||||||
|
while (n < len && accTimeout < timeout) {
|
||||||
|
try {
|
||||||
|
int read = input.read(data, off + n, len - n);
|
||||||
|
if (read > -1) {
|
||||||
|
n += read;
|
||||||
|
} else {
|
||||||
|
throw new IOException("EOF encountered.");
|
||||||
|
}
|
||||||
|
} catch (SocketTimeoutException te) {
|
||||||
|
if (Thread.interrupted()) {
|
||||||
|
throw new ClosedByInterruptException();
|
||||||
|
}
|
||||||
|
accTimeout += SO_TIMEOUT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (n < len && accTimeout >= timeout) {
|
||||||
|
throw new SocketTimeoutException("Timeout occurred, failed to read " + len + " bytes in " + timeout
|
||||||
|
+ " milliseconds.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isConnected() {
|
public boolean isConnected() {
|
||||||
Socket socket = this.socket;
|
Socket socket = this.socket;
|
||||||
if (socket != null) {
|
if (socket != null) {
|
||||||
|
|||||||
@@ -11,8 +11,6 @@ public abstract class BioSocketChannelPool {
|
|||||||
|
|
||||||
public static BioSocketChannel open(SocketAddress address) throws Exception {
|
public static BioSocketChannel open(SocketAddress address) throws Exception {
|
||||||
Socket socket = new Socket();
|
Socket socket = new Socket();
|
||||||
socket.setReceiveBufferSize(32 * 1024);
|
|
||||||
socket.setSendBufferSize(32 * 1024);
|
|
||||||
socket.setSoTimeout(BioSocketChannel.SO_TIMEOUT);
|
socket.setSoTimeout(BioSocketChannel.SO_TIMEOUT);
|
||||||
socket.setTcpNoDelay(true);
|
socket.setTcpNoDelay(true);
|
||||||
socket.setKeepAlive(true);
|
socket.setKeepAlive(true);
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import io.netty.util.internal.SystemPropertyUtil;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.SocketAddress;
|
import java.net.SocketAddress;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.NotImplementedException;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@@ -202,6 +203,11 @@ public class NettySocketChannel implements SocketChannel {
|
|||||||
} while (true);
|
} while (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void read(byte[] data, int off, int len, int timeout) throws IOException {
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isConnected() {
|
public boolean isConnected() {
|
||||||
return channel != null ? true : false;
|
return channel != null ? true : false;
|
||||||
}
|
}
|
||||||
@@ -224,4 +230,5 @@ public class NettySocketChannel implements SocketChannel {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,11 +37,10 @@ public abstract class NettySocketChannelPool {
|
|||||||
static {
|
static {
|
||||||
boot.group(group)
|
boot.group(group)
|
||||||
.channel(NioSocketChannel.class)
|
.channel(NioSocketChannel.class)
|
||||||
.option(ChannelOption.SO_RCVBUF, 32 * 1024)
|
|
||||||
.option(ChannelOption.SO_SNDBUF, 32 * 1024)
|
|
||||||
.option(ChannelOption.TCP_NODELAY, true)
|
.option(ChannelOption.TCP_NODELAY, true)
|
||||||
// 如果是延时敏感型应用,建议关闭Nagle算法
|
// 如果是延时敏感型应用,建议关闭Nagle算法
|
||||||
.option(ChannelOption.SO_KEEPALIVE, true)
|
.option(ChannelOption.SO_KEEPALIVE, true)
|
||||||
|
.option(ChannelOption.SO_REUSEADDR, true)
|
||||||
.option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT)
|
.option(ChannelOption.RCVBUF_ALLOCATOR, AdaptiveRecvByteBufAllocator.DEFAULT)
|
||||||
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
|
.option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ public interface SocketChannel {
|
|||||||
|
|
||||||
public byte[] read(int readSize, int timeout) throws IOException;
|
public byte[] read(int readSize, int timeout) throws IOException;
|
||||||
|
|
||||||
|
public void read(byte[] data, int off, int len, int timeout) throws IOException;
|
||||||
|
|
||||||
public boolean isConnected();
|
public boolean isConnected();
|
||||||
|
|
||||||
public SocketAddress getRemoteSocketAddress();
|
public SocketAddress getRemoteSocketAddress();
|
||||||
|
|||||||
@@ -127,6 +127,17 @@ public abstract class ByteHelper {
|
|||||||
out.write((byte) (data >>> 24));
|
out.write((byte) (data >>> 24));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void writeUnsignedInt64LittleEndian(long data, ByteArrayOutputStream out) {
|
||||||
|
out.write((byte) (data & 0xFF));
|
||||||
|
out.write((byte) (data >>> 8));
|
||||||
|
out.write((byte) (data >>> 16));
|
||||||
|
out.write((byte) (data >>> 24));
|
||||||
|
out.write((byte) (data >>> 32));
|
||||||
|
out.write((byte) (data >>> 40));
|
||||||
|
out.write((byte) (data >>> 48));
|
||||||
|
out.write((byte) (data >>> 56));
|
||||||
|
}
|
||||||
|
|
||||||
public static void writeUnsignedShortLittleEndian(int data, ByteArrayOutputStream out) {
|
public static void writeUnsignedShortLittleEndian(int data, ByteArrayOutputStream out) {
|
||||||
out.write((byte) (data & 0xFF));
|
out.write((byte) (data & 0xFF));
|
||||||
out.write((byte) ((data >>> 8) & 0xFF));
|
out.write((byte) ((data >>> 8) & 0xFF));
|
||||||
|
|||||||
@@ -0,0 +1,119 @@
|
|||||||
|
package com.alibaba.otter.canal.parse.driver.mysql;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.GTIDSet;
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.MysqlGTIDSet;
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.UUIDSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by hiwjd on 2018/4/25. hiwjd0@gmail.com
|
||||||
|
*/
|
||||||
|
public class MysqlGTIDSetTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEncode() throws IOException {
|
||||||
|
GTIDSet gtidSet = MysqlGTIDSet.parse("726757ad-4455-11e8-ae04-0242ac110002:1");
|
||||||
|
byte[] bytes = gtidSet.encode();
|
||||||
|
|
||||||
|
byte[] expected = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x72, 0x67, 0x57, (byte) 0xad, 0x44, 0x55,
|
||||||
|
0x11, (byte) 0xe8, (byte) 0xae, 0x04, 0x02, 0x42, (byte) 0xac, 0x11, 0x00, 0x02, 0x01, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00 };
|
||||||
|
|
||||||
|
for (int i = 0; i < bytes.length; i++) {
|
||||||
|
assertEquals(expected[i], bytes[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testParse() {
|
||||||
|
Map<String, MysqlGTIDSet> cases = new HashMap<String, MysqlGTIDSet>(5);
|
||||||
|
cases.put("726757ad-4455-11e8-ae04-0242ac110002:1",
|
||||||
|
buildForTest(new Material("726757ad-4455-11e8-ae04-0242ac110002", 1, 2)));
|
||||||
|
cases.put("726757ad-4455-11e8-ae04-0242ac110002:1-3",
|
||||||
|
buildForTest(new Material("726757ad-4455-11e8-ae04-0242ac110002", 1, 4)));
|
||||||
|
cases.put("726757ad-4455-11e8-ae04-0242ac110002:1-3:4",
|
||||||
|
buildForTest(new Material("726757ad-4455-11e8-ae04-0242ac110002", 1, 5)));
|
||||||
|
cases.put("726757ad-4455-11e8-ae04-0242ac110002:1-3:7-9",
|
||||||
|
buildForTest(new Material("726757ad-4455-11e8-ae04-0242ac110002", 1, 4, 7, 10)));
|
||||||
|
cases.put("726757ad-4455-11e8-ae04-0242ac110002:1-3,726757ad-4455-11e8-ae04-0242ac110003:4",
|
||||||
|
buildForTest(Arrays.asList(new Material("726757ad-4455-11e8-ae04-0242ac110002", 1, 4),
|
||||||
|
new Material("726757ad-4455-11e8-ae04-0242ac110003", 4, 5))));
|
||||||
|
|
||||||
|
for (Map.Entry<String, MysqlGTIDSet> entry : cases.entrySet()) {
|
||||||
|
MysqlGTIDSet expected = entry.getValue();
|
||||||
|
MysqlGTIDSet actual = MysqlGTIDSet.parse(entry.getKey());
|
||||||
|
|
||||||
|
assertEquals(expected, actual);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class Material {
|
||||||
|
|
||||||
|
public Material(String uuid, long start, long stop){
|
||||||
|
this.uuid = uuid;
|
||||||
|
this.start = start;
|
||||||
|
this.stop = stop;
|
||||||
|
this.start1 = 0;
|
||||||
|
this.stop1 = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Material(String uuid, long start, long stop, long start1, long stop1){
|
||||||
|
this.uuid = uuid;
|
||||||
|
this.start = start;
|
||||||
|
this.stop = stop;
|
||||||
|
this.start1 = start1;
|
||||||
|
this.stop1 = stop1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String uuid;
|
||||||
|
public long start;
|
||||||
|
public long stop;
|
||||||
|
public long start1;
|
||||||
|
public long stop1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private MysqlGTIDSet buildForTest(Material material) {
|
||||||
|
return buildForTest(Arrays.asList(material));
|
||||||
|
}
|
||||||
|
|
||||||
|
private MysqlGTIDSet buildForTest(List<Material> materials) {
|
||||||
|
Map<String, UUIDSet> sets = new HashMap<String, UUIDSet>();
|
||||||
|
for (Material a : materials) {
|
||||||
|
UUIDSet.Interval interval = new UUIDSet.Interval();
|
||||||
|
interval.start = a.start;
|
||||||
|
interval.stop = a.stop;
|
||||||
|
List<UUIDSet.Interval> intervals = new ArrayList<UUIDSet.Interval>();
|
||||||
|
intervals.add(interval);
|
||||||
|
|
||||||
|
if (a.start1 > 0 && a.stop1 > 0) {
|
||||||
|
UUIDSet.Interval interval1 = new UUIDSet.Interval();
|
||||||
|
interval1.start = a.start1;
|
||||||
|
interval1.stop = a.stop1;
|
||||||
|
intervals.add(interval1);
|
||||||
|
}
|
||||||
|
|
||||||
|
UUIDSet us = new UUIDSet();
|
||||||
|
us.SID = UUID.fromString(a.uuid);
|
||||||
|
us.intervals = intervals;
|
||||||
|
|
||||||
|
sets.put(a.uuid, us);
|
||||||
|
}
|
||||||
|
|
||||||
|
MysqlGTIDSet gs = new MysqlGTIDSet();
|
||||||
|
gs.sets = sets;
|
||||||
|
|
||||||
|
return gs;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
package com.alibaba.otter.canal.parse.driver.mysql;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.UUIDSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by hiwjd on 2018/4/26. hiwjd0@gmail.com
|
||||||
|
*/
|
||||||
|
public class UUIDSetTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testToString() {
|
||||||
|
Map<String, String> cases = new HashMap<String, String>(4);
|
||||||
|
cases.put("726757ad-4455-11e8-ae04-0242ac110002:1", "726757ad-4455-11e8-ae04-0242ac110002:1");
|
||||||
|
cases.put("726757ad-4455-11e8-ae04-0242ac110002:1-3", "726757ad-4455-11e8-ae04-0242ac110002:1-3");
|
||||||
|
cases.put("726757ad-4455-11e8-ae04-0242ac110002:1-3:4-6", "726757ad-4455-11e8-ae04-0242ac110002:1-6");
|
||||||
|
cases.put("726757ad-4455-11e8-ae04-0242ac110002:1-3:5-7", "726757ad-4455-11e8-ae04-0242ac110002:1-3:5-7");
|
||||||
|
|
||||||
|
for (Map.Entry<String, String> entry : cases.entrySet()) {
|
||||||
|
String expected = entry.getValue();
|
||||||
|
assertEquals(expected, UUIDSet.parse(entry.getKey()).toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import java.text.SimpleDateFormat;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
import org.apache.commons.lang.SystemUtils;
|
import org.apache.commons.lang.SystemUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -16,6 +17,7 @@ import com.alibaba.otter.canal.protocol.CanalEntry.Column;
|
|||||||
import com.alibaba.otter.canal.protocol.CanalEntry.Entry;
|
import com.alibaba.otter.canal.protocol.CanalEntry.Entry;
|
||||||
import com.alibaba.otter.canal.protocol.CanalEntry.EntryType;
|
import com.alibaba.otter.canal.protocol.CanalEntry.EntryType;
|
||||||
import com.alibaba.otter.canal.protocol.CanalEntry.EventType;
|
import com.alibaba.otter.canal.protocol.CanalEntry.EventType;
|
||||||
|
import com.alibaba.otter.canal.protocol.CanalEntry.Pair;
|
||||||
import com.alibaba.otter.canal.protocol.CanalEntry.RowChange;
|
import com.alibaba.otter.canal.protocol.CanalEntry.RowChange;
|
||||||
import com.alibaba.otter.canal.protocol.CanalEntry.RowData;
|
import com.alibaba.otter.canal.protocol.CanalEntry.RowData;
|
||||||
import com.alibaba.otter.canal.protocol.CanalEntry.TransactionBegin;
|
import com.alibaba.otter.canal.protocol.CanalEntry.TransactionBegin;
|
||||||
@@ -56,10 +58,12 @@ public class AbstractCanalClientTest {
|
|||||||
context_format += "****************************************************" + SEP;
|
context_format += "****************************************************" + SEP;
|
||||||
|
|
||||||
row_format = SEP
|
row_format = SEP
|
||||||
+ "----------------> binlog[{}:{}] , name[{},{}] , eventType : {} , executeTime : {}({}) , delay : {} ms"
|
+ "----------------> binlog[{}:{}] , name[{},{}] , eventType : {} , executeTime : {}({}) , gtid : ({}) , delay : {} ms"
|
||||||
+ SEP;
|
+ SEP;
|
||||||
|
|
||||||
transaction_format = SEP + "================> binlog[{}:{}] , executeTime : {}({}) , delay : {}ms" + SEP;
|
transaction_format = SEP
|
||||||
|
+ "================> binlog[{}:{}] , executeTime : {}({}) , gtid : ({}) , delay : {}ms"
|
||||||
|
+ SEP;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,14 +86,15 @@ public class AbstractCanalClientTest {
|
|||||||
});
|
});
|
||||||
|
|
||||||
thread.setUncaughtExceptionHandler(handler);
|
thread.setUncaughtExceptionHandler(handler);
|
||||||
thread.start();
|
|
||||||
running = true;
|
running = true;
|
||||||
|
thread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void stop() {
|
protected void stop() {
|
||||||
if (!running) {
|
if (!running) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
connector.stopRunning();
|
||||||
running = false;
|
running = false;
|
||||||
if (thread != null) {
|
if (thread != null) {
|
||||||
try {
|
try {
|
||||||
@@ -157,8 +162,12 @@ public class AbstractCanalClientTest {
|
|||||||
long time = entry.getHeader().getExecuteTime();
|
long time = entry.getHeader().getExecuteTime();
|
||||||
Date date = new Date(time);
|
Date date = new Date(time);
|
||||||
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
|
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
|
||||||
return entry.getHeader().getLogfileName() + ":" + entry.getHeader().getLogfileOffset() + ":"
|
String position = entry.getHeader().getLogfileName() + ":" + entry.getHeader().getLogfileOffset() + ":"
|
||||||
+ entry.getHeader().getExecuteTime() + "(" + format.format(date) + ")";
|
+ entry.getHeader().getExecuteTime() + "(" + format.format(date) + ")";
|
||||||
|
if (StringUtils.isNotEmpty(entry.getHeader().getGtid())) {
|
||||||
|
position += " gtid(" + entry.getHeader().getGtid() + ")";
|
||||||
|
}
|
||||||
|
return position;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void printEntry(List<Entry> entrys) {
|
protected void printEntry(List<Entry> entrys) {
|
||||||
@@ -181,8 +190,9 @@ public class AbstractCanalClientTest {
|
|||||||
new Object[] { entry.getHeader().getLogfileName(),
|
new Object[] { entry.getHeader().getLogfileName(),
|
||||||
String.valueOf(entry.getHeader().getLogfileOffset()),
|
String.valueOf(entry.getHeader().getLogfileOffset()),
|
||||||
String.valueOf(entry.getHeader().getExecuteTime()), simpleDateFormat.format(date),
|
String.valueOf(entry.getHeader().getExecuteTime()), simpleDateFormat.format(date),
|
||||||
String.valueOf(delayTime) });
|
entry.getHeader().getGtid(), String.valueOf(delayTime) });
|
||||||
logger.info(" BEGIN ----> Thread id: {}", begin.getThreadId());
|
logger.info(" BEGIN ----> Thread id: {}", begin.getThreadId());
|
||||||
|
printXAInfo(begin.getPropsList());
|
||||||
} else if (entry.getEntryType() == EntryType.TRANSACTIONEND) {
|
} else if (entry.getEntryType() == EntryType.TRANSACTIONEND) {
|
||||||
TransactionEnd end = null;
|
TransactionEnd end = null;
|
||||||
try {
|
try {
|
||||||
@@ -193,11 +203,12 @@ public class AbstractCanalClientTest {
|
|||||||
// 打印事务提交信息,事务id
|
// 打印事务提交信息,事务id
|
||||||
logger.info("----------------\n");
|
logger.info("----------------\n");
|
||||||
logger.info(" END ----> transaction id: {}", end.getTransactionId());
|
logger.info(" END ----> transaction id: {}", end.getTransactionId());
|
||||||
|
printXAInfo(end.getPropsList());
|
||||||
logger.info(transaction_format,
|
logger.info(transaction_format,
|
||||||
new Object[] { entry.getHeader().getLogfileName(),
|
new Object[] { entry.getHeader().getLogfileName(),
|
||||||
String.valueOf(entry.getHeader().getLogfileOffset()),
|
String.valueOf(entry.getHeader().getLogfileOffset()),
|
||||||
String.valueOf(entry.getHeader().getExecuteTime()), simpleDateFormat.format(date),
|
String.valueOf(entry.getHeader().getExecuteTime()), simpleDateFormat.format(date),
|
||||||
String.valueOf(delayTime) });
|
entry.getHeader().getGtid(), String.valueOf(delayTime) });
|
||||||
}
|
}
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
@@ -218,13 +229,14 @@ public class AbstractCanalClientTest {
|
|||||||
String.valueOf(entry.getHeader().getLogfileOffset()), entry.getHeader().getSchemaName(),
|
String.valueOf(entry.getHeader().getLogfileOffset()), entry.getHeader().getSchemaName(),
|
||||||
entry.getHeader().getTableName(), eventType,
|
entry.getHeader().getTableName(), eventType,
|
||||||
String.valueOf(entry.getHeader().getExecuteTime()), simpleDateFormat.format(date),
|
String.valueOf(entry.getHeader().getExecuteTime()), simpleDateFormat.format(date),
|
||||||
String.valueOf(delayTime) });
|
entry.getHeader().getGtid(), String.valueOf(delayTime) });
|
||||||
|
|
||||||
if (eventType == EventType.QUERY || rowChage.getIsDdl()) {
|
if (eventType == EventType.QUERY || rowChage.getIsDdl()) {
|
||||||
logger.info(" sql ----> " + rowChage.getSql() + SEP);
|
logger.info(" sql ----> " + rowChage.getSql() + SEP);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printXAInfo(rowChage.getPropsList());
|
||||||
for (RowData rowData : rowChage.getRowDatasList()) {
|
for (RowData rowData : rowChage.getRowDatasList()) {
|
||||||
if (eventType == EventType.DELETE) {
|
if (eventType == EventType.DELETE) {
|
||||||
printColumn(rowData.getBeforeColumnsList());
|
printColumn(rowData.getBeforeColumnsList());
|
||||||
@@ -251,6 +263,27 @@ public class AbstractCanalClientTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void printXAInfo(List<Pair> pairs) {
|
||||||
|
if (pairs == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String xaType = null;
|
||||||
|
String xaXid = null;
|
||||||
|
for (Pair pair : pairs) {
|
||||||
|
String key = pair.getKey();
|
||||||
|
if (StringUtils.endsWithIgnoreCase(key, "XA_TYPE")) {
|
||||||
|
xaType = pair.getValue();
|
||||||
|
} else if (StringUtils.endsWithIgnoreCase(key, "XA_XID")) {
|
||||||
|
xaXid = pair.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (xaType != null && xaXid != null) {
|
||||||
|
logger.info(" ------> " + xaType + " " + xaXid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void setConnector(CanalConnector connector) {
|
public void setConnector(CanalConnector connector) {
|
||||||
this.connector = connector;
|
this.connector = connector;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
package com.alibaba.otter.canal.example;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.client.CanalConnector;
|
||||||
|
import com.alibaba.otter.canal.client.CanalConnectors;
|
||||||
|
import com.alibaba.otter.canal.client.impl.SimpleCanalConnector;
|
||||||
|
import com.alibaba.otter.canal.protocol.Message;
|
||||||
|
|
||||||
|
public class SimpleCanalClientPermanceTest {
|
||||||
|
|
||||||
|
public static void main(String args[]) {
|
||||||
|
String destination = "example";
|
||||||
|
String ip = "127.0.0.1";
|
||||||
|
int batchSize = 1024;
|
||||||
|
int count = 0;
|
||||||
|
int sum = 0;
|
||||||
|
int perSum = 0;
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
long end = 0;
|
||||||
|
final ArrayBlockingQueue<Long> queue = new ArrayBlockingQueue<Long>(100);
|
||||||
|
try {
|
||||||
|
final CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress(ip, 11111),
|
||||||
|
destination,
|
||||||
|
"",
|
||||||
|
"");
|
||||||
|
|
||||||
|
Thread ackThread = new Thread(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
long batchId = queue.take();
|
||||||
|
connector.ack(batchId);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
ackThread.start();
|
||||||
|
|
||||||
|
((SimpleCanalConnector) connector).setLazyParseEntry(true);
|
||||||
|
connector.connect();
|
||||||
|
connector.subscribe();
|
||||||
|
while (true) {
|
||||||
|
Message message = connector.getWithoutAck(batchSize);
|
||||||
|
long batchId = message.getId();
|
||||||
|
int size = message.getRawEntries().size();
|
||||||
|
sum += size;
|
||||||
|
perSum += size;
|
||||||
|
count++;
|
||||||
|
queue.add(batchId);
|
||||||
|
if (count % 10 == 0) {
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
long tps = (perSum * 1000) / (end - start);
|
||||||
|
System.out.println(" total : " + sum + " , current : " + perSum + " , cost : " + (end - start)
|
||||||
|
+ " , tps : " + tps);
|
||||||
|
start = end;
|
||||||
|
perSum = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -263,7 +263,7 @@ public class CanalInstanceWithManager extends AbstractCanalInstance {
|
|||||||
mysqlEventParser.setMasterPosition(masterPosition);
|
mysqlEventParser.setMasterPosition(masterPosition);
|
||||||
|
|
||||||
if (parameters.getPositions().size() > 1) {
|
if (parameters.getPositions().size() > 1) {
|
||||||
EntryPosition standbyPosition = JsonUtils.unmarshalFromString(parameters.getPositions().get(0),
|
EntryPosition standbyPosition = JsonUtils.unmarshalFromString(parameters.getPositions().get(1),
|
||||||
EntryPosition.class);
|
EntryPosition.class);
|
||||||
mysqlEventParser.setStandbyPosition(standbyPosition);
|
mysqlEventParser.setStandbyPosition(standbyPosition);
|
||||||
}
|
}
|
||||||
|
|||||||
111
kafka-client/pom.xml
Normal file
111
kafka-client/pom.xml
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<artifactId>canal</artifactId>
|
||||||
|
<groupId>com.alibaba.otter</groupId>
|
||||||
|
<version>1.0.26-SNAPSHOT</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<groupId>com.alibaba.otter</groupId>
|
||||||
|
<artifactId>canal.kafka.client</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<name>canal kafka client module for otter ${project.version}</name>
|
||||||
|
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.otter</groupId>
|
||||||
|
<artifactId>canal.protocol</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.kafka</groupId>
|
||||||
|
<artifactId>kafka-clients</artifactId>
|
||||||
|
<version>0.9.0.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- junit -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>dev</id>
|
||||||
|
<activation>
|
||||||
|
<activeByDefault>true</activeByDefault>
|
||||||
|
<property>
|
||||||
|
<name>env</name>
|
||||||
|
<value>!javadoc</value>
|
||||||
|
</property>
|
||||||
|
</activation>
|
||||||
|
</profile>
|
||||||
|
|
||||||
|
<profile>
|
||||||
|
<id>javadoc</id>
|
||||||
|
<activation>
|
||||||
|
<property>
|
||||||
|
<name>env</name>
|
||||||
|
<value>javadoc</value>
|
||||||
|
</property>
|
||||||
|
</activation>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
|
<version>2.9.1</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>attach-javadocs</id>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>jar</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<aggregate>true</aggregate>
|
||||||
|
<show>public</show>
|
||||||
|
<nohelp>true</nohelp>
|
||||||
|
<header>${project.artifactId}-${project.version}</header>
|
||||||
|
<footer>${project.artifactId}-${project.version}</footer>
|
||||||
|
<doctitle>${project.artifactId}-${project.version}</doctitle>
|
||||||
|
<links>
|
||||||
|
<link>https://github.com/alibaba/canal</link>
|
||||||
|
</links>
|
||||||
|
<outputDirectory>${project.build.directory}/apidocs/apidocs/${project.version}</outputDirectory>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-scm-publish-plugin</artifactId>
|
||||||
|
<version>1.0-beta-2</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>attach-javadocs</id>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>publish-scm</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<checkoutDirectory>${project.build.directory}/scmpublish</checkoutDirectory>
|
||||||
|
<checkinComment>Publishing javadoc for ${project.artifactId}:${project.version}</checkinComment>
|
||||||
|
<content>${project.build.directory}/apidocs</content>
|
||||||
|
<skipDeletedFiles>true</skipDeletedFiles>
|
||||||
|
<pubScmUrl>scm:git:git@github.com:alibaba/canal.git</pubScmUrl>
|
||||||
|
<scmBranch>gh-pages</scmBranch>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,242 @@
|
|||||||
|
package com.alibaba.otter.canal.kafka.client;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.common.utils.AddressUtils;
|
||||||
|
import com.alibaba.otter.canal.common.utils.BooleanMutex;
|
||||||
|
import com.alibaba.otter.canal.common.zookeeper.ZkClientx;
|
||||||
|
import com.alibaba.otter.canal.kafka.client.running.ClientRunningData;
|
||||||
|
import com.alibaba.otter.canal.kafka.client.running.ClientRunningListener;
|
||||||
|
import com.alibaba.otter.canal.kafka.client.running.ClientRunningMonitor;
|
||||||
|
import com.alibaba.otter.canal.protocol.Message;
|
||||||
|
import com.alibaba.otter.canal.protocol.exception.CanalClientException;
|
||||||
|
import org.apache.kafka.clients.consumer.ConsumerRecords;
|
||||||
|
import org.apache.kafka.clients.consumer.KafkaConsumer;
|
||||||
|
import org.apache.kafka.common.TopicPartition;
|
||||||
|
import org.apache.kafka.common.serialization.StringDeserializer;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* canal kafka 数据操作客户端
|
||||||
|
*
|
||||||
|
* @author machengyuan @ 2018-6-12
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
public class KafkaCanalConnector {
|
||||||
|
|
||||||
|
private KafkaConsumer<String, Message> kafkaConsumer;
|
||||||
|
private String topic;
|
||||||
|
private Integer partition;
|
||||||
|
private Properties properties;
|
||||||
|
private ClientRunningMonitor runningMonitor; // 运行控制
|
||||||
|
private ZkClientx zkClientx;
|
||||||
|
private BooleanMutex mutex = new BooleanMutex(false);
|
||||||
|
private volatile boolean connected = false;
|
||||||
|
private volatile boolean running = false;
|
||||||
|
|
||||||
|
public KafkaCanalConnector(String zkServers, String servers, String topic, Integer partition, String groupId) {
|
||||||
|
this.topic = topic;
|
||||||
|
this.partition = partition;
|
||||||
|
|
||||||
|
properties = new Properties();
|
||||||
|
properties.put("bootstrap.servers", servers);
|
||||||
|
properties.put("group.id", groupId);
|
||||||
|
properties.put("enable.auto.commit", false);
|
||||||
|
properties.put("auto.commit.interval.ms", "1000");
|
||||||
|
properties.put("auto.offset.reset", "latest"); //如果没有offset则从最后的offset开始读
|
||||||
|
properties.put("request.timeout.ms", "40000"); // 必须大于session.timeout.ms的设置
|
||||||
|
properties.put("session.timeout.ms", "30000"); // 默认为30秒
|
||||||
|
properties.put("max.poll.records", "1"); // 所以一次只取一条数据
|
||||||
|
properties.put("key.deserializer", StringDeserializer.class.getName());
|
||||||
|
properties.put("value.deserializer", MessageDeserializer.class.getName());
|
||||||
|
|
||||||
|
if (zkServers != null) {
|
||||||
|
zkClientx = new ZkClientx(zkServers);
|
||||||
|
|
||||||
|
ClientRunningData clientData = new ClientRunningData();
|
||||||
|
clientData.setGroupId(groupId);
|
||||||
|
clientData.setAddress(AddressUtils.getHostIp());
|
||||||
|
|
||||||
|
runningMonitor = new ClientRunningMonitor();
|
||||||
|
runningMonitor.setTopic(topic);
|
||||||
|
runningMonitor.setZkClient(zkClientx);
|
||||||
|
runningMonitor.setClientData(clientData);
|
||||||
|
runningMonitor.setListener(new ClientRunningListener() {
|
||||||
|
public void processActiveEnter() {
|
||||||
|
mutex.set(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void processActiveExit() {
|
||||||
|
mutex.set(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 重新设置sessionTime
|
||||||
|
*
|
||||||
|
* @param timeout
|
||||||
|
* @param unit
|
||||||
|
*/
|
||||||
|
public void setSessionTimeout(Long timeout, TimeUnit unit) {
|
||||||
|
long t = unit.toMillis(timeout);
|
||||||
|
properties.put("request.timeout.ms", String.valueOf(t + 60000));
|
||||||
|
properties.put("session.timeout.ms", String.valueOf(t));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打开连接
|
||||||
|
*/
|
||||||
|
public void connect() {
|
||||||
|
if (connected) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (runningMonitor != null) {
|
||||||
|
if (!runningMonitor.isStart()) {
|
||||||
|
runningMonitor.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
connected = true;
|
||||||
|
|
||||||
|
if (kafkaConsumer == null) {
|
||||||
|
kafkaConsumer = new KafkaConsumer<String, Message>(properties);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭链接
|
||||||
|
*/
|
||||||
|
public void disconnnect() {
|
||||||
|
kafkaConsumer.close();
|
||||||
|
|
||||||
|
connected = false;
|
||||||
|
if (runningMonitor.isStart()) {
|
||||||
|
runningMonitor.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void waitClientRunning() {
|
||||||
|
try {
|
||||||
|
if (zkClientx != null) {
|
||||||
|
if (!connected) {// 未调用connect
|
||||||
|
throw new CanalClientException("should connect first");
|
||||||
|
}
|
||||||
|
|
||||||
|
running = true;
|
||||||
|
mutex.get();// 阻塞等待
|
||||||
|
} else {
|
||||||
|
// 单机模式直接设置为running
|
||||||
|
running = true;
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
throw new CanalClientException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean checkValid() {
|
||||||
|
if (zkClientx != null) {
|
||||||
|
return mutex.state();
|
||||||
|
} else {
|
||||||
|
return true;// 默认都放过
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 订阅topic
|
||||||
|
*/
|
||||||
|
public void subscribe() {
|
||||||
|
waitClientRunning();
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (partition == null) {
|
||||||
|
kafkaConsumer.subscribe(Collections.singletonList(topic));
|
||||||
|
} else {
|
||||||
|
TopicPartition topicPartition = new TopicPartition(topic, partition);
|
||||||
|
kafkaConsumer.assign(Collections.singletonList(topicPartition));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取消订阅
|
||||||
|
*/
|
||||||
|
public void unsubscribe() {
|
||||||
|
waitClientRunning();
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
kafkaConsumer.unsubscribe();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取数据,自动进行确认
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Message get() {
|
||||||
|
return get(100L, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message get(Long timeout, TimeUnit unit) {
|
||||||
|
waitClientRunning();
|
||||||
|
if (!running) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Message message = getWithoutAck(timeout, unit);
|
||||||
|
this.ack();
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Message getWithoutAck() {
|
||||||
|
return getWithoutAck(100L, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取数据,不进行确认,等待处理完成手工确认
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public Message getWithoutAck(Long timeout, TimeUnit unit) {
|
||||||
|
waitClientRunning();
|
||||||
|
if (!running) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
ConsumerRecords<String, Message> records = kafkaConsumer.poll(unit.toMillis(timeout)); // 基于配置,最多只能poll到一条数据
|
||||||
|
|
||||||
|
if (!records.isEmpty()) {
|
||||||
|
return records.iterator().next().value();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 提交offset,如果超过 session.timeout.ms 设置的时间没有ack则会抛出异常,ack失败
|
||||||
|
*/
|
||||||
|
public void ack() {
|
||||||
|
waitClientRunning();
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
kafkaConsumer.commitSync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stopRunning() {
|
||||||
|
if (running) {
|
||||||
|
running = false; // 设置为非running状态
|
||||||
|
if (!mutex.state()) {
|
||||||
|
mutex.set(true); // 中断阻塞
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package com.alibaba.otter.canal.kafka.client;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* canal kafka connectors创建工具类
|
||||||
|
*
|
||||||
|
* @author machengyuan @ 2018-6-12
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
public class KafkaCanalConnectors {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建kafka客户端链接,独立运行不注册zk信息
|
||||||
|
*
|
||||||
|
* @param servers
|
||||||
|
* @param topic
|
||||||
|
* @param partition
|
||||||
|
* @param groupId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static KafkaCanalConnector newKafkaConnector(String servers, String topic, Integer partition, String groupId) {
|
||||||
|
return new KafkaCanalConnector(null, servers, topic, partition, groupId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建kafka客户端链接,独立运行不注册zk信息
|
||||||
|
*
|
||||||
|
* @param servers
|
||||||
|
* @param topic
|
||||||
|
* @param groupId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static KafkaCanalConnector newKafkaConnector(String servers, String topic, String groupId) {
|
||||||
|
return new KafkaCanalConnector(null, servers, topic, null, groupId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建kafka客户端链接
|
||||||
|
*
|
||||||
|
* @param zkServers
|
||||||
|
* @param servers
|
||||||
|
* @param topic
|
||||||
|
* @param partition
|
||||||
|
* @param groupId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static KafkaCanalConnector newKafkaConnector(String zkServers, String servers, String topic, Integer partition, String groupId) {
|
||||||
|
return new KafkaCanalConnector(zkServers, servers, topic, partition, groupId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建kafka客户端链接
|
||||||
|
*
|
||||||
|
* @param zkServers
|
||||||
|
* @param servers
|
||||||
|
* @param topic
|
||||||
|
* @param groupId
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static KafkaCanalConnector newKafkaConnector(String zkServers, String servers, String topic, String groupId) {
|
||||||
|
return new KafkaCanalConnector(zkServers, servers, topic, null, groupId);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package com.alibaba.otter.canal.kafka.client;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.kafka.common.serialization.Deserializer;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.protocol.CanalEntry;
|
||||||
|
import com.alibaba.otter.canal.protocol.CanalPacket;
|
||||||
|
import com.alibaba.otter.canal.protocol.Message;
|
||||||
|
import com.alibaba.otter.canal.protocol.exception.CanalClientException;
|
||||||
|
import com.google.protobuf.ByteString;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Kafka Message类的反序列化
|
||||||
|
*
|
||||||
|
* @author machengyuan @ 2018-6-12
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
public class MessageDeserializer implements Deserializer<Message> {
|
||||||
|
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(MessageDeserializer.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configure(Map<String, ?> configs, boolean isKey) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Message deserialize(String topic, byte[] data) {
|
||||||
|
try {
|
||||||
|
if (data == null) return null;
|
||||||
|
else {
|
||||||
|
CanalPacket.Packet p = CanalPacket.Packet.parseFrom(data);
|
||||||
|
switch (p.getType()) {
|
||||||
|
case MESSAGES: {
|
||||||
|
if (!p.getCompression().equals(CanalPacket.Compression.NONE)) {
|
||||||
|
throw new CanalClientException("compression is not supported in this connector");
|
||||||
|
}
|
||||||
|
|
||||||
|
CanalPacket.Messages messages = CanalPacket.Messages.parseFrom(p.getBody());
|
||||||
|
Message result = new Message(messages.getBatchId());
|
||||||
|
for (ByteString byteString : messages.getMessagesList()) {
|
||||||
|
result.addEntry(CanalEntry.Entry.parseFrom(byteString));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("Error when deserializing byte[] to message ");
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
// nothing to do
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
package com.alibaba.otter.canal.kafka.client.running;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* client running状态信息
|
||||||
|
*
|
||||||
|
* @author machengyuan 2018-06-20 下午04:10:12
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
public class ClientRunningData {
|
||||||
|
|
||||||
|
private String groupId;
|
||||||
|
private String address;
|
||||||
|
private boolean active = true;
|
||||||
|
|
||||||
|
public String getGroupId() {
|
||||||
|
return groupId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setGroupId(String groupId) {
|
||||||
|
this.groupId = groupId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddress(String address) {
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isActive() {
|
||||||
|
return active;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setActive(boolean active) {
|
||||||
|
this.active = active;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.alibaba.otter.canal.kafka.client.running;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* client running状态信息
|
||||||
|
*
|
||||||
|
* @author machengyuan 2018-06-20 下午04:10:12
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
public interface ClientRunningListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 触发现在轮到自己做为active
|
||||||
|
*/
|
||||||
|
public void processActiveEnter();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 触发一下当前active模式失败
|
||||||
|
*/
|
||||||
|
public void processActiveExit();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,275 @@
|
|||||||
|
package com.alibaba.otter.canal.kafka.client.running;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.common.AbstractCanalLifeCycle;
|
||||||
|
import com.alibaba.otter.canal.common.utils.AddressUtils;
|
||||||
|
import com.alibaba.otter.canal.common.utils.BooleanMutex;
|
||||||
|
import com.alibaba.otter.canal.common.utils.JsonUtils;
|
||||||
|
import com.alibaba.otter.canal.common.zookeeper.ZkClientx;
|
||||||
|
import com.alibaba.otter.canal.common.zookeeper.ZookeeperPathUtils;
|
||||||
|
import com.alibaba.otter.canal.protocol.exception.CanalClientException;
|
||||||
|
import org.I0Itec.zkclient.IZkDataListener;
|
||||||
|
import org.I0Itec.zkclient.exception.ZkException;
|
||||||
|
import org.I0Itec.zkclient.exception.ZkInterruptedException;
|
||||||
|
import org.I0Itec.zkclient.exception.ZkNoNodeException;
|
||||||
|
import org.I0Itec.zkclient.exception.ZkNodeExistsException;
|
||||||
|
import org.apache.zookeeper.CreateMode;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.slf4j.MDC;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ScheduledExecutorService;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* kafka client running状态信息
|
||||||
|
*
|
||||||
|
* @author machengyuan 2018-06-20 下午04:10:12
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
public class ClientRunningMonitor extends AbstractCanalLifeCycle {
|
||||||
|
|
||||||
|
private static final String TOPIC_ROOT_NODE = ZookeeperPathUtils.CANAL_ROOT_NODE + ZookeeperPathUtils.ZOOKEEPER_SEPARATOR + "topics";
|
||||||
|
|
||||||
|
private static final String TOPIC_NODE = TOPIC_ROOT_NODE + ZookeeperPathUtils.ZOOKEEPER_SEPARATOR + "{0}";
|
||||||
|
|
||||||
|
private static final String TOPIC_CLIENTID_NODE = TOPIC_NODE + ZookeeperPathUtils.ZOOKEEPER_SEPARATOR + "{1}";
|
||||||
|
|
||||||
|
private static final String TOPIC_CLIENTID_RUNNING_NODE = TOPIC_CLIENTID_NODE + ZookeeperPathUtils.ZOOKEEPER_SEPARATOR + ZookeeperPathUtils.RUNNING_NODE;
|
||||||
|
|
||||||
|
private static String getTopicClientRunning(String topic, String groupId) {
|
||||||
|
return MessageFormat.format(TOPIC_CLIENTID_RUNNING_NODE, topic, groupId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getClientIdNodePath(String topic, String groupId) {
|
||||||
|
return MessageFormat.format(TOPIC_CLIENTID_NODE, topic, groupId);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(ClientRunningMonitor.class);
|
||||||
|
private ZkClientx zkClient;
|
||||||
|
private String topic;
|
||||||
|
private ClientRunningData clientData;
|
||||||
|
private IZkDataListener dataListener;
|
||||||
|
private BooleanMutex mutex = new BooleanMutex(false);
|
||||||
|
private volatile boolean release = false;
|
||||||
|
private volatile ClientRunningData activeData;
|
||||||
|
private ScheduledExecutorService delayExector = Executors.newScheduledThreadPool(1);
|
||||||
|
private ClientRunningListener listener;
|
||||||
|
private int delayTime = 5;
|
||||||
|
|
||||||
|
private static Integer virtualPort;
|
||||||
|
|
||||||
|
public ClientRunningMonitor() {
|
||||||
|
if (virtualPort == null) {
|
||||||
|
Random rand = new Random();
|
||||||
|
virtualPort = rand.nextInt(9000) + 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
dataListener = new IZkDataListener() {
|
||||||
|
|
||||||
|
public void handleDataChange(String dataPath, Object data) throws Exception {
|
||||||
|
MDC.put("kafkaTopic", topic);
|
||||||
|
ClientRunningData runningData = JsonUtils.unmarshalFromByte((byte[]) data, ClientRunningData.class);
|
||||||
|
if (!isMine(runningData.getAddress())) {
|
||||||
|
mutex.set(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!runningData.isActive() && isMine(runningData.getAddress())) { // 说明出现了主动释放的操作,并且本机之前是active
|
||||||
|
release = true;
|
||||||
|
releaseRunning();// 彻底释放mainstem
|
||||||
|
}
|
||||||
|
|
||||||
|
activeData = (ClientRunningData) runningData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void handleDataDeleted(String dataPath) throws Exception {
|
||||||
|
MDC.put("kafkaTopic", topic);
|
||||||
|
|
||||||
|
mutex.set(false);
|
||||||
|
// 触发一下退出,可能是人为干预的释放操作或者网络闪断引起的session expired timeout
|
||||||
|
processActiveExit();
|
||||||
|
if (!release && activeData != null && isMine(activeData.getAddress())) {
|
||||||
|
// 如果上一次active的状态就是本机,则即时触发一下active抢占
|
||||||
|
initRunning();
|
||||||
|
} else {
|
||||||
|
// 否则就是等待delayTime,避免因网络瞬端或者zk异常,导致出现频繁的切换操作
|
||||||
|
delayExector.schedule(new Runnable() {
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
initRunning();
|
||||||
|
}
|
||||||
|
}, delayTime, TimeUnit.SECONDS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
super.start();
|
||||||
|
|
||||||
|
String path = getTopicClientRunning(this.topic, clientData.getGroupId());
|
||||||
|
|
||||||
|
zkClient.subscribeDataChanges(path, dataListener);
|
||||||
|
initRunning();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
super.stop();
|
||||||
|
String path = getTopicClientRunning(this.topic, clientData.getGroupId());
|
||||||
|
zkClient.unsubscribeDataChanges(path, dataListener);
|
||||||
|
releaseRunning(); // 尝试一下release
|
||||||
|
//Fix issue #697
|
||||||
|
if (delayExector != null) {
|
||||||
|
delayExector.shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 改动记录:
|
||||||
|
// 1,在方法上加synchronized关键字,保证同步顺序执行;
|
||||||
|
// 2,判断Zk上已经存在的activeData是否是本机,是的话把mutex重置为true,否则会导致死锁
|
||||||
|
// 3,增加异常处理,保证出现异常时,running节点能被删除,否则会导致死锁
|
||||||
|
public synchronized void initRunning() {
|
||||||
|
if (!isStart()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String path = getTopicClientRunning(this.topic, clientData.getGroupId());
|
||||||
|
// 序列化
|
||||||
|
byte[] bytes = JsonUtils.marshalToByte(clientData);
|
||||||
|
try {
|
||||||
|
mutex.set(false);
|
||||||
|
zkClient.create(path, bytes, CreateMode.EPHEMERAL);
|
||||||
|
processActiveEnter();// 触发一下事件
|
||||||
|
activeData = clientData;
|
||||||
|
mutex.set(true);
|
||||||
|
} catch (ZkNodeExistsException e) {
|
||||||
|
bytes = zkClient.readData(path, true);
|
||||||
|
if (bytes == null) {// 如果不存在节点,立即尝试一次
|
||||||
|
initRunning();
|
||||||
|
} else {
|
||||||
|
activeData = JsonUtils.unmarshalFromByte(bytes, ClientRunningData.class);
|
||||||
|
// 如果发现已经存在,判断一下是否自己,避免活锁
|
||||||
|
if (activeData.getAddress().contains(":") && isMine(activeData.getAddress())) {
|
||||||
|
mutex.set(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (ZkNoNodeException e) {
|
||||||
|
zkClient.createPersistent(getClientIdNodePath(this.topic, clientData.getGroupId()),
|
||||||
|
true); // 尝试创建父节点
|
||||||
|
initRunning();
|
||||||
|
} catch (Throwable t) {
|
||||||
|
logger.error(MessageFormat.format("There is an error when execute initRunning method, with destination [{0}].",
|
||||||
|
topic),
|
||||||
|
t);
|
||||||
|
// 出现任何异常尝试release
|
||||||
|
releaseRunning();
|
||||||
|
throw new CanalClientException("something goes wrong in initRunning method. ", t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 阻塞等待自己成为active,如果自己成为active,立马返回
|
||||||
|
*
|
||||||
|
* @throws InterruptedException
|
||||||
|
*/
|
||||||
|
public void waitForActive() throws InterruptedException {
|
||||||
|
initRunning();
|
||||||
|
mutex.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查当前的状态
|
||||||
|
*/
|
||||||
|
public boolean check() {
|
||||||
|
String path = getTopicClientRunning(this.topic, clientData.getGroupId());
|
||||||
|
//ZookeeperPathUtils.getDestinationClientRunning(this.destination, clientData.getClientId());
|
||||||
|
try {
|
||||||
|
byte[] bytes = zkClient.readData(path);
|
||||||
|
ClientRunningData eventData = JsonUtils.unmarshalFromByte(bytes, ClientRunningData.class);
|
||||||
|
activeData = eventData;// 更新下为最新值
|
||||||
|
// 检查下nid是否为自己
|
||||||
|
boolean result = isMine(activeData.getAddress());
|
||||||
|
if (!result) {
|
||||||
|
logger.warn("canal is running in [{}] , but not in [{}]",
|
||||||
|
activeData.getAddress(),
|
||||||
|
clientData.getAddress());
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
} catch (ZkNoNodeException e) {
|
||||||
|
logger.warn("canal is not run any in node");
|
||||||
|
return false;
|
||||||
|
} catch (ZkInterruptedException e) {
|
||||||
|
logger.warn("canal check is interrupt");
|
||||||
|
Thread.interrupted();// 清除interrupt标记
|
||||||
|
return check();
|
||||||
|
} catch (ZkException e) {
|
||||||
|
logger.warn("canal check is failed");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean releaseRunning() {
|
||||||
|
if (check()) {
|
||||||
|
String path = getTopicClientRunning(this.topic, clientData.getGroupId());
|
||||||
|
zkClient.delete(path);
|
||||||
|
mutex.set(false);
|
||||||
|
processActiveExit();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ====================== helper method ======================
|
||||||
|
|
||||||
|
private boolean isMine(String address) {
|
||||||
|
return address.equals(clientData.getAddress());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processActiveEnter() {
|
||||||
|
if (listener != null) {
|
||||||
|
// 触发回调
|
||||||
|
listener.processActiveEnter();
|
||||||
|
this.clientData.setAddress(/*address*/AddressUtils.getHostIp() + ":" + virtualPort);
|
||||||
|
|
||||||
|
String path = getTopicClientRunning(this.topic, clientData.getGroupId());
|
||||||
|
// 序列化
|
||||||
|
byte[] bytes = JsonUtils.marshalToByte(clientData);
|
||||||
|
zkClient.writeData(path, bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void processActiveExit() {
|
||||||
|
if (listener != null) {
|
||||||
|
listener.processActiveExit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setListener(ClientRunningListener listener) {
|
||||||
|
this.listener = listener;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================== setter / getter =======================
|
||||||
|
|
||||||
|
public void setTopic(String topic) {
|
||||||
|
this.topic = topic;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setClientData(ClientRunningData clientData) {
|
||||||
|
this.clientData = clientData;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDelayTime(int delayTime) {
|
||||||
|
this.delayTime = delayTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setZkClient(ZkClientx zkClient) {
|
||||||
|
this.zkClient = zkClient;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package com.alibaba.otter.canal.kafka.client.running;
|
||||||
|
|
||||||
|
import org.junit.Assert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Kafka 测试基类
|
||||||
|
*
|
||||||
|
* @author machengyuan @ 2018-6-12
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
public abstract class AbstractKafkaTest {
|
||||||
|
|
||||||
|
public static String topic = "example";
|
||||||
|
public static Integer partition = null;
|
||||||
|
public static String groupId = "g4";
|
||||||
|
public static String servers = "slave1:6667,slave2:6667,slave3:6667";
|
||||||
|
public static String zkServers = "slave1:2181,slave2:2181,slave3:2181";
|
||||||
|
|
||||||
|
public void sleep(long time) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(time);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Assert.fail(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,144 @@
|
|||||||
|
package com.alibaba.otter.canal.kafka.client.running;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.apache.kafka.common.errors.WakeupException;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.kafka.client.KafkaCanalConnector;
|
||||||
|
import com.alibaba.otter.canal.kafka.client.KafkaCanalConnectors;
|
||||||
|
import com.alibaba.otter.canal.protocol.Message;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Kafka client example
|
||||||
|
*
|
||||||
|
* @author machengyuan @ 2018-6-12
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
public class CanalKafkaClientExample {
|
||||||
|
|
||||||
|
protected final static Logger logger = LoggerFactory.getLogger(CanalKafkaClientExample.class);
|
||||||
|
|
||||||
|
private KafkaCanalConnector connector;
|
||||||
|
|
||||||
|
private static volatile boolean running = false;
|
||||||
|
|
||||||
|
private Thread thread = null;
|
||||||
|
|
||||||
|
private Thread.UncaughtExceptionHandler handler = new Thread.UncaughtExceptionHandler() {
|
||||||
|
|
||||||
|
public void uncaughtException(Thread t, Throwable e) {
|
||||||
|
logger.error("parse events has an error", e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public CanalKafkaClientExample(String zkServers, String servers, String topic, Integer partition, String groupId) {
|
||||||
|
connector = KafkaCanalConnectors.newKafkaConnector(zkServers, servers, topic, partition, groupId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
final CanalKafkaClientExample kafkaCanalClientExample = new CanalKafkaClientExample(AbstractKafkaTest.zkServers,
|
||||||
|
AbstractKafkaTest.servers,
|
||||||
|
AbstractKafkaTest.topic,
|
||||||
|
AbstractKafkaTest.partition,
|
||||||
|
AbstractKafkaTest.groupId);
|
||||||
|
logger.info("## start the kafka consumer: {}-{}", AbstractKafkaTest.topic, AbstractKafkaTest.groupId);
|
||||||
|
kafkaCanalClientExample.start();
|
||||||
|
logger.info("## the canal kafka consumer is running now ......");
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
logger.info("## stop the kafka consumer");
|
||||||
|
kafkaCanalClientExample.stop();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
logger.warn("##something goes wrong when stopping kafka consumer:", e);
|
||||||
|
} finally {
|
||||||
|
logger.info("## kafka consumer is down.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
while (running)
|
||||||
|
;
|
||||||
|
} catch (Throwable e) {
|
||||||
|
logger.error("## Something goes wrong when starting up the kafka consumer:", e);
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
Assert.notNull(connector, "connector is null");
|
||||||
|
thread = new Thread(new Runnable() {
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
process();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
thread.setUncaughtExceptionHandler(handler);
|
||||||
|
thread.start();
|
||||||
|
running = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
if (!running) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
connector.stopRunning();
|
||||||
|
running = false;
|
||||||
|
if (thread != null) {
|
||||||
|
try {
|
||||||
|
thread.join();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void process() {
|
||||||
|
while (!running)
|
||||||
|
;
|
||||||
|
while (running) {
|
||||||
|
try {
|
||||||
|
connector.connect();
|
||||||
|
connector.subscribe();
|
||||||
|
while (running) {
|
||||||
|
try {
|
||||||
|
Message message = connector.getWithoutAck(1L, TimeUnit.SECONDS); // 获取message
|
||||||
|
if (message == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
long batchId = message.getId();
|
||||||
|
int size = message.getEntries().size();
|
||||||
|
if (batchId == -1 || size == 0) {
|
||||||
|
// try {
|
||||||
|
// Thread.sleep(1000);
|
||||||
|
// } catch (InterruptedException e) {
|
||||||
|
// }
|
||||||
|
} else {
|
||||||
|
// printSummary(message, batchId, size);
|
||||||
|
// printEntry(message.getEntries());
|
||||||
|
logger.info(message.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
connector.ack(); // 提交确认
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
connector.unsubscribe();
|
||||||
|
} catch (WakeupException e) {
|
||||||
|
// No-op. Continue process
|
||||||
|
}
|
||||||
|
connector.disconnnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
package com.alibaba.otter.canal.kafka.client.running;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import org.apache.kafka.common.errors.WakeupException;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.kafka.client.KafkaCanalConnector;
|
||||||
|
import com.alibaba.otter.canal.kafka.client.KafkaCanalConnectors;
|
||||||
|
import com.alibaba.otter.canal.protocol.Message;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Kafka consumer获取Message的测试例子
|
||||||
|
*
|
||||||
|
* @author machengyuan @ 2018-6-12
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
public class KafkaClientRunningTest extends AbstractKafkaTest {
|
||||||
|
|
||||||
|
private Logger logger = LoggerFactory.getLogger(KafkaClientRunningTest.class);
|
||||||
|
|
||||||
|
private boolean running = true;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testKafkaConsumer() {
|
||||||
|
final ExecutorService executor = Executors.newFixedThreadPool(1);
|
||||||
|
|
||||||
|
final KafkaCanalConnector connector = KafkaCanalConnectors.newKafkaConnector(servers, topic, partition, groupId);
|
||||||
|
|
||||||
|
executor.submit(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
connector.connect();
|
||||||
|
connector.subscribe();
|
||||||
|
while (running) {
|
||||||
|
try {
|
||||||
|
Message message = connector.getWithoutAck(3L, TimeUnit.SECONDS);
|
||||||
|
if (message != null) {
|
||||||
|
System.out.println(message);
|
||||||
|
}
|
||||||
|
connector.ack();
|
||||||
|
} catch (WakeupException e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
connector.unsubscribe();
|
||||||
|
connector.disconnnect();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
sleep(60000);
|
||||||
|
running = false;
|
||||||
|
executor.shutdown();
|
||||||
|
logger.info("shutdown completed");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
19
kafka-client/src/test/resources/logback.xml
Normal file
19
kafka-client/src/test/resources/logback.xml
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<configuration scan="true" scanPeriod=" 5 seconds">
|
||||||
|
|
||||||
|
<jmxConfigurator />
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{56} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<logger name="org.apache.kafka" additivity="false">
|
||||||
|
<level value="ERROR" />
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</logger>
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT"/>
|
||||||
|
</root>
|
||||||
|
</configuration>
|
||||||
140
kafka/pom.xml
Normal file
140
kafka/pom.xml
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<artifactId>canal</artifactId>
|
||||||
|
<groupId>com.alibaba.otter</groupId>
|
||||||
|
<version>1.0.26-SNAPSHOT</version>
|
||||||
|
<relativePath>../pom.xml</relativePath>
|
||||||
|
</parent>
|
||||||
|
<groupId>com.alibaba.otter</groupId>
|
||||||
|
<artifactId>canal.kafka</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<name>canal kafka module for otter ${project.version}</name>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba.otter</groupId>
|
||||||
|
<artifactId>canal.deployer</artifactId>
|
||||||
|
<version>${project.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.yaml</groupId>
|
||||||
|
<artifactId>snakeyaml</artifactId>
|
||||||
|
<version>1.17</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.kafka</groupId>
|
||||||
|
<artifactId>kafka_2.11</artifactId>
|
||||||
|
<version>0.9.0.1</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-log4j12</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<!-- deploy模块的packaging通常是jar,如果项目中没有java 源代码或资源文件,加上这一段配置使项目能通过构建 -->
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<addMavenDescriptor>true</addMavenDescriptor>
|
||||||
|
</archive>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/logback.xml</exclude>
|
||||||
|
<exclude>**/canal.properties</exclude>
|
||||||
|
<exclude>**/spring/**</exclude>
|
||||||
|
<exclude>**/example/**</exclude>
|
||||||
|
<exclude>**/kafka.yml</exclude>
|
||||||
|
</excludes>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
|
<!-- 这是最新版本,推荐使用这个版本 -->
|
||||||
|
<version>2.2.1</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>assemble</id>
|
||||||
|
<goals>
|
||||||
|
<goal>single</goal>
|
||||||
|
</goals>
|
||||||
|
<phase>package</phase>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<appendAssemblyId>false</appendAssemblyId>
|
||||||
|
<attach>false</attach>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>dev</id>
|
||||||
|
<activation>
|
||||||
|
<activeByDefault>true</activeByDefault>
|
||||||
|
<property>
|
||||||
|
<name>env</name>
|
||||||
|
<value>!release</value>
|
||||||
|
</property>
|
||||||
|
</activation>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<!-- maven assembly插件需要一个描述文件 来告诉插件包的结构以及打包所需的文件来自哪里 -->
|
||||||
|
<descriptors>
|
||||||
|
<descriptor>${basedir}/src/main/assembly/dev.xml</descriptor>
|
||||||
|
</descriptors>
|
||||||
|
<finalName>canal</finalName>
|
||||||
|
<outputDirectory>${project.build.directory}</outputDirectory>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</profile>
|
||||||
|
|
||||||
|
<profile>
|
||||||
|
<id>release</id>
|
||||||
|
<activation>
|
||||||
|
<property>
|
||||||
|
<name>env</name>
|
||||||
|
<value>release</value>
|
||||||
|
</property>
|
||||||
|
</activation>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<!-- 发布模式使用的maven assembly插件描述文件 -->
|
||||||
|
<descriptors>
|
||||||
|
<descriptor>${basedir}/src/main/assembly/release.xml</descriptor>
|
||||||
|
</descriptors>
|
||||||
|
<!-- 如果一个应用的包含多个deploy模块,如果使用同样的包名, 如果把它们复制的一个目录中可能会失败,所以包名加了 artifactId以示区分 -->
|
||||||
|
<finalName>${project.artifactId}-${project.version}</finalName>
|
||||||
|
<!-- scm 要求 release 模式打出的包放到顶级目录下的target子目录中 -->
|
||||||
|
<outputDirectory>${project.parent.build.directory}</outputDirectory>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
</project>
|
||||||
64
kafka/src/main/assembly/dev.xml
Normal file
64
kafka/src/main/assembly/dev.xml
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
|
||||||
|
<id>dist</id>
|
||||||
|
<formats>
|
||||||
|
<format>dir</format>
|
||||||
|
</formats>
|
||||||
|
<includeBaseDirectory>false</includeBaseDirectory>
|
||||||
|
<fileSets>
|
||||||
|
<fileSet>
|
||||||
|
<directory>.</directory>
|
||||||
|
<outputDirectory>/</outputDirectory>
|
||||||
|
<includes>
|
||||||
|
<include>README*</include>
|
||||||
|
</includes>
|
||||||
|
</fileSet>
|
||||||
|
<fileSet>
|
||||||
|
<directory>./src/main/bin</directory>
|
||||||
|
<outputDirectory>bin</outputDirectory>
|
||||||
|
<includes>
|
||||||
|
<include>**/*</include>
|
||||||
|
</includes>
|
||||||
|
<fileMode>0755</fileMode>
|
||||||
|
</fileSet>
|
||||||
|
<fileSet>
|
||||||
|
<directory>../deployer/src/main/conf</directory>
|
||||||
|
<outputDirectory>/conf</outputDirectory>
|
||||||
|
<includes>
|
||||||
|
<include>**/*</include>
|
||||||
|
</includes>
|
||||||
|
</fileSet>
|
||||||
|
<fileSet>
|
||||||
|
<directory>../deployer/src/main/resources</directory>
|
||||||
|
<outputDirectory>/conf</outputDirectory>
|
||||||
|
<includes>
|
||||||
|
<include>**/*</include>
|
||||||
|
</includes>
|
||||||
|
<excludes>
|
||||||
|
<exclude>logback.xml</exclude>
|
||||||
|
</excludes>
|
||||||
|
</fileSet>
|
||||||
|
<fileSet>
|
||||||
|
<directory>./src/main/resources</directory>
|
||||||
|
<outputDirectory>/conf</outputDirectory>
|
||||||
|
<includes>
|
||||||
|
<include>**/*</include>
|
||||||
|
</includes>
|
||||||
|
</fileSet>
|
||||||
|
<fileSet>
|
||||||
|
<directory>target</directory>
|
||||||
|
<outputDirectory>logs</outputDirectory>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*</exclude>
|
||||||
|
</excludes>
|
||||||
|
</fileSet>
|
||||||
|
</fileSets>
|
||||||
|
<dependencySets>
|
||||||
|
<dependencySet>
|
||||||
|
<outputDirectory>lib</outputDirectory>
|
||||||
|
<excludes>
|
||||||
|
<exclude>junit:junit</exclude>
|
||||||
|
</excludes>
|
||||||
|
</dependencySet>
|
||||||
|
</dependencySets>
|
||||||
|
</assembly>
|
||||||
64
kafka/src/main/assembly/release.xml
Normal file
64
kafka/src/main/assembly/release.xml
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
|
||||||
|
<id>dist</id>
|
||||||
|
<formats>
|
||||||
|
<format>tar.gz</format>
|
||||||
|
</formats>
|
||||||
|
<includeBaseDirectory>false</includeBaseDirectory>
|
||||||
|
<fileSets>
|
||||||
|
<fileSet>
|
||||||
|
<directory>.</directory>
|
||||||
|
<outputDirectory>/</outputDirectory>
|
||||||
|
<includes>
|
||||||
|
<include>README*</include>
|
||||||
|
</includes>
|
||||||
|
</fileSet>
|
||||||
|
<fileSet>
|
||||||
|
<directory>./src/main/bin</directory>
|
||||||
|
<outputDirectory>bin</outputDirectory>
|
||||||
|
<includes>
|
||||||
|
<include>**/*</include>
|
||||||
|
</includes>
|
||||||
|
<fileMode>0755</fileMode>
|
||||||
|
</fileSet>
|
||||||
|
<fileSet>
|
||||||
|
<directory>../deployer/src/main/conf</directory>
|
||||||
|
<outputDirectory>/conf</outputDirectory>
|
||||||
|
<includes>
|
||||||
|
<include>**/*</include>
|
||||||
|
</includes>
|
||||||
|
</fileSet>
|
||||||
|
<fileSet>
|
||||||
|
<directory>../deployer/src/main/resources</directory>
|
||||||
|
<outputDirectory>/conf</outputDirectory>
|
||||||
|
<includes>
|
||||||
|
<include>**/*</include>
|
||||||
|
</includes>
|
||||||
|
<excludes>
|
||||||
|
<exclude>logback.xml</exclude>
|
||||||
|
</excludes>
|
||||||
|
</fileSet>
|
||||||
|
<fileSet>
|
||||||
|
<directory>./src/main/resources</directory>
|
||||||
|
<outputDirectory>/conf</outputDirectory>
|
||||||
|
<includes>
|
||||||
|
<include>**/*</include>
|
||||||
|
</includes>
|
||||||
|
</fileSet>
|
||||||
|
<fileSet>
|
||||||
|
<directory>target</directory>
|
||||||
|
<outputDirectory>logs</outputDirectory>
|
||||||
|
<excludes>
|
||||||
|
<exclude>**/*</exclude>
|
||||||
|
</excludes>
|
||||||
|
</fileSet>
|
||||||
|
</fileSets>
|
||||||
|
<dependencySets>
|
||||||
|
<dependencySet>
|
||||||
|
<outputDirectory>lib</outputDirectory>
|
||||||
|
<excludes>
|
||||||
|
<exclude>junit:junit</exclude>
|
||||||
|
</excludes>
|
||||||
|
</dependencySet>
|
||||||
|
</dependencySets>
|
||||||
|
</assembly>
|
||||||
25
kafka/src/main/bin/startup.bat
Executable file
25
kafka/src/main/bin/startup.bat
Executable file
@@ -0,0 +1,25 @@
|
|||||||
|
@echo off
|
||||||
|
@if not "%ECHO%" == "" echo %ECHO%
|
||||||
|
@if "%OS%" == "Windows_NT" setlocal
|
||||||
|
|
||||||
|
set ENV_PATH=.\
|
||||||
|
if "%OS%" == "Windows_NT" set ENV_PATH=%~dp0%
|
||||||
|
|
||||||
|
set conf_dir=%ENV_PATH%\..\conf
|
||||||
|
set canal_conf=%conf_dir%\canal.properties
|
||||||
|
set logback_configurationFile=%conf_dir%\logback.xml
|
||||||
|
|
||||||
|
set CLASSPATH=%conf_dir%
|
||||||
|
set CLASSPATH=%conf_dir%\..\lib\*;%CLASSPATH%
|
||||||
|
|
||||||
|
set JAVA_MEM_OPTS= -Xms128m -Xmx512m -XX:PermSize=128m
|
||||||
|
set JAVA_OPTS_EXT= -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dapplication.codeset=UTF-8 -Dfile.encoding=UTF-8
|
||||||
|
set JAVA_DEBUG_OPT= -server -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=9099,server=y,suspend=n
|
||||||
|
set CANAL_OPTS= -DappName=otter-canal -Dlogback.configurationFile="%logback_configurationFile%" -Dcanal.conf="%canal_conf%"
|
||||||
|
|
||||||
|
set JAVA_OPTS= %JAVA_MEM_OPTS% %JAVA_OPTS_EXT% %JAVA_DEBUG_OPT% %CANAL_OPTS%
|
||||||
|
|
||||||
|
set CMD_STR= java %JAVA_OPTS% -classpath "%CLASSPATH%" java %JAVA_OPTS% -classpath "%CLASSPATH%" com.alibaba.otter.canal.kafka.CanalLauncher
|
||||||
|
echo start cmd : %CMD_STR%
|
||||||
|
|
||||||
|
java %JAVA_OPTS% -classpath "%CLASSPATH%" com.alibaba.otter.canal.kafka.CanalLauncher
|
||||||
104
kafka/src/main/bin/startup.sh
Normal file
104
kafka/src/main/bin/startup.sh
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
current_path=`pwd`
|
||||||
|
case "`uname`" in
|
||||||
|
Linux)
|
||||||
|
bin_abs_path=$(readlink -f $(dirname $0))
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
bin_abs_path=`cd $(dirname $0); pwd`
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
base=${bin_abs_path}/..
|
||||||
|
canal_conf=$base/conf/canal.properties
|
||||||
|
logback_configurationFile=$base/conf/logback.xml
|
||||||
|
export LANG=en_US.UTF-8
|
||||||
|
export BASE=$base
|
||||||
|
|
||||||
|
if [ -f $base/bin/canal.pid ] ; then
|
||||||
|
echo "found canal.pid , Please run stop.sh first ,then startup.sh" 2>&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -d $base/logs/canal ] ; then
|
||||||
|
mkdir -p $base/logs/canal
|
||||||
|
fi
|
||||||
|
|
||||||
|
## set java path
|
||||||
|
if [ -z "$JAVA" ] ; then
|
||||||
|
JAVA=$(which java)
|
||||||
|
fi
|
||||||
|
|
||||||
|
ALIBABA_JAVA="/usr/alibaba/java/bin/java"
|
||||||
|
TAOBAO_JAVA="/opt/taobao/java/bin/java"
|
||||||
|
if [ -z "$JAVA" ]; then
|
||||||
|
if [ -f $ALIBABA_JAVA ] ; then
|
||||||
|
JAVA=$ALIBABA_JAVA
|
||||||
|
elif [ -f $TAOBAO_JAVA ] ; then
|
||||||
|
JAVA=$TAOBAO_JAVA
|
||||||
|
else
|
||||||
|
echo "Cannot find a Java JDK. Please set either set JAVA or put java (>=1.5) in your PATH." 2>&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
case "$#"
|
||||||
|
in
|
||||||
|
0 )
|
||||||
|
;;
|
||||||
|
1 )
|
||||||
|
var=$*
|
||||||
|
if [ -f $var ] ; then
|
||||||
|
canal_conf=$var
|
||||||
|
else
|
||||||
|
echo "THE PARAMETER IS NOT CORRECT.PLEASE CHECK AGAIN."
|
||||||
|
exit
|
||||||
|
fi;;
|
||||||
|
2 )
|
||||||
|
var=$1
|
||||||
|
if [ -f $var ] ; then
|
||||||
|
canal_conf=$var
|
||||||
|
else
|
||||||
|
if [ "$1" = "debug" ]; then
|
||||||
|
DEBUG_PORT=$2
|
||||||
|
DEBUG_SUSPEND="n"
|
||||||
|
JAVA_DEBUG_OPT="-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=$DEBUG_PORT,server=y,suspend=$DEBUG_SUSPEND"
|
||||||
|
fi
|
||||||
|
fi;;
|
||||||
|
* )
|
||||||
|
echo "THE PARAMETERS MUST BE TWO OR LESS.PLEASE CHECK AGAIN."
|
||||||
|
exit;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
str=`file -L $JAVA | grep 64-bit`
|
||||||
|
if [ -n "$str" ]; then
|
||||||
|
JAVA_OPTS="-server -Xms2048m -Xmx3072m -Xmn1024m -XX:SurvivorRatio=2 -XX:PermSize=96m -XX:MaxPermSize=256m -Xss256k -XX:-UseAdaptiveSizePolicy -XX:MaxTenuringThreshold=15 -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError"
|
||||||
|
else
|
||||||
|
JAVA_OPTS="-server -Xms1024m -Xmx1024m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:MaxPermSize=128m "
|
||||||
|
fi
|
||||||
|
|
||||||
|
JAVA_OPTS=" $JAVA_OPTS -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF-8"
|
||||||
|
CANAL_OPTS="-DappName=otter-canal -Dlogback.configurationFile=$logback_configurationFile -Dcanal.conf=$canal_conf"
|
||||||
|
|
||||||
|
if [ -e $canal_conf -a -e $logback_configurationFile ]
|
||||||
|
then
|
||||||
|
|
||||||
|
for i in $base/lib/*;
|
||||||
|
do CLASSPATH=$i:"$CLASSPATH";
|
||||||
|
done
|
||||||
|
CLASSPATH="$base/conf:$CLASSPATH";
|
||||||
|
|
||||||
|
echo "cd to $bin_abs_path for workaround relative path"
|
||||||
|
cd $bin_abs_path
|
||||||
|
|
||||||
|
echo LOG CONFIGURATION : $logback_configurationFile
|
||||||
|
echo canal conf : $canal_conf
|
||||||
|
echo CLASSPATH :$CLASSPATH
|
||||||
|
$JAVA $JAVA_OPTS $JAVA_DEBUG_OPT $CANAL_OPTS -classpath .:$CLASSPATH com.alibaba.otter.canal.kafka.CanalLauncher 1>>$base/logs/canal/canal.log 2>&1 &
|
||||||
|
echo $! > $base/bin/canal.pid
|
||||||
|
|
||||||
|
echo "cd to $current_path for continue"
|
||||||
|
cd $current_path
|
||||||
|
else
|
||||||
|
echo "canal conf("$canal_conf") OR log configration file($logback_configurationFile) is not exist,please create then first!"
|
||||||
|
fi
|
||||||
65
kafka/src/main/bin/stop.sh
Normal file
65
kafka/src/main/bin/stop.sh
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
cygwin=false;
|
||||||
|
linux=false;
|
||||||
|
case "`uname`" in
|
||||||
|
CYGWIN*)
|
||||||
|
cygwin=true
|
||||||
|
;;
|
||||||
|
Linux*)
|
||||||
|
linux=true
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
get_pid() {
|
||||||
|
STR=$1
|
||||||
|
PID=$2
|
||||||
|
if $cygwin; then
|
||||||
|
JAVA_CMD="$JAVA_HOME\bin\java"
|
||||||
|
JAVA_CMD=`cygpath --path --unix $JAVA_CMD`
|
||||||
|
JAVA_PID=`ps |grep $JAVA_CMD |awk '{print $1}'`
|
||||||
|
else
|
||||||
|
if $linux; then
|
||||||
|
if [ ! -z "$PID" ]; then
|
||||||
|
JAVA_PID=`ps -C java -f --width 1000|grep "$STR"|grep "$PID"|grep -v grep|awk '{print $2}'`
|
||||||
|
else
|
||||||
|
JAVA_PID=`ps -C java -f --width 1000|grep "$STR"|grep -v grep|awk '{print $2}'`
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
if [ ! -z "$PID" ]; then
|
||||||
|
JAVA_PID=`ps aux |grep "$STR"|grep "$PID"|grep -v grep|awk '{print $2}'`
|
||||||
|
else
|
||||||
|
JAVA_PID=`ps aux |grep "$STR"|grep -v grep|awk '{print $2}'`
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
echo $JAVA_PID;
|
||||||
|
}
|
||||||
|
|
||||||
|
base=`dirname $0`/..
|
||||||
|
pidfile=$base/bin/canal.pid
|
||||||
|
if [ ! -f "$pidfile" ];then
|
||||||
|
echo "canal is not running. exists"
|
||||||
|
exit
|
||||||
|
fi
|
||||||
|
|
||||||
|
pid=`cat $pidfile`
|
||||||
|
if [ "$pid" == "" ] ; then
|
||||||
|
pid=`get_pid "appName=otter-canal"`
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "`hostname`: stopping canal $pid ... "
|
||||||
|
kill $pid
|
||||||
|
|
||||||
|
LOOPS=0
|
||||||
|
while (true);
|
||||||
|
do
|
||||||
|
gpid=`get_pid "appName=otter-canal" "$pid"`
|
||||||
|
if [ "$gpid" == "" ] ; then
|
||||||
|
echo "Oook! cost:$LOOPS"
|
||||||
|
`rm $pidfile`
|
||||||
|
break;
|
||||||
|
fi
|
||||||
|
let LOOPS=LOOPS+1
|
||||||
|
sleep 1
|
||||||
|
done
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.alibaba.otter.canal.kafka;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.kafka.producer.CanalKafkaStarter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* canal-kafka独立版本启动的入口类
|
||||||
|
*
|
||||||
|
* @author machengyuan 2018-6-11 下午05:30:49
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
public class CanalLauncher {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
CanalServerStarter.init();
|
||||||
|
CanalKafkaStarter.init();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package com.alibaba.otter.canal.kafka;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.deployer.CanalController;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* canal server 启动类
|
||||||
|
*
|
||||||
|
* @author machengyuan 2018-6-11 下午05:30:49
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
public class CanalServerStarter {
|
||||||
|
|
||||||
|
private static final String CLASSPATH_URL_PREFIX = "classpath:";
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(CanalServerStarter.class);
|
||||||
|
private volatile static boolean running = false;
|
||||||
|
|
||||||
|
public static void init() {
|
||||||
|
try {
|
||||||
|
logger.info("## set default uncaught exception handler");
|
||||||
|
setGlobalUncaughtExceptionHandler();
|
||||||
|
|
||||||
|
logger.info("## load canal configurations");
|
||||||
|
String conf = System.getProperty("canal.conf", "classpath:canal.properties");
|
||||||
|
Properties properties = new Properties();
|
||||||
|
if (conf.startsWith(CLASSPATH_URL_PREFIX)) {
|
||||||
|
conf = StringUtils.substringAfter(conf, CLASSPATH_URL_PREFIX);
|
||||||
|
properties.load(CanalLauncher.class.getClassLoader().getResourceAsStream(conf));
|
||||||
|
} else {
|
||||||
|
properties.load(new FileInputStream(conf));
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("## start the canal server.");
|
||||||
|
final CanalController controller = new CanalController(properties);
|
||||||
|
controller.start();
|
||||||
|
running = true;
|
||||||
|
logger.info("## the canal server is running now ......");
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
logger.info("## stop the canal server");
|
||||||
|
running = false;
|
||||||
|
controller.stop();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
logger.warn("##something goes wrong when stopping canal Server:", e);
|
||||||
|
} finally {
|
||||||
|
logger.info("## canal server is down.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
} catch (Throwable e) {
|
||||||
|
logger.error("## Something goes wrong when starting up the canal Server:", e);
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isRunning() {
|
||||||
|
return running;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void setGlobalUncaughtExceptionHandler() {
|
||||||
|
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void uncaughtException(Thread t, Throwable e) {
|
||||||
|
logger.error("UnCaughtException", e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package com.alibaba.otter.canal.kafka.producer;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.apache.kafka.clients.producer.KafkaProducer;
|
||||||
|
import org.apache.kafka.clients.producer.Producer;
|
||||||
|
import org.apache.kafka.clients.producer.ProducerRecord;
|
||||||
|
import org.apache.kafka.common.serialization.StringSerializer;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.kafka.producer.KafkaProperties.Topic;
|
||||||
|
import com.alibaba.otter.canal.protocol.CanalEntry;
|
||||||
|
import com.alibaba.otter.canal.protocol.Message;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* kafka producer 主操作类
|
||||||
|
*
|
||||||
|
* @author machengyuan 2018-6-11 下午05:30:49
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
public class CanalKafkaProducer {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(CanalKafkaProducer.class);
|
||||||
|
|
||||||
|
private Producer<String, Message> producer;
|
||||||
|
|
||||||
|
public void init(KafkaProperties kafkaProperties) {
|
||||||
|
Properties properties = new Properties();
|
||||||
|
properties.put("bootstrap.servers", kafkaProperties.getServers());
|
||||||
|
properties.put("acks", "all");
|
||||||
|
properties.put("retries", kafkaProperties.getRetries());
|
||||||
|
properties.put("batch.size", kafkaProperties.getBatchSize());
|
||||||
|
properties.put("linger.ms", kafkaProperties.getLingerMs());
|
||||||
|
properties.put("buffer.memory", kafkaProperties.getBufferMemory());
|
||||||
|
properties.put("key.serializer", StringSerializer.class.getName());
|
||||||
|
properties.put("value.serializer", MessageSerializer.class.getName());
|
||||||
|
producer = new KafkaProducer<String, Message>(properties);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
try {
|
||||||
|
logger.info("## stop the kafka producer");
|
||||||
|
producer.close();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
logger.warn("##something goes wrong when stopping kafka producer:", e);
|
||||||
|
} finally {
|
||||||
|
logger.info("## kafka producer is down.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void send(Topic topic, Message message) {
|
||||||
|
boolean valid = false;
|
||||||
|
if (message != null && !message.getEntries().isEmpty()) {
|
||||||
|
for (CanalEntry.Entry entry : message.getEntries()) {
|
||||||
|
if (entry.getEntryType() != CanalEntry.EntryType.TRANSACTIONBEGIN
|
||||||
|
&& entry.getEntryType() != CanalEntry.EntryType.TRANSACTIONEND) {
|
||||||
|
valid = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!valid) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ProducerRecord<String, Message> record;
|
||||||
|
if (topic.getPartition() != null) {
|
||||||
|
record = new ProducerRecord<String, Message>(topic.getTopic(), topic.getPartition(), null, message);
|
||||||
|
} else {
|
||||||
|
record = new ProducerRecord<String, Message>(topic.getTopic(), message);
|
||||||
|
}
|
||||||
|
producer.send(record);
|
||||||
|
logger.debug("send message to kafka topic: {} \n {}", topic.getTopic(), message.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,147 @@
|
|||||||
|
package com.alibaba.otter.canal.kafka.producer;
|
||||||
|
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.yaml.snakeyaml.Yaml;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.kafka.CanalServerStarter;
|
||||||
|
import com.alibaba.otter.canal.kafka.producer.KafkaProperties.CanalDestination;
|
||||||
|
import com.alibaba.otter.canal.kafka.producer.KafkaProperties.Topic;
|
||||||
|
import com.alibaba.otter.canal.protocol.ClientIdentity;
|
||||||
|
import com.alibaba.otter.canal.protocol.Message;
|
||||||
|
import com.alibaba.otter.canal.server.embedded.CanalServerWithEmbedded;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* kafka 启动类
|
||||||
|
*
|
||||||
|
* @author machengyuan 2018-6-11 下午05:30:49
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
public class CanalKafkaStarter {
|
||||||
|
|
||||||
|
private static final String CLASSPATH_URL_PREFIX = "classpath:";
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(CanalKafkaStarter.class);
|
||||||
|
|
||||||
|
private volatile static boolean running = false;
|
||||||
|
|
||||||
|
private static ExecutorService executorService;
|
||||||
|
|
||||||
|
private static CanalKafkaProducer canalKafkaProducer;
|
||||||
|
|
||||||
|
private static KafkaProperties kafkaProperties;
|
||||||
|
|
||||||
|
public static void init() {
|
||||||
|
try {
|
||||||
|
logger.info("## load kafka configurations");
|
||||||
|
String conf = System.getProperty("kafka.conf", "classpath:kafka.yml");
|
||||||
|
|
||||||
|
if (conf.startsWith(CLASSPATH_URL_PREFIX)) {
|
||||||
|
conf = StringUtils.substringAfter(conf, CLASSPATH_URL_PREFIX);
|
||||||
|
kafkaProperties = new Yaml().loadAs(CanalKafkaStarter.class.getClassLoader().getResourceAsStream(conf),
|
||||||
|
KafkaProperties.class);
|
||||||
|
} else {
|
||||||
|
kafkaProperties = new Yaml().loadAs(new FileInputStream(conf), KafkaProperties.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化 kafka producer
|
||||||
|
canalKafkaProducer = new CanalKafkaProducer();
|
||||||
|
canalKafkaProducer.init(kafkaProperties);
|
||||||
|
|
||||||
|
// 对应每个instance启动一个worker线程
|
||||||
|
List<CanalDestination> destinations = kafkaProperties.getCanalDestinations();
|
||||||
|
|
||||||
|
executorService = Executors.newFixedThreadPool(destinations.size());
|
||||||
|
|
||||||
|
logger.info("## start the kafka workers.");
|
||||||
|
for (final CanalDestination destination : destinations) {
|
||||||
|
executorService.execute(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
worker(destination);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
running = true;
|
||||||
|
logger.info("## the kafka workers is running now ......");
|
||||||
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
logger.info("## stop the kafka workers");
|
||||||
|
running = false;
|
||||||
|
executorService.shutdown();
|
||||||
|
canalKafkaProducer.stop();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
logger.warn("##something goes wrong when stopping kafka workers:", e);
|
||||||
|
} finally {
|
||||||
|
logger.info("## canal kafka is down.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (Throwable e) {
|
||||||
|
logger.error("## Something goes wrong when starting up the canal kafka workers:", e);
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void worker(CanalDestination destination) {
|
||||||
|
while (!running)
|
||||||
|
;
|
||||||
|
while (!CanalServerStarter.isRunning())
|
||||||
|
; // 等待server启动完成
|
||||||
|
logger.info("## start the canal consumer: {}.", destination.getCanalDestination());
|
||||||
|
CanalServerWithEmbedded server = CanalServerWithEmbedded.instance();
|
||||||
|
ClientIdentity clientIdentity = new ClientIdentity(destination.getCanalDestination(), (short) 1001, "");
|
||||||
|
while (running) {
|
||||||
|
try {
|
||||||
|
if (!server.getCanalInstances().containsKey(clientIdentity.getDestination())) {
|
||||||
|
try {
|
||||||
|
Thread.sleep(3000);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
server.subscribe(clientIdentity);
|
||||||
|
logger.info("## the canal consumer {} is running now ......", destination.getCanalDestination());
|
||||||
|
|
||||||
|
while (running) {
|
||||||
|
Message message = server.getWithoutAck(clientIdentity, kafkaProperties.getCanalBatchSize()); // 获取指定数量的数据
|
||||||
|
long batchId = message.getId();
|
||||||
|
try {
|
||||||
|
int size = message.getEntries().size();
|
||||||
|
if (batchId != -1 && size != 0) {
|
||||||
|
if (!StringUtils.isEmpty(destination.getTopic())) {
|
||||||
|
Topic topic = new Topic();
|
||||||
|
topic.setTopic(destination.getTopic());
|
||||||
|
topic.setPartition(destination.getPartition());
|
||||||
|
destination.getTopics().add(topic);
|
||||||
|
}
|
||||||
|
for (Topic topic : destination.getTopics()) {
|
||||||
|
canalKafkaProducer.send(topic, message); // 发送message到所有topic
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (batchId != -1) {
|
||||||
|
server.ack(clientIdentity, batchId); // 提交确认
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
server.rollback(clientIdentity);
|
||||||
|
logger.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("process error!", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,161 @@
|
|||||||
|
package com.alibaba.otter.canal.kafka.producer;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* kafka 配置项
|
||||||
|
*
|
||||||
|
* @author machengyuan 2018-6-11 下午05:30:49
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
public class KafkaProperties {
|
||||||
|
|
||||||
|
private String servers = "localhost:6667";
|
||||||
|
private int retries = 0;
|
||||||
|
private int batchSize = 16384;
|
||||||
|
private int lingerMs = 1;
|
||||||
|
private long bufferMemory = 33554432L;
|
||||||
|
|
||||||
|
private int canalBatchSize = 5;
|
||||||
|
|
||||||
|
private List<CanalDestination> canalDestinations = new ArrayList<CanalDestination>();
|
||||||
|
|
||||||
|
public static class CanalDestination {
|
||||||
|
|
||||||
|
private String canalDestination;
|
||||||
|
private String topic;
|
||||||
|
private Integer partition;
|
||||||
|
private Set<Topic> topics = new HashSet<Topic>();
|
||||||
|
|
||||||
|
public String getCanalDestination() {
|
||||||
|
return canalDestination;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCanalDestination(String canalDestination) {
|
||||||
|
this.canalDestination = canalDestination;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getTopic() {
|
||||||
|
return topic;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTopic(String topic) {
|
||||||
|
this.topic = topic;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getPartition() {
|
||||||
|
return partition;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPartition(Integer partition) {
|
||||||
|
this.partition = partition;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Set<Topic> getTopics() {
|
||||||
|
return topics;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTopics(Set<Topic> topics) {
|
||||||
|
this.topics = topics;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Topic {
|
||||||
|
|
||||||
|
private String topic;
|
||||||
|
private Integer partition;
|
||||||
|
|
||||||
|
public String getTopic() {
|
||||||
|
return topic;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTopic(String topic) {
|
||||||
|
this.topic = topic;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getPartition() {
|
||||||
|
return partition;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPartition(Integer partition) {
|
||||||
|
this.partition = partition;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
|
Topic topic1 = (Topic) o;
|
||||||
|
|
||||||
|
if (topic != null ? !topic.equals(topic1.topic) : topic1.topic != null) return false;
|
||||||
|
return partition != null ? partition.equals(topic1.partition) : topic1.partition == null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int result = topic != null ? topic.hashCode() : 0;
|
||||||
|
result = 31 * result + (partition != null ? partition.hashCode() : 0);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getServers() {
|
||||||
|
return servers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setServers(String servers) {
|
||||||
|
this.servers = servers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRetries() {
|
||||||
|
return retries;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRetries(int retries) {
|
||||||
|
this.retries = retries;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getBatchSize() {
|
||||||
|
return batchSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBatchSize(int batchSize) {
|
||||||
|
this.batchSize = batchSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getLingerMs() {
|
||||||
|
return lingerMs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLingerMs(int lingerMs) {
|
||||||
|
this.lingerMs = lingerMs;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getBufferMemory() {
|
||||||
|
return bufferMemory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBufferMemory(long bufferMemory) {
|
||||||
|
this.bufferMemory = bufferMemory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getCanalBatchSize() {
|
||||||
|
return canalBatchSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCanalBatchSize(int canalBatchSize) {
|
||||||
|
this.canalBatchSize = canalBatchSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<CanalDestination> getCanalDestinations() {
|
||||||
|
return canalDestinations;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCanalDestinations(List<CanalDestination> canalDestinations) {
|
||||||
|
this.canalDestinations = canalDestinations;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
package com.alibaba.otter.canal.kafka.producer;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.apache.kafka.common.errors.SerializationException;
|
||||||
|
import org.apache.kafka.common.serialization.Serializer;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.protocol.CanalEntry;
|
||||||
|
import com.alibaba.otter.canal.protocol.CanalPacket;
|
||||||
|
import com.alibaba.otter.canal.protocol.Message;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Kafka Message类的序列化
|
||||||
|
*
|
||||||
|
* @author machengyuan 2018-6-11 下午05:30:49
|
||||||
|
* @version 1.0.0
|
||||||
|
*/
|
||||||
|
public class MessageSerializer implements Serializer<Message> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configure(Map<String, ?> configs, boolean isKey) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] serialize(String topic, Message data) {
|
||||||
|
try {
|
||||||
|
if (data == null) return null;
|
||||||
|
else {
|
||||||
|
CanalPacket.Messages.Builder messageBuilder = CanalPacket.Messages.newBuilder();
|
||||||
|
if (data.getId() != -1 && !CollectionUtils.isEmpty(data.getEntries())) {
|
||||||
|
for (CanalEntry.Entry entry : data.getEntries()) {
|
||||||
|
messageBuilder.addMessages(entry.toByteString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
CanalPacket.Packet.Builder packetBuilder = CanalPacket.Packet.newBuilder();
|
||||||
|
packetBuilder.setType(CanalPacket.PacketType.MESSAGES);
|
||||||
|
packetBuilder.setBody(messageBuilder.build().toByteString());
|
||||||
|
return packetBuilder.build().toByteArray();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new SerializationException("Error when serializing message to byte[] ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() {
|
||||||
|
// nothing to do
|
||||||
|
}
|
||||||
|
}
|
||||||
18
kafka/src/main/resources/kafka.yml
Normal file
18
kafka/src/main/resources/kafka.yml
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
servers: slave1:6667,slave2:6667,slave3:6667
|
||||||
|
retries: 0
|
||||||
|
batchSize: 16384
|
||||||
|
lingerMs: 1
|
||||||
|
bufferMemory: 33554432
|
||||||
|
# canal的批次大小,单位 k
|
||||||
|
canalBatchSize: 50
|
||||||
|
|
||||||
|
canalDestinations:
|
||||||
|
- canalDestination: example
|
||||||
|
topic: example
|
||||||
|
partition:
|
||||||
|
# 一个destination可以对应多个topic
|
||||||
|
# topics:
|
||||||
|
# - topics: example
|
||||||
|
# partition:
|
||||||
|
|
||||||
|
|
||||||
85
kafka/src/main/resources/logback.xml
Normal file
85
kafka/src/main/resources/logback.xml
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
<configuration scan="true" scanPeriod=" 5 seconds">
|
||||||
|
<jmxConfigurator />
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{56} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<appender name="CANAL-ROOT" class="ch.qos.logback.classic.sift.SiftingAppender">
|
||||||
|
<discriminator>
|
||||||
|
<Key>destination</Key>
|
||||||
|
<DefaultValue>canal</DefaultValue>
|
||||||
|
</discriminator>
|
||||||
|
<sift>
|
||||||
|
<appender name="FILE-${destination}" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||||
|
<File>../logs/${destination}/${destination}.log</File>
|
||||||
|
<rollingPolicy
|
||||||
|
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||||
|
<!-- rollover daily -->
|
||||||
|
<fileNamePattern>../logs/${destination}/%d{yyyy-MM-dd}/${destination}-%d{yyyy-MM-dd}-%i.log.gz</fileNamePattern>
|
||||||
|
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
|
||||||
|
<!-- or whenever the file size reaches 100MB -->
|
||||||
|
<maxFileSize>512MB</maxFileSize>
|
||||||
|
</timeBasedFileNamingAndTriggeringPolicy>
|
||||||
|
<maxHistory>60</maxHistory>
|
||||||
|
</rollingPolicy>
|
||||||
|
<encoder>
|
||||||
|
<pattern>
|
||||||
|
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{56} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
</sift>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<appender name="CANAL-META" class="ch.qos.logback.classic.sift.SiftingAppender">
|
||||||
|
<discriminator>
|
||||||
|
<Key>destination</Key>
|
||||||
|
<DefaultValue>canal</DefaultValue>
|
||||||
|
</discriminator>
|
||||||
|
<sift>
|
||||||
|
<appender name="META-FILE-${destination}" class="ch.qos.logback.core.rolling.RollingFileAppender">
|
||||||
|
<File>../logs/${destination}/meta.log</File>
|
||||||
|
<rollingPolicy
|
||||||
|
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
|
||||||
|
<!-- rollover daily -->
|
||||||
|
<fileNamePattern>../logs/${destination}/%d{yyyy-MM-dd}/meta-%d{yyyy-MM-dd}-%i.log.gz</fileNamePattern>
|
||||||
|
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
|
||||||
|
<!-- or whenever the file size reaches 100MB -->
|
||||||
|
<maxFileSize>32MB</maxFileSize>
|
||||||
|
</timeBasedFileNamingAndTriggeringPolicy>
|
||||||
|
<maxHistory>60</maxHistory>
|
||||||
|
</rollingPolicy>
|
||||||
|
<encoder>
|
||||||
|
<pattern>
|
||||||
|
%d{yyyy-MM-dd HH:mm:ss.SSS} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
</sift>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<logger name="com.alibaba.otter.canal.instance" additivity="false">
|
||||||
|
<level value="INFO" />
|
||||||
|
<appender-ref ref="CANAL-ROOT" />
|
||||||
|
</logger>
|
||||||
|
<logger name="com.alibaba.otter.canal.deployer" additivity="false">
|
||||||
|
<level value="INFO" />
|
||||||
|
<appender-ref ref="CANAL-ROOT" />
|
||||||
|
</logger>
|
||||||
|
<logger name="com.alibaba.otter.canal.meta.FileMixedMetaManager" additivity="false">
|
||||||
|
<level value="INFO" />
|
||||||
|
<appender-ref ref="CANAL-META" />
|
||||||
|
</logger>
|
||||||
|
<logger name="com.alibaba.otter.canal.kafka" additivity="false">
|
||||||
|
<level value="INFO" />
|
||||||
|
<appender-ref ref="CANAL-ROOT" />
|
||||||
|
</logger>
|
||||||
|
|
||||||
|
<root level="WARN">
|
||||||
|
<appender-ref ref="STDOUT"/>
|
||||||
|
<appender-ref ref="CANAL-ROOT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
||||||
@@ -109,9 +109,10 @@ public class FileMixedMetaManager extends MemoryMetaManager implements CanalMeta
|
|||||||
// 定时将内存中的最新值刷到file中,多次变更只刷一次
|
// 定时将内存中的最新值刷到file中,多次变更只刷一次
|
||||||
if (logger.isInfoEnabled()) {
|
if (logger.isInfoEnabled()) {
|
||||||
LogPosition cursor = (LogPosition) getCursor(clientIdentity);
|
LogPosition cursor = (LogPosition) getCursor(clientIdentity);
|
||||||
logger.info("clientId:{} cursor:[{},{},{}] address[{}]",
|
logger.info("clientId:{} cursor:[{},{},{},{},{}] address[{}]", new Object[] {
|
||||||
new Object[] { clientIdentity.getClientId(), cursor.getPostion().getJournalName(),
|
clientIdentity.getClientId(), cursor.getPostion().getJournalName(),
|
||||||
cursor.getPostion().getPosition(), cursor.getPostion().getTimestamp(),
|
cursor.getPostion().getPosition(), cursor.getPostion().getTimestamp(),
|
||||||
|
cursor.getPostion().getServerId(), cursor.getPostion().getGtid(),
|
||||||
cursor.getIdentity().getSourceAddress().toString() });
|
cursor.getIdentity().getSourceAddress().toString() });
|
||||||
}
|
}
|
||||||
flushDataToFile(clientIdentity.getDestination());
|
flushDataToFile(clientIdentity.getDestination());
|
||||||
|
|||||||
@@ -83,7 +83,8 @@ public class MemoryMetaManager extends AbstractCanalLifeCycle implements CanalMe
|
|||||||
}
|
}
|
||||||
|
|
||||||
public synchronized List<ClientIdentity> listAllSubscribeInfo(String destination) throws CanalMetaManagerException {
|
public synchronized List<ClientIdentity> listAllSubscribeInfo(String destination) throws CanalMetaManagerException {
|
||||||
return destinations.get(destination);
|
// fixed issue #657, fixed ConcurrentModificationException
|
||||||
|
return Lists.newArrayList(destinations.get(destination));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Position getCursor(ClientIdentity clientIdentity) throws CanalMetaManagerException {
|
public Position getCursor(ClientIdentity clientIdentity) throws CanalMetaManagerException {
|
||||||
|
|||||||
@@ -73,6 +73,10 @@
|
|||||||
<groupId>org.apache.commons</groupId>
|
<groupId>org.apache.commons</groupId>
|
||||||
<artifactId>commons-compress</artifactId>
|
<artifactId>commons-compress</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.lmax</groupId>
|
||||||
|
<artifactId>disruptor</artifactId>
|
||||||
|
</dependency>
|
||||||
<!-- test dependency -->
|
<!-- test dependency -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import com.alibaba.otter.canal.common.AbstractCanalLifeCycle;
|
|||||||
import com.alibaba.otter.canal.common.alarm.CanalAlarmHandler;
|
import com.alibaba.otter.canal.common.alarm.CanalAlarmHandler;
|
||||||
import com.alibaba.otter.canal.filter.CanalEventFilter;
|
import com.alibaba.otter.canal.filter.CanalEventFilter;
|
||||||
import com.alibaba.otter.canal.parse.CanalEventParser;
|
import com.alibaba.otter.canal.parse.CanalEventParser;
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.MysqlGTIDSet;
|
||||||
import com.alibaba.otter.canal.parse.exception.CanalParseException;
|
import com.alibaba.otter.canal.parse.exception.CanalParseException;
|
||||||
import com.alibaba.otter.canal.parse.exception.TableIdNotFoundException;
|
import com.alibaba.otter.canal.parse.exception.TableIdNotFoundException;
|
||||||
import com.alibaba.otter.canal.parse.inbound.EventTransactionBuffer.TransactionFlushCallback;
|
import com.alibaba.otter.canal.parse.inbound.EventTransactionBuffer.TransactionFlushCallback;
|
||||||
@@ -87,10 +88,19 @@ public abstract class AbstractEventParser<EVENT> extends AbstractCanalLifeCycle
|
|||||||
protected TimerTask heartBeatTimerTask;
|
protected TimerTask heartBeatTimerTask;
|
||||||
protected Throwable exception = null;
|
protected Throwable exception = null;
|
||||||
|
|
||||||
|
protected boolean isGTIDMode = false; // 是否是GTID模式
|
||||||
|
protected boolean parallel = true; // 是否开启并行解析模式
|
||||||
|
protected Integer parallelThreadSize = Runtime.getRuntime()
|
||||||
|
.availableProcessors() * 60 / 100; // 60%的能力跑解析,剩余部分处理网络
|
||||||
|
protected int parallelBufferSize = 256; // 必须为2的幂
|
||||||
|
protected MultiStageCoprocessor multiStageCoprocessor;
|
||||||
|
|
||||||
protected abstract BinlogParser buildParser();
|
protected abstract BinlogParser buildParser();
|
||||||
|
|
||||||
protected abstract ErosaConnection buildErosaConnection();
|
protected abstract ErosaConnection buildErosaConnection();
|
||||||
|
|
||||||
|
protected abstract MultiStageCoprocessor buildMultiStageCoprocessor();
|
||||||
|
|
||||||
protected abstract EntryPosition findStartPosition(ErosaConnection connection) throws IOException;
|
protected abstract EntryPosition findStartPosition(ErosaConnection connection) throws IOException;
|
||||||
|
|
||||||
protected void preDump(ErosaConnection connection) {
|
protected void preDump(ErosaConnection connection) {
|
||||||
@@ -214,14 +224,39 @@ public abstract class AbstractEventParser<EVENT> extends AbstractCanalLifeCycle
|
|||||||
};
|
};
|
||||||
|
|
||||||
// 4. 开始dump数据
|
// 4. 开始dump数据
|
||||||
if (StringUtils.isEmpty(startPosition.getJournalName()) && startPosition.getTimestamp() != null) {
|
if (parallel) {
|
||||||
|
// build stage processor
|
||||||
|
multiStageCoprocessor = buildMultiStageCoprocessor();
|
||||||
|
multiStageCoprocessor.start();
|
||||||
|
|
||||||
|
if (isGTIDMode()) {
|
||||||
|
// 判断所属instance是否启用GTID模式,是的话调用ErosaConnection中GTID对应方法dump数据
|
||||||
|
erosaConnection.dump(MysqlGTIDSet.parse(startPosition.getGtid()), multiStageCoprocessor);
|
||||||
|
} else {
|
||||||
|
if (StringUtils.isEmpty(startPosition.getJournalName())
|
||||||
|
&& startPosition.getTimestamp() != null) {
|
||||||
|
erosaConnection.dump(startPosition.getTimestamp(), multiStageCoprocessor);
|
||||||
|
} else {
|
||||||
|
erosaConnection.dump(startPosition.getJournalName(),
|
||||||
|
startPosition.getPosition(),
|
||||||
|
multiStageCoprocessor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (isGTIDMode()) {
|
||||||
|
// 判断所属instance是否启用GTID模式,是的话调用ErosaConnection中GTID对应方法dump数据
|
||||||
|
erosaConnection.dump(MysqlGTIDSet.parse(startPosition.getGtid()), sinkHandler);
|
||||||
|
} else {
|
||||||
|
if (StringUtils.isEmpty(startPosition.getJournalName())
|
||||||
|
&& startPosition.getTimestamp() != null) {
|
||||||
erosaConnection.dump(startPosition.getTimestamp(), sinkHandler);
|
erosaConnection.dump(startPosition.getTimestamp(), sinkHandler);
|
||||||
} else {
|
} else {
|
||||||
erosaConnection.dump(startPosition.getJournalName(),
|
erosaConnection.dump(startPosition.getJournalName(),
|
||||||
startPosition.getPosition(),
|
startPosition.getPosition(),
|
||||||
sinkHandler);
|
sinkHandler);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (TableIdNotFoundException e) {
|
} catch (TableIdNotFoundException e) {
|
||||||
exception = e;
|
exception = e;
|
||||||
// 特殊处理TableIdNotFound异常,出现这样的异常,一种可能就是起始的position是一个事务当中,导致tablemap
|
// 特殊处理TableIdNotFound异常,出现这样的异常,一种可能就是起始的position是一个事务当中,导致tablemap
|
||||||
@@ -267,6 +302,9 @@ public abstract class AbstractEventParser<EVENT> extends AbstractCanalLifeCycle
|
|||||||
eventSink.interrupt();
|
eventSink.interrupt();
|
||||||
transactionBuffer.reset();// 重置一下缓冲队列,重新记录数据
|
transactionBuffer.reset();// 重置一下缓冲队列,重新记录数据
|
||||||
binlogParser.reset();// 重新置位
|
binlogParser.reset();// 重新置位
|
||||||
|
if (multiStageCoprocessor != null) {
|
||||||
|
multiStageCoprocessor.reset();
|
||||||
|
}
|
||||||
|
|
||||||
if (running) {
|
if (running) {
|
||||||
// sleep一段时间再进行重试
|
// sleep一段时间再进行重试
|
||||||
@@ -293,6 +331,11 @@ public abstract class AbstractEventParser<EVENT> extends AbstractCanalLifeCycle
|
|||||||
stopHeartBeat(); // 先停止心跳
|
stopHeartBeat(); // 先停止心跳
|
||||||
parseThread.interrupt(); // 尝试中断
|
parseThread.interrupt(); // 尝试中断
|
||||||
eventSink.interrupt();
|
eventSink.interrupt();
|
||||||
|
|
||||||
|
if (multiStageCoprocessor != null && multiStageCoprocessor.isStart()) {
|
||||||
|
multiStageCoprocessor.stop();
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
parseThread.join();// 等待其结束
|
parseThread.join();// 等待其结束
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
@@ -368,6 +411,9 @@ public abstract class AbstractEventParser<EVENT> extends AbstractCanalLifeCycle
|
|||||||
position.setTimestamp(entry.getHeader().getExecuteTime());
|
position.setTimestamp(entry.getHeader().getExecuteTime());
|
||||||
// add serverId at 2016-06-28
|
// add serverId at 2016-06-28
|
||||||
position.setServerId(entry.getHeader().getServerId());
|
position.setServerId(entry.getHeader().getServerId());
|
||||||
|
// set gtid
|
||||||
|
position.setGtid(entry.getHeader().getGtid());
|
||||||
|
|
||||||
logPosition.setPostion(position);
|
logPosition.setPostion(position);
|
||||||
|
|
||||||
LogIdentity identity = new LogIdentity(runningInfo.getAddress(), -1L);
|
LogIdentity identity = new LogIdentity(runningInfo.getAddress(), -1L);
|
||||||
@@ -530,4 +576,38 @@ public abstract class AbstractEventParser<EVENT> extends AbstractCanalLifeCycle
|
|||||||
return exception;
|
return exception;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isGTIDMode() {
|
||||||
|
return isGTIDMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setIsGTIDMode(boolean isGTIDMode) {
|
||||||
|
this.isGTIDMode = isGTIDMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isParallel() {
|
||||||
|
return parallel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setParallel(boolean parallel) {
|
||||||
|
this.parallel = parallel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getParallelThreadSize() {
|
||||||
|
return parallelThreadSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setParallelThreadSize(Integer parallelThreadSize) {
|
||||||
|
if (parallelThreadSize != null) {
|
||||||
|
this.parallelThreadSize = parallelThreadSize;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getParallelBufferSize() {
|
||||||
|
return parallelBufferSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setParallelBufferSize(int parallelBufferSize) {
|
||||||
|
this.parallelBufferSize = parallelBufferSize;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ package com.alibaba.otter.canal.parse.inbound;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.GTIDSet;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通用的Erosa的链接接口, 用于一般化处理mysql/oracle的解析过程
|
* 通用的Erosa的链接接口, 用于一般化处理mysql/oracle的解析过程
|
||||||
*
|
*
|
||||||
@@ -24,5 +26,18 @@ public interface ErosaConnection {
|
|||||||
|
|
||||||
public void dump(long timestamp, SinkFunction func) throws IOException;
|
public void dump(long timestamp, SinkFunction func) throws IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通过GTID同步binlog
|
||||||
|
*/
|
||||||
|
public void dump(GTIDSet gtidSet, SinkFunction func) throws IOException;
|
||||||
|
|
||||||
|
// -------------
|
||||||
|
|
||||||
|
public void dump(String binlogfilename, Long binlogPosition, MultiStageCoprocessor coprocessor) throws IOException;
|
||||||
|
|
||||||
|
public void dump(long timestamp, MultiStageCoprocessor coprocessor) throws IOException;
|
||||||
|
|
||||||
|
public void dump(GTIDSet gtidSet, MultiStageCoprocessor coprocessor) throws IOException;
|
||||||
|
|
||||||
ErosaConnection fork();
|
ErosaConnection fork();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.alibaba.otter.canal.parse.inbound;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.common.CanalLifeCycle;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogBuffer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 针对解析器提供一个多阶段协同的处理
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* 1. 网络接收 (单线程)
|
||||||
|
* 2. 事件基本解析 (单线程,事件类型、DDL解析构造TableMeta、维护位点信息)
|
||||||
|
* 3. 事件深度解析 (多线程, DML事件数据的完整解析)
|
||||||
|
* 4. 投递到store (单线程)
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @author agapple 2018年7月3日 下午4:54:17
|
||||||
|
* @since 1.0.26
|
||||||
|
*/
|
||||||
|
public interface MultiStageCoprocessor extends CanalLifeCycle {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 网络数据投递
|
||||||
|
*/
|
||||||
|
public boolean publish(LogBuffer buffer);
|
||||||
|
|
||||||
|
public boolean publish(LogBuffer buffer, String binlogFileName);
|
||||||
|
|
||||||
|
public void reset();
|
||||||
|
}
|
||||||
@@ -7,9 +7,11 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import com.alibaba.otter.canal.filter.CanalEventFilter;
|
import com.alibaba.otter.canal.filter.CanalEventFilter;
|
||||||
import com.alibaba.otter.canal.filter.aviater.AviaterRegexFilter;
|
import com.alibaba.otter.canal.filter.aviater.AviaterRegexFilter;
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.MysqlGTIDSet;
|
||||||
import com.alibaba.otter.canal.parse.exception.CanalParseException;
|
import com.alibaba.otter.canal.parse.exception.CanalParseException;
|
||||||
import com.alibaba.otter.canal.parse.inbound.AbstractEventParser;
|
import com.alibaba.otter.canal.parse.inbound.AbstractEventParser;
|
||||||
import com.alibaba.otter.canal.parse.inbound.BinlogParser;
|
import com.alibaba.otter.canal.parse.inbound.BinlogParser;
|
||||||
|
import com.alibaba.otter.canal.parse.inbound.MultiStageCoprocessor;
|
||||||
import com.alibaba.otter.canal.parse.inbound.mysql.dbsync.LogEventConvert;
|
import com.alibaba.otter.canal.parse.inbound.mysql.dbsync.LogEventConvert;
|
||||||
import com.alibaba.otter.canal.parse.inbound.mysql.tsdb.TableMetaTSDB;
|
import com.alibaba.otter.canal.parse.inbound.mysql.tsdb.TableMetaTSDB;
|
||||||
import com.alibaba.otter.canal.parse.inbound.mysql.tsdb.TableMetaTSDBBuilder;
|
import com.alibaba.otter.canal.parse.inbound.mysql.tsdb.TableMetaTSDBBuilder;
|
||||||
@@ -68,7 +70,18 @@ public abstract class AbstractMysqlEventParser extends AbstractEventParser {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
protected boolean processTableMeta(EntryPosition position) {
|
protected boolean processTableMeta(EntryPosition position) {
|
||||||
|
if (isGTIDMode()) {
|
||||||
|
if (binlogParser instanceof LogEventConvert) {
|
||||||
|
// 记录gtid
|
||||||
|
((LogEventConvert) binlogParser).setGtidSet(MysqlGTIDSet.parse(position.getGtid()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (tableMetaTSDB != null) {
|
if (tableMetaTSDB != null) {
|
||||||
|
if (position.getTimestamp() == null || position.getTimestamp() <= 0) {
|
||||||
|
throw new CanalParseException("use gtid and TableMeta TSDB should be config timestamp > 0");
|
||||||
|
}
|
||||||
|
|
||||||
return tableMetaTSDB.rollback(position);
|
return tableMetaTSDB.rollback(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,6 +118,14 @@ public abstract class AbstractMysqlEventParser extends AbstractEventParser {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected MultiStageCoprocessor buildMultiStageCoprocessor() {
|
||||||
|
return new MysqlMultiStageCoprocessor(parallelBufferSize,
|
||||||
|
parallelThreadSize,
|
||||||
|
(LogEventConvert) binlogParser,
|
||||||
|
transactionBuffer,
|
||||||
|
destination);
|
||||||
|
}
|
||||||
|
|
||||||
// ============================ setter / getter =========================
|
// ============================ setter / getter =========================
|
||||||
|
|
||||||
public void setConnectionCharsetNumber(byte connectionCharsetNumber) {
|
public void setConnectionCharsetNumber(byte connectionCharsetNumber) {
|
||||||
|
|||||||
@@ -4,15 +4,18 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.NotImplementedException;
|
||||||
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;
|
||||||
|
|
||||||
import com.alibaba.otter.canal.parse.exception.CanalParseException;
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.GTIDSet;
|
||||||
import com.alibaba.otter.canal.parse.inbound.ErosaConnection;
|
import com.alibaba.otter.canal.parse.inbound.ErosaConnection;
|
||||||
|
import com.alibaba.otter.canal.parse.inbound.MultiStageCoprocessor;
|
||||||
import com.alibaba.otter.canal.parse.inbound.SinkFunction;
|
import com.alibaba.otter.canal.parse.inbound.SinkFunction;
|
||||||
import com.alibaba.otter.canal.parse.inbound.mysql.local.BinLogFileQueue;
|
import com.alibaba.otter.canal.parse.inbound.mysql.local.BinLogFileQueue;
|
||||||
import com.taobao.tddl.dbsync.binlog.FileLogFetcher;
|
import com.taobao.tddl.dbsync.binlog.FileLogFetcher;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogBuffer;
|
||||||
import com.taobao.tddl.dbsync.binlog.LogContext;
|
import com.taobao.tddl.dbsync.binlog.LogContext;
|
||||||
import com.taobao.tddl.dbsync.binlog.LogDecoder;
|
import com.taobao.tddl.dbsync.binlog.LogDecoder;
|
||||||
import com.taobao.tddl.dbsync.binlog.LogEvent;
|
import com.taobao.tddl.dbsync.binlog.LogEvent;
|
||||||
@@ -91,7 +94,7 @@ public class LocalBinLogConnection implements ErosaConnection {
|
|||||||
while (fetcher.fetch()) {
|
while (fetcher.fetch()) {
|
||||||
event = decoder.decode(fetcher, context);
|
event = decoder.decode(fetcher, context);
|
||||||
if (event == null) {
|
if (event == null) {
|
||||||
throw new CanalParseException("parse failed");
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!func.sink(event)) {
|
if (!func.sink(event)) {
|
||||||
@@ -201,6 +204,137 @@ public class LocalBinLogConnection implements ErosaConnection {
|
|||||||
dump(binlogFilename, binlogFileOffset, func);
|
dump(binlogFilename, binlogFileOffset, func);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dump(GTIDSet gtidSet, SinkFunction func) throws IOException {
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dump(String binlogfilename, Long binlogPosition, MultiStageCoprocessor coprocessor) throws IOException {
|
||||||
|
File current = new File(directory, binlogfilename);
|
||||||
|
|
||||||
|
FileLogFetcher fetcher = new FileLogFetcher(bufferSize);
|
||||||
|
try {
|
||||||
|
fetcher.open(current, binlogPosition);
|
||||||
|
while (running) {
|
||||||
|
boolean needContinue = true;
|
||||||
|
while (fetcher.fetch()) {
|
||||||
|
LogBuffer buffer = fetcher.duplicate();
|
||||||
|
fetcher.consume(fetcher.limit());
|
||||||
|
// set filename
|
||||||
|
if (!coprocessor.publish(buffer, binlogfilename)) {
|
||||||
|
needContinue = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (needContinue) {// 读取下一个
|
||||||
|
fetcher.close(); // 关闭上一个文件
|
||||||
|
|
||||||
|
File nextFile;
|
||||||
|
if (needWait) {
|
||||||
|
nextFile = binlogs.waitForNextFile(current);
|
||||||
|
} else {
|
||||||
|
nextFile = binlogs.getNextFile(current);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nextFile == null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
current = nextFile;
|
||||||
|
fetcher.open(current);
|
||||||
|
binlogfilename = nextFile.getName();
|
||||||
|
} else {
|
||||||
|
break;// 跳出
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
logger.warn("LocalBinLogConnection dump interrupted");
|
||||||
|
} finally {
|
||||||
|
if (fetcher != null) {
|
||||||
|
fetcher.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dump(long timestampMills, MultiStageCoprocessor coprocessor) throws IOException {
|
||||||
|
List<File> currentBinlogs = binlogs.currentBinlogs();
|
||||||
|
File current = currentBinlogs.get(currentBinlogs.size() - 1);
|
||||||
|
long timestampSeconds = timestampMills / 1000;
|
||||||
|
|
||||||
|
String binlogFilename = null;
|
||||||
|
long binlogFileOffset = 0;
|
||||||
|
|
||||||
|
FileLogFetcher fetcher = new FileLogFetcher(bufferSize);
|
||||||
|
LogDecoder decoder = new LogDecoder();
|
||||||
|
decoder.handle(LogEvent.FORMAT_DESCRIPTION_EVENT);
|
||||||
|
decoder.handle(LogEvent.QUERY_EVENT);
|
||||||
|
decoder.handle(LogEvent.XID_EVENT);
|
||||||
|
LogContext context = new LogContext();
|
||||||
|
try {
|
||||||
|
fetcher.open(current);
|
||||||
|
context.setLogPosition(new LogPosition(current.getName()));
|
||||||
|
while (running) {
|
||||||
|
boolean needContinue = true;
|
||||||
|
String lastXidLogFilename = current.getName();
|
||||||
|
long lastXidLogFileOffset = 0;
|
||||||
|
|
||||||
|
binlogFilename = lastXidLogFilename;
|
||||||
|
binlogFileOffset = lastXidLogFileOffset;
|
||||||
|
while (fetcher.fetch()) {
|
||||||
|
LogEvent event = decoder.decode(fetcher, context);
|
||||||
|
if (event != null) {
|
||||||
|
if (event.getWhen() > timestampSeconds) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
needContinue = false;
|
||||||
|
if (LogEvent.QUERY_EVENT == event.getHeader().getType()) {
|
||||||
|
if (StringUtils.endsWithIgnoreCase(((QueryLogEvent) event).getQuery(), "BEGIN")) {
|
||||||
|
binlogFilename = lastXidLogFilename;
|
||||||
|
binlogFileOffset = lastXidLogFileOffset;
|
||||||
|
} else if (StringUtils.endsWithIgnoreCase(((QueryLogEvent) event).getQuery(), "COMMIT")) {
|
||||||
|
lastXidLogFilename = current.getName();
|
||||||
|
lastXidLogFileOffset = event.getLogPos();
|
||||||
|
}
|
||||||
|
} else if (LogEvent.XID_EVENT == event.getHeader().getType()) {
|
||||||
|
lastXidLogFilename = current.getName();
|
||||||
|
lastXidLogFileOffset = event.getLogPos();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (needContinue) {// 读取下一个
|
||||||
|
fetcher.close(); // 关闭上一个文件
|
||||||
|
|
||||||
|
File nextFile = binlogs.getBefore(current);
|
||||||
|
if (nextFile == null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
current = nextFile;
|
||||||
|
fetcher.open(current);
|
||||||
|
context.setLogPosition(new LogPosition(current.getName()));
|
||||||
|
} else {
|
||||||
|
break;// 跳出
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (fetcher != null) {
|
||||||
|
fetcher.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dump(binlogFilename, binlogFileOffset, coprocessor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dump(GTIDSet gtidSet, MultiStageCoprocessor coprocessor) throws IOException {
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
public ErosaConnection fork() {
|
public ErosaConnection fork() {
|
||||||
LocalBinLogConnection connection = new LocalBinLogConnection();
|
LocalBinLogConnection connection = new LocalBinLogConnection();
|
||||||
|
|
||||||
|
|||||||
@@ -15,8 +15,10 @@ import org.slf4j.LoggerFactory;
|
|||||||
import com.alibaba.otter.canal.parse.driver.mysql.MysqlConnector;
|
import com.alibaba.otter.canal.parse.driver.mysql.MysqlConnector;
|
||||||
import com.alibaba.otter.canal.parse.driver.mysql.MysqlQueryExecutor;
|
import com.alibaba.otter.canal.parse.driver.mysql.MysqlQueryExecutor;
|
||||||
import com.alibaba.otter.canal.parse.driver.mysql.MysqlUpdateExecutor;
|
import com.alibaba.otter.canal.parse.driver.mysql.MysqlUpdateExecutor;
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.GTIDSet;
|
||||||
import com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket;
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket;
|
||||||
import com.alibaba.otter.canal.parse.driver.mysql.packets.client.BinlogDumpCommandPacket;
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.client.BinlogDumpCommandPacket;
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.client.BinlogDumpGTIDCommandPacket;
|
||||||
import com.alibaba.otter.canal.parse.driver.mysql.packets.client.RegisterSlaveCommandPacket;
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.client.RegisterSlaveCommandPacket;
|
||||||
import com.alibaba.otter.canal.parse.driver.mysql.packets.client.SemiAckCommandPacket;
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.client.SemiAckCommandPacket;
|
||||||
import com.alibaba.otter.canal.parse.driver.mysql.packets.server.ErrorPacket;
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.server.ErrorPacket;
|
||||||
@@ -24,9 +26,11 @@ import com.alibaba.otter.canal.parse.driver.mysql.packets.server.ResultSetPacket
|
|||||||
import com.alibaba.otter.canal.parse.driver.mysql.utils.PacketManager;
|
import com.alibaba.otter.canal.parse.driver.mysql.utils.PacketManager;
|
||||||
import com.alibaba.otter.canal.parse.exception.CanalParseException;
|
import com.alibaba.otter.canal.parse.exception.CanalParseException;
|
||||||
import com.alibaba.otter.canal.parse.inbound.ErosaConnection;
|
import com.alibaba.otter.canal.parse.inbound.ErosaConnection;
|
||||||
|
import com.alibaba.otter.canal.parse.inbound.MultiStageCoprocessor;
|
||||||
import com.alibaba.otter.canal.parse.inbound.SinkFunction;
|
import com.alibaba.otter.canal.parse.inbound.SinkFunction;
|
||||||
import com.alibaba.otter.canal.parse.inbound.mysql.dbsync.DirectLogFetcher;
|
import com.alibaba.otter.canal.parse.inbound.mysql.dbsync.DirectLogFetcher;
|
||||||
import com.alibaba.otter.canal.parse.support.AuthenticationInfo;
|
import com.alibaba.otter.canal.parse.support.AuthenticationInfo;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogBuffer;
|
||||||
import com.taobao.tddl.dbsync.binlog.LogContext;
|
import com.taobao.tddl.dbsync.binlog.LogContext;
|
||||||
import com.taobao.tddl.dbsync.binlog.LogDecoder;
|
import com.taobao.tddl.dbsync.binlog.LogDecoder;
|
||||||
import com.taobao.tddl.dbsync.binlog.LogEvent;
|
import com.taobao.tddl.dbsync.binlog.LogEvent;
|
||||||
@@ -154,15 +158,89 @@ public class MysqlConnection implements ErosaConnection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (event.getSemival() == 1) {
|
if (event.getSemival() == 1) {
|
||||||
sendSemiAck(context.getLogPosition().getFileName(), binlogPosition);
|
sendSemiAck(context.getLogPosition().getFileName(), context.getLogPosition().getPosition());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dump(GTIDSet gtidSet, SinkFunction func) throws IOException {
|
||||||
|
updateSettings();
|
||||||
|
sendBinlogDumpGTID(gtidSet);
|
||||||
|
|
||||||
|
DirectLogFetcher fetcher = new DirectLogFetcher(connector.getReceiveBufferSize());
|
||||||
|
try {
|
||||||
|
fetcher.start(connector.getChannel());
|
||||||
|
LogDecoder decoder = new LogDecoder(LogEvent.UNKNOWN_EVENT, LogEvent.ENUM_END_EVENT);
|
||||||
|
LogContext context = new LogContext();
|
||||||
|
while (fetcher.fetch()) {
|
||||||
|
LogEvent event = null;
|
||||||
|
event = decoder.decode(fetcher, context);
|
||||||
|
|
||||||
|
if (event == null) {
|
||||||
|
throw new CanalParseException("parse failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!func.sink(event)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
fetcher.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void dump(long timestamp, SinkFunction func) throws IOException {
|
public void dump(long timestamp, SinkFunction func) throws IOException {
|
||||||
throw new NullPointerException("Not implement yet");
|
throw new NullPointerException("Not implement yet");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dump(String binlogfilename, Long binlogPosition, MultiStageCoprocessor coprocessor) throws IOException {
|
||||||
|
updateSettings();
|
||||||
|
sendRegisterSlave();
|
||||||
|
sendBinlogDump(binlogfilename, binlogPosition);
|
||||||
|
((MysqlMultiStageCoprocessor) coprocessor).setConnection(this);
|
||||||
|
DirectLogFetcher fetcher = new DirectLogFetcher(connector.getReceiveBufferSize());
|
||||||
|
try {
|
||||||
|
fetcher.start(connector.getChannel());
|
||||||
|
while (fetcher.fetch()) {
|
||||||
|
LogBuffer buffer = fetcher.duplicate();
|
||||||
|
fetcher.consume(fetcher.limit());
|
||||||
|
if (!coprocessor.publish(buffer)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
fetcher.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dump(long timestamp, MultiStageCoprocessor coprocessor) throws IOException {
|
||||||
|
throw new NullPointerException("Not implement yet");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void dump(GTIDSet gtidSet, MultiStageCoprocessor coprocessor) throws IOException {
|
||||||
|
updateSettings();
|
||||||
|
sendBinlogDumpGTID(gtidSet);
|
||||||
|
|
||||||
|
((MysqlMultiStageCoprocessor) coprocessor).setConnection(this);
|
||||||
|
DirectLogFetcher fetcher = new DirectLogFetcher(connector.getReceiveBufferSize());
|
||||||
|
try {
|
||||||
|
fetcher.start(connector.getChannel());
|
||||||
|
while (fetcher.fetch()) {
|
||||||
|
LogBuffer buffer = fetcher.duplicate();
|
||||||
|
fetcher.consume(fetcher.limit());
|
||||||
|
if (!coprocessor.publish(buffer)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
fetcher.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void sendRegisterSlave() throws IOException {
|
private void sendRegisterSlave() throws IOException {
|
||||||
RegisterSlaveCommandPacket cmd = new RegisterSlaveCommandPacket();
|
RegisterSlaveCommandPacket cmd = new RegisterSlaveCommandPacket();
|
||||||
cmd.reportHost = authInfo.getAddress().getAddress().getHostAddress();
|
cmd.reportHost = authInfo.getAddress().getAddress().getHostAddress();
|
||||||
@@ -208,7 +286,7 @@ public class MysqlConnection implements ErosaConnection {
|
|||||||
connector.setDumping(true);
|
connector.setDumping(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendSemiAck(String binlogfilename, Long binlogPosition) throws IOException {
|
public void sendSemiAck(String binlogfilename, Long binlogPosition) throws IOException {
|
||||||
SemiAckCommandPacket semiAckCmd = new SemiAckCommandPacket();
|
SemiAckCommandPacket semiAckCmd = new SemiAckCommandPacket();
|
||||||
semiAckCmd.binlogFileName = binlogfilename;
|
semiAckCmd.binlogFileName = binlogfilename;
|
||||||
semiAckCmd.binlogPosition = binlogPosition;
|
semiAckCmd.binlogPosition = binlogPosition;
|
||||||
@@ -222,6 +300,20 @@ public class MysqlConnection implements ErosaConnection {
|
|||||||
PacketManager.writePkg(connector.getChannel(), semiAckHeader.toBytes(), cmdBody);
|
PacketManager.writePkg(connector.getChannel(), semiAckHeader.toBytes(), cmdBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void sendBinlogDumpGTID(GTIDSet gtidSet) throws IOException {
|
||||||
|
BinlogDumpGTIDCommandPacket binlogDumpCmd = new BinlogDumpGTIDCommandPacket();
|
||||||
|
binlogDumpCmd.slaveServerId = this.slaveId;
|
||||||
|
binlogDumpCmd.gtidSet = gtidSet;
|
||||||
|
byte[] cmdBody = binlogDumpCmd.toBytes();
|
||||||
|
|
||||||
|
logger.info("COM_BINLOG_DUMP_GTID:{}", binlogDumpCmd);
|
||||||
|
HeaderPacket binlogDumpHeader = new HeaderPacket();
|
||||||
|
binlogDumpHeader.setPacketBodyLength(cmdBody.length);
|
||||||
|
binlogDumpHeader.setPacketSequenceNumber((byte) 0x00);
|
||||||
|
PacketManager.writePkg(connector.getChannel(), binlogDumpHeader.toBytes(), cmdBody);
|
||||||
|
connector.setDumping(true);
|
||||||
|
}
|
||||||
|
|
||||||
public MysqlConnection fork() {
|
public MysqlConnection fork() {
|
||||||
MysqlConnection connection = new MysqlConnection();
|
MysqlConnection connection = new MysqlConnection();
|
||||||
connection.setCharset(getCharset());
|
connection.setCharset(getCharset());
|
||||||
@@ -499,4 +591,5 @@ public class MysqlConnection implements ErosaConnection {
|
|||||||
public void setAuthInfo(AuthenticationInfo authInfo) {
|
public void setAuthInfo(AuthenticationInfo authInfo) {
|
||||||
this.authInfo = authInfo;
|
this.authInfo = authInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -344,6 +344,18 @@ public class MysqlEventParser extends AbstractMysqlEventParser implements CanalE
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected EntryPosition findStartPosition(ErosaConnection connection) throws IOException {
|
protected EntryPosition findStartPosition(ErosaConnection connection) throws IOException {
|
||||||
|
if (isGTIDMode()) {
|
||||||
|
// GTID模式下,CanalLogPositionManager里取最后的gtid,没有则取instanc配置中的
|
||||||
|
LogPosition logPosition = getLogPositionManager().getLatestIndexBy(destination);
|
||||||
|
if (logPosition != null) {
|
||||||
|
return logPosition.getPostion();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (StringUtils.isNotEmpty(masterPosition.getGtid())) {
|
||||||
|
return masterPosition;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
EntryPosition startPosition = findStartPositionInternal(connection);
|
EntryPosition startPosition = findStartPositionInternal(connection);
|
||||||
if (needTransactionPosition.get()) {
|
if (needTransactionPosition.get()) {
|
||||||
logger.warn("prepare to find last position : {}", startPosition.toString());
|
logger.warn("prepare to find last position : {}", startPosition.toString());
|
||||||
@@ -751,7 +763,7 @@ public class MysqlEventParser extends AbstractMysqlEventParser implements CanalE
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (StringUtils.equals(endPosition.getJournalName(), logfilename)
|
if (StringUtils.equals(endPosition.getJournalName(), logfilename)
|
||||||
&& endPosition.getPosition() < logfileoffset) {
|
&& endPosition.getPosition() <= logfileoffset) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,435 @@
|
|||||||
|
package com.alibaba.otter.canal.parse.inbound.mysql;
|
||||||
|
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.locks.LockSupport;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.common.AbstractCanalLifeCycle;
|
||||||
|
import com.alibaba.otter.canal.common.utils.NamedThreadFactory;
|
||||||
|
import com.alibaba.otter.canal.parse.exception.CanalParseException;
|
||||||
|
import com.alibaba.otter.canal.parse.inbound.ErosaConnection;
|
||||||
|
import com.alibaba.otter.canal.parse.inbound.EventTransactionBuffer;
|
||||||
|
import com.alibaba.otter.canal.parse.inbound.MultiStageCoprocessor;
|
||||||
|
import com.alibaba.otter.canal.parse.inbound.TableMeta;
|
||||||
|
import com.alibaba.otter.canal.parse.inbound.mysql.dbsync.LogEventConvert;
|
||||||
|
import com.alibaba.otter.canal.protocol.CanalEntry;
|
||||||
|
import com.lmax.disruptor.BatchEventProcessor;
|
||||||
|
import com.lmax.disruptor.BlockingWaitStrategy;
|
||||||
|
import com.lmax.disruptor.EventFactory;
|
||||||
|
import com.lmax.disruptor.EventHandler;
|
||||||
|
import com.lmax.disruptor.ExceptionHandler;
|
||||||
|
import com.lmax.disruptor.InsufficientCapacityException;
|
||||||
|
import com.lmax.disruptor.LifecycleAware;
|
||||||
|
import com.lmax.disruptor.RingBuffer;
|
||||||
|
import com.lmax.disruptor.Sequence;
|
||||||
|
import com.lmax.disruptor.SequenceBarrier;
|
||||||
|
import com.lmax.disruptor.WorkHandler;
|
||||||
|
import com.lmax.disruptor.WorkerPool;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogBuffer;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogContext;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogDecoder;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogEvent;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogPosition;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.event.DeleteRowsLogEvent;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.event.RowsLogEvent;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.event.UpdateRowsLogEvent;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.event.WriteRowsLogEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 针对解析器提供一个多阶段协同的处理
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* 1. 网络接收 (单线程)
|
||||||
|
* 2. 事件基本解析 (单线程,事件类型、DDL解析构造TableMeta、维护位点信息)
|
||||||
|
* 3. 事件深度解析 (多线程, DML事件数据的完整解析)
|
||||||
|
* 4. 投递到store (单线程)
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @author agapple 2018年7月3日 下午4:54:17
|
||||||
|
* @since 1.0.26
|
||||||
|
*/
|
||||||
|
public class MysqlMultiStageCoprocessor extends AbstractCanalLifeCycle implements MultiStageCoprocessor {
|
||||||
|
|
||||||
|
private LogEventConvert logEventConvert;
|
||||||
|
private EventTransactionBuffer transactionBuffer;
|
||||||
|
private ErosaConnection connection;
|
||||||
|
|
||||||
|
private int parserThreadCount;
|
||||||
|
private int ringBufferSize;
|
||||||
|
private RingBuffer<MessageEvent> disruptorMsgBuffer;
|
||||||
|
private ExecutorService parserExecutor;
|
||||||
|
private ExecutorService stageExecutor;
|
||||||
|
private String destination;
|
||||||
|
private CanalParseException exception;
|
||||||
|
|
||||||
|
public MysqlMultiStageCoprocessor(int ringBufferSize, int parserThreadCount, LogEventConvert logEventConvert,
|
||||||
|
EventTransactionBuffer transactionBuffer, String destination){
|
||||||
|
this.ringBufferSize = ringBufferSize;
|
||||||
|
this.parserThreadCount = parserThreadCount;
|
||||||
|
this.logEventConvert = logEventConvert;
|
||||||
|
this.transactionBuffer = transactionBuffer;
|
||||||
|
this.destination = destination;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void start() {
|
||||||
|
super.start();
|
||||||
|
this.exception = null;
|
||||||
|
this.disruptorMsgBuffer = RingBuffer.createSingleProducer(new MessageEventFactory(),
|
||||||
|
ringBufferSize,
|
||||||
|
new BlockingWaitStrategy());
|
||||||
|
|
||||||
|
this.parserExecutor = Executors.newFixedThreadPool(parserThreadCount,
|
||||||
|
new NamedThreadFactory("MultiStageCoprocessor-Parser-" + destination));
|
||||||
|
|
||||||
|
this.stageExecutor = Executors.newFixedThreadPool(2, new NamedThreadFactory("MultiStageCoprocessor-other-"
|
||||||
|
+ destination));
|
||||||
|
SequenceBarrier sequenceBarrier = disruptorMsgBuffer.newBarrier();
|
||||||
|
ExceptionHandler exceptionHandler = new SimpleFatalExceptionHandler();
|
||||||
|
// stage 2
|
||||||
|
BatchEventProcessor<MessageEvent> simpleParserStage = new BatchEventProcessor<MessageEvent>(disruptorMsgBuffer,
|
||||||
|
sequenceBarrier,
|
||||||
|
new SimpleParserStage());
|
||||||
|
simpleParserStage.setExceptionHandler(exceptionHandler);
|
||||||
|
disruptorMsgBuffer.addGatingSequences(simpleParserStage.getSequence());
|
||||||
|
|
||||||
|
// stage 3
|
||||||
|
SequenceBarrier dmlParserSequenceBarrier = disruptorMsgBuffer.newBarrier(simpleParserStage.getSequence());
|
||||||
|
WorkHandler<MessageEvent>[] workHandlers = new DmlParserStage[parserThreadCount];
|
||||||
|
for (int i = 0; i < parserThreadCount; i++) {
|
||||||
|
workHandlers[i] = new DmlParserStage();
|
||||||
|
}
|
||||||
|
WorkerPool<MessageEvent> workerPool = new WorkerPool<MessageEvent>(disruptorMsgBuffer,
|
||||||
|
dmlParserSequenceBarrier,
|
||||||
|
exceptionHandler,
|
||||||
|
workHandlers);
|
||||||
|
Sequence[] sequence = workerPool.getWorkerSequences();
|
||||||
|
disruptorMsgBuffer.addGatingSequences(sequence);
|
||||||
|
|
||||||
|
// stage 4
|
||||||
|
SequenceBarrier sinkSequenceBarrier = disruptorMsgBuffer.newBarrier(sequence);
|
||||||
|
BatchEventProcessor<MessageEvent> sinkStoreStage = new BatchEventProcessor<MessageEvent>(disruptorMsgBuffer,
|
||||||
|
sinkSequenceBarrier,
|
||||||
|
new SinkStoreStage());
|
||||||
|
sinkStoreStage.setExceptionHandler(exceptionHandler);
|
||||||
|
disruptorMsgBuffer.addGatingSequences(sinkStoreStage.getSequence());
|
||||||
|
|
||||||
|
// start work
|
||||||
|
stageExecutor.submit(simpleParserStage);
|
||||||
|
stageExecutor.submit(sinkStoreStage);
|
||||||
|
workerPool.start(parserExecutor);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop() {
|
||||||
|
try {
|
||||||
|
parserExecutor.shutdownNow();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
stageExecutor.shutdownNow();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
super.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 网络数据投递
|
||||||
|
*/
|
||||||
|
public boolean publish(LogBuffer buffer) {
|
||||||
|
return publish(buffer, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean publish(LogBuffer buffer, String binlogFileName) {
|
||||||
|
if (!isStart()) {
|
||||||
|
if (exception != null) {
|
||||||
|
throw exception;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean interupted = false;
|
||||||
|
do {
|
||||||
|
try {
|
||||||
|
long next = disruptorMsgBuffer.tryNext();
|
||||||
|
MessageEvent event = disruptorMsgBuffer.get(next);
|
||||||
|
event.setBuffer(buffer);
|
||||||
|
if (binlogFileName != null) {
|
||||||
|
event.setBinlogFileName(binlogFileName);
|
||||||
|
}
|
||||||
|
disruptorMsgBuffer.publish(next);
|
||||||
|
break;
|
||||||
|
} catch (InsufficientCapacityException e) {
|
||||||
|
// park
|
||||||
|
LockSupport.parkNanos(1L);
|
||||||
|
interupted = Thread.interrupted();
|
||||||
|
}
|
||||||
|
} while (!interupted && isStart());
|
||||||
|
|
||||||
|
if (exception != null) {
|
||||||
|
throw exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
return isStart();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reset() {
|
||||||
|
if (isStart()) {
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
start();
|
||||||
|
}
|
||||||
|
|
||||||
|
private class SimpleParserStage implements EventHandler<MessageEvent>, LifecycleAware {
|
||||||
|
|
||||||
|
private LogDecoder decoder;
|
||||||
|
private LogContext context;
|
||||||
|
|
||||||
|
public SimpleParserStage(){
|
||||||
|
decoder = new LogDecoder(LogEvent.UNKNOWN_EVENT, LogEvent.ENUM_END_EVENT);
|
||||||
|
context = new LogContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onEvent(MessageEvent event, long sequence, boolean endOfBatch) throws Exception {
|
||||||
|
try {
|
||||||
|
LogBuffer buffer = event.getBuffer();
|
||||||
|
if (StringUtils.isNotEmpty(event.getBinlogFileName())
|
||||||
|
&& !context.getLogPosition().getFileName().equals(event.getBinlogFileName())) {
|
||||||
|
// set roate binlog file name
|
||||||
|
context.setLogPosition(new LogPosition(event.getBinlogFileName(), context.getLogPosition()
|
||||||
|
.getPosition()));
|
||||||
|
}
|
||||||
|
|
||||||
|
LogEvent logEvent = decoder.decode(buffer, context);
|
||||||
|
event.setEvent(logEvent);
|
||||||
|
|
||||||
|
int eventType = logEvent.getHeader().getType();
|
||||||
|
TableMeta tableMeta = null;
|
||||||
|
boolean needDmlParse = false;
|
||||||
|
switch (eventType) {
|
||||||
|
case LogEvent.WRITE_ROWS_EVENT_V1:
|
||||||
|
case LogEvent.WRITE_ROWS_EVENT:
|
||||||
|
tableMeta = logEventConvert.parseRowsEventForTableMeta((WriteRowsLogEvent) logEvent);
|
||||||
|
needDmlParse = true;
|
||||||
|
break;
|
||||||
|
case LogEvent.UPDATE_ROWS_EVENT_V1:
|
||||||
|
case LogEvent.UPDATE_ROWS_EVENT:
|
||||||
|
tableMeta = logEventConvert.parseRowsEventForTableMeta((UpdateRowsLogEvent) logEvent);
|
||||||
|
needDmlParse = true;
|
||||||
|
break;
|
||||||
|
case LogEvent.DELETE_ROWS_EVENT_V1:
|
||||||
|
case LogEvent.DELETE_ROWS_EVENT:
|
||||||
|
tableMeta = logEventConvert.parseRowsEventForTableMeta((DeleteRowsLogEvent) logEvent);
|
||||||
|
needDmlParse = true;
|
||||||
|
break;
|
||||||
|
case LogEvent.ROWS_QUERY_LOG_EVENT:
|
||||||
|
needDmlParse = true;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
CanalEntry.Entry entry = logEventConvert.parse(event.getEvent(), false);
|
||||||
|
event.setEntry(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 记录一下DML的表结构
|
||||||
|
event.setNeedDmlParse(needDmlParse);
|
||||||
|
event.setTable(tableMeta);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
exception = new CanalParseException(e);
|
||||||
|
throw exception;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStart() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onShutdown() {
|
||||||
|
if (isStart()) {
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class DmlParserStage implements WorkHandler<MessageEvent>, LifecycleAware {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onEvent(MessageEvent event) throws Exception {
|
||||||
|
try {
|
||||||
|
if (event.isNeedDmlParse()) {
|
||||||
|
int eventType = event.getEvent().getHeader().getType();
|
||||||
|
CanalEntry.Entry entry = null;
|
||||||
|
switch (eventType) {
|
||||||
|
case LogEvent.ROWS_QUERY_LOG_EVENT:
|
||||||
|
entry = logEventConvert.parse(event.getEvent(), false);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
// 单独解析dml事件
|
||||||
|
entry = logEventConvert.parseRowsEvent((RowsLogEvent) event.getEvent(), event.getTable());
|
||||||
|
}
|
||||||
|
|
||||||
|
event.setEntry(entry);
|
||||||
|
}
|
||||||
|
} catch (Throwable e) {
|
||||||
|
exception = new CanalParseException(e);
|
||||||
|
throw exception;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStart() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onShutdown() {
|
||||||
|
if (isStart()) {
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private class SinkStoreStage implements EventHandler<MessageEvent>, LifecycleAware {
|
||||||
|
|
||||||
|
public void onEvent(MessageEvent event, long sequence, boolean endOfBatch) throws Exception {
|
||||||
|
try {
|
||||||
|
if (event.getEntry() != null) {
|
||||||
|
transactionBuffer.add(event.getEntry());
|
||||||
|
}
|
||||||
|
|
||||||
|
LogEvent logEvent = event.getEvent();
|
||||||
|
if (connection instanceof MysqlConnection && logEvent.getSemival() == 1) {
|
||||||
|
// semi ack回报
|
||||||
|
((MysqlConnection) connection).sendSemiAck(logEvent.getHeader().getLogFileName(),
|
||||||
|
logEvent.getHeader().getLogPos());
|
||||||
|
}
|
||||||
|
|
||||||
|
// clear for gc
|
||||||
|
event.setBuffer(null);
|
||||||
|
event.setBinlogFileName(null);
|
||||||
|
event.setEvent(null);
|
||||||
|
event.setTable(null);
|
||||||
|
event.setEntry(null);
|
||||||
|
event.setNeedDmlParse(false);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
exception = new CanalParseException(e);
|
||||||
|
throw exception;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onStart() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onShutdown() {
|
||||||
|
if (isStart()) {
|
||||||
|
stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class MessageEvent {
|
||||||
|
|
||||||
|
private String binlogFileName; // for local binlog parse
|
||||||
|
private LogBuffer buffer;
|
||||||
|
private CanalEntry.Entry entry;
|
||||||
|
private boolean needDmlParse = false;
|
||||||
|
private TableMeta table;
|
||||||
|
private LogEvent event;
|
||||||
|
|
||||||
|
public String getBinlogFileName() {
|
||||||
|
return binlogFileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBinlogFileName(String binlogFileName) {
|
||||||
|
this.binlogFileName = binlogFileName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LogBuffer getBuffer() {
|
||||||
|
return buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBuffer(LogBuffer buffer) {
|
||||||
|
this.buffer = buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LogEvent getEvent() {
|
||||||
|
return event;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEvent(LogEvent event) {
|
||||||
|
this.event = event;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CanalEntry.Entry getEntry() {
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEntry(CanalEntry.Entry entry) {
|
||||||
|
this.entry = entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isNeedDmlParse() {
|
||||||
|
return needDmlParse;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNeedDmlParse(boolean needDmlParse) {
|
||||||
|
this.needDmlParse = needDmlParse;
|
||||||
|
}
|
||||||
|
|
||||||
|
public TableMeta getTable() {
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTable(TableMeta table) {
|
||||||
|
this.table = table;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class SimpleFatalExceptionHandler implements ExceptionHandler {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleEventException(final Throwable ex, final long sequence, final Object event) {
|
||||||
|
throw new RuntimeException(ex);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleOnStartException(final Throwable ex) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handleOnShutdownException(final Throwable ex) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class MessageEventFactory implements EventFactory<MessageEvent> {
|
||||||
|
|
||||||
|
public MessageEvent newInstance() {
|
||||||
|
return new MessageEvent();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLogEventConvert(LogEventConvert logEventConvert) {
|
||||||
|
this.logEventConvert = logEventConvert;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTransactionBuffer(EventTransactionBuffer transactionBuffer) {
|
||||||
|
this.transactionBuffer = transactionBuffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConnection(ErosaConnection connection) {
|
||||||
|
this.connection = connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -64,9 +64,6 @@ public class DirectLogFetcher extends LogFetcher {
|
|||||||
if ("1".equals(dbsemi)) {
|
if ("1".equals(dbsemi)) {
|
||||||
issemi = true;
|
issemi = true;
|
||||||
}
|
}
|
||||||
// 和mysql driver一样,提供buffer机制,提升读取binlog速度
|
|
||||||
// this.input = new
|
|
||||||
// BufferedInputStream(channel.socket().getInputStream(), 16384);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -171,10 +168,13 @@ public class DirectLogFetcher extends LogFetcher {
|
|||||||
private final boolean fetch0(final int off, final int len) throws IOException {
|
private final boolean fetch0(final int off, final int len) throws IOException {
|
||||||
ensureCapacity(off + len);
|
ensureCapacity(off + len);
|
||||||
|
|
||||||
byte[] read = channel.read(len, READ_TIMEOUT_MILLISECONDS);
|
// byte[] read = channel.read(len, READ_TIMEOUT_MILLISECONDS);
|
||||||
System.arraycopy(read, 0, this.buffer, off, len);
|
// System.arraycopy(read, 0, this.buffer, off, len);
|
||||||
|
|
||||||
if (limit < off + len) limit = off + len;
|
channel.read(buffer, off, len, READ_TIMEOUT_MILLISECONDS);
|
||||||
|
if (limit < off + len) {
|
||||||
|
limit = off + len;
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import com.alibaba.otter.canal.common.AbstractCanalLifeCycle;
|
import com.alibaba.otter.canal.common.AbstractCanalLifeCycle;
|
||||||
import com.alibaba.otter.canal.filter.aviater.AviaterRegexFilter;
|
import com.alibaba.otter.canal.filter.aviater.AviaterRegexFilter;
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.GTIDSet;
|
||||||
import com.alibaba.otter.canal.parse.exception.CanalParseException;
|
import com.alibaba.otter.canal.parse.exception.CanalParseException;
|
||||||
import com.alibaba.otter.canal.parse.exception.TableIdNotFoundException;
|
import com.alibaba.otter.canal.parse.exception.TableIdNotFoundException;
|
||||||
import com.alibaba.otter.canal.parse.inbound.BinlogParser;
|
import com.alibaba.otter.canal.parse.inbound.BinlogParser;
|
||||||
@@ -40,11 +41,11 @@ import com.alibaba.otter.canal.protocol.position.EntryPosition;
|
|||||||
import com.google.protobuf.ByteString;
|
import com.google.protobuf.ByteString;
|
||||||
import com.taobao.tddl.dbsync.binlog.LogEvent;
|
import com.taobao.tddl.dbsync.binlog.LogEvent;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.DeleteRowsLogEvent;
|
import com.taobao.tddl.dbsync.binlog.event.DeleteRowsLogEvent;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.event.GtidLogEvent;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.IntvarLogEvent;
|
import com.taobao.tddl.dbsync.binlog.event.IntvarLogEvent;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.LogHeader;
|
import com.taobao.tddl.dbsync.binlog.event.LogHeader;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.QueryLogEvent;
|
import com.taobao.tddl.dbsync.binlog.event.QueryLogEvent;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.RandLogEvent;
|
import com.taobao.tddl.dbsync.binlog.event.RandLogEvent;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.RotateLogEvent;
|
|
||||||
import com.taobao.tddl.dbsync.binlog.event.RowsLogBuffer;
|
import com.taobao.tddl.dbsync.binlog.event.RowsLogBuffer;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.RowsLogEvent;
|
import com.taobao.tddl.dbsync.binlog.event.RowsLogEvent;
|
||||||
import com.taobao.tddl.dbsync.binlog.event.RowsQueryLogEvent;
|
import com.taobao.tddl.dbsync.binlog.event.RowsQueryLogEvent;
|
||||||
@@ -65,6 +66,12 @@ import com.taobao.tddl.dbsync.binlog.event.mariadb.AnnotateRowsEvent;
|
|||||||
*/
|
*/
|
||||||
public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogParser<LogEvent> {
|
public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogParser<LogEvent> {
|
||||||
|
|
||||||
|
public static final String XA_XID = "XA_XID";
|
||||||
|
public static final String XA_TYPE = "XA_TYPE";
|
||||||
|
public static final String XA_START = "XA START";
|
||||||
|
public static final String XA_END = "XA END";
|
||||||
|
public static final String XA_COMMIT = "XA COMMIT";
|
||||||
|
public static final String XA_ROLLBACK = "XA ROLLBACK";
|
||||||
public static final String ISO_8859_1 = "ISO-8859-1";
|
public static final String ISO_8859_1 = "ISO-8859-1";
|
||||||
public static final String UTF_8 = "UTF-8";
|
public static final String UTF_8 = "UTF-8";
|
||||||
public static final int TINYINT_MAX_VALUE = 256;
|
public static final int TINYINT_MAX_VALUE = 256;
|
||||||
@@ -81,7 +88,6 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
private volatile AviaterRegexFilter nameBlackFilter;
|
private volatile AviaterRegexFilter nameBlackFilter;
|
||||||
|
|
||||||
private TableMetaCache tableMetaCache;
|
private TableMetaCache tableMetaCache;
|
||||||
private String binlogFileName = "mysql-bin.000001";
|
|
||||||
private Charset charset = Charset.defaultCharset();
|
private Charset charset = Charset.defaultCharset();
|
||||||
private boolean filterQueryDcl = false;
|
private boolean filterQueryDcl = false;
|
||||||
private boolean filterQueryDml = false;
|
private boolean filterQueryDml = false;
|
||||||
@@ -92,6 +98,17 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
private boolean filterRows = false;
|
private boolean filterRows = false;
|
||||||
private boolean useDruidDdlFilter = true;
|
private boolean useDruidDdlFilter = true;
|
||||||
|
|
||||||
|
// latest gtid
|
||||||
|
private GTIDSet gtidSet;
|
||||||
|
|
||||||
|
public LogEventConvert(GTIDSet gtidSet){
|
||||||
|
this.gtidSet = gtidSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LogEventConvert(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Entry parse(LogEvent logEvent, boolean isSeek) throws CanalParseException {
|
public Entry parse(LogEvent logEvent, boolean isSeek) throws CanalParseException {
|
||||||
if (logEvent == null || logEvent instanceof UnknownLogEvent) {
|
if (logEvent == null || logEvent instanceof UnknownLogEvent) {
|
||||||
@@ -100,9 +117,6 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
|
|
||||||
int eventType = logEvent.getHeader().getType();
|
int eventType = logEvent.getHeader().getType();
|
||||||
switch (eventType) {
|
switch (eventType) {
|
||||||
case LogEvent.ROTATE_EVENT:
|
|
||||||
binlogFileName = ((RotateLogEvent) logEvent).getFilename();
|
|
||||||
break;
|
|
||||||
case LogEvent.QUERY_EVENT:
|
case LogEvent.QUERY_EVENT:
|
||||||
return parseQueryEvent((QueryLogEvent) logEvent, isSeek);
|
return parseQueryEvent((QueryLogEvent) logEvent, isSeek);
|
||||||
case LogEvent.XID_EVENT:
|
case LogEvent.XID_EVENT:
|
||||||
@@ -128,6 +142,8 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
return parseIntrvarLogEvent((IntvarLogEvent) logEvent);
|
return parseIntrvarLogEvent((IntvarLogEvent) logEvent);
|
||||||
case LogEvent.RAND_EVENT:
|
case LogEvent.RAND_EVENT:
|
||||||
return parseRandLogEvent((RandLogEvent) logEvent);
|
return parseRandLogEvent((RandLogEvent) logEvent);
|
||||||
|
case LogEvent.GTID_LOG_EVENT:
|
||||||
|
return parseGTIDLogEvent((GtidLogEvent) logEvent);
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -137,21 +153,76 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
|
|
||||||
public void reset() {
|
public void reset() {
|
||||||
// do nothing
|
// do nothing
|
||||||
binlogFileName = "mysql-bin.000001";
|
|
||||||
if (tableMetaCache != null) {
|
if (tableMetaCache != null) {
|
||||||
tableMetaCache.clearTableMeta();
|
tableMetaCache.clearTableMeta();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Entry parseGTIDLogEvent(GtidLogEvent logEvent) {
|
||||||
|
LogHeader logHeader = logEvent.getHeader();
|
||||||
|
String value = logEvent.getSid().toString() + ":" + logEvent.getGno();
|
||||||
|
Pair.Builder builder = Pair.newBuilder();
|
||||||
|
builder.setKey("gtid");
|
||||||
|
builder.setValue(value);
|
||||||
|
if (gtidSet != null) {
|
||||||
|
gtidSet.update(value);
|
||||||
|
}
|
||||||
|
if (logEvent.getLastCommitted() != null) {
|
||||||
|
builder.setKey("lastCommitted");
|
||||||
|
builder.setValue(String.valueOf(logEvent.getLastCommitted()));
|
||||||
|
builder.setKey("sequenceNumber");
|
||||||
|
builder.setValue(String.valueOf(logEvent.getSequenceNumber()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Header header = createHeader(logHeader, "", "", EventType.GTID);
|
||||||
|
return createEntry(header, EntryType.GTIDLOG, builder.build().toByteString());
|
||||||
|
}
|
||||||
|
|
||||||
private Entry parseQueryEvent(QueryLogEvent event, boolean isSeek) {
|
private Entry parseQueryEvent(QueryLogEvent event, boolean isSeek) {
|
||||||
String queryString = event.getQuery();
|
String queryString = event.getQuery();
|
||||||
if (StringUtils.endsWithIgnoreCase(queryString, BEGIN)) {
|
if (StringUtils.startsWithIgnoreCase(queryString, XA_START)) {
|
||||||
|
// xa start use TransactionBegin
|
||||||
|
TransactionBegin.Builder beginBuilder = TransactionBegin.newBuilder();
|
||||||
|
beginBuilder.setThreadId(event.getSessionId());
|
||||||
|
beginBuilder.addProps(createSpecialPair(XA_TYPE, XA_START));
|
||||||
|
beginBuilder.addProps(createSpecialPair(XA_XID, getXaXid(queryString, XA_START)));
|
||||||
|
TransactionBegin transactionBegin = beginBuilder.build();
|
||||||
|
Header header = createHeader(event.getHeader(), "", "", null);
|
||||||
|
return createEntry(header, EntryType.TRANSACTIONBEGIN, transactionBegin.toByteString());
|
||||||
|
} else if (StringUtils.startsWithIgnoreCase(queryString, XA_END)) {
|
||||||
|
// xa start use TransactionEnd
|
||||||
|
TransactionEnd.Builder endBuilder = TransactionEnd.newBuilder();
|
||||||
|
endBuilder.setTransactionId(String.valueOf(0L));
|
||||||
|
endBuilder.addProps(createSpecialPair(XA_TYPE, XA_END));
|
||||||
|
endBuilder.addProps(createSpecialPair(XA_XID, getXaXid(queryString, XA_END)));
|
||||||
|
TransactionEnd transactionEnd = endBuilder.build();
|
||||||
|
Header header = createHeader(event.getHeader(), "", "", null);
|
||||||
|
return createEntry(header, EntryType.TRANSACTIONEND, transactionEnd.toByteString());
|
||||||
|
} else if (StringUtils.startsWithIgnoreCase(queryString, XA_COMMIT)) {
|
||||||
|
// xa commit
|
||||||
|
Header header = createHeader(event.getHeader(), "", "", EventType.XACOMMIT);
|
||||||
|
RowChange.Builder rowChangeBuider = RowChange.newBuilder();
|
||||||
|
rowChangeBuider.setSql(queryString);
|
||||||
|
rowChangeBuider.addProps(createSpecialPair(XA_TYPE, XA_COMMIT));
|
||||||
|
rowChangeBuider.addProps(createSpecialPair(XA_XID, getXaXid(queryString, XA_COMMIT)));
|
||||||
|
rowChangeBuider.setEventType(EventType.XACOMMIT);
|
||||||
|
return createEntry(header, EntryType.ROWDATA, rowChangeBuider.build().toByteString());
|
||||||
|
} else if (StringUtils.startsWithIgnoreCase(queryString, XA_ROLLBACK)) {
|
||||||
|
// xa rollback
|
||||||
|
Header header = createHeader(event.getHeader(), "", "", EventType.XAROLLBACK);
|
||||||
|
RowChange.Builder rowChangeBuider = RowChange.newBuilder();
|
||||||
|
rowChangeBuider.setSql(queryString);
|
||||||
|
rowChangeBuider.addProps(createSpecialPair(XA_TYPE, XA_ROLLBACK));
|
||||||
|
rowChangeBuider.addProps(createSpecialPair(XA_XID, getXaXid(queryString, XA_ROLLBACK)));
|
||||||
|
rowChangeBuider.setEventType(EventType.XAROLLBACK);
|
||||||
|
return createEntry(header, EntryType.ROWDATA, rowChangeBuider.build().toByteString());
|
||||||
|
} else if (StringUtils.endsWithIgnoreCase(queryString, BEGIN)) {
|
||||||
TransactionBegin transactionBegin = createTransactionBegin(event.getSessionId());
|
TransactionBegin transactionBegin = createTransactionBegin(event.getSessionId());
|
||||||
Header header = createHeader(binlogFileName, event.getHeader(), "", "", null);
|
Header header = createHeader(event.getHeader(), "", "", null);
|
||||||
return createEntry(header, EntryType.TRANSACTIONBEGIN, transactionBegin.toByteString());
|
return createEntry(header, EntryType.TRANSACTIONBEGIN, transactionBegin.toByteString());
|
||||||
} else if (StringUtils.endsWithIgnoreCase(queryString, COMMIT)) {
|
} else if (StringUtils.endsWithIgnoreCase(queryString, COMMIT)) {
|
||||||
TransactionEnd transactionEnd = createTransactionEnd(0L); // MyISAM可能不会有xid事件
|
TransactionEnd transactionEnd = createTransactionEnd(0L); // MyISAM可能不会有xid事件
|
||||||
Header header = createHeader(binlogFileName, event.getHeader(), "", "", null);
|
Header header = createHeader(event.getHeader(), "", "", null);
|
||||||
return createEntry(header, EntryType.TRANSACTIONEND, transactionEnd.toByteString());
|
return createEntry(header, EntryType.TRANSACTIONEND, transactionEnd.toByteString());
|
||||||
} else {
|
} else {
|
||||||
boolean notFilter = false;
|
boolean notFilter = false;
|
||||||
@@ -194,9 +265,10 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
tableMetaCache.apply(position, event.getDbName(), queryString, null);
|
tableMetaCache.apply(position, event.getDbName(), queryString, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
Header header = createHeader(binlogFileName, event.getHeader(), schemaName, tableName, type);
|
Header header = createHeader(event.getHeader(), schemaName, tableName, type);
|
||||||
RowChange.Builder rowChangeBuider = RowChange.newBuilder();
|
RowChange.Builder rowChangeBuider = RowChange.newBuilder();
|
||||||
if (type != EventType.QUERY) {
|
if (type != EventType.QUERY && type != EventType.INSERT && type != EventType.UPDATE
|
||||||
|
&& type != EventType.DELETE) {
|
||||||
rowChangeBuider.setIsDdl(true);
|
rowChangeBuider.setIsDdl(true);
|
||||||
}
|
}
|
||||||
rowChangeBuider.setSql(queryString);
|
rowChangeBuider.setSql(queryString);
|
||||||
@@ -208,6 +280,10 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getXaXid(String queryString, String type) {
|
||||||
|
return StringUtils.substringAfter(queryString, type);
|
||||||
|
}
|
||||||
|
|
||||||
private boolean processFilter(String queryString, DdlResult result) {
|
private boolean processFilter(String queryString, DdlResult result) {
|
||||||
String schemaName = result.getSchemaName();
|
String schemaName = result.getSchemaName();
|
||||||
String tableName = result.getTableName();
|
String tableName = result.getTableName();
|
||||||
@@ -349,15 +425,11 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
|
|
||||||
private Entry parseXidEvent(XidLogEvent event) {
|
private Entry parseXidEvent(XidLogEvent event) {
|
||||||
TransactionEnd transactionEnd = createTransactionEnd(event.getXid());
|
TransactionEnd transactionEnd = createTransactionEnd(event.getXid());
|
||||||
Header header = createHeader(binlogFileName, event.getHeader(), "", "", null);
|
Header header = createHeader(event.getHeader(), "", "", null);
|
||||||
return createEntry(header, EntryType.TRANSACTIONEND, transactionEnd.toByteString());
|
return createEntry(header, EntryType.TRANSACTIONEND, transactionEnd.toByteString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Entry parseRowsEvent(RowsLogEvent event) {
|
public TableMeta parseRowsEventForTableMeta(RowsLogEvent event) {
|
||||||
if (filterRows) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
TableMapLogEvent table = event.getTable();
|
TableMapLogEvent table = event.getTable();
|
||||||
if (table == null) {
|
if (table == null) {
|
||||||
// tableId对应的记录不存在
|
// tableId对应的记录不存在
|
||||||
@@ -365,8 +437,7 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
}
|
}
|
||||||
|
|
||||||
boolean isHeartBeat = isAliSQLHeartBeat(table.getDbName(), table.getTableName());
|
boolean isHeartBeat = isAliSQLHeartBeat(table.getDbName(), table.getTableName());
|
||||||
boolean isRDSHeartBeat = tableMetaCache.isOnRDS()
|
boolean isRDSHeartBeat = tableMetaCache.isOnRDS() && isRDSHeartBeat(table.getDbName(), table.getTableName());
|
||||||
&& isRDSHeartBeat(table.getDbName(), table.getTableName());
|
|
||||||
|
|
||||||
String fullname = table.getDbName() + "." + table.getTableName();
|
String fullname = table.getDbName() + "." + table.getTableName();
|
||||||
// check name filter
|
// check name filter
|
||||||
@@ -392,10 +463,41 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
// 处理alisql模式的test.heartbeat心跳数据
|
// 处理alisql模式的test.heartbeat心跳数据
|
||||||
// 心跳表基本无权限,需要mock一个tableMeta
|
// 心跳表基本无权限,需要mock一个tableMeta
|
||||||
FieldMeta idMeta = new FieldMeta("id", "smallint(6)", false, true, null);
|
FieldMeta idMeta = new FieldMeta("id", "smallint(6)", false, true, null);
|
||||||
FieldMeta typeMeta = new FieldMeta("type", "int(11)", true, false, null);
|
FieldMeta typeMeta = new FieldMeta("ts", "int(11)", true, false, null);
|
||||||
tableMeta = new TableMeta(table.getDbName(), table.getTableName(), Arrays.asList(idMeta, typeMeta));
|
tableMeta = new TableMeta(table.getDbName(), table.getTableName(), Arrays.asList(idMeta, typeMeta));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EntryPosition position = createPosition(event.getHeader());
|
||||||
|
if (tableMetaCache != null && tableMeta == null) {// 入错存在table meta
|
||||||
|
tableMeta = getTableMeta(table.getDbName(), table.getTableName(), true, position);
|
||||||
|
if (tableMeta == null) {
|
||||||
|
if (!filterTableError) {
|
||||||
|
throw new CanalParseException("not found [" + fullname + "] in db , pls check!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tableMeta;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Entry parseRowsEvent(RowsLogEvent event) {
|
||||||
|
return parseRowsEvent(event, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Entry parseRowsEvent(RowsLogEvent event, TableMeta tableMeta) {
|
||||||
|
if (filterRows) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (tableMeta == null) { // 如果没有外部指定
|
||||||
|
tableMeta = parseRowsEventForTableMeta(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tableMeta == null) {
|
||||||
|
// 拿不到表结构,执行忽略
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
EventType eventType = null;
|
EventType eventType = null;
|
||||||
int type = event.getHeader().getType();
|
int type = event.getHeader().getType();
|
||||||
if (LogEvent.WRITE_ROWS_EVENT_V1 == type || LogEvent.WRITE_ROWS_EVENT == type) {
|
if (LogEvent.WRITE_ROWS_EVENT_V1 == type || LogEvent.WRITE_ROWS_EVENT == type) {
|
||||||
@@ -408,12 +510,6 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
throw new CanalParseException("unsupport event type :" + event.getHeader().getType());
|
throw new CanalParseException("unsupport event type :" + event.getHeader().getType());
|
||||||
}
|
}
|
||||||
|
|
||||||
Header header = createHeader(binlogFileName,
|
|
||||||
event.getHeader(),
|
|
||||||
table.getDbName(),
|
|
||||||
table.getTableName(),
|
|
||||||
eventType);
|
|
||||||
EntryPosition position = createPosition(event.getHeader());
|
|
||||||
RowChange.Builder rowChangeBuider = RowChange.newBuilder();
|
RowChange.Builder rowChangeBuider = RowChange.newBuilder();
|
||||||
rowChangeBuider.setTableId(event.getTableId());
|
rowChangeBuider.setTableId(event.getTableId());
|
||||||
rowChangeBuider.setIsDdl(false);
|
rowChangeBuider.setIsDdl(false);
|
||||||
@@ -422,18 +518,9 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
RowsLogBuffer buffer = event.getRowsBuf(charset.name());
|
RowsLogBuffer buffer = event.getRowsBuf(charset.name());
|
||||||
BitSet columns = event.getColumns();
|
BitSet columns = event.getColumns();
|
||||||
BitSet changeColumns = event.getChangeColumns();
|
BitSet changeColumns = event.getChangeColumns();
|
||||||
boolean tableError = false;
|
|
||||||
if (tableMetaCache != null && tableMeta == null) {// 入错存在table meta
|
|
||||||
// cache
|
|
||||||
tableMeta = getTableMeta(table.getDbName(), table.getTableName(), true, position);
|
|
||||||
if (tableMeta == null) {
|
|
||||||
tableError = true;
|
|
||||||
if (!filterTableError) {
|
|
||||||
throw new CanalParseException("not found [" + fullname + "] in db , pls check!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
boolean tableError = false;
|
||||||
|
int rowsCount = 0;
|
||||||
while (buffer.nextOneRow(columns)) {
|
while (buffer.nextOneRow(columns)) {
|
||||||
// 处理row记录
|
// 处理row记录
|
||||||
RowData.Builder rowDataBuilder = RowData.newBuilder();
|
RowData.Builder rowDataBuilder = RowData.newBuilder();
|
||||||
@@ -454,16 +541,20 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
tableError |= parseOneRow(rowDataBuilder, event, buffer, changeColumns, true, tableMeta);
|
tableError |= parseOneRow(rowDataBuilder, event, buffer, changeColumns, true, tableMeta);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rowsCount ++;
|
||||||
rowChangeBuider.addRowDatas(rowDataBuilder.build());
|
rowChangeBuider.addRowDatas(rowDataBuilder.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TableMapLogEvent table = event.getTable();
|
||||||
|
Header header = createHeader(event.getHeader(), table.getDbName(), table.getTableName(), eventType, rowsCount);
|
||||||
|
|
||||||
RowChange rowChange = rowChangeBuider.build();
|
RowChange rowChange = rowChangeBuider.build();
|
||||||
if (tableError) {
|
if (tableError) {
|
||||||
Entry entry = createEntry(header, EntryType.ROWDATA, ByteString.EMPTY);
|
Entry entry = createEntry(header, EntryType.ROWDATA, ByteString.EMPTY);
|
||||||
logger.warn("table parser error : {}storeValue: {}", entry.toString(), rowChange.toString());
|
logger.warn("table parser error : {}storeValue: {}", entry.toString(), rowChange.toString());
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
Entry entry = createEntry(header, EntryType.ROWDATA, rowChangeBuider.build().toByteString());
|
Entry entry = createEntry(header, EntryType.ROWDATA, rowChange.toByteString());
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -472,7 +563,7 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
}
|
}
|
||||||
|
|
||||||
private EntryPosition createPosition(LogHeader logHeader) {
|
private EntryPosition createPosition(LogHeader logHeader) {
|
||||||
return new EntryPosition(binlogFileName,
|
return new EntryPosition(logHeader.getLogFileName(),
|
||||||
logHeader.getLogPos(),
|
logHeader.getLogPos(),
|
||||||
logHeader.getWhen() * 1000L,
|
logHeader.getWhen() * 1000L,
|
||||||
logHeader.getServerId()); // 记录到秒
|
logHeader.getServerId()); // 记录到秒
|
||||||
@@ -559,11 +650,14 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
// fixed issue
|
// fixed issue
|
||||||
// https://github.com/alibaba/canal/issues/66,特殊处理binary/varbinary,不能做编码处理
|
// https://github.com/alibaba/canal/issues/66,特殊处理binary/varbinary,不能做编码处理
|
||||||
boolean isBinary = false;
|
boolean isBinary = false;
|
||||||
|
boolean isSingleBit = false;
|
||||||
if (fieldMeta != null) {
|
if (fieldMeta != null) {
|
||||||
if (StringUtils.containsIgnoreCase(fieldMeta.getColumnType(), "VARBINARY")) {
|
if (StringUtils.containsIgnoreCase(fieldMeta.getColumnType(), "VARBINARY")) {
|
||||||
isBinary = true;
|
isBinary = true;
|
||||||
} else if (StringUtils.containsIgnoreCase(fieldMeta.getColumnType(), "BINARY")) {
|
} else if (StringUtils.containsIgnoreCase(fieldMeta.getColumnType(), "BINARY")) {
|
||||||
isBinary = true;
|
isBinary = true;
|
||||||
|
} else if (StringUtils.containsIgnoreCase(fieldMeta.getColumnType(), "TINYINT(1)")) {
|
||||||
|
isSingleBit = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
buffer.nextValue(info.type, info.meta, isBinary);
|
buffer.nextValue(info.type, info.meta, isBinary);
|
||||||
@@ -573,6 +667,9 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
}
|
}
|
||||||
|
|
||||||
int javaType = buffer.getJavaType();
|
int javaType = buffer.getJavaType();
|
||||||
|
if (isSingleBit && javaType == Types.TINYINT) {
|
||||||
|
javaType = Types.BIT;
|
||||||
|
}
|
||||||
if (buffer.isNull()) {
|
if (buffer.isNull()) {
|
||||||
columnBuilder.setIsNull(true);
|
columnBuilder.setIsNull(true);
|
||||||
} else {
|
} else {
|
||||||
@@ -689,7 +786,7 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Entry buildQueryEntry(String queryString, LogHeader logHeader, String tableName) {
|
private Entry buildQueryEntry(String queryString, LogHeader logHeader, String tableName) {
|
||||||
Header header = createHeader(binlogFileName, logHeader, "", tableName, EventType.QUERY);
|
Header header = createHeader(logHeader, "", tableName, EventType.QUERY);
|
||||||
RowChange.Builder rowChangeBuider = RowChange.newBuilder();
|
RowChange.Builder rowChangeBuider = RowChange.newBuilder();
|
||||||
rowChangeBuider.setSql(queryString);
|
rowChangeBuider.setSql(queryString);
|
||||||
rowChangeBuider.setEventType(EventType.QUERY);
|
rowChangeBuider.setEventType(EventType.QUERY);
|
||||||
@@ -697,19 +794,23 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Entry buildQueryEntry(String queryString, LogHeader logHeader) {
|
private Entry buildQueryEntry(String queryString, LogHeader logHeader) {
|
||||||
Header header = createHeader(binlogFileName, logHeader, "", "", EventType.QUERY);
|
Header header = createHeader(logHeader, "", "", EventType.QUERY);
|
||||||
RowChange.Builder rowChangeBuider = RowChange.newBuilder();
|
RowChange.Builder rowChangeBuider = RowChange.newBuilder();
|
||||||
rowChangeBuider.setSql(queryString);
|
rowChangeBuider.setSql(queryString);
|
||||||
rowChangeBuider.setEventType(EventType.QUERY);
|
rowChangeBuider.setEventType(EventType.QUERY);
|
||||||
return createEntry(header, EntryType.ROWDATA, rowChangeBuider.build().toByteString());
|
return createEntry(header, EntryType.ROWDATA, rowChangeBuider.build().toByteString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Header createHeader(String binlogFile, LogHeader logHeader, String schemaName, String tableName,
|
|
||||||
EventType eventType) {
|
private Header createHeader(LogHeader logHeader, String schemaName, String tableName, EventType eventType) {
|
||||||
|
return createHeader(logHeader, schemaName, tableName, eventType, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Header createHeader(LogHeader logHeader, String schemaName, String tableName, EventType eventType, Integer rowsCount) {
|
||||||
// header会做信息冗余,方便以后做检索或者过滤
|
// header会做信息冗余,方便以后做检索或者过滤
|
||||||
Header.Builder headerBuilder = Header.newBuilder();
|
Header.Builder headerBuilder = Header.newBuilder();
|
||||||
headerBuilder.setVersion(version);
|
headerBuilder.setVersion(version);
|
||||||
headerBuilder.setLogfileName(binlogFile);
|
headerBuilder.setLogfileName(logHeader.getLogFileName());
|
||||||
headerBuilder.setLogfileOffset(logHeader.getLogPos() - logHeader.getEventLen());
|
headerBuilder.setLogfileOffset(logHeader.getLogPos() - logHeader.getEventLen());
|
||||||
headerBuilder.setServerId(logHeader.getServerId());
|
headerBuilder.setServerId(logHeader.getServerId());
|
||||||
headerBuilder.setServerenCode(UTF_8);// 经过java输出后所有的编码为unicode
|
headerBuilder.setServerenCode(UTF_8);// 经过java输出后所有的编码为unicode
|
||||||
@@ -725,6 +826,17 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
headerBuilder.setTableName(tableName);
|
headerBuilder.setTableName(tableName);
|
||||||
}
|
}
|
||||||
headerBuilder.setEventLength(logHeader.getEventLen());
|
headerBuilder.setEventLength(logHeader.getEventLen());
|
||||||
|
// enable gtid position
|
||||||
|
if (gtidSet != null) {
|
||||||
|
String gtid = gtidSet.toString();
|
||||||
|
headerBuilder.setGtid(gtid);
|
||||||
|
}
|
||||||
|
|
||||||
|
// add rowsCount suppport
|
||||||
|
if (rowsCount > 0) {
|
||||||
|
Pair pair = createSpecialPair("rowsCount", String.valueOf(rowsCount));
|
||||||
|
headerBuilder.addProps(pair);
|
||||||
|
}
|
||||||
return headerBuilder.build();
|
return headerBuilder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -845,4 +957,8 @@ public class LogEventConvert extends AbstractCanalLifeCycle implements BinlogPar
|
|||||||
this.filterRows = filterRows;
|
this.filterRows = filterRows;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setGtidSet(GTIDSet gtidSet) {
|
||||||
|
this.gtidSet = gtidSet;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import java.util.ArrayList;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.StringUtils;
|
||||||
|
|
||||||
import com.alibaba.fastsql.sql.SQLUtils;
|
import com.alibaba.fastsql.sql.SQLUtils;
|
||||||
import com.alibaba.fastsql.sql.ast.SQLExpr;
|
import com.alibaba.fastsql.sql.ast.SQLExpr;
|
||||||
import com.alibaba.fastsql.sql.ast.SQLStatement;
|
import com.alibaba.fastsql.sql.ast.SQLStatement;
|
||||||
@@ -18,9 +20,11 @@ import com.alibaba.fastsql.sql.ast.statement.SQLAlterTableItem;
|
|||||||
import com.alibaba.fastsql.sql.ast.statement.SQLAlterTableRename;
|
import com.alibaba.fastsql.sql.ast.statement.SQLAlterTableRename;
|
||||||
import com.alibaba.fastsql.sql.ast.statement.SQLAlterTableStatement;
|
import com.alibaba.fastsql.sql.ast.statement.SQLAlterTableStatement;
|
||||||
import com.alibaba.fastsql.sql.ast.statement.SQLConstraint;
|
import com.alibaba.fastsql.sql.ast.statement.SQLConstraint;
|
||||||
|
import com.alibaba.fastsql.sql.ast.statement.SQLCreateDatabaseStatement;
|
||||||
import com.alibaba.fastsql.sql.ast.statement.SQLCreateIndexStatement;
|
import com.alibaba.fastsql.sql.ast.statement.SQLCreateIndexStatement;
|
||||||
import com.alibaba.fastsql.sql.ast.statement.SQLCreateTableStatement;
|
import com.alibaba.fastsql.sql.ast.statement.SQLCreateTableStatement;
|
||||||
import com.alibaba.fastsql.sql.ast.statement.SQLDeleteStatement;
|
import com.alibaba.fastsql.sql.ast.statement.SQLDeleteStatement;
|
||||||
|
import com.alibaba.fastsql.sql.ast.statement.SQLDropDatabaseStatement;
|
||||||
import com.alibaba.fastsql.sql.ast.statement.SQLDropIndexStatement;
|
import com.alibaba.fastsql.sql.ast.statement.SQLDropIndexStatement;
|
||||||
import com.alibaba.fastsql.sql.ast.statement.SQLDropTableStatement;
|
import com.alibaba.fastsql.sql.ast.statement.SQLDropTableStatement;
|
||||||
import com.alibaba.fastsql.sql.ast.statement.SQLExprTableSource;
|
import com.alibaba.fastsql.sql.ast.statement.SQLExprTableSource;
|
||||||
@@ -157,6 +161,17 @@ public class DruidDdlParser {
|
|||||||
processName(ddlResult, schmeaName, delete.getTableName(), false);
|
processName(ddlResult, schmeaName, delete.getTableName(), false);
|
||||||
ddlResult.setType(EventType.DELETE);
|
ddlResult.setType(EventType.DELETE);
|
||||||
ddlResults.add(ddlResult);
|
ddlResults.add(ddlResult);
|
||||||
|
} else if (statement instanceof SQLCreateDatabaseStatement) {
|
||||||
|
DdlResult ddlResult = new DdlResult();
|
||||||
|
ddlResult.setType(EventType.QUERY);
|
||||||
|
// 只设置schema
|
||||||
|
processName(ddlResult, schmeaName, null, false);
|
||||||
|
ddlResults.add(ddlResult);
|
||||||
|
} else if (statement instanceof SQLDropDatabaseStatement) {
|
||||||
|
DdlResult ddlResult = new DdlResult();
|
||||||
|
ddlResult.setType(EventType.QUERY);
|
||||||
|
// 只设置schema
|
||||||
|
processName(ddlResult, schmeaName, null, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,6 +180,9 @@ public class DruidDdlParser {
|
|||||||
|
|
||||||
private static void processName(DdlResult ddlResult, String schema, SQLExpr sqlName, boolean isOri) {
|
private static void processName(DdlResult ddlResult, String schema, SQLExpr sqlName, boolean isOri) {
|
||||||
if (sqlName == null) {
|
if (sqlName == null) {
|
||||||
|
if (StringUtils.isNotBlank(schema)) {
|
||||||
|
ddlResult.setSchemaName(schema);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -70,7 +70,9 @@ public class DatabaseTableMeta implements TableMetaTSDB {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Thread newThread(Runnable r) {
|
public Thread newThread(Runnable r) {
|
||||||
return new Thread(r, "[scheduler-table-meta-snapshot]");
|
Thread thread = new Thread(r, "[scheduler-table-meta-snapshot]");
|
||||||
|
thread.setDaemon(true);
|
||||||
|
return thread;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -298,7 +300,17 @@ public class DatabaseTableMeta implements TableMetaTSDB {
|
|||||||
createDDL = packet.getFieldValues().get(1);
|
createDDL = packet.getFieldValues().get(1);
|
||||||
tableMetaFromDB.setFields(TableMetaCache.parseTableMeta(schema, table, packet));
|
tableMetaFromDB.setFields(TableMetaCache.parseTableMeta(schema, table, packet));
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (Throwable e) {
|
||||||
|
try {
|
||||||
|
// retry for broke pipe, see:
|
||||||
|
// https://github.com/alibaba/canal/issues/724
|
||||||
|
connection.reconnect();
|
||||||
|
ResultSetPacket packet = connection.query("show create table " + getFullName(schema, table));
|
||||||
|
if (packet.getFieldValues().size() > 1) {
|
||||||
|
createDDL = packet.getFieldValues().get(1);
|
||||||
|
tableMetaFromDB.setFields(TableMetaCache.parseTableMeta(schema, table, packet));
|
||||||
|
}
|
||||||
|
} catch (IOException e1) {
|
||||||
if (e.getMessage().contains("errorNumber=1146")) {
|
if (e.getMessage().contains("errorNumber=1146")) {
|
||||||
logger.error("table not exist in db , pls check :" + getFullName(schema, table) + " , mem : "
|
logger.error("table not exist in db , pls check :" + getFullName(schema, table) + " , mem : "
|
||||||
+ tableMetaFromMem);
|
+ tableMetaFromMem);
|
||||||
@@ -306,6 +318,7 @@ public class DatabaseTableMeta implements TableMetaTSDB {
|
|||||||
}
|
}
|
||||||
throw new CanalParseException(e);
|
throw new CanalParseException(e);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
boolean result = compareTableMeta(tableMetaFromMem, tableMetaFromDB);
|
boolean result = compareTableMeta(tableMetaFromMem, tableMetaFromDB);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import com.alibaba.fastsql.sql.ast.SQLExpr;
|
|||||||
import com.alibaba.fastsql.sql.ast.SQLStatement;
|
import com.alibaba.fastsql.sql.ast.SQLStatement;
|
||||||
import com.alibaba.fastsql.sql.ast.expr.SQLCharExpr;
|
import com.alibaba.fastsql.sql.ast.expr.SQLCharExpr;
|
||||||
import com.alibaba.fastsql.sql.ast.expr.SQLIdentifierExpr;
|
import com.alibaba.fastsql.sql.ast.expr.SQLIdentifierExpr;
|
||||||
|
import com.alibaba.fastsql.sql.ast.expr.SQLMethodInvokeExpr;
|
||||||
import com.alibaba.fastsql.sql.ast.expr.SQLNullExpr;
|
import com.alibaba.fastsql.sql.ast.expr.SQLNullExpr;
|
||||||
import com.alibaba.fastsql.sql.ast.expr.SQLPropertyExpr;
|
import com.alibaba.fastsql.sql.ast.expr.SQLPropertyExpr;
|
||||||
import com.alibaba.fastsql.sql.ast.statement.SQLColumnConstraint;
|
import com.alibaba.fastsql.sql.ast.statement.SQLColumnConstraint;
|
||||||
@@ -93,7 +94,7 @@ public class MemoryTableMeta implements TableMetaTSDB {
|
|||||||
tableMeta = tableMetas.get(keys);
|
tableMeta = tableMetas.get(keys);
|
||||||
if (tableMeta == null) {
|
if (tableMeta == null) {
|
||||||
Schema schemaRep = repository.findSchema(schema);
|
Schema schemaRep = repository.findSchema(schema);
|
||||||
if (schema == null) {
|
if (schemaRep == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
SchemaObject data = schemaRep.findTable(table);
|
SchemaObject data = schemaRep.findTable(table);
|
||||||
@@ -243,6 +244,8 @@ public class MemoryTableMeta implements TableMetaTSDB {
|
|||||||
return DruidDdlParser.unescapeName(((SQLIdentifierExpr) sqlName).getName());
|
return DruidDdlParser.unescapeName(((SQLIdentifierExpr) sqlName).getName());
|
||||||
} else if (sqlName instanceof SQLCharExpr) {
|
} else if (sqlName instanceof SQLCharExpr) {
|
||||||
return ((SQLCharExpr) sqlName).getText();
|
return ((SQLCharExpr) sqlName).getText();
|
||||||
|
} else if (sqlName instanceof SQLMethodInvokeExpr) {
|
||||||
|
return DruidDdlParser.unescapeName(((SQLMethodInvokeExpr) sqlName).getMethodName());
|
||||||
} else {
|
} else {
|
||||||
return sqlName.toString();
|
return sqlName.toString();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,7 +87,6 @@ public class MetaHistoryDO {
|
|||||||
this.useSchema = useSchema;
|
this.useSchema = useSchema;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String getExtra() {
|
public String getExtra() {
|
||||||
return extra;
|
return extra;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,89 @@
|
|||||||
|
package com.alibaba.otter.canal.parse;
|
||||||
|
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.common.AbstractCanalLifeCycle;
|
||||||
|
import com.alibaba.otter.canal.parse.exception.CanalParseException;
|
||||||
|
import com.alibaba.otter.canal.parse.inbound.mysql.MysqlEventParser;
|
||||||
|
import com.alibaba.otter.canal.parse.index.AbstractLogPositionManager;
|
||||||
|
import com.alibaba.otter.canal.parse.support.AuthenticationInfo;
|
||||||
|
import com.alibaba.otter.canal.protocol.CanalEntry;
|
||||||
|
import com.alibaba.otter.canal.protocol.position.EntryPosition;
|
||||||
|
import com.alibaba.otter.canal.protocol.position.LogPosition;
|
||||||
|
import com.alibaba.otter.canal.sink.CanalEventSink;
|
||||||
|
import com.alibaba.otter.canal.sink.exception.CanalSinkException;
|
||||||
|
|
||||||
|
public class MysqlBinlogDumpPerformanceTest {
|
||||||
|
|
||||||
|
public static void main(String args[]) {
|
||||||
|
final MysqlEventParser controller = new MysqlEventParser();
|
||||||
|
final EntryPosition startPosition = new EntryPosition("mysql-bin.001699", 120L, 100L);
|
||||||
|
controller.setConnectionCharset(Charset.forName("UTF-8"));
|
||||||
|
controller.setSlaveId(3344L);
|
||||||
|
controller.setDetectingEnable(false);
|
||||||
|
controller.setFilterQueryDml(true);
|
||||||
|
controller.setMasterInfo(new AuthenticationInfo(new InetSocketAddress("127.0.0.1", 3328), "root", "hello"));
|
||||||
|
controller.setMasterPosition(startPosition);
|
||||||
|
controller.setEnableTsdb(false);
|
||||||
|
controller.setDestination("example");
|
||||||
|
controller.setTsdbSpringXml("classpath:spring/tsdb/h2-tsdb.xml");
|
||||||
|
// controller.setEventFilter(new AviaterRegexFilter("test\\..*"));
|
||||||
|
// controller.setEventBlackFilter(new
|
||||||
|
// AviaterRegexFilter("canal_tsdb\\..*"));
|
||||||
|
controller.setParallel(true);
|
||||||
|
controller.setParallelBufferSize(256);
|
||||||
|
controller.setParallelThreadSize(16);
|
||||||
|
controller.setIsGTIDMode(false);
|
||||||
|
final AtomicLong sum = new AtomicLong(0);
|
||||||
|
final AtomicLong last = new AtomicLong(0);
|
||||||
|
final AtomicLong start = new AtomicLong(System.currentTimeMillis());
|
||||||
|
final AtomicLong end = new AtomicLong(0);
|
||||||
|
controller.setEventSink(new AbstractCanalEventSinkTest<List<CanalEntry.Entry>>() {
|
||||||
|
|
||||||
|
public boolean sink(List<CanalEntry.Entry> entrys, InetSocketAddress remoteAddress, String destination)
|
||||||
|
throws CanalSinkException,
|
||||||
|
InterruptedException {
|
||||||
|
|
||||||
|
sum.addAndGet(entrys.size());
|
||||||
|
long current = sum.get();
|
||||||
|
if (current - last.get() >= 100000) {
|
||||||
|
end.set(System.currentTimeMillis());
|
||||||
|
long tps = ((current - last.get()) * 1000) / (end.get() - start.get());
|
||||||
|
System.out.println(" total : " + sum + " , cost : " + (end.get() - start.get()) + " , tps : " + tps);
|
||||||
|
last.set(current);
|
||||||
|
start.set(end.get());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
});
|
||||||
|
controller.setLogPositionManager(new AbstractLogPositionManager() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public LogPosition getLatestIndexBy(String destination) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void persistLogPosition(String destination, LogPosition logPosition) throws CanalParseException {
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
controller.start();
|
||||||
|
|
||||||
|
try {
|
||||||
|
Thread.sleep(100 * 1000 * 1000L);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
}
|
||||||
|
controller.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static abstract class AbstractCanalEventSinkTest<T> extends AbstractCanalLifeCycle implements CanalEventSink<T> {
|
||||||
|
|
||||||
|
public void interrupt() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
package com.alibaba.otter.canal.parse;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.MysqlConnector;
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.MysqlUpdateExecutor;
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket;
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.client.BinlogDumpCommandPacket;
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.utils.PacketManager;
|
||||||
|
import com.alibaba.otter.canal.parse.inbound.mysql.dbsync.DirectLogFetcher;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogContext;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogDecoder;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogEvent;
|
||||||
|
|
||||||
|
public class MysqlBinlogEventPerformanceTest {
|
||||||
|
|
||||||
|
protected static Charset charset = Charset.forName("utf-8");
|
||||||
|
|
||||||
|
public static void main(String args[]) {
|
||||||
|
DirectLogFetcher fetcher = new DirectLogFetcher();
|
||||||
|
try {
|
||||||
|
MysqlConnector connector = new MysqlConnector(new InetSocketAddress("127.0.0.1", 3306), "root", "hello");
|
||||||
|
connector.connect();
|
||||||
|
updateSettings(connector);
|
||||||
|
sendBinlogDump(connector, "mysql-bin.000006", 120L, 3);
|
||||||
|
fetcher.start(connector.getChannel());
|
||||||
|
LogDecoder decoder = new LogDecoder(LogEvent.UNKNOWN_EVENT, LogEvent.ENUM_END_EVENT);
|
||||||
|
LogContext context = new LogContext();
|
||||||
|
AtomicLong sum = new AtomicLong(0);
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
long last = 0;
|
||||||
|
long end = 0;
|
||||||
|
while (fetcher.fetch()) {
|
||||||
|
decoder.decode(fetcher, context);
|
||||||
|
sum.incrementAndGet();
|
||||||
|
long current = sum.get();
|
||||||
|
if (current - last >= 100000) {
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
long tps = ((current - last) * 1000) / (end - start);
|
||||||
|
System.out.println(" total : " + sum + " , cost : " + (end - start) + " , tps : " + tps);
|
||||||
|
last = current;
|
||||||
|
start = end;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
fetcher.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void sendBinlogDump(MysqlConnector connector, String binlogfilename, Long binlogPosition, int slaveId)
|
||||||
|
throws IOException {
|
||||||
|
BinlogDumpCommandPacket binlogDumpCmd = new BinlogDumpCommandPacket();
|
||||||
|
binlogDumpCmd.binlogFileName = binlogfilename;
|
||||||
|
binlogDumpCmd.binlogPosition = binlogPosition;
|
||||||
|
binlogDumpCmd.slaveServerId = slaveId;
|
||||||
|
byte[] cmdBody = binlogDumpCmd.toBytes();
|
||||||
|
|
||||||
|
HeaderPacket binlogDumpHeader = new HeaderPacket();
|
||||||
|
binlogDumpHeader.setPacketBodyLength(cmdBody.length);
|
||||||
|
binlogDumpHeader.setPacketSequenceNumber((byte) 0x00);
|
||||||
|
PacketManager.writePkg(connector.getChannel(), binlogDumpHeader.toBytes(), cmdBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void updateSettings(MysqlConnector connector) throws IOException {
|
||||||
|
update("set @master_binlog_checksum= '@@global.binlog_checksum'", connector);
|
||||||
|
update("SET @mariadb_slave_capability='" + LogEvent.MARIA_SLAVE_CAPABILITY_MINE + "'", connector);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void update(String cmd, MysqlConnector connector) throws IOException {
|
||||||
|
MysqlUpdateExecutor exector = new MysqlUpdateExecutor(connector);
|
||||||
|
exector.update(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,192 @@
|
|||||||
|
package com.alibaba.otter.canal.parse;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.BitSet;
|
||||||
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.MysqlConnector;
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.MysqlUpdateExecutor;
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.HeaderPacket;
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.packets.client.BinlogDumpCommandPacket;
|
||||||
|
import com.alibaba.otter.canal.parse.driver.mysql.utils.PacketManager;
|
||||||
|
import com.alibaba.otter.canal.parse.inbound.mysql.dbsync.DirectLogFetcher;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogBuffer;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogContext;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogDecoder;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.LogEvent;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.event.DeleteRowsLogEvent;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.event.RowsLogBuffer;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.event.RowsLogEvent;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.event.TableMapLogEvent;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.event.TableMapLogEvent.ColumnInfo;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.event.UpdateRowsLogEvent;
|
||||||
|
import com.taobao.tddl.dbsync.binlog.event.WriteRowsLogEvent;
|
||||||
|
|
||||||
|
public class MysqlBinlogParsePerformanceTest {
|
||||||
|
|
||||||
|
protected static Charset charset = Charset.forName("utf-8");
|
||||||
|
|
||||||
|
public static void main(String args[]) {
|
||||||
|
DirectLogFetcher fetcher = new DirectLogFetcher();
|
||||||
|
try {
|
||||||
|
MysqlConnector connector = new MysqlConnector(new InetSocketAddress("127.0.0.1", 3306), "root", "hello");
|
||||||
|
connector.connect();
|
||||||
|
updateSettings(connector);
|
||||||
|
sendBinlogDump(connector, "mysql-bin.000006", 120L, 3);
|
||||||
|
fetcher.start(connector.getChannel());
|
||||||
|
final BlockingQueue<LogBuffer> buffer = new ArrayBlockingQueue<LogBuffer>(1024);
|
||||||
|
Thread thread = new Thread(new Runnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
try {
|
||||||
|
consumer(buffer);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
thread.start();
|
||||||
|
|
||||||
|
while (fetcher.fetch()) {
|
||||||
|
buffer.put(fetcher.duplicate());
|
||||||
|
fetcher.consume(fetcher.limit());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
fetcher.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void consumer(BlockingQueue<LogBuffer> buffer) throws IOException, InterruptedException {
|
||||||
|
LogDecoder decoder = new LogDecoder(LogEvent.UNKNOWN_EVENT, LogEvent.ENUM_END_EVENT);
|
||||||
|
LogContext context = new LogContext();
|
||||||
|
|
||||||
|
AtomicLong sum = new AtomicLong(0);
|
||||||
|
long start = System.currentTimeMillis();
|
||||||
|
long last = 0;
|
||||||
|
long end = 0;
|
||||||
|
while (true) {
|
||||||
|
LogEvent event = null;
|
||||||
|
event = decoder.decode(buffer.take(), context);
|
||||||
|
int eventType = event.getHeader().getType();
|
||||||
|
switch (eventType) {
|
||||||
|
case LogEvent.ROTATE_EVENT:
|
||||||
|
break;
|
||||||
|
case LogEvent.WRITE_ROWS_EVENT_V1:
|
||||||
|
case LogEvent.WRITE_ROWS_EVENT:
|
||||||
|
parseRowsEvent((WriteRowsLogEvent) event, sum);
|
||||||
|
break;
|
||||||
|
case LogEvent.UPDATE_ROWS_EVENT_V1:
|
||||||
|
case LogEvent.UPDATE_ROWS_EVENT:
|
||||||
|
parseRowsEvent((UpdateRowsLogEvent) event, sum);
|
||||||
|
break;
|
||||||
|
case LogEvent.DELETE_ROWS_EVENT_V1:
|
||||||
|
case LogEvent.DELETE_ROWS_EVENT:
|
||||||
|
parseRowsEvent((DeleteRowsLogEvent) event, sum);
|
||||||
|
break;
|
||||||
|
case LogEvent.XID_EVENT:
|
||||||
|
sum.incrementAndGet();
|
||||||
|
break;
|
||||||
|
case LogEvent.QUERY_EVENT:
|
||||||
|
sum.incrementAndGet();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
long current = sum.get();
|
||||||
|
if (current - last >= 100000) {
|
||||||
|
end = System.currentTimeMillis();
|
||||||
|
long tps = ((current - last) * 1000) / (end - start);
|
||||||
|
System.out.println(" total : " + sum + " , cost : " + (end - start) + " , tps : " + tps);
|
||||||
|
last = current;
|
||||||
|
start = end;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void sendBinlogDump(MysqlConnector connector, String binlogfilename, Long binlogPosition, int slaveId)
|
||||||
|
throws IOException {
|
||||||
|
BinlogDumpCommandPacket binlogDumpCmd = new BinlogDumpCommandPacket();
|
||||||
|
binlogDumpCmd.binlogFileName = binlogfilename;
|
||||||
|
binlogDumpCmd.binlogPosition = binlogPosition;
|
||||||
|
binlogDumpCmd.slaveServerId = slaveId;
|
||||||
|
byte[] cmdBody = binlogDumpCmd.toBytes();
|
||||||
|
|
||||||
|
HeaderPacket binlogDumpHeader = new HeaderPacket();
|
||||||
|
binlogDumpHeader.setPacketBodyLength(cmdBody.length);
|
||||||
|
binlogDumpHeader.setPacketSequenceNumber((byte) 0x00);
|
||||||
|
PacketManager.writePkg(connector.getChannel(), binlogDumpHeader.toBytes(), cmdBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void updateSettings(MysqlConnector connector) throws IOException {
|
||||||
|
update("set @master_binlog_checksum= '@@global.binlog_checksum'", connector);
|
||||||
|
update("SET @mariadb_slave_capability='" + LogEvent.MARIA_SLAVE_CAPABILITY_MINE + "'", connector);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void update(String cmd, MysqlConnector connector) throws IOException {
|
||||||
|
MysqlUpdateExecutor exector = new MysqlUpdateExecutor(connector);
|
||||||
|
exector.update(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void parseRowsEvent(RowsLogEvent event, AtomicLong sum) {
|
||||||
|
try {
|
||||||
|
RowsLogBuffer buffer = event.getRowsBuf(charset.name());
|
||||||
|
BitSet columns = event.getColumns();
|
||||||
|
BitSet changeColumns = event.getChangeColumns();
|
||||||
|
while (buffer.nextOneRow(columns)) {
|
||||||
|
int type = event.getHeader().getType();
|
||||||
|
if (LogEvent.WRITE_ROWS_EVENT_V1 == type || LogEvent.WRITE_ROWS_EVENT == type) {
|
||||||
|
parseOneRow(event, buffer, columns, true);
|
||||||
|
} else if (LogEvent.DELETE_ROWS_EVENT_V1 == type || LogEvent.DELETE_ROWS_EVENT == type) {
|
||||||
|
parseOneRow(event, buffer, columns, false);
|
||||||
|
} else {
|
||||||
|
parseOneRow(event, buffer, columns, false);
|
||||||
|
if (!buffer.nextOneRow(changeColumns)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
parseOneRow(event, buffer, changeColumns, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
sum.incrementAndGet();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("parse row data failed.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void parseOneRow(RowsLogEvent event, RowsLogBuffer buffer, BitSet cols, boolean isAfter)
|
||||||
|
throws UnsupportedEncodingException {
|
||||||
|
TableMapLogEvent map = event.getTable();
|
||||||
|
if (map == null) {
|
||||||
|
throw new RuntimeException("not found TableMap with tid=" + event.getTableId());
|
||||||
|
}
|
||||||
|
|
||||||
|
final int columnCnt = map.getColumnCnt();
|
||||||
|
final ColumnInfo[] columnInfo = map.getColumnInfo();
|
||||||
|
for (int i = 0; i < columnCnt; i++) {
|
||||||
|
if (!cols.get(i)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ColumnInfo info = columnInfo[i];
|
||||||
|
buffer.nextValue(info.type, info.meta);
|
||||||
|
if (buffer.isNull()) {
|
||||||
|
} else {
|
||||||
|
buffer.getValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,6 @@ import java.util.Date;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import com.alibaba.otter.canal.protocol.CanalEntry;
|
|
||||||
import com.alibaba.otter.canal.protocol.CanalEntry.EntryType;
|
import com.alibaba.otter.canal.protocol.CanalEntry.EntryType;
|
||||||
import com.alibaba.otter.canal.protocol.position.Position;
|
import com.alibaba.otter.canal.protocol.position.Position;
|
||||||
import com.alibaba.otter.canal.store.CanalEventStore;
|
import com.alibaba.otter.canal.store.CanalEventStore;
|
||||||
@@ -18,7 +17,7 @@ import com.alibaba.otter.canal.store.model.Events;
|
|||||||
public class DummyEventStore implements CanalEventStore<Event> {
|
public class DummyEventStore implements CanalEventStore<Event> {
|
||||||
|
|
||||||
private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
||||||
private static final String messgae = "{0} [{1}:{2}:{3}] {4} {5}.{6}";
|
private static final String messgae = "{0} [{1}:{2}:{3}]";
|
||||||
|
|
||||||
public void ack(Position position) throws CanalStoreException {
|
public void ack(Position position) throws CanalStoreException {
|
||||||
|
|
||||||
@@ -82,46 +81,40 @@ public class DummyEventStore implements CanalEventStore<Event> {
|
|||||||
|
|
||||||
public void put(List<Event> datas) throws InterruptedException, CanalStoreException {
|
public void put(List<Event> datas) throws InterruptedException, CanalStoreException {
|
||||||
for (Event data : datas) {
|
for (Event data : datas) {
|
||||||
CanalEntry.Header header = data.getEntry().getHeader();
|
Date date = new Date(data.getExecuteTime());
|
||||||
Date date = new Date(header.getExecuteTime());
|
|
||||||
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
|
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
|
||||||
if (data.getEntry().getEntryType() == EntryType.TRANSACTIONBEGIN
|
if (data.getEntryType() == EntryType.TRANSACTIONBEGIN || data.getEntryType() == EntryType.TRANSACTIONEND) {
|
||||||
|| data.getEntry().getEntryType() == EntryType.TRANSACTIONEND) {
|
|
||||||
// System.out.println(MessageFormat.format(messgae, new Object[]
|
// System.out.println(MessageFormat.format(messgae, new Object[]
|
||||||
// { Thread.currentThread().getName(),
|
// { Thread.currentThread().getName(),
|
||||||
// header.getLogfilename(), header.getLogfileoffset(),
|
// header.getLogfilename(), header.getLogfileoffset(),
|
||||||
// format.format(date),
|
// format.format(date),
|
||||||
// data.getEntry().getEntryType(), "" }));
|
// data.getEntry().getEntryType(), "" }));
|
||||||
System.out.println(data.getEntry().getEntryType());
|
System.out.println(data.getEntryType());
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
System.out.println(MessageFormat.format(messgae,
|
System.out.println(MessageFormat.format(messgae,
|
||||||
new Object[] { Thread.currentThread().getName(), header.getLogfileName(),
|
new Object[] { Thread.currentThread().getName(), data.getJournalName(),
|
||||||
String.valueOf(header.getLogfileOffset()), format.format(date), header.getEventType(),
|
String.valueOf(data.getPosition()), format.format(date) }));
|
||||||
header.getSchemaName(), header.getTableName() }));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean put(List<Event> datas, long timeout, TimeUnit unit) throws InterruptedException, CanalStoreException {
|
public boolean put(List<Event> datas, long timeout, TimeUnit unit) throws InterruptedException, CanalStoreException {
|
||||||
for (Event data : datas) {
|
for (Event data : datas) {
|
||||||
CanalEntry.Header header = data.getEntry().getHeader();
|
Date date = new Date(data.getExecuteTime());
|
||||||
Date date = new Date(header.getExecuteTime());
|
|
||||||
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
|
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
|
||||||
if (data.getEntry().getEntryType() == EntryType.TRANSACTIONBEGIN
|
if (data.getEntryType() == EntryType.TRANSACTIONBEGIN || data.getEntryType() == EntryType.TRANSACTIONEND) {
|
||||||
|| data.getEntry().getEntryType() == EntryType.TRANSACTIONEND) {
|
|
||||||
// System.out.println(MessageFormat.format(messgae, new Object[]
|
// System.out.println(MessageFormat.format(messgae, new Object[]
|
||||||
// { Thread.currentThread().getName(),
|
// { Thread.currentThread().getName(),
|
||||||
// header.getLogfilename(), header.getLogfileoffset(),
|
// header.getLogfilename(), header.getLogfileoffset(),
|
||||||
// format.format(date),
|
// format.format(date),
|
||||||
// data.getEntry().getEntryType(), "" }));
|
// data.getEntry().getEntryType(), "" }));
|
||||||
System.out.println(data.getEntry().getEntryType());
|
System.out.println(data.getEntryType());
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
System.out.println(MessageFormat.format(messgae,
|
System.out.println(MessageFormat.format(messgae,
|
||||||
new Object[] { Thread.currentThread().getName(), header.getLogfileName(),
|
new Object[] { Thread.currentThread().getName(), data.getJournalName(),
|
||||||
String.valueOf(header.getLogfileOffset()), format.format(date), header.getEventType(),
|
String.valueOf(data.getPosition()), format.format(date) }));
|
||||||
header.getSchemaName(), header.getTableName() }));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -131,23 +124,20 @@ public class DummyEventStore implements CanalEventStore<Event> {
|
|||||||
System.out.println("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
|
System.out.println("\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
|
||||||
for (Event data : datas) {
|
for (Event data : datas) {
|
||||||
|
|
||||||
CanalEntry.Header header = data.getEntry().getHeader();
|
Date date = new Date(data.getExecuteTime());
|
||||||
Date date = new Date(header.getExecuteTime());
|
|
||||||
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
|
SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
|
||||||
if (data.getEntry().getEntryType() == EntryType.TRANSACTIONBEGIN
|
if (data.getEntryType() == EntryType.TRANSACTIONBEGIN || data.getEntryType() == EntryType.TRANSACTIONEND) {
|
||||||
|| data.getEntry().getEntryType() == EntryType.TRANSACTIONEND) {
|
|
||||||
// System.out.println(MessageFormat.format(messgae, new Object[]
|
// System.out.println(MessageFormat.format(messgae, new Object[]
|
||||||
// { Thread.currentThread().getName(),
|
// { Thread.currentThread().getName(),
|
||||||
// header.getLogfilename(), header.getLogfileoffset(),
|
// header.getLogfilename(), header.getLogfileoffset(),
|
||||||
// format.format(date),
|
// format.format(date),
|
||||||
// data.getEntry().getEntryType(), "" }));
|
// data.getEntry().getEntryType(), "" }));
|
||||||
System.out.println(data.getEntry().getEntryType());
|
System.out.println(data.getEntryType());
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
System.out.println(MessageFormat.format(messgae,
|
System.out.println(MessageFormat.format(messgae,
|
||||||
new Object[] { Thread.currentThread().getName(), header.getLogfileName(),
|
new Object[] { Thread.currentThread().getName(), data.getJournalName(),
|
||||||
String.valueOf(header.getLogfileOffset()), format.format(date), header.getEventType(),
|
String.valueOf(data.getPosition()), format.format(date) }));
|
||||||
header.getSchemaName(), header.getTableName() }));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user