@Override protected TDomainClass openEnvelope(final DatumEnvelope datumEnvelope) { logger.debug("converting datum envelope: " + datumEnvelope.toString()); final TDomainClass datum = datumSerDe.deserializeDatum(new SerializedDatum(datumEnvelope.getDatumBytes(), new DatumTypeVersion( datumEnvelope.getDatumTypeId().toString(), datumEnvelope.getDatumSchemaVersion()))); datumAuditor.report(datum); return datum; } }
@Override public void send(final DatumEnvelope datumEnvelope, final DeliveryCallback deliveryCallback) throws SilentSenderException { final String dataAsString = getDataAsString(datumEnvelope.getDatumBytes().array()); final String key = datumEnvelope.getDatumKey() != null ? datumEnvelope.getDatumKey().toString() : null; stringSender.send(dataAsString, key, deliveryCallback); }
@Override public DateTime extractDatumDateTime(DatumEnvelope envelope) { return new DateTime(envelope.getLogicalTimestamp()); } }
@Override public TDomainClass deserialize(String topic, byte[] data) { final DatumEnvelope datumEnvelope = avroDatumEnvelopeSerDe.deserializeDatumEnvelope(ByteBuffer.wrap(data)); final DatumTypeVersion datumTypeVersion = new DatumTypeVersion(datumEnvelope.getDatumTypeId().toString(), datumEnvelope.getDatumSchemaVersion()); final TDomainClass datum = datumSerDe.deserializeDatum(new SerializedDatum(datumEnvelope.getDatumBytes(), datumTypeVersion)); listener.onDeserialize(topic, datum, datumEnvelope, data.length); return datum; }
private BreadcrumbDispatcher<DatumEnvelope> getEnvelopeBreadcrumbsDispatcher(final DatumProducerConfig datumProducerConfig, final EndPoint endPoint, final MetricFactoryProvider metricFactoryProvider) { final Function<DatumEnvelope, BreadcrumbKey> breadcrumbKeyMapper = (envelope) -> new BreadcrumbKey(envelope.getDatumTypeId().toString(), envelope.getSourceHost().toString(), endPoint.getName(), breadcrumbsConfig.getApplication()); final BreadcrumbDispatcher<DatumEnvelope> breadcrumbDispatcher = new KeyedBreadcrumbDispatcher<>( breadcrumbsConfig.getBreadcrumbBucketDuration(), new EnvelopeTimestampSelector(), new KeyedBreadcrumbBaker(breadcrumbsConfig.getTier(), breadcrumbsConfig.getDatacenter()), new BreadcrumbProducingHandler(datumProducerConfig, metricFactoryProvider), breadcrumbKeyMapper, Duration.standardDays(1) ); return new PeriodicBreadcrumbDispatcher<>(breadcrumbDispatcher, breadcrumbsConfig.getBreadcrumbBucketFlushInterval()); } }
System.out.println(String.format("Received an envelope: '%s %s'", envelope.getCreationTime(), envelope.getDatumTypeId())); numberOfMessagesConsumed++; if (numberOfMessagesConsumed == 2) {
private void updateLagMetrics(final DatumEnvelope envelope) { final DateTime datumTime = new DateTime(envelope.getLogicalTimestamp()); final DateTime now = DateTime.now(); if (datumTime.isBefore(now)) { try { final Duration lag = new Interval(datumTime, now).toDuration(); final long logicalSecondsBehind = lag.getStandardSeconds(); logicalDelayAverager.addSample((int) logicalSecondsBehind); final long logicalMillisBehind = lag.getMillis(); logicalDelayInMillisAverager.addSample((int) logicalMillisBehind); logicalTimestampDelayHistogram.update(logicalSecondsBehind); } catch (final Exception e) { logger.error("Error while updating lag metric", e); } } else { // Message from the future final long logicalTimeAheadInMillis = new Interval(now, datumTime).toDuration().getMillis(); futureLogicalMessagesCount.inc(); if (logicalTimeAheadInMillis > LOG_FUTURE_MESSAGES_LAG_THRESHOLD_IN_MILLIS) { logger.warn("A message with a future logical timestamp has arrived from {}. Arriving timestamp is: {}, now reference is {}, delta is: {}, skipping lag metrics update.", envelope.getSourceHost(), envelope.getLogicalTimestamp(), now.getMillis(), logicalTimeAheadInMillis); } } }
public DatumEnvelope buildEnvelope(final TDomainClass domainObject) { final long logicalTimestamp = datumTimestampSelector.extractDatumDateTime(domainObject).getMillis(); final String datumKey = datumKeySelector.getDatumKey(domainObject); final SerializedDatum serializedDatum = datumSerDe.serializeDatum(domainObject); return new DatumEnvelope(datumTypeId, serializedDatum.getDatumTypeVersion().getVersion(), logicalTimestamp, incarnation, source, Instant.now().getMillis(), serializedDatum.getPayload(), datumSerDe.getClass().getSimpleName(), datumKey, UUID.randomUUID().toString()); } }
private void assertListenerArguments(String topic, MyDatum datum, DatumEnvelope datumEnvelope, long dataSizeBytes) { assertEquals(TEST_TOPIC, topic); assertEquals(expectedTestData, datum.getData()); assertEquals(PRODUCER_SOURCE, datumEnvelope.getSourceHost().toString()); assertTrue(dataSizeBytes > 0); } };
@Override public void send(final DatumEnvelope datumEnvelope, final DeliveryCallback deliveryCallback) throws SilentSenderException { final ByteBuffer serializedDatumEnvelope = datumEnvelopeSerializer.serializeDatumEnvelope(datumEnvelope); final String key = datumEnvelope.getDatumKey() != null ? datumEnvelope.getDatumKey().toString() : null; binarySender.send(serializedDatumEnvelope.array(), key, deliveryCallback); }
/** * @param buffer data * @param writer writer schema for the SpecificDatumReader, useful when producers may use different schema versions * @return deserialized object */ public DatumEnvelope deserializeDatumEnvelope(final ByteBuffer buffer, final Schema writer) { try (final InputStream byteBufferInputStream = new ByteBufferInputStream(Collections.singletonList(buffer))) { // hack alert: using old envelope to reconcile version diffs final DatumReader<DatumEnvelope> datumReader = new SpecificDatumReader<>(writer, DatumEnvelope.getClassSchema()); final BinaryDecoder decoder = DecoderFactory.get().directBinaryDecoder(byteBufferInputStream, null); return datumReader.read(null, decoder); } catch (final Exception e) { throw new RuntimeException("Could not deserialize datum envelope", e); } }
@Override public DatumEnvelope build() { try { DatumEnvelope record = new DatumEnvelope(); record.datum_type_id = fieldSetFlags()[0] ? this.datum_type_id : (java.lang.CharSequence) defaultValue(fields()[0]); record.datum_schema_version = fieldSetFlags()[1] ? this.datum_schema_version : (java.lang.Integer) defaultValue(fields()[1]); record.logical_timestamp = fieldSetFlags()[2] ? this.logical_timestamp : (java.lang.Long) defaultValue(fields()[2]); record.incarnation = fieldSetFlags()[3] ? this.incarnation : (java.lang.Integer) defaultValue(fields()[3]); record.source_host = fieldSetFlags()[4] ? this.source_host : (java.lang.CharSequence) defaultValue(fields()[4]); record.creation_time = fieldSetFlags()[5] ? this.creation_time : (java.lang.Long) defaultValue(fields()[5]); record.datumBytes = fieldSetFlags()[6] ? this.datumBytes : (java.nio.ByteBuffer) defaultValue(fields()[6]); record.serDeType = fieldSetFlags()[7] ? this.serDeType : (java.lang.CharSequence) defaultValue(fields()[7]); record.datumKey = fieldSetFlags()[8] ? this.datumKey : (java.lang.CharSequence) defaultValue(fields()[8]); record.datum_unique_id = fieldSetFlags()[9] ? this.datum_unique_id : (java.lang.CharSequence) defaultValue(fields()[9]); return record; } catch (Exception e) { throw new org.apache.avro.AvroRuntimeException(e); } } }
@Override public DateTime extractDatumDateTime(DatumEnvelope envelope) { return new DateTime(envelope.getLogicalTimestamp()); } }