├── .gitignore
├── LICENSE
├── README.md
├── pom.xml
└── src
├── main
├── assembly
│ ├── dev.xml
│ └── release.xml
├── bin
│ ├── startup.bat
│ ├── startup.sh
│ └── stop.sh
├── java
│ └── com
│ │ └── totoro
│ │ └── canal
│ │ └── es
│ │ ├── TotoroBootStrap.java
│ │ ├── TotoroLauncher.java
│ │ ├── channel
│ │ └── TotoroChannel.java
│ │ ├── common
│ │ ├── GlobalTask.java
│ │ ├── RecycleAble.java
│ │ ├── RollBackMonitorFactory.java
│ │ ├── TotoroException.java
│ │ └── TotoroObjectPool.java
│ │ ├── consum
│ │ └── es
│ │ │ ├── Consumer.java
│ │ │ ├── ConsumerTask.java
│ │ │ ├── ElasticSearchConsumer.java
│ │ │ ├── ElasticsearchMetadata.java
│ │ │ ├── ElasticsearchService.java
│ │ │ ├── EsColumnHashMap.java
│ │ │ ├── EsConf.java
│ │ │ ├── EsEntryArrayList.java
│ │ │ ├── EsRowDataArrayList.java
│ │ │ └── impl
│ │ │ └── ElasticsearchServiceImpl.java
│ │ ├── select
│ │ ├── exception
│ │ │ └── SelectException.java
│ │ └── selector
│ │ │ ├── CanalConf.java
│ │ │ ├── SelectorTask.java
│ │ │ ├── TotoroSelector.java
│ │ │ └── canal
│ │ │ └── CanalEmbedSelector.java
│ │ ├── transform
│ │ ├── EsAdapter.java
│ │ ├── MessageFilter.java
│ │ ├── MessageFilterChain.java
│ │ ├── SimpleEsAdapter.java
│ │ ├── SimpleMessageFilter.java
│ │ ├── TableFilter.java
│ │ ├── TotoroTransForm.java
│ │ ├── TransForm.java
│ │ └── TransFormTask.java
│ │ └── util
│ │ └── IPAddressUtil.java
└── resources
│ ├── canal-es.properties
│ ├── logback.xml
│ └── totoro-logo.logo
└── test
└── java
└── test
└── syn
├── Lock.java
├── LockTest.java
└── TestFuture.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled class file
2 | *.class
3 |
4 | # Log file
5 | *.log
6 |
7 | # BlueJ files
8 | *.ctxt
9 |
10 | # Mobile Tools for Java (J2ME)
11 | .mtj.tmp/
12 |
13 | # Package Files #
14 | *.jar
15 | *.war
16 | *.ear
17 | *.zip
18 | *.tar.gz
19 | *.rar
20 |
21 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
22 | hs_err_pid*
23 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 
2 | =========================
3 | 一个基于阿里巴巴[Canal](https://github.com/alibaba/canal),实时同步mysql数据到Elasticsearch的工具。轻量,易于配置,部署简单,支持数据回滚。
4 | 使用 Totoro 可以帮助你轻松的将mysql的数据实时同步到Elasticsearch。Totoro是基于阿里巴巴的canal,是数据库级别的
5 | 监听,对原有项目没有任何侵入,所以你无需更改项目中的任何代码就可以实现实时的数据同步。
6 |
7 |
8 | []()
9 |
10 |
11 | [Elasticsearch](https://www.elastic.co/cn/)是一个分布式搜索服务,提供Restful API,底层基于Lucene,
12 | 采用多shard的方式保证数据安全,并且提供自动resharding的功能,github等大型的站点也采用Elasticsearch作为其搜索服务。
13 |
14 | 对于Elasticsearch,如果要在项目中使用,第一个要解决的问题就是,如何将数据库中的数据同步到Elasticsearch,下面常见的几种中方案
15 |
16 | * 修改代码,修改插入数据库的代码,同时插入Elasticsearch
17 | * 修改代码,修改插入数据库的代码,同时放入消息队列,在消息队列的另一端进行插入Elasticsearch
18 | * 使用 Elasticsearch技术栈的[Logstash](https://www.elastic.co/cn/products/logstash),编写sql语句定时执行搜集数据,并插入到Elasticsearch
19 |
20 | 在上诉方案中,一三都有明显缺陷,第二种方案是目前采用最多的,但还是会对原有项目代码有侵入,并且要引入消息队列。
21 | 如果你不想引入消息队列并且想获得一个开箱即用的同步中间件,那么Totoro会令你非常满意
22 | 。
23 |
24 | Totoro的方案是,基于阿里巴巴开源数据库中间件canal,监听mysql数据库,并且在mysql数据库数据发生变化的时候
25 | 发送消息给Totoro,Totoro会将数据同步到Elasticsearch,如果消费出错支持回滚,保证数据被正确消费。
26 |
27 | 
28 |
29 |
30 | 工作原理
31 | ======================
32 |
33 | totoro主要分为4个模块: select、Transformation、consumer、channel
34 | * select 负责从canal中拉取数据
35 | * Transformation 负责将select生产的数据,进行过滤、处理、转换
36 | * consumer 负责消费数据,在这里就是将数据同步到elasticsearch
37 | * channel 是以上3个模块链接者,也是数据在totoro中的容器。select将拉取的数据放入channel,Transformation监听到select放入的数据,进行处理,处理完再放回channel,等待consumer去消费
38 | 
39 | select、Transformation、consumer分别是3个任务,并行执行。为保证数据消费顺序与ack(每次消费一条数据要向canal进行ack)顺序,
40 | 其中select task与consumer task 都分别只有一条线程。而Transformation task具有多条线程。
41 |
42 |
43 | QuickStart
44 | ======================
45 | 1.安装Canal
46 |
47 | 有关于canal的安装,请参见canal的文档,里面有很详细说明 [Canal QuickStart](https://github.com/alibaba/canal/wiki/QuickStart)
48 |
49 |
50 | 2.安装Totoro
51 | ```
52 | git clone git@github.com:zhongchengxcr/canal-elasticsearch.git
53 | cd canal-elasticsearch
54 | mvn clean compile package -P dev -Dmaven.test.skip=true
55 |
56 | ```
57 | 编译完成后,进入/target/totoro目录,可以看到如下结构:
58 | ```$xslt
59 | -rw-r--r-- 1 zhongcheng staff 2214 12 21 17:08 README.md
60 | drwxr-xr-x 5 zhongcheng staff 160 12 21 17:22 bin
61 | drwxr-xr-x 5 zhongcheng staff 160 12 21 17:08 conf
62 | drwxr-xr-x 73 zhongcheng staff 2336 12 21 17:08 lib
63 | drwxr-xr-x 4 zhongcheng staff 128 12 21 17:08 logs
64 | ```
65 |
66 | 3.配置修改
67 | ```$xslt
68 | vi conf/canal-es.properties
69 |
70 | ###一下配置需要根据自己的业务进行修改
71 |
72 | # ----------------------- canal 相关配置 -----------------------------
73 | #canal的实例名字
74 | totoro.canal.destination=totoro
75 | #cananl 服务端的模式,单机:single ,集群:cluster/
76 | totoro.canal.mode=sign
77 | #canal 地址,包括端口号
78 | totoro.canal.address=127.0.0.1:11111
79 | #过滤表达式
80 | totoro.canal.filter.patten=
81 | #如果canal模式是集群的话,则需要填写zk地址
82 | totoro.canal.zk.address=
83 | totoro.canal.username=
84 | totoro.canal.password=
85 |
86 | #此项必须配置,不配置会导致 totoro 启动不了, totoro 只会处理在此配置的表
87 | #没有在此配置的表,totoro 将会忽略,不会进行同步,格式 "database.table.id" 多个使用 ","分割
88 | #database代表数据库,table 代表 数据库中的表,id代表 table中的 id
89 | #totoro 会默认将 database 作为 es中的index,table作为es中的type ,使用db中的id作为es的id
90 | totoro.canal.table.accept=demo.cc.id
91 |
92 |
93 | # ----------------------- elasticsearch 相关配置 -----------------------------
94 | totoro.es.address=127.0.0.1:9300
95 | totoro.es.cluster.name=my-elasticsearch
96 | totoro.es.username=
97 | totoro.es.password=
98 | # ----------------------- totoro 相关配置 ----------------------------
99 | #处理信息转换的线程数量 默认 3个 , 不要配置太大,2-4 之间吧,取决于业务情况,太大并不会增加性能,反而会增加上下文切换的开销
100 | totoro.cannal.trans.thread.nums=3
101 |
102 | ```
103 | 4.准备启动
104 |
105 | ```$xslt
106 | cd ../bin
107 | ./startup.sh
108 |
109 |
110 | _____ _
111 | |_ _|___ | |_ ___ _ __ ___
112 | | | / _ \ | __|/ _ \ | '__|/ _ \
113 | | || (_) || |_| (_) || | | (_) |
114 | |_| \___/ \__|\___/ |_| \___/
115 | [Totoro 1.0-SNAPSHOT,Build 2017/12/20,Author:zhongcheng_m@yeah.net]
116 |
117 | cd to /Users/zhongcheng/IdeaProjects/canal-elasticsearch/target/totoro/bin for workaround relative path
118 | LOG CONFIGURATION : /Users/zhongcheng/IdeaProjects/canal-elasticsearch/target/totoro/bin/../conf/logback.xml
119 | sync conf : /Users/zhongcheng/IdeaProjects/canal-elasticsearch/target/totoro/bin/../conf/canal-es.properties
120 | cd to /Users/zhongcheng/IdeaProjects/canal-elasticsearch/target/totoro/bin for continue
121 |
122 | ```
123 |
124 | 5.查看日志
125 |
126 | ```$xslt
127 | cd ../logs
128 | tail -100f totoro.log
129 |
130 | [2017-12-21 17:58:27.699] [INFO] [main] [c.t.c.e.s.s.canal.CanalEmbedSelector] --- TotoroSelector init start , conf :CanalConf{mode=SIGN, destination='totoro', filterPatten='', address='127.0.0.1:11111', zkAddress='', userName='', accept='demo.cc.id'}
131 | [2017-12-21 17:58:27.719] [INFO] [main] [c.t.c.e.s.s.canal.CanalEmbedSelector] --- TotoroSelector init complete .......
132 | [2017-12-21 17:58:27.737] [INFO] [main] [c.t.c.e.select.selector.SelectorTask] --- Selector task init .......
133 | [2017-12-21 17:58:27.737] [INFO] [main] [c.t.c.e.select.selector.SelectorTask] --- Selector task complete .......
134 | [2017-12-21 17:58:27.748] [INFO] [main] [c.t.c.e.transform.MessageFilterChain] --- TableFilter has benn registered to message filter chain
135 | [2017-12-21 17:58:27.748] [INFO] [main] [c.t.c.e.transform.MessageFilterChain] --- SimpleMessageFilter has benn registered to message filter chain
136 | [2017-12-21 17:58:27.749] [INFO] [main] [c.t.c.es.transform.SimpleEsAdapter] --- Add accept :demo.cc.id
137 | [2017-12-21 17:58:27.750] [INFO] [main] [c.t.canal.es.transform.TransFormTask] --- TransFormTask init start .......
138 | [2017-12-21 17:58:27.758] [INFO] [main] [c.t.canal.es.transform.TransFormTask] --- TransFormTask init complete .......
139 | [2017-12-21 17:58:30.571] [INFO] [main] [c.t.c.e.c.e.i.ElasticsearchServiceImpl] --- Complete the connection to elasticsearch
140 | [2017-12-21 17:58:30.572] [INFO] [main] [c.t.canal.es.consum.es.ConsumerTask] --- Consumer task init start .......
141 | [2017-12-21 17:58:30.573] [INFO] [main] [c.t.canal.es.consum.es.ConsumerTask] --- Consumer task init complete.......
142 | [2017-12-21 17:58:30.573] [INFO] [main] [com.totoro.canal.es.TotoroLauncher] --- Totoro init complete .......
143 | [2017-12-21 17:58:30.574] [INFO] [taskName = TransFormTask] [c.t.canal.es.transform.TransFormTask] --- TransFormTask start .......
144 | [2017-12-21 17:58:30.574] [INFO] [taskName = ConsumerTask] [c.t.canal.es.consum.es.ConsumerTask] --- ConsumerTask start .......
145 | [2017-12-21 17:58:30.656] [INFO] [taskName = SelectorTask] [c.t.c.e.select.selector.SelectorTask] --- Selector task start .......
146 |
147 | ```
148 |
149 | 6.关闭
150 | ```$xslt
151 | cd ../bin
152 | ./stop.sh
153 | ```
154 |
155 | 存在的问题
156 | ======================
157 | * 内存抖动与GC时间过高(已解决,使用对象池化)
158 |
159 | * Elasticsearch 客户端 transport 的 cpu与内存消耗比较多,经测试 发现 transport 启动了 20+ 线程
160 |
161 | * 数据转换的灵活性不够
162 |
163 | * 单一节点,不支持多节点部署
164 |
165 |
166 | 计划
167 | ======================
168 | * 0.1.0
169 | #####将selector部分抽出单独的模块,分别支持canal,各种mq,以及自定义的selector
170 |
171 | 欢迎有想法的朋友一起参与,讨论
172 | QQ群:688734361
173 | 
174 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
25 | *
26 | *
27 | * Copyright: Copyright (c) 2017/11/19 下午6:00 28 | *
29 | * Company: xxx 30 | *
31 | *
32 | * @author zhongcheng_m@yeah.net
33 | * @version 1.0.0
34 | */
35 | public class TotoroBootStrap {
36 |
37 |
38 | private static final Logger logger = LoggerFactory.getLogger(TotoroLauncher.class);
39 |
40 | private volatile boolean running = false;
41 |
42 | private TotoroChannel channel;
43 |
44 | private TotoroSelector totoroSelector;
45 |
46 | private ElasticsearchService elasticsearchService;
47 |
48 | private Map
19 | * Copyright: Copyright (c)
20 | *
21 | * Company: xx
22 | *
23 | *
24 | * @author zhongcheng_m@yeah.net
25 | * @version 1.0.0
26 | */
27 | public class TotoroLauncher {
28 |
29 | private static final Logger logger = LoggerFactory.getLogger(TotoroLauncher.class);
30 |
31 | private static final String CLASSPATH_URL_PREFIX = "classpath:";
32 |
33 |
34 | static {
35 | System.out.println("");
36 | System.out.println(" _____ _");
37 | System.out.println("|_ _|___ | |_ ___ _ __ ___ ");
38 | System.out.println(" | | / _ \\ | __|/ _ \\ | '__|/ _ \\ ");
39 | System.out.println(" | || (_) || |_| (_) || | | (_) |");
40 | System.out.println(" |_| \\___/ \\__|\\___/ |_| \\___/");
41 | System.out.println("[Totoro 1.0-SNAPSHOT,Build 2017/12/20,Author:zhongcheng_m@yeah.net]");
42 | System.out.println("");
43 | }
44 |
45 | public static void main(String[] args) throws InterruptedException, IOException, ExecutionException {
46 |
47 | setGlobalUncaughtExceptionHandler();
48 |
49 | String conf = System.getProperty("canal-es.properties", "classpath:canal-es.properties");
50 |
51 | Properties properties = getProperties(conf);
52 |
53 | TotoroBootStrap canalScheduler = new TotoroBootStrap(properties);
54 | canalScheduler.start();
55 |
56 | Runtime.getRuntime().addShutdownHook(new Thread(() -> {
57 | try {
58 | logger.info("## stop the totoro server");
59 | canalScheduler.stop();
60 | } catch (Throwable e) {
61 | logger.warn("##something goes wrong when stopping totoro Server:", e);
62 | } finally {
63 | logger.info("## totoro server is down.");
64 | }
65 | }));
66 |
67 | }
68 |
69 | private static Properties getProperties(String conf) throws IOException {
70 | Properties properties = new Properties();
71 |
72 | if (conf.startsWith(CLASSPATH_URL_PREFIX)) {
73 | conf = StringUtils.substringAfter(conf, CLASSPATH_URL_PREFIX);
74 | properties.load(TotoroLauncher.class.getClassLoader().getResourceAsStream(conf));
75 | } else {
76 | properties.load(new FileInputStream(conf));
77 | }
78 | return properties;
79 | }
80 |
81 |
82 | private static void setGlobalUncaughtExceptionHandler() {
83 | Thread.setDefaultUncaughtExceptionHandler((t, e) -> logger.error("UnCaughtException", e));
84 | }
85 |
86 |
87 | }
88 |
--------------------------------------------------------------------------------
/src/main/java/com/totoro/canal/es/channel/TotoroChannel.java:
--------------------------------------------------------------------------------
1 | package com.totoro.canal.es.channel;
2 |
3 | import com.alibaba.otter.canal.common.utils.BooleanMutex;
4 | import com.alibaba.otter.canal.protocol.Message;
5 | import com.totoro.canal.es.common.RollBackMonitorFactory;
6 | import com.totoro.canal.es.consum.es.ElasticsearchMetadata;
7 | import com.totoro.canal.es.select.selector.TotoroSelector;
8 | import org.slf4j.Logger;
9 | import org.slf4j.LoggerFactory;
10 |
11 | import java.util.concurrent.Future;
12 | import java.util.concurrent.LinkedBlockingQueue;
13 |
14 | /**
15 | *
16 | * Copyright: Copyright (c)
17 | *
18 | * Company: xx
19 | *
20 | *
21 | * @author zhongcheng_m@yeah.net
22 | * @version 1.0.0
23 | */
24 | public class TotoroChannel {
25 |
26 | private BooleanMutex rollBack = RollBackMonitorFactory.getBooleanMutex();
27 |
28 | private Logger logger = LoggerFactory.getLogger(TotoroChannel.class);
29 |
30 | private LinkedBlockingQueue
6 | *
7 | * Copyright: Copyright (c) 2017/12/22 下午12:30
8 | *
9 | * Company: xxx
10 | *
11 | *
12 | * @author zhongcheng_m@yeah.net
13 | * @version 1.0.0
14 | */
15 | public interface RecycleAble {
16 |
17 | boolean recycle();
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/totoro/canal/es/common/RollBackMonitorFactory.java:
--------------------------------------------------------------------------------
1 | package com.totoro.canal.es.common;
2 |
3 | import com.alibaba.otter.canal.common.utils.BooleanMutex;
4 |
5 | /**
6 | * 说明 .
8 | *
9 | * Copyright: Copyright (c) 2017/12/05 上午10:04
10 | *
11 | * Company: xxx
12 | *
13 | *
14 | * @author zhongcheng_m@yeah.net
15 | * @version 1.0.0
16 | */
17 | public class RollBackMonitorFactory {
18 |
19 | private static BooleanMutex booleanMutex = new BooleanMutex(false);
20 |
21 | public static BooleanMutex getBooleanMutex() {
22 | return booleanMutex;
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/totoro/canal/es/common/TotoroException.java:
--------------------------------------------------------------------------------
1 | package com.totoro.canal.es.common;
2 |
3 | /**
4 | * 说明 .
6 | *
7 | * Copyright: Copyright (c) 2017/12/01 下午12:53
8 | *
9 | * Company: xxx
10 | *
11 | *
12 | * @author zhongcheng_m@yeah.net
13 | * @version 1.0.0
14 | */
15 | public class TotoroException extends RuntimeException {
16 |
17 | private String errMsg;
18 |
19 | public TotoroException(String errMsg) {
20 | super(errMsg);
21 | this.errMsg = errMsg;
22 | }
23 |
24 | public String getErrMsg() {
25 | return errMsg;
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/com/totoro/canal/es/common/TotoroObjectPool.java:
--------------------------------------------------------------------------------
1 | package com.totoro.canal.es.common;
2 |
3 | import com.totoro.canal.es.consum.es.ElasticsearchMetadata;
4 | import com.totoro.canal.es.consum.es.EsColumnHashMap;
5 | import com.totoro.canal.es.consum.es.EsEntryArrayList;
6 | import com.totoro.canal.es.consum.es.EsRowDataArrayList;
7 | import com.totoro.canal.es.transform.TotoroTransForm;
8 | import com.totoro.canal.es.transform.TransForm;
9 | import io.netty.util.Recycler;
10 |
11 | /**
12 | * 说明 .
14 | *
15 | * Copyright: Copyright (c) 2017/12/22 下午12:49
16 | *
17 | * Company: xxx
18 | *
19 | *
20 | * @author zhongcheng_m@yeah.net
21 | * @version 1.0.0
22 | */
23 | public class TotoroObjectPool {
24 |
25 | public static ElasticsearchMetadata esMetadata() {
26 | return ELASTICSEARCH_METADATA_RECYCLER.get();
27 | }
28 |
29 | public static ElasticsearchMetadata.EsEntry esEntry() {
30 | return ELASTICSEARCH_ENTRY_RECYCLER.get();
31 | }
32 |
33 | public static ElasticsearchMetadata.EsRowData esRowData() {
34 | return ELASTICSEARCH_ROWDATA_RECYCLER.get();
35 | }
36 |
37 | static {
38 | System.setProperty("io.netty.recycler.ratio", "1");
39 | }
40 |
41 |
42 | public static void main(String[] args) {
43 |
44 |
45 | System.out.printf(System.getProperty("io.netty.recycler.ratio"));
46 |
47 | ElasticsearchMetadata.EsRowData esRowData = TotoroObjectPool.esRowData();
48 | ElasticsearchMetadata.EsRowData esRowData1 = TotoroObjectPool.esRowData();
49 | ElasticsearchMetadata.EsRowData esRowData6 = TotoroObjectPool.esRowData();
50 | ElasticsearchMetadata.EsRowData esRowData5 = TotoroObjectPool.esRowData();
51 |
52 |
53 | System.out.println(esRowData);
54 | System.out.println(esRowData1);
55 | System.out.println(esRowData6);
56 | System.out.println(esRowData5);
57 |
58 | esRowData.recycle();
59 | esRowData1.recycle();
60 | esRowData6.recycle();
61 | esRowData5.recycle();
62 |
63 | ElasticsearchMetadata.EsRowData esRowData7 = TotoroObjectPool.esRowData();
64 | ElasticsearchMetadata.EsRowData esRowData8 = TotoroObjectPool.esRowData();
65 | ElasticsearchMetadata.EsRowData esRowData9 = TotoroObjectPool.esRowData();
66 | ElasticsearchMetadata.EsRowData esRowData0 = TotoroObjectPool.esRowData();
67 |
68 | ElasticsearchMetadata.EsRowData esRowData12 = TotoroObjectPool.esRowData();
69 | ElasticsearchMetadata.EsRowData esRowData121 = TotoroObjectPool.esRowData();
70 |
71 |
72 | System.out.println(esRowData7);
73 | System.out.println(esRowData8);
74 | esRowData7.recycle();
75 | esRowData8.recycle();
76 |
77 |
78 | System.out.println(esRowData9);
79 | System.out.println(esRowData0);
80 | esRowData9.recycle();
81 | esRowData0.recycle();
82 |
83 |
84 | // new Thread(()->{
85 | //
86 | // ElasticsearchMetadata.EsRowData esRowData3 = TotoroObjectPool.esRowData();
87 | // ElasticsearchMetadata.EsRowData esRowData4 = TotoroObjectPool.esRowData();
88 | // System.out.println(esRowData3);
89 | // System.out.println(esRowData4);
90 | //
91 | // esRowData3.recycle();
92 | // esRowData4.recycle();
93 | //
94 | // try {
95 | // Thread.sleep(1000L);
96 | // } catch (InterruptedException e) {
97 | // e.printStackTrace();
98 | // }
99 | //
100 | // }).start();
101 | //
102 | // new Thread(()->{
103 | //
104 | // ElasticsearchMetadata.EsRowData esRowData3 = TotoroObjectPool.esRowData();
105 | // ElasticsearchMetadata.EsRowData esRowData4 = TotoroObjectPool.esRowData();
106 | // System.out.println(esRowData3);
107 | // System.out.println(esRowData4);
108 | //
109 | // esRowData3.recycle();
110 | // esRowData4.recycle();
111 | //
112 | // try {
113 | // Thread.sleep(1000L);
114 | // } catch (InterruptedException e) {
115 | // e.printStackTrace();
116 | // }
117 | // }).start();
118 | // new Thread(()->{
119 | //
120 | // ElasticsearchMetadata.EsRowData esRowData3 = TotoroObjectPool.esRowData();
121 | // ElasticsearchMetadata.EsRowData esRowData4 = TotoroObjectPool.esRowData();
122 | // System.out.println(esRowData3);
123 | // System.out.println(esRowData4);
124 | // esRowData3.recycle();
125 | // esRowData4.recycle();
126 | //
127 | // try {
128 | // Thread.sleep(1000L);
129 | // } catch (InterruptedException e) {
130 | // e.printStackTrace();
131 | // }
132 | //
133 | // }).start();
134 | // new Thread(()->{
135 | //
136 | // ElasticsearchMetadata.EsRowData esRowData3 = TotoroObjectPool.esRowData();
137 | // ElasticsearchMetadata.EsRowData esRowData4 = TotoroObjectPool.esRowData();
138 | // System.out.println(esRowData3);
139 | // System.out.println(esRowData4);
140 | // esRowData3.recycle();
141 | // esRowData4.recycle();
142 | //
143 | // try {
144 | // Thread.sleep(1000L);
145 | // } catch (InterruptedException e) {
146 | // e.printStackTrace();
147 | // }
148 | //
149 | // }).start();
150 | // new Thread(()->{
151 | //
152 | // ElasticsearchMetadata.EsRowData esRowData3 = TotoroObjectPool.esRowData();
153 | // ElasticsearchMetadata.EsRowData esRowData4 = TotoroObjectPool.esRowData();
154 | // System.out.println(esRowData3);
155 | // System.out.println(esRowData4);
156 | // esRowData3.recycle();
157 | // esRowData4.recycle();
158 | //
159 | // try {
160 | // Thread.sleep(1000L);
161 | // } catch (InterruptedException e) {
162 | // e.printStackTrace();
163 | // }
164 | //
165 | // }).start();
166 |
167 | }
168 |
169 | public static EsColumnHashMap esColumnHashMap() {
170 | return ELASTICSEARCH_COLUMNMAP_RECYCLER.get();
171 | }
172 |
173 | public static EsEntryArrayList esEntryArrayList() {
174 | return ELASTICSEARCH_ENTRY_LIST__RECYCLER.get();
175 | }
176 |
177 | public static EsRowDataArrayList esRowDataArrayList() {
178 | return ELASTICSEARCH_ROW_DATA__RECYCLER.get();
179 | }
180 |
181 | public static TransForm transForm() {
182 | return TRANS_FORM_RECYCLER.get();
183 | }
184 |
185 |
186 | private static final Recycler
15 | *
16 | * Copyright: Copyright (c) 2017/12/01 下午12:49
17 | *
18 | * Company: xxx
19 | *
20 | *
21 | * @author zhongcheng_m@yeah.net
22 | * @version 1.0.0
23 | */
24 | public class ConsumerTask extends GlobalTask {
25 |
26 | private final Set
11 | *
12 | * Copyright: Copyright (c) 2017/11/19 下午6:51
13 | *
14 | * Company: xxx
15 | *
16 | *
17 | * @author zhongcheng_m@yeah.net
18 | * @version 1.0.0
19 | */
20 | public class ElasticSearchConsumer implements Consumer {
21 |
22 |
23 | private ElasticsearchService elasticsearchService;
24 |
25 | public ElasticSearchConsumer(ElasticsearchService elasticsearchService) {
26 | this.elasticsearchService = elasticsearchService;
27 | }
28 |
29 | @Override
30 | public void consume(ElasticsearchMetadata metadata) {
31 |
32 | List
13 | *
14 | * Copyright: Copyright (c) 2017/11/19 下午6:32
15 | *
16 | * Company: xxx
17 | *
18 | *
19 | * @author zhongcheng_m@yeah.net
20 | * @version 1.0.0
21 | */
22 | public class ElasticsearchMetadata implements RecycleAble {
23 |
24 | public final static int INSERT = 1;
25 |
26 | public final static int DELETE = 2;
27 |
28 | public final static int UPDATE = 3;
29 |
30 | private Long batchId;
31 |
32 | private EsEntryArrayList esEntries;
33 |
34 | private final Recycler.Handle
11 | *
12 | * Copyright: Copyright (c) 2017/12/22 下午1:00
13 | *
14 | * Company: xxx
15 | *
16 | *
17 | * @author zhongcheng_m@yeah.net
18 | * @version 1.0.0
19 | */
20 | public class EsColumnHashMap extends HashMap
12 | *
13 | * Copyright: Copyright (c) 2017/12/19 下午4:39
14 | *
15 | * Company: xxx
16 | *
17 | *
18 | * @author zhongcheng_m@yeah.net
19 | * @version 1.0.0
20 | */
21 | public class EsConf implements Serializable {
22 |
23 |
24 | private String address;
25 |
26 | private String username;
27 |
28 | private String password;
29 |
30 | private String clusterName;
31 |
32 |
33 | public String getAddress() {
34 | return address;
35 | }
36 |
37 | public EsConf setAddress(String address) {
38 | this.address = address;
39 | return this;
40 | }
41 |
42 | public String getUsername() {
43 | return username;
44 | }
45 |
46 | public EsConf setUsername(String username) {
47 | this.username = username;
48 | return this;
49 | }
50 |
51 | public String getPassword() {
52 | return password;
53 | }
54 |
55 | public EsConf setPassword(String password) {
56 | this.password = password;
57 | return this;
58 | }
59 |
60 | public String getClusterName() {
61 | return clusterName;
62 | }
63 |
64 | public EsConf setClusterName(String clusterName) {
65 | this.clusterName = clusterName;
66 | return this;
67 | }
68 |
69 |
70 | public EsConf builder() {
71 | Preconditions.checkArgument(!StringUtils.isEmpty(address));
72 | Preconditions.checkArgument(!StringUtils.isEmpty(clusterName));
73 | Preconditions.checkArgument(IPAddressUtil.isAddress(address));
74 | return this;
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/src/main/java/com/totoro/canal/es/consum/es/EsEntryArrayList.java:
--------------------------------------------------------------------------------
1 | package com.totoro.canal.es.consum.es;
2 |
3 | import com.totoro.canal.es.common.RecycleAble;
4 | import io.netty.util.Recycler;
5 |
6 | import java.util.ArrayList;
7 |
8 | /**
9 | * 说明 .
11 | *
12 | * Copyright: Copyright (c) 2017/12/22 下午2:21
13 | *
14 | * Company: xxx
15 | *
16 | *
17 | * @author zhongcheng_m@yeah.net
18 | * @version 1.0.0
19 | */
20 | public class EsEntryArrayList extends ArrayList
11 | *
12 | * Copyright: Copyright (c) 2017/12/22 下午2:25
13 | *
14 | * Company: xxx
15 | *
16 | *
17 | * @author zhongcheng_m@yeah.net
18 | * @version 1.0.0
19 | */
20 | public class EsRowDataArrayList extends ArrayList
23 | *
24 | * Copyright: Copyright (c) 2017/11/19 下午6:53
25 | *
26 | * Company: xxx
27 | *
28 | *
29 | * @author zhongcheng_m@yeah.net
30 | * @version 1.0.0
31 | */
32 | public class ElasticsearchServiceImpl implements ElasticsearchService {
33 |
34 | private Logger logger = LoggerFactory.getLogger(getClass());
35 |
36 | private TransportClient transportClient;
37 |
38 |
39 | public ElasticsearchServiceImpl(EsConf esConf) throws UnknownHostException {
40 |
41 | String clusterName = esConf.getClusterName();
42 | String address = esConf.getAddress();
43 | String[] hostPort = address.split(":");
44 |
45 | logger.info("Connect to elasticsearch {}:{}", clusterName, address);
46 |
47 | Settings settings = Settings.builder().put("cluster.name", clusterName)
48 | .put("client.transport.sniff", true)
49 | .build();
50 | transportClient = new PreBuiltTransportClient(settings)
51 | .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(hostPort[0]), Integer.valueOf(hostPort[1])));
52 |
53 |
54 | logger.info("Complete the connection to elasticsearch");
55 | }
56 |
57 | @Override
58 | public void insertById(final String index, final String type, final List
23 | * Copyright: Copyright (c)
24 | *
25 | * Company: xx
26 | *
27 | *
28 | * @author zhongcheng_m@yeah.net
29 | * @version 1.0.0
30 | */
31 | public class SelectException extends RuntimeException {
32 |
33 | /**
34 | *
35 | */
36 | private static final long serialVersionUID = 1L;
37 |
38 | public SelectException(String cause){
39 | super(cause);
40 | }
41 |
42 | public SelectException(Throwable t){
43 | super(t);
44 | }
45 |
46 | public SelectException(String cause, Throwable t){
47 | super(cause, t);
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/com/totoro/canal/es/select/selector/CanalConf.java:
--------------------------------------------------------------------------------
1 | package com.totoro.canal.es.select.selector;
2 |
3 | import com.google.common.base.Preconditions;
4 | import com.totoro.canal.es.common.TotoroException;
5 | import com.totoro.canal.es.select.selector.canal.CanalEmbedSelector;
6 | import com.totoro.canal.es.util.IPAddressUtil;
7 | import org.apache.commons.lang.StringUtils;
8 |
9 | import java.io.Serializable;
10 |
11 | /**
12 | * 说明 .
14 | *
15 | * Copyright: Copyright (c) 2017/12/04 上午10:50
16 | *
17 | * Company: xxx
18 | *
19 | *
20 | * @author zhongcheng_m@yeah.net
21 | * @version 1.0.0
22 | */
23 | public class CanalConf implements Serializable {
24 |
25 | private CanalEmbedSelector.Mode mode = CanalEmbedSelector.Mode.SIGN;
26 |
27 | //#canal的实例名字
28 | private String destination;
29 |
30 | private String filterPatten = "";
31 |
32 | //canal 地址
33 | private String address;
34 |
35 | private String zkAddress;
36 |
37 | private String userName;
38 |
39 | private String passWord;
40 |
41 | private String accept;
42 |
43 |
44 | public CanalEmbedSelector.Mode getMode() {
45 | return mode;
46 | }
47 |
48 | public CanalConf setMode(String mode) {
49 | if (!StringUtils.isEmpty(mode)) {
50 | mode = mode.toUpperCase();
51 | try {
52 | this.mode = CanalEmbedSelector.Mode.valueOf(mode);
53 | } catch (IllegalArgumentException e) {
54 | throw new TotoroException("no match patten,mode:" + mode);
55 | }
56 | }
57 | return this;
58 | }
59 |
60 | public String getDestination() {
61 | return destination;
62 | }
63 |
64 | public CanalConf setDestination(String destination) {
65 | this.destination = destination;
66 | return this;
67 | }
68 |
69 | public String getAddress() {
70 | return address;
71 | }
72 |
73 | public CanalConf setAddress(String address) {
74 | this.address = address;
75 | return this;
76 | }
77 |
78 | public String getFilterPatten() {
79 | return filterPatten;
80 | }
81 |
82 | public CanalConf setFilterPatten(String filterPatten) {
83 | if (!StringUtils.isEmpty(filterPatten)) {
84 | this.filterPatten = filterPatten;
85 | }
86 |
87 | return this;
88 | }
89 |
90 | public String getZkAddress() {
91 | return zkAddress;
92 | }
93 |
94 | public CanalConf setZkAddress(String zkAddress) {
95 | this.zkAddress = zkAddress;
96 | return this;
97 | }
98 |
99 | public String getUserName() {
100 | return userName;
101 | }
102 |
103 | public CanalConf setUserName(String userName) {
104 | this.userName = userName;
105 | return this;
106 | }
107 |
108 | public String getPassWord() {
109 | return passWord;
110 | }
111 |
112 | public CanalConf setPassWord(String passWord) {
113 | this.passWord = passWord;
114 | return this;
115 | }
116 |
117 |
118 | public String getAccept() {
119 | return accept;
120 | }
121 |
122 | public CanalConf setAccept(String accept) {
123 | this.accept = accept;
124 | return this;
125 | }
126 |
127 | public CanalConf builder() {
128 |
129 | Preconditions.checkArgument(StringUtils.isNotEmpty(destination), "Illegal destination , destination can't be empty");
130 | Preconditions.checkArgument(StringUtils.isNotEmpty(accept), "Illegal accept , accept can't be empty");
131 |
132 | if (CanalEmbedSelector.Mode.SIGN.equals(mode)) {
133 | Preconditions.checkArgument(IPAddressUtil.isAddress(address), "Illegal address : %s", address);
134 |
135 | } else if (CanalEmbedSelector.Mode.CLUSTER.equals(mode)) {
136 | Preconditions.checkArgument(IPAddressUtil.isAddress(address), "Illegal zkAddress : %s", zkAddress);
137 | }
138 |
139 | return this;
140 | }
141 |
142 |
143 | @Override
144 | public String toString() {
145 | final StringBuilder sb = new StringBuilder("CanalConf{");
146 | sb.append("mode=").append(mode);
147 | sb.append(", destination='").append(destination).append('\'');
148 | sb.append(", filterPatten='").append(filterPatten).append('\'');
149 | sb.append(", address='").append(address).append('\'');
150 | sb.append(", zkAddress='").append(zkAddress).append('\'');
151 | sb.append(", userName='").append(userName).append('\'');
152 | sb.append(", accept='").append(accept).append('\'');
153 | sb.append('}');
154 | return sb.toString();
155 | }
156 | }
157 |
--------------------------------------------------------------------------------
/src/main/java/com/totoro/canal/es/select/selector/SelectorTask.java:
--------------------------------------------------------------------------------
1 | package com.totoro.canal.es.select.selector;
2 |
3 | import com.alibaba.otter.canal.common.utils.BooleanMutex;
4 | import com.alibaba.otter.canal.protocol.CanalEntry;
5 | import com.alibaba.otter.canal.protocol.Message;
6 | import com.google.protobuf.InvalidProtocolBufferException;
7 | import com.totoro.canal.es.TotoroBootStrap;
8 | import com.totoro.canal.es.channel.TotoroChannel;
9 | import com.totoro.canal.es.common.RollBackMonitorFactory;
10 | import com.totoro.canal.es.common.GlobalTask;
11 | import org.apache.commons.lang.SystemUtils;
12 | import org.springframework.util.CollectionUtils;
13 |
14 | import java.text.SimpleDateFormat;
15 | import java.util.Date;
16 | import java.util.List;
17 |
18 | /**
19 | * 说明 .
21 | *
22 | * Copyright: Copyright (c) 2017/12/01 下午4:42
23 | *
24 | * Company: xxx
25 | *
26 | *
27 | * @author zhongcheng_m@yeah.net
28 | * @version 1.0.0
29 | */
30 | public class SelectorTask extends GlobalTask {
31 |
32 |
33 | private TotoroSelector totoroSelector;
34 |
35 | private TotoroChannel channel;
36 |
37 | private BooleanMutex rollBack = RollBackMonitorFactory.getBooleanMutex();
38 |
39 | private static String context_format = null;
40 |
41 | private static String row_format = null;
42 |
43 | private static String transaction_format = null;
44 |
45 | private static final String SEP = SystemUtils.LINE_SEPARATOR;
46 |
47 | private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
48 |
49 | private SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT);
50 |
51 | static {
52 | context_format = SEP + "****************************************************" + SEP;
53 | context_format += "* Batch Id: [{}] ,count : [{}] , memsize : [{}] , Time : {}" + SEP;
54 | context_format += "* Start : [{}] " + SEP;
55 | context_format += "* End : [{}] " + SEP;
56 | context_format += "****************************************************" + SEP;
57 |
58 | row_format = SEP
59 | + "----------------> binlog[{}:{}] , name[{},{}] , eventType : {} , executeTime : {} , delay : {}ms"
60 | + SEP;
61 |
62 | transaction_format = SEP + "================> binlog[{}:{}] , executeTime : {} , delay : {}ms" + SEP;
63 |
64 | }
65 |
66 | /**
67 | * 是否耦合度过高??
68 | */
69 |
70 | public SelectorTask(TotoroSelector totoroSelector, TotoroChannel channel, TotoroBootStrap canalScheduler) {
71 | logger.info("Selector task init .......");
72 | this.totoroSelector = totoroSelector;
73 | this.channel = channel;
74 | logger.info("Selector task complete .......");
75 | }
76 |
77 |
78 | @Override
79 | public void run() {
80 | running = true;
81 |
82 | totoroSelector.start();
83 | totoroSelector.rollback();
84 |
85 | logger.info("Selector task start .......");
86 | Message message;
87 | rollBack.set(true);
88 | while (running) {
89 | try {
90 | //出现回滚立即停止
91 | message = totoroSelector.selector();
92 |
93 |
94 | /**
95 | * 当前处理回滚的调度模型,可以保证在消费端出错的时候、正确处理回滚,并正确应答和继续消费数据。
96 | *
97 | * 当发生回滚时,首先 consumer task 会将 rollback 设置为true ,自己停止工作,等待唤醒
98 | * 然后 trans task 也会同样挂起
99 | * channel会拒绝接受 message 和 future ,对于已经提交的 future 会尝试取消
100 | *
101 | * 到此 除了 selector task 以外 的所有线程 全部尽最大努力去停止处理消息,但注意此时还没有回滚
102 | *
103 | * 因为 selector 是循环获取数据,每次循环都会判断 rollback 状态,一旦发现rollback状态,跳出循环
104 | * 返回到 selector task里面 ,task会感知到回滚状态 ,清空渠道中的消息 ,并回滚 至最后一个未应答的
105 | * 消费点,然后丢弃本条消息,重新获取一次消息(回滚的消息)
106 | * 当上面所有工作 都做完了,便完成了回滚 ,selector task 改变回滚状态,重新正常工作
107 | *
108 | * 粗略测试结果 : 40000条数据,单机测试,当 batchId 能被2整除的时候回滚
109 | * 在不真正消费数据的前提下(消费端直接应答),处理性能非常好
110 | *
111 | */
112 | if (rollBack.state() == false) {
113 | totoroSelector.rollback();
114 | logger.info("The rollback happened =============> discard message , batchId :{}", message.getId());
115 | //丢弃刚才的消息
116 | message = totoroSelector.selector();
117 | channel.clearMessage();
118 | rollBack.set(true);
119 | }
120 |
121 | long batchId = message.getId();
122 | int size = message.getEntries().size();
123 | if (batchId == -1 || size == 0) {
124 | message = null;//help gc
125 | } else {
126 | logger.info("Put message into channel =====> batchId :{}", message.getId());
127 |
128 | if (logger.isDebugEnabled()) {
129 | printSummary(message, batchId, size);
130 | printEntry(message.getEntries());
131 | }
132 | //将消息放入管道
133 | channel.putMessage(message);
134 | }
135 | } catch (InterruptedException e) {
136 | logger.error("Selector task has been interrupted ", e);
137 | running = false;
138 | break;
139 | }
140 | }
141 | }
142 |
143 |
144 | private void printSummary(Message message, long batchId, int size) {
145 | long memsize = 0;
146 | for (CanalEntry.Entry entry : message.getEntries()) {
147 | memsize += entry.getHeader().getEventLength();
148 | }
149 |
150 | String startPosition = null;
151 | String endPosition = null;
152 | if (!CollectionUtils.isEmpty(message.getEntries())) {
153 | startPosition = buildPositionForDump(message.getEntries().get(0));
154 | endPosition = buildPositionForDump(message.getEntries().get(message.getEntries().size() - 1));
155 | }
156 |
157 | logger.info(context_format, new Object[]{batchId, size, memsize, format.format(new Date()), startPosition,
158 | endPosition});
159 | }
160 |
161 | protected String buildPositionForDump(CanalEntry.Entry entry) {
162 | long time = entry.getHeader().getExecuteTime();
163 | Date date = new Date(time);
164 | return entry.getHeader().getLogfileName() + ":" + entry.getHeader().getLogfileOffset() + ":"
165 | + entry.getHeader().getExecuteTime() + "(" + format.format(date) + ")";
166 | }
167 |
168 | protected void printEntry(List
9 | * Copyright: Copyright (c)
10 | *
11 | * Company: xx
12 | *
13 | *
14 | * @author zhongcheng_m@yeah.net
15 | * @version 1.0.0
16 | */
17 | public interface TotoroSelector {
18 |
19 | /**
20 | */
21 | void start();
22 |
23 | /**
24 | */
25 | boolean isStart();
26 |
27 | /**
28 | */
29 | void stop();
30 |
31 | /**
32 | */
33 | Message selector() throws InterruptedException;
34 |
35 |
36 | /**
37 | */
38 | Long lastEntryTime();
39 |
40 | /**
41 | */
42 | List
26 | * Copyright: Copyright (c)
27 | *
28 | * Company: xx
29 | *
30 | *
31 | * @author zhongcheng_m@yeah.net
32 | * @version 1.0.0
33 | */
34 | public class CanalEmbedSelector implements TotoroSelector {
35 |
36 | private static final Logger logger = LoggerFactory.getLogger(CanalEmbedSelector.class);
37 |
38 | private volatile boolean running = false;
39 |
40 | private CanalConnector connector; // instance client
41 |
42 | private String destination;
43 |
44 | private static final int maxEmptyTimes = 10;
45 |
46 | private int batchSize = 5 * 1024;
47 |
48 | private String filterPatten;
49 |
50 | private long batchTimeout = -1L;
51 |
52 | private Mode mode;
53 |
54 | private BooleanMutex rollBack = RollBackMonitorFactory.getBooleanMutex();
55 |
56 | public enum Mode {
57 | SIGN, CLUSTER
58 | }
59 |
60 | public CanalEmbedSelector(CanalConf conf) {
61 |
62 | logger.info("TotoroSelector init start , conf :{}", conf.toString());
63 |
64 | this.mode = conf.getMode();
65 | this.destination = conf.getDestination();
66 | this.filterPatten = conf.getFilterPatten();
67 |
68 | String userName = conf.getUserName();
69 | String passWord = conf.getPassWord();
70 |
71 | if (Mode.SIGN.equals(mode)) {
72 | String address = conf.getAddress();
73 |
74 | String[] hostPort = address.split(":");
75 |
76 | String ip = hostPort[0];
77 | Integer port = Integer.valueOf(hostPort[1]);
78 |
79 | SocketAddress socketAddress = new InetSocketAddress(ip, port);
80 |
81 | connector = CanalConnectors.newSingleConnector(socketAddress,
82 | destination,
83 | userName,
84 | passWord);
85 |
86 | } else if (Mode.CLUSTER.equals(mode)) {
87 | String zkAddress = conf.getZkAddress();
88 | connector = CanalConnectors.newClusterConnector(zkAddress, destination, userName, passWord);
89 | } else {
90 | throw new TotoroException("Invalid mode");
91 | }
92 | logger.info("TotoroSelector init complete .......");
93 | }
94 |
95 |
96 | @Override
97 | public void start() {
98 | if (running) {
99 | return;
100 | }
101 | connector.connect();
102 | connector.subscribe(filterPatten);
103 | running = true;
104 | }
105 |
106 | @Override
107 | public boolean isStart() {
108 | return running;
109 | }
110 |
111 | @Override
112 | public void stop() {
113 | connector.disconnect();
114 | }
115 |
116 | @Override
117 | public Message selector() throws InterruptedException {
118 | if (!running) {
119 | throw new RuntimeException("CanalEmbedSelector has benn not start");
120 | }
121 |
122 | Message message = null;
123 | int emptyTimes = 0;
124 |
125 | if (batchTimeout < 0) {
126 | while (running) {
127 | message = connector.getWithoutAck(batchSize);
128 |
129 | if (message == null || message.getId() == -1L) {
130 | if (rollBack.state() == false) {
131 | break;
132 | } else {
133 | applyWait(emptyTimes++);
134 | }
135 |
136 | } else {
137 | break;
138 | }
139 | }
140 | if (!running) {
141 | throw new InterruptedException();
142 | }
143 | } else {
144 |
145 | while (running) {
146 | message = connector.getWithoutAck(batchSize, batchTimeout, TimeUnit.SECONDS);
147 | if (message == null || message.getId() == -1L) {
148 | continue;
149 | } else {
150 | break;
151 | }
152 | }
153 | if (!running) {
154 | throw new InterruptedException();
155 | }
156 | }
157 |
158 | return message;
159 | }
160 |
161 | @Override
162 | public Long lastEntryTime() {
163 | return null;
164 | }
165 |
166 | @Override
167 | public List
10 | *
11 | * Copyright: Copyright (c) 2017/12/19 上午9:49
12 | *
13 | * Company: xxx
14 | *
15 | *
16 | * @author zhongcheng_m@yeah.net
17 | * @version 1.0.0
18 | */
19 | public interface EsAdapter {
20 |
21 | String getEsIdColumn(String database, String table);
22 |
23 | int getEsEventType(CanalEntry.EventType eventType);
24 |
25 | List
8 | *
9 | * Copyright: Copyright (c) 2017/12/18 下午3:06
10 | *
11 | * Company: xxx
12 | *
13 | *
14 | * @author zhongcheng_m@yeah.net
15 | * @version 1.0.0
16 | */
17 | public interface MessageFilter {
18 |
19 | boolean filter(CanalEntry.Entry entry);
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/totoro/canal/es/transform/MessageFilterChain.java:
--------------------------------------------------------------------------------
1 | package com.totoro.canal.es.transform;
2 |
3 | import com.alibaba.otter.canal.protocol.CanalEntry;
4 | import org.slf4j.Logger;
5 | import org.slf4j.LoggerFactory;
6 |
7 | import java.util.ArrayList;
8 | import java.util.List;
9 | import java.util.concurrent.locks.ReentrantReadWriteLock;
10 |
11 | /**
12 | * 说明 .
14 | *
15 | * Copyright: Copyright (c) 2017/12/18 下午3:07
16 | *
17 | * Company: xxx
18 | *
19 | *
20 | * @author zhongcheng_m@yeah.net
21 | * @version 1.0.0
22 | */
23 | public class MessageFilterChain implements MessageFilter {
24 |
25 | private Logger logger = LoggerFactory.getLogger(getClass());
26 |
27 | private MessageFilterChain() {
28 |
29 | }
30 |
31 | private List
17 | *
18 | * Copyright: Copyright (c) 2017/12/19 上午9:37
19 | *
20 | * Company: xxx
21 | *
22 | *
23 | * @author zhongcheng_m@yeah.net
24 | * @version 1.0.0
25 | */
26 | public class SimpleEsAdapter implements EsAdapter {
27 |
28 | private Logger logger = LoggerFactory.getLogger(getClass());
29 |
30 | private static Map
11 | *
12 | * Copyright: Copyright (c) 2017/12/18 下午3:31
13 | *
14 | * Company: xxx
15 | *
16 | *
17 | * @author zhongcheng_m@yeah.net
18 | * @version 1.0.0
19 | */
20 | public class SimpleMessageFilter implements MessageFilter {
21 |
22 | private static final Set
13 | *
14 | * Copyright: Copyright (c) 2017/12/18 下午3:29
15 | *
16 | * Company: xxx
17 | *
18 | *
19 | * @author zhongcheng_m@yeah.net
20 | * @version 1.0.0
21 | */
22 | public class TableFilter implements MessageFilter {
23 |
24 | private static final String DELIMITER = ",";
25 |
26 | private static final String CONNECTOR = "\\.";
27 |
28 | private static final String CONNECTOR_TEP = ".";
29 |
30 | private Set
22 | *
23 | * Copyright: Copyright (c) 2017/11/19 下午6:44
24 | *
25 | * Company: xxx
26 | *
27 | *
28 | * @author zhongcheng_m@yeah.net
29 | * @version 1.0.0
30 | */
31 | public class TotoroTransForm implements TransForm
19 | *
20 | * Copyright: Copyright (c) 2017/12/01 下午1:27
21 | *
22 | * Company: xxx
23 | *
24 | *
25 | * @author zhongcheng_m@yeah.net
26 | * @version 1.0.0
27 | */
28 | public class TransFormTask extends GlobalTask {
29 |
30 | private TotoroChannel channel;
31 |
32 | private BooleanMutex booleanMutex = RollBackMonitorFactory.getBooleanMutex();
33 |
34 | private EsAdapter esAdapter;
35 |
36 | public TransFormTask(TotoroChannel channel, EsAdapter esAdapter, int threadNum) {
37 |
38 | logger.info("TransFormTask init start .......");
39 |
40 | this.channel = channel;
41 | ThreadFactory threadFactory = new ThreadFactoryBuilder()
42 | .setNameFormat("trans-pool-%d")
43 | .build();
44 | executorService = Executors.newFixedThreadPool(threadNum, threadFactory);
45 | this.esAdapter = esAdapter;
46 |
47 | logger.info("TransFormTask init complete .......");
48 | }
49 |
50 |
51 | @Override
52 | public void run() {
53 | logger.info("TransFormTask start .......");
54 | running = true;
55 | while (running) {
56 | try {
57 | booleanMutex.get();
58 | } catch (InterruptedException e) {
59 | logger.error("TransFormTask task has been interrupted ", e);
60 | running = false;
61 | break;
62 | }
63 | try {
64 | Message message = channel.takeMessage();
65 | logger.info("Transform message =====> {}", message.getId());
66 | TotoroTransForm transForm = getTotoroTransForm(message);
67 | Future
8 | *
9 | * Copyright: Copyright (c) 2017/12/04 下午12:46
10 | *
11 | * Company: xxx
12 | *
13 | *
14 | * @author zhongcheng_m@yeah.net
15 | * @version 1.0.0
16 | */
17 | public class IPAddressUtil extends sun.net.util.IPAddressUtil {
18 | public static boolean isAddress(String address) {
19 | if (!StringUtils.isEmpty(address)) {
20 | String[] hostPort = address.trim().split(":");
21 | if (hostPort.length == 2) {
22 | String ip = hostPort[0];
23 | String portStr = hostPort[1];
24 | if (sun.net.util.IPAddressUtil.isIPv4LiteralAddress(ip) && StringUtils.isNumeric(portStr)) {
25 | Integer port = Integer.valueOf(portStr);
26 | return 0 < port && port < 65535;
27 | }
28 | }
29 | }
30 | return false;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/resources/canal-es.properties:
--------------------------------------------------------------------------------
1 | # ----------------------- canal 相关配置 -----------------------------
2 | #canal的实例名字
3 | totoro.canal.destination=totoro
4 | #cananl 服务端的模式,单机:single ,集群:cluster/
5 | totoro.canal.mode=sign
6 | #canal 地址,包括端口号
7 | totoro.canal.address=127.0.0.1:11111
8 | #过滤表达式
9 | totoro.canal.filter.patten=
10 | #如果canal模式是集群的话,则需要填写zk地址
11 | totoro.canal.zk.address=
12 | totoro.canal.username=
13 | totoro.canal.password=
14 |
15 | #此项必须配置,不配置会导致 totoro 启动不了, totoro 只会处理在此配置的表
16 | #没有在此配置的表,totoro 将会忽略,不会进行同步,格式 "database.table.id" 多个使用 ","分割
17 | #database代表数据库,table 代表 数据库中的表,id代表 table中的 id
18 | #totoro 会默认将 database 作为 es中的index,table作为es中的type ,使用db中的id作为es的id
19 | totoro.canal.table.accept=demo.cc.id
20 |
21 |
22 | # ----------------------- elasticsearch 相关配置 -----------------------------
23 | totoro.es.address=127.0.0.1:9300
24 | totoro.es.cluster.name=my-elasticsearch
25 | totoro.es.username=
26 | totoro.es.password=
27 | # ----------------------- totoro 相关配置 ----------------------------
28 | #处理信息转换的线程数量 默认 3个 , 不要配置太大,2-4 之间吧,取决于业务情况,太大并不会增加性能,反而会增加上下文切换的开销
29 | totoro.cannal.trans.thread.nums=3
30 |
--------------------------------------------------------------------------------
/src/main/resources/logback.xml:
--------------------------------------------------------------------------------
1 |
2 |
13 | *
14 | * Copyright: Copyright (c) 2017/12/04 下午2:19
15 | *
16 | * Company: xxx
17 | *
18 | *
19 | * @author zhongcheng_m@yeah.net
20 | * @version 1.0.0
21 | */
22 | public class Lock {
23 |
24 | public static void get() {
25 | LockSupport.park();
26 | }
27 |
28 |
29 | public static void main(String[] args) throws ExecutionException, InterruptedException {
30 |
31 | ExecutorService executorService = Executors.newFixedThreadPool(2);
32 |
33 |
34 | TestFuture testFuture = new TestFuture(() -> {
35 | Thread.sleep(4000);
36 | return "zhong";
37 | });
38 |
39 | executorService.submit(testFuture);
40 |
41 | System.out.printf("-------");
42 |
43 | System.out.printf(testFuture.get());
44 |
45 | System.out.printf("asdasda");
46 |
47 | }
48 |
49 |
50 | public static class Student {
51 |
52 |
53 | public Student(String name, int age) {
54 | this.name = name;
55 | this.age = age;
56 | }
57 |
58 | private String name;
59 |
60 | private int age;
61 |
62 | public String getName() {
63 | return name;
64 | }
65 |
66 | public Student setName(String name) {
67 | this.name = name;
68 | return this;
69 | }
70 |
71 | public int getAge() {
72 | return age;
73 | }
74 |
75 | public Student setAge(int age) {
76 | this.age = age;
77 | return this;
78 | }
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/src/test/java/test/syn/LockTest.java:
--------------------------------------------------------------------------------
1 | package test.syn;
2 |
3 | import com.alibaba.otter.canal.client.CanalConnector;
4 | import com.alibaba.otter.canal.client.CanalConnectors;
5 | import com.alibaba.otter.canal.protocol.Message;
6 |
7 | import java.net.InetSocketAddress;
8 | import java.util.concurrent.locks.LockSupport;
9 |
10 | /**
11 | * 说明 .
13 | *
14 | * Copyright: Copyright (c) 2017/12/04 下午2:13
15 | *
16 | * Company: xxx
17 | *
18 | *
19 | * @author zhongcheng_m@yeah.net
20 | * @version 1.0.0
21 | */
22 | public class LockTest {
23 |
24 | static boolean running = true;
25 |
26 | private static final int maxEmptyTimes = 10;
27 |
28 | public static void main(String[] args) throws InterruptedException {
29 |
30 | CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress("127.0.0.1", 11111),
31 | "totoro",
32 | "",
33 | "");
34 |
35 | connector.connect();
36 | connector.subscribe();
37 | int emptyTimes = 0;
38 |
39 | while (running) {
40 | Message message = connector.getWithoutAck(5 * 1024);
41 |
42 | if (message == null || message.getId() == -1L) {
43 | applyWait(emptyTimes++);
44 | } else {
45 | //logger.info(message.toString());
46 | long messageId = message.getId();
47 | System.out.println("消息id:" + messageId);
48 | Thread.sleep(1000);
49 |
50 | connector.rollback();
51 | }
52 |
53 |
54 | }
55 |
56 | }
57 |
58 | private static void applyWait(int emptyTimes) throws InterruptedException {
59 | int newEmptyTimes = emptyTimes > maxEmptyTimes ? maxEmptyTimes : emptyTimes;
60 | if (emptyTimes >= 3) {
61 | Thread.yield();
62 | LockSupport.parkNanos(10000 * 1000L * newEmptyTimes);
63 | }
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/src/test/java/test/syn/TestFuture.java:
--------------------------------------------------------------------------------
1 | package test.syn;
2 |
3 | import java.util.concurrent.Callable;
4 | import java.util.concurrent.FutureTask;
5 |
6 | /**
7 | * 说明 .
9 | *
10 | * Copyright: Copyright (c) 2017/12/22 下午2:45
11 | *
12 | * Company: xxx
13 | *
14 | *
15 | * @author zhongcheng_m@yeah.net
16 | * @version 1.0.0
17 | */
18 | public class TestFuture extends FutureTask
5 | *
7 | *
5 | *
13 | *
14 | *
10 | *
12 | *
10 | *
11 | *
10 | *
10 | *
22 | *
21 | * 类详细说明.
22 | *
13 | *
20 | *
9 | *
7 | *
13 | *
16 | *
10 | *
12 | *
21 | *
18 | *
7 | *
12 | *
12 | *
8 | *