Skip to content

Commit

Permalink
Delete from concurrences collection
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidJGapCR committed Oct 9, 2023
1 parent 8e5c807 commit 4e3dc78
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
// The Ed-Fi Alliance licenses this file to you under the Apache License, Version 2.0.
// See the LICENSE and NOTICES files in the project root for more information.

import { MeadowlarkId } from '@edfi/meadowlark-core';
import { DocumentUuid, MeadowlarkId } from '@edfi/meadowlark-core';

export interface ConcurrencyDocument {
meadowlarkId: MeadowlarkId;
meadowlarkIds: MeadowlarkId[];
_id: MeadowlarkId | DocumentUuid;
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,20 @@ export const limitFive = (session: ClientSession): FindOptions => ({ limit: 5, s
* */
export async function insertMeadowlarkIdOnConcurrencyCollection(
concurrencyCollection: Collection<ConcurrencyDocument>,
meadowlarkId: MeadowlarkId,
document: ConcurrencyDocument,
session: ClientSession,
meadowlarkIds: ConcurrencyDocument[],
): Promise<void> {
const { acknowledged, upsertedCount, modifiedCount } = await concurrencyCollection.replaceOne(
{ _id: meadowlarkId },
document,
asUpsert(session),
);
const { acknowledged, insertedCount, insertedIds } = await concurrencyCollection.insertMany(meadowlarkIds);

// eslint-disable-next-line no-console
console.log(acknowledged, insertedCount, insertedIds);
}

export async function deleteMeadowlarkIdOnConcurrencyCollection(
concurrencyCollection: Collection<ConcurrencyDocument>,
meadowlarkIds: ConcurrencyDocument[],
): Promise<void> {
const { acknowledged, deletedCount } = await concurrencyCollection.deleteMany({ meadowlarkIds });

console.log(acknowledged, upsertedCount, modifiedCount);
// eslint-disable-next-line no-console
console.log(acknowledged, deletedCount);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ import { DeleteResult, DeleteRequest, ReferringDocumentInfo, DocumentUuid, Trace
import { ClientSession, Collection, MongoClient, WithId } from 'mongodb';
import retry from 'async-retry';
import { MeadowlarkDocument } from '../model/MeadowlarkDocument';
import { getDocumentCollection, limitFive, onlyReturnId } from './Db';
import {
deleteMeadowlarkIdOnConcurrencyCollection,
getConcurrencyCollection,
getDocumentCollection,
insertMeadowlarkIdOnConcurrencyCollection,
limitFive,
onlyReturnId,
} from './Db';
import { onlyReturnAliasIds, onlyDocumentsReferencing } from './ReferenceValidation';
import { ConcurrencyDocument } from '../model/ConcurrencyDocument';

const moduleName: string = 'mongodb.repository.Delete';

Expand Down Expand Up @@ -75,6 +83,7 @@ async function checkForReferencesToDocument(
export async function deleteDocumentByMeadowlarkIdTransaction(
{ documentUuid, validateNoReferencesToDocument, traceId }: DeleteRequest,
mongoCollection: Collection<MeadowlarkDocument>,
concurrencyCollection: Collection<ConcurrencyDocument>, // RND-644
session: ClientSession,
): Promise<DeleteResult> {
if (validateNoReferencesToDocument) {
Expand All @@ -93,8 +102,18 @@ export async function deleteDocumentByMeadowlarkIdTransaction(
traceId,
);

const concurrencyDocuments: ConcurrencyDocument[] = [
{
_id: documentUuid,
},
];

await insertMeadowlarkIdOnConcurrencyCollection(concurrencyCollection, concurrencyDocuments); // RND-644

const { acknowledged, deletedCount } = await mongoCollection.deleteOne({ documentUuid }, { session });

await deleteMeadowlarkIdOnConcurrencyCollection(concurrencyCollection, concurrencyDocuments); // RND-644

if (!acknowledged) {
const msg =
'mongoCollection.deleteOne returned acknowledged: false, indicating a problem with write concern configuration';
Expand All @@ -119,13 +138,19 @@ export async function deleteDocumentByDocumentUuid(
let deleteResult: DeleteResult = { response: 'UNKNOWN_FAILURE', failureMessage: '' };
try {
const mongoCollection: Collection<MeadowlarkDocument> = getDocumentCollection(client);
const concurrencyCollection: Collection<ConcurrencyDocument> = getConcurrencyCollection(client);

const numberOfRetries: number = Config.get('MONGODB_MAX_NUMBER_OF_RETRIES');

await retry(
async () => {
await session.withTransaction(async () => {
deleteResult = await deleteDocumentByMeadowlarkIdTransaction(deleteRequest, mongoCollection, session);
deleteResult = await deleteDocumentByMeadowlarkIdTransaction(
deleteRequest,
mongoCollection,
concurrencyCollection,
session,
);
if (deleteResult.response !== 'DELETE_SUCCESS') {
await session.abortTransaction();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
writeLockReferencedDocuments,
insertMeadowlarkIdOnConcurrencyCollection,
getConcurrencyCollection,
deleteMeadowlarkIdOnConcurrencyCollection,
} from './Db';
import { deleteDocumentByMeadowlarkIdTransaction } from './Delete';
import { onlyDocumentsReferencing, validateReferences } from './ReferenceValidation';
Expand Down Expand Up @@ -170,17 +171,12 @@ async function updateAllowingIdentityChange(
// Ensure referenced documents are not modified in other transactions
await writeLockReferencedDocuments(mongoCollection, document.outboundRefs, session);

const concurrencyDocument: ConcurrencyDocument = {
meadowlarkId: updateRequest.meadowlarkId,
meadowlarkIds: document.outboundRefs,
};
const concurrencyDocuments: ConcurrencyDocument[] = document.outboundRefs.map((ref) => ({
_id: ref,
}));
concurrencyDocuments.push({ _id: updateRequest.meadowlarkId });

await insertMeadowlarkIdOnConcurrencyCollection(
concurrencyCollection,
updateRequest.meadowlarkId,
concurrencyDocument,
session,
); // RND-644
await insertMeadowlarkIdOnConcurrencyCollection(concurrencyCollection, concurrencyDocuments); // RND-644

return tryUpdateByReplacementResult;
}
Expand Down Expand Up @@ -213,6 +209,7 @@ async function updateAllowingIdentityChange(
const deleteResult = await deleteDocumentByMeadowlarkIdTransaction(
{ documentUuid, resourceInfo, security, validateNoReferencesToDocument: true, traceId },
mongoCollection,
concurrencyCollection,
session,
);
Logger.debug(`${moduleName}.updateAllowingIdentityChange: Updating document uuid ${documentUuid}`, traceId);
Expand Down Expand Up @@ -263,17 +260,12 @@ async function updateDisallowingIdentityChange(
// Ensure referenced documents are not modified in other transactions
await writeLockReferencedDocuments(mongoCollection, document.outboundRefs, session);

const concurrencyDocument: ConcurrencyDocument = {
meadowlarkId: updateRequest.meadowlarkId,
meadowlarkIds: document.outboundRefs,
};
const concurrencyDocuments: ConcurrencyDocument[] = document.outboundRefs.map((ref) => ({
_id: ref,
}));
concurrencyDocuments.push({ _id: updateRequest.meadowlarkId });

await insertMeadowlarkIdOnConcurrencyCollection(
concurrencyCollection,
updateRequest.meadowlarkId,
concurrencyDocument,
session,
); // RND-644
await insertMeadowlarkIdOnConcurrencyCollection(concurrencyCollection, concurrencyDocuments); // RND-644

const tryUpdateByReplacementResult: UpdateResult | null = await tryUpdateByReplacement(
document,
Expand All @@ -282,6 +274,8 @@ async function updateDisallowingIdentityChange(
session,
);

await deleteMeadowlarkIdOnConcurrencyCollection(concurrencyCollection, concurrencyDocuments); // RND-644

if (tryUpdateByReplacementResult != null) return tryUpdateByReplacementResult;

// Failure to match means either:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
onlyReturnDocumentUuidAndTimestamps,
insertMeadowlarkIdOnConcurrencyCollection,
getConcurrencyCollection,
deleteMeadowlarkIdOnConcurrencyCollection,
} from './Db';
import { onlyDocumentsReferencing, validateReferences } from './ReferenceValidation';
import { ConcurrencyDocument } from '../model/ConcurrencyDocument';
Expand Down Expand Up @@ -141,12 +142,12 @@ export async function upsertDocumentTransaction(

await writeLockReferencedDocuments(mongoCollection, document.outboundRefs, session);

const concurrencyDocument: ConcurrencyDocument = {
meadowlarkId,
meadowlarkIds: document.outboundRefs,
};
const concurrencyDocuments: ConcurrencyDocument[] = document.outboundRefs.map((ref) => ({
_id: ref,
}));
concurrencyDocuments.push({ _id: meadowlarkId });

await insertMeadowlarkIdOnConcurrencyCollection(concurrencyCollection, meadowlarkId, concurrencyDocument, session); // RND-644
await insertMeadowlarkIdOnConcurrencyCollection(concurrencyCollection, concurrencyDocuments); // RND-644
// Perform the document upsert
Logger.debug(`${moduleName}.upsertDocumentTransaction Upserting document uuid ${documentUuid}`, traceId);

Expand All @@ -156,6 +157,8 @@ export async function upsertDocumentTransaction(
asUpsert(session),
);

await deleteMeadowlarkIdOnConcurrencyCollection(concurrencyCollection, concurrencyDocuments); // RND-644

if (!acknowledged) {
const msg =
'mongoCollection.replaceOne returned acknowledged: false, indicating a problem with write concern configuration';
Expand Down Expand Up @@ -200,7 +203,7 @@ export async function upsertDocument(upsertRequest: UpsertRequest, client: Mongo
},
{
retries: numberOfRetries,
onRetry: () => {
onRetry: async () => {
Logger.warn(
`${moduleName}.upsertDocument got write conflict error for meadowlarkId ${upsertRequest.meadowlarkId}. Retrying...`,
upsertRequest.traceId,
Expand Down

0 comments on commit 4e3dc78

Please sign in to comment.