Search in sources :

Example 6 with SchemaId

use of com.linkedin.databus2.schemas.SchemaId in project databus by linkedin.

the class DbusEventAvroDecoder method getGenericRecord.

@Override
public GenericRecord getGenericRecord(DbusEvent e, GenericRecord reuse) {
    byte[] md5 = new byte[16];
    e.schemaId(md5);
    SchemaId schemaId = new SchemaId(md5);
    VersionedSchema writerSchema = _schemaSet.getById(schemaId);
    if (null == writerSchema) {
        LOG.error("Unable to find schema for id " + schemaId + "; event = " + e);
        throw new DatabusRuntimeException("No schema available to decode event " + e);
    }
    ByteBuffer valueBuffer = e.value();
    byte[] valueBytes = null;
    if (valueBuffer.hasArray()) {
        valueBytes = valueBuffer.array();
    } else {
        valueBytes = new byte[valueBuffer.remaining()];
        valueBuffer.get(valueBytes);
    }
    return getGenericRecord(valueBytes, writerSchema.getSchema(), reuse);
}
Also used : SchemaId(com.linkedin.databus2.schemas.SchemaId) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema) ByteBuffer(java.nio.ByteBuffer) DatabusRuntimeException(com.linkedin.databus.core.DatabusRuntimeException)

Example 7 with SchemaId

use of com.linkedin.databus2.schemas.SchemaId in project databus by linkedin.

the class DbusEventAvroDecoder method getPayloadSchema.

@Override
public /**
   * @param e DatabusEvent
   * @return Avro Schema, sourceName, version tuple describing the payload data appearing in 'e'.
   */
VersionedSchema getPayloadSchema(DbusEvent e) {
    byte[] md5 = new byte[16];
    e.schemaId(md5);
    SchemaId schemaId = new SchemaId(md5);
    VersionedSchema writerSchema = _schemaSet.getById(schemaId);
    return writerSchema;
}
Also used : SchemaId(com.linkedin.databus2.schemas.SchemaId) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema)

Example 8 with SchemaId

use of com.linkedin.databus2.schemas.SchemaId in project databus by linkedin.

the class DispatcherState method refreshSchemas.

private void refreshSchemas(List<RegisterResponseMetadataEntry> metadataSchemaList) {
    final boolean isDebugEnabled = LOG.isDebugEnabled();
    try {
        for (Map.Entry<Long, List<RegisterResponseEntry>> e : _payloadSchemaMap.entrySet()) {
            for (RegisterResponseEntry r : e.getValue()) {
                final long id = r.getId();
                String schemaName = null;
                if (_sources.containsKey(id)) {
                    schemaName = _sources.get(r.getId()).getName();
                } else {
                    LOG.error("Obtained a RegisterResponseEntry with schema that has no sourceId set. id = " + id);
                    continue;
                }
                String schema = r.getSchema();
                if (_schemaSet.add(schemaName, r.getVersion(), schema)) {
                    LOG.info("Registering schema name=" + schemaName + " id=" + e.getKey().toString() + " version=" + r.getVersion());
                    if (isDebugEnabled) {
                        String msg = "Registering schema name=" + schemaName + " id=" + e.getKey().toString() + " version=" + r.getVersion() + ": " + schema;
                        DbusLogAccumulator.addLog(msg, LOG);
                    }
                } else {
                    if (isDebugEnabled) {
                        String msg = "Schema already known: " + schemaName + " version " + r.getId();
                        DbusLogAccumulator.addLog(msg, LOG);
                    }
                }
            }
        }
        //Refresh metadata schema map
        if ((metadataSchemaList != null) && !metadataSchemaList.isEmpty()) {
            for (RegisterResponseMetadataEntry e : metadataSchemaList) {
                SchemaId id = new SchemaId(e.getCrc32());
                if (_metadataSchemasSet.add(SchemaRegistryService.DEFAULT_METADATA_SCHEMA_SOURCE, e.getVersion(), id, e.getSchema())) {
                    LOG.info("Added metadata schema version " + e.getVersion() + ",schemaID=0x" + id);
                } else {
                    if (isDebugEnabled) {
                        String msg = "Metadata schema version " + e.getVersion() + ",schemaId=0x" + id + " already exists";
                        DbusLogAccumulator.addLog(msg, LOG);
                    }
                }
            }
        } else {
            if (isDebugEnabled) {
                String msg = "Metadata schema is empty";
                DbusLogAccumulator.addLog(msg, LOG);
            }
        }
        _eventDecoder = new DbusEventAvroDecoder(_schemaSet, _metadataSchemasSet);
    } catch (Exception e) {
        LOG.error("Error adding schema", e);
    }
}
Also used : RegisterResponseMetadataEntry(com.linkedin.databus2.core.container.request.RegisterResponseMetadataEntry) RegisterResponseEntry(com.linkedin.databus2.core.container.request.RegisterResponseEntry) SchemaId(com.linkedin.databus2.schemas.SchemaId) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with SchemaId

use of com.linkedin.databus2.schemas.SchemaId in project databus by linkedin.

the class GenericDispatcher method doCheckStartSource.

protected boolean doCheckStartSource(DispatcherState curState, Long eventSrcId, SchemaId schemaId) {
    boolean success = true;
    if (eventSrcId >= 0) {
        IdNamePair source = curState.getSources().get(eventSrcId);
        if (null == source) {
            _log.error("Unable to find source: srcid=" + eventSrcId);
            success = false;
        } else {
            VersionedSchema verSchema = curState.getSchemaSet().getLatestVersionByName(source.getName());
            VersionedSchema exactSchema = _schemaIdCheck ? curState.getSchemaSet().getById(schemaId) : null;
            if (null == verSchema) {
                _log.error("Unable to find schema: srcid=" + source.getId() + " name=" + source.getName());
                success = false;
            } else if (_schemaIdCheck && null == exactSchema) {
                _log.error("Unable to find schema: srcid=" + source.getId() + " name=" + source.getName() + " schemaId=" + schemaId);
                success = false;
            } else if (verSchema.getSchema() != curState.getCurrentSourceSchema()) {
                curState.switchToStartStreamSource(source, verSchema.getSchema());
                success = doStartStreamSource(curState);
            }
        }
    }
    return success;
}
Also used : IdNamePair(com.linkedin.databus.core.util.IdNamePair) VersionedSchema(com.linkedin.databus2.schemas.VersionedSchema)

Example 10 with SchemaId

use of com.linkedin.databus2.schemas.SchemaId in project databus by linkedin.

the class TestInternalMetadata method testGetMetadata_UnhappyPath_EventHasNoMetadata.

/**
   * Verifies that getMetadata() returns null if event has no metadata.
   */
@Test
public void testGetMetadata_UnhappyPath_EventHasNoMetadata() throws Exception {
    LOG.info("starting testGetMetadata_UnhappyPath_EventHasNoMetadata()");
    // build the event without any metadata
    DbusEvent event = createEvent(null);
    // create a metadata schema set, just because we like to
    VersionedSchemaSet metadataSchemaSet = new VersionedSchemaSet();
    metadataSchemaSet.add(SchemaRegistryService.DEFAULT_METADATA_SCHEMA_SOURCE, METADATA_SCHEMA_VERSION, new SchemaId(METADATA_SCHEMA_CHECKSUM), CORRECT_METADATA_SCHEMA, // preserve original string
    true);
    // now create the decoder and attempt to use it to extract and decode the event's metadata
    DbusEventAvroDecoder eventDecoder = createDecoder(metadataSchemaSet);
    try {
        GenericRecord reuse = null;
        GenericRecord decodedMetadata = eventDecoder.getMetadata(event, reuse);
        Assert.assertNull(decodedMetadata, "getMetadata() should have returned null;");
    } catch (Exception ex) {
        Assert.fail("getMetadata() should not have thrown exception: " + ex);
    }
    LOG.info("leaving testGetMetadata_UnhappyPath_EventHasNoMetadata()");
}
Also used : DbusEvent(com.linkedin.databus.core.DbusEvent) DbusEventAvroDecoder(com.linkedin.databus.client.DbusEventAvroDecoder) SchemaId(com.linkedin.databus2.schemas.SchemaId) VersionedSchemaSet(com.linkedin.databus2.schemas.VersionedSchemaSet) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.testng.annotations.Test)

Aggregations

SchemaId (com.linkedin.databus2.schemas.SchemaId)14 VersionedSchema (com.linkedin.databus2.schemas.VersionedSchema)6 VersionedSchemaSet (com.linkedin.databus2.schemas.VersionedSchemaSet)5 Test (org.testng.annotations.Test)5 IOException (java.io.IOException)4 ByteBuffer (java.nio.ByteBuffer)4 GenericRecord (org.apache.avro.generic.GenericRecord)4 DbusEventAvroDecoder (com.linkedin.databus.client.DbusEventAvroDecoder)3 DbusEvent (com.linkedin.databus.core.DbusEvent)3 DbusEventInfo (com.linkedin.databus.core.DbusEventInfo)3 DbusEventKey (com.linkedin.databus.core.DbusEventKey)3 DatabusException (com.linkedin.databus2.core.DatabusException)3 RegisterResponseMetadataEntry (com.linkedin.databus2.core.container.request.RegisterResponseMetadataEntry)3 HashMap (java.util.HashMap)3 DatabusRuntimeException (com.linkedin.databus.core.DatabusRuntimeException)2 DbusEventPart (com.linkedin.databus.core.DbusEventPart)2 IdNamePair (com.linkedin.databus.core.util.IdNamePair)2 RegisterResponseEntry (com.linkedin.databus2.core.container.request.RegisterResponseEntry)2 DatabusSourcesConnection (com.linkedin.databus.client.DatabusSourcesConnection)1 SCN (com.linkedin.databus.client.pub.SCN)1