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