Search in sources :

Example 6 with DbusEventAvroDecoder

use of com.linkedin.databus.client.DbusEventAvroDecoder in project databus by linkedin.

the class TestInternalMetadata method testGetMetadata_UnhappyPath_MissingSchema.

/**
   * Verifies that getMetadata() throws an exception if the metadata schema specified
   * in the event header is unavailable.
   */
@Test
public void testGetMetadata_UnhappyPath_MissingSchema() throws Exception {
    LOG.info("starting testGetMetadata_UnhappyPath_MissingSchema()");
    // build the event's metadata and then the event
    DbusEventPart metadataPart = createMetadataPart();
    DbusEvent event = createEvent(metadataPart);
    // create an empty metadata schema set
    VersionedSchemaSet metadataSchemaSet = new VersionedSchemaSet();
    // 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.fail("getMetadata() should have thrown exception");
    } catch (Exception ex) {
    // expected case:  event had metadata, but schema to decode it was missing
    }
    LOG.info("leaving testGetMetadata_UnhappyPath_MissingSchema()");
}
Also used : DbusEventPart(com.linkedin.databus.core.DbusEventPart) DbusEvent(com.linkedin.databus.core.DbusEvent) DbusEventAvroDecoder(com.linkedin.databus.client.DbusEventAvroDecoder) VersionedSchemaSet(com.linkedin.databus2.schemas.VersionedSchemaSet) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.testng.annotations.Test)

Example 7 with DbusEventAvroDecoder

use of com.linkedin.databus.client.DbusEventAvroDecoder in project databus by linkedin.

the class TestInternalMetadata method testGetMetadata_UnhappyPath_BadSchema.

/**
   * Verifies that getMetadata() returns null if there's a mismatch between the event's metadata
   * and the metadata schema whose signature/checksum is specified in the event header.
   */
@Test
public void testGetMetadata_UnhappyPath_BadSchema() throws Exception {
    LOG.info("starting testGetMetadata_UnhappyPath_BadSchema()");
    // build the event's metadata and then the event
    DbusEventPart metadataPart = createMetadataPart();
    DbusEvent event = createEvent(metadataPart);
    // create a metadata schema set with a schema that claims to match the event's
    // metadata but doesn't actually
    VersionedSchemaSet metadataSchemaSet = new VersionedSchemaSet();
    metadataSchemaSet.add(SchemaRegistryService.DEFAULT_METADATA_SCHEMA_SOURCE, // METADATA_SCHEMA_VERSION
    metadataPart.getSchemaVersion(), // METADATA_SCHEMA_CHECKSUM
    new SchemaId(metadataPart.getSchemaDigest()), INCORRECT_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_BadSchema()");
}
Also used : DbusEventPart(com.linkedin.databus.core.DbusEventPart) 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)

Example 8 with DbusEventAvroDecoder

use of com.linkedin.databus.client.DbusEventAvroDecoder in project databus by linkedin.

the class TestInternalMetadata method testGetMetadata_HappyPath.

/**
   * Verifies that getMetadata() returns the expected GenericRecord for the event's
   * metadata and that it has the expected fields and values in it.
   */
@Test
public void testGetMetadata_HappyPath() throws Exception {
    LOG.info("starting testGetMetadata_HappyPath()");
    // build the event's metadata and then the event
    DbusEventPart metadataPart = createMetadataPart();
    DbusEvent event = createEvent(metadataPart);
    // create a metadata schema set that correctly corresponds to the metadata
    VersionedSchemaSet metadataSchemaSet = new VersionedSchemaSet();
    metadataSchemaSet.add(SchemaRegistryService.DEFAULT_METADATA_SCHEMA_SOURCE, // METADATA_SCHEMA_VERSION
    metadataPart.getSchemaVersion(), // METADATA_SCHEMA_CHECKSUM
    new SchemaId(metadataPart.getSchemaDigest()), CORRECT_METADATA_SCHEMA, // preserve original string
    true);
    // now create the decoder and 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.assertNotNull(decodedMetadata, "getMetadata() returned null GenericRecord;");
        Utf8 etag = (Utf8) decodedMetadata.get("etag");
        Assert.assertEquals(etag.toString(), "dunno what an etag is");
        Integer flags = (Integer) decodedMetadata.get("flags");
        Assert.assertEquals(flags, null, "expected flags to be null");
        Long expires = (Long) decodedMetadata.get("expires");
        Assert.assertNotNull(expires, "expected expires to have a value;");
        Assert.assertEquals(expires.longValue(), 1366150681);
        Utf8 nonexistentField = (Utf8) decodedMetadata.get("nonexistentField");
        Assert.assertNull(nonexistentField, "unexpected value for 'nonexistentField';");
    } catch (Exception ex) {
        Assert.fail("unexpected error decoding metadata: " + ex);
    }
    LOG.info("leaving testGetMetadata_HappyPath()");
}
Also used : DbusEventPart(com.linkedin.databus.core.DbusEventPart) DbusEvent(com.linkedin.databus.core.DbusEvent) DbusEventAvroDecoder(com.linkedin.databus.client.DbusEventAvroDecoder) SchemaId(com.linkedin.databus2.schemas.SchemaId) Utf8(org.apache.avro.util.Utf8) VersionedSchemaSet(com.linkedin.databus2.schemas.VersionedSchemaSet) GenericRecord(org.apache.avro.generic.GenericRecord) Test(org.testng.annotations.Test)

Example 9 with DbusEventAvroDecoder

use of com.linkedin.databus.client.DbusEventAvroDecoder in project databus by linkedin.

the class EventInfoDtailPrinter method printEvent.

/**
   * @see com.linkedin.databus2.tools.dtail.DtailPrinter#printEvent(com.linkedin.databus.core.DbusEventInternalReadable, com.linkedin.databus.client.pub.DbusEventDecoder)
   */
@Override
public ConsumerCallbackResult printEvent(DbusEventInternalReadable e, DbusEventDecoder eventDecoder) {
    DbusEventAvroDecoder avroDecoder = (DbusEventAvroDecoder) eventDecoder;
    byte[] payloadSchemaDigest = e.schemaId();
    DbusEventPart metadataPart = e.getPayloadMetadataPart();
    String s = String.format("format=%s opcode=%s partition=%d scn=%d ts=%d (%s.%d) srcid=%d extRepl=%s schema=%s " + "payload_schema_digest=%s metadata_schema=%s metadata_schema_digest=%s", e.getClass().getSimpleName(), e.getOpcode(), e.getPartitionId(), e.sequence(), e.timestampInNanos(), EVENT_TS_FORMAT.format(new Date(e.timestampInNanos() / 1000000)), e.timestampInNanos() % 1000000000, e.getSourceId(), e.isExtReplicatedEvent(), versionedSchemaId(eventDecoder.getPayloadSchema(e)), null != payloadSchemaDigest ? Hex.encodeHexString(payloadSchemaDigest) : "null", null != metadataPart ? versionedSchemaId(avroDecoder.getMetadataSchema(e)) : "null", null != metadataPart ? Hex.encodeHexString(metadataPart.getSchemaDigest()) : "null");
    try {
        _out.write(s.getBytes("UTF-8"));
        _out.write('\n');
    } catch (UnsupportedEncodingException e1) {
        return ConsumerCallbackResult.ERROR;
    } catch (IOException e1) {
        return ConsumerCallbackResult.ERROR;
    }
    return ConsumerCallbackResult.SUCCESS;
}
Also used : DbusEventPart(com.linkedin.databus.core.DbusEventPart) DbusEventAvroDecoder(com.linkedin.databus.client.DbusEventAvroDecoder) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Date(java.util.Date)

Aggregations

DbusEventAvroDecoder (com.linkedin.databus.client.DbusEventAvroDecoder)9 VersionedSchemaSet (com.linkedin.databus2.schemas.VersionedSchemaSet)6 GenericRecord (org.apache.avro.generic.GenericRecord)5 DbusEvent (com.linkedin.databus.core.DbusEvent)4 DbusEventPart (com.linkedin.databus.core.DbusEventPart)4 Test (org.testng.annotations.Test)4 SchemaId (com.linkedin.databus2.schemas.SchemaId)3 VersionedSchema (com.linkedin.databus2.schemas.VersionedSchema)3 IOException (java.io.IOException)3 FileSystemSchemaRegistryService (com.linkedin.databus2.schemas.FileSystemSchemaRegistryService)2 SchemaRegistryService (com.linkedin.databus2.schemas.SchemaRegistryService)2 Schema (org.apache.avro.Schema)2 Field (org.apache.avro.Schema.Field)2 ResultSetEntry (com.linkedin.databus.bootstrap.utils.BootstrapAuditTableReader.ResultSetEntry)1 ConfigLoader (com.linkedin.databus.core.util.ConfigLoader)1 OracleTriggerMonitoredSourceInfo (com.linkedin.databus2.producers.db.OracleTriggerMonitoredSourceInfo)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1