-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
merge-chunks-in-sharded-cluster.txt
211 lines (171 loc) · 9.37 KB
/
merge-chunks-in-sharded-cluster.txt
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
=================================
Merge Chunks in a Sharded Cluster
=================================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Overview
--------
.. versionchanged:: 3.0
The :dbcommand:`mergeChunks` command allows you to combine continuous
chunks on the same shard into a single chunk. This tutorial explains
how to merge neighboring chunks in a sharded cluster.
Procedure
---------
.. note::
Examples in this procedure use a ``members`` :term:`collection` in the
``test`` :term:`database`, using the ``username`` field as the
:term:`shard key`.
Identify Chunk Ranges
~~~~~~~~~~~~~~~~~~~~~
In the :binary:`~bin.mongo` shell, identify the :term:`chunk`
ranges with the following operation:
.. code-block:: javascript
sh.status()
In the output, the chunk ranges appear after the chunk counts for each
sharded collection, as in the following example:
.. code-block:: none
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5c1152e27510bed491e81840")
}
shards:
{ "_id" : "shardA", "host" : "shardA/shardA-m1.example.net:27018,shardA-m2.example.net:27018,shardA-m3.example.net:27018", "state" : 1 }
{ "_id" : "shardB", "host" : "shardB/shardB-m1.example.net:27018,shardB-m2.example.net:27018,shardB-m3.example.net:27018", "state" : 1 }
active mongoses:
"3.4.17" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
NaN
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
8 : Success
databases:
{ "_id" : "test", "primary" : "shardA", "partitioned" : true }
test.members
shard key: { "username" : 1 }
unique: false
balancing: true
chunks:
shardA 7
shardB 7
{ "username" : { "$minKey" : 1 } } -->> { "username" : "user16643" } on : shardB Timestamp(2, 0)
{ "username" : "user16643" } -->> { "username" : "user2329" } on : shardB Timestamp(3, 0)
{ "username" : "user2329" } -->> { "username" : "user29937" } on : shardB Timestamp(4, 0)
{ "username" : "user29937" } -->> { "username" : "user36583" } on : shardB Timestamp(5, 0)
{ "username" : "user36583" } -->> { "username" : "user43229" } on : shardB Timestamp(6, 0)
{ "username" : "user43229" } -->> { "username" : "user49877" } on : shardB Timestamp(7, 0)
{ "username" : "user49877" } -->> { "username" : "user56522" } on : shardB Timestamp(8, 0)
{ "username" : "user56522" } -->> { "username" : "user63169" } on : shardA Timestamp(8, 1)
{ "username" : "user63169" } -->> { "username" : "user69816" } on : shardA Timestamp(1, 17)
{ "username" : "user69816" } -->> { "username" : "user76462" } on : shardA Timestamp(1, 19)
{ "username" : "user76462" } -->> { "username" : "user83108" } on : shardA Timestamp(1, 21)
{ "username" : "user83108" } -->> { "username" : "user89756" } on : shardA Timestamp(1, 23)
{ "username" : "user89756" } -->> { "username" : "user96401" } on : shardA Timestamp(5, 2)
{ "username" : "user96401" } -->> { "username" : { "$maxKey" : 1 } } on : shardA Timestamp(5, 3)
The chunk ranges appear after the chunk counts for each sharded
collection. For example, the following are the chunk ranges for the
``test.members`` collection:
.. code-block:: none
{ "username" : { "$minKey" : 1 } } -->> { "username" : "user16643" } on : shardB Timestamp(2, 0)
{ "username" : "user16643" } -->> { "username" : "user2329" } on : shardB Timestamp(3, 0)
{ "username" : "user2329" } -->> { "username" : "user29937" } on : shardB Timestamp(4, 0)
{ "username" : "user29937" } -->> { "username" : "user36583" } on : shardB Timestamp(5, 0)
{ "username" : "user36583" } -->> { "username" : "user43229" } on : shardB Timestamp(6, 0)
{ "username" : "user43229" } -->> { "username" : "user49877" } on : shardB Timestamp(7, 0)
{ "username" : "user49877" } -->> { "username" : "user56522" } on : shardB Timestamp(8, 0)
{ "username" : "user56522" } -->> { "username" : "user63169" } on : shardA Timestamp(8, 1)
{ "username" : "user63169" } -->> { "username" : "user69816" } on : shardA Timestamp(1, 17)
{ "username" : "user69816" } -->> { "username" : "user76462" } on : shardA Timestamp(1, 19)
{ "username" : "user76462" } -->> { "username" : "user83108" } on : shardA Timestamp(1, 21)
{ "username" : "user83108" } -->> { "username" : "user89756" } on : shardA Timestamp(1, 23)
{ "username" : "user89756" } -->> { "username" : "user96401" } on : shardA Timestamp(5, 2)
{ "username" : "user96401" } -->> { "username" : { "$maxKey" : 1 } } on : shardA Timestamp(5, 3)
Merge Chunks
~~~~~~~~~~~~
Merge contiguous :term:`chunks <chunk>` on the same shard.
For example, consider the following chunk ranges on ``shardA``:
.. note::
The chunks to be merged are highlighted.
.. code-block:: javascript
:emphasize-lines: 3-6
{ "username" : "user56522" } -->> { "username" : "user63169" } on : shardA Timestamp(8, 1)
{ "username" : "user63169" } -->> { "username" : "user69816" } on : shardA Timestamp(1, 17)
{ "username" : "user69816" } -->> { "username" : "user76462" } on : shardA Timestamp(1, 19)
{ "username" : "user76462" } -->> { "username" : "user83108" } on : shardA Timestamp(1, 21)
{ "username" : "user83108" } -->> { "username" : "user89756" } on : shardA Timestamp(1, 23)
{ "username" : "user89756" } -->> { "username" : "user96401" } on : shardA Timestamp(5, 2)
{ "username" : "user96401" } -->> { "username" : { "$maxKey" : 1 } } on : shardA Timestamp(5, 3)
To merge the highlighted contiguous chunks, issue the
:dbcommand:`mergeChunks` command against the ``admin`` database:
.. code-block:: javascript
db.adminCommand( {
mergeChunks: "test.members",
bounds: [ { "username" : "user69816" },
{ "username" : "user96401" } ]
} )
On success, :dbcommand:`mergeChunks` produces the following output:
.. code-block:: javascript
{ "ok" : 1 }
On any failure condition, :dbcommand:`mergeChunks` returns a document
where the value of the ``ok`` field is ``0``.
View Merged Chunks Ranges
~~~~~~~~~~~~~~~~~~~~~~~~~
After merging the identified chunks, confirm the new chunk, as follows:
.. code-block:: javascript
sh.status()
The output of :method:`sh.status()` should resemble:
.. code-block:: none
:emphasize-lines: 40
--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("5c1152e27510bed491e81840")
}
shards:
{ "_id" : "shardA", "host" : "shardA/shardA-m1.example.net:27018,shardA-m2.example.net:27018,shardA-m3.example.net:27018", "state" : 1 }
{ "_id" : "shardB", "host" : "shardB/shardB-m1.example.net:27018,shardB-m2.example.net:27018,shardB-m3.example.net:27018", "state" : 1 }
active mongoses:
"3.4.17" : 1
autosplit:
Currently enabled: yes
balancer:
Currently enabled: yes
Currently running: no
NaN
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
9 : Success
databases:
{ "_id" : "test", "primary" : "shardA", "partitioned" : true }
test.members
shard key: { "username" : 1 }
unique: false
balancing: true
chunks:
shardA 5
shardB 6
{ "username" : { "$minKey" : 1 } } -->> { "username" : "user16643" } on : shardA Timestamp(9, 0)
{ "username" : "user16643" } -->> { "username" : "user2329" } on : shardB Timestamp(9, 1)
{ "username" : "user2329" } -->> { "username" : "user29937" } on : shardB Timestamp(4, 0)
{ "username" : "user29937" } -->> { "username" : "user36583" } on : shardB Timestamp(5, 0)
{ "username" : "user36583" } -->> { "username" : "user43229" } on : shardB Timestamp(6, 0)
{ "username" : "user43229" } -->> { "username" : "user49877" } on : shardB Timestamp(7, 0)
{ "username" : "user49877" } -->> { "username" : "user56522" } on : shardB Timestamp(8, 0)
{ "username" : "user56522" } -->> { "username" : "user63169" } on : shardA Timestamp(8, 1)
{ "username" : "user63169" } -->> { "username" : "user69816" } on : shardA Timestamp(1, 17)
{ "username" : "user69816" } -->> { "username" : "user96401" } on : shardA Timestamp(8, 2)
{ "username" : "user96401" } -->> { "username" : { "$maxKey" : 1 } } on : shardA Timestamp(5, 3)
After the merge, the :ref:`balancer <sharding-balancing>` may migrate
chunks across shards to ensure a more even distribution of chunks.