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=========================================================
19 * Modifications copyright (c) 2018 Nokia
20 * ================================================================================
22 package org.openecomp.sdc.be.components.impl;
24 import com.datastax.driver.core.Cluster;
25 import com.datastax.driver.core.KeyspaceMetadata;
26 import com.datastax.driver.core.Metadata;
27 import com.datastax.driver.core.Session;
28 import org.openecomp.sdc.be.config.ConfigurationManager;
29 import org.openecomp.sdc.be.dao.cassandra.schema.SdcSchemaUtils;
30 import org.openecomp.sdc.be.dao.cassandra.schema.Table;
31 import org.openecomp.sdc.common.log.wrappers.Logger;
32 import org.openecomp.sdc.common.util.GeneralUtility;
33 import org.springframework.stereotype.Component;
35 import javax.annotation.PostConstruct;
37 import java.io.FileInputStream;
38 import java.io.InputStream;
41 @Component("cassandra-health-check")
42 public class CassandraHealthCheck {
45 private static final Logger log = Logger.getLogger(CassandraHealthCheck.class.getName());
47 private String localDataCenterName = null;
49 private Set<String> sdcKeyspaces = new HashSet<>();
51 private int HC_FormulaNumber;
53 private SdcSchemaUtils sdcSchemaUtils;
58 //Initialize local data center name - this field must be filled by DevOps
59 localDataCenterName = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getLocalDataCenter();
61 if (GeneralUtility.isEmptyString(localDataCenterName)) {
62 log.error("localDataCenter Name in configuration.yaml is missing.");
66 //Collect all SDC keyspaces
67 for (Table table : Table.values()) {
68 sdcKeyspaces.add(table.getTableDescription().getKeyspace());
71 String janusGraphCfgFile = ConfigurationManager.getConfigurationManager()
72 .getConfiguration().getJanusGraphCfgFile();
73 Properties prop = new Properties();
74 InputStream janusGraphProp = null;
76 //load a properties file
77 janusGraphProp = new FileInputStream(janusGraphCfgFile);
78 prop.load(janusGraphProp);
79 //Add janusgraph keyspace
80 String janusGraphKeyspace = prop.getProperty("storage.cassandra.keyspace");
81 if (!GeneralUtility.isEmptyString(janusGraphKeyspace)) {
82 sdcKeyspaces.add(janusGraphKeyspace);
84 } catch (Exception e) {
85 log.error("Failed to open titen.properties file , url is : {}", janusGraphCfgFile, e);
88 log.info("All sdc keyspaces are : {}", sdcKeyspaces);
89 sdcSchemaUtils = new SdcSchemaUtils();
90 //Calculate the Formula of Health Check
91 Cluster cluster = null;
94 log.info("creating cluster for Cassandra Health Check.");
95 //Create cluster from nodes in cassandra configuration
96 cluster = sdcSchemaUtils.createCluster();
97 if (cluster == null) {
98 log.error("Failure create cassandra cluster.");
102 Metadata metadata = cluster.getMetadata();
104 if (metadata == null) {
105 log.error("Failure get cassandra metadata.");
109 log.info("Cluster Metadata: {}", metadata);
110 List<KeyspaceMetadata> keyspaces = metadata.getKeyspaces();
111 List<Integer> replactionFactorList = new ArrayList<>();
113 //Collect the keyspaces Replication Factor of current localDataCenter
114 for (KeyspaceMetadata keyspace : keyspaces) {
116 if (sdcKeyspaces.contains(keyspace.getName())) {
118 log.info("keyspace : {} , replication: {}", keyspace.getName(), keyspace.getReplication());
119 Map<String, String> replicationOptions = keyspace.getReplication();
121 //In 1 site with one data center
122 if (replicationOptions.containsKey("replication_factor")) {
123 replactionFactorList.add(Integer.parseInt(replicationOptions.get("replication_factor")));
125 //In multiple sites with some data center
126 else if (replicationOptions.containsKey(localDataCenterName)) {
127 replactionFactorList.add(Integer.parseInt(replicationOptions.get(localDataCenterName)));
132 if (replactionFactorList.size() == 0) {
133 log.error("Replication factor NOT found in all keyspaces");
137 int maxReplicationFactor = Collections.max(replactionFactorList);
138 log.info("maxReplication Factor is: {}", maxReplicationFactor);
140 int localQuorum = maxReplicationFactor/2 + 1;
141 log.info("localQuorum is: {}", localQuorum);
143 HC_FormulaNumber = maxReplicationFactor - localQuorum;
145 log.info("Health Check formula : Replication Factor – Local_Quorum = {}", HC_FormulaNumber);
148 } catch (Exception e) {
149 log.error("create cassandra cluster failed with exception.", e);
151 if (cluster != null) {
158 public boolean getCassandraStatus() {
160 if (GeneralUtility.isEmptyString(localDataCenterName)) {
161 log.error("localDataCenter Name in configuration.yaml is missing.");
165 Cluster cluster = null;
166 Session session = null;
168 log.info("creating cluster for Cassandra for monitoring.");
169 cluster = sdcSchemaUtils.createCluster();
170 if (cluster == null) {
171 log.error("Failure create cassandra cluster.");
174 session = cluster.connect();
175 Metadata metadata = cluster.getMetadata();
177 if (metadata == null) {
178 log.error("Failure get cassandra metadata.");
182 log.info("The number of cassandra nodes is:{}", metadata.getAllHosts().size());
184 //Count the number of data center nodes that are down
185 Long downHostsNumber = metadata.getAllHosts().stream()
186 .filter(x -> x.getDatacenter().equals(localDataCenterName) && !x.isUp()).count();
188 log.info("The cassandra down nodes number is {}", downHostsNumber);
189 return HC_FormulaNumber >= downHostsNumber;
191 } catch (Exception e) {
192 log.error("create cassandra cluster failed with exception.", e);
195 if (session != null) {
198 if (cluster != null) {