Skip to content

Commit

Permalink
creates a copy of the execution if it is not reused by someone else a…
Browse files Browse the repository at this point in the history
…s the owner
  • Loading branch information
thoniTUB committed Aug 24, 2021
1 parent 12ede6e commit 29861be
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
import static com.bakdata.conquery.models.auth.AuthorizationHelper.buildDatasetAbilityMap;

import java.net.URL;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -132,16 +128,12 @@ public ManagedExecution<?> postQuery(Dataset dataset, QueryDescription query, Us
*/
private ManagedExecution<?> tryReuse(QueryDescription query, ManagedExecutionId executionId, DatasetRegistry datasetRegistry, ConqueryConfig config, ExecutionManager executionManager, User user) {

final ManagedExecution<?> execution = datasetRegistry.getMetaRegistry().resolve(executionId);
ManagedExecution<?> execution = datasetRegistry.getMetaRegistry().resolve(executionId);

if (execution == null) {
return null;
}

// If the user is not the owner of the execution, we definitely create a new Execution, so the owner can cancel it
if (!user.equals(execution.getOwner())) {
return null;
}

// Direct reuse only works if the queries are of the same type (As reuse reconstructs the Query for different types)
if (!query.getClass().equals(execution.getSubmitted().getClass())) {
Expand All @@ -158,6 +150,15 @@ private ManagedExecution<?> tryReuse(QueryDescription query, ManagedExecutionId
}
}

// If the user is not the owner of the execution, we definitely create a new Execution, so the owner can cancel it
if (!user.equals(execution.getOwner())) {
final ManagedExecution<?> newExecution = executionManager.createExecution(datasetRegistry,execution.getSubmitted(),user,execution.getDataset());
newExecution.setLabel(execution.getLabel());
newExecution.setTags(execution.getTags().clone());
storage.updateExecution(newExecution);
execution = newExecution;
}

ExecutionState state = execution.getState();
if (state.equals(ExecutionState.RUNNING)) {
log.trace("The Execution[{}] was already started and its state is: {}", execution.getId(), state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
Expand All @@ -15,6 +16,9 @@
import com.bakdata.conquery.integration.common.LoadingUtil;
import com.bakdata.conquery.integration.json.JsonIntegrationTest;
import com.bakdata.conquery.integration.json.QueryTest;
import com.bakdata.conquery.io.storage.MetaStorage;
import com.bakdata.conquery.models.auth.entities.User;
import com.bakdata.conquery.models.auth.permissions.Ability;
import com.bakdata.conquery.models.common.Range;
import com.bakdata.conquery.models.datasets.concepts.Concept;
import com.bakdata.conquery.models.datasets.concepts.Connector;
Expand All @@ -23,6 +27,7 @@
import com.bakdata.conquery.models.exceptions.ValidatorHelper;
import com.bakdata.conquery.models.execution.ExecutionState;
import com.bakdata.conquery.apiv1.FullExecutionStatus;
import com.bakdata.conquery.models.execution.ManagedExecution;
import com.bakdata.conquery.models.identifiable.CentralRegistry;
import com.bakdata.conquery.models.identifiable.ids.specific.ConceptId;
import com.bakdata.conquery.models.identifiable.ids.specific.ConnectorId;
Expand All @@ -44,6 +49,7 @@
import com.bakdata.conquery.util.support.TestConquery;
import com.github.powerlibraries.io.In;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.argparse4j.impl.Arguments;


@Slf4j
Expand Down Expand Up @@ -85,7 +91,8 @@ public void execute(String name, TestConquery testConquery) throws Exception {

assertThat(id).isNotNull();

final ManagedQuery execution = (ManagedQuery) conquery.getMetaStorage().getExecution(id);
final MetaStorage metaStorage = conquery.getMetaStorage();
final ManagedQuery execution = (ManagedQuery) metaStorage.getExecution(id);

// Normal reuse
{
Expand Down Expand Up @@ -165,7 +172,7 @@ public void execute(String name, TestConquery testConquery) throws Exception {
reused1.setSecondaryId(query.getSecondaryId());

final ManagedExecutionId reused1Id = IntegrationUtils.assertQueryResult(conquery, reused1, 4L, ExecutionState.DONE, conquery.getTestUser(), 201);
final ManagedQuery execution1 = (ManagedQuery) conquery.getMetaStorage().getExecution(reused1Id);
final ManagedQuery execution1 = (ManagedQuery) metaStorage.getExecution(reused1Id);
{
final SecondaryIdQuery reused2 = new SecondaryIdQuery();
reused2.setRoot(new CQReusedQuery(execution1.getId()));
Expand All @@ -175,7 +182,7 @@ public void execute(String name, TestConquery testConquery) throws Exception {
final ManagedExecutionId
reused2Id =
IntegrationUtils.assertQueryResult(conquery, reused2, 4L, ExecutionState.DONE, conquery.getTestUser(), 201);
final ManagedQuery execution2 = (ManagedQuery) conquery.getMetaStorage().getExecution(reused2Id);
final ManagedQuery execution2 = (ManagedQuery) metaStorage.getExecution(reused2Id);

assertThat(reused2Id)
.as("Query should be reused.")
Expand Down Expand Up @@ -204,6 +211,34 @@ public void execute(String name, TestConquery testConquery) throws Exception {
.as("Query should NOT be reused.")
.isNotEqualTo(reused1Id);
}

{
// Reuse by another user (create a copy of the actual query)

final SecondaryIdQuery reused = new SecondaryIdQuery();
reused.setRoot(new CQReusedQuery(execution.getId()));

reused.setSecondaryId(query.getSecondaryId());

User shareHolder = new User("shareholder", "ShareHolder");
conquery.getMetaProcessor().addUser(shareHolder);

shareHolder.addPermissions(metaStorage, Set.of(
dataset.createPermission(Set.of(Ability.READ)),
execution.createPermission(Set.of(Ability.READ))
));

ManagedExecutionId copyId = IntegrationUtils.assertQueryResult(conquery, reused, 4L, ExecutionState.DONE, shareHolder, 201);

ManagedExecution<?> copy = metaStorage.getExecution(copyId);


// Contentwise the label and tags should be the same
assertThat(copy).usingRecursiveComparison().comparingOnlyFields("label","tags").isEqualTo(execution);

// However the Object holding the tags must be different, so the two are not linked here
assertThat(copy.getTags()).isNotSameAs(execution.getTags());
}
}
}
}

0 comments on commit 29861be

Please sign in to comment.