Skip to content

Commit

Permalink
Pass UserId of requester into Scheduling RQ
Browse files Browse the repository at this point in the history
- Additionally, do not change a missing `x-hasura-user-id` into an empty String
  • Loading branch information
Mythicaeda committed Sep 6, 2023
1 parent 74ac8ab commit f064ca2
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 18 deletions.
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 f064ca2

Please sign in to comment.