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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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,45 @@ 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;
}
// 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(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;
}
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;
}
}