summaryrefslogtreecommitdiff
path: root/src/mongo/shell/data_consistency_checker.js
blob: 805fed616552ee8ee0626347272edbc49583d768 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var CollInfos = class {
    /**
     * OO wrapper around the response of db.getCollectionInfos() to avoid calling it multiple times.
     * This class stores information about all collections but its methods typically apply to a
     * single collection, so a collName is typically required to be passed in as a parameter.
     */
    constructor(conn, connName, dbName) {
        // Special listCollections filter to prevent reloading the view catalog.
        const listCollectionsFilter = {
            $or: [
                {type: 'collection'},
                {type: {$exists: false}},
            ]
        };
        this.conn = conn;
        this.connName = connName;
        this.dbName = dbName;
        this.collInfosRes = conn.getDB(dbName).getCollectionInfos(listCollectionsFilter);
    }

    ns(collName) {
        return this.dbName + '.' + collName;
    }

    /**
     * Do additional filtering to narrow down collections that have names in collNames.
     */
    filter(desiredCollNames) {
        this.collInfosRes = this.collInfosRes.filter(info => desiredCollNames.includes(info.name));
    }

    /**
     * Get collInfo for non-capped collections.
     *
     * Don't call isCapped(), which calls listCollections.
     */
    getNonCappedCollNames() {
        const infos = this.collInfosRes.filter(info => !info.options.capped);
        return infos.map(info => info.name);
    }

    hostAndNS(collName) {
        return `${this.conn.host}--${this.ns(collName)}`;
    }

    print(collectionPrinted, collName) {
        const ns = this.ns(collName);
        const alreadyPrinted = collectionPrinted.has(this.hostAndNS(collName));

        // Extract basic collection info.
        const coll = this.conn.getDB(this.dbName).getCollection(collName);
        let collInfo = null;

        const collInfoRaw = this.collInfosRes.find(elem => elem.name === collName);
        if (collInfoRaw) {
            collInfo = {
                ns: ns,
                host: this.conn.host,
                UUID: collInfoRaw.info.uuid,
                count: coll.find().itcount()
            };
        }

        const infoPrefix = `${this.connName}(${this.conn.host}) info for ${ns} : `;
        if (collInfo !== null) {
            if (alreadyPrinted) {
                print(`${this.connName} info for ${ns} already printed. Search for ` +
                      `'${infoPrefix}'`);
            } else {
                print(infoPrefix + tojsononeline(collInfo));
            }
        } else {
            print(infoPrefix + 'collection does not exist');
        }

        const collStats = this.conn.getDB(this.dbName).runCommand({collStats: collName});
        const statsPrefix = `${this.connName}(${this.conn.host}) collStats for ${ns}: `;
        if (collStats.ok === 1) {
            if (alreadyPrinted) {
                print(`${this.connName} collStats for ${ns} already printed. Search for ` +
                      `'${statsPrefix}'`);
            } else {
                print(statsPrefix + tojsononeline(collStats));
            }
        } else {
            print(`${statsPrefix}  error: ${tojsononeline(collStats)}`);
        }

        collectionPrinted.add(this.hostAndNS(collName));

        // Return true if collInfo & collStats can be retrieved for conn.
        return collInfo !== null && collStats.ok === 1;
    }
};

var DataConsistencyChecker = class {
    static dumpCollectionDiff(
        rst, collectionPrinted, primaryCollInfos, secondaryCollInfos, collName) {
        print('Dumping collection: ' + primaryCollInfos.ns(collName));

        const primaryExists = primaryCollInfos.print(collectionPrinted, collName);
        const secondaryExists = secondaryCollInfos.print(collectionPrinted, collName);

        if (!primaryExists || !secondaryExists) {
            print(`Skipping checking collection differences for ${
                primaryCollInfos.ns(collName)} since it does not exist on primary and secondary`);
            return;
        }

        const primary = primaryCollInfos.conn;
        const secondary = secondaryCollInfos.conn;

        const primarySession = primary.getDB('test').getSession();
        const secondarySession = secondary.getDB('test').getSession();
        const diff = rst.getCollectionDiffUsingSessions(
            primarySession, secondarySession, primaryCollInfos.dbName, collName);

        for (let {
                 primary: primaryDoc,
                 secondary: secondaryDoc,
             } of diff.docsWithDifferentContents) {
            print(`Mismatching documents between the primary ${primary.host}` +
                  ` and the secondary ${secondary.host}:`);
            print('    primary:   ' + tojsononeline(primaryDoc));
            print('    secondary: ' + tojsononeline(secondaryDoc));
        }

        if (diff.docsMissingOnPrimary.length > 0) {
            print(`The following documents are missing on the primary ${primary.host}:`);
            print(diff.docsMissingOnPrimary.map(doc => tojsononeline(doc)).join('\n'));
        }

        if (diff.docsMissingOnSecondary.length > 0) {
            print(`The following documents are missing on the secondary ${secondary.host}:`);
            print(diff.docsMissingOnSecondary.map(doc => tojsononeline(doc)).join('\n'));
        }
    }
};