Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async Redesign for simpler background thread Model #2408

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@
import static com.yahoo.elide.annotation.LifeCycleHookBinding.TransactionPhase.PRESECURITY;
import com.yahoo.elide.annotation.LifeCycleHookBinding;
import com.yahoo.elide.async.models.AsyncAPI;
import com.yahoo.elide.async.models.AsyncAPIJob;
import com.yahoo.elide.async.models.AsyncAPIResult;
import com.yahoo.elide.async.models.AsyncQuery;
import com.yahoo.elide.async.models.QueryStatus;
import com.yahoo.elide.async.models.QueryType;
import com.yahoo.elide.async.models.TableExport;
import com.yahoo.elide.async.service.AsyncExecutorService;
import com.yahoo.elide.async.service.thread.AsyncAPIBackgroundRunnable;
import com.yahoo.elide.core.exceptions.InvalidOperationException;
import com.yahoo.elide.core.exceptions.InvalidValueException;
import com.yahoo.elide.core.lifecycle.LifeCycleHook;
Expand All @@ -22,6 +27,7 @@

import java.security.Principal;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

/**
* AsyncAPI Base Hook methods.
Expand Down Expand Up @@ -61,11 +67,46 @@ protected void executeHook(LifeCycleHookBinding.Operation operation, LifeCycleHo
if (operation.equals(CREATE)) {
if (phase.equals(PREFLUSH)) {
validateOptions(query, requestScope);
executeAsync(query, queryWorker);

// Graphql TableExport will not take this flow.
if (query.getClass().equals(AsyncQuery.class) || (query.getClass().equals(TableExport.class)
&& query.getQueryType().equals(QueryType.JSONAPI_V1_0))) {
executeAsync(query, queryWorker);
}
return;
}
if (phase.equals(POSTCOMMIT)) {
completeAsync(query, requestScope);
if (query.getClass().equals(AsyncQuery.class) || (query.getClass().equals(TableExport.class)
&& query.getQueryType().equals(QueryType.JSONAPI_V1_0))) {
completeAsync(query, requestScope);
return;
}

// Graphql TableExport takes this flow.
AsyncAPIJob job = null;
try {
job = new AsyncAPIJob((AsyncAPI) query.clone(), requestScope.getUser());
} catch (CloneNotSupportedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
AsyncAPIBackgroundRunnable task = new AsyncAPIBackgroundRunnable(asyncExecutorService.getElide(),
asyncExecutorService.getAsyncAPIDao(), job, (com.yahoo.elide.core.RequestScope) requestScope);
asyncExecutorService.getExecutor().submit(task);

try {
job.getDone().await(query.getAsyncAfterSeconds(), TimeUnit.SECONDS);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(1);
}
if (job.getAsyncApi().getStatus() == QueryStatus.COMPLETE
|| job.getAsyncApi().getStatus() == QueryStatus.FAILURE) {
query.setStatus(job.getAsyncApi().getStatus());
query.setResult(job.getAsyncApi().getResult());
}
return;
}
if (phase.equals(PRESECURITY)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*/
@MappedSuperclass
@Data
public abstract class AsyncAPI implements PrincipalOwned {
public abstract class AsyncAPI implements PrincipalOwned, Cloneable {
@Id
@Column(columnDefinition = "varchar(36)")
@Pattern(regexp = "^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$",
Expand Down Expand Up @@ -88,4 +88,8 @@ public int hashCode() {
public boolean equals(Object obj) {
return obj instanceof AsyncAPI && this.getClass() == obj.getClass() && id.equals(((AsyncAPI) obj).id);
}

public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2021, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.async.models;

import com.yahoo.elide.core.security.User;

import lombok.Data;

import java.util.concurrent.CountDownLatch;

/**
* Model Class for AsyncAPI Job.
*/
@Data
public class AsyncAPIJob {
User user;
AsyncAPI asyncApi;
CountDownLatch done = new CountDownLatch(1);

public AsyncAPIJob(AsyncAPI asyncAPI, User user) {
this.user = user;
this.asyncApi = asyncAPI;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,16 @@ public class TableExport extends AsyncAPI {
public void setResult(AsyncAPIResult result) {
this.result = (TableExportResult) result;
}

@Override
public Object clone() throws CloneNotSupportedException {
TableExport cloneObj = new TableExport();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to copy result too?

cloneObj.setId(this.getId());
cloneObj.setStatus(this.getStatus());
cloneObj.setResultType(this.getResultType());
cloneObj.setQuery(this.getQuery());
cloneObj.setRequestId(this.getRequestId());
cloneObj.setPrincipalName(this.getPrincipalName());
return cloneObj;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
/*
* Copyright 2021, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package com.yahoo.elide.async.service.thread;


import com.yahoo.elide.Elide;
import com.yahoo.elide.async.export.formatter.CSVExportFormatter;
import com.yahoo.elide.async.export.formatter.JSONExportFormatter;
import com.yahoo.elide.async.export.formatter.TableExportFormatter;
import com.yahoo.elide.async.export.validator.SingleRootProjectionValidator;
import com.yahoo.elide.async.export.validator.Validator;
import com.yahoo.elide.async.models.AsyncAPI;
import com.yahoo.elide.async.models.AsyncAPIJob;
import com.yahoo.elide.async.models.AsyncQuery;
import com.yahoo.elide.async.models.QueryStatus;
import com.yahoo.elide.async.models.ResultType;
import com.yahoo.elide.async.models.TableExport;
import com.yahoo.elide.async.models.TableExportResult;
import com.yahoo.elide.async.service.dao.AsyncAPIDAO;
import com.yahoo.elide.async.service.storageengine.FileResultStorageEngine;
import com.yahoo.elide.async.service.storageengine.ResultStorageEngine;
import com.yahoo.elide.core.PersistentResource;
import com.yahoo.elide.core.RequestScope;
import com.yahoo.elide.core.datastore.DataStoreTransaction;
import com.yahoo.elide.core.exceptions.BadRequestException;
import com.yahoo.elide.core.exceptions.TransactionException;
import com.yahoo.elide.core.request.EntityProjection;
import com.yahoo.elide.core.security.User;
import com.yahoo.elide.graphql.GraphQLRequestScope;
import com.yahoo.elide.graphql.QueryRunner;
import com.yahoo.elide.graphql.parser.GraphQLEntityProjectionMaker;
import com.yahoo.elide.graphql.parser.GraphQLProjectionInfo;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import io.reactivex.Observable;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
* Runnable for running the async queries in background.
*/
@Slf4j
@Data
public class AsyncAPIBackgroundRunnable implements Runnable {

private Elide elide;
private AsyncAPIDAO asyncAPIDao;
private AsyncAPIJob job;
private RequestScope scope;
private Integer recordNumber = 0;

public AsyncAPIBackgroundRunnable(Elide elide, AsyncAPIDAO asyncAPIDao,
AsyncAPIJob job, RequestScope scope) {
this.elide = elide;
this.asyncAPIDao = asyncAPIDao;
this.job = job;
this.scope = scope;
}

@Override
public void run() {
executeAsyncAPI(AsyncQuery.class);
}

/**
* This method deletes the historical queries based on threshold.
* @param type AsyncAPI Type Implementation.
*/
protected <T extends AsyncAPI> void executeAsyncAPI(Class<T> type) {
TableExport exportObj = (TableExport) job.getAsyncApi();
Map<ResultType, TableExportFormatter> supportedFormatters = new HashMap<>();
supportedFormatters.put(ResultType.CSV, new CSVExportFormatter(elide, false));
supportedFormatters.put(ResultType.JSON, new JSONExportFormatter(elide));
TableExportFormatter formatter = supportedFormatters.get(exportObj.getResultType());

log.debug("TableExport Object from request: {}", exportObj);
TableExportResult exportResult = new TableExportResult();
ResultStorageEngine engine = new FileResultStorageEngine("/tmp");
try (DataStoreTransaction tx = elide.getDataStore().beginTransaction()) {
// Change Status to Processing
asyncAPIDao.updateStatus(exportObj.getId(), QueryStatus.PROCESSING, TableExport.class);
// 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 = export(exportObj, requestScope, projection);

Observable<String> results = Observable.empty();
String preResult = formatter.preFormat(projection, exportObj);
results = observableResults.map(resource -> {
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);

storeResults(exportObj, engine, finalResults);

exportResult.setUrl(new URL(generateDownloadURL(exportObj, scope)));
exportResult.setRecordCount(recordNumber);
asyncAPIDao.updateStatus(exportObj.getId(), QueryStatus.COMPLETE, TableExport.class);
exportObj.setStatus(QueryStatus.COMPLETE);
} catch (BadRequestException e) {
exportResult.setMessage(e.getMessage());
asyncAPIDao.updateStatus(exportObj.getId(), QueryStatus.FAILURE, TableExport.class);
exportObj.setStatus(QueryStatus.FAILURE);
} catch (MalformedURLException e) {
exportResult.setMessage("Download url generation failure.");
asyncAPIDao.updateStatus(exportObj.getId(), QueryStatus.FAILURE, TableExport.class);
exportObj.setStatus(QueryStatus.FAILURE);
} catch (Exception e) {
exportResult.setMessage(e.getMessage());
asyncAPIDao.updateStatus(exportObj.getId(), QueryStatus.FAILURE, TableExport.class);
exportObj.setStatus(QueryStatus.FAILURE);
} finally {
// Follows same flow as GraphQL. The query may result in failure but request was successfully processed.
exportResult.setHttpStatus(200);
exportResult.setCompletedOn(new Date());
exportObj.setResult(exportResult);
asyncAPIDao.updateAsyncAPIResult(exportResult, exportObj.getId(), TableExport.class);
job.setAsyncApi(exportObj);

//Notify listeners
job.getDone().countDown();
}
}

public String generateDownloadURL(TableExport exportObj, RequestScope scope) {
String downloadPath = scope.getElideSettings().getExportApiPath();
String baseURL = scope.getBaseUrlEndPoint();
return baseURL + downloadPath + "/" + exportObj.getId();
}

protected TableExport storeResults(TableExport exportObj, ResultStorageEngine resultStorageEngine,
Observable<String> result) {
return resultStorageEngine.storeResults(exportObj, result);
}

private Observable<String> concatStringWithObservable(String toConcat, Observable<String> observable,
boolean stringFirst) {
if (toConcat == null) {
return observable;
}

return stringFirst ? Observable.just(toConcat).concatWith(observable)
: observable.concatWith(Observable.just(toConcat));
}

private void validateProjections(Collection<EntityProjection> projections) {
List<Validator> validators = new ArrayList<>(Arrays.asList(new SingleRootProjectionValidator()));
validators.forEach(validator -> validator.validateProjection(projections));
}

public RequestScope getRequestScope(TableExport export, RequestScope scope, DataStoreTransaction tx,
Map<String, List<String>> additionalRequestHeaders) {
UUID requestId = UUID.fromString(export.getRequestId());
User user = scope.getUser();
String apiVersion = scope.getApiVersion();
return new GraphQLRequestScope("", tx, user, apiVersion, elide.getElideSettings(),
null, requestId, additionalRequestHeaders);
}

public Collection<EntityProjection> getProjections(TableExport export, RequestScope scope) {
GraphQLProjectionInfo projectionInfo;
try {
String graphQLDocument = export.getQuery();
ObjectMapper mapper = elide.getMapper().getObjectMapper();

JsonNode node = QueryRunner.getTopLevelNode(mapper, graphQLDocument);
Map<String, Object> variables = QueryRunner.extractVariables(mapper, node);
String queryString = QueryRunner.extractQuery(node);

projectionInfo = new GraphQLEntityProjectionMaker(elide.getElideSettings(), variables,
scope.getApiVersion()).make(queryString);

} catch (IOException e) {
throw new IllegalStateException(e);
}

return projectionInfo.getProjections().values();
}

private Observable<PersistentResource> export(TableExport exportObj, RequestScope scope,
EntityProjection projection) {
Observable<PersistentResource> results = Observable.empty();

UUID requestId = UUID.fromString(exportObj.getRequestId());

try {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be cleaned up since we are restricting async to reads. We don't need to call precommit, or run any lifecycle hooks for example.

DataStoreTransaction tx = scope.getTransaction();
elide.getTransactionRegistry().addRunningTransaction(requestId, tx);

//TODO - we need to add the baseUrlEndpoint to the queryObject.
//TODO - Can we have projectionInfo as null?
scope.setEntityProjection(projection);

if (projection != null) {
projection.setPagination(null);
results = PersistentResource.loadRecords(projection, Collections.emptyList(), scope);
}

tx.preCommit(scope);
scope.runQueuedPreSecurityTriggers();
scope.getPermissionExecutor().executeCommitChecks();

tx.flush(scope);

scope.runQueuedPreCommitTriggers();

elide.getAuditLogger().commit();
tx.commit(scope);

scope.runQueuedPostCommitTriggers();
} catch (IOException e) {
log.error("IOException during TableExport", e);
throw new TransactionException(e);
} finally {
elide.getTransactionRegistry().removeRunningTransaction(requestId);
elide.getAuditLogger().clear();
}

return results;
}
}