use of org.locationtech.geowave.core.store.util.RowConsumer in project geowave by locationtech.
the class BatchedRangeRead method executeQueryAsync.
public CloseableIterator<T> executeQueryAsync(final Statement... statements) {
// first create a list of asynchronous query executions
final List<CompletionStage<AsyncResultSet>> futures = Lists.newArrayListWithExpectedSize(statements.length);
final BlockingQueue<Object> results = new LinkedBlockingQueue<>(MAX_BOUNDED_READS_ENQUEUED);
new Thread(new Runnable() {
@Override
public void run() {
// set it to 1 to make sure all queries are submitted in
// the loop
final AtomicInteger queryCount = new AtomicInteger(1);
for (final Statement s : statements) {
try {
readSemaphore.acquire();
final CompletionStage<AsyncResultSet> f = operations.getSession().executeAsync(s);
synchronized (futures) {
futures.add(f);
}
queryCount.incrementAndGet();
f.whenCompleteAsync((result, t) -> {
if (result != null) {
try {
final Iterator<GeoWaveRow> iterator = (Iterator) Streams.stream(ResultSets.newInstance(result)).map(row -> new CassandraRow(row)).filter(filter).iterator();
rowTransformer.apply(rowMerging ? new GeoWaveRowMergingIterator(iterator) : iterator).forEachRemaining(row -> {
try {
results.put(row);
} catch (final InterruptedException e) {
LOGGER.warn("interrupted while waiting to enqueue a cassandra result", e);
}
});
} finally {
checkFinalize(queryCount, results, readSemaphore);
}
} else if (t != null) {
checkFinalize(queryCount, results, readSemaphore);
// can do logging or start counting errors.
if (!(t instanceof CancellationException)) {
LOGGER.error("Failure from async query", t);
throw new RuntimeException(t);
}
}
});
} catch (final InterruptedException e) {
LOGGER.warn("Exception while executing query", e);
readSemaphore.release();
}
}
// then decrement
if (queryCount.decrementAndGet() <= 0) {
// statements submitted
try {
results.put(RowConsumer.POISON);
} catch (final InterruptedException e) {
LOGGER.error("Interrupted while finishing blocking queue, this may result in deadlock!");
}
}
}
}, "Cassandra Query Executor").start();
return new CloseableIteratorWrapper<T>(new Closeable() {
@Override
public void close() throws IOException {
synchronized (futures) {
for (final CompletionStage<AsyncResultSet> f : futures) {
f.toCompletableFuture().cancel(true);
}
}
}
}, new RowConsumer(results));
}
use of org.locationtech.geowave.core.store.util.RowConsumer in project geowave by locationtech.
the class KuduRangeRead method executeQueryAsync.
public CloseableIterator<T> executeQueryAsync(final List<AsyncKuduScanner> scanners) {
final BlockingQueue<Object> results = new LinkedBlockingQueue<>(MAX_BOUNDED_READS_ENQUEUED);
final AtomicBoolean isCanceled = new AtomicBoolean(false);
new Thread(() -> {
final AtomicInteger queryCount = new AtomicInteger(1);
for (final AsyncKuduScanner scanner : scanners) {
try {
readSemaphore.acquire();
executeScanner(scanner, readSemaphore, results, queryCount, isCanceled, visibilityEnabled, filter, rowTransformer, rowMerging);
} catch (final InterruptedException e) {
LOGGER.warn("Exception while executing query", e);
readSemaphore.release();
}
}
// then decrement
if (queryCount.decrementAndGet() <= 0) {
// and if there are no queries, there may not have been any statements submitted
try {
results.put(RowConsumer.POISON);
} catch (final InterruptedException e) {
LOGGER.error("Interrupted while finishing blocking queue, this may result in deadlock!");
}
}
}, "Kudu Query Executor").start();
return new CloseableIteratorWrapper<T>(() -> isCanceled.set(true), new RowConsumer(results));
}
use of org.locationtech.geowave.core.store.util.RowConsumer in project geowave by locationtech.
the class BatchedRangeRead method executeQueryAsync.
private CloseableIterator<T> executeQueryAsync(final List<RangeReadInfo> reads) {
// first create a list of asynchronous query executions
final List<RFuture<Collection<ScoredEntry<GeoWaveRedisPersistedRow>>>> futures = Lists.newArrayListWithExpectedSize(reads.size());
final BlockingQueue<Object> results = new LinkedBlockingQueue<>(MAX_BOUNDED_READS_ENQUEUED);
new Thread(new Runnable() {
@Override
public void run() {
// set it to 1 to make sure all queries are submitted in
// the loop
final AtomicInteger queryCount = new AtomicInteger(1);
for (final RangeReadInfo r : reads) {
try {
ByteArray partitionKey;
if ((r.partitionKey == null) || (r.partitionKey.length == 0)) {
partitionKey = EMPTY_PARTITION_KEY;
} else {
partitionKey = new ByteArray(r.partitionKey);
}
readSemaphore.acquire();
final RFuture<Collection<ScoredEntry<GeoWaveRedisPersistedRow>>> f = setCache.get(partitionKey).entryRangeAsync(r.startScore, true, r.endScore, // sure the end is inclusive and do more precise client-side filtering
((r.endScore <= r.startScore) || (r.explicitEndCheck != null)));
queryCount.incrementAndGet();
f.handle((result, throwable) -> {
if (!f.isSuccess()) {
if (!f.isCancelled()) {
LOGGER.warn("Async Redis query failed", throwable);
}
checkFinalize(readSemaphore, results, queryCount);
return result;
} else {
try {
result.forEach(i -> i.getValue().setPartitionKey(r.partitionKey));
transformAndFilter(result.stream().filter(e -> r.passesExplicitRowChecks(e)).iterator()).forEachRemaining(row -> {
try {
results.put(row);
} catch (final InterruptedException e) {
LOGGER.warn("interrupted while waiting to enqueue a redis result", e);
}
});
} finally {
checkFinalize(readSemaphore, results, queryCount);
}
return result;
}
});
synchronized (futures) {
futures.add(f);
}
} catch (final InterruptedException e) {
LOGGER.warn("Exception while executing query", e);
readSemaphore.release();
}
}
// then decrement
if (queryCount.decrementAndGet() <= 0) {
// statements submitted
try {
results.put(RowConsumer.POISON);
} catch (final InterruptedException e) {
LOGGER.error("Interrupted while finishing blocking queue, this may result in deadlock!");
}
}
}
}, "Redis Query Executor").start();
return new CloseableIteratorWrapper<>(new Closeable() {
@Override
public void close() throws IOException {
List<RFuture<Collection<ScoredEntry<GeoWaveRedisPersistedRow>>>> newFutures;
synchronized (futures) {
newFutures = new ArrayList<>(futures);
}
for (final RFuture<Collection<ScoredEntry<GeoWaveRedisPersistedRow>>> f : newFutures) {
f.cancel(true);
}
}
}, new RowConsumer<>(results));
}
Aggregations