MongoDB mobile chunks script

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.