MongoDB mobile chunks script
As the data volume increases, you may need to add shards. After adding shards, you need to move some of the data to the new shards. Originally, MongoDB supports automatic balancing (balancer, that is, automatically sharding and moving data to the shard based on the data of each shard). However, automatic balancing requires a large amount of system resources, in addition, it is easy to cause program freezing, so in actual applications, we will automatically turn off the balance.
The problem we encountered was. The original two shards are added due to the increase in data volume. In the original two shards, each shard has hundreds of chunks, and a single website (for example, Tudou) has nearly 200 chunks.
Problem: the automatic balancing has been stopped.
Manual movement, too much data, time-consuming and labor-consuming.
Method: Use a single website to move the script.
VaR namespace = "Tudou. Video ";
VaR cursor = dB. Chunks. Find ({ns: namespace });
VaR S1 = 0, S2 = 0, S3 = 0;
VaR allcnt = cursor. Count ();
VaR percnt = parseint (allcnt/3 );
Print ("all-count:" + allcnt + "per-count:" + percnt );
While (cursor. hasnext ()){
VaR chunk = cursor. Next ();
If (Chunk. Shard = "S1 "){
S1 ++;
Print ("S1" + Chunk. _ id );
If (S1> percnt & S3 <percnt ){
Print ("S1" + S1 + "movechunk" + Chunk. Min. md5id );
// Printjson (Chunk. min );
VaR ret = dB. admincommand ({movechunk: namespace, find: Chunk. Min, to: "S3 "});
Printjson (RET );
If (Ret. OK = 1 ){
S3 ++;
S1 --;
}
}
} Else if (Chunk. Shard = "S2 "){
S2 ++;
Print ("S2" + Chunk. _ id );
If (s2> percnt & S3 <percnt ){
Print ("S2" + S2 + "movechunk" + Chunk. Min. md5id );
VaR ret = dB. admincommand ({movechunk: namespace, find: Chunk. Min, to: "S3 "});
Printjson (RET );
If (Ret. OK = 1 ){
S3 ++;
S2 --;
}
}
} Else if (Chunk. Shard = "S3 "){
Print ("S3" + Chunk. _ id );
S3 ++;
}
}
Another problem: whether it is manual or script (in fact, it is manual, which simplifies the change ). The program may be stuck, it may be a part of a machine, or it may cause the routing mongos to be stuck.