Sync Integ to Master
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / CassandraHealthCheck.java
1 package org.openecomp.sdc.be.components.impl;
2
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;
14
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;
22 import java.util.Map;
23 import java.util.Properties;
24 import java.util.Set;
25
26 @Component("cassandra-health-check")
27 public class CassandraHealthCheck {
28
29
30     private static final Logger log = LoggerFactory.getLogger(CassandraHealthCheck.class);
31
32     private String localDataCenterName = null;
33
34     private Set<String> sdcKeyspaces = new HashSet<String>();
35
36     private int HC_FormulaNumber;
37
38     @PostConstruct
39     private void init() {
40
41         //Initialize local data center name - this field must be filled by DevOps
42         localDataCenterName = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getLocalDataCenter();
43
44         if (GeneralUtility.isEmptyString(localDataCenterName))  {
45             log.error("localDataCenter Name in configuration.yaml is missing.");
46             return;
47         }
48
49         //Collect all SDC keyspaces
50         for (Table table : Table.values()) {
51             sdcKeyspaces.add(table.getTableDescription().getKeyspace());
52         }
53
54         String titanCfgFile = ConfigurationManager.getConfigurationManager().getConfiguration().getTitanCfgFile();
55         Properties prop = new Properties();
56         InputStream titanProp = null;
57         try {
58             //load a properties file
59             titanProp = new FileInputStream(titanCfgFile);
60             prop.load(titanProp);
61             //Add titan keyspace
62             String titanKeyspace = prop.getProperty("storage.cassandra.keyspace");
63             if (!GeneralUtility.isEmptyString(titanKeyspace))  {
64                 sdcKeyspaces.add(titanKeyspace);
65             }
66         } catch (Exception e) {
67             log.error("Failed to open titen.properties file , url is : {}", titanCfgFile, e);
68         }
69
70         log.info("All sdc keyspaces are : {}", sdcKeyspaces);
71
72         //Calculate the Formula of Health Check
73         Cluster cluster = null;
74         try {
75
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.");
81                 return;
82             }
83
84             Metadata metadata = cluster.getMetadata();
85
86             if (metadata == null) {
87                 log.error("Failure get cassandra metadata.");
88                 return;
89             }
90
91             log.info("Cluster Metadata: {}", metadata);
92             List<KeyspaceMetadata> keyspaces = metadata.getKeyspaces();
93             List<Integer> replactionFactorList = new ArrayList<Integer>();
94
95             //Collect the keyspaces Replication Factor of current localDataCenter
96             for (KeyspaceMetadata keyspace : keyspaces) {
97
98                 if (sdcKeyspaces.contains(keyspace.getName()))  {
99
100                     log.info("keyspace : {} , replication: {}",  keyspace.getName(), keyspace.getReplication());
101                     Map<String, String> replicationOptions = keyspace.getReplication();
102
103                     //In 1 site with one data center
104                     if (replicationOptions.containsKey("replication_factor")) {
105                         replactionFactorList.add(Integer.parseInt(replicationOptions.get("replication_factor")));
106                     }
107                     //In multiple sites with some data center
108                     else if (replicationOptions.containsKey(localDataCenterName)) {
109                         replactionFactorList.add(Integer.parseInt(replicationOptions.get(localDataCenterName)));
110                     }
111                 }
112             }
113
114             if (replactionFactorList.size() == 0)  {
115                 log.error("Replication factor NOT found in all keyspaces");
116                 return;
117             }
118
119             int maxReplicationFactor = Collections.max(replactionFactorList);
120             log.info("maxReplication Factor is: {}", maxReplicationFactor);
121
122             int localQuorum = maxReplicationFactor/2 + 1;
123             log.info("localQuorum is: {}", localQuorum);
124
125             HC_FormulaNumber = maxReplicationFactor - localQuorum;
126
127             log.info("Health Check formula : Replication Factor – Local_Quorum = {}", HC_FormulaNumber);
128
129
130         } catch (Exception e) {
131             log.error("create cassandra cluster failed with exception.", e);
132         } finally {
133             if (cluster != null) {
134                 cluster.close();
135             }
136         }
137
138     }
139
140     public boolean getCassandraStatus()  {
141
142         if (GeneralUtility.isEmptyString(localDataCenterName)) {
143             log.error("localDataCenter Name in configuration.yaml is missing.");
144             return false;
145         }
146
147         Cluster cluster = null;
148         Session session = null;
149         try {
150             log.info("creating cluster for Cassandra for monitoring.");
151             cluster = SdcSchemaUtils.createCluster();
152             if (cluster == null) {
153                 log.error("Failure create cassandra cluster.");
154                 return false;
155             }
156             session = cluster.connect();
157             Metadata metadata = cluster.getMetadata();
158
159             if (metadata == null) {
160                 log.error("Failure get cassandra metadata.");
161                 return false;
162             }
163
164             log.info("The number of cassandra nodes is:{}", metadata.getAllHosts().size());
165
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();
169
170             log.info("The cassandra down nodes number is {}", downHostsNumber);
171             return HC_FormulaNumber >= downHostsNumber;
172
173         } catch (Exception e) {
174             log.error("create cassandra cluster failed with exception.", e);
175             return false;
176         } finally {
177             if (session != null) {
178                 session.close();
179             }
180             if (cluster != null) {
181                 cluster.close();
182             }
183         }
184     }
185 }