58b2783448b60a9770fee8a14675f35f85e16d5f
[dcaegen2/services.git] /
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : DATALAKE
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
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
21 package org.onap.datalake.feeder.service;
22
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29 import javax.annotation.PostConstruct;
30
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;
38
39 /**
40  * Service to check topic changes in Kafka and topic setting updates
41  * 
42  * @author Guobiao Mo
43  *
44  */
45 @Service
46 public class TopicConfigPollingService implements Runnable {
47
48         private final Logger log = LoggerFactory.getLogger(this.getClass());
49
50         @Autowired
51         ApplicationConfiguration config;
52
53         @Autowired
54         private DmaapService dmaapService;
55
56         //effective TopicConfig Map
57         private Map<String, TopicConfig> effectiveTopicConfigMap = new HashMap<>();
58
59         //monitor Kafka topic list changes
60         private List<String> activeTopics;
61         private ThreadLocal<Integer> activeTopicsVersionLocal = ThreadLocal.withInitial(() -> -1);
62         private int currentActiveTopicsVersion = -1;
63
64         private boolean active = false;
65
66         @PostConstruct
67         private void init() {
68                 try {
69                         log.info("init(), ccalling poll()...");
70                         activeTopics = poll();
71                         currentActiveTopicsVersion++;
72                 } catch (Exception ex) {
73                         log.error("error connection to HDFS.", ex);
74                 }
75         }
76
77         public boolean isActiveTopicsChanged(boolean update) {
78                 boolean changed = currentActiveTopicsVersion > activeTopicsVersionLocal.get();
79                 log.debug("isActiveTopicsChanged={}, currentActiveTopicsVersion={} local={}", changed, currentActiveTopicsVersion, activeTopicsVersionLocal.get());
80                 if (changed && update) {
81                         activeTopicsVersionLocal.set(currentActiveTopicsVersion);
82                 }
83
84                 return changed;
85         }
86
87         public List<String> getActiveTopics() {
88                 return activeTopics;
89         }
90
91         public TopicConfig getEffectiveTopicConfig(String topicStr) {
92                 return effectiveTopicConfigMap.get(topicStr);
93         }
94
95         @Override
96         public void run() {
97                 active = true;
98                 log.info("TopicConfigPollingService started.");
99                 
100                 while (active) {
101                         try { //sleep first since we already pool in init()
102                                 Thread.sleep(config.getDmaapCheckNewTopicInterval());
103                         } catch (InterruptedException e) {
104                                 log.error("Thread.sleep(config.getDmaapCheckNewTopicInterval())", e);
105                                 Thread.currentThread().interrupt();
106                         }
107
108                         try {
109                                 List<String> newTopics = poll();
110                                 if (!CollectionUtils.isEqualCollection(activeTopics, newTopics)) {
111                                         log.info("activeTopics list is updated, old={}", activeTopics);
112                                         log.info("activeTopics list is updated, new={}", newTopics);
113
114                                         activeTopics = newTopics;
115                                         currentActiveTopicsVersion++;
116                                 } else {
117                                         log.debug("activeTopics list is not updated.");
118                                 }
119                         } catch (IOException e) {
120                                 log.error("dmaapService.getActiveTopics()", e);
121                         }
122                 }
123
124                 log.info("exit since active is set to false");
125         }
126
127         public void shutdown() {
128                 active = false;
129         }
130
131         private List<String> poll() throws IOException {
132                 log.debug("poll(), use dmaapService to getActiveTopicConfigs...");
133                 List<TopicConfig> activeTopicConfigs = dmaapService.getActiveTopicConfigs();
134                 activeTopicConfigs.stream().forEach(topicConfig -> effectiveTopicConfigMap.put(topicConfig.getName(), topicConfig));
135
136                 List<String> ret = new ArrayList<>(activeTopicConfigs.size());
137                 activeTopicConfigs.stream().forEach(topicConfig -> ret.add(topicConfig.getName()));
138
139                 return ret;
140         }
141
142 }