jstests/libs/clustered_collections/clustered_capped_utils.js
// Validate tailable cursor not keeping up with TTL deletion - CappedPositionLost.
Validate tailable cursor not keeping up with TTL deletion - CappedPositionLost.
Validate tailable cursor not keeping up with TTL deletion - CappedPositionLost .
static testClusteredTailableCursorCappedPositionLostWithTTL(
        db, collName, clusterKey, isReplicated, awaitData) {
        jsTest.log(
            "Validating tailable cursor falling behind with TTL deletion on capped clustered collection (isReplicated: " +
            isReplicated + ", awaitData: " + awaitData + ")");
        assert.commandWorked(db.adminCommand({setParameter: 1, ttlMonitorEnabled: false}));
        const oneDayInSeconds = 60 * 60 * 24;
        const nineDaysInMilliseconds = 9 * oneDayInSeconds * 1000;
        const tenDaysInMilliseconds = 10 * oneDayInSeconds * 1000;
        const tenDaysAgo = new Date(new Date() - tenDaysInMilliseconds);
        const nineDaysAgo = new Date(new Date() - nineDaysInMilliseconds);
        const today = new Date();
        assert.commandWorked(db.createCollection(collName, {
            clusteredIndex: {key: {[clusterKey]: 1}, unique: true},
            capped: true,
            expireAfterSeconds: oneDayInSeconds
        }));
        assert.commandWorked(
            db.getCollection(collName).insertOne({[clusterKey]: tenDaysAgo, info: "10 days ago"}));
        assert.commandWorked(
            db.getCollection(collName).insertOne({[clusterKey]: nineDaysAgo, info: "9 days ago"}));
        assert.commandWorked(
            db.getCollection(collName).insertOne({[clusterKey]: today, info: "today - surviving"}));
        let tailCommand = {find: collName, batchSize: 1, tailable: true, awaitData: awaitData};
        if (isReplicated) {
            tailCommand['readConcern'] = {level: "majority"};
        } else {
            tailCommand['readConcern'] = {level: "local"};
        }
        const tailable = db.runCommand(tailCommand);
        assert.commandWorked(tailable);
        const cursorId = tailable.cursor.id;
        assert(!bsonBinaryEqual({cursorId: cursorId}, {cursorId: NumberLong(0)}));
        assert.eq("10 days ago", tailable.cursor.firstBatch[0].info);
        assert.eq(3, db.getCollection(collName).find().itcount());
        assert.commandWorked(db.adminCommand({setParameter: 1, ttlMonitorEnabled: true}));
        ClusteredCollectionUtil.waitForTTL(db);
        assert.eq(1, db.getCollection(collName).find().itcount());
        let getMore = db.runCommand({getMore: cursorId, collection: collName, batchSize: 1});
        assert.commandFailedWithCode(getMore, 136);
        assert.eq(getMore.codeName, "CappedPositionLost");
        db.getCollection(collName).drop();
    }
static testClusteredTailableCursorCappedPositionLostWithTTL ( db , collName , clusterKey , isReplicated , awaitData ) { jsTest . log ( "Validating tailable cursor falling behind with TTL deletion on capped clustered collection (isReplicated: " + isReplicated + ", awaitData: " + awaitData + ")" ) ; assert . commandWorked ( db . adminCommand ( { setParameter : 1 , ttlMonitorEnabled : false } ) ) ; const oneDayInSeconds = 60 * 60 * 24 ; const nineDaysInMilliseconds = 9 * oneDayInSeconds * 1000 ; const tenDaysInMilliseconds = 10 * oneDayInSeconds * 1000 ; const tenDaysAgo = new Date ( new Date ( ) - tenDaysInMilliseconds ) ; const nineDaysAgo = new Date ( new Date ( ) - nineDaysInMilliseconds ) ; const today = new Date ( ) ; assert . commandWorked ( db . createCollection ( collName , { clusteredIndex : { key : { [ clusterKey ] : 1 } , unique : true } , capped : true , expireAfterSeconds : oneDayInSeconds } ) ) ; assert . commandWorked ( db . getCollection ( collName ) . insertOne ( { [ clusterKey ] : tenDaysAgo , info : "10 days ago" } ) ) ; assert . commandWorked ( db . getCollection ( collName ) . insertOne ( { [ clusterKey ] : nineDaysAgo , info : "9 days ago" } ) ) ; assert . commandWorked ( db . getCollection ( collName ) . insertOne ( { [ clusterKey ] : today , info : "today - surviving" } ) ) ; let tailCommand = { find : collName , batchSize : 1 , tailable : true , awaitData : awaitData } ; if ( isReplicated ) { tailCommand [ 'readConcern' ] = { level : "majority" } ; } else { tailCommand [ 'readConcern' ] = { level : "local" } ; } const tailable = db . runCommand ( tailCommand ) ; assert . commandWorked ( tailable ) ; const cursorId = tailable . cursor . id ; assert ( ! bsonBinaryEqual ( { cursorId : cursorId } , { cursorId : NumberLong ( 0 ) } ) ) ; assert . eq ( "10 days ago" , tailable . cursor . firstBatch [ 0 ] . info ) ; assert . eq ( 3 , db . getCollection ( collName ) . find ( ) . itcount ( ) ) ; assert . commandWorked ( db . adminCommand ( { setParameter : 1 , ttlMonitorEnabled : true } ) ) ; ClusteredCollectionUtil . waitForTTL ( db ) ; assert . eq ( 1 , db . getCollection ( collName ) . find ( ) . itcount ( ) ) ; let getMore = db . runCommand ( { getMore : cursorId , collection : collName , batchSize : 1 } ) ; assert . commandFailedWithCode ( getMore , 136 ) ; assert . eq ( getMore . codeName , "CappedPositionLost" ) ; db . getCollection ( collName ) . drop ( ) ; }
Validate tailable cursor not keeping up with TTL deletion - CappedPositionLost.
Validate tailable cursor not keeping up with TTL deletion - CappedPositionLost .
// Create a clustered capped collection, and insert two old documents subject to imminent // TTL deletion, and a recent document. // Tail up to and including the first document, before it gets TTL reaped. // TTL delete the two old documents, while the tailable cursor is still on the first one. // Confirm that the tailable cursor returns CappedPositionLost, as the document it was // pointing to has been TTL-deleted.