d105c4a778a665e2f6ab8de6a34fed0ea079a04b
[dcaegen2/collectors/hv-ves.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * dcaegen2-collectors-veshv
4  * ================================================================================
5  * Copyright (C) 2019 NOKIA
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.dcae.collectors.veshv.kafkaconsumer.impl
21
22 import kotlinx.coroutines.CoroutineDispatcher
23 import kotlinx.coroutines.Dispatchers
24 import kotlinx.coroutines.GlobalScope
25 import kotlinx.coroutines.Job
26 import kotlinx.coroutines.delay
27 import kotlinx.coroutines.isActive
28 import kotlinx.coroutines.launch
29 import org.apache.kafka.clients.consumer.KafkaConsumer
30 import org.apache.kafka.common.TopicPartition
31 import org.onap.dcae.collectors.veshv.kafkaconsumer.metrics.Metrics
32 import org.onap.dcae.collectors.veshv.utils.logging.Logger
33
34 internal class OffsetKafkaConsumer(private val kafkaConsumer: KafkaConsumer<ByteArray, ByteArray>,
35                                    private val topics: Set<String>,
36                                    private val metrics: Metrics,
37                                    private val dispatcher: CoroutineDispatcher = Dispatchers.IO) {
38
39     suspend fun start(updateInterval: Long = 500L): Job =
40             GlobalScope.launch(dispatcher) {
41                 kafkaConsumer.subscribe(topics)
42                     val topicPartitions = kafkaConsumer.assignment()
43                     while (isActive) {
44                         kafkaConsumer.endOffsets(topicPartitions)
45                                 .forEach { (topicPartition, offset) ->
46                                     update(topicPartition, offset)
47                                 }
48                         kafkaConsumer.commitSync()
49                         delay(updateInterval)
50                     }
51             }
52
53     private fun update(topicPartition: TopicPartition, offset: Long) {
54         logger.trace {
55             "Current consumer offset $offset for topic partition $topicPartition"
56         }
57         metrics.notifyOffsetChanged(offset, topicPartition)
58     }
59
60     companion object {
61         val logger = Logger(OffsetKafkaConsumer::class)
62     }
63 }