summaryrefslogtreecommitdiff
path: root/jstests/sharding/update_zone_key_range.js
blob: eae462f7c6f0ea19f1bc470b49e20fa533fef20f (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
 * Basic integration tests for updateZoneKeyRange command on sharded, unsharded, and sharded with
 * compound shard key collections.
 */

(function() {
'use strict';

var st = new ShardingTest({shards: 1});

var configDB = st.s.getDB('config');
var shardName = configDB.shards.findOne()._id;

assert.commandWorked(st.s.adminCommand({addShardToZone: shardName, zone: 'x'}));
assert.commandWorked(st.s.adminCommand({enableSharding: 'test'}));

var currentMinBoundary;
var currentMaxBoundary;
var currentZone;

function testZoneOnShard(ns, testParameters) {
    var chunkMinBoundary = testParameters["min"];
    var chunkMaxBoundary = testParameters["max"];
    var zone = testParameters["zone"];
    var returnCode = testParameters["returnCode"];

    if (returnCode === 0) {
        assert.commandWorked(st.s.adminCommand(
            {updateZoneKeyRange: ns, min: chunkMinBoundary, max: chunkMaxBoundary, zone: zone}));
        var tagDoc = configDB.tags.findOne();
        if (zone === null) {
            // Testing basic remove
            assert.eq(null, tagDoc);
        } else {
            // Testing basic assign works
            currentMinBoundary = chunkMinBoundary;
            currentMaxBoundary = chunkMaxBoundary;
            currentZone = zone;
            verifyChunkBounds(tagDoc, ns, currentMinBoundary, currentMaxBoundary, currentZone);
        }
    } else {
        assert.commandFailedWithCode(
            st.s.adminCommand(
                {updateZoneKeyRange: ns, min: chunkMinBoundary, max: chunkMaxBoundary, zone: zone}),
            returnCode);

        var tagDoc = configDB.tags.findOne();
        verifyChunkBounds(tagDoc, ns, currentMinBoundary, currentMaxBoundary, currentZone);
    }
}

function verifyChunkBounds(tagDoc, ns, minKey, maxKey, tag) {
    assert.eq(ns, tagDoc.ns);
    assert.eq(minKey, tagDoc.min);
    assert.eq(maxKey, tagDoc.max);
    assert.eq(tag, tagDoc.tag);
}

var basicIntegrationTestCases = [
    {'min': {x: 0}, 'max': {x: 10}, 'zone': 'x', 'returnCode': 0},
    {'min': {x: -10}, 'max': {x: 20}, 'zone': 'x', 'returnCode': ErrorCodes.RangeOverlapConflict},
    {'min': {x: 10}, 'max': {x: 0}, 'zone': 'x', 'returnCode': ErrorCodes.FailedToParse},
    {'min': {x: 0}, 'max': {x: 10}, 'zone': null, 'returnCode': 0}
];

/**
 * Basic integration test for updateZoneKeyRange ensuring we can successfully set zone boundaries
 * min: {x:0}, max: {x:10}. Then goes through the following cases:
 *
 * Case 1:
 *  Fails to update zone key range that overlaps with existing zone
 *  min: {x: -10}, max: {x: 20}
 *
 * Case 2:
 *  Fails to update zone key range with invalid range min > max
 *  min: {x: 10}, max: {x: 0}
 *
 * Case 3:
 *  Successfully does basic remove of zone by setting zone to null
 *
 */

assert.commandWorked(st.s.adminCommand({shardCollection: 'test.ranged', key: {x: 1}}));
basicIntegrationTestCases.forEach(function(test) {
    testZoneOnShard('test.ranged', test);
});

/**
 * Basic integration test for updateZoneKeyRange on an unsharded collection ensuring we can
 * successfully set zone boundaries min: {x:0}, max: {x:10}. Then goes through the same cases as
 * before
 */

basicIntegrationTestCases.forEach(function(test) {
    testZoneOnShard('test.unsharded', test);
});

/**
 * Basic integration test for updateZoneKeyRange on an sharded collection with a compound key
 * ensuring we can successfully set zone boundaries min: {_id: 0, x: 0}, max: {_id: 100, x: 10}.
 * Then goes through the following cases:
 *
 * Case 1:
 *  Fails to update zone key range with invalid second field in compound key range min > max
 *  min: {_id: 100, x: 10}, max: {_id: 100, x: 1}
 *
 * Case 2:
 *  Fails to update zone key range with invalid first field in compound key range min > max
 *  min: {_id: 10, x: 1}, max: {_id: 1, x: 10}
 *
 * Case 3:
 *  Fails to update zone key range with invalid both invalid fields in compound key range min > max
 *  min: {_id: 10, x: 10}, max: {_id: 1, x: 1}
 *
 * Case 4:
 *  Successfully does basic remove of zone by setting zone to null
 */

var compoundKeyTestCases = [
    {'min': {_id: 0, x: 0}, 'max': {_id: 100, x: 10}, 'zone': 'x', 'returnCode': 0},
    {
        'min': {_id: 100, x: 10},
        'max': {_id: 100, x: 1},
        'zone': 'x',
        'returnCode': ErrorCodes.FailedToParse
    },
    {
        'min': {_id: 10, x: 1},
        'max': {_id: 1, x: 10},
        'zone': 'x',
        'returnCode': ErrorCodes.FailedToParse
    },
    {
        'min': {_id: 10, x: 10},
        'max': {_id: 1, x: 1},
        'zone': 'x',
        'returnCode': ErrorCodes.FailedToParse
    },
    {
        'min': {_id: 0, x: 0},
        'max': {_id: 0, x: 0},
        'zone': 'x',
        'returnCode': ErrorCodes.FailedToParse
    },
    {'min': {_id: 0, x: 0}, 'max': {_id: 100, x: 10}, 'zone': null, 'returnCode': 0}
];

assert.commandWorked(st.s.adminCommand({shardCollection: 'test.compound', key: {_id: 1, x: 1}}));
compoundKeyTestCases.forEach(function(test) {
    testZoneOnShard('test.compound', test);
});

st.stop();
})();