// Connect to the 'local' database where the oplog resides //use local; const { MongoClient } = require('mongodb') //const client=new MongoClient('mongodb+srv://gayatriparekh:gayu%40211@cluster0.9fplife.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0') const client=new MongoClient('mongodb+srv://localhost:27017') // Connect to database client.connect() .then(() => console.log('Connected Successfully')) .catch(error => console.log('Failed to connect', error)) //----- const session = client.startSession(); session.startTransaction(); try { const db = client.db('gkdbase');//const db = client.db('local'); const collection = db.collection('log2');//const collection = db.collection('oplog.rs'); //const abortTrans= await collection.find({sessionId}).toArray(); } catch (error) { //await session.abortTransaction(); console.error(`Undo transaction ${sessionId} aborted: ${error}`); } finally { session.endSession(); } //----- // Function to analyze the oplog and find potential aborted transactions async function findAbortedTransactions() { print("Searching for potential aborted transactions in oplog..."); // Fetch all oplog entries related to transactions const cursor = db.oplog.rs.find({ "o.applyOps": { $exists: true }, // Look for entries with 'applyOps', indicating a multi-document transaction }).sort({ ts: -1 }); // Sort by timestamp, most recent first let abortedTransactions = []; // Iterate through the oplog entries cursor.forEach(oplogEntry => { const oplogTs = oplogEntry.ts; // Oplog timestamp const transactionId = oplogEntry.txnNumber || "Unknown"; // Transaction ID if available const applyOps = oplogEntry.o.applyOps; // Operations in the transaction // Analyze transaction details let hasAbortMarker = false; // Look for specific patterns indicating a transaction abort or rollback applyOps.forEach(op => { if (op.abort || op.rollback) { hasAbortMarker = true; } }); // If no commit marker exists or if abort markers are found if (hasAbortMarker) { abortedTransactions.push({ ts: oplogTs, txnNumber: transactionId, details: oplogEntry }); } }); // Print the results if (abortedTransactions.length > 0) { print("Found potential aborted transactions:"); abortedTransactions.forEach(txn => { printjson({ "Timestamp": txn.ts, "Transaction Number": txn.txnNumber, "Details": txn.details }); }); } else { print("No aborted transactions found in the oplog."); try { const db = client.db('gkdbase'); //const collection = db.collection('testcol'); const undoLog = db.collection('undo_log2');//const undoLog = db.collection('undo_log'); const undoOperations = await undoLog.find({ sessionId }).toArray(); for (let op of undoOperations) { if (op.operation === 'delete') { await collection.deleteOne({ _id: op.documentId }, { session }); } else if (op.operation === 'insert') { await collection.insertOne(op.document, { session }); } else if (op.operation === 'update') { await collection.updateOne({ _id: op.documentId }, { $set: op.originalDocument }, { session }); } } await session.commitTransaction(); console.log(`Undo transaction ${sessionId} committed successfully`); } catch (error) { await session.abortTransaction(); console.error(`Undo transaction ${sessionId} aborted: ${error}`); } finally { session.endSession(); } } } // Run the function findAbortedTransactions();