有些时候有于硬件资源有限,所以我们不得不进行一些回收工作,下面我们就要将刚刚启用的Shard Server 回收,系统首先会将在这个即将被移除的Shard Server 上的数据先平均分配到其它的Shard Server 上,然后最终在将这个Shard Server 踢下线, 我们需要不停的调用db.runCommand({"removeshard" :"localhost:20002"});来观察这个移除操作进行到哪里了:
> use admin switched to db admin > db.runCommand({"removeshard" : "localhost:20002"}); { "msg" : "draining started successfully", "state" : "started", "shard" : "shard0002", "ok" : 1 } > db.runCommand({"removeshard" : "localhost:20002"}); { "msg" : "draining ongoing", "state" : "ongoing", "remaining" : { "chunks" : NumberLong(44), "dbs" : NumberLong(0) }, "ok" : 1 } …… > db.runCommand({"removeshard" : "localhost:20002"}); { "msg" : "draining ongoing", "state" : "ongoing", "remaining" : { "chunks" : NumberLong(1), "dbs" : NumberLong(0) }, "ok" : 1 } > db.runCommand({"removeshard" : "localhost:20002"}); { "msg" : "removeshard completed successfully", "state" : "completed", "shard" : "shard0002", "ok" : 1 } > db.runCommand({"removeshard" : "localhost:20002"}); { "assertion" : "can‘t find shard for: localhost:20002", "assertionCode" : 13129, "errmsg" : "db assertion failure", "ok" : 0 }
最终移除后,当我们再次调用db.runCommand({"removeshard" : "localhost:20002"});的时候系统会报错,已便通知我们不存在20002 这个端口的Shard Server 了,因为它已经被移除掉了。
> use test switched to db test > db.users_2.stats() { "sharded" : true, "ns" : "test.users_2", "count" : 500000, "size" : 48000000, "avgObjSize" : 96, "storageSize" : 95203584, "nindexes" : 1, "nchunks" : 92, "shards" : { "shard0000" : { "ns" : "test.users_2", "count" : 248749, "size" : 23879904, "avgObjSize" : 96, "storageSize" : 61875968, "numExtents" : 11, "nindexes" : 1, "lastExtentSize" : 15001856, "paddingFactor" : 1, "flags" : 1, "totalIndexSize" : 13033472, "indexSizes" : { "_id_" : 13033472 }, "ok" : 1 }, "shard0001" : { "ns" : "test.users_2", "count" : 251251, "size" : 24120096, "avgObjSize" : 96, "storageSize" : 33327616, "numExtents" : 8, "nindexes" : 1, "lastExtentSize" : 12079360, "paddingFactor" : 1, "flags" : 1, "totalIndexSize" : 10469376, "indexSizes" : { "_id_" : 10469376 }, "ok" : 1 } }, "ok" : 1 }
可以看出数据又被平均分配到了另外2 台Shard Server 上了!
时间: 2024-10-10 11:17:42