elasticsearch-shard

edit

在某些情况下,分片副本的Lucene索引或translog可能会损坏。 elasticsearch-shard 命令使您能够移除分片中损坏的部分,如果无法自动恢复或从备份中恢复分片的好副本。

当你运行elasticsearch-shard时,你将会丢失损坏的数据。这个工具应该仅在无法从另一个分片副本或恢复快照中恢复时,作为最后的手段使用。

概要

edit
bin/elasticsearch-shard remove-corrupted-data
  ([--index <Index>] [--shard-id <ShardId>] | [--dir <IndexPath>])
  [--truncate-clean-translog]
  [-E <KeyValuePair>]
  [-h, --help] ([-s, --silent] | [-v, --verbose])

描述

edit

当Elasticsearch检测到分片数据损坏时,它会失败该分片副本并拒绝使用它。在正常情况下,分片会自动从另一个副本中恢复。如果没有可用的良好分片副本,并且您无法从快照中恢复一个,您可以使用elasticsearch-shard移除损坏的数据并恢复对未受影响段中剩余数据的访问。

在运行 elasticsearch-shard 之前停止 Elasticsearch。

要删除损坏的分片数据,请使用 remove-corrupted-data 子命令。

有两种方式来指定路径:

  • 使用--index--shard-id选项指定索引名称和分片名称。
  • 使用--dir选项指定损坏的索引或事务日志文件的完整路径。

JVM 选项

edit

CLI 工具以 64MB 的堆内存运行。对于大多数工具来说,这个值是合适的。然而,如果需要,可以通过设置 CLI_JAVA_OPTS 环境变量来覆盖此值。例如,以下命令将 elasticsearch-shard 工具使用的堆内存大小增加到 1GB。

export CLI_JAVA_OPTS="-Xmx1g"
bin/elasticsearch-shard ...

移除损坏的数据

edit

elasticsearch-shard 分析分片副本并提供发现的损坏概览。要继续,您必须确认您想要删除损坏的数据。

在运行 elasticsearch-shard 之前备份您的数据。这是一个破坏性操作,会从分片中移除损坏的数据。

$ bin/elasticsearch-shard remove-corrupted-data --index my-index-000001 --shard-id 0


    WARNING: Elasticsearch MUST be stopped before running this tool.

  Please make a complete backup of your index before using this tool.


Opening Lucene index at /var/lib/elasticsearchdata/indices/P45vf_YQRhqjfwLMUvSqDw/0/index/

 >> Lucene index is corrupted at /var/lib/elasticsearchdata/indices/P45vf_YQRhqjfwLMUvSqDw/0/index/

Opening translog at /var/lib/elasticsearchdata/indices/P45vf_YQRhqjfwLMUvSqDw/0/translog/


 >> Translog is clean at /var/lib/elasticsearchdata/indices/P45vf_YQRhqjfwLMUvSqDw/0/translog/


  Corrupted Lucene index segments found - 32 documents will be lost.

            WARNING:              YOU WILL LOSE DATA.

Continue and remove docs from the index ? Y

WARNING: 1 broken segments (containing 32 documents) detected
Took 0.056 sec total.
Writing...
OK
Wrote new segments file "segments_c"
Marking index with the new history uuid : 0pIBd9VTSOeMfzYT6p0AsA
Changing allocation id V8QXk-QXSZinZMT-NvEq4w to tjm9Ve6uTBewVFAlfUMWjA

You should run the following command to allocate this shard:

POST /_cluster/reroute
{
  "commands" : [
    {
      "allocate_stale_primary" : {
        "index" : "index42",
        "shard" : 0,
        "node" : "II47uXW2QvqzHBnMcl2o_Q",
        "accept_data_loss" : false
      }
    }
  ]
}

You must accept the possibility of data loss by changing the `accept_data_loss` parameter to `true`.

Deleted corrupt marker corrupted_FzTSBSuxT7i3Tls_TgwEag from /var/lib/elasticsearchdata/indices/P45vf_YQRhqjfwLMUvSqDw/0/index/

当您使用 elasticsearch-shard 删除损坏的数据时,分片的分配 ID 会发生变化。在重新启动节点后,您必须使用 cluster reroute API 来告诉 Elasticsearch 使用新的 ID。elasticsearch-shard 命令会显示您需要提交的请求。

您还可以使用 -h 选项来获取 elasticsearch-shard 工具支持的所有选项和参数的列表。

最后,您可以使用 --truncate-clean-translog 选项来截断分片的 translog,即使它看起来没有损坏。