-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmerge-chunks.js
142 lines (122 loc) · 4.03 KB
/
merge-chunks.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
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
use config
// dbName: Set this to the database name
// collection: Set this to the collection you want to merge chunks in
// keyPattern: Set this to the key pattern, ie { "_id" : "hashed" }
// sleepValue: How long to sleep between each chunk (for backoff)
mergeChunks = function(dbName, collection, keyPattern, sleepValue) {
ns = dbName + "." + collection
checkChunkForMerge = function(chunk) {
hasDocs = chunkHasDocs(chunk)
if(hasDocs) {
// Not an empty chunk
print("Count is non-zero!")
return
}
print("Running datasize")
dataSize = db.getSiblingDB(dbName).runCommand({
"dataSize": chunk.ns,
"keyPattern": keyPattern,
"min": chunk.min,
"max": chunk.max
})
print("DataSize of " + dataSize.size + " and took " + dataSize.millis + "ms")
// Found an empty chunk
if(dataSize.size != 0) {
// Not an empty chunk
return
}
// Lets try and find a colocated chunk to merge with
mergableRange = null
prevChunk = db.chunks.findOne({ns: ns, "max": chunk.min})
nextChunk = null
if(prevChunk && prevChunk.shard == chunk.shard) {
mergableRange = [prevChunk.min, chunk.max]
} else {
nextChunk = db.chunks.findOne({ns: ns, "min": chunk.max})
if(nextChunk && nextChunk.shard == chunk.shard) {
mergableRange = [chunk.min, nextChunk.max]
}
}
// If we cant we have to move the empty chunk
if(mergableRange == null) {
dest = null
if(prevChunk) {
dest = prevChunk.shard;
mergableRange = [prevChunk.min, chunk.max]
} else if(nextChunk) {
dest = nextChunk.shard;
mergableRange = [chunk.min, nextChunk.max]
} else {
print("Unable to move empty chunk to mergeable range")
return
}
print("Moving chunk to " + dest)
printjson(chunk)
db.getSiblingDB('admin').runCommand({
moveChunk : chunk.ns,
bounds : [chunk.min, chunk.max],
to : dest,
_secondaryThrottle : true,
_waitForDelete : true
})
chunk.shard = dest
}
// Lets try the merge
if(mergableRange) {
print("Merging a chunk on " + chunk.shard)
db.getSiblingDB('admin').runCommand({
mergeChunks: chunk.ns,
bounds: mergableRange
})
} else {
print("Unable to merge with another chunk!")
}
}
chunkHasDocs = function(chunk) {
query = {}
shardKeys = Object.keys(keyPattern)
if (shardKeys.length > 1) {
firstKey = shardKeys[0]
secondKey = shardKeys[1]
if (chunk.min[firstKey].toString() == chunk.max[firstKey].toString()) {
query[firstKey] = chunk.min[firstKey]
query[secondKey] = {$gte: chunk.min[secondKey], $lt: chunk.max[secondKey]}
} else {
lowerRange = {}
lowerRange[firstKey] = chunk.min[firstKey]
lowerRange[secondKey] = {$gte: chunk.min[secondKey]}
midRange = {}
midRange[firstKey] = {$gt: chunk.min[firstKey] , $lt: chunk.max[firstKey]}
upperRange = {}
upperRange[firstKey] = chunk.max[firstKey]
upperRange[secondKey] = {$lt: chunk.max[secondKey]}
query.$or = [
lowerRange,
midRange,
upperRange
]
}
} else {
shardKey = shardKeys[0]
// If its hashed then we cant do the count :-(
if (keyPattern[shardKey] == "hashed") return 0
query[shardKey] = {$gte: chunk.min[shardKey], $lt: chunk.max[shardKey]}
}
print("Running count")
printjson(query)
return db.getSiblingDB(dbName)[collection].find(query, {"_id": 1}).readPref("secondary").limit(1).maxTimeMS(10000).hasNext()
}
print("Loading cursor to iterate through chunks...")
cursor = db.chunks.find({ns: ns}).sort({"max": 1}).addOption(DBQuery.Option.noTimeout)
print("Done!")
docsProcessed = 0
cursor.forEach(function(chunk){
checkChunkForMerge(chunk)
docsProcessed = docsProcessed + 1
print("Processed " + docsProcessed + " documents!")
if(sleepValue) {
print("Sleeping...")
sleep(sleepValue)
}
})
}