From 3e4f108d66fb31e5ea713fef1cf7bc99c0510c21 Mon Sep 17 00:00:00 2001 From: kjaniak Date: Wed, 26 Jun 2019 15:57:29 +0200 Subject: Reorganize kafka module Change-Id: I2eb7a8a6e92c9d89586b877f4cae438497b62ae2 Issue-ID: DCAEGEN2-1635 Signed-off-by: kjaniak --- .../dcaeapp/impl/adapters/KafkaSource.kt | 54 ++++++++++++++++++++++ .../veshv/simulators/dcaeapp/impl/consumer.kt | 26 +++++++---- 2 files changed, 72 insertions(+), 8 deletions(-) create mode 100644 sources/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/adapters/KafkaSource.kt (limited to 'sources/hv-collector-dcae-app-simulator/src/main/kotlin') diff --git a/sources/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/adapters/KafkaSource.kt b/sources/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/adapters/KafkaSource.kt new file mode 100644 index 00000000..1b664edc --- /dev/null +++ b/sources/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/adapters/KafkaSource.kt @@ -0,0 +1,54 @@ +/* + * ============LICENSE_START======================================================= + * dcaegen2-collectors-veshv + * ================================================================================ + * Copyright (C) 2018,2019 NOKIA + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.dcae.collectors.veshv.simulators.dcaeapp.impl.adapters + +import org.onap.dcae.collectors.veshv.kafka.api.KafkaPropertiesFactory +import org.onap.dcae.collectors.veshv.utils.logging.Logger +import reactor.core.publisher.Flux +import reactor.kafka.receiver.KafkaReceiver +import reactor.kafka.receiver.ReceiverOptions +import reactor.kafka.receiver.ReceiverRecord + +/** + * @author Piotr Jaszczyk + * @since May 2018 + */ +internal class KafkaSource(private val receiver: KafkaReceiver) { + fun start(): Flux> = + receiver.receive() + .doOnNext { it.receiverOffset().acknowledge() } + .also { logger.info { "Started Kafka source" } } + + companion object { + private val logger = Logger(KafkaSource::class) + + fun create(bootstrapServers: String, topics: Set) = + KafkaSource(KafkaReceiver.create(createReceiverOptions(bootstrapServers, topics))) + + fun createReceiverOptions(bootstrapServers: String, + topics: Set): ReceiverOptions? { + val props = KafkaPropertiesFactory.create(bootstrapServers) + return ReceiverOptions.create(props) + .addAssignListener { partitions -> logger.debug { "Partitions assigned $partitions" } } + .addRevokeListener { partitions -> logger.debug { "Partitions revoked $partitions" } } + .subscription(topics) + } + } +} diff --git a/sources/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/consumer.kt b/sources/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/consumer.kt index 8a7aafbe..6ee640a4 100644 --- a/sources/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/consumer.kt +++ b/sources/hv-collector-dcae-app-simulator/src/main/kotlin/org/onap/dcae/collectors/veshv/simulators/dcaeapp/impl/consumer.kt @@ -20,8 +20,7 @@ package org.onap.dcae.collectors.veshv.simulators.dcaeapp.impl import org.apache.kafka.clients.consumer.ConsumerRecord -import org.onap.dcae.collectors.veshv.kafka.api.ConsumerFactory -import org.onap.dcae.collectors.veshv.kafka.api.KafkaConsumer +import org.onap.dcae.collectors.veshv.simulators.dcaeapp.impl.adapters.KafkaSource import org.onap.dcae.collectors.veshv.utils.logging.Logger import java.util.concurrent.ConcurrentLinkedQueue @@ -41,9 +40,10 @@ internal class ConsumerState(private val messages: ConcurrentLinkedQueue = ConcurrentLinkedQueue() @@ -51,7 +51,7 @@ internal class Consumer : KafkaConsumer, ConsumerStateProvider { override fun reset() = consumedMessages.clear() - override fun update(record: ConsumerRecord) { + fun update(record: ConsumerRecord) { logger.trace { "Updating stats for message from ${record.topic()}:${record.partition()}" } consumedMessages.add(record.value()) } @@ -63,9 +63,19 @@ internal class Consumer : KafkaConsumer, ConsumerStateProvider { internal class DcaeAppConsumerFactory(private val kafkaBootstrapServers: String) { - private val consumerProvider = { Consumer() } - fun createConsumersFor(topics: Set) = - ConsumerFactory.createConsumersForTopics(kafkaBootstrapServers, topics, consumerProvider) - .mapValues { it.value as Consumer } + KafkaSource.create(kafkaBootstrapServers, topics).let { kafkaSource -> + val topicToConsumer = topics.associateWith { Consumer() } + kafkaSource.start() + .map { + val topic = it.topic() + topicToConsumer.get(topic)?.update(it) + ?: logger.warn { "No consumer configured for topic $topic" } + }.subscribe() + topicToConsumer + } + + companion object { + private val logger = Logger(DcaeAppConsumerFactory::class) + } } -- cgit 1.2.3-korg