-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow customization of C* cluster and session creation. #14
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
dependencies { | ||
compile "io.ratpack:ratpack-core:${ratpackVersion}" | ||
compile "io.ratpack:ratpack-guice:${ratpackVersion}" | ||
|
||
compile 'com.datastax.cassandra:cassandra-driver-core:3.1.0' | ||
compile 'smartthings.brave:smartthings-brave-cassandra-latencytracker:0.1.14' | ||
compile project(':ratpack-cassandra') | ||
|
||
testCompile "io.ratpack:ratpack-groovy-test:${ratpackVersion}" | ||
//Note cassandra unit pulls in netty-all which may need changed if the version conflicts with Ratpacks | ||
testCompile 'org.cassandraunit:cassandra-unit:3.0.0.1' | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package smartthings.ratpack.cassandra.zipkin; | ||
|
||
import com.datastax.driver.core.Session; | ||
import com.google.inject.Scopes; | ||
import smartthings.ratpack.cassandra.CassandraHealthCheck; | ||
import smartthings.ratpack.cassandra.CassandraModule; | ||
import smartthings.ratpack.cassandra.CassandraService; | ||
import smartthings.ratpack.cassandra.RatpackSession; | ||
|
||
public class CassandraTracingModule extends CassandraModule { | ||
|
||
@Override | ||
protected void configure() { | ||
bind(TracedSession.class).in(Scopes.SINGLETON); | ||
bind(Session.class).to(TracedSession.class); | ||
bind(RatpackSession.class).to(TracedSession.class); | ||
bind(CassandraService.class).in(Scopes.SINGLETON); | ||
bind(CassandraHealthCheck.class).in(Scopes.SINGLETON); | ||
} | ||
|
||
public static class Config extends CassandraModule.Config { | ||
String serviceName; | ||
|
||
public String getServiceName() { | ||
return serviceName; | ||
} | ||
|
||
public void setServiceName(String serviceName) { | ||
this.serviceName = serviceName; | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package smartthings.ratpack.cassandra.zipkin; | ||
|
||
import com.datastax.driver.core.Cluster; | ||
import com.datastax.driver.core.Session; | ||
import com.github.kristofa.brave.Brave; | ||
import com.google.inject.Inject; | ||
import smartthings.ratpack.cassandra.AbstractSession; | ||
|
||
public final class TracedSession extends AbstractSession { | ||
|
||
private final Brave brave; | ||
private final Cluster cluster; | ||
private final String keyspace; | ||
private final CassandraTracingModule.Config config; | ||
|
||
@Inject | ||
public TracedSession(Cluster cluster, Brave brave, CassandraTracingModule.Config config) { | ||
super(cluster, config.getKeyspace()); | ||
this.brave = brave; | ||
this.cluster = cluster; | ||
this.keyspace = config.getKeyspace(); | ||
this.config = config; | ||
} | ||
|
||
protected Session createDelegate() { | ||
Session session = (keyspace != null && !keyspace.equals("")) ? cluster.connect(keyspace) : cluster.connect(); | ||
|
||
return smartthings.brave.cassandra.TracedSession.create(session, brave, config.getServiceName()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @llinder, the resultset callback works properly with Ratpack when we use this smartthings.brave.cassandra.TracedSession? (https://github.com/SmartThingsOSS/smartthings-brave/blob/master/brave-cassandra-latencytracker/src/main/java/smartthings/brave/cassandra/TracedSession.java#L122). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here's the stacktrace Brian is seeing:
From a really quick look, my guess is that the traced version has brave internals operating in a non-ratpack way. I think the problem might be here: I wonder if changing to a Blocking.get would solve for both (Traced and Non-Traced). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the problem is that the current span is being injected into the Ratpack registry from a thread spawned by the Cassandra driver. What we will need to figure out is how to call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We might want to consider using a different trace context. I can't think of a good reason it needs to interact with the Ratpack lifecycle at that level. Even a thread local context might be suitable for our needs. I will try to do some investigation work later today. |
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
package smartthings.ratpack.cassandra; | ||
|
||
import com.datastax.driver.core.CloseFuture; | ||
import com.datastax.driver.core.Cluster; | ||
import com.datastax.driver.core.PreparedStatement; | ||
import com.datastax.driver.core.RegularStatement; | ||
import com.datastax.driver.core.ResultSet; | ||
import com.datastax.driver.core.ResultSetFuture; | ||
import com.datastax.driver.core.Session; | ||
import com.datastax.driver.core.Statement; | ||
import com.google.common.util.concurrent.ListenableFuture; | ||
import java.util.Map; | ||
import ratpack.exec.Promise; | ||
import ratpack.service.Service; | ||
import ratpack.service.StartEvent; | ||
import ratpack.service.StopEvent; | ||
|
||
/** | ||
* Abstract class that transparently provides the role of both an | ||
* injectable {@link Session} and fills the contract of a Ratpack {@link Service}. | ||
* | ||
* This is an abstract class to allow users to extend it for different reasons including | ||
* the ability to provide different types for DI resolution. | ||
*/ | ||
public abstract class AbstractSession implements RatpackSession, Session, Service { | ||
|
||
private final Cluster cluster; | ||
private final String keyspace; | ||
private Session delegate; | ||
|
||
public AbstractSession(Cluster cluster) { | ||
this.cluster = cluster; | ||
this.keyspace = null; | ||
} | ||
|
||
public AbstractSession(Cluster cluster, String keyspace) { | ||
this.cluster = cluster; | ||
this.keyspace = keyspace; | ||
} | ||
|
||
@Override | ||
public String getLoggedKeyspace() { | ||
return getDelegate().getLoggedKeyspace(); | ||
} | ||
|
||
@Override | ||
public Session init() { | ||
return getDelegate().init(); | ||
} | ||
|
||
@Override | ||
public ListenableFuture<Session> initAsync() { | ||
return getDelegate().initAsync(); | ||
} | ||
|
||
@Override | ||
public ResultSet execute(String s) { | ||
return getDelegate().execute(s); | ||
} | ||
|
||
@Override | ||
public ResultSet execute(String s, Object... objects) { | ||
return getDelegate().execute(s, objects); | ||
} | ||
|
||
@Override | ||
public ResultSet execute(String s, Map<String, Object> map) { | ||
return getDelegate().execute(s, map); | ||
} | ||
|
||
@Override | ||
public ResultSet execute(Statement statement) { | ||
return getDelegate().execute(statement); | ||
} | ||
|
||
@Override | ||
public ResultSetFuture executeAsync(String s) { | ||
return getDelegate().executeAsync(s); | ||
} | ||
|
||
@Override | ||
public ResultSetFuture executeAsync(String s, Object... objects) { | ||
return getDelegate().executeAsync(s, objects); | ||
} | ||
|
||
@Override | ||
public ResultSetFuture executeAsync(String s, Map<String, Object> map) { | ||
return getDelegate().executeAsync(s, map); | ||
} | ||
|
||
@Override | ||
public ResultSetFuture executeAsync(Statement statement) { | ||
return getDelegate().executeAsync(statement); | ||
} | ||
|
||
@Override | ||
public PreparedStatement prepare(String s) { | ||
return getDelegate().prepare(s); | ||
} | ||
|
||
@Override | ||
public PreparedStatement prepare(RegularStatement regularStatement) { | ||
return getDelegate().prepare(regularStatement); | ||
} | ||
|
||
@Override | ||
public ListenableFuture<PreparedStatement> prepareAsync(String s) { | ||
return getDelegate().prepareAsync(s); | ||
} | ||
|
||
@Override | ||
public | ||
ListenableFuture<PreparedStatement> prepareAsync(RegularStatement regularStatement) { | ||
return getDelegate().prepareAsync(regularStatement); | ||
} | ||
|
||
@Override | ||
public Promise<ResultSet> executePromise(String query) { | ||
return Promise.async(upstream -> { | ||
ResultSetFuture resultSetFuture = getDelegate().executeAsync(query); | ||
upstream.accept(resultSetFuture); | ||
}); | ||
} | ||
|
||
@Override | ||
public Promise<ResultSet> executePromise(String query, Object... values) { | ||
return Promise.async(upstream -> { | ||
ResultSetFuture resultSetFuture = getDelegate().executeAsync(query, values); | ||
upstream.accept(resultSetFuture); | ||
}); | ||
} | ||
|
||
@Override | ||
public Promise<ResultSet> executePromise(String query, Map<String, Object> values) { | ||
return Promise.async(upstream -> { | ||
ResultSetFuture resultSetFuture = getDelegate().executeAsync(query, values); | ||
upstream.accept(resultSetFuture); | ||
}); | ||
} | ||
|
||
@Override | ||
public Promise<ResultSet> executePromise(Statement statement) { | ||
return Promise.async(upstream -> { | ||
ResultSetFuture resultSetFuture = getDelegate().executeAsync(statement); | ||
upstream.accept(resultSetFuture); | ||
}); | ||
} | ||
|
||
@Override | ||
public Promise<PreparedStatement> preparePromise(String query) { | ||
return Promise.async(upstream -> { | ||
ListenableFuture<PreparedStatement> f = getDelegate().prepareAsync(query); | ||
upstream.accept(f); | ||
}); | ||
} | ||
|
||
@Override | ||
public Promise<PreparedStatement> preparePromise(RegularStatement statement) { | ||
return Promise.async(upstream -> { | ||
ListenableFuture<PreparedStatement> f = getDelegate().prepareAsync(statement); | ||
upstream.accept(f); | ||
}); | ||
} | ||
|
||
@Override | ||
public CloseFuture closeAsync() { | ||
return getDelegate().closeAsync(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
getDelegate().close(); | ||
} | ||
|
||
@Override | ||
public boolean isClosed() { | ||
return getDelegate().isClosed(); | ||
} | ||
|
||
@Override | ||
public Cluster getCluster() { | ||
return cluster; | ||
} | ||
|
||
@Override | ||
public State getState() { | ||
return getDelegate().getState(); | ||
} | ||
|
||
@Override | ||
public void onStart(StartEvent event) throws Exception { | ||
delegate = getDelegate(); | ||
} | ||
|
||
@Override | ||
public void onStop(StopEvent event) throws Exception { | ||
if (getDelegate() != null && !getDelegate().isClosed()) { | ||
getDelegate().closeAsync(); | ||
} | ||
} | ||
|
||
protected final Session getDelegate() { | ||
if (delegate == null) { | ||
synchronized (this) { | ||
if (delegate == null) { | ||
delegate = createDelegate(); | ||
} | ||
} | ||
} | ||
return delegate; | ||
} | ||
|
||
protected Session createDelegate() { | ||
return (keyspace != null && !keyspace.equals("")) ? cluster.connect(keyspace) : cluster.connect(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this works as long as the provided implementation extends
@Service
. It would be good to test that part more though.