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

Enable Mongo transactions for updating model cards #1237

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
21 changes: 18 additions & 3 deletions backend/src/services/model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Validator } from 'jsonschema'
import _ from 'lodash'
import mongoose from 'mongoose'

import authentication from '../connectors/authentication/index.js'
import { ModelAction, ModelActionKeys } from '../connectors/authorisation/actions.js'
Expand All @@ -10,9 +11,11 @@ import ModelCardRevisionModel, { ModelCardRevisionDoc } from '../models/ModelCar
import { UserInterface } from '../models/User.js'
import { GetModelCardVersionOptions, GetModelCardVersionOptionsKeys, GetModelFiltersKeys } from '../types/enums.js'
import { isValidatorResultError } from '../types/ValidatorResultError.js'
import { isReplicaSet } from '../utils/database.js'
import { toEntity } from '../utils/entity.js'
import { BadReq, Forbidden, NotFound } from '../utils/error.js'
import { BadReq, Forbidden, InternalError, NotFound } from '../utils/error.js'
import { convertStringToId } from '../utils/id.js'
import log from './log.js'
import { findSchemaById } from './schema.js'

export function checkModelRestriction(model: ModelInterface) {
Expand Down Expand Up @@ -254,9 +257,21 @@ export async function _setModelCard(
}

const revision = new ModelCardRevisionModel({ ...newDocument, modelId, createdBy: user.dn })
await revision.save()

await ModelModel.updateOne({ id: modelId }, { $set: { card: newDocument } })
if (!isReplicaSet()) {
throw InternalError('Database is not in replica set mode, cannot use transactions')
}

await mongoose.connection
.transaction(async function executeUpdate(session) {
await revision.save({ session })
await ModelModel.updateOne({ id: modelId }, { $set: { card: newDocument } }, { session: session })
})
.catch((error) => {
const message = 'Unable to save model card revision'
log.error('Error when updating model card/revision. Transaction rolled back.', error)
throw InternalError(message, { modelId })
})

return revision
}
Expand Down
5 changes: 5 additions & 0 deletions backend/src/utils/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ export async function connectToMongoose() {
}
}

export async function isReplicaSet(): Promise<boolean> {
const options = mongoose.connection.getClient().options
return Object.prototype.hasOwnProperty.call(options, 'replicaSet') && options.replicaSet.length > 0
}

export async function disconnectFromMongoose() {
await mongoose.disconnect()
log.info({ log: false }, 'Disconnected from Mongoose')
Expand Down
4 changes: 4 additions & 0 deletions backend/test/services/model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ const modelMocks = vi.hoisted(() => {
})
vi.mock('../../src/models/Model.js', () => ({ default: modelMocks }))

vi.mock('../../src/utils/database.ts', async () => ({
isReplicaSet: vi.fn(() => true),
}))

const authenticationMocks = vi.hoisted(() => ({
getEntities: vi.fn(() => ['user']),
}))
Expand Down
Loading