-
Notifications
You must be signed in to change notification settings - Fork 1
/
newShardsChunks.js
86 lines (68 loc) · 2.17 KB
/
newShardsChunks.js
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
db=db.getSiblingDB("objStore")
sh.setBalancerState(false);
print("WARNING: This must either be run when no writes are")
print("happening OR set to a suitably large time after the ")
print("last write date!")
nshards = 3
lowestnewshard = 3
totalprefixes=100
pfxpershard = totalprefixes/nshards
startwhen = 120 // In 2 minutes - so we see 'most move over
//Generator puts a 5 minute gap between runs anyway
nshardkeys = 100
print("Checking for newest record in the Database")
//Verify we do not have any chunks with higher ID's
maxid = ObjectId()
for(x=0;x<nshardkeys;x++){
print("Shardkey "+x+" of "+nshardkeys)
query = {_id:{$lt:{s:x+1}}}
sort = {_id:-1}
c = db.blobs.find(query,{_id:1}).sort(sort).limit(1).next()
id = c['_id']['i']
if(id > maxid)
{
printjson(id.getTimestamp())
maxid = id
}
c = db.references.find(query,{_id:1}).sort(sort).limit(1).next()
id = c['_id']['i']
if(id > maxid)
{
printjson(id.getTimestamp())
maxid = id
}
}
printjson(maxid)
print(maxid.getTimestamp())
var idAsString = maxid.valueOf()
var timepartAsString = idAsString.substring(0,8)
var timepartAsInt = parseInt(timepartAsString,16)
timepartAsInt=timepartAsInt+startwhen
newid = timepartAsInt.toString(16)+idAsString.substring(8,24)
newObjectId = ObjectId(newid)
print("Splitting from : ")
printjson(newObjectId)
print(newObjectId.getTimestamp())
print("Moving Chunks")
for(x=0;x<totalprefixes;x++){
split = {_id:{s:x,i:newObjectId}};
shard = "shard_"+(Math.floor(x/pfxpershard)+lowestnewshard);
print ("Moving " + tojson(split) + " to " + shard);
print("Checking it's empty")
query = {_id:{$gt:{s:x,i:newObjectId},$lt:{s:x+1}}}
blobcount = db.getSiblingDB('objStore').blobs.count(query)
refcount = db.getSiblingDB('objStore').references.count(query)
if(blobcount > 0 || refcount>0)
{
print("Error - moving a non empty chunk ")
printjson(query)
}
r = sh.splitAt("objStore.blobs",split);
if(r['ok'] != 1) {printjson(r)}
r=sh.moveChunk("objStore.blobs",split,shard)
if(r['ok'] != 1){ printjson(r)}
r=sh.splitAt("objStore.references",split);
if(r['ok'] != 1){ printjson(r)}
r=sh.moveChunk("objStore.references",split,shard)
if(r['ok'] != 1) {printjson(r)}
}