From 02e5f4ab72b893760cc6dd44c9855261b367b881 Mon Sep 17 00:00:00 2001 From: asen Date: Fri, 30 Nov 2018 15:05:56 +0800 Subject: [PATCH 1/6] keyspace init and local_DC default from first connect --- pom.xml | 20 ++++++++--- vertx-cassandra-mapping/pom.xml | 2 +- vertx-cassandra/pom.xml | 2 +- .../cassandra/CassandraConfigurator.java | 2 ++ .../impl/DefaultCassandraSession.java | 2 +- .../EnvironmentCassandraConfigurator.java | 9 +++++ .../impl/JsonCassandraConfigurator.java | 35 ++++++++++++++----- .../ZooKeeperCassandraConfigurator.java | 14 +++++++- 8 files changed, 69 insertions(+), 17 deletions(-) diff --git a/pom.xml b/pom.xml index 0224208..a37e83e 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ vertx-cassandra-parent pom - 3.7.0-SNAPSHOT + 3.8.0-SNAPSHOT vertx-cassandra @@ -21,12 +21,12 @@ 3.3.2 3.11.1 - 3.5.0 + 3.6.0.CR2 2.4.0 - 2.3.0 + 2.3.1 4.2.0 3.1.0 - 4.1.15.Final + 4.1.19.Final "localhost" @@ -145,6 +145,18 @@ + + org.apache.maven.plugins + maven-source-plugin + + + attach-sources + + jar + + + + diff --git a/vertx-cassandra-mapping/pom.xml b/vertx-cassandra-mapping/pom.xml index 6d6a466..58bb124 100644 --- a/vertx-cassandra-mapping/pom.xml +++ b/vertx-cassandra-mapping/pom.xml @@ -7,7 +7,7 @@ vertx-cassandra-parent com.englishtown.vertx - 3.7.0-SNAPSHOT + 3.8.0-SNAPSHOT diff --git a/vertx-cassandra/pom.xml b/vertx-cassandra/pom.xml index 6c8abed..0c02652 100644 --- a/vertx-cassandra/pom.xml +++ b/vertx-cassandra/pom.xml @@ -7,7 +7,7 @@ vertx-cassandra-parent com.englishtown.vertx - 3.7.0-SNAPSHOT + 3.8.0-SNAPSHOT diff --git a/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/CassandraConfigurator.java b/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/CassandraConfigurator.java index 5764c49..ce44da8 100644 --- a/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/CassandraConfigurator.java +++ b/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/CassandraConfigurator.java @@ -82,4 +82,6 @@ public interface CassandraConfigurator { * @param callback */ void onReady(Handler> callback); + + String getKeyspace(); } diff --git a/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/DefaultCassandraSession.java b/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/DefaultCassandraSession.java index 2e2fc7b..421b911 100644 --- a/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/DefaultCassandraSession.java +++ b/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/DefaultCassandraSession.java @@ -176,7 +176,7 @@ public void reconnect() { public void reconnectAsync(Handler> callback) { logger.debug("Call to reconnect the session has been made"); Session oldSession = session; - FutureUtils.addCallback(cluster.connectAsync(), new FutureCallback() { + FutureUtils.addCallback(cluster.connectAsync(this.getConfigurator().getKeyspace()), new FutureCallback() { @Override public void onSuccess(Session session) { DefaultCassandraSession.this.session = session; diff --git a/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/EnvironmentCassandraConfigurator.java b/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/EnvironmentCassandraConfigurator.java index 593f119..c7042a6 100644 --- a/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/EnvironmentCassandraConfigurator.java +++ b/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/EnvironmentCassandraConfigurator.java @@ -21,6 +21,7 @@ public class EnvironmentCassandraConfigurator extends JsonCassandraConfigurator public static final String ENV_VAR_LOCAL_DC = "CASSANDRA_LOCAL_DC"; public static final String ENV_VAR_USERNAME = "CASSANDRA_USERNAME"; public static final String ENV_VAR_PASSWORD = "CASSANDRA_PASSWORD"; + public static final String ENV_KEYSPACE = "KEYSPACE"; public static final Logger logger = LoggerFactory.getLogger(EnvironmentCassandraConfigurator.class); private final EnvVarDelegate envVarDelegate; @@ -42,6 +43,7 @@ private void init() { initSeeds(); initLoadBalancingPolicy(); initAuthProvider(); + initKeyspace(); } private void initSeeds() { @@ -88,4 +90,11 @@ public String get(String name) { } } + private void initKeyspace(){ + String keyspace = envVarDelegate.get(ENV_KEYSPACE); + if (!Strings.isNullOrEmpty(keyspace)){ + this.keyspace = keyspace; + } + + } } diff --git a/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/JsonCassandraConfigurator.java b/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/JsonCassandraConfigurator.java index cd11374..27003b9 100644 --- a/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/JsonCassandraConfigurator.java +++ b/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/JsonCassandraConfigurator.java @@ -11,6 +11,7 @@ import io.vertx.core.Vertx; import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; +import javafx.util.Builder; import javax.inject.Inject; import java.util.ArrayList; @@ -31,6 +32,8 @@ public class JsonCassandraConfigurator implements CassandraConfigurator { protected MetricsOptions metricsOptions; protected AuthProvider authProvider; + protected String keyspace; + protected final List DEFAULT_SEEDS = ImmutableList.of("127.0.0.1"); public static final String CONFIG_CASSANDRA = "cassandra"; @@ -61,6 +64,8 @@ public class JsonCassandraConfigurator implements CassandraConfigurator { public static final String CONSISTENCY_SERIAL = "SERIAL"; public static final String CONSISTENCY_LOCAL_SERIAL = "LOCAL_SERIAL"; + public static final String KEYSPACE = "KEYSPACE"; + @Inject public JsonCassandraConfigurator(Vertx vertx) { this(vertx.getOrCreateContext().config().getJsonObject(CONFIG_CASSANDRA, new JsonObject())); @@ -135,6 +140,7 @@ protected void init(JsonObject config) { initQueryOptions(config.getJsonObject(CONFIG_QUERY, config)); initMetricsOptions(config.getJsonObject(CONFIG_METRICS)); initAuthProvider(config.getJsonObject(CONFIG_AUTH)); + initKeyspace(config.getString(KEYSPACE)); } @@ -152,6 +158,12 @@ protected void initSeeds(JsonArray seeds) { } } + protected void initKeyspace(String keyspace){ + if (!Strings.isNullOrEmpty(keyspace)){ + this.keyspace = keyspace; + } + } + protected void initPort(JsonObject config) { Integer i = config.getInteger(CONFIG_PORT); @@ -189,14 +201,16 @@ protected void initLoadBalancingPolicy(JsonObject loadBalancing) { String localDc = loadBalancing.getString("local_dc"); int usedHostsPerRemoteDc = loadBalancing.getInteger("used_hosts_per_remote_dc", 0); - if (localDc == null || localDc.isEmpty()) { - throw new IllegalArgumentException("A DCAwareRoundRobinPolicy requires a local_dc in configuration."); - } +// if (localDc == null || localDc.isEmpty()) { +// throw new IllegalArgumentException("A DCAwareRoundRobinPolicy requires a local_dc in configuration."); +// } - loadBalancingPolicy = DCAwareRoundRobinPolicy.builder() - .withLocalDc(localDc) - .withUsedHostsPerRemoteDc(usedHostsPerRemoteDc) - .build(); + DCAwareRoundRobinPolicy.Builder builder= DCAwareRoundRobinPolicy.builder() + .withUsedHostsPerRemoteDc(usedHostsPerRemoteDc); + if (!Strings.isNullOrEmpty(localDc)){ + builder.withLocalDc(localDc); + } + loadBalancingPolicy = builder.build(); } else { @@ -238,7 +252,7 @@ protected void initReconnectionPolicy(JsonObject reconnection) { throw new IllegalArgumentException("ConstantReconnectionPolicy requires a delay in configuration"); } - reconnectionPolicy = new ConstantReconnectionPolicy(delay.intValue()); + reconnectionPolicy = new ConstantReconnectionPolicy(delay); } else if ("ExponentialReconnectionPolicy".equalsIgnoreCase(name) || "exponential".equalsIgnoreCase(name)) { Long baseDelay = reconnection.getLong("base_delay"); @@ -248,7 +262,7 @@ protected void initReconnectionPolicy(JsonObject reconnection) { throw new IllegalArgumentException("ExponentialReconnectionPolicy requires base_delay and max_delay in configuration"); } - reconnectionPolicy = new ExponentialReconnectionPolicy(baseDelay.longValue(), maxDelay.longValue()); + reconnectionPolicy = new ExponentialReconnectionPolicy(baseDelay, maxDelay); } else { Class clazz; @@ -471,4 +485,7 @@ protected void initAuthProvider(JsonObject auth) { } + public String getKeyspace() { + return keyspace; + } } diff --git a/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/zookeeper/ZooKeeperCassandraConfigurator.java b/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/zookeeper/ZooKeeperCassandraConfigurator.java index 89302ee..1f45bb2 100644 --- a/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/zookeeper/ZooKeeperCassandraConfigurator.java +++ b/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/zookeeper/ZooKeeperCassandraConfigurator.java @@ -5,6 +5,7 @@ import com.englishtown.vertx.cassandra.impl.EnvironmentCassandraConfigurator; import com.englishtown.vertx.curator.CuratorClient; import com.englishtown.vertx.curator.promises.WhenConfiguratorHelper; +import com.google.common.base.Strings; import io.vertx.core.AsyncResult; import io.vertx.core.Future; import io.vertx.core.Handler; @@ -135,6 +136,18 @@ private void initZooKeeper() { })); } + if (Strings.isNullOrEmpty(keyspace)){ + promises.add(helper.getConfigElement(ZKPaths.makePath(getPathPrefix(), "KEYSPACE")).then( + value -> { + String keyspace = value.asString(); + if (!Strings.isNullOrEmpty(keyspace)){ + initKeyspace(keyspace); + } + return null; + } + )); + } + when.all(promises) .then(aVoid -> { runOnReadyCallbacks(Future.succeededFuture(null)); @@ -164,5 +177,4 @@ public void onReady(Handler> callback) { protected String getPathPrefix() { return pathPrefix; } - } From 340c14e4b8cc17114f3583d983dd5aecbbd09f1c Mon Sep 17 00:00:00 2001 From: "asen.pan" Date: Fri, 30 Nov 2018 15:39:14 +0800 Subject: [PATCH 2/6] note --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 18664c1..67e781a 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ The main configuration is via the normal config.json file for a Vert.x module. { "cassandra": { "seeds": [], - + "KEYSPACE": , "query": { "consistency_level": , "serial_consistency_level": , @@ -77,11 +77,12 @@ The main configuration is via the normal config.json file for a Vert.x module. * `seeds` - an array of string seed IP or host names. At least one seed must be provided. * `lb_policy_name` - (optional) the load balancing policy name. The following values are accepted: - * "DCAwareRoundRobinPolicy" - requires string field `local_dc` and optional numeric field `used_hosts_per_remote_dc` + * "DCAwareRoundRobinPolicy" - optional string field `local_dc` and optional numeric field `used_hosts_per_remote_dc` * Any FQCN such of a class that implements `LoadBalancingPolicy` * `reconnect_policy_name` - (optional) the reconnect policy name. The following values are accepted: * "constant"|"ConstantReconnectionPolicy" - creates a `ConstantReconnectionPolicy` policy. Expects additional numeric field `delay` in ms. * "exponential"|"ExponentialReconnectionPolicy" - creates an `ExponentialReconnectionPolicy` policy. Expects additional numeric fields `base_delay` and `max_delay` in ms. +* 'KEYSPACE' - the default keyspace name, optional. Refer to the [Cassandra Java driver documentation](http://www.datastax.com/documentation/developer/java-driver/2.0/index.html) for a description of the remaining configuration options. From ef11277fe437033bca62f3fe8d9e826a3ad55a04 Mon Sep 17 00:00:00 2001 From: asen Date: Fri, 30 Nov 2018 15:41:03 +0800 Subject: [PATCH 3/6] update version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index a37e83e..4acc770 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ vertx-cassandra-parent pom - 3.8.0-SNAPSHOT + 3.7.0-SNAPSHOT vertx-cassandra From a3a81eac92aa41cf9f27c5d526db695636b9228f Mon Sep 17 00:00:00 2001 From: asen Date: Fri, 30 Nov 2018 17:57:45 +0800 Subject: [PATCH 4/6] Metrics --- vertx-cassandra-mapping/pom.xml | 2 +- vertx-cassandra/pom.xml | 2 +- .../vertx/cassandra/impl/DefaultCassandraSession.java | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/vertx-cassandra-mapping/pom.xml b/vertx-cassandra-mapping/pom.xml index 58bb124..6d6a466 100644 --- a/vertx-cassandra-mapping/pom.xml +++ b/vertx-cassandra-mapping/pom.xml @@ -7,7 +7,7 @@ vertx-cassandra-parent com.englishtown.vertx - 3.8.0-SNAPSHOT + 3.7.0-SNAPSHOT diff --git a/vertx-cassandra/pom.xml b/vertx-cassandra/pom.xml index 0c02652..6c8abed 100644 --- a/vertx-cassandra/pom.xml +++ b/vertx-cassandra/pom.xml @@ -7,7 +7,7 @@ vertx-cassandra-parent com.englishtown.vertx - 3.8.0-SNAPSHOT + 3.7.0-SNAPSHOT diff --git a/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/DefaultCassandraSession.java b/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/DefaultCassandraSession.java index 421b911..d15d64b 100644 --- a/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/DefaultCassandraSession.java +++ b/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/DefaultCassandraSession.java @@ -104,6 +104,9 @@ protected void init(CassandraConfigurator configurator) { if (!configurator.getMetricsOptions().isJMXReportingEnabled()) { clusterBuilder.withoutJMXReporting(); } + if (!configurator.getMetricsOptions().isEnabled()){ + clusterBuilder.withoutMetrics(); + } } if (configurator.getAuthProvider() != null) { From 2757f8a24e52ec41fc88e504a4b65197b4ba0ef6 Mon Sep 17 00:00:00 2001 From: asen Date: Fri, 30 Nov 2018 19:31:29 +0800 Subject: [PATCH 5/6] default keyspace is null --- .../vertx/cassandra/impl/DefaultCassandraSession.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/DefaultCassandraSession.java b/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/DefaultCassandraSession.java index d15d64b..5cd00fc 100644 --- a/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/DefaultCassandraSession.java +++ b/vertx-cassandra/src/main/java/com/englishtown/vertx/cassandra/impl/DefaultCassandraSession.java @@ -4,6 +4,7 @@ import com.englishtown.vertx.cassandra.CassandraConfigurator; import com.englishtown.vertx.cassandra.CassandraSession; import com.englishtown.vertx.cassandra.FutureUtils; +import com.google.common.base.Strings; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.ListenableFuture; import io.vertx.core.AsyncResult; @@ -179,7 +180,8 @@ public void reconnect() { public void reconnectAsync(Handler> callback) { logger.debug("Call to reconnect the session has been made"); Session oldSession = session; - FutureUtils.addCallback(cluster.connectAsync(this.getConfigurator().getKeyspace()), new FutureCallback() { + FutureUtils.addCallback(Strings.isNullOrEmpty(this.getConfigurator().getKeyspace()) ? cluster.connectAsync() + : cluster.connectAsync(this.getConfigurator().getKeyspace()), new FutureCallback() { @Override public void onSuccess(Session session) { DefaultCassandraSession.this.session = session; From 51ad6a300f4a0367fd55574960f00e52ed628d4d Mon Sep 17 00:00:00 2001 From: asen Date: Tue, 4 Dec 2018 11:17:37 +0800 Subject: [PATCH 6/6] vert x version --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 4acc770..40d4d1e 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ 3.3.2 3.11.1 - 3.6.0.CR2 + 3.6.0 2.4.0 2.3.1 4.2.0 @@ -186,5 +186,5 @@ HEAD https://github.com/ef-labs/${project.artifactId} - +