You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pr@cassandra.apache.org by GitBox <gi...@apache.org> on 2019/02/27 15:22:33 UTC

[GitHub] clohfink commented on a change in pull request #3: Add integration tests task for CASSANDRA-15031

clohfink commented on a change in pull request #3: Add integration tests task for CASSANDRA-15031
URL: https://github.com/apache/cassandra-sidecar/pull/3#discussion_r260798662
 
 

 ##########
 File path: src/main/java/org/apache/cassandra/sidecar/CQLSession.java
 ##########
 @@ -0,0 +1,112 @@
+package org.apache.cassandra.sidecar;
+
+import java.net.InetSocketAddress;
+import java.util.Collections;
+import java.util.Optional;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.datastax.driver.core.Cluster;
+import com.datastax.driver.core.ConsistencyLevel;
+import com.datastax.driver.core.NettyOptions;
+import com.datastax.driver.core.QueryOptions;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.policies.ExponentialReconnectionPolicy;
+import com.datastax.driver.core.policies.ReconnectionPolicy;
+import com.datastax.driver.core.policies.RoundRobinPolicy;
+import com.datastax.driver.core.policies.WhiteListPolicy;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+
+/**
+ * Represents a connection to Cassandra cluster. Currently supports returning the local connection only as
+ * defined in the Configuration.
+ */
+@Singleton
+public class CQLSession
+{
+    private static final Logger logger = LoggerFactory.getLogger(CassandraSidecarDaemon.class);
+    private Session localSession;
+    private final InetSocketAddress inet;
+    private final WhiteListPolicy wlp;
+    private NettyOptions nettyOptions;
+    private QueryOptions queryOptions;
+    private ReconnectionPolicy reconnectionPolicy;
+
+    @Inject
+    public CQLSession(Configuration configuration)
+    {
+        inet = InetSocketAddress.createUnresolved(configuration.getCassandraHost(), configuration.getCassandraPort());
+        wlp = new WhiteListPolicy(new RoundRobinPolicy(), Collections.singletonList(inet));
+        this.nettyOptions = new NettyOptions();
+        this.queryOptions = new QueryOptions().setConsistencyLevel(ConsistencyLevel.ONE);
+        this.reconnectionPolicy = new ExponentialReconnectionPolicy(1000,
+                                                                    configuration.getHealthCheckFrequencyMillis());
+    }
+
+    @VisibleForTesting
+    CQLSession(InetSocketAddress target, NettyOptions options)
+    {
+        inet = target;
+        wlp = new WhiteListPolicy(new RoundRobinPolicy(), Collections.singletonList(inet));
+        this.nettyOptions = options;
+        this.queryOptions = new QueryOptions().setConsistencyLevel(ConsistencyLevel.ONE);
+        reconnectionPolicy = new ExponentialReconnectionPolicy(100, 1000);
+    }
+
+    /**
+     * Provides a Session connected only to the local node from configuration. If optional is null it means the
+     * the connection was not able to be established. The session still might throw a NoHostAvailableException if the
+     * local host goes offline or otherwise unavailable.
+     *
+     * @return Optional<Session>
+     */
+    public synchronized Optional<Session> getLocalCql()
+    {
+        Cluster cluster = null;
+        try
+        {
+            if (localSession == null)
+            {
+                cluster = Cluster.builder()
+                                 .addContactPointsWithPorts(inet)
+                                 .withLoadBalancingPolicy(wlp)
+                                 .withQueryOptions(queryOptions)
+                                 .withReconnectionPolicy(reconnectionPolicy)
+                                 .withoutMetrics()
+                                 // tests can create a lot of these Cluster objects, to avoid creating HWTs and
+                                 // event thread pools for each we have the override
+                                 .withNettyOptions(nettyOptions)
+                                 .build();
+                localSession = cluster.connect();
+            }
+        }
+        catch (Exception e)
+        {
+            logger.debug("Failed to reach Cassandra", e);
 
 Review comment:
   its actually an expected scenario with a NoHostAvailableException, so ill just break that one out as a debug and an error for other exceptions

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscribe@cassandra.apache.org
For additional commands, e-mail: pr-help@cassandra.apache.org