Search in sources :

Example 6 with DBUpdateImage

use of com.linkedin.databus2.ggParser.XmlStateMachine.DbUpdateState.DBUpdateImage in project databus by linkedin.

the class TestGoldenGateEventProducer method generateUpdatesForSchema2.

// aux method to generate updates
private List<TransactionState.PerSourceTransactionalUpdate> generateUpdatesForSchema2(short[] sourceIds, Map<String, String> keyVals, long scn) throws DatabusException {
    List<TransactionState.PerSourceTransactionalUpdate> dbUpdates = new ArrayList<TransactionState.PerSourceTransactionalUpdate>(10);
    HashSet<DBUpdateImage> db = new HashSet<DBUpdateImage>();
    String keyName = "name1";
    String valName = "name2";
    Schema.Type keyType = Schema.Type.RECORD;
    Schema s = Schema.parse(avroSchema2);
    for (short sourceId : sourceIds) {
        for (String k : keyVals.keySet()) {
            ColumnsState.KeyPair kp = new ColumnsState.KeyPair(k, keyType);
            ArrayList<ColumnsState.KeyPair> keyPairs = new ArrayList<ColumnsState.KeyPair>(1);
            keyPairs.add(kp);
            GenericRecord gr = new GenericData.Record(s);
            gr.put(keyName, k);
            gr.put(valName, keyVals.get(k));
            DBUpdateImage dbi = new DBUpdateImage(keyPairs, scn, gr, s, DbUpdateState.DBUpdateImage.OpType.INSERT, false);
            db.add(dbi);
            TransactionState.PerSourceTransactionalUpdate dbUpdate = new TransactionState.PerSourceTransactionalUpdate(sourceId, db);
            dbUpdates.add(dbUpdate);
        }
    }
    return dbUpdates;
}
Also used : TransactionState(com.linkedin.databus2.ggParser.XmlStateMachine.TransactionState) Schema(org.apache.avro.Schema) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) ArrayList(java.util.ArrayList) DBUpdateImage(com.linkedin.databus2.ggParser.XmlStateMachine.DbUpdateState.DBUpdateImage) ColumnsState(com.linkedin.databus2.ggParser.XmlStateMachine.ColumnsState) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord) HashSet(java.util.HashSet)

Example 7 with DBUpdateImage

use of com.linkedin.databus2.ggParser.XmlStateMachine.DbUpdateState.DBUpdateImage in project databus by linkedin.

the class TestGoldenGateEventProducer method generateUpdates.

// aux method to generate updates
private List<TransactionState.PerSourceTransactionalUpdate> generateUpdates(short[] sourceIds, List<String> keys, long scn) throws DatabusException {
    List<TransactionState.PerSourceTransactionalUpdate> dbUpdates = new ArrayList<TransactionState.PerSourceTransactionalUpdate>(10);
    HashSet<DBUpdateImage> db = new HashSet<DBUpdateImage>();
    String keyName = "name";
    Schema.Type keyType = Schema.Type.RECORD;
    Schema s = Schema.parse(avroSchema);
    for (short sourceId : sourceIds) {
        for (String k : keys) {
            ColumnsState.KeyPair kp = new ColumnsState.KeyPair(k, keyType);
            ArrayList<ColumnsState.KeyPair> keyPairs = new ArrayList<ColumnsState.KeyPair>(1);
            keyPairs.add(kp);
            GenericRecord gr = new GenericData.Record(s);
            gr.put(keyName, k);
            DBUpdateImage dbi = new DBUpdateImage(keyPairs, scn, gr, s, DbUpdateState.DBUpdateImage.OpType.INSERT, false);
            db.add(dbi);
            TransactionState.PerSourceTransactionalUpdate dbUpdate = new TransactionState.PerSourceTransactionalUpdate(sourceId, db);
            dbUpdates.add(dbUpdate);
        }
    }
    return dbUpdates;
}
Also used : TransactionState(com.linkedin.databus2.ggParser.XmlStateMachine.TransactionState) Schema(org.apache.avro.Schema) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) ArrayList(java.util.ArrayList) DBUpdateImage(com.linkedin.databus2.ggParser.XmlStateMachine.DbUpdateState.DBUpdateImage) ColumnsState(com.linkedin.databus2.ggParser.XmlStateMachine.ColumnsState) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord) HashSet(java.util.HashSet)

Example 8 with DBUpdateImage

use of com.linkedin.databus2.ggParser.XmlStateMachine.DbUpdateState.DBUpdateImage in project databus by linkedin.

the class DbUpdateState method onEndElement.

/**
 * clean up the state and generate the required data structures
 * @param stateMachine
 * @param xmlStreamReader
 */
@Override
public void onEndElement(StateMachine stateMachine, XMLStreamReader xmlStreamReader) throws DatabusException, XMLStreamException {
    _currentStateType = STATETYPE.ENDELEMENT;
    // TODO construct a data structure that will hold key, csn and the avro
    GenericRecord record = stateMachine.columnsState.getGenericRecord();
    long scn = stateMachine.tokensState.getScn();
    if (_scn < scn) {
        _scn = scn;
        if (LOG.isDebugEnabled())
            LOG.debug("Setting current DbUpdates scn to " + _scn);
    }
    /**
     * The record is empty, this will happen when we have skipped the columns state
     */
    if (record == null) {
        if (LOG.isDebugEnabled())
            LOG.debug("Unable to process the current dbUpdate (record was found to be empty), skipping the dbUpdate");
        onError(stateMachine, xmlStreamReader);
        return;
    }
    if (scn == TokenState.ERRORSCN) {
        LOG.error("Unable to find scn for the given dbUpdate");
        throw new DatabusException("Unable to find scn for the given dbUpdate, terminating the parser");
    }
    // check if the current columns state has seen any missing elements, if it has, then print the current scn associated with the dbupdate for debugging purpose
    if (stateMachine.columnsState.isSeenMissingFields()) {
        LOG.error("There were missing fields seen in Columns section and the corresponding scn is : " + stateMachine.tokensState.getScn());
    }
    Boolean isReplicated = false;
    if (stateMachine.getReplicationBitConfig().getSourceType() == ReplicationBitSetterStaticConfig.SourceType.COLUMN)
        isReplicated = stateMachine.columnsState.isReplicated();
    else if (stateMachine.getReplicationBitConfig().getSourceType() == ReplicationBitSetterStaticConfig.SourceType.TOKEN)
        isReplicated = stateMachine.tokensState.isReplicated();
    else if (stateMachine.getReplicationBitConfig().getSourceType() == ReplicationBitSetterStaticConfig.SourceType.NONE)
        isReplicated = false;
    else
        throw new DatabusException("Unknown source type specified in replicationBitConfig, expected COLUMN or TOKEN or NONE");
    DBUpdateImage eventImage = new DBUpdateImage(stateMachine.columnsState.getKeyPairs(), stateMachine.tokensState.getScn(), stateMachine.columnsState.getGenericRecord(), stateMachine.columnsState.getCurrentSchema(), _opType, isReplicated);
    Integer sourceId = stateMachine.getTableToSourceId().get(_currentTable);
    if (sourceId == null) {
        LOG.error("The table " + _currentTable + " does not have a sourceId, the current dbUpdate cannot be processed.");
        onError(stateMachine, xmlStreamReader);
        return;
    }
    if (getHashSet(sourceId) == null) {
        LOG.error("The hashset is empty, cannot proceed without a valid hashset");
        throw new DatabusException("Error while creating hashset for storing dbUpdates");
    }
    // The equals method of the hashset is overridden to compare only the key, thereby ensuring the latest update on the key
    if (getHashSet(sourceId) != null && getHashSet(sourceId).contains(eventImage))
        getHashSet(sourceId).remove(eventImage);
    getHashSet(sourceId).add(eventImage);
    stateMachine.columnsState.cleanUpState(stateMachine, xmlStreamReader);
    stateMachine.tokensState.cleanUpState(stateMachine, xmlStreamReader);
    xmlStreamReader.nextTag();
    setNextStateProcessor(stateMachine, xmlStreamReader);
}
Also used : DatabusException(com.linkedin.databus2.core.DatabusException) GenericRecord(org.apache.avro.generic.GenericRecord)

Aggregations

Schema (org.apache.avro.Schema)6 GenericRecord (org.apache.avro.generic.GenericRecord)6 TransactionState (com.linkedin.databus2.ggParser.XmlStateMachine.TransactionState)5 ColumnsState (com.linkedin.databus2.ggParser.XmlStateMachine.ColumnsState)4 DBUpdateImage (com.linkedin.databus2.ggParser.XmlStateMachine.DbUpdateState.DBUpdateImage)4 VersionedSchema (com.linkedin.databus2.schemas.VersionedSchema)4 ArrayList (java.util.ArrayList)4 HashSet (java.util.HashSet)4 DatabusException (com.linkedin.databus2.core.DatabusException)3 TransactionInfo (com.linkedin.databus.monitoring.mbean.GGParserStatistics.TransactionInfo)2 PhysicalSourceStaticConfig (com.linkedin.databus2.relay.config.PhysicalSourceStaticConfig)2 DbusEvent (com.linkedin.databus.core.DbusEvent)1 DbusEventBuffer (com.linkedin.databus.core.DbusEventBuffer)1 DbusEventIterator (com.linkedin.databus.core.DbusEventBuffer.DbusEventIterator)1 DbusEventBufferAppendable (com.linkedin.databus.core.DbusEventBufferAppendable)1 KeyPair (com.linkedin.databus2.ggParser.XmlStateMachine.ColumnsState.KeyPair)1 DbUpdateState (com.linkedin.databus2.ggParser.XmlStateMachine.DbUpdateState)1 KeyPair (com.linkedin.databus2.producers.ds.KeyPair)1 PrimaryKeySchema (com.linkedin.databus2.producers.ds.PrimaryKeySchema)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1