Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a specific shard of an ElasticSearch index

I recently had a SNAFU cause my cluster to end up with split-brain (despite having many controls in place) resulting in shards that are basically busted. I've got all the nodes back in play properly, recognizing the right master, etc. but the cluster remains red and rightfully so; there are a few shards that have no home.

After using my RubberBand script, I was able to explore using VisualJSON to find shards like the following one, that have no node:

{
    "index": "logstash-2013.12.27",
    "node": null,
    "primary": false,
    "relocating_node": null,
    "shard": 4,
    "state": "UNASSIGNED"
},

I would like to delete them but I can't seem to find an API call to delete a shard, only deleting whole indices or using queries. Thanks in advance!

like image 291
Spanky Avatar asked Jan 09 '14 04:01

Spanky


2 Answers

This command will take an orphaned shard and assign it to node efsKb4DzQ2iaIfKfu36vsA.

curl -XPOST 'localhost:9200/_cluster/reroute' -d '{
  "commands": [
    {
      "allocate": {
        "index": "tweedle-2013.12.21",
        "shard": 3,
        "node": "efsKb4DzQ2iaIfKfu36vsA",
        "allow_primary": true
      }
    }
  ]
}'
like image 62
Spanky Avatar answered Oct 04 '22 00:10

Spanky


You can't delete an unassigned shard because there is no shard to be deleted. An unassigned shard is not a corrupted shard, but a missing replica.

Your config probably tells ES (ElasticSearch) to create replicas and assign them on different nodes for high availability and/or fault tolerance. ES was not able to automatically create and assign a replica and, thus, you see the UNASSIGNED state. It could have been due to a network error, memory not available, etc.

You may want to find the reason why the allocation failed:

curl -XPOST 'localhost:9200/_cluster/allocation/explain?pretty'

And, then, ask ES to retry the allocation for you:

curl -XPOST 'localhost:9200/_cluster/reroute?retry_failed'

Credits to ES's expert answer which says

After 5 unsuccessful allocation attempts, the master gives up and needs manual triggering to give it another allocation attempt

like image 28
Ricardo Avatar answered Oct 03 '22 23:10

Ricardo