Search in sources :

Example 91 with DatabusException

use of com.linkedin.databus2.core.DatabusException in project databus by linkedin.

the class GoldenGateEventProducer method obtainKey.

/**
   * Given a DBImage, returns the key
   * If it is a single key, it returns the object if it is LONG /INT / STRING
   * For compound key, it casts the fields as String, delimits the fields and returns the appended string
   * @param dbUpdate The post-image of the event
   * @return Actual key object
   * @throws DatabusException
   */
protected static Object obtainKey(DbUpdateState.DBUpdateImage dbUpdate) throws DatabusException {
    if (null == dbUpdate) {
        throw new DatabusException("DBUpdateImage is null");
    }
    List<KeyPair> pairs = dbUpdate.getKeyPairs();
    if (null == pairs || pairs.size() == 0) {
        throw new DatabusException("There do not seem to be any keys");
    }
    if (pairs.size() == 1) {
        Object key = dbUpdate.getKeyPairs().get(0).getKey();
        Schema.Type pKeyType = dbUpdate.getKeyPairs().get(0).getKeyType();
        Object keyObj = null;
        if (pKeyType == Schema.Type.INT) {
            if (key instanceof Integer) {
                keyObj = key;
            } else {
                throw new DatabusException("Schema.Type does not match actual key type (INT) " + key.getClass().getName());
            }
        } else if (pKeyType == Schema.Type.LONG) {
            if (key instanceof Long) {
                keyObj = key;
            } else {
                throw new DatabusException("Schema.Type does not match actual key type (LONG) " + key.getClass().getName());
            }
            keyObj = key;
        } else {
            keyObj = key;
        }
        return keyObj;
    } else {
        // Treat multiple keys as a separate case to avoid unnecessary casts
        Iterator<KeyPair> li = pairs.iterator();
        String compositeKey = "";
        while (li.hasNext()) {
            KeyPair kp = li.next();
            Schema.Type pKeyType = kp.getKeyType();
            Object key = kp.getKey();
            if (pKeyType == Schema.Type.INT) {
                if (key instanceof Integer)
                    compositeKey += kp.getKey().toString();
                else
                    throw new DatabusException("Schema.Type does not match actual key type (INT) " + key.getClass().getName());
            } else if (pKeyType == Schema.Type.LONG) {
                if (key instanceof Long)
                    compositeKey += key.toString();
                else
                    throw new DatabusException("Schema.Type does not match actual key type (LONG) " + key.getClass().getName());
            } else {
                compositeKey += key;
            }
            if (li.hasNext()) {
                // Add the delimiter for all keys except the last key
                compositeKey += DbusConstants.COMPOUND_KEY_DELIMITER;
            }
        }
        return compositeKey;
    }
}
Also used : KeyPair(com.linkedin.databus2.ggParser.XmlStateMachine.ColumnsState.KeyPair) DatabusException(com.linkedin.databus2.core.DatabusException) Schema(org.apache.avro.Schema) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 92 with DatabusException

use of com.linkedin.databus2.core.DatabusException in project databus by linkedin.

the class GoldenGateEventProducer method registerMbeans.

// register each source with the mbeanServer
private void registerMbeans(GGMonitoredSourceInfo source) throws DatabusException {
    try {
        Hashtable<String, String> props = new Hashtable<String, String>();
        props.put("type", "SourceStatistics");
        props.put("name", source.getSourceName());
        ObjectName objectName = new ObjectName(ServerContainer.JMX_DOMAIN, props);
        if (_mbeanServer.isRegistered(objectName)) {
            _log.warn("Unregistering old gg-source statistics mbean: " + objectName);
            _mbeanServer.unregisterMBean(objectName);
        }
        _mbeanServer.registerMBean(source.getStatisticsBean(), objectName);
        _log.info("Registered gg-source statistics mbean: " + objectName);
        _registeredMbeans.add(objectName);
    } catch (Exception ex) {
        _log.error("Failed to register the gg-source statistics mbean for source (" + source.getSourceName() + ") due to an exception.", ex);
        throw new DatabusException("Failed to initialize gg event statistics mbeans.", ex);
    }
}
Also used : DatabusException(com.linkedin.databus2.core.DatabusException) Hashtable(java.util.Hashtable) XMLStreamException(javax.xml.stream.XMLStreamException) NotImplementedException(org.apache.commons.lang.NotImplementedException) DatabusException(com.linkedin.databus2.core.DatabusException) InvalidConfigException(com.linkedin.databus.core.util.InvalidConfigException) UnsupportedKeyException(com.linkedin.databus.core.UnsupportedKeyException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) EventCreationException(com.linkedin.databus2.producers.EventCreationException) IOException(java.io.IOException) ObjectName(javax.management.ObjectName)

Example 93 with DatabusException

use of com.linkedin.databus2.core.DatabusException in project databus by linkedin.

the class AbstractStateTransitionProcessor method validateNextTransition.

private void validateNextTransition(StateMachine stateMachine, String currentState, StateProcessor.STATETYPE currentStateType, String nextState, StateProcessor.STATETYPE nextStateType) throws DatabusException {
    TransitionElement startTransition = new TransitionElement(currentStateType, currentState);
    TransitionElement endTransition = new TransitionElement(nextStateType, nextState);
    HashSet<TransitionElement> expectedTranstitions = _transitionMapping.get(startTransition);
    if (expectedTranstitions == null)
        throw new DatabusException("No mapping found for this particular state " + startTransition + ". The state machine does not know the expected transitions");
    if (!expectedTranstitions.contains(endTransition)) {
        throw new DatabusException("The current state is : " + startTransition + " the expected state was: " + expectedTranstitions + ". The next state found was: " + endTransition);
    }
}
Also used : DatabusException(com.linkedin.databus2.core.DatabusException)

Example 94 with DatabusException

use of com.linkedin.databus2.core.DatabusException in project databus by linkedin.

the class ColumnsState method generateAvroRecord.

/**
   * Convert the eventFields read from the xml to an avro record
   * @param eventFields hashmap <database field, value>
   * @return
   */
private GenericRecord generateAvroRecord(HashMap<String, ColumnState.EventField> eventFields, ReplicationBitSetterStaticConfig replicationBitConfig, Pattern replicationValuePattern) throws Exception {
    GenericRecord record = new GenericData.Record(getCurrentSchema());
    List<Schema.Field> fields = getCurrentSchema().getFields();
    String pkFieldName = SchemaHelper.getMetaField(getCurrentSchema(), "pk");
    if (pkFieldName == null)
        throw new DatabusException("No primary key specified in the schema");
    PrimaryKey pk = new PrimaryKey(pkFieldName);
    for (Schema.Field field : fields) {
        if (field.schema().getType() == Schema.Type.ARRAY) {
            throw new DatabusException("The gg parser cannot handle ARRAY datatypes. Found in field: " + field);
        } else {
            //The current database field being processed (field is avro field name  and databaseFieldName is oracle field name (one to one mapping)
            String databaseFieldName = SchemaHelper.getMetaField(field, "dbFieldName");
            //Check if it's ok for this field to be null
            checkNullSafety(eventFields, pk, field, databaseFieldName, replicationBitConfig);
            //Insert the field into the generic record
            String fieldValue = insertFieldIntoRecord(eventFields, record, pkFieldName, pk, field, databaseFieldName);
            //Set the replication flag if this is a replicated event
            if (replicationBitConfig.getSourceType() == ReplicationBitSetterStaticConfig.SourceType.COLUMN && isReplicationField(databaseFieldName, replicationBitConfig)) {
                setReplicated(StateMachineHelper.verifyReplicationStatus(replicationValuePattern, fieldValue, replicationBitConfig.getMissingValueBehavior()));
            }
        }
    }
    //prettyPrint(record);     //TODO remove this -> only for debugging
    return record;
}
Also used : DatabusException(com.linkedin.databus2.core.DatabusException) Schema(org.apache.avro.Schema) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 95 with DatabusException

use of com.linkedin.databus2.core.DatabusException in project databus by linkedin.

the class ColumnsState method checkNullSafety.

/**
   * Checks if the given field can be null.
   * 1. Primary keys cannot be null
   * 2. Replication fields cannot be null
   * 3. If the errorOnMissing field config is null, then this method will throw an exception
   * @param eventFields Map containing dbFieldName => fieldValue
   * @param pk The primary key object stored as an class object
   * @param field The current field being processed(Avro)
   * @param databaseFieldName Field being processed(oracle name)
   * @throws DatabusException
   */
private void checkNullSafety(HashMap<String, ColumnState.EventField> eventFields, PrimaryKey pk, Schema.Field field, String databaseFieldName, ReplicationBitSetterStaticConfig replicationBitConfig) throws DatabusException {
    if (eventFields.get(databaseFieldName) == null) {
        LOG.error("Missing field " + databaseFieldName + " in event from the xml trail for table " + _currentTable);
        if (//Are we ok to accept null fields ?
        !_errorOnMissingFields) {
            //We cannot tolerate empty primary key fields, so we'll throw exception if key is null
            if (pk.isPartOfPrimaryKey(field))
                throw new DatabusException("Skip errors on missing DB Fields is true, but cannot proceed because primary key not found: " + field.name());
            //We also need the replication field, it's not optional if MissingValueBehavior == STOP_WITH_ERROR
            if (replicationBitConfig.getSourceType() == ReplicationBitSetterStaticConfig.SourceType.COLUMN && isReplicationField(databaseFieldName, replicationBitConfig) && replicationBitConfig.getMissingValueBehavior() == MissingValueBehavior.STOP_WITH_ERROR) {
                throw new DatabusException("Skip errors on missing DB Fields is true, but the replication field is missing, this is mandatory, cannot proceed with  " + field.name() + " field missing");
            }
            setSeenMissingFields(true);
            //If not primary key, we create fake hash entry with a null eventfield entry
            ColumnState.EventField emptyEventField = new ColumnState.EventField(false, null, true);
            eventFields.put(databaseFieldName, emptyEventField);
        } else
            throw new DatabusException("Unable to find a required field " + databaseFieldName + " in the xml trail file");
    }
}
Also used : DatabusException(com.linkedin.databus2.core.DatabusException)

Aggregations

DatabusException (com.linkedin.databus2.core.DatabusException)76 Test (org.testng.annotations.Test)21 ArrayList (java.util.ArrayList)19 IOException (java.io.IOException)14 Schema (org.apache.avro.Schema)14 ConditionCheck (com.linkedin.databus2.test.ConditionCheck)13 Logger (org.apache.log4j.Logger)13 InvalidConfigException (com.linkedin.databus.core.util.InvalidConfigException)12 Channel (org.jboss.netty.channel.Channel)12 DefaultHttpRequest (org.jboss.netty.handler.codec.http.DefaultHttpRequest)11 PhysicalPartition (com.linkedin.databus.core.data_model.PhysicalPartition)10 UnsupportedKeyException (com.linkedin.databus.core.UnsupportedKeyException)9 VersionedSchema (com.linkedin.databus2.schemas.VersionedSchema)9 InetSocketAddress (java.net.InetSocketAddress)9 SocketAddress (java.net.SocketAddress)9 SQLException (java.sql.SQLException)9 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)9 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)9 EventCreationException (com.linkedin.databus2.producers.EventCreationException)7 PhysicalSourceStaticConfig (com.linkedin.databus2.relay.config.PhysicalSourceStaticConfig)7