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

Make SQL executions async #3442

Merged
merged 3 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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 @@ -135,6 +135,8 @@ protected void setAdditionalFieldsForStatusWithColumnDescription(Subject subject

@Override
public void cancel() {
// TODO This will cause an error when SqlExecutionManager calls cancelQuery() for a form. I wonder if we can just cancel all subqueries and remove this
// hard cast? Also, the DistributedExecutionManager#cancelQuery() will send the CancelQuery message anyway.
log.debug("Sending cancel message to all workers.");
((DistributedNamespace) getNamespace()).getWorkerHandler().sendToAll(new CancelQuery(getId()));
}
jnsrnhld marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.bakdata.conquery.sql.conquery;


import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

import com.bakdata.conquery.io.storage.MetaStorage;
import com.bakdata.conquery.models.datasets.Dataset;
import com.bakdata.conquery.models.execution.ExecutionState;
import com.bakdata.conquery.models.execution.InternalExecution;
import com.bakdata.conquery.models.execution.ManagedExecution;
import com.bakdata.conquery.models.forms.managed.ManagedInternalForm;
import com.bakdata.conquery.models.identifiable.ids.specific.ManagedExecutionId;
import com.bakdata.conquery.models.query.ExecutionManager;
import com.bakdata.conquery.models.query.ManagedQuery;
import com.bakdata.conquery.models.worker.Namespace;
Expand All @@ -22,29 +27,27 @@ public class SqlExecutionManager extends ExecutionManager<SqlExecutionResult> {

private final SqlExecutionService executionService;
private final SqlConverter converter;
private final Map<ManagedExecutionId, CompletableFuture<Void>> runningExecutions;

public SqlExecutionManager(final SqlContext context, SqlExecutionService sqlExecutionService, MetaStorage storage) {
super(storage);
executionService = sqlExecutionService;
converter = new SqlConverter(context.getSqlDialect(), context.getConfig());
this.executionService = sqlExecutionService;
this.converter = new SqlConverter(context.getSqlDialect(), context.getConfig());
this.runningExecutions = new HashMap<>();
}

@Override
protected void doExecute(Namespace namespace, InternalExecution<?> execution) {

// todo(tm): Non-blocking execution
if (execution instanceof ManagedQuery managedQuery) {
SqlQuery sqlQuery = converter.convert(managedQuery.getQuery());
SqlExecutionResult result = executionService.execute(sqlQuery);
addResult(managedQuery, result);
managedQuery.setLastResultCount(((long) result.getRowCount()));
managedQuery.finish(ExecutionState.DONE);
CompletableFuture<Void> sqlQueryExecution = executeAsync(managedQuery);
runningExecutions.put(managedQuery.getId(), sqlQueryExecution);
return;
}

if (execution instanceof ManagedInternalForm<?> managedForm) {
managedForm.getSubQueries().values().forEach(subQuery -> doExecute(namespace, subQuery));
managedForm.finish(ExecutionState.DONE);
CompletableFuture.allOf(managedForm.getSubQueries().values().stream().map(this::executeAsync).toArray(CompletableFuture[]::new))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Die CompletableFuture's nutzen den ForkJoinPool.commonPool() per default. Passt das so oder sollen wir einen eigenen Executor übergeben bzw. das generell anders lösen?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ich würde erstmal beim default bleiben. Sollte der Probleme machen lässt sich das leicht ändern.

.thenRun(() -> managedForm.finish(ExecutionState.DONE));
return;
}

Expand All @@ -53,7 +56,30 @@ protected void doExecute(Namespace namespace, InternalExecution<?> execution) {

@Override
public void cancelQuery(Dataset dataset, ManagedExecution query) {
// unsupported for now

CompletableFuture<Void> sqlQueryExecution = runningExecutions.remove(query.getId());

// already finished/canceled
if (sqlQueryExecution == null) {
return;
}

if (!sqlQueryExecution.isCancelled()) {
sqlQueryExecution.cancel(true);
}

query.cancel();
}

private CompletableFuture<Void> executeAsync(ManagedQuery managedQuery) {
SqlQuery sqlQuery = converter.convert(managedQuery.getQuery());
return CompletableFuture.supplyAsync(() -> executionService.execute(sqlQuery))
.thenAccept(result -> {
addResult(managedQuery, result);
managedQuery.setLastResultCount(((long) result.getRowCount()));
managedQuery.finish(ExecutionState.DONE);
runningExecutions.remove(managedQuery.getId());
});
}

}
Loading