Skip to content

Commit

Permalink
Merge pull request #1123 from NASA-AMMOS/fix/simulation-requested-by
Browse files Browse the repository at this point in the history
Pass user-id into Simulation/Scheduling Requests
  • Loading branch information
Mythicaeda authored Sep 6, 2023
2 parents ad48a38 + f064ca2 commit ad445b0
Show file tree
Hide file tree
Showing 20 changed files with 46 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private HasuraParsers() {}
.field("x-hasura-role", stringP)
.optionalField("x-hasura-user-id", stringP)
.map(
untuple((role, userId) -> new HasuraAction.Session(role, userId.orElse(""))),
untuple((role, userId) -> new HasuraAction.Session(role, userId.orElse(null))),
$ -> tuple($.hasuraRole(), Optional.ofNullable($.hasuraUserId())));

private static <I extends HasuraAction.Input> JsonParser<HasuraAction<I>> hasuraActionF(final JsonParser<I> inputP) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ private void getSimulationResults(final Context ctx) {

this.checkPermissions(Action.simulate, body.session(), planId);

final var response = this.simulationAction.run(planId);
final var response = this.simulationAction.run(planId, body.session());
ctx.result(ResponseSerializers.serializeSimulationResultsResponse(response).toString());
} catch (final InvalidEntityException ex) {
ctx.status(400).result(ResponseSerializers.serializeInvalidEntityException(ex).toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public InMemoryResultsCellRepository(final PlanRepository planRepository) {
}

@Override
public ResultsProtocol.OwnerRole allocate(final PlanId planId) {
public ResultsProtocol.OwnerRole allocate(final PlanId planId, final String requestedBy) {
try {
final var planRevision = planRepository.getPlanRevision(planId);
final var cell = new InMemoryCell(planId, planRevision);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Optional;

public interface ResultsCellRepository {
ResultsProtocol.OwnerRole allocate(PlanId planId);
ResultsProtocol.OwnerRole allocate(PlanId planId, String requestedBy);

Optional<ResultsProtocol.OwnerRole> claim(PlanId planId, Long datasetId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
simulation_id,
simulation_start_time,
simulation_end_time,
arguments
arguments,
requested_by
)
values(?, ?::timestamptz, ?::timestamptz, ?::jsonb)
values(?, ?::timestamptz, ?::timestamptz, ?::jsonb, ?)
returning
dataset_id,
status,
Expand All @@ -40,12 +41,14 @@ public SimulationDatasetRecord apply(
final long simulationId,
final Timestamp simulationStart,
final Timestamp simulationEnd,
final Map<String, SerializedValue> arguments
final Map<String, SerializedValue> arguments,
final String requestedBy
) throws SQLException {
this.statement.setLong(1, simulationId);
PreparedStatements.setTimestamp(this.statement, 2, simulationStart);
PreparedStatements.setTimestamp(this.statement, 3, simulationEnd);
this.statement.setString(4, simulationArgumentsP.unparse(arguments).toString());
this.statement.setString(5, requestedBy);

try (final var results = this.statement.executeQuery()) {
if (!results.next()) throw new FailedInsertException("simulation_dataset");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public PostgresResultsCellRepository(final DataSource dataSource) {
}

@Override
public ResultsProtocol.OwnerRole allocate(final PlanId planId) {
public ResultsProtocol.OwnerRole allocate(final PlanId planId, final String requestedBy) {
try (final var connection = this.dataSource.getConnection()) {
final SimulationRecord simulation = getSimulation(connection, planId);
final SimulationTemplateRecord template;
Expand Down Expand Up @@ -73,7 +73,8 @@ public ResultsProtocol.OwnerRole allocate(final PlanId planId) {
simulation,
startTime,
endTime,
arguments);
arguments,
requestedBy);

return new PostgresResultsCell(
this.dataSource,
Expand Down Expand Up @@ -169,15 +170,17 @@ private static SimulationDatasetRecord createSimulationDataset(
final SimulationRecord simulation,
final Timestamp simulationStart,
final Timestamp simulationEnd,
final Map<String, SerializedValue> arguments
final Map<String, SerializedValue> arguments,
final String requestedBy
) throws SQLException
{
try (final var createSimulationDatasetAction = new CreateSimulationDatasetAction(connection)) {
return createSimulationDatasetAction.apply(
simulation.id(),
simulationStart,
simulationEnd,
arguments);
arguments,
requestedBy);
}
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ public record CachedSimulationService (
) implements SimulationService {

@Override
public ResultsProtocol.State getSimulationResults(final PlanId planId, final RevisionData revisionData) {
public ResultsProtocol.State getSimulationResults(final PlanId planId, final RevisionData revisionData, final String requestedBy) {
final var cell$ = this.store.lookup(planId);
if (cell$.isPresent()) {
return cell$.get().get();
} else {
// Allocate a fresh cell.
final var cell = this.store.allocate(planId);
final var cell = this.store.allocate(planId, requestedBy);

// Return the current value of the reader; if it's incomplete, the caller can check it again later.
return cell.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import gov.nasa.jpl.aerie.merlin.protocol.types.SerializedValue;
import gov.nasa.jpl.aerie.merlin.server.ResultsProtocol;
import gov.nasa.jpl.aerie.merlin.server.exceptions.NoSuchPlanException;
import gov.nasa.jpl.aerie.merlin.server.models.HasuraAction;
import gov.nasa.jpl.aerie.merlin.server.models.PlanId;
import org.apache.commons.lang3.tuple.Pair;

Expand Down Expand Up @@ -34,10 +35,11 @@ public GetSimulationResultsAction(
this.simulationService = Objects.requireNonNull(simulationService);
}

public Response run(final PlanId planId) throws NoSuchPlanException, MissionModelService.NoSuchMissionModelException {
public Response run(final PlanId planId, final HasuraAction.Session session)
throws NoSuchPlanException, MissionModelService.NoSuchMissionModelException {
final var revisionData = this.planService.getPlanRevisionData(planId);

final var response = this.simulationService.getSimulationResults(planId, revisionData);
final var response = this.simulationService.getSimulationResults(planId, revisionData, session.hasuraUserId());

if (response instanceof ResultsProtocol.State.Pending r) {
return new Response.Pending(r.simulationDatasetId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
import java.util.Optional;

public interface SimulationService {
ResultsProtocol.State getSimulationResults(PlanId planId, RevisionData revisionData);
ResultsProtocol.State getSimulationResults(PlanId planId, RevisionData revisionData, final String requestedBy);
Optional<SimulationResultsHandle> get(PlanId planId, RevisionData revisionData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public record UncachedSimulationService (
) implements SimulationService {

@Override
public ResultsProtocol.State getSimulationResults(final PlanId planId, final RevisionData revisionData) {
public ResultsProtocol.State getSimulationResults(final PlanId planId, final RevisionData revisionData, final String requestedBy) {
if (!(revisionData instanceof InMemoryRevisionData inMemoryRevisionData)) {
throw new Error("UncachedSimulationService only accepts InMemoryRevisionData");
}
Expand All @@ -39,7 +39,7 @@ public ResultsProtocol.State getSimulationResults(final PlanId planId, final Rev
@Override
public Optional<SimulationResultsHandle> get(final PlanId planId, final RevisionData revisionData) {
return Optional.ofNullable(
getSimulationResults(planId, revisionData) instanceof ResultsProtocol.State.Success s ?
getSimulationResults(planId, revisionData, null) instanceof ResultsProtocol.State.Success s ?
s.results() :
null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void testHasuraActionParsers() {
final var expected = new HasuraAction<>(
"testAction",
new HasuraAction.MissionModelInput("1"),
new HasuraAction.Session("aerie_admin", ""));
new HasuraAction.Session("aerie_admin", null));

assertThat(hasuraMissionModelActionP.parse(json).getSuccessOrThrow()).isEqualTo(expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private void schedule(final Context ctx) {
ctx.status(500).result(ExceptionSerializers.serializeIOException(ex).toString());
}

final var response = this.scheduleAction.run(specificationId);
final var response = this.scheduleAction.run(specificationId, session);
ctx.result(serializeScheduleResultsResponse(response).toString());
} catch (final IOException e) {
log.error("low level input/output problem during scheduling", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private SchedulerParsers() {}
.field("x-hasura-role", stringP)
.optionalField("x-hasura-user-id", stringP)
.map(
untuple((role, userId) -> new HasuraAction.Session(role, userId.orElse(""))),
untuple((role, userId) -> new HasuraAction.Session(role, userId.orElse(null))),
session -> tuple(session.hasuraRole(), Optional.ofNullable(session.hasuraUserId())));

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public record InMemoryResultsCellRepository(SpecificationRepository specificationRepository) implements ResultsCellRepository {
@Override
public ResultsProtocol.OwnerRole allocate(final SpecificationId specificationId)
public ResultsProtocol.OwnerRole allocate(final SpecificationId specificationId, final String requestedBy)
{
throw new UnsupportedOperationException(); // TODO stubbed method must be implemented
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import gov.nasa.jpl.aerie.scheduler.server.models.SpecificationId;

public interface ResultsCellRepository {
ResultsProtocol.OwnerRole allocate(SpecificationId specificationId);
ResultsProtocol.OwnerRole allocate(SpecificationId specificationId, final String requestedBy);

Optional<ResultsProtocol.OwnerRole> claim(SpecificationId specificationId);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package gov.nasa.jpl.aerie.scheduler.server.remotes.postgres;

import gov.nasa.jpl.aerie.scheduler.server.services.ScheduleFailure;
import org.intellij.lang.annotations.Language;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Optional;

import static gov.nasa.jpl.aerie.scheduler.server.remotes.postgres.PreparedStatements.getDatasetId;

/*package-local*/ final class CreateRequestAction implements AutoCloseable {
private final @Language("SQL") String sql = """
insert into scheduling_request (specification_id, specification_revision)
values (?, ?)
insert into scheduling_request (specification_id, specification_revision, requested_by)
values (?, ?, ?)
returning
analysis_id,
status,
Expand All @@ -28,9 +26,10 @@ public CreateRequestAction(final Connection connection) throws SQLException {
this.statement = connection.prepareStatement(sql);
}

public RequestRecord apply(final SpecificationRecord specification) throws SQLException {
public RequestRecord apply(final SpecificationRecord specification, final String requestedBy) throws SQLException {
this.statement.setLong(1, specification.id());
this.statement.setLong(2, specification.revision());
this.statement.setString(3, requestedBy);

final var result = this.statement.executeQuery();
if (!result.next()) throw new FailedInsertException("scheduling_request");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ public PostgresResultsCellRepository(final DataSource dataSource) {
}

@Override
public ResultsProtocol.OwnerRole allocate(final SpecificationId specificationId)
public ResultsProtocol.OwnerRole allocate(final SpecificationId specificationId, final String requestedBy)
{
try (final var connection = this.dataSource.getConnection()) {
final var spec = getSpecification(connection, specificationId);
final var request = createRequest(connection, spec);
final var request = createRequest(connection, spec, requestedBy);

return new PostgresResultsCell(
this.dataSource,
Expand Down Expand Up @@ -134,10 +134,11 @@ private static Optional<RequestRecord> getRequest(

private static RequestRecord createRequest(
final Connection connection,
final SpecificationRecord specification
final SpecificationRecord specification,
final String requestedBy
) throws SQLException {
try (final var createRequestAction = new CreateRequestAction(connection)) {
return createRequestAction.apply(specification);
return createRequestAction.apply(specification, requestedBy);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ public record CachedSchedulerService(
) implements SchedulerService {

@Override
public ResultsProtocol.State getScheduleResults(final ScheduleRequest request) {
public ResultsProtocol.State getScheduleResults(final ScheduleRequest request, final String requestedBy) {
final var specificationId = request.specificationId();
final var cell$ = this.store.lookup(specificationId);
if (cell$.isPresent()) {
return cell$.get().get();
} else {
// Allocate a fresh cell.
final var cell = this.store.allocate(specificationId);
final var cell = this.store.allocate(specificationId, requestedBy);

// Return the current value of the reader; if it's incomplete, the caller can check it again later.
return cell.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import gov.nasa.jpl.aerie.scheduler.server.ResultsProtocol;
import gov.nasa.jpl.aerie.scheduler.server.exceptions.NoSuchSpecificationException;
import gov.nasa.jpl.aerie.scheduler.server.models.HasuraAction;
import gov.nasa.jpl.aerie.scheduler.server.models.SpecificationId;

/**
Expand Down Expand Up @@ -51,15 +52,15 @@ record Complete(ScheduleResults results, long analysisId, Optional<Long> dataset
* @return a response object wrapping summary results of the run (either successful or not)
* @throws NoSuchSpecificationException if the target specification could not be found
*/
public Response run(final SpecificationId specificationId)
public Response run(final SpecificationId specificationId, final HasuraAction.Session session)
throws NoSuchSpecificationException, IOException
{
//record the plan revision as of the scheduling request time (in case work commences much later eg in worker thread)
//TODO may also need to verify the model revision / other volatile metadata matches one from request
final var specificationRev = this.specificationService.getSpecificationRevisionData(specificationId);

//submit request to run scheduler (possibly asynchronously or even cached depending on service)
final var response = this.schedulerService.getScheduleResults(new ScheduleRequest(specificationId, specificationRev));
final var response = this.schedulerService.getScheduleResults(new ScheduleRequest(specificationId, specificationRev), session.hasuraUserId());

return repackResponse(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ public interface SchedulerService {
* @param request details of the scheduling request, including the target plan and goals to operate on
* @return summary of the scheduling run, including goal satisfaction metrics and changes made
*/
ResultsProtocol.State getScheduleResults(final ScheduleRequest request);
ResultsProtocol.State getScheduleResults(final ScheduleRequest request, final String requestedBy);
}

0 comments on commit ad445b0

Please sign in to comment.