Skip to content
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
65 changes: 62 additions & 3 deletions cmake/tests/cdc_consumer_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
over them would make a cycle, which the sink refuses, so the consumer halts;
(v) documents just under 16 MB, whose events would pass the limit and so arrive without them
(fullDocumentOmitted): the consumer reads them from the source, and skips one deleted
since.
since;
(vi) drop-database: after the bootstrap copies three collections and a view, dropping the
source database removes them from the sink through streamed drops, not another bootstrap;
an empty system.views may remain, and an out-of-scope sink sentinel stays unchanged.

The source needs the change stream, so a WAL; a node built with fault points is not required.
Requires pymongo.
Expand Down Expand Up @@ -297,6 +300,61 @@ def scenario_converge(source, sink):
flush=True)


def scenario_drop_database(source, sink):
"""(vi) A database drop streams every collection drop, including system.views."""
db = source.client[DB]
collections = ("empty", "populated", "indexed")
for name in collections:
db.create_collection(name)
db.populated.insert_one({"_id": 1, "value": "copied"})
db.indexed.insert_one({"_id": 1, "x": 1})
db.indexed.create_index("x")
db.command({"create": "all_items", "viewOn": "populated", "pipeline": []})
sink.client[OTHER_DB].keep.insert_one({"_id": "sink-only"})
# Make the initial bootstrap issue dropDatabase, so the listener is exercised before the test.
sink.client[DB].stale.insert_one({"_id": "stale"})
recorder = DdlRecorder()
sink_client = MongoClient("127.0.0.1", sink.port, directConnection=True, w=1,
event_listeners=[recorder])
consumer = ConsumerThread(source.client, sink_client)
try:
copied = wait_converged(source.client, sink.client, "before dropping the database")
assert set(collections) | {"all_items"} <= set(copied), copied
assert any(name == "dropDatabase" for name, _ in recorder.commands), recorder.commands
bootstrap_end = len(recorder.commands)

source.client.drop_database(DB)
deadline = time.monotonic() + CONVERGE_TIMEOUT_S
while True:
assert consumer.error is None, consumer.error
assert consumer.thread.is_alive(), "the consumer stopped after dropDatabase"
remaining = set(sink.client[DB].list_collection_names())
# drop_views drops each view, not system.views itself; MongoDB may retain it empty.
if remaining <= {cdc.VIEWS_COLLECTION}:
break
assert time.monotonic() < deadline, \
f"after dropDatabase: the sink still holds {sorted(remaining)}"
time.sleep(1)
assert list(sink.client[DB].system.views.find()) == []
assert sink.client[OTHER_DB].list_collection_names() == ["keep"], \
sink.client[OTHER_DB].list_collection_names()
assert list(sink.client[OTHER_DB].keep.find()) == [{"_id": "sink-only"}]
progress = sink.client.admin[cdc.PROGRESS_COLLECTION].find_one({"_id": "progress"})
assert progress is not None and progress["state"] == "streaming", progress
finally:
consumer.stop()
sink_client.close()
# Inspect the complete command history after stopping: a fresh bootstrap could otherwise
# clear the sink and make missing CDC drops look successful.
streamed = recorder.commands[bootstrap_end:]
assert all(name != "dropDatabase" for name, _ in streamed), streamed
# Three collection drops, plus the view drop caused by the source's system.views drop.
assert sum(name == "drop" for name, _ in streamed) >= len(collections) + 1, streamed
assert consumer.error is None, consumer.error
print("PASS (vi) dropDatabase streamed all collection and view drops without bootstrapping "
"again; the out-of-scope sentinel stayed unchanged", flush=True)


INDEX_STEPS = (lambda db: db.orders.create_index([("x", 1)], name="i"),
lambda db: db.orders.drop_index("i"),
lambda db: db.orders.create_index([("y", 1)], name="i"))
Expand Down Expand Up @@ -513,7 +571,7 @@ def main():
parser.add_argument("--scenario", action="append",
help="Run only this scenario (converge, index-race-gate, "
"index-race-no-gate, crash-before-publish, view-race-gate, "
"view-race-no-gate, oversized); may be repeated")
"view-race-no-gate, oversized, drop-database); may be repeated")
args = parser.parse_args()
if hasattr(os, "sched_getaffinity"):
os.sched_setaffinity(0, sorted(os.sched_getaffinity(0))[:8])
Expand All @@ -524,7 +582,8 @@ def main():
("crash-before-publish", scenario_crash_before_publish),
("view-race-gate", lambda s, k: scenario_view_race(s, k, True)),
("view-race-no-gate", lambda s, k: scenario_view_race(s, k, False)),
("oversized", scenario_oversized))
("oversized", scenario_oversized),
("drop-database", scenario_drop_database))
for label, scenario in scenarios:
if args.scenario and label not in args.scenario:
continue
Expand Down
8 changes: 5 additions & 3 deletions src/mongo/db/storage/kv/kv_storage_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,18 +369,20 @@ Status KVStorageEngine::_dropCollections(OperationContext* opCtx,
std::vector<std::string>& collections) {
MONGO_LOG(1) << "KVStorageEngine::_dropCollections";

// Each collection drop needs its own transaction. Reusing a transaction number makes the
// log service treat later schema drops as retries, so their CDC records are lost.
invariant(!opCtx->lockState()->inAWriteUnitOfWork());
Status firstError = Status::OK();
WriteUnitOfWork wuow(opCtx);
for (const auto& collection : collections) {
// NamespaceString nss{std::move(collection)};
WriteUnitOfWork wuow(opCtx);

Status result = dbce->dropCollection(opCtx, collection);
if (!result.isOK() && firstError.isOK()) {
firstError = result;
}
wuow.commit();
}

wuow.commit();
return firstError;
}
/**
Expand Down
132 changes: 132 additions & 0 deletions tests/jstests/eloq_cdc/drop_database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/**
* dropDatabase must emit one drop per collection, including empty collections. Database and
* deployment streams see all drops and can resume between them; each collection stream is
* invalidated. A later insert into a recreated collection bounds the drops without relying on
* collection enumeration order or a fixed sleep.
*/
(function() {
"use strict";

load("tests/jstests/libs/eloq_cdc_util.js");

const testDB = db.getSiblingDB("eloq_cdc_drop_database");
const admin = db.getSiblingDB("admin");
const names = ["empty", "indexed", "populated"];
const marker = {_id: "after-drop", value: 2};
const deploymentFilter = [{$match: {"ns.db": testDB.getName()}}];
const streams = [];

assert.commandWorked(testDB.dropDatabase());
for (let name of names) {
assert.commandWorked(testDB.createCollection(name));
}
assert.commandWorked(testDB.indexed.createIndex({value: 1}));
assert.commandWorked(testDB.populated.insert({_id: "before-drop", value: 1}));
EloqCdcUtil.settleDdl(db.getMongo());

function open(database, collection, spec, pipeline) {
const reply = assert.commandWorked(EloqCdcUtil.openRaw(
db.getMongo(), database.getName(), collection, spec, pipeline, {batchSize: 0}));
assert.eq(reply.cursor.firstBatch, [], tojson(reply));
const stream = {
db: database,
collection: reply.cursor.ns.substring(reply.cursor.ns.indexOf(".") + 1),
id: reply.cursor.id,
events: []
};
streams.push(stream);
return stream;
}

function getMore(stream) {
const reply = assert.commandWorked(stream.db.runCommand(
{getMore: stream.id, collection: stream.collection, maxTimeMS: 1000}));
stream.id = reply.cursor.id;
stream.events = stream.events.concat(reply.cursor.nextBatch);
return reply.cursor;
}

function isMarker(event) {
return event.operationType === "insert" && event.ns.coll === "populated" &&
event.documentKey._id === marker._id;
}

// The marker follows every drop. Read through its watermark too, so duplicate or unexpected
// events fail the test rather than being left in an unread batch.
function readThroughMarker(stream) {
assert.soon(function() {
assert.neq(stream.id, 0, "dropDatabase closed a database/deployment stream");
getMore(stream);
return stream.events.some(isMarker);
}, () => "marker never arrived; events: " + tojson(stream.events),
EloqCdcUtil.kEventTimeoutMS, 100);
const last = stream.events[stream.events.length - 1];
assert(isMarker(last), tojson(stream.events));
const markerTs = EloqCdcUtil.tokenTimestamp(last._id);
assert.soon(function() {
assert.neq(stream.id, 0, "dropDatabase closed a database/deployment stream");
const cursor = getMore(stream);
assert.eq(cursor.nextBatch, [], "unexpected events: " + tojson(cursor.nextBatch));
const token = cursor.postBatchResumeToken;
return EloqCdcUtil.tokenKind(token) === 1 &&
EloqCdcUtil.tokenTimestamp(token) >= markerTs;
}, "the watermark never passed the marker", EloqCdcUtil.kEventTimeoutMS, 100);
return stream.events;
}

try {
const database = open(testDB, 1, {});
const deployment = open(admin, 1, {allChangesForCluster: true}, deploymentFilter);
const collections = names.map(name => open(testDB, name, {}));

assert.commandWorked(testDB.dropDatabase());
assert.eq(testDB.getCollectionNames(), []);
// Repeating the drop on an empty/missing database must not emit more drops.
assert.commandWorked(testDB.dropDatabase());
assert.commandWorked(testDB.populated.insert(marker));

const events = readThroughMarker(database);
assert.eq(events.map(e => e.operationType),
["drop", "drop", "drop", "create", "insert"], tojson(events));
const drops = events.slice(0, names.length);
assert.eq(drops.map(e => e.ns.coll).sort(), names, tojson(drops));
for (let event of events) {
assert.eq(event.ns.db, testDB.getName(), tojson(event));
}
assert.eq(events[3].ns.coll, "populated", tojson(events[3]));
assert.eq(events[4].fullDocument, marker, tojson(events[4]));
assert.eq(readThroughMarker(deployment), events);

// The first emitted drop is not necessarily the first name in 'names'. Resume from the
// actual event, and require all remaining drops plus the new incarnation, exactly once.
const resumeAfter = drops[0]._id;
assert.eq(readThroughMarker(open(testDB, 1, {resumeAfter: resumeAfter})), events.slice(1));
assert.eq(readThroughMarker(open(admin, 1,
{allChangesForCluster: true, resumeAfter: resumeAfter},
deploymentFilter)),
events.slice(1));

// All original collection streams close, even the one whose namespace was recreated.
for (let stream of collections) {
assert.soon(function() {
if (stream.id != 0) {
getMore(stream);
}
return stream.id == 0;
}, () => "collection stream did not close: " + tojson(stream.events),
EloqCdcUtil.kEventTimeoutMS, 100);
assert.eq(stream.events.map(e => e.operationType), ["drop", "invalidate"],
tojson(stream.events));
assert.eq(stream.events[0], drops.find(e => e.ns.coll === stream.collection));
}
assert.eq(testDB.populated.find().toArray(), [marker]);
} finally {
for (let stream of streams) {
if (stream.id != 0) {
assert.commandWorked(stream.db.runCommand(
{killCursors: stream.collection, cursors: [stream.id]}));
}
}
}
assert.commandWorked(testDB.dropDatabase());
})();
Loading