summaryrefslogtreecommitdiff
path: root/jstests/concurrency/fsm_workloads/yield_geo_near_dedup.js
blob: 7d3af9d2c2477f3b71b5d7d1f0364517af317a7e (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
'use strict';

/*
 * Intersperses $geoNear aggregations with updates of non-geo fields to test deduplication.
 * @tags: [requires_non_retryable_writes]
 */
load('jstests/concurrency/fsm_libs/extend_workload.js');      // for extendWorkload
load('jstests/concurrency/fsm_workloads/yield_geo_near.js');  // for $config

var $config = extendWorkload($config, function($config, $super) {
    $config.states.remove = function remove(db, collName) {
        var id = Random.randInt(this.nDocs);
        var doc = db[collName].findOne({_id: id});
        if (doc !== null) {
            var res = db[collName].remove({_id: id});
            assertAlways.commandWorked(res);
            if (res.nRemoved > 0) {
                // Re-insert the document with the same '_id', but an incremented
                // 'timesInserted' to
                // distinguish it from the deleted document.
                doc.timesInserted++;
                assertAlways.commandWorked(db[collName].insert(doc));
            }
        }
    };

    /*
     * Use geo $nearSphere query to find points near the origin. Note this should be done using
     *the
     * geoNear command, rather than a $nearSphere query, as the $nearSphere query doesn't work
     *in a
     * sharded environment. Unfortunately this means we cannot batch the request.
     *
     * Only points are covered in this test as there is no guarantee that geometries indexed in
     * multiple cells will be deduplicated correctly with interspersed updates. If multiple
     *index
     * cells for the same geometry occur in the same search interval, an update may cause
     *geoNear
     * to return the same document multiple times.
     */
    $config.states.query = function geoNear(db, collName) {
        // This distance gets about 80 docs around the origin. There is one doc inserted
        // every 1m^2 and the area scanned by a 5m radius is PI*(5m)^2 ~ 79.
        const maxDistance = 5;
        const cursor = db[collName].aggregate([{
            $geoNear: {
                near: [0, 0],
                distanceField: "dist",
                maxDistance: maxDistance,
                spherical: true,
            }
        }]);

        // We only run the verification when workloads are run on separate collections, since the
        // aggregation may fail if we don't have exactly one 2d index to use.
        assertWhenOwnColl(function verifyResults() {
            const seenObjs = [];
            const seenObjsOriginals = [];
            while (cursor.hasNext()) {
                const doc = cursor.next();

                // The pair (_id, timesInserted) is the smallest set of attributes that uniquely
                // identifies a document.
                const objToSearch = {_id: doc._id, timesInserted: doc.timesInserted};
                for (let i = 0; i < seenObjs.length; ++i) {
                    assertWhenOwnColl.neq(
                        bsonWoCompare(seenObjs[i], objToSearch),
                        0,
                        () => `$geoNear returned document ${tojson(doc)} multiple ` +
                            `times: first occurence was ${tojson(seenObjsOriginals[i])}`);
                }
                seenObjs.push(objToSearch);
                seenObjsOriginals.push(doc);
            }
        });
    };

    $config.data.genUpdateDoc = function genUpdateDoc() {
        // Attempts to perform an in-place update to trigger an invalidation on MMAP v1.
        return {$inc: {timesUpdated: 1}};
    };

    $config.data.getIndexSpec = function getIndexSpec() {
        return {geo: '2dsphere'};
    };

    $config.data.getReplaceSpec = function getReplaceSpec(i, coords) {
        return {_id: i, geo: coords, timesUpdated: 0, timesInserted: 0};
    };

    return $config;
});