use of com.datastax.driver.core.ResultSetFuture in project newts by OpenNMS.
the class CassandraIndexer method update.
@Override
public void update(Collection<Sample> samples) {
Timer.Context ctx = m_updateTimer.time();
Set<StatementGenerator> generators = Sets.newHashSet();
Map<Context, Map<Resource, ResourceMetadata>> cacheQueue = Maps.newHashMap();
for (Sample sample : samples) {
maybeIndexResource(cacheQueue, generators, sample.getContext(), sample.getResource());
maybeIndexResourceAttributes(cacheQueue, generators, sample.getContext(), sample.getResource());
maybeAddMetricName(cacheQueue, generators, sample.getContext(), sample.getResource(), sample.getName());
}
try {
if (!generators.isEmpty()) {
synchronized (statementsInFlight) {
generators.removeAll(statementsInFlight);
statementsInFlight.addAll(generators);
}
m_inserts.mark(generators.size());
// Asynchronously execute the statements
List<ResultSetFuture> futures = Lists.newArrayList();
for (Statement statementToExecute : toStatements(generators)) {
futures.add(m_session.executeAsync(statementToExecute));
}
for (ResultSetFuture future : futures) {
future.getUninterruptibly();
}
}
// Order matters here; We want the cache updated only after a successful Cassandra write.
for (Context context : cacheQueue.keySet()) {
for (Map.Entry<Resource, ResourceMetadata> entry : cacheQueue.get(context).entrySet()) {
m_cache.merge(context, entry.getKey(), entry.getValue());
}
}
} finally {
synchronized (statementsInFlight) {
statementsInFlight.removeAll(generators);
}
ctx.stop();
}
}
use of com.datastax.driver.core.ResultSetFuture in project newts by OpenNMS.
the class CassandraSearcher method search.
@Override
public SearchResults search(Context context, Query query, boolean populateMetricsAndAttributes) {
checkNotNull(context, "context argument");
checkNotNull(query, "query argument");
Timer.Context ctx = m_searchTimer.time();
ConsistencyLevel readConsistency = m_contextConfigurations.getReadConsistency(context);
SearchResults searchResults = new SearchResults();
try {
Set<String> ids;
Query q = query.rewrite();
if (q instanceof BooleanQuery) {
ids = searchForIds(context, (BooleanQuery) q, readConsistency);
} else if (q instanceof TermQuery) {
ids = searchForIds(context, (TermQuery) q, readConsistency);
} else {
throw new IllegalStateException("Unsupported query: " + q);
}
for (final String id : ids) {
if (!populateMetricsAndAttributes) {
Resource resource = new Resource(id);
List<String> emptyList = Collections.emptyList();
searchResults.addResult(resource, emptyList);
} else {
// Fetch the metric names and attributes concurrently
ResultSetFuture attrsFuture = fetchResourceAttributes(context, id, readConsistency);
ResultSetFuture metricsFuture = fetchMetricNames(context, id, readConsistency);
try {
Map<String, String> attrs = getResourceAttributesFromResults(attrsFuture);
Collection<String> metrics = getMetricNamesFromResults(metricsFuture);
Resource resource = attrs.size() > 0 ? new Resource(id, Optional.of(attrs)) : new Resource(id);
searchResults.addResult(resource, metrics);
} catch (ExecutionException | InterruptedException e) {
throw Throwables.propagate(e);
}
}
}
return searchResults;
} finally {
ctx.stop();
}
}
use of com.datastax.driver.core.ResultSetFuture in project ignite by apache.
the class CassandraSessionImplTest method executeFailureTest.
/**
*/
@SuppressWarnings("unchecked")
@Test
public void executeFailureTest() {
Session session1 = mock(Session.class);
Session session2 = mock(Session.class);
when(session1.prepare(nullable(String.class))).thenReturn(preparedStatement1);
when(session2.prepare(nullable(String.class))).thenReturn(preparedStatement2);
ResultSetFuture rsFuture = mock(ResultSetFuture.class);
ResultSet rs = mock(ResultSet.class);
Iterator it = mock(Iterator.class);
when(it.hasNext()).thenReturn(true);
when(it.next()).thenReturn(mock(Row.class));
when(rs.iterator()).thenReturn(it);
when(rsFuture.getUninterruptibly()).thenReturn(rs);
/* @formatter:off */
when(session1.executeAsync(any(Statement.class))).thenThrow(new InvalidQueryException("You may have used a PreparedStatement that was created with another Cluster instance")).thenThrow(new RuntimeException("this session should be refreshed / recreated"));
when(session2.executeAsync(boundStatement1)).thenThrow(new InvalidQueryException("You may have used a PreparedStatement that was created with another Cluster instance"));
when(session2.executeAsync(boundStatement2)).thenReturn(rsFuture);
/* @formatter:on */
Cluster cluster = mock(Cluster.class);
when(cluster.connect()).thenReturn(session1).thenReturn(session2);
when(session1.getCluster()).thenReturn(cluster);
when(session2.getCluster()).thenReturn(cluster);
Cluster.Builder builder = mock(Cluster.Builder.class);
when(builder.build()).thenReturn(cluster);
CassandraSessionImpl cassandraSession = new CassandraSessionImpl(builder, null, ConsistencyLevel.ONE, ConsistencyLevel.ONE, 0, mock(IgniteLogger.class));
BatchExecutionAssistant<String, String> batchExecutionAssistant = new MyBatchExecutionAssistant();
ArrayList<String> data = new ArrayList<>();
for (int i = 0; i < 10; i++) {
data.add(String.valueOf(i));
}
cassandraSession.execute(batchExecutionAssistant, data);
verify(cluster, times(2)).connect();
verify(session1, times(1)).prepare(nullable(String.class));
verify(session2, times(1)).prepare(nullable(String.class));
assertEquals(10, batchExecutionAssistant.processedCount());
}
Aggregations