Search in sources :

Example 1 with AsyncAPIResult

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);
}
Also used : NoHttpResponseException(org.apache.http.NoHttpResponseException) AsyncQuery(com.yahoo.elide.async.models.AsyncQuery) AsyncAPIResult(com.yahoo.elide.async.models.AsyncAPIResult) Test(org.junit.jupiter.api.Test)

Example 2 with AsyncAPIResult

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;
}
Also used : Arrays(java.util.Arrays) Getter(lombok.Getter) ResultStorageEngine(com.yahoo.elide.async.service.storageengine.ResultStorageEngine) AsyncAPIResult(com.yahoo.elide.async.models.AsyncAPIResult) URL(java.net.URL) Date(java.util.Date) SingleRootProjectionValidator(com.yahoo.elide.async.export.validator.SingleRootProjectionValidator) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) ArrayList(java.util.ArrayList) Validator(com.yahoo.elide.async.export.validator.Validator) Map(java.util.Map) PersistentResource(com.yahoo.elide.core.PersistentResource) Observable(io.reactivex.Observable) AsyncAPI(com.yahoo.elide.async.models.AsyncAPI) TableExport(com.yahoo.elide.async.models.TableExport) RequestScope(com.yahoo.elide.core.RequestScope) Elide(com.yahoo.elide.Elide) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) FileExtensionType(com.yahoo.elide.async.models.FileExtensionType) MalformedURLException(java.net.MalformedURLException) Collection(java.util.Collection) EntityProjection(com.yahoo.elide.core.request.EntityProjection) IOException(java.io.IOException) TableExportFormatter(com.yahoo.elide.async.export.formatter.TableExportFormatter) UUID(java.util.UUID) TableExportResult(com.yahoo.elide.async.models.TableExportResult) AsyncExecutorService(com.yahoo.elide.async.service.AsyncExecutorService) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Collections(java.util.Collections) EntityProjection(com.yahoo.elide.core.request.EntityProjection) PersistentResource(com.yahoo.elide.core.PersistentResource) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) IOException(java.io.IOException) RequestScope(com.yahoo.elide.core.RequestScope) TableExportResult(com.yahoo.elide.async.models.TableExportResult) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Date(java.util.Date) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) ArrayList(java.util.ArrayList) List(java.util.List) Elide(com.yahoo.elide.Elide) UUID(java.util.UUID)

Example 3 with AsyncAPIResult

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);
    }
}
Also used : ExecutionException(java.util.concurrent.ExecutionException) AsyncAPIResult(com.yahoo.elide.async.models.AsyncAPIResult) Date(java.util.Date) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with AsyncAPIResult

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;
}
Also used : JSONAPITableExportOperation(com.yahoo.elide.async.operation.JSONAPITableExportOperation) ResultType(com.yahoo.elide.async.models.ResultType) RequestScope(com.yahoo.elide.core.security.RequestScope) TableExport(com.yahoo.elide.async.models.TableExport) InvalidOperationException(com.yahoo.elide.core.exceptions.InvalidOperationException) GraphQLTableExportOperation(com.yahoo.elide.async.operation.GraphQLTableExportOperation) AsyncAPIResult(com.yahoo.elide.async.models.AsyncAPIResult) QueryType(com.yahoo.elide.async.models.QueryType) TableExportFormatter(com.yahoo.elide.async.export.formatter.TableExportFormatter)

Example 5 with AsyncAPIResult

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());
    }
}
Also used : AsyncAPIResult(com.yahoo.elide.async.models.AsyncAPIResult)

Aggregations

AsyncAPIResult (com.yahoo.elide.async.models.AsyncAPIResult)5 TableExportFormatter (com.yahoo.elide.async.export.formatter.TableExportFormatter)2 TableExport (com.yahoo.elide.async.models.TableExport)2 Date (java.util.Date)2 Elide (com.yahoo.elide.Elide)1 SingleRootProjectionValidator (com.yahoo.elide.async.export.validator.SingleRootProjectionValidator)1 Validator (com.yahoo.elide.async.export.validator.Validator)1 AsyncAPI (com.yahoo.elide.async.models.AsyncAPI)1 AsyncQuery (com.yahoo.elide.async.models.AsyncQuery)1 FileExtensionType (com.yahoo.elide.async.models.FileExtensionType)1 QueryType (com.yahoo.elide.async.models.QueryType)1 ResultType (com.yahoo.elide.async.models.ResultType)1 TableExportResult (com.yahoo.elide.async.models.TableExportResult)1 GraphQLTableExportOperation (com.yahoo.elide.async.operation.GraphQLTableExportOperation)1 JSONAPITableExportOperation (com.yahoo.elide.async.operation.JSONAPITableExportOperation)1 AsyncExecutorService (com.yahoo.elide.async.service.AsyncExecutorService)1 ResultStorageEngine (com.yahoo.elide.async.service.storageengine.ResultStorageEngine)1 PersistentResource (com.yahoo.elide.core.PersistentResource)1 RequestScope (com.yahoo.elide.core.RequestScope)1 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)1