2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 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.catalog.impl;
23 import org.openecomp.sdc.be.config.BeEcompErrorManager;
24 import org.openecomp.sdc.be.config.ConfigurationManager;
25 import org.openecomp.sdc.be.config.DmaapProducerConfiguration;
26 import org.openecomp.sdc.common.api.Constants;
27 import org.openecomp.sdc.common.api.HealthCheckInfo;
28 import org.openecomp.sdc.common.log.wrappers.Logger;
29 import org.springframework.stereotype.Component;
31 import javax.annotation.PostConstruct;
32 import javax.annotation.PreDestroy;
33 import java.util.concurrent.Executors;
34 import java.util.concurrent.ScheduledExecutorService;
35 import java.util.concurrent.ScheduledFuture;
36 import java.util.concurrent.TimeUnit;
37 import java.util.concurrent.atomic.AtomicBoolean;
39 @Component("dmaapProducerHealth")
40 public class DmaapProducerHealth {
43 private static final String DMAAP_HEALTH_LOG_CONTEXT = "dmaapProducer.healthcheck";
44 private static final String DMAAP_HEALTH_CHECK_STR = "dmaapProducerHealthCheck";
45 private static final Logger log = Logger.getLogger(DmaapProducerHealth.class.getName());
46 private static final Logger logHealth = Logger.getLogger(DMAAP_HEALTH_LOG_CONTEXT);
47 private HealthCheckInfo healthCheckInfo = DmaapProducerHealth.HealthCheckInfoResult.UNAVAILABLE.getHealthCheckInfo();
48 private long healthCheckReadTimeout = 20;
49 private long reconnectInterval = 5;
50 private HealthCheckScheduledTask healthCheckScheduledTask = null ;
51 private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
52 private ScheduledFuture<?> scheduledFuture = null;
53 private DmaapProducerConfiguration configuration = null ;
55 private volatile AtomicBoolean lastHealthState = new AtomicBoolean(false);
56 private volatile AtomicBoolean reportedHealthState = null;
58 public enum HealthCheckInfoResult {
59 OK(new HealthCheckInfo(Constants.HC_COMPONENT_DMAAP_PRODUCER, HealthCheckInfo.HealthCheckStatus.UP, null, DmaapStatusDescription.OK.getDescription())),
60 UNAVAILABLE(new HealthCheckInfo(Constants.HC_COMPONENT_DMAAP_PRODUCER, HealthCheckInfo.HealthCheckStatus.DOWN, null, DmaapStatusDescription.UNAVAILABLE.getDescription())),
61 DOWN(new HealthCheckInfo(Constants.HC_COMPONENT_DMAAP_PRODUCER, HealthCheckInfo.HealthCheckStatus.DOWN, null, DmaapStatusDescription.DOWN.getDescription()));
63 private HealthCheckInfo healthCheckInfo;
64 HealthCheckInfoResult(HealthCheckInfo healthCheckInfo) {
65 this.healthCheckInfo = healthCheckInfo;
67 public HealthCheckInfo getHealthCheckInfo() {
68 return healthCheckInfo;
72 public enum DmaapStatusDescription {
73 OK("OK"), UNAVAILABLE("DmaapProducer is not available"),DOWN("DOWN"), NOT_CONFIGURED("DmaapProducer configuration is missing/wrong ");
76 DmaapStatusDescription(String desc) {
79 public String getDescription() {
86 public DmaapProducerHealth init() {
87 log.trace("Enter init method of DmaapProducer health");
88 synchronized (DmaapProducerHealth.class){
89 this.configuration = ConfigurationManager.getConfigurationManager().getConfiguration().getDmaapProducerConfiguration();
91 Integer pollingInterval = configuration.getPollingInterval();
92 if (pollingInterval != null && pollingInterval!=0) {
93 reconnectInterval = pollingInterval;
95 Integer healthCheckReadTimeoutConfig = configuration.getTimeoutMs();
96 if (healthCheckReadTimeoutConfig != null) {
97 this.healthCheckReadTimeout = healthCheckReadTimeoutConfig;
99 this.healthCheckScheduledTask = new HealthCheckScheduledTask( configuration ); //what is the representation? csv? delimiter? json or other
100 startHealthCheckTask(true);
102 log.trace("Exit init method of DistributionEngineClusterHealth");
107 protected void destroy() {
108 if (scheduledFuture != null) {
109 scheduledFuture.cancel(true);
110 scheduledFuture = null;
112 if (scheduler != null) {
113 scheduler.shutdown();
118 * Start health check task.
122 private void startHealthCheckTask(boolean startTask) {
123 synchronized (DmaapProducerHealth.class){
124 if (startTask && this.scheduledFuture == null) {
125 this.scheduledFuture = this.scheduler.scheduleAtFixedRate(this.healthCheckScheduledTask , 0, reconnectInterval, TimeUnit.SECONDS);
130 void report(Boolean isUp){
131 if (reportedHealthState == null)
132 reportedHealthState = new AtomicBoolean(isUp);
133 reportedHealthState.set(isUp);
137 public HealthCheckInfo getHealthCheckInfo() {
138 return healthCheckInfo;
142 * Health Check Task Scheduler - infinite check.
144 public class HealthCheckScheduledTask implements Runnable {
145 private final DmaapProducerConfiguration config;
146 private static final int TIMEOUT = 8192;
148 HealthCheckScheduledTask(final DmaapProducerConfiguration config){
149 this.config = config;
153 logHealth.trace("Executing Dmaap Health Check Task - Start");
154 boolean prevIsReachable;
156 //first try simple ping
158 if ( reportedHealthState != null ){
159 reachable = reportedHealthState.get();
164 prevIsReachable = lastHealthState.getAndSet( reachable );
165 healthCheckInfo = reachable ? HealthCheckInfoResult.OK.healthCheckInfo : HealthCheckInfoResult.DOWN.healthCheckInfo;
167 catch( Exception e ){
168 log.debug("{} - cannot check connectivity -> {}",DMAAP_HEALTH_CHECK_STR, e );
169 prevIsReachable = lastHealthState.getAndSet(false);
170 healthCheckInfo = HealthCheckInfoResult.UNAVAILABLE.healthCheckInfo;
172 if (prevIsReachable != lastHealthState.get())
173 logAlarm( lastHealthState.get() );
179 private void logAlarm(boolean lastHealthState) {
181 if ( lastHealthState ) {
182 BeEcompErrorManager.getInstance().logDmaapHealthCheckRecovery( DMAAP_HEALTH_CHECK_STR );
184 BeEcompErrorManager.getInstance().logDmaapHealthCheckError( DMAAP_HEALTH_CHECK_STR );
186 }catch( Exception e ){
187 log.debug("cannot logAlarm -> {}" ,e );