Search in sources :

Example 1 with DivolteIdentifier

use of io.divolte.server.DivolteIdentifier in project divolte-collector by divolte.

the class GoogleCloudPubSubFlusherTest method generateMessage.

private AvroRecordBuffer generateMessage() {
    final DivolteIdentifier partyId = this.partyId.orElseThrow(IllegalStateException::new);
    final DivolteIdentifier sessionId = this.sessionId.orElseThrow(IllegalStateException::new);
    final GenericRecord record = new GenericRecordBuilder(MINIMAL_SCHEMA).set("partyId", partyId.toString()).set("sessionId", sessionId.toString()).set("counter", generatedEventCounter++).build();
    return AvroRecordBuffer.fromRecord(partyId, sessionId, record);
}
Also used : DivolteIdentifier(io.divolte.server.DivolteIdentifier) GenericRecordBuilder(org.apache.avro.generic.GenericRecordBuilder) GenericRecord(org.apache.avro.generic.GenericRecord)

Example 2 with DivolteIdentifier

use of io.divolte.server.DivolteIdentifier in project divolte-collector by divolte.

the class DivolteIdentifierSerializerTest method serializerShouldPrependIdWithVersion.

@Test
public void serializerShouldPrependIdWithVersion() {
    DivolteIdentifierSerializer serializer = new DivolteIdentifierSerializer();
    DivolteIdentifier cv = DivolteIdentifier.generate(42);
    byte[] asBytes = serializer.serialize("topic", cv);
    assertEquals('0', asBytes[0]);
}
Also used : DivolteIdentifier(io.divolte.server.DivolteIdentifier) Test(org.junit.Test)

Example 3 with DivolteIdentifier

use of io.divolte.server.DivolteIdentifier in project divolte-collector by divolte.

the class KafkaFlusher method sendBatch.

@Override
protected ImmutableList<ProducerRecord<DivolteIdentifier, AvroRecordBuffer>> sendBatch(final List<ProducerRecord<DivolteIdentifier, AvroRecordBuffer>> batch) throws InterruptedException {
    // First start sending the messages.
    // (This will serialize them, determine the partition and then assign them to a per-partition buffer.)
    final int batchSize = batch.size();
    final List<Future<RecordMetadata>> sendResults = batch.stream().map(producer::send).collect(Collectors.toCollection(() -> new ArrayList<>(batchSize)));
    // Force a flush so we can check the results without blocking unnecessarily due to
    // a user-configured flushing policy.
    producer.flush();
    // When finished, each message can be in one of several states.
    // - Completed.
    // - An error occurred, but a retry may succeed.
    // - A fatal error occurred.
    // (In addition, we can be interrupted due to shutdown.)
    final ImmutableList.Builder<ProducerRecord<DivolteIdentifier, AvroRecordBuffer>> remaining = ImmutableList.builder();
    for (int i = 0; i < batchSize; ++i) {
        final Future<RecordMetadata> result = sendResults.get(i);
        try {
            final RecordMetadata metadata = result.get();
            if (logger.isDebugEnabled()) {
                final ProducerRecord<DivolteIdentifier, AvroRecordBuffer> record = batch.get(i);
                logger.debug("Finished sending event (partyId={}) to Kafka: topic/partition/offset = {}/{}/{}", record.key(), metadata.topic(), metadata.partition(), metadata.offset());
            }
        } catch (final ExecutionException e) {
            final Throwable cause = e.getCause();
            final ProducerRecord<DivolteIdentifier, AvroRecordBuffer> record = batch.get(i);
            if (cause instanceof RetriableException) {
                // A retry may succeed.
                if (logger.isDebugEnabled()) {
                    logger.debug("Transient error sending event (partyId=" + record.key() + ") to Kafka. Will retry.", cause);
                }
                remaining.add(record);
            } else {
                // Fatal error.
                logger.error("Error sending event (partyId=" + record.key() + ") to Kafka; abandoning.", cause);
            }
        }
    }
    return remaining.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ArrayList(java.util.ArrayList) AvroRecordBuffer(io.divolte.server.AvroRecordBuffer) RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) DivolteIdentifier(io.divolte.server.DivolteIdentifier) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException) RetriableException(org.apache.kafka.common.errors.RetriableException)

Aggregations

DivolteIdentifier (io.divolte.server.DivolteIdentifier)3 ImmutableList (com.google.common.collect.ImmutableList)1 AvroRecordBuffer (io.divolte.server.AvroRecordBuffer)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 GenericRecordBuilder (org.apache.avro.generic.GenericRecordBuilder)1 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)1 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)1 RetriableException (org.apache.kafka.common.errors.RetriableException)1 Test (org.junit.Test)1