You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2019/11/08 03:11:56 UTC

[james-project] 09/36: JAMES-2904 ClusterWithKeyspaceCreatedFactory should rely on ClusterConfiguration

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 9d366040c122a602707f912181613184bebea43a
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Wed Oct 2 11:32:15 2019 +0700

    JAMES-2904 ClusterWithKeyspaceCreatedFactory should rely on ClusterConfiguration
---
 .../init/ClusterWithKeyspaceCreatedFactory.java    | 47 +++-------------------
 .../cassandra/init/ResilientClusterProvider.java   |  6 +--
 .../init/configuration/ClusterConfiguration.java   | 20 ++++++++-
 .../james/backends/cassandra/CassandraCluster.java | 21 ++++------
 .../SessionWithInitializedTablesFactoryTest.java   | 22 +++++-----
 5 files changed, 45 insertions(+), 71 deletions(-)

diff --git a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterWithKeyspaceCreatedFactory.java b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterWithKeyspaceCreatedFactory.java
index 4ff38ba..a93ca15 100644
--- a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterWithKeyspaceCreatedFactory.java
+++ b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ClusterWithKeyspaceCreatedFactory.java
@@ -19,53 +19,18 @@
 
 package org.apache.james.backends.cassandra.init;
 
+import org.apache.james.backends.cassandra.init.configuration.ClusterConfiguration;
+
 import com.datastax.driver.core.Cluster;
 import com.datastax.driver.core.Session;
 
 public class ClusterWithKeyspaceCreatedFactory {
-
-    private static final int DEFAULT_REPLICATION_FACTOR = 1;
-    
-    public static Configuration config(Cluster cluster, String keyspace) {
-        return new Configuration(cluster, keyspace);
-    }
-    
-    public static class Configuration {
-        private Cluster cluster;
-        private String keyspace;
-        private boolean durableWrites;
-        private int replicationFactor;
-        
-        private Configuration(Cluster cluster, String keyspace) {
-            this.cluster = cluster;
-            this.keyspace = keyspace;
-            this.durableWrites = true;
-            this.replicationFactor = DEFAULT_REPLICATION_FACTOR;
-        }
-        
-        public Configuration disableDurableWrites() {
-            this.durableWrites = false;
-            return this;
-        }
-        
-        public Configuration replicationFactor(int replicationFactor) {
-            this.replicationFactor = replicationFactor;
-            return this;
-        }
-        
-        public Cluster clusterWithInitializedKeyspace() {
-            createKeyspace(cluster, keyspace, replicationFactor, durableWrites);
-            return cluster;
-        }
-    }
-
-    private static void createKeyspace(Cluster cluster, String keyspace, int replicationFactor, boolean durableWrites) {
+    public static void createKeyspace(Cluster cluster, ClusterConfiguration clusterConfiguration) {
         try (Session session = cluster.connect()) {
-            session.execute("CREATE KEYSPACE IF NOT EXISTS " + keyspace
-                + " WITH replication = {'class':'SimpleStrategy', 'replication_factor':" + replicationFactor + "}"
-                + " AND durable_writes = " + String.valueOf(durableWrites)
+            session.execute("CREATE KEYSPACE IF NOT EXISTS " + clusterConfiguration.getKeyspace()
+                + " WITH replication = {'class':'SimpleStrategy', 'replication_factor':" + clusterConfiguration.getReplicationFactor() + "}"
+                + " AND durable_writes = " + clusterConfiguration.isDurableWrites()
                 + ";");
         }
     }
-
 }
diff --git a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ResilientClusterProvider.java b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ResilientClusterProvider.java
index 150152b..523a925 100644
--- a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ResilientClusterProvider.java
+++ b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/ResilientClusterProvider.java
@@ -71,10 +71,8 @@ public class ResilientClusterProvider implements Provider<Cluster> {
                     .connectTimeoutMillis(configuration.getConnectTimeoutMillis())
                     .build();
             try {
-                return ClusterWithKeyspaceCreatedFactory
-                    .config(cluster, configuration.getKeyspace())
-                    .replicationFactor(configuration.getReplicationFactor())
-                    .clusterWithInitializedKeyspace();
+                ClusterWithKeyspaceCreatedFactory.createKeyspace(cluster, configuration);
+                return cluster;
             } catch (Exception e) {
                 cluster.close();
                 throw e;
diff --git a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/configuration/ClusterConfiguration.java b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/configuration/ClusterConfiguration.java
index 0173373..68958f4 100644
--- a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/configuration/ClusterConfiguration.java
+++ b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/init/configuration/ClusterConfiguration.java
@@ -47,6 +47,7 @@ public class ClusterConfiguration {
         private Optional<Integer> readTimeoutMillis;
         private Optional<Integer> connectTimeoutMillis;
         private Optional<Boolean> useSsl;
+        private Optional<Boolean> durableWrites;
         private Optional<String> username;
         private Optional<String> password;
 
@@ -63,6 +64,7 @@ public class ClusterConfiguration {
             username = Optional.empty();
             password = Optional.empty();
             useSsl = Optional.empty();
+            durableWrites = Optional.empty();
         }
 
         public Builder host(Host host) {
@@ -175,6 +177,12 @@ public class ClusterConfiguration {
             return connectTimeoutMillis(Optional.of(connectTimeoutMillis));
         }
 
+        public Builder disableDurableWrites() {
+            this.durableWrites = Optional.of(false);
+
+            return this;
+        }
+
         public ClusterConfiguration build() {
             return new ClusterConfiguration(
                 hosts.build(),
@@ -188,7 +196,8 @@ public class ClusterConfiguration {
                 connectTimeoutMillis.orElse(DEFAULT_CONNECT_TIMEOUT_MILLIS),
                 useSsl.orElse(false),
                 username,
-                password);
+                password,
+                durableWrites.orElse(true));
         }
     }
 
@@ -283,10 +292,12 @@ public class ClusterConfiguration {
     private final boolean useSsl;
     private final Optional<String> username;
     private final Optional<String> password;
+    private final boolean durableWrites;
 
     public ClusterConfiguration(List<Host> hosts, String keyspace, int replicationFactor, int minDelay, int maxRetry,
                                 QueryLoggerConfiguration queryLoggerConfiguration, Optional<PoolingOptions> poolingOptions,
-                                int readTimeoutMillis, int connectTimeoutMillis, boolean useSsl, Optional<String> username, Optional<String> password) {
+                                int readTimeoutMillis, int connectTimeoutMillis, boolean useSsl, Optional<String> username,
+                                Optional<String> password, boolean durableWrites) {
         this.hosts = hosts;
         this.keyspace = keyspace;
         this.replicationFactor = replicationFactor;
@@ -299,6 +310,11 @@ public class ClusterConfiguration {
         this.useSsl = useSsl;
         this.username = username;
         this.password = password;
+        this.durableWrites = durableWrites;
+    }
+
+    public boolean isDurableWrites() {
+        return durableWrites;
     }
 
     public List<Host> getHosts() {
diff --git a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraCluster.java b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraCluster.java
index 6c95990..aff80b4 100644
--- a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraCluster.java
+++ b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/CassandraCluster.java
@@ -61,19 +61,14 @@ public final class CassandraCluster implements AutoCloseable {
                 .host(host.getHostName())
                 .port(host.getPort())
                 .build();
-            session = new SessionWithInitializedTablesFactory(
-                ClusterConfiguration.builder()
-                    .host(host)
-                    .keyspace(KEYSPACE)
-                    .replicationFactor(1)
-                    .build(),
-                ClusterWithKeyspaceCreatedFactory
-                    .config(cluster, KEYSPACE)
-                    .replicationFactor(1)
-                    .disableDurableWrites()
-                    .clusterWithInitializedKeyspace(),
-                module)
-                .get();
+            ClusterConfiguration clusterConfiguration = ClusterConfiguration.builder()
+                .host(host)
+                .keyspace(KEYSPACE)
+                .replicationFactor(1)
+                .disableDurableWrites()
+                .build();
+            ClusterWithKeyspaceCreatedFactory.createKeyspace(cluster, clusterConfiguration);
+            session = new SessionWithInitializedTablesFactory(clusterConfiguration, cluster, module).get();
             typesProvider = new CassandraTypesProvider(module, session);
         } catch (Exception exception) {
             throw new RuntimeException(exception);
diff --git a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/init/SessionWithInitializedTablesFactoryTest.java b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/init/SessionWithInitializedTablesFactoryTest.java
index 041aaf1..13a4f53 100644
--- a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/init/SessionWithInitializedTablesFactoryTest.java
+++ b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/init/SessionWithInitializedTablesFactoryTest.java
@@ -126,18 +126,18 @@ class SessionWithInitializedTablesFactoryTest {
                 .host(host.getHostName())
                 .port(host.getPort())
                 .build();
+        ClusterConfiguration clusterConfiguration = ClusterConfiguration.builder()
+            .host(host)
+            .keyspace(KEYSPACE)
+            .replicationFactor(1)
+            .disableDurableWrites()
+            .build();
+        ClusterWithKeyspaceCreatedFactory.createKeyspace(cluster, clusterConfiguration);
         return () -> new SessionWithInitializedTablesFactory(
-                ClusterConfiguration.builder()
-                        .host(host)
-                        .keyspace(KEYSPACE)
-                        .replicationFactor(1)
-                        .build(),
-                ClusterWithKeyspaceCreatedFactory
-                        .config(cluster, KEYSPACE)
-                        .replicationFactor(1)
-                        .disableDurableWrites()
-                        .clusterWithInitializedKeyspace(),
-                MODULE).get();
+                clusterConfiguration,
+                cluster,
+                MODULE)
+            .get();
     }
 
     private static void cleanCassandra(Session session) {


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org