|
11 | 11 | import java.util.HashMap; |
12 | 12 | import java.util.List; |
13 | 13 | import java.util.Map; |
| 14 | +import java.util.concurrent.BlockingQueue; |
| 15 | +import java.util.concurrent.LinkedBlockingQueue; |
| 16 | +import java.util.concurrent.TimeUnit; |
14 | 17 |
|
15 | 18 | import org.apache.avro.Schema; |
16 | 19 | import org.apache.avro.generic.GenericData; |
|
23 | 26 | import com.google.code.or.binlog.impl.event.AbstractBinlogEventV4; |
24 | 27 | import com.google.code.or.binlog.impl.event.DeleteRowsEvent; |
25 | 28 | import com.google.code.or.binlog.impl.event.DeleteRowsEventV2; |
| 29 | +import com.google.code.or.binlog.impl.event.FormatDescriptionEvent; |
26 | 30 | import com.google.code.or.binlog.impl.event.QueryEvent; |
27 | 31 | import com.google.code.or.binlog.impl.event.RotateEvent; |
28 | 32 | import com.google.code.or.binlog.impl.event.TableMapEvent; |
|
54 | 58 | import com.google.code.or.common.glossary.column.TinyColumn; |
55 | 59 | import com.google.code.or.common.glossary.column.YearColumn; |
56 | 60 | import com.linkedin.databus.core.DatabusRuntimeException; |
| 61 | +import com.linkedin.databus.core.DatabusThreadBase; |
57 | 62 | import com.linkedin.databus.core.DbusOpcode; |
58 | 63 | import com.linkedin.databus2.core.DatabusException; |
59 | 64 | import com.linkedin.databus2.producers.ds.DbChangeEntry; |
|
74 | 79 | * This class is responsible for converting Bin log events to Avro records using schemaRegistry and calling an application callback |
75 | 80 | * to let the application generate DbusEvent and append to EventBuffer. |
76 | 81 | */ |
77 | | -class ORListener implements BinlogEventListener |
| 82 | +class ORListener extends DatabusThreadBase implements BinlogEventListener |
78 | 83 | { |
79 | 84 | /** |
80 | 85 | * Application Callback from this class to process one transaction |
@@ -134,143 +139,40 @@ public interface TransactionProcessor |
134 | 139 | /** Flag to indicate the begining of the txn is seen. Used to indicate **/ |
135 | 140 | private boolean _isBeginTxnSeen = false; |
136 | 141 |
|
137 | | - public ORListener(int currentFileNumber, |
| 142 | + private BlockingQueue<BinlogEventV4> binlogEventQueue = null; |
| 143 | + |
| 144 | + public ORListener(String name, |
| 145 | + int currentFileNumber, |
138 | 146 | Logger log, |
139 | 147 | String binlogFilePrefix, |
140 | 148 | TransactionProcessor txnProcessor, |
141 | 149 | Map<String, Short> tableUriToSrcIdMap, |
142 | 150 | Map<String, String> tableUriToSrcNameMap, |
143 | | - SchemaRegistryService schemaRegistryService) |
| 151 | + SchemaRegistryService schemaRegistryService, |
| 152 | + int maxQueueSize) |
144 | 153 | { |
| 154 | + super("ORListener_" + name); |
145 | 155 | _log = log; |
146 | 156 | _txnProcessor = txnProcessor; |
147 | 157 | _binlogFilePrefix = binlogFilePrefix; |
148 | 158 | _tableUriToSrcIdMap = tableUriToSrcIdMap; |
149 | 159 | _tableUriToSrcNameMap = tableUriToSrcNameMap; |
150 | 160 | _schemaRegistryService = schemaRegistryService; |
151 | 161 | _currFileNum = currentFileNumber; |
| 162 | + binlogEventQueue = new LinkedBlockingQueue<BinlogEventV4>(maxQueueSize); |
152 | 163 | } |
153 | 164 |
|
154 | 165 | @Override |
155 | 166 | public void onEvents(BinlogEventV4 event) |
156 | 167 | { |
157 | | - if ( event == null) |
158 | | - { |
159 | | - _log.error("Received null event"); |
160 | | - return; |
161 | | - } |
162 | | - |
163 | | - // Beginning of Txn |
164 | | - if (event instanceof QueryEvent) |
165 | | - { |
166 | | - QueryEvent qe = (QueryEvent)event; |
167 | | - String sql = qe.getSql().toString(); |
168 | | - if ("BEGIN".equalsIgnoreCase(sql)) |
169 | | - { |
170 | | - _isBeginTxnSeen = true; |
171 | | - _log.info("BEGIN sql: " + sql); |
172 | | - _currTxnSizeInBytes = event.getHeader().getEventLength(); |
173 | | - startXtion(qe); |
174 | | - return; |
175 | | - } |
176 | | - } |
177 | | - |
178 | | - if ( ! _isBeginTxnSeen ) |
179 | | - { |
180 | | - if (_log.isDebugEnabled()) |
181 | | - { |
182 | | - _log.debug("Skipping event (" + event |
183 | | - + ") as this is before the start of first transaction"); |
| 168 | + boolean isPut = false; |
| 169 | + do { |
| 170 | + try { |
| 171 | + isPut = binlogEventQueue.offer(event, 100, TimeUnit.MILLISECONDS); |
| 172 | + } catch (InterruptedException e) { |
| 173 | + _log.error("failed to put binlog event to binlogEventQueue event: " + event, e); |
184 | 174 | } |
185 | | - return; |
186 | | - } |
187 | | - |
188 | | - _currTxnSizeInBytes += event.getHeader().getEventLength(); |
189 | | - |
190 | | - if (event instanceof QueryEvent) |
191 | | - { |
192 | | - QueryEvent qe = (QueryEvent)event; |
193 | | - String sql = qe.getSql().toString(); |
194 | | - if ("COMMIT".equalsIgnoreCase(sql)) |
195 | | - { |
196 | | - _log.debug("COMMIT sql: " + sql); |
197 | | - endXtion(qe); |
198 | | - return; |
199 | | - } |
200 | | - else if ("ROLLBACK".equalsIgnoreCase(sql)) |
201 | | - { |
202 | | - _log.debug("ROLLBACK sql: " + sql); |
203 | | - rollbackXtion(qe); |
204 | | - return; |
205 | | - } |
206 | | - else |
207 | | - { |
208 | | - // Ignore DDL statements for now |
209 | | - _log.debug("Likely DDL statement sql: " + sql); |
210 | | - return; |
211 | | - } |
212 | | - } |
213 | | - else if (event instanceof RotateEvent) |
214 | | - { |
215 | | - RotateEvent re = (RotateEvent)event; |
216 | | - String fileName = re.getBinlogFileName().toString(); |
217 | | - _log.info("File Rotated : FileName :" + fileName + ", _binlogFilePrefix :" + _binlogFilePrefix); |
218 | | - String fileNumStr = fileName.substring(fileName.lastIndexOf(_binlogFilePrefix) + _binlogFilePrefix.length() + 1); |
219 | | - _currFileNum = Integer.parseInt(fileNumStr); |
220 | | - } |
221 | | - else if (event instanceof XidEvent) |
222 | | - { |
223 | | - XidEvent xe = (XidEvent)event; |
224 | | - long xid = xe.getXid(); |
225 | | - _log.debug("Treating XID event with xid = " + xid + " as commit for the transaction"); |
226 | | - endXtion(xe); |
227 | | - return; |
228 | | - } |
229 | | - else if (event instanceof WriteRowsEvent) |
230 | | - { |
231 | | - WriteRowsEvent wre = (WriteRowsEvent)event; |
232 | | - insertRows(wre); |
233 | | - } |
234 | | - else if (event instanceof WriteRowsEventV2) |
235 | | - { |
236 | | - WriteRowsEventV2 wre = (WriteRowsEventV2)event; |
237 | | - insertRows(wre); |
238 | | - } |
239 | | - else if (event instanceof UpdateRowsEvent) |
240 | | - { |
241 | | - UpdateRowsEvent ure = (UpdateRowsEvent)event; |
242 | | - updateRows(ure); |
243 | | - } |
244 | | - else if (event instanceof UpdateRowsEventV2) |
245 | | - { |
246 | | - UpdateRowsEventV2 ure = (UpdateRowsEventV2)event; |
247 | | - updateRows(ure); |
248 | | - } |
249 | | - else if (event instanceof DeleteRowsEventV2) |
250 | | - { |
251 | | - DeleteRowsEventV2 dre = (DeleteRowsEventV2)event; |
252 | | - deleteRows(dre); |
253 | | - } |
254 | | - else if (event instanceof DeleteRowsEvent) |
255 | | - { |
256 | | - DeleteRowsEvent dre = (DeleteRowsEvent)event; |
257 | | - deleteRows(dre); |
258 | | - } |
259 | | - else if (event instanceof TableMapEvent) |
260 | | - { |
261 | | - TableMapEvent tme = (TableMapEvent)event; |
262 | | - processTableMapEvent(tme); |
263 | | - } |
264 | | - else |
265 | | - { |
266 | | - _log.warn("Skipping !! Unknown OR event e: " + event); |
267 | | - return; |
268 | | - } |
269 | | - |
270 | | - if ( _log.isDebugEnabled()) |
271 | | - { |
272 | | - _log.debug("e: " + event); |
273 | | - } |
| 175 | + } while (!isPut && !isShutdownRequested()); |
274 | 176 | } |
275 | 177 |
|
276 | 178 |
|
@@ -774,4 +676,153 @@ public static long scn(int logId, int offset) |
774 | 676 | scn |= offset; |
775 | 677 | return scn; |
776 | 678 | } |
| 679 | + |
| 680 | + @Override |
| 681 | + public void run() { |
| 682 | + List<BinlogEventV4> eventList = new ArrayList<BinlogEventV4>(); |
| 683 | + BinlogEventV4 event; |
| 684 | + boolean isShutdown = false; |
| 685 | + while (!isShutdown) |
| 686 | + { |
| 687 | + eventList.clear(); |
| 688 | + int eventNumber = binlogEventQueue.drainTo(eventList); |
| 689 | + for (int i = 0; i < eventNumber; i++) |
| 690 | + { |
| 691 | + if (isShutdownRequested()) |
| 692 | + { |
| 693 | + isShutdown = true; |
| 694 | + break; |
| 695 | + } |
| 696 | + |
| 697 | + event = eventList.get(i); |
| 698 | + if (event == null) |
| 699 | + { |
| 700 | + _log.error("Received null event"); |
| 701 | + continue; |
| 702 | + } |
| 703 | + |
| 704 | + try { |
| 705 | + // Beginning of Txn |
| 706 | + if (event instanceof QueryEvent) |
| 707 | + { |
| 708 | + QueryEvent qe = (QueryEvent)event; |
| 709 | + String sql = qe.getSql().toString(); |
| 710 | + if ("BEGIN".equalsIgnoreCase(sql)) |
| 711 | + { |
| 712 | + _isBeginTxnSeen = true; |
| 713 | + _log.info("BEGIN sql: " + sql); |
| 714 | + _currTxnSizeInBytes = event.getHeader().getEventLength(); |
| 715 | + startXtion(qe); |
| 716 | + continue; |
| 717 | + } |
| 718 | + } |
| 719 | + |
| 720 | + if ( ! _isBeginTxnSeen ) |
| 721 | + { |
| 722 | + if (_log.isDebugEnabled()) |
| 723 | + { |
| 724 | + _log.debug("Skipping event (" + event |
| 725 | + + ") as this is before the start of first transaction"); |
| 726 | + } |
| 727 | + continue; |
| 728 | + } |
| 729 | + |
| 730 | + _currTxnSizeInBytes += event.getHeader().getEventLength(); |
| 731 | + |
| 732 | + if (event instanceof QueryEvent) |
| 733 | + { |
| 734 | + QueryEvent qe = (QueryEvent)event; |
| 735 | + String sql = qe.getSql().toString(); |
| 736 | + if ("COMMIT".equalsIgnoreCase(sql)) |
| 737 | + { |
| 738 | + _log.debug("COMMIT sql: " + sql); |
| 739 | + endXtion(qe); |
| 740 | + continue; |
| 741 | + } |
| 742 | + else if ("ROLLBACK".equalsIgnoreCase(sql)) |
| 743 | + { |
| 744 | + _log.debug("ROLLBACK sql: " + sql); |
| 745 | + rollbackXtion(qe); |
| 746 | + continue; |
| 747 | + } |
| 748 | + else |
| 749 | + { |
| 750 | + // Ignore DDL statements for now |
| 751 | + _log.debug("Likely DDL statement sql: " + sql); |
| 752 | + continue; |
| 753 | + } |
| 754 | + } |
| 755 | + else if (event instanceof RotateEvent) |
| 756 | + { |
| 757 | + RotateEvent re = (RotateEvent)event; |
| 758 | + String fileName = re.getBinlogFileName().toString(); |
| 759 | + _log.info("File Rotated : FileName :" + fileName + ", _binlogFilePrefix :" + _binlogFilePrefix); |
| 760 | + String fileNumStr = fileName.substring(fileName.lastIndexOf(_binlogFilePrefix) + _binlogFilePrefix.length() + 1); |
| 761 | + _currFileNum = Integer.parseInt(fileNumStr); |
| 762 | + } |
| 763 | + else if (event instanceof XidEvent) |
| 764 | + { |
| 765 | + XidEvent xe = (XidEvent)event; |
| 766 | + long xid = xe.getXid(); |
| 767 | + _log.debug("Treating XID event with xid = " + xid + " as commit for the transaction"); |
| 768 | + endXtion(xe); |
| 769 | + continue; |
| 770 | + } |
| 771 | + else if (event instanceof WriteRowsEvent) |
| 772 | + { |
| 773 | + WriteRowsEvent wre = (WriteRowsEvent)event; |
| 774 | + insertRows(wre); |
| 775 | + } |
| 776 | + else if (event instanceof WriteRowsEventV2) |
| 777 | + { |
| 778 | + WriteRowsEventV2 wre = (WriteRowsEventV2)event; |
| 779 | + insertRows(wre); |
| 780 | + } |
| 781 | + else if (event instanceof UpdateRowsEvent) |
| 782 | + { |
| 783 | + UpdateRowsEvent ure = (UpdateRowsEvent)event; |
| 784 | + updateRows(ure); |
| 785 | + } |
| 786 | + else if (event instanceof UpdateRowsEventV2) |
| 787 | + { |
| 788 | + UpdateRowsEventV2 ure = (UpdateRowsEventV2)event; |
| 789 | + updateRows(ure); |
| 790 | + } |
| 791 | + else if (event instanceof DeleteRowsEventV2) |
| 792 | + { |
| 793 | + DeleteRowsEventV2 dre = (DeleteRowsEventV2)event; |
| 794 | + deleteRows(dre); |
| 795 | + } |
| 796 | + else if (event instanceof DeleteRowsEvent) |
| 797 | + { |
| 798 | + DeleteRowsEvent dre = (DeleteRowsEvent)event; |
| 799 | + deleteRows(dre); |
| 800 | + } |
| 801 | + else if (event instanceof TableMapEvent) |
| 802 | + { |
| 803 | + TableMapEvent tme = (TableMapEvent)event; |
| 804 | + processTableMapEvent(tme); |
| 805 | + } |
| 806 | + else if (event instanceof FormatDescriptionEvent) |
| 807 | + { |
| 808 | + // we don't need process this event |
| 809 | + _log.info("receive FormatDescriptionEvent event"); |
| 810 | + } |
| 811 | + else |
| 812 | + { |
| 813 | + _log.warn("Skipping !! Unknown OR event e: " + event); |
| 814 | + continue; |
| 815 | + } |
| 816 | + |
| 817 | + if ( _log.isDebugEnabled()) |
| 818 | + { |
| 819 | + _log.debug("e: " + event); |
| 820 | + } |
| 821 | + } catch (Exception e) { |
| 822 | + _log.error("failed to process binlog event, event: " + event, e); |
| 823 | + } |
| 824 | + } |
| 825 | + } |
| 826 | + _log.info("ORListener Thread done"); |
| 827 | + } |
777 | 828 | } |
0 commit comments