diff options
author | shalomb <shalomb@amdocs.com> | 2017-11-14 17:17:51 +0200 |
---|---|---|
committer | shalomb <shalomb@amdocs.com> | 2017-11-15 00:08:05 +0200 |
commit | 6319dfebab190e2ba0cb5a0e85032cd44835e947 (patch) | |
tree | 10a84c0c7750074654dfc8650336207a834bec43 /openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib | |
parent | b6e88fe3fb33b4f21b49a4676c4b96e4dad4ffc8 (diff) |
Casandra data center and consistency level
remove enrichment test
Change-Id: Id2a0eda0a78ae7711ec010248edaa6a1273c5a43
Issue-Id:SDC-656
Signed-off-by: shalomb <shalomb@amdocs.com>
Diffstat (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib')
3 files changed, 93 insertions, 16 deletions
diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraSessionFactory.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraSessionFactory.java index 4bc8262439..18b4b06811 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraSessionFactory.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/impl/cassandra/CassandraSessionFactory.java @@ -21,13 +21,21 @@ package org.openecomp.core.nosqldb.impl.cassandra; import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.ConsistencyLevel; +import com.datastax.driver.core.QueryOptions; import com.datastax.driver.core.SSLOptions; import com.datastax.driver.core.Session; +import com.datastax.driver.core.policies.DCAwareRoundRobinPolicy; +import com.datastax.driver.core.policies.LoadBalancingPolicy; +import com.datastax.driver.core.policies.TokenAwarePolicy; import com.google.common.base.Optional; +import org.apache.commons.lang.ArrayUtils; import org.openecomp.core.nosqldb.util.CassandraUtils; import org.openecomp.sdc.logging.api.Logger; import org.openecomp.sdc.logging.api.LoggerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManagerFactory; import java.io.FileInputStream; import java.io.IOException; import java.security.KeyManagementException; @@ -37,8 +45,7 @@ import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManagerFactory; +import java.util.Objects; public class CassandraSessionFactory { @@ -75,11 +82,46 @@ public class CassandraSessionFactory { if (isAuthenticate) { builder.withCredentials(CassandraUtils.getUser(), CassandraUtils.getPassword()); } + + setConsistencyLevel(builder, addresses); + + setLocalDataCenter(builder); + + Cluster cluster = builder.build(); String keyStore = CassandraUtils.getKeySpace(); return cluster.connect(keyStore); } + private static void setLocalDataCenter(Cluster.Builder builder) { + String localDataCenter = CassandraUtils.getLocalDataCenter(); + if (Objects.nonNull(localDataCenter)) { + log.info("localDatacenter was provided, setting Cassndra client to use datacenter: {} as " + + "local.", localDataCenter); + + LoadBalancingPolicy tokenAwarePolicy = new TokenAwarePolicy( + DCAwareRoundRobinPolicy.builder().withLocalDc(localDataCenter).build()); + builder.withLoadBalancingPolicy(tokenAwarePolicy); + } else { + log.info( + "localDatacenter was provided, the driver will use the datacenter of the first contact point that was reached at initialization"); + } + } + + private static void setConsistencyLevel(Cluster.Builder builder, String[] addresses) { + if (ArrayUtils.isNotEmpty(addresses) && addresses.length > 1) { + String consistencyLevel = CassandraUtils.getConsistencyLevel(); + if (Objects.nonNull(consistencyLevel)) { + log.info( + "consistencyLevel was provided, setting Cassandra client to use consistencyLevel: {}" + + " as " + , consistencyLevel); + builder.withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf + (consistencyLevel))); + } + } + } + private static Optional<SSLOptions> getSslOptions() { Optional<String> truststorePath = Optional.of(CassandraUtils.getTruststore()); Optional<String> truststorePassword = Optional.of(CassandraUtils.getTruststorePassword()); @@ -116,7 +158,7 @@ public class CassandraSessionFactory { ctx.init(null, tmf.getTrustManagers(), new SecureRandom()); } catch (Exception exception) { - log.debug("",exception); + log.debug("", exception); } finally { if (tsf != null) { tsf.close(); diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java index 7a70900873..cc9f626f62 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/CassandraUtils.java @@ -50,8 +50,8 @@ public class CassandraUtils { public static String getStatement(String statementName) { if (statementMap.size() == 0) { - statementMap = FileUtils.readViaInputStream(CASSANDRA_STATEMENT_DEFINITION_FILE, - stream -> JsonUtil.json2Object(stream, Map.class)); + statementMap = FileUtils.readViaInputStream(CASSANDRA_STATEMENT_DEFINITION_FILE, + stream -> JsonUtil.json2Object(stream, Map.class)); } return statementMap.get(statementName); @@ -90,4 +90,14 @@ public class CassandraUtils { public static boolean isAuthenticate() { return ConfigurationManager.getInstance().isAuthenticate(); } + + public static String getConsistencyLevel() { + + return ConfigurationManager.getInstance().getConsistencyLevel(); + + } + + public static String getLocalDataCenter() { + return ConfigurationManager.getInstance().getLocalDataCenter(); + } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java index bde9d06ae4..80adeb6e2a 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-nosqldb-lib/openecomp-nosqldb-core/src/main/java/org/openecomp/core/nosqldb/util/ConfigurationManager.java @@ -32,6 +32,7 @@ import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.function.Function; /** @@ -60,6 +61,10 @@ public class ConfigurationManager { private static final String CASSANDRA_SSL_KEY = "ssl"; private static final String CASSANDRA_TRUSTSTORE_PATH_KEY = "truststorePath"; private static final String CASSANDRA_TRUSTSTORE_PASSWORD_KEY = "truststorePassword"; + private static final String CONSISTENCY_LEVEL = "cassandra.consistencyLevel"; + private static final String CONSISTENCY_LEVEL_KEY = "consistencyLevel"; + private static final String LOCAL_DATA_CENTER_KEY = "localDataCenter"; + private static final String LOCAL_DATA_CENTER = "cassandra.localDataCenter"; private static ConfigurationManager instance = null; private final LinkedHashMap<String, Object> cassandraConfiguration; @@ -78,8 +83,8 @@ public class ConfigurationManager { try { Map<String, LinkedHashMap<String, Object>> configurationMap = configurationYamlFile != null - ? readFromFile(configurationYamlFile, reader) // load from file - : FileUtils.readViaInputStream(CONFIGURATION_YAML_FILE, reader); // or from resource + ? readFromFile(configurationYamlFile, reader) // load from file + : FileUtils.readViaInputStream(CONFIGURATION_YAML_FILE, reader); // or from resource cassandraConfiguration = configurationMap.get(CASSANDRA_KEY); } catch (IOException e) { @@ -93,7 +98,7 @@ public class ConfigurationManager { * @return the instance */ public static ConfigurationManager getInstance() { - if (instance == null) { + if (Objects.isNull(instance)) { instance = new ConfigurationManager(); } return instance; @@ -107,7 +112,7 @@ public class ConfigurationManager { public String[] getAddresses() { String addresses = System.getProperty(CASSANDRA_ADDRESSES); - if (addresses != null) { + if (Objects.isNull(addresses)) { return addresses.split(","); } List lsAddresses = (ArrayList) cassandraConfiguration.get(CASSANDRA_HOSTS_KEY); @@ -124,7 +129,7 @@ public class ConfigurationManager { */ public String getKeySpace() { String keySpace = System.getProperty(CASSANDRA_DOX_KEY_STORE); - if (keySpace == null) { + if (Objects.isNull(keySpace)) { //keySpace = cassandraConfiguration.get(cassandraKeySpaceKey); //if (keySpace == null) keySpace = DEFAULT_KEYSPACE_NAME; @@ -139,7 +144,7 @@ public class ConfigurationManager { */ public String getUsername() { String username = System.getProperty(CASSANDRA_USER); - if (username == null) { + if (Objects.isNull(username)) { username = (String) cassandraConfiguration.get(CASSANDRA_USERNAME_KEY); } return username; @@ -152,7 +157,7 @@ public class ConfigurationManager { */ public String getPassword() { String password = System.getProperty(CASSANDRA_PASSWORD); - if (password == null) { + if (Objects.isNull(password)) { password = (String) cassandraConfiguration.get(CASSANDRA_PASSWORD_KEY); } return password; @@ -165,7 +170,7 @@ public class ConfigurationManager { */ public String getTruststorePath() { String truststorePath = System.getProperty(CASSANDRA_TRUSTSTORE); - if (truststorePath == null) { + if (Objects.isNull(truststorePath)) { truststorePath = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PATH_KEY); } return truststorePath; @@ -178,7 +183,7 @@ public class ConfigurationManager { */ public String getTruststorePassword() { String truststorePassword = System.getProperty(CASSANDRA_TRUSTSTORE_PASSWORD); - if (truststorePassword == null) { + if (Objects.isNull(truststorePassword)) { truststorePassword = (String) cassandraConfiguration.get(CASSANDRA_TRUSTSTORE_PASSWORD_KEY); } return truststorePassword; @@ -192,9 +197,9 @@ public class ConfigurationManager { public int getSslPort() { int port; String sslPort = System.getProperty(CASSANDRA_PORT); - if (sslPort == null) { + if (Objects.isNull(sslPort)) { sslPort = (String) cassandraConfiguration.get(CASSANDRA_PORT_KEY); - if (sslPort == null) { + if (Objects.isNull(sslPort)) { sslPort = "0"; } } @@ -240,4 +245,24 @@ public class ConfigurationManager { return reader.apply(is); } } + + public String getConsistencyLevel() { + String consistencyLevel = System.getProperty(CONSISTENCY_LEVEL); + if (Objects.isNull(consistencyLevel)) { + consistencyLevel = (String) cassandraConfiguration.get(CONSISTENCY_LEVEL_KEY); + } + + if (Objects.isNull(consistencyLevel)) { + consistencyLevel = "LOCAL_QUORUM"; + } + return consistencyLevel; + } + + public String getLocalDataCenter() { + String localDataCenter = System.getProperty(LOCAL_DATA_CENTER); + if (Objects.isNull(localDataCenter)) { + localDataCenter = (String) cassandraConfiguration.get(LOCAL_DATA_CENTER_KEY); + } + return localDataCenter; + } } |