use of com.yahoo.elide.async.models.AsyncAPIResult in project elide by yahoo.
the class AsyncExecutorServiceTest method testExecuteQueryFail.
// Test for executor hook execution
@Test
public void testExecuteQueryFail() throws Exception {
AsyncQuery queryObj = mock(AsyncQuery.class);
when(queryObj.getAsyncAfterSeconds()).thenReturn(10);
Callable<AsyncAPIResult> mockCallable = mock(Callable.class);
when(mockCallable.call()).thenThrow(new NoHttpResponseException(""));
service.executeQuery(queryObj, mockCallable);
verify(queryObj, times(1)).setStatus(QueryStatus.PROCESSING);
verify(queryObj, times(1)).setStatus(QueryStatus.FAILURE);
}
use of com.yahoo.elide.async.models.AsyncAPIResult in project elide by yahoo.
the class TableExportOperation method call.
@Override
public AsyncAPIResult call() {
log.debug("TableExport Object from request: {}", exportObj);
Elide elide = service.getElide();
TableExportResult exportResult = new TableExportResult();
UUID requestId = UUID.fromString(exportObj.getRequestId());
try (DataStoreTransaction tx = elide.getDataStore().beginTransaction()) {
// Do Not Cache Export Results
Map<String, List<String>> requestHeaders = new HashMap<String, List<String>>();
requestHeaders.put("bypasscache", new ArrayList<String>(Arrays.asList("true")));
RequestScope requestScope = getRequestScope(exportObj, scope, tx, requestHeaders);
Collection<EntityProjection> projections = getProjections(exportObj, requestScope);
validateProjections(projections);
EntityProjection projection = projections.iterator().next();
Observable<PersistentResource> observableResults = Observable.empty();
elide.getTransactionRegistry().addRunningTransaction(requestId, tx);
// TODO - we need to add the baseUrlEndpoint to the queryObject.
// TODO - Can we have projectionInfo as null?
requestScope.setEntityProjection(projection);
if (projection != null) {
projection.setPagination(null);
observableResults = PersistentResource.loadRecords(projection, Collections.emptyList(), requestScope);
}
Observable<String> results = Observable.empty();
String preResult = formatter.preFormat(projection, exportObj);
results = observableResults.map(resource -> {
this.recordNumber++;
return formatter.format(resource, recordNumber);
});
String postResult = formatter.postFormat(projection, exportObj);
// Stitch together Pre-Formatted, Formatted, Post-Formatted results of Formatter in single observable.
Observable<String> interimResults = concatStringWithObservable(preResult, results, true);
Observable<String> finalResults = concatStringWithObservable(postResult, interimResults, false);
TableExportResult result = storeResults(exportObj, engine, finalResults);
if (result != null && result.getMessage() != null) {
throw new IllegalStateException(result.getMessage());
}
exportResult.setUrl(new URL(generateDownloadURL(exportObj, scope)));
exportResult.setRecordCount(recordNumber);
tx.flush(requestScope);
elide.getAuditLogger().commit();
tx.commit(requestScope);
} catch (BadRequestException e) {
exportResult.setMessage(e.getMessage());
} catch (MalformedURLException e) {
exportResult.setMessage("Download url generation failure.");
} catch (IOException e) {
log.error("IOException during TableExport", e);
exportResult.setMessage(e.getMessage());
} catch (Exception e) {
exportResult.setMessage(e.getMessage());
} finally {
// Follows same flow as GraphQL. The query may result in failure but request was successfully processed.
exportResult.setHttpStatus(200);
exportResult.setCompletedOn(new Date());
elide.getTransactionRegistry().removeRunningTransaction(requestId);
elide.getAuditLogger().clear();
}
return exportResult;
}
use of com.yahoo.elide.async.models.AsyncAPIResult in project elide by yahoo.
the class AsyncExecutorService method executeQuery.
/**
* Execute Query asynchronously.
* @param queryObj Query Object
* @param callable A Callabale implementation to execute in background.
*/
public void executeQuery(AsyncAPI queryObj, Callable<AsyncAPIResult> callable) {
AsyncAPIResultFuture resultFuture = new AsyncAPIResultFuture();
try {
Future<AsyncAPIResult> asyncExecuteFuture = executor.submit(callable);
resultFuture.setAsyncFuture(asyncExecuteFuture);
queryObj.setStatus(QueryStatus.PROCESSING);
AsyncAPIResult queryResultObj = asyncExecuteFuture.get(queryObj.getAsyncAfterSeconds(), TimeUnit.SECONDS);
queryObj.setResult(queryResultObj);
queryObj.setStatus(QueryStatus.COMPLETE);
queryObj.setUpdatedOn(new Date());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
log.error("InterruptedException: {}", e.toString());
queryObj.setStatus(QueryStatus.FAILURE);
} catch (ExecutionException e) {
log.error("ExecutionException: {}", e.toString());
queryObj.setStatus(QueryStatus.FAILURE);
} catch (TimeoutException e) {
log.error("TimeoutException: {}", e.toString());
resultFuture.setSynchronousTimeout(true);
} catch (Exception e) {
log.error("Exception: {}", e.toString());
queryObj.setStatus(QueryStatus.FAILURE);
} finally {
asyncResultFutureThreadLocal.set(resultFuture);
}
}
use of com.yahoo.elide.async.models.AsyncAPIResult in project elide by yahoo.
the class TableExportHook method getOperation.
@Override
public Callable<AsyncAPIResult> getOperation(AsyncAPI export, RequestScope requestScope) {
Callable<AsyncAPIResult> operation = null;
TableExport exportObj = (TableExport) export;
ResultType resultType = exportObj.getResultType();
QueryType queryType = exportObj.getQueryType();
com.yahoo.elide.core.RequestScope scope = (com.yahoo.elide.core.RequestScope) requestScope;
TableExportFormatter formatter = supportedFormatters.get(resultType);
if (formatter == null) {
throw new InvalidOperationException("Formatter unavailable for " + resultType);
}
if (queryType.equals(QueryType.GRAPHQL_V1_0)) {
operation = new GraphQLTableExportOperation(formatter, getAsyncExecutorService(), export, scope, engine);
} else if (queryType.equals(QueryType.JSONAPI_V1_0)) {
operation = new JSONAPITableExportOperation(formatter, getAsyncExecutorService(), export, scope, engine);
} else {
throw new InvalidOperationException(queryType + "is not supported");
}
return operation;
}
use of com.yahoo.elide.async.models.AsyncAPIResult in project elide by yahoo.
the class AsyncAPIUpdateOperation method run.
/**
* This is the main method which updates the Async API request.
*/
@Override
public void run() {
try {
AsyncAPIResult queryResultObj = task.get();
// add queryResult object to query object
asyncAPIDao.updateAsyncAPIResult(queryResultObj, queryObj.getId(), queryObj.getClass());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
log.error("InterruptedException: {}", e.toString());
asyncAPIDao.updateStatus(queryObj.getId(), QueryStatus.FAILURE, queryObj.getClass());
} catch (Exception e) {
log.error("Exception: {}", e.toString());
asyncAPIDao.updateStatus(queryObj.getId(), QueryStatus.FAILURE, queryObj.getClass());
}
}
Aggregations