企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Cluster Reroute 原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/5.0/cluster-reroute.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/cluster-reroute.html) 译文链接 : [http://www.apache.wiki/display/Elasticsearch/Cluster+Reroute](http://www.apache.wiki/display/Elasticsearch/Cluster+Reroute) 贡献者 : [那伊抹微笑](/display/~wangyangting) **reroute** 命令可以明确的执行包括指定命令的集群重新路由分配命令。例如,一个分片可以从一个节点明确的移动到另一个节点,分配可以被取消,或者未分配的分片可能被明确的分配到一个指定的节点。 下面是一个见简单的重新路由 **API** 调用的例子 :  ``` curl -XPOST 'localhost:9200/_cluster/reroute' -d '{ "commands" : [ { "move" : { "index" : "test", "shard" : 0, "from_node" : "node1", "to_node" : "node2" } }, { "allocate_replica" : { "index" : "test", "shard" : 1, "node" : "node3" } } ] }' ``` 要记住的一个重要方面是, 在分配时,集群将针对 **re-balancing**(重新平衡)它的状态到一个相等的状态。例如,如果分配包括从 **node1** 移动分片到 **node2**,在一个相等的状态中,其它的分片将会从 **node2** 移动到 **node1** 以达到平衡。 即可可以设置禁用分配,这意味着只有在明确的指定分配情况下才被执行。很显然,仅仅一次所有的命令被请求时,该集群将重新平衡它的状态。 另一种选择是在 **dry_run**(作为一个 **URI** 标记,或者在请求体中)中运行命令。这将导致命令应用到当前的集群状态,并且在命令别被执行后返回集群结果。 如果指定了 **explain** 参数,会返回命令可以或者不能被执行的详细的说明。 支持的命令是 :  | 命令 | 描述 | | --- | --- | | move | Move a started shard from one node to another node. Accepts `index` and `shard` for index name and shard number, `from_node` for the node to move the shard `from`, and `to_node` for the node to move the shard to. | | cancel | Cancel allocation of a shard (or recovery). Accepts `index` and `shard` for index name and shard number, and `node` for the node to cancel the shard allocation on. It also accepts `allow_primary` flag to explicitly specify that it is allowed to cancel allocation for a primary shard. This can be used to force resynchronization of existing replicas from the primary shard by cancelling them and allowing them to be reinitialized through the standard reallocation process. | | allocate_replica | Allocate an unassigned replica shard to a node. Accepts the `index` and `shard` for index name and shard number, and `node` to allocate the shard to. Takes [allocation deciders](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/modules-cluster.html "Cluster") into account. | 两个更多的命令可用,允许分配主分片到节点。这些命令需要谨慎使用,因为主分片的分配通常有 **Elasticsearch** 自动处理。为什么主分片不能被自动分配的原因如下 :  * 一个新的索引被创建,但是没有满足分配策略者的节点。 * 一个最新的分片数据的副本,不能在集群中当前数据节点上找到。为了防止数据丢失,系统不会自动的促使一个稳定的分片复制为主分片。 在手动覆盖时,两个命令强制分配主分片可供选择 :  | 命令 | 描述 | | --- | --- | | allocate_stale_primary | Allocate a primary shard to a node that holds a stale copy. Accepts the `index` and `shard` for index name and shard number, and `node` to allocate the shard to. Using this command may lead to data loss for the provided shard id. If a node which has the good copy of the data rejoins the cluster later on, that data will be overwritten with the data of the stale copy that was forcefully allocated with this command. To ensure that these implications are well-understood, this command requires the special field `accept_data_loss` to be explicitly set to `true` for it to work. | | allocate_empty_primary | Allocate an empty primary shard to a node. Accepts the `index` and `shard` for index name and shard number, and `node` to allocate the shard to. Using this command leads to a complete loss of all data that was indexed into this shard, if it was previously started. If a node which has a copy of the data rejoins the cluster later on, that data will be deleted! To ensure that these implications are well-understood, this command requires the special field `accept_data_loss` to be explicitly set to `true` for it to work. | ## Retry failed shards(重试故障的分片) 集群尝试分配一个分片的最大次数为 **index.allocation.max_retries**(默认为 **5**),在放弃和离开未分配的分片之前。该场景会造成结构上的问题,例如有一个分析器,它引用了一个在所有节点上不存在的 **stopwords** 文件。 在该问题被修复后,可以通过手动重复与 **?retry_failed** 一起调用 [`_reroute`](https://www.elastic.co/guide/en/elasticsearch/reference/5.0/cluster-reroute.html "Cluster Reroute") API,它将为那些节点单独的重试。