1 package org.openecomp.sdc.be.components.impl;
3 import com.datastax.driver.core.Cluster;
4 import com.datastax.driver.core.KeyspaceMetadata;
5 import com.datastax.driver.core.Metadata;
6 import com.datastax.driver.core.Session;
7 import org.openecomp.sdc.be.config.ConfigurationManager;
8 import org.openecomp.sdc.be.dao.cassandra.schema.SdcSchemaUtils;
9 import org.openecomp.sdc.be.dao.cassandra.schema.Table;
10 import org.openecomp.sdc.common.util.GeneralUtility;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.springframework.stereotype.Component;
15 import javax.annotation.PostConstruct;
16 import java.io.FileInputStream;
17 import java.io.InputStream;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.HashSet;
21 import java.util.List;
23 import java.util.Properties;
26 @Component("cassandra-health-check")
27 public class CassandraHealthCheck {
30 private static final Logger log = LoggerFactory.getLogger(CassandraHealthCheck.class);
32 private String localDataCenterName = null;
34 private Set<String> sdcKeyspaces = new HashSet<String>();
36 private int HC_FormulaNumber;
41 //Initialize local data center name - this field must be filled by DevOps
42 localDataCenterName = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getLocalDataCenter();
44 if (GeneralUtility.isEmptyString(localDataCenterName)) {
45 log.error("localDataCenter Name in configuration.yaml is missing.");
49 //Collect all SDC keyspaces
50 for (Table table : Table.values()) {
51 sdcKeyspaces.add(table.getTableDescription().getKeyspace());
54 String titanCfgFile = ConfigurationManager.getConfigurationManager().getConfiguration().getTitanCfgFile();
55 Properties prop = new Properties();
56 InputStream titanProp = null;
58 //load a properties file
59 titanProp = new FileInputStream(titanCfgFile);
62 String titanKeyspace = prop.getProperty("storage.cassandra.keyspace");
63 if (!GeneralUtility.isEmptyString(titanKeyspace)) {
64 sdcKeyspaces.add(titanKeyspace);
66 } catch (Exception e) {
67 log.error("Failed to open titen.properties file , url is : {}", titanCfgFile, e);
70 log.info("All sdc keyspaces are : {}", sdcKeyspaces);
72 //Calculate the Formula of Health Check
73 Cluster cluster = null;
76 log.info("creating cluster for Cassandra Health Check.");
77 //Create cluster from nodes in cassandra configuration
78 cluster = SdcSchemaUtils.createCluster();
79 if (cluster == null) {
80 log.error("Failure create cassandra cluster.");
84 Metadata metadata = cluster.getMetadata();
86 if (metadata == null) {
87 log.error("Failure get cassandra metadata.");
91 log.info("Cluster Metadata: {}", metadata);
92 List<KeyspaceMetadata> keyspaces = metadata.getKeyspaces();
93 List<Integer> replactionFactorList = new ArrayList<Integer>();
95 //Collect the keyspaces Replication Factor of current localDataCenter
96 for (KeyspaceMetadata keyspace : keyspaces) {
98 if (sdcKeyspaces.contains(keyspace.getName())) {
100 log.info("keyspace : {} , replication: {}", keyspace.getName(), keyspace.getReplication());
101 Map<String, String> replicationOptions = keyspace.getReplication();
103 //In 1 site with one data center
104 if (replicationOptions.containsKey("replication_factor")) {
105 replactionFactorList.add(Integer.parseInt(replicationOptions.get("replication_factor")));
107 //In multiple sites with some data center
108 else if (replicationOptions.containsKey(localDataCenterName)) {
109 replactionFactorList.add(Integer.parseInt(replicationOptions.get(localDataCenterName)));
114 if (replactionFactorList.size() == 0) {
115 log.error("Replication factor NOT found in all keyspaces");
119 int maxReplicationFactor = Collections.max(replactionFactorList);
120 log.info("maxReplication Factor is: {}", maxReplicationFactor);
122 int localQuorum = maxReplicationFactor/2 + 1;
123 log.info("localQuorum is: {}", localQuorum);
125 HC_FormulaNumber = maxReplicationFactor - localQuorum;
127 log.info("Health Check formula : Replication Factor – Local_Quorum = {}", HC_FormulaNumber);
130 } catch (Exception e) {
131 log.error("create cassandra cluster failed with exception.", e);
133 if (cluster != null) {
140 public boolean getCassandraStatus() {
142 if (GeneralUtility.isEmptyString(localDataCenterName)) {
143 log.error("localDataCenter Name in configuration.yaml is missing.");
147 Cluster cluster = null;
148 Session session = null;
150 log.info("creating cluster for Cassandra for monitoring.");
151 cluster = SdcSchemaUtils.createCluster();
152 if (cluster == null) {
153 log.error("Failure create cassandra cluster.");
156 session = cluster.connect();
157 Metadata metadata = cluster.getMetadata();
159 if (metadata == null) {
160 log.error("Failure get cassandra metadata.");
164 log.info("The number of cassandra nodes is:{}", metadata.getAllHosts().size());
166 //Count the number of data center nodes that are down
167 Long downHostsNumber = metadata.getAllHosts().stream()
168 .filter(x -> x.getDatacenter().equals(localDataCenterName) && !x.isUp()).count();
170 log.info("The cassandra down nodes number is {}", downHostsNumber);
171 return HC_FormulaNumber >= downHostsNumber;
173 } catch (Exception e) {
174 log.error("create cassandra cluster failed with exception.", e);
177 if (session != null) {
180 if (cluster != null) {