2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright 2019 China Mobile
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.datalake.feeder.service;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
29 import javax.annotation.PostConstruct;
31 import org.apache.commons.collections.CollectionUtils;
32 import org.onap.datalake.feeder.config.ApplicationConfiguration;
33 import org.onap.datalake.feeder.dto.TopicConfig;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.stereotype.Service;
40 * Service to check topic changes in Kafka and topic setting updates in DB
46 public class TopicConfigPollingService implements Runnable {
48 private final Logger log = LoggerFactory.getLogger(this.getClass());
51 ApplicationConfiguration config;
54 private DmaapService dmaapService;
56 //effective TopicConfig Map
57 private Map<String, TopicConfig> effectiveTopicConfigMap = new HashMap<>();
59 //monitor Kafka topic list changes
60 private List<String> activeTopics;
61 private ThreadLocal<Integer> activeTopicsVersionLocal = ThreadLocal.withInitial(() -> -1);
62 private int currentActiveTopicsVersion = -1;
64 private boolean active = false;
69 log.info("init(), ccalling poll()...");
70 activeTopics = poll();
71 currentActiveTopicsVersion++;
72 } catch (Exception ex) {
73 log.error("error connection to HDFS.", ex);
77 public boolean isActiveTopicsChanged(boolean update) {//update=true means sync local version
78 boolean changed = currentActiveTopicsVersion > activeTopicsVersionLocal.get();
79 log.debug("isActiveTopicsChanged={}, currentActiveTopicsVersion={} local={}", changed, currentActiveTopicsVersion, activeTopicsVersionLocal.get());
80 if (changed && update) {
81 activeTopicsVersionLocal.set(currentActiveTopicsVersion);
87 public List<String> getActiveTopics() {
91 public TopicConfig getEffectiveTopicConfig(String topicStr) {
92 return effectiveTopicConfigMap.get(topicStr);
98 log.info("TopicConfigPollingService started.");
101 try { //sleep first since we already pool in init()
102 Thread.sleep(config.getDmaapCheckNewTopicInterval());
106 } catch (InterruptedException e) {
107 log.error("Thread.sleep(config.getDmaapCheckNewTopicInterval())", e);
108 Thread.currentThread().interrupt();
112 List<String> newTopics = poll();
113 if (!CollectionUtils.isEqualCollection(activeTopics, newTopics)) {
114 log.info("activeTopics list is updated, old={}", activeTopics);
115 log.info("activeTopics list is updated, new={}", newTopics);
117 activeTopics = newTopics;
118 currentActiveTopicsVersion++;
120 log.debug("activeTopics list is not updated.");
122 } catch (IOException e) {
123 log.error("dmaapService.getActiveTopics()", e);
127 log.info("exit since active is set to false");
130 public void shutdown() {
134 private List<String> poll() throws IOException {
135 log.debug("poll(), use dmaapService to getActiveTopicConfigs...");
136 List<TopicConfig> activeTopicConfigs = dmaapService.getActiveTopicConfigs();
137 Map<String, TopicConfig> tempEffectiveTopicConfigMap = new HashMap<>();
139 activeTopicConfigs.stream().forEach(topicConfig -> tempEffectiveTopicConfigMap.put(topicConfig.getName(), topicConfig));
140 effectiveTopicConfigMap = tempEffectiveTopicConfigMap;
141 log.debug("poll(), effectiveTopicConfigMap={}", effectiveTopicConfigMap);
143 List<String> ret = new ArrayList<>(activeTopicConfigs.size());
144 activeTopicConfigs.stream().forEach(topicConfig -> ret.add(topicConfig.getName()));