summaryrefslogtreecommitdiff
path: root/jstests/libs/trace_missing_docs.js
blob: 3bc9ef753330e51c9667699c8c64b575d4320069 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

//
// On error inserting documents, traces back and shows where the document was dropped
//

function traceMissingDoc(coll, doc, mongos) {
    if (mongos)
        coll = mongos.getCollection(coll + "");
    else
        mongos = coll.getMongo();

    var config = mongos.getDB("config");
    var shards = config.shards.find().toArray();
    for (var i = 0; i < shards.length; i++) {
        shards[i].conn = new Mongo(shards[i].host);
    }

    var shardKeyPatt = config.collections.findOne({_id: coll + ""}).key;

    // Project out the shard key
    var shardKey = {};
    for (var k in shardKeyPatt) {
        if (doc[k] == undefined) {
            jsTest.log("Shard key " + tojson(shardKey) + " not found in doc " + tojson(doc) +
                       ", falling back to _id search...");
            shardKeyPatt = {
                _id: 1
            };
            shardKey = {
                _id: doc['_id']
            };
            break;
        }
        shardKey[k] = doc[k];
    }

    if (doc['_id'] == undefined) {
        jsTest.log("Id not found in doc " + tojson(doc) + " cannot trace oplog entries.");
        return;
    }

    jsTest.log("Using shard key : " + tojson(shardKey));

    var allOps = [];
    for (var i = 0; i < shards.length; i++) {
        var oplog = shards[i].conn.getCollection("local.oplog.rs");
        if (!oplog.findOne()) {
            oplog = shards[i].conn.getCollection("local.oplog.$main");
        }

        if (!oplog.findOne()) {
            jsTest.log("No oplog was found on shard " + shards[i]._id);
            continue;
        }

        var addKeyQuery = function(query, prefix) {
            for (var k in shardKey) {
                query[prefix + '.' + k] = shardKey[k];
            }
            return query;
        };

        var addToOps = function(cursor) {
            cursor.forEach(function(doc) {
                doc.shard = shards[i]._id;
                doc.realTime = new Date(doc.ts.getTime() * 1000);
                allOps.push(doc);
            });
        };

        // Find ops
        addToOps(oplog.find(addKeyQuery({op: 'i'}, 'o')));
        var updateQuery = {
            $or: [addKeyQuery({op: 'u'}, 'o2'), {op: 'u', 'o2._id': doc['_id']}]
        };
        addToOps(oplog.find(updateQuery));
        addToOps(oplog.find({op: 'd', 'o._id': doc['_id']}));
    }

    var compareOps = function(opA, opB) {
        return bsonWoCompare(opA.ts, opB.ts);
    };

    allOps.sort(compareOps);

    print("Ops found for doc " + tojson(doc) + " on each shard:\n");
    for (var i = 0; i < allOps.length; i++) {
        printjson(allOps[i]);
    }

    return allOps;
}