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=========================================================
20 package org.openecomp.sdc.be.components.distribution.engine;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.List;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
28 import java.util.concurrent.Future;
29 import java.util.concurrent.ScheduledExecutorService;
30 import java.util.concurrent.ScheduledFuture;
31 import java.util.concurrent.ThreadFactory;
32 import java.util.concurrent.TimeUnit;
33 import java.util.concurrent.atomic.AtomicBoolean;
34 import javax.annotation.PreDestroy;
35 import lombok.AllArgsConstructor;
37 import org.openecomp.sdc.be.config.BeEcompErrorManager;
38 import org.openecomp.sdc.be.config.ConfigurationManager;
39 import org.openecomp.sdc.be.config.DistributionEngineConfiguration;
40 import org.openecomp.sdc.common.api.Constants;
41 import org.openecomp.sdc.common.api.HealthCheckInfo;
42 import org.openecomp.sdc.common.api.HealthCheckInfo.HealthCheckStatus;
43 import org.openecomp.sdc.common.log.wrappers.Logger;
44 import org.springframework.stereotype.Component;
46 @Component("distribution-engine-cluster-health")
47 public class DistributionEngineClusterHealth {
49 private static final String UEB_HEALTH_CHECK_STR = "uebHealthCheck";
50 private static final Logger logger = Logger.getLogger(DistributionEngineClusterHealth.class.getName());
51 protected static final String UEB_HEALTH_LOG_CONTEXT = "ueb.healthcheck";
52 //TODO use LoggerMetric instead
53 private static final Logger healthLogger = Logger.getLogger(UEB_HEALTH_LOG_CONTEXT);
54 boolean lastHealthState = false;
55 Object lockOject = new Object();
56 ScheduledExecutorService healthCheckScheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
58 public Thread newThread(Runnable r) {
59 return new Thread(r, "UEB-Health-Check-Task");
62 HealthCheckScheduledTask healthCheckScheduledTask = null;
63 private long reconnectInterval = 5;
64 private long healthCheckReadTimeout = 20;
65 private List<String> uebServers = null;
66 private String publicApiKey = null;
67 private HealthCheckInfo healthCheckInfo = HealthCheckInfoResult.UNKNOWN.getHealthCheckInfo();
68 private Map<String, AtomicBoolean> envNamePerStatus = null;
69 private ScheduledFuture<?> scheduledFuture = null;
71 protected void init(final String publicApiKey) {
72 logger.trace("Enter init method of DistributionEngineClusterHealth");
73 Long reconnectIntervalConfig = ConfigurationManager.getConfigurationManager().getConfiguration()
74 .getUebHealthCheckReconnectIntervalInSeconds();
75 if (reconnectIntervalConfig != null) {
76 reconnectInterval = reconnectIntervalConfig.longValue();
78 Long healthCheckReadTimeoutConfig = ConfigurationManager.getConfigurationManager().getConfiguration().getUebHealthCheckReadTimeout();
79 if (healthCheckReadTimeoutConfig != null) {
80 healthCheckReadTimeout = healthCheckReadTimeoutConfig.longValue();
82 DistributionEngineConfiguration distributionEngineConfiguration = ConfigurationManager.getConfigurationManager()
83 .getDistributionEngineConfiguration();
84 this.uebServers = distributionEngineConfiguration.getUebServers();
85 this.publicApiKey = publicApiKey;
86 this.healthCheckScheduledTask = new HealthCheckScheduledTask(this.uebServers);
87 logger.trace("Exit init method of DistributionEngineClusterHealth");
91 protected void destroy() {
92 if (scheduledFuture != null) {
93 scheduledFuture.cancel(true);
94 scheduledFuture = null;
96 if (healthCheckScheduler != null) {
97 healthCheckScheduler.shutdown();
102 * Start health check task.
104 * @param envNamePerStatus
107 public void startHealthCheckTask(Map<String, AtomicBoolean> envNamePerStatus, boolean startTask) {
108 this.envNamePerStatus = envNamePerStatus;
109 if (startTask && this.scheduledFuture == null) {
110 this.scheduledFuture = this.healthCheckScheduler.scheduleAtFixedRate(healthCheckScheduledTask, 0, reconnectInterval, TimeUnit.SECONDS);
114 public void startHealthCheckTask(Map<String, AtomicBoolean> envNamePerStatus) {
115 startHealthCheckTask(envNamePerStatus, true);
118 private void logAlarm(boolean lastHealthState) {
119 if (lastHealthState) {
120 BeEcompErrorManager.getInstance().logBeHealthCheckUebClusterRecovery(UEB_HEALTH_CHECK_STR);
122 BeEcompErrorManager.getInstance().logBeHealthCheckUebClusterError(UEB_HEALTH_CHECK_STR);
126 public HealthCheckInfo getHealthCheckInfo() {
127 return healthCheckInfo;
131 * change the health check to DISABLE
133 public void setHealthCheckUebIsDisabled() {
134 healthCheckInfo = HealthCheckInfoResult.DISABLED.getHealthCheckInfo();
138 * change the health check to NOT CONFGIURED
140 public void setHealthCheckUebConfigurationError() {
141 healthCheckInfo = HealthCheckInfoResult.NOT_CONFIGURED.getHealthCheckInfo();
144 public void setHealthCheckOkAndReportInCaseLastStateIsDown() {
145 if (lastHealthState) {
148 synchronized (lockOject) {
149 if (!lastHealthState) {
150 logger.debug("Going to update health check state to available");
151 lastHealthState = true;
152 healthCheckInfo = HealthCheckInfoResult.OK.getHealthCheckInfo();
153 logAlarm(lastHealthState);
160 private enum HealthCheckInfoResult {
162 OK (new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.UP, null, ClusterStatusDescription.OK.getDescription())),
163 UNAVAILABLE (new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.DOWN, null, ClusterStatusDescription.UNAVAILABLE.getDescription())),
164 NOT_CONFIGURED (new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.DOWN, null, ClusterStatusDescription.NOT_CONFIGURED.getDescription())),
165 DISABLED (new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.DOWN, null, ClusterStatusDescription.DISABLED.getDescription())),
166 UNKNOWN (new HealthCheckInfo(Constants.HC_COMPONENT_DISTRIBUTION_ENGINE, HealthCheckStatus.UNKNOWN, null, ClusterStatusDescription.UNKNOWN.getDescription()));
168 private final HealthCheckInfo healthCheckInfo;
173 private enum ClusterStatusDescription {
175 UNAVAILABLE("U-EB cluster is not available"),
176 NOT_CONFIGURED("U-EB cluster is not configured"),
177 DISABLED("DE is disabled in configuration"),
178 UNKNOWN("U-EB cluster is currently unknown (try again in few minutes)");
179 private final String description;
183 * Health Check Task Scheduler.
185 * 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
186 * query is sent to the next UEB server.
190 public class HealthCheckScheduledTask implements Runnable {
193 List<UebHealthCheckCall> healthCheckCalls = new ArrayList<>();
195 * executor for the query itself
197 private final ExecutorService healthCheckExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {
199 public Thread newThread(Runnable r) {
200 return new Thread(r, "UEB-Health-Check-Thread");
204 public HealthCheckScheduledTask(List<String> localUebServers) {
205 logger.debug("Create health check calls for servers {}", localUebServers);
206 if (localUebServers != null) {
207 for (String server : localUebServers) {
208 healthCheckCalls.add(new UebHealthCheckCall(server, publicApiKey));
215 healthLogger.trace("Executing UEB Health Check Task - Start");
216 boolean healthStatus = verifyAtLeastOneEnvIsUp();
218 boolean queryUebStatus = queryUeb();
219 if (queryUebStatus == lastHealthState) {
222 synchronized (lockOject) {
223 if (queryUebStatus != lastHealthState) {
224 logger.trace("UEB Health State Changed to {}. Issuing alarm / recovery alarm...", healthStatus);
225 lastHealthState = queryUebStatus;
226 logAlarm(lastHealthState);
227 if (queryUebStatus) {
228 healthCheckInfo = HealthCheckInfoResult.OK.getHealthCheckInfo();
230 healthCheckInfo = HealthCheckInfoResult.UNAVAILABLE.getHealthCheckInfo();
235 healthLogger.trace("Not all UEB Environments are up");
240 * verify that at least one environment is up.
242 private boolean verifyAtLeastOneEnvIsUp() {
243 boolean healthStatus = false;
244 if (envNamePerStatus != null) {
245 Collection<AtomicBoolean> values = envNamePerStatus.values();
246 if (values != null) {
247 for (AtomicBoolean status : values) {
259 * 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.
263 private boolean queryUeb() {
264 Boolean result = false;
266 for (UebHealthCheckCall healthCheckCall : healthCheckCalls) {
269 .debug("Before running Health Check retry query number {} towards UEB server {}", retryNumber, healthCheckCall.getServer());
270 Future<Boolean> future = healthCheckExecutor.submit(healthCheckCall);
271 result = future.get(healthCheckReadTimeout, TimeUnit.SECONDS);
272 healthLogger.debug("After running Health Check retry query number {} towards UEB server {}. Result is {}", retryNumber,
273 healthCheckCall.getServer(), result);
274 if (result != null && result.booleanValue()) {
277 } catch (Exception e) {
278 String message = e.getMessage();
279 if (message == null) {
280 message = e.getClass().getName();
282 healthLogger.debug("Error occured during running Health Check retry query towards UEB server {}. Result is {}",
283 healthCheckCall.getServer(), message);
284 healthLogger.trace("Error occured during running Health Check retry query towards UEB server {}. Result is {}",
285 healthCheckCall.getServer(), message, e);