function incrementOID(oid) { const prefix = oid.toString().substr(10, 16); const suffix = oid.toString().substr(26, 8); const number = parseInt(suffix, 16) + 1; const incremented = number.toString(16).padStart(8, '0'); return ObjectId(prefix + incremented); } const conn = MongoRunner.runMongod(); const testDB = conn.getDB(jsTestName()); const coll = testDB.getCollection('t'); const bucketsColl = testDB.getCollection('system.buckets.' + coll.getName()); const timeFieldName = 'time'; const metaFieldName = 'meta'; assert.commandWorked(testDB.createCollection( coll.getName(), {timeseries: {timeField: timeFieldName, metaField: metaFieldName}})); assert.contains(bucketsColl.getName(), testDB.getCollectionNames()); // Insert a measurement to create a bucket. assert.commandWorked( coll.insert({[timeFieldName]: ISODate("2023-08-01T00:00:00.000Z"), [metaFieldName]: 1})); // Get a bucket ID, and generate the next consecutive id. const id1 = bucketsColl.findOne({meta: 1})._id; const id2 = incrementOID(id1); // Create a bucket that we will corrupt. When we try to reopen this bucket, we will notice that it // is uncompressed but should be compressed, so we will try to compress it. However, since it will // be corrupted, compression will fail and we freeze it. let uncompressedCorruptedBucket = { _id: id2, control: { version: 1, min: {time: ISODate("2023-08-01T00:00:00.000Z"), a: 1}, max: {time: ISODate("2023-08-01T00:00:00.000Z"), a: 1} }, [metaFieldName]: 2, data: { time: {0: ISODate("2023-08-01T00:00:00.000Z"), "1": ISODate("2023-08-01T00:00:00.000Z")}, "a": {"1": 1}, } }; // Insert our corrupted bucket directly into the buckets collection. bucketsColl.insert(uncompressedCorruptedBucket); // Corrupt our bucket by adding an out of order index in the a column. let res = assert.commandWorked( bucketsColl.updateOne({_id: id2}, {$set: {"data.a.0": 0, "control.min.a": 0}})); // When we try to insert this measurement, we will try to reopen the corrupted bucket first. After // we fail to compress it and freeze it, we will try to allocate a new bucket with the next // sequential OID. However, while initializing the bucket state of our new bucket we will see that // there is an existing bucket in a different stripe with the same OID (the corrupted bucket we // created) that is frozen; when this happens, we throw an exception and fail the write. assert.commandWorked( coll.insert({[timeFieldName]: ISODate("2023-08-01T00:00:00.000Z"), [metaFieldName]: 2})); MongoRunner.stopMongod(conn, null, {skipValidation: true});