1 package org.openecomp.sdc.be.components.impl;
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.HashSet;
10 import java.util.List;
12 import java.util.Properties;
15 import javax.annotation.PostConstruct;
17 import org.openecomp.sdc.be.config.ConfigurationManager;
18 import org.openecomp.sdc.be.dao.cassandra.schema.SdcSchemaUtils;
19 import org.openecomp.sdc.be.dao.cassandra.schema.Table;
20 import org.openecomp.sdc.common.util.GeneralUtility;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 import org.springframework.stereotype.Component;
25 import com.datastax.driver.core.Cluster;
26 import com.datastax.driver.core.KeyspaceMetadata;
27 import com.datastax.driver.core.Metadata;
28 import com.datastax.driver.core.Session;
30 @Component("cassandra-health-check")
31 public class CassandraHealthCheck {
34 private static Logger log = LoggerFactory.getLogger(CassandraHealthCheck.class.getName());
36 private String localDataCenterName = null;
38 private Set<String> sdcKeyspaces = new HashSet<String>();
40 private int HC_FormulaNumber;
45 //Initialize local data center name - this field must be filled by DevOps
46 localDataCenterName = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getLocalDataCenter();
48 if (GeneralUtility.isEmptyString(localDataCenterName)) {
49 log.error("localDataCenter Name in configuration.yaml is missing.");
53 //Collect all SDC keyspaces
54 for (Table table : Table.values()) {
55 sdcKeyspaces.add(table.getTableDescription().getKeyspace());
58 String titanCfgFile = ConfigurationManager.getConfigurationManager().getConfiguration().getTitanCfgFile();
59 Properties prop = new Properties();
60 InputStream titanProp = null;
62 //load a properties file
63 titanProp = new FileInputStream(titanCfgFile);
66 String titanKeyspace = prop.getProperty("storage.cassandra.keyspace");
67 if (!GeneralUtility.isEmptyString(titanKeyspace)) {
68 sdcKeyspaces.add(titanKeyspace);
70 } catch (Exception e) {
71 log.error("Failed to open titen.properties file , url is : {}", titanCfgFile);
74 log.info("All sdc keyspaces are : {}", sdcKeyspaces);
76 //Calculate the Formula of Health Check
77 Cluster cluster = null;
80 log.info("creating cluster for Cassandra Health Check.");
81 //Create cluster from nodes in cassandra configuration
82 cluster = SdcSchemaUtils.createCluster();
83 if (cluster == null) {
84 log.error("Failure create cassandra cluster.");
88 Metadata metadata = cluster.getMetadata();
90 if (metadata == null) {
91 log.error("Failure get cassandra metadata.");
95 log.info("Cluster Metadata: {}", metadata.toString());
96 List<KeyspaceMetadata> keyspaces = metadata.getKeyspaces();
97 List<Integer> replactionFactorList = new ArrayList<Integer>();
99 //Collect the keyspaces Replication Factor of current localDataCenter
100 for (KeyspaceMetadata keyspace : keyspaces) {
102 if (sdcKeyspaces.contains(keyspace.getName())) {
104 log.info("keyspace : {} , replication: {}", keyspace.getName(), keyspace.getReplication());
105 Map<String, String> replicationOptions = keyspace.getReplication();
107 //In 1 site with one data center
108 if (replicationOptions.containsKey("replication_factor")) {
109 replactionFactorList.add(Integer.parseInt(replicationOptions.get("replication_factor")));
111 //In multiple sites with some data center
112 else if (replicationOptions.containsKey(localDataCenterName)) {
113 replactionFactorList.add(Integer.parseInt(replicationOptions.get(localDataCenterName)));
118 if (replactionFactorList.size() == 0) {
119 log.error("Replication factor NOT found in all keyspaces");
123 int maxReplicationFactor = Collections.max(replactionFactorList);
124 log.info("maxReplication Factor is: {}", maxReplicationFactor);
126 int localQuorum = maxReplicationFactor/2 + 1;
127 log.info("localQuorum is: {}", localQuorum);
129 HC_FormulaNumber = maxReplicationFactor - localQuorum;
131 log.info("Health Check formula : Replication Factor – Local_Quorum = {}", HC_FormulaNumber);
134 } catch (Exception e) {
135 log.error("create cassandra cluster failed with exception.", e);
137 if (cluster != null) {
144 public boolean getCassandraStatus() {
146 if (GeneralUtility.isEmptyString(localDataCenterName)) {
147 log.error("localDataCenter Name in configuration.yaml is missing.");
151 Cluster cluster = null;
152 Session session = null;
154 log.info("creating cluster for Cassandra for monitoring.");
155 cluster = SdcSchemaUtils.createCluster();
156 if (cluster == null) {
157 log.error("Failure create cassandra cluster.");
160 session = cluster.connect();
161 Metadata metadata = cluster.getMetadata();
163 if (metadata == null) {
164 log.error("Failure get cassandra metadata.");
168 log.info("The number of cassandra nodes is:{}", metadata.getAllHosts().size());
170 //Count the number of data center nodes that are down
171 Long downHostsNumber = metadata.getAllHosts().stream()
172 .filter(x -> x.getDatacenter().equals(localDataCenterName) && !x.isUp()).count();
174 log.info("The cassandra down nodes number is {}", downHostsNumber.toString());
175 return (HC_FormulaNumber >= downHostsNumber);
177 } catch (Exception e) {
178 log.error("create cassandra cluster failed with exception.", e);
181 if (session != null) {
184 if (cluster != null) {