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();
}
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);
}
}
Aggregations