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