Open Kilda Java Documentation
KafkaHealthCheckMessageConsumer.java
Go to the documentation of this file.
1 /* Copyright 2017 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.northbound.messaging.kafka;
17 
19 import static org.openkilda.messaging.Utils.MAPPER;
21 
28 
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.slf4j.MDC;
32 import org.slf4j.MDC.MDCCloseable;
33 import org.springframework.kafka.annotation.KafkaListener;
34 
35 import java.io.IOException;
36 import java.util.Map;
37 import java.util.concurrent.ConcurrentHashMap;
38 import java.util.stream.Collectors;
39 
47  private static final int HEALTH_CHECK_COMPONENTS_COUNT = 5;
48 
52  private static final Logger logger = LoggerFactory.getLogger(KafkaMessageConsumer.class);
53 
57  private volatile Map<String, HealthCheckInfoData> messages = new ConcurrentHashMap<>();
58 
64  @KafkaListener(id = "northbound-listener-health-check", topics = "#{kafkaTopicsConfig.getHealthCheckTopic()}")
65  public void receive(final String record) {
66  Message message;
67 
68  try {
69  logger.trace("message received: {}", record);
70  message = MAPPER.readValue(record, Message.class);
71  } catch (IOException exception) {
72  logger.error("Could not deserialize message: {}", record, exception);
73  return;
74  }
75 
76  try (MDCCloseable closable = MDC.putCloseable(CORRELATION_ID, message.getCorrelationId())) {
77  if (Destination.NORTHBOUND.equals(message.getDestination())) {
78  logger.debug("message received: {}", message);
79  InfoMessage info = (InfoMessage) message;
80  HealthCheckInfoData healthCheck = (HealthCheckInfoData) info.getData();
81  messages.put(healthCheck.getId(), healthCheck);
82  } else {
83  logger.trace("Skip message: {}", message);
84  }
85  }
86  }
87 
91  @Override
92  public Map<String, String> poll(final String correlationId) {
93  try {
94  for (int i = POLL_TIMEOUT / POLL_PAUSE; i < POLL_TIMEOUT; i += POLL_PAUSE) {
95  if (HEALTH_CHECK_COMPONENTS_COUNT == messages.size()) {
96  return messages.values().stream().collect(Collectors.toMap(
98  }
99  Thread.sleep(POLL_PAUSE);
100  }
101  } catch (InterruptedException exception) {
102  String errorMessage = "Unable to poll message";
103  logger.error("{}: {}={}", errorMessage, CORRELATION_ID, correlationId);
104  throw new MessageException(correlationId, System.currentTimeMillis(),
105  INTERNAL_ERROR, errorMessage, "kilda-test");
106  }
107  return messages.values().stream().collect(Collectors.toMap(
109  }
110 
114  @Override
115  public void clear() {
116  messages.clear();
117  }
118 }
static final ObjectMapper MAPPER
Definition: Utils.java:31
static final String CORRELATION_ID
Definition: Utils.java:43