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