use of com.yahoo.elide.async.models.AsyncAPI in project elide by yahoo.
the class AsyncAPICancelRunnable method cancelAsyncAPI.
/**
* This method cancels queries based on threshold.
* @param type AsyncAPI Type Implementation.
*/
protected <T extends AsyncAPI> void cancelAsyncAPI(Class<T> type) {
try {
TransactionRegistry transactionRegistry = elide.getTransactionRegistry();
Map<UUID, DataStoreTransaction> runningTransactionMap = transactionRegistry.getRunningTransactions();
// Running transaction UUIDs
Set<UUID> runningTransactionUUIDs = runningTransactionMap.keySet();
// Construct filter expression
PathElement statusPathElement = new PathElement(type, QueryStatus.class, "status");
FilterExpression fltStatusExpression = new InPredicate(statusPathElement, QueryStatus.CANCELLED, QueryStatus.PROCESSING, QueryStatus.QUEUED);
Iterable<T> asyncAPIIterable = asyncAPIDao.loadAsyncAPIByFilter(fltStatusExpression, type);
// Active AsyncAPI UUIDs
Set<UUID> asyncTransactionUUIDs = StreamSupport.stream(asyncAPIIterable.spliterator(), false).filter(query -> query.getStatus() == QueryStatus.CANCELLED || TimeUnit.SECONDS.convert(Math.abs(new Date(System.currentTimeMillis()).getTime() - query.getCreatedOn().getTime()), TimeUnit.MILLISECONDS) > maxRunTimeSeconds).map(query -> UUID.fromString(query.getRequestId())).collect(Collectors.toSet());
// AsyncAPI UUIDs that have active transactions
Set<UUID> queryUUIDsToCancel = Sets.intersection(runningTransactionUUIDs, asyncTransactionUUIDs);
// AsyncAPI IDs that need to be cancelled
Set<String> queryIDsToCancel = queryUUIDsToCancel.stream().map(uuid -> StreamSupport.stream(asyncAPIIterable.spliterator(), false).filter(query -> query.getRequestId().equals(uuid.toString())).map(T::getId).findFirst().orElseThrow(IllegalStateException::new)).collect(Collectors.toSet());
// Cancel Transactions
queryUUIDsToCancel.stream().forEach((uuid) -> {
DataStoreTransaction runningTransaction = transactionRegistry.getRunningTransaction(uuid);
if (runningTransaction != null) {
JsonApiDocument jsonApiDoc = new JsonApiDocument();
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
RequestScope scope = new RequestScope("", "query", NO_VERSION, jsonApiDoc, runningTransaction, null, queryParams, Collections.emptyMap(), uuid, elide.getElideSettings());
runningTransaction.cancel(scope);
}
});
// Change queryStatus for cancelled queries
if (!queryIDsToCancel.isEmpty()) {
PathElement idPathElement = new PathElement(type, String.class, "id");
FilterExpression fltIdExpression = new InPredicate(idPathElement, queryIDsToCancel);
asyncAPIDao.updateStatusAsyncAPIByFilter(fltIdExpression, QueryStatus.CANCEL_COMPLETE, type);
}
} catch (Exception e) {
log.error("Exception in scheduled cancellation: {}", e.toString());
}
}
use of com.yahoo.elide.async.models.AsyncAPI in project elide by yahoo.
the class ElideAsyncConfiguration method getTableExportHook.
// TODO Remove this method when ElideSettings has all the settings.
// Then the check can be done in TableExportHook.
// Trying to avoid adding too many individual properties to ElideSettings for now.
// https://github.com/yahoo/elide/issues/1803
private TableExportHook getTableExportHook(AsyncExecutorService asyncExecutorService, ElideConfigProperties settings, Map<ResultType, TableExportFormatter> supportedFormatters, ResultStorageEngine resultStorageEngine) {
boolean exportEnabled = ElideAutoConfiguration.isExportEnabled(settings.getAsync());
TableExportHook tableExportHook = null;
if (exportEnabled) {
tableExportHook = new TableExportHook(asyncExecutorService, settings.getAsync().getMaxAsyncAfterSeconds(), supportedFormatters, resultStorageEngine);
} else {
tableExportHook = new TableExportHook(asyncExecutorService, settings.getAsync().getMaxAsyncAfterSeconds(), supportedFormatters, resultStorageEngine) {
@Override
public void validateOptions(AsyncAPI export, RequestScope requestScope) {
throw new InvalidOperationException("TableExport is not supported.");
}
};
}
return tableExportHook;
}
use of com.yahoo.elide.async.models.AsyncAPI in project elide by yahoo.
the class AsyncAPICancelRunnableTest method testTimeBasedCancellation.
@Test
public void testTimeBasedCancellation() {
DataStoreTransaction dtx = elide.getDataStore().beginTransaction();
transactionRegistry.addRunningTransaction(UUID.fromString("edc4a871-dff2-4054-804e-d80075cf828d"), dtx);
transactionRegistry.addRunningTransaction(UUID.fromString("edc4a871-dff2-4054-804e-d80075cf827d"), dtx);
AsyncQuery asyncQuery1 = createAsyncQueryTestObject("edc4a871-dff2-4054-804e-d80075cf828d", System.currentTimeMillis(), QueryStatus.QUEUED);
AsyncQuery asyncQuery2 = createAsyncQueryTestObject("edc4a871-dff2-4054-804e-d80075cf827d", 1577883600000L, QueryStatus.QUEUED);
AsyncQuery asyncQuery3 = createAsyncQueryTestObject("edc4a871-dff2-4054-804e-d80075cf826d", 1577883600000L, QueryStatus.QUEUED);
Collection<AsyncAPI> asyncCollection = new ArrayList<>();
asyncCollection.add(asyncQuery1);
asyncCollection.add(asyncQuery2);
asyncCollection.add(asyncQuery3);
when(cancelThread.getAsyncAPIDao().loadAsyncAPIByFilter(any(), any())).thenReturn(asyncCollection);
cancelThread.cancelAsyncAPI(AsyncQuery.class);
ArgumentCaptor<FilterExpression> filterCaptor = ArgumentCaptor.forClass(FilterExpression.class);
ArgumentCaptor<QueryStatus> statusCaptor = ArgumentCaptor.forClass(QueryStatus.class);
verify(asyncAPIDao, times(1)).updateStatusAsyncAPIByFilter(filterCaptor.capture(), statusCaptor.capture(), any());
assertEquals("asyncQuery.id IN [[edc4a871-dff2-4054-804e-d80075cf827d]]", filterCaptor.getValue().toString());
assertEquals("CANCEL_COMPLETE", statusCaptor.getValue().toString());
}
use of com.yahoo.elide.async.models.AsyncAPI in project elide by yahoo.
the class AsyncAPICancelRunnableTest method testActiveTransactionCancellation.
@Test
public void testActiveTransactionCancellation() {
DataStoreTransaction dtx = elide.getDataStore().beginTransaction();
transactionRegistry.addRunningTransaction(UUID.fromString("edc4a871-dff2-4054-804e-d80075cf828d"), dtx);
AsyncQuery asyncQuery1 = createAsyncQueryTestObject("edc4a871-dff2-4054-804e-d80075cf828d", 1577883600000L, QueryStatus.QUEUED);
AsyncQuery asyncQuery2 = createAsyncQueryTestObject("edc4a871-dff2-4054-804e-d80075cf827d", 1577883600000L, QueryStatus.QUEUED);
Collection<AsyncAPI> asyncCollection = new ArrayList<>();
asyncCollection.add(asyncQuery1);
asyncCollection.add(asyncQuery2);
when(cancelThread.getAsyncAPIDao().loadAsyncAPIByFilter(any(), any())).thenReturn(asyncCollection);
cancelThread.cancelAsyncAPI(AsyncQuery.class);
ArgumentCaptor<FilterExpression> filterCaptor = ArgumentCaptor.forClass(FilterExpression.class);
ArgumentCaptor<QueryStatus> statusCaptor = ArgumentCaptor.forClass(QueryStatus.class);
verify(asyncAPIDao, times(1)).loadAsyncAPIByFilter(any(), any());
verify(asyncAPIDao, times(1)).updateStatusAsyncAPIByFilter(filterCaptor.capture(), statusCaptor.capture(), any());
assertEquals("asyncQuery.id IN [[edc4a871-dff2-4054-804e-d80075cf828d]]", filterCaptor.getValue().toString());
assertEquals("CANCEL_COMPLETE", statusCaptor.getValue().toString());
}
use of com.yahoo.elide.async.models.AsyncAPI in project elide by yahoo.
the class AsyncAPICancelRunnableTest method testStatusBasedFilter.
@Test
public void testStatusBasedFilter() {
DataStoreTransaction dtx = elide.getDataStore().beginTransaction();
transactionRegistry.addRunningTransaction(UUID.fromString("edc4a871-dff2-4054-804e-d80075cf828d"), dtx);
transactionRegistry.addRunningTransaction(UUID.fromString("edc4a871-dff2-4054-804e-d80075cf827d"), dtx);
transactionRegistry.addRunningTransaction(UUID.fromString("edc4a871-dff2-4054-804e-d80075cf826d"), dtx);
AsyncQuery asyncQuery1 = createAsyncQueryTestObject("edc4a871-dff2-4054-804e-d80075cf828d", 1577883600000L, QueryStatus.CANCEL_COMPLETE);
AsyncQuery asyncQuery2 = createAsyncQueryTestObject("edc4a871-dff2-4054-804e-d80075cf827d", 1577883600000L, QueryStatus.CANCELLED);
AsyncQuery asyncQuery3 = createAsyncQueryTestObject("edc4a871-dff2-4054-804e-d80075cf826d", 1577883600000L, QueryStatus.PROCESSING);
Collection<AsyncAPI> asyncCollection = new ArrayList<>();
asyncCollection.add(asyncQuery1);
asyncCollection.add(asyncQuery2);
asyncCollection.add(asyncQuery3);
when(cancelThread.getAsyncAPIDao().loadAsyncAPIByFilter(any(), any())).thenReturn(asyncCollection);
cancelThread.cancelAsyncAPI(AsyncQuery.class);
ArgumentCaptor<FilterExpression> fltStatusCaptor = ArgumentCaptor.forClass(FilterExpression.class);
verify(asyncAPIDao, times(1)).loadAsyncAPIByFilter(fltStatusCaptor.capture(), any());
assertEquals("asyncQuery.status IN [CANCELLED, PROCESSING, QUEUED]", fltStatusCaptor.getValue().toString());
verify(asyncAPIDao, times(1)).updateStatusAsyncAPIByFilter(any(), any(), any());
}
Aggregations