~comcloudway/builds.sr.ht

292dc73e28d585675f7980c3310a4ac8cde98354 — Conrad Hoffmann 1 year, 11 months ago eaf435b
api: pass job owner to StartJobGroupUnsafe()

This allows it to make use of the index on `owner_id` in the `job`
table. Without this, the query it makes is prohibitively expensive. A
sample query analysis went from > 1800 ms without the index to < 1ms
when using the index.
2 files changed, 7 insertions(+), 5 deletions(-)

M api/graph/resolver.go
M api/graph/schema.resolvers.go
M api/graph/resolver.go => api/graph/resolver.go +5 -3
@@ 66,7 66,7 @@ func FetchLogs(ctx context.Context, url string) (*model.Log, error) {
}

// Starts a job group. Does not authenticate the user.
func StartJobGroupUnsafe(ctx context.Context, tx *sql.Tx, id int) error {
func StartJobGroupUnsafe(ctx context.Context, tx *sql.Tx, id, ownerID int) error {
	var manifests []struct {
		ID       int
		Manifest *Manifest


@@ 74,9 74,11 @@ func StartJobGroupUnsafe(ctx context.Context, tx *sql.Tx, id int) error {

	rows, err := tx.QueryContext(ctx, `
		UPDATE job SET status = 'queued'
		WHERE job_group_id = $1
		WHERE
			job_group_id = $1 AND
			owner_id = $2
		RETURNING id, manifest;
	`, id)
	`, id, ownerID)
	if err != nil {
		return err
	}

M api/graph/schema.resolvers.go => api/graph/schema.resolvers.go +2 -2
@@ 540,7 540,7 @@ func (r *mutationResolver) CreateGroup(ctx context.Context, jobIds []int, trigge
			return nil
		}

		return StartJobGroupUnsafe(ctx, tx, group.ID)
		return StartJobGroupUnsafe(ctx, tx, group.ID, group.OwnerID)
	}); err != nil {
		return nil, err
	}


@@ 566,7 566,7 @@ func (r *mutationResolver) StartGroup(ctx context.Context, groupID int) (*model.
			return err
		}

		return StartJobGroupUnsafe(ctx, tx, groupID)
		return StartJobGroupUnsafe(ctx, tx, groupID, group.OwnerID)
	}); err != nil {
		return nil, err
	}