2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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.openecomp.sdc.be.components.distribution.engine;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.Executors;
29 import java.util.concurrent.Future;
30 import java.util.concurrent.ScheduledExecutorService;
31 import java.util.concurrent.ScheduledFuture;
32 import java.util.concurrent.ThreadFactory;
33 import java.util.concurrent.TimeUnit;
34 import java.util.concurrent.atomic.AtomicBoolean;
36 import javax.annotation.PostConstruct;
37 import javax.annotation.PreDestroy;
39 import org.openecomp.sdc.be.config.BeEcompErrorManager;
40 import org.openecomp.sdc.be.config.ConfigurationManager;
41 import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
42 import org.openecomp.sdc.common.api.Constants;
43 import org.openecomp.sdc.common.api.HealthCheckInfo;
44 import org.openecomp.sdc.common.api.HealthCheckInfo.HealthCheckStatus;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.springframework.stereotype.Component;
49 @Component("distribution-engine-cluster-health")
50 public class DistributionEngineClusterHealth {
52 protected static String UEB_HEALTH_LOG_CONTEXT = "ueb.healthcheck";
54 private static final Logger healthLogger = LoggerFactory.getLogger(UEB_HEALTH_LOG_CONTEXT);
56 private static final String UEB_HEALTH_CHECK_STR = "uebHealthCheck";
58 boolean lastHealthState = false;
60 Object lockOject = new Object();
62 private long reconnectInterval = 5;
64 private long healthCheckReadTimeout = 20;
66 private static final Logger logger = LoggerFactory.getLogger(DistributionEngineClusterHealth.class);
68 private List<String> uebServers = null;
70 private String publicApiKey = null;
72 public enum HealthCheckInfoResult {
74 OK(new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.UP, null, ClusterStatusDescription.OK.getDescription())),
75 UNAVAILABLE(new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.DOWN, null, ClusterStatusDescription.UNAVAILABLE.getDescription())),
76 NOT_CONFIGURED(new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.DOWN, null, ClusterStatusDescription.NOT_CONFIGURED.getDescription())),
77 DISABLED(new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.DOWN, null, ClusterStatusDescription.DISABLED.getDescription()));
79 private HealthCheckInfo healthCheckInfo;
81 HealthCheckInfoResult(HealthCheckInfo healthCheckInfo) {
82 this.healthCheckInfo = healthCheckInfo;
85 public HealthCheckInfo getHealthCheckInfo() {
86 return healthCheckInfo;
91 private HealthCheckInfo healthCheckInfo = HealthCheckInfoResult.UNAVAILABLE.getHealthCheckInfo();
93 private Map<String, AtomicBoolean> envNamePerStatus = null;
95 private ScheduledFuture<?> scheduledFuture = null;
97 ScheduledExecutorService healthCheckScheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
99 public Thread newThread(Runnable r) {
100 return new Thread(r, "UEB-Health-Check-Task");
104 HealthCheckScheduledTask healthCheckScheduledTask = null;
106 public enum ClusterStatusDescription {
108 OK("OK"), UNAVAILABLE("U-EB cluster is not available"), NOT_CONFIGURED("U-EB cluster is not configured"), DISABLED("DE is disabled in configuration");
112 ClusterStatusDescription(String desc) {
116 public String getDescription() {
123 * Health Check Task Scheduler.
125 * It schedules a task which send a apiKey get query towards the UEB servers. In case a query to the first UEB server is failed, then a second query is sent to the next UEB server.
131 public class HealthCheckScheduledTask implements Runnable {
133 List<UebHealthCheckCall> healthCheckCalls = new ArrayList<>();
135 public HealthCheckScheduledTask(List<String> uebServers) {
137 logger.debug("Create health check calls for servers {}", uebServers);
138 if (uebServers != null) {
139 for (String server : uebServers) {
140 healthCheckCalls.add(new UebHealthCheckCall(server, publicApiKey));
148 healthLogger.trace("Executing UEB Health Check Task - Start");
150 boolean healthStatus = verifyAtLeastOneEnvIsUp();
152 if (true == healthStatus) {
153 boolean queryUebStatus = queryUeb();
154 if (queryUebStatus == lastHealthState) {
158 synchronized (lockOject) {
159 if (queryUebStatus != lastHealthState) {
160 logger.trace("UEB Health State Changed to {}. Issuing alarm / recovery alarm...", healthStatus);
161 lastHealthState = queryUebStatus;
162 logAlarm(lastHealthState);
163 if (true == queryUebStatus) {
164 healthCheckInfo = HealthCheckInfoResult.OK.getHealthCheckInfo();
166 healthCheckInfo = HealthCheckInfoResult.UNAVAILABLE.getHealthCheckInfo();
171 healthLogger.trace("Not all UEB Environments are up");
177 * verify that at least one environment is up.
180 private boolean verifyAtLeastOneEnvIsUp() {
182 boolean healthStatus = false;
184 if (envNamePerStatus != null) {
185 Collection<AtomicBoolean> values = envNamePerStatus.values();
186 if (values != null) {
187 for (AtomicBoolean status : values) {
188 if (true == status.get()) {
200 * executor for the query itself
202 ExecutorService healthCheckExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {
204 public Thread newThread(Runnable r) {
205 return new Thread(r, "UEB-Health-Check-Thread");
210 * go all UEB servers and send a get apiKeys query. In case a query is succeed, no query is sent to the rest of UEB servers.
215 private boolean queryUeb() {
217 Boolean result = false;
219 for (UebHealthCheckCall healthCheckCall : healthCheckCalls) {
222 healthLogger.debug("Before running Health Check retry query number {} towards UEB server {}", retryNumber, healthCheckCall.getServer());
224 Future<Boolean> future = healthCheckExecutor.submit(healthCheckCall);
225 result = future.get(healthCheckReadTimeout, TimeUnit.SECONDS);
227 healthLogger.debug("After running Health Check retry query number {} towards UEB server {}. Result is {}", retryNumber, healthCheckCall.getServer(), result);
229 if (result != null && true == result.booleanValue()) {
233 } catch (Exception e) {
234 String message = e.getMessage();
235 if (message == null) {
236 message = e.getClass().getName();
238 healthLogger.debug("Error occured during running Health Check retry query towards UEB server {}. Result is {}", healthCheckCall.getServer(), message);
239 healthLogger.trace("Error occured during running Health Check retry query towards UEB server {}. Result is {}", healthCheckCall.getServer(), message, e);
249 public List<UebHealthCheckCall> getHealthCheckCalls() {
250 return healthCheckCalls;
256 protected void init() {
258 logger.trace("Enter init method of DistributionEngineClusterHealth");
260 Long reconnectIntervalConfig = ConfigurationManager.getConfigurationManager().getConfiguration().getUebHealthCheckReconnectIntervalInSeconds();
261 if (reconnectIntervalConfig != null) {
262 reconnectInterval = reconnectIntervalConfig.longValue();
264 Long healthCheckReadTimeoutConfig = ConfigurationManager.getConfigurationManager().getConfiguration().getUebHealthCheckReadTimeout();
265 if (healthCheckReadTimeoutConfig != null) {
266 healthCheckReadTimeout = healthCheckReadTimeoutConfig.longValue();
269 DistributionEngineConfiguration distributionEngineConfiguration = ConfigurationManager.getConfigurationManager().getDistributionEngineConfiguration();
271 this.uebServers = distributionEngineConfiguration.getUebServers();
272 this.publicApiKey = distributionEngineConfiguration.getUebPublicKey();
274 this.healthCheckScheduledTask = new HealthCheckScheduledTask(this.uebServers);
276 logger.trace("Exit init method of DistributionEngineClusterHealth");
281 protected void destroy() {
283 if (scheduledFuture != null) {
284 scheduledFuture.cancel(true);
285 scheduledFuture = null;
288 if (healthCheckScheduler != null) {
289 healthCheckScheduler.shutdown();
295 * Start health check task.
297 * @param envNamePerStatus
300 public void startHealthCheckTask(Map<String, AtomicBoolean> envNamePerStatus, boolean startTask) {
301 this.envNamePerStatus = envNamePerStatus;
303 if (startTask == true && this.scheduledFuture == null) {
304 this.scheduledFuture = this.healthCheckScheduler.scheduleAtFixedRate(healthCheckScheduledTask, 0, reconnectInterval, TimeUnit.SECONDS);
308 public void startHealthCheckTask(Map<String, AtomicBoolean> envNamePerStatus) {
309 startHealthCheckTask(envNamePerStatus, true);
312 private void logAlarm(boolean lastHealthState) {
313 if (lastHealthState == true) {
314 BeEcompErrorManager.getInstance().logBeHealthCheckUebClusterRecovery(UEB_HEALTH_CHECK_STR);
316 BeEcompErrorManager.getInstance().logBeHealthCheckUebClusterError(UEB_HEALTH_CHECK_STR);
320 public HealthCheckInfo getHealthCheckInfo() {
321 return healthCheckInfo;
325 * change the health check to DISABLE
327 public void setHealthCheckUebIsDisabled() {
328 healthCheckInfo = HealthCheckInfoResult.DISABLED.getHealthCheckInfo();
332 * change the health check to NOT CONFGIURED
334 public void setHealthCheckUebConfigurationError() {
335 healthCheckInfo = HealthCheckInfoResult.NOT_CONFIGURED.getHealthCheckInfo();
338 public void setHealthCheckOkAndReportInCaseLastStateIsDown() {
340 if (lastHealthState == true) {
343 synchronized (lockOject) {
344 if (lastHealthState == false) {
345 logger.debug("Going to update health check state to available");
346 lastHealthState = true;
347 healthCheckInfo = HealthCheckInfoResult.OK.getHealthCheckInfo();
348 logAlarm(lastHealthState);