summaryrefslogtreecommitdiff
path: root/jstests/sharding/hash_basic.js
blob: e4bf6ded27be07c6f2face35c51eca7ac3da16ca (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
var st = new ShardingTest({shards: 2, chunkSize: 1});

var testDB = st.s.getDB('test');
testDB.adminCommand({enableSharding: 'test'});
st.ensurePrimaryShard('test', 'shard0001');
testDB.adminCommand({shardCollection: 'test.user', key: {x: 'hashed'}});

var configDB = st.s.getDB('config');
var chunkCountBefore = configDB.chunks.count();
assert.gt(chunkCountBefore, 1);

for (var x = 0; x < 1000; x++) {
    testDB.user.insert({x: x});
}

// For debugging
(function() {
    var chunkList = configDB.chunks.find().sort({min: -1}).toArray();
    chunkList.forEach(function(chunk) {
        chunk.count = 0;
    });

    for (var x = 0; x < 1000; x++) {
        var hashVal = testDB.adminCommand({_hashBSONElement: x}).out;
        var countSet = false;

        for (var y = 0; y < chunkList.length - 2; y++) {
            var chunkDoc = chunkList[y];
            if (chunkDoc.min.x <= hashVal) {
                countSet = true;
                chunkDoc.count++;

                print('doc in chunk: x [' + x + '], h[' + hashVal + '], min[' + chunkDoc.min.x +
                      '], max[' + chunkDoc.max.x + ']');
                break;
            }
        }

        if (!countSet) {
            chunkDoc = chunkList[chunkList.length - 1];
            print('doc in chunk: x [' + x + '], h[' + hashVal + '], min[' + chunkDoc.min.x +
                  '], max[' + chunkDoc.max.x + ']');
            chunkDoc.count++;
        }
    }

    chunkList.forEach(function(chunkDoc) {
        print('chunk details: ' + tojson(chunkDoc));
    });
});

var chunkDoc = configDB.chunks.find().sort({min: 1}).next();
var min = chunkDoc.min;
var max = chunkDoc.max;

// Assumption: There are documents in the MinKey chunk, otherwise, splitVector will
// fail. Note: This chunk will have 267 documents if collection was presplit to 4.
var cmdRes = testDB.adminCommand({split: 'test.user', bounds: [min, max]});
assert(cmdRes.ok, 'split on bounds failed on chunk[' + tojson(chunkDoc) + ']: ' + tojson(cmdRes));

chunkDoc = configDB.chunks.find().sort({min: 1}).skip(1).next();
var middle = chunkDoc.min + 1000000;

cmdRes = testDB.adminCommand({split: 'test.user', middle: {x: middle}});
assert(cmdRes.ok, 'split failed with middle [' + middle + ']: ' + tojson(cmdRes));

cmdRes = testDB.adminCommand({split: 'test.user', find: {x: 7}});
assert(cmdRes.ok, 'split failed with find: ' + tojson(cmdRes));

var chunkList = configDB.chunks.find().sort({min: 1}).toArray();
assert.eq(chunkCountBefore + 3, chunkList.length);

chunkList.forEach(function(chunkToMove) {
    var toShard = configDB.shards.findOne({_id: {$ne: chunkToMove.shard}})._id;

    print(jsTestName() + " - moving chunk " + chunkToMove._id + " from shard " + chunkToMove.shard +
          " to " + toShard + "...");

    var cmdRes = testDB.adminCommand({
        moveChunk: 'test.user',
        bounds: [chunkToMove.min, chunkToMove.max],
        to: toShard,
        _waitForDelete: true
    });
    print(jsTestName() + " - result from moving chunk " + chunkToMove._id + ": " + tojson(cmdRes));
});

st.stop();