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.log.wrappers.Logger;
11 import org.openecomp.sdc.common.util.GeneralUtility;
12 import org.springframework.stereotype.Component;
14 import javax.annotation.PostConstruct;
15 import java.io.FileInputStream;
16 import java.io.InputStream;
19 @Component("cassandra-health-check")
20 public class CassandraHealthCheck {
23 private static final Logger log = Logger.getLogger(CassandraHealthCheck.class.getName());
25 private String localDataCenterName = null;
27 private Set<String> sdcKeyspaces = new HashSet<>();
29 private int HC_FormulaNumber;
34 //Initialize local data center name - this field must be filled by DevOps
35 localDataCenterName = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getLocalDataCenter();
37 if (GeneralUtility.isEmptyString(localDataCenterName)) {
38 log.error("localDataCenter Name in configuration.yaml is missing.");
42 //Collect all SDC keyspaces
43 for (Table table : Table.values()) {
44 sdcKeyspaces.add(table.getTableDescription().getKeyspace());
47 String titanCfgFile = ConfigurationManager.getConfigurationManager().getConfiguration().getTitanCfgFile();
48 Properties prop = new Properties();
49 InputStream titanProp = null;
51 //load a properties file
52 titanProp = new FileInputStream(titanCfgFile);
55 String titanKeyspace = prop.getProperty("storage.cassandra.keyspace");
56 if (!GeneralUtility.isEmptyString(titanKeyspace)) {
57 sdcKeyspaces.add(titanKeyspace);
59 } catch (Exception e) {
60 log.error("Failed to open titen.properties file , url is : {}", titanCfgFile, e);
63 log.info("All sdc keyspaces are : {}", sdcKeyspaces);
65 //Calculate the Formula of Health Check
66 Cluster cluster = null;
69 log.info("creating cluster for Cassandra Health Check.");
70 //Create cluster from nodes in cassandra configuration
71 cluster = SdcSchemaUtils.createCluster();
72 if (cluster == null) {
73 log.error("Failure create cassandra cluster.");
77 Metadata metadata = cluster.getMetadata();
79 if (metadata == null) {
80 log.error("Failure get cassandra metadata.");
84 log.info("Cluster Metadata: {}", metadata);
85 List<KeyspaceMetadata> keyspaces = metadata.getKeyspaces();
86 List<Integer> replactionFactorList = new ArrayList<>();
88 //Collect the keyspaces Replication Factor of current localDataCenter
89 for (KeyspaceMetadata keyspace : keyspaces) {
91 if (sdcKeyspaces.contains(keyspace.getName())) {
93 log.info("keyspace : {} , replication: {}", keyspace.getName(), keyspace.getReplication());
94 Map<String, String> replicationOptions = keyspace.getReplication();
96 //In 1 site with one data center
97 if (replicationOptions.containsKey("replication_factor")) {
98 replactionFactorList.add(Integer.parseInt(replicationOptions.get("replication_factor")));
100 //In multiple sites with some data center
101 else if (replicationOptions.containsKey(localDataCenterName)) {
102 replactionFactorList.add(Integer.parseInt(replicationOptions.get(localDataCenterName)));
107 if (replactionFactorList.size() == 0) {
108 log.error("Replication factor NOT found in all keyspaces");
112 int maxReplicationFactor = Collections.max(replactionFactorList);
113 log.info("maxReplication Factor is: {}", maxReplicationFactor);
115 int localQuorum = maxReplicationFactor/2 + 1;
116 log.info("localQuorum is: {}", localQuorum);
118 HC_FormulaNumber = maxReplicationFactor - localQuorum;
120 log.info("Health Check formula : Replication Factor – Local_Quorum = {}", HC_FormulaNumber);
123 } catch (Exception e) {
124 log.error("create cassandra cluster failed with exception.", e);
126 if (cluster != null) {
133 public boolean getCassandraStatus() {
135 if (GeneralUtility.isEmptyString(localDataCenterName)) {
136 log.error("localDataCenter Name in configuration.yaml is missing.");
140 Cluster cluster = null;
141 Session session = null;
143 log.info("creating cluster for Cassandra for monitoring.");
144 cluster = SdcSchemaUtils.createCluster();
145 if (cluster == null) {
146 log.error("Failure create cassandra cluster.");
149 session = cluster.connect();
150 Metadata metadata = cluster.getMetadata();
152 if (metadata == null) {
153 log.error("Failure get cassandra metadata.");
157 log.info("The number of cassandra nodes is:{}", metadata.getAllHosts().size());
159 //Count the number of data center nodes that are down
160 Long downHostsNumber = metadata.getAllHosts().stream()
161 .filter(x -> x.getDatacenter().equals(localDataCenterName) && !x.isUp()).count();
163 log.info("The cassandra down nodes number is {}", downHostsNumber);
164 return HC_FormulaNumber >= downHostsNumber;
166 } catch (Exception e) {
167 log.error("create cassandra cluster failed with exception.", e);
170 if (session != null) {
173 if (cluster != null) {