Search in sources :

Example 1 with TableMapEvent

use of com.google.code.or.binlog.impl.event.TableMapEvent in project databus by linkedin.

the class ORListener method run.

@Override
public void run() {
    List<BinlogEventV4> eventList = new ArrayList<BinlogEventV4>();
    BinlogEventV4 event;
    while (!isShutdownRequested()) {
        if (isPauseRequested()) {
            LOG.info("Pause requested for ORListener. Pausing !!");
            signalPause();
            LOG.info("Pausing. Waiting for resume command");
            try {
                awaitUnPauseRequest();
            } catch (InterruptedException e) {
                _log.info("Interrupted !!");
            }
            LOG.info("Resuming ORListener !!");
            signalResumed();
            LOG.info("ORListener resumed !!");
        }
        eventList.clear();
        int eventNumber = _binlogEventQueue.drainTo(eventList);
        if (eventNumber == 0) {
            try {
                event = _binlogEventQueue.poll(_queueTimeoutMs, TimeUnit.MILLISECONDS);
                if (event != null) {
                    eventList.add(event);
                    eventNumber = eventList.size();
                }
            } catch (InterruptedException e) {
                _log.info("Interrupted when poll from _binlogEventQueue!!");
            }
        }
        for (int i = 0; i < eventNumber; i++) {
            event = eventList.get(i);
            if (event == null) {
                _log.error("Received null event");
                continue;
            }
            try {
                // Beginning of Txn
                if (event instanceof QueryEvent) {
                    QueryEvent qe = (QueryEvent) event;
                    String sql = qe.getSql().toString();
                    if ("BEGIN".equalsIgnoreCase(sql)) {
                        _isBeginTxnSeen = true;
                        _log.info("BEGIN sql: " + sql);
                        _currTxnSizeInBytes = event.getHeader().getEventLength();
                        startXtion(qe);
                        continue;
                    }
                } else if (event instanceof RotateEvent) {
                    RotateEvent re = (RotateEvent) event;
                    String fileName = re.getBinlogFileName().toString();
                    _log.info("File Rotated : FileName :" + fileName + ", _binlogFilePrefix :" + _binlogFilePrefix);
                    String fileNumStr = fileName.substring(fileName.lastIndexOf(_binlogFilePrefix) + _binlogFilePrefix.length() + 1);
                    _currFileNum = Integer.parseInt(fileNumStr);
                    _tableMapEvents.clear();
                    continue;
                }
                if (!_isBeginTxnSeen) {
                    if (_log.isDebugEnabled()) {
                        _log.debug("Skipping event (" + event + ") as this is before the start of first transaction");
                    }
                    continue;
                }
                _currTxnSizeInBytes += event.getHeader().getEventLength();
                if (event instanceof QueryEvent) {
                    QueryEvent qe = (QueryEvent) event;
                    String sql = qe.getSql().toString();
                    if ("COMMIT".equalsIgnoreCase(sql)) {
                        _log.debug("COMMIT sql: " + sql);
                        endXtion(qe);
                        continue;
                    } else if ("ROLLBACK".equalsIgnoreCase(sql)) {
                        _log.debug("ROLLBACK sql: " + sql);
                        rollbackXtion(qe);
                        continue;
                    } else {
                        // Ignore DDL statements for now
                        _log.debug("Likely DDL statement sql: " + sql);
                        continue;
                    }
                } else if (event instanceof XidEvent) {
                    XidEvent xe = (XidEvent) event;
                    long xid = xe.getXid();
                    _log.debug("Treating XID event with xid = " + xid + " as commit for the transaction");
                    endXtion(xe);
                    continue;
                } else if (event instanceof FormatDescriptionEvent) {
                    // we don't need process this event
                    _log.info("received FormatDescriptionEvent event");
                    continue;
                } else if (event instanceof WriteRowsEvent) {
                    WriteRowsEvent wre = (WriteRowsEvent) event;
                    insertRows(wre);
                } else if (event instanceof WriteRowsEventV2) {
                    WriteRowsEventV2 wre = (WriteRowsEventV2) event;
                    insertRows(wre);
                } else if (event instanceof UpdateRowsEvent) {
                    UpdateRowsEvent ure = (UpdateRowsEvent) event;
                    updateRows(ure);
                } else if (event instanceof UpdateRowsEventV2) {
                    UpdateRowsEventV2 ure = (UpdateRowsEventV2) event;
                    updateRows(ure);
                } else if (event instanceof DeleteRowsEventV2) {
                    DeleteRowsEventV2 dre = (DeleteRowsEventV2) event;
                    deleteRows(dre);
                } else if (event instanceof DeleteRowsEvent) {
                    DeleteRowsEvent dre = (DeleteRowsEvent) event;
                    deleteRows(dre);
                } else if (event instanceof TableMapEvent) {
                    TableMapEvent tme = (TableMapEvent) event;
                    processTableMapEvent(tme);
                } else {
                    _log.warn("Skipping !! Unknown OR event e: " + event);
                    continue;
                }
                if (_log.isDebugEnabled()) {
                    _log.debug("e: " + event);
                }
            } catch (Exception e) {
                _log.error("failed to process binlog event, event: " + event, e);
            }
        }
    }
    _log.info("ORListener Thread done");
    doShutdownNotify();
}
Also used : RotateEvent(com.google.code.or.binlog.impl.event.RotateEvent) DeleteRowsEvent(com.google.code.or.binlog.impl.event.DeleteRowsEvent) TableMapEvent(com.google.code.or.binlog.impl.event.TableMapEvent) FormatDescriptionEvent(com.google.code.or.binlog.impl.event.FormatDescriptionEvent) WriteRowsEvent(com.google.code.or.binlog.impl.event.WriteRowsEvent) ArrayList(java.util.ArrayList) BinlogEventV4(com.google.code.or.binlog.BinlogEventV4) AbstractBinlogEventV4(com.google.code.or.binlog.impl.event.AbstractBinlogEventV4) QueryEvent(com.google.code.or.binlog.impl.event.QueryEvent) NoSuchSchemaException(com.linkedin.databus2.schemas.NoSuchSchemaException) DatabusException(com.linkedin.databus2.core.DatabusException) DatabusRuntimeException(com.linkedin.databus.core.DatabusRuntimeException) WriteRowsEventV2(com.google.code.or.binlog.impl.event.WriteRowsEventV2) UpdateRowsEvent(com.google.code.or.binlog.impl.event.UpdateRowsEvent) XidEvent(com.google.code.or.binlog.impl.event.XidEvent) UpdateRowsEventV2(com.google.code.or.binlog.impl.event.UpdateRowsEventV2) DeleteRowsEventV2(com.google.code.or.binlog.impl.event.DeleteRowsEventV2)

Example 2 with TableMapEvent

use of com.google.code.or.binlog.impl.event.TableMapEvent in project databus by linkedin.

the class ORListener method frameAvroRecord.

private void frameAvroRecord(long tableId, BinlogEventV4Header bh, List<Row> rl, final DbusOpcode doc) {
    try {
        final long timestampInNanos = bh.getTimestamp() * 1000000L;
        final long scn = scn(_currFileNum, (int) bh.getPosition());
        final boolean isReplicated = false;
        final TableMapEvent tme = _tableMapEvents.get(tableId);
        String tableName = tme.getDatabaseName().toString().toLowerCase() + "." + tme.getTableName().toString().toLowerCase();
        VersionedSchema vs = _schemaRegistryService.fetchLatestVersionedSchemaBySourceName(_tableUriToSrcNameMap.get(tableName));
        Schema schema = vs.getSchema();
        if (_log.isDebugEnabled())
            _log.debug("File Number :" + _currFileNum + ", Position :" + (int) bh.getPosition() + ", SCN =" + scn);
        for (Row r : rl) {
            List<Column> cl = r.getColumns();
            GenericRecord gr = new GenericData.Record(schema);
            generateAvroEvent(schema, cl, gr);
            List<KeyPair> kps = generateKeyPair(cl, schema);
            DbChangeEntry db = new DbChangeEntry(scn, timestampInNanos, gr, doc, isReplicated, schema, kps);
            _transaction.getPerSourceTransaction(_tableUriToSrcIdMap.get(tableName)).mergeDbChangeEntrySet(db);
        }
    } catch (NoSuchSchemaException ne) {
        throw new DatabusRuntimeException(ne);
    } catch (DatabusException de) {
        throw new DatabusRuntimeException(de);
    }
}
Also used : TableMapEvent(com.google.code.or.binlog.impl.event.TableMapEvent) KeyPair(com.linkedin.databus2.producers.ds.KeyPair) Schema(org.apache.avro.Schema) PrimaryKeySchema(com.linkedin.databus2.producers.ds.PrimaryKeySchema) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) DbChangeEntry(com.linkedin.databus2.producers.ds.DbChangeEntry) NoSuchSchemaException(com.linkedin.databus2.schemas.NoSuchSchemaException) DatabusException(com.linkedin.databus2.core.DatabusException) FloatColumn(com.google.code.or.common.glossary.column.FloatColumn) SetColumn(com.google.code.or.common.glossary.column.SetColumn) DatetimeColumn(com.google.code.or.common.glossary.column.DatetimeColumn) EnumColumn(com.google.code.or.common.glossary.column.EnumColumn) TimestampColumn(com.google.code.or.common.glossary.column.TimestampColumn) TimeColumn(com.google.code.or.common.glossary.column.TimeColumn) BitColumn(com.google.code.or.common.glossary.column.BitColumn) LongColumn(com.google.code.or.common.glossary.column.LongColumn) YearColumn(com.google.code.or.common.glossary.column.YearColumn) Datetime2Column(com.google.code.or.common.glossary.column.Datetime2Column) DecimalColumn(com.google.code.or.common.glossary.column.DecimalColumn) DoubleColumn(com.google.code.or.common.glossary.column.DoubleColumn) LongLongColumn(com.google.code.or.common.glossary.column.LongLongColumn) TinyColumn(com.google.code.or.common.glossary.column.TinyColumn) Column(com.google.code.or.common.glossary.Column) StringColumn(com.google.code.or.common.glossary.column.StringColumn) Int24Column(com.google.code.or.common.glossary.column.Int24Column) BlobColumn(com.google.code.or.common.glossary.column.BlobColumn) NullColumn(com.google.code.or.common.glossary.column.NullColumn) ShortColumn(com.google.code.or.common.glossary.column.ShortColumn) DateColumn(com.google.code.or.common.glossary.column.DateColumn) GenericRecord(org.apache.avro.generic.GenericRecord) Row(com.google.code.or.common.glossary.Row) GenericRecord(org.apache.avro.generic.GenericRecord) DatabusRuntimeException(com.linkedin.databus.core.DatabusRuntimeException)

Aggregations

TableMapEvent (com.google.code.or.binlog.impl.event.TableMapEvent)2 DatabusRuntimeException (com.linkedin.databus.core.DatabusRuntimeException)2 DatabusException (com.linkedin.databus2.core.DatabusException)2 NoSuchSchemaException (com.linkedin.databus2.schemas.NoSuchSchemaException)2 BinlogEventV4 (com.google.code.or.binlog.BinlogEventV4)1 AbstractBinlogEventV4 (com.google.code.or.binlog.impl.event.AbstractBinlogEventV4)1 DeleteRowsEvent (com.google.code.or.binlog.impl.event.DeleteRowsEvent)1 DeleteRowsEventV2 (com.google.code.or.binlog.impl.event.DeleteRowsEventV2)1 FormatDescriptionEvent (com.google.code.or.binlog.impl.event.FormatDescriptionEvent)1 QueryEvent (com.google.code.or.binlog.impl.event.QueryEvent)1 RotateEvent (com.google.code.or.binlog.impl.event.RotateEvent)1 UpdateRowsEvent (com.google.code.or.binlog.impl.event.UpdateRowsEvent)1 UpdateRowsEventV2 (com.google.code.or.binlog.impl.event.UpdateRowsEventV2)1 WriteRowsEvent (com.google.code.or.binlog.impl.event.WriteRowsEvent)1 WriteRowsEventV2 (com.google.code.or.binlog.impl.event.WriteRowsEventV2)1 XidEvent (com.google.code.or.binlog.impl.event.XidEvent)1 Column (com.google.code.or.common.glossary.Column)1 Row (com.google.code.or.common.glossary.Row)1 BitColumn (com.google.code.or.common.glossary.column.BitColumn)1 BlobColumn (com.google.code.or.common.glossary.column.BlobColumn)1