Search in sources :

Example 6 with Event

use of io.micronaut.http.sse.Event in project akhq by tchiotludo.

the class RecordRepository method tail.

public Flowable<Event<TailEvent>> tail(String clusterId, TailOptions options) {
    return Flowable.generate(() -> {
        KafkaConsumer<byte[], byte[]> consumer = this.kafkaModule.getConsumer(options.clusterId);
        Map<String, Topic> topics = topicRepository.findByName(clusterId, options.topics).stream().collect(Collectors.toMap(Topic::getName, Function.identity()));
        consumer.assign(topics.values().stream().flatMap(topic -> topic.getPartitions().stream().map(partition -> new TopicPartition(topic.getName(), partition.getId()))).collect(Collectors.toList()));
        if (options.getAfter() != null) {
            options.getAfter().forEach(s -> {
                String[] split = s.split(",");
                consumer.seek(new TopicPartition(split[0], Integer.parseInt(split[1])), Long.parseLong(split[2]));
            });
        }
        return new TailState(consumer, new TailEvent(), topics);
    }, (state, subscriber) -> {
        ConsumerRecords<byte[], byte[]> records = this.poll(state.getConsumer());
        TailEvent tailEvent = state.getTailEvent();
        List<Record> list = new ArrayList<>();
        for (ConsumerRecord<byte[], byte[]> record : records) {
            tailEvent.offsets.put(ImmutableMap.of(record.topic(), record.partition()), record.offset());
            Record current = newRecord(record, options, state.getTopics().get(record.topic()));
            if (searchFilter(options, current)) {
                list.add(current);
                log.trace("Record [topic: {}] [partition: {}] [offset: {}] [key: {}]", record.topic(), record.partition(), record.offset(), record.key());
            }
        }
        tailEvent.records = list;
        subscriber.onNext(Event.of(tailEvent).name("tailBody"));
        state.tailEvent = tailEvent;
        return state;
    });
}
Also used : JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) Environment(io.micronaut.context.env.Environment) org.apache.kafka.clients.consumer(org.apache.kafka.clients.consumer) java.util(java.util) TopicController(org.akhq.controllers.TopicController) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Record(org.akhq.models.Record) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) Debug(org.akhq.utils.Debug) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) Function(java.util.function.Function) Event(io.micronaut.http.sse.Event) RecordsToDelete(org.apache.kafka.clients.admin.RecordsToDelete) Topic(org.akhq.models.Topic) AvroToJsonSerializer(org.akhq.utils.AvroToJsonSerializer) KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KeyValue(org.akhq.models.KeyValue) KafkaModule(org.akhq.modules.KafkaModule) SchemaSerializer(org.akhq.modules.schemaregistry.SchemaSerializer) URIBuilder(org.codehaus.httpcache4j.uri.URIBuilder) Splitter(com.google.common.base.Splitter) RecordWithSchemaSerializerFactory(org.akhq.modules.schemaregistry.RecordWithSchemaSerializerFactory) TopicPartition(org.apache.kafka.common.TopicPartition) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Singleton(jakarta.inject.Singleton) Value(io.micronaut.context.annotation.Value) KafkaFuture(org.apache.kafka.common.KafkaFuture) RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) Collectors(java.util.stream.Collectors) lombok(lombok) Partition(org.akhq.models.Partition) ExecutionException(java.util.concurrent.ExecutionException) StringUtils(io.micronaut.core.util.StringUtils) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) SchemaRegistryType(org.akhq.configs.SchemaRegistryType) Inject(jakarta.inject.Inject) DeletedRecords(org.apache.kafka.clients.admin.DeletedRecords) TopicPartition(org.apache.kafka.common.TopicPartition) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Record(org.akhq.models.Record) Topic(org.akhq.models.Topic)

Aggregations

Event (io.micronaut.http.sse.Event)6 ByteBuffer (io.micronaut.core.io.buffer.ByteBuffer)2 Get (io.micronaut.http.annotation.Get)2 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 Splitter (com.google.common.base.Splitter)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 SchemaRegistryClient (io.confluent.kafka.schemaregistry.client.SchemaRegistryClient)1 Value (io.micronaut.context.annotation.Value)1 Environment (io.micronaut.context.env.Environment)1 ReferenceCounted (io.micronaut.core.io.buffer.ReferenceCounted)1 StringUtils (io.micronaut.core.util.StringUtils)1 Headline (io.micronaut.docs.streaming.Headline)1 MutableHttpRequest (io.micronaut.http.MutableHttpRequest)1 Headline (io.micronaut.http.client.docs.streaming.Headline)1 HttpClientException (io.micronaut.http.client.exceptions.HttpClientException)1 SseClient (io.micronaut.http.client.sse.SseClient)1 CodecException (io.micronaut.http.codec.CodecException)1 MediaTypeCodec (io.micronaut.http.codec.MediaTypeCodec)1 JsonSubscriber (io.micronaut.http.netty.stream.JsonSubscriber)1 IdleStateEvent (io.netty.handler.timeout.IdleStateEvent)1