Saturday, August 24, 2013

Manually editing Solr's clusterstate.json on Zookeeper

There will probably come a time when you want to do something not readily covered in Solr's APIs.  Manually editing the clusterstate.json is easy to do but should be approached with caution.

I wanted to drop all shards from a single host from my Solr cluster (didn't care about losing the data) and do a little spring cleaning.  To open a command-line interface with ZK:

/path/to/zookeeper/bin/zkCli.sh -server 127.0.0.1:2181

Then simply:

get /clusterstate.json

Place that content into a local file.  After backing up the original content, make your edits.  For instance, to drop the dead nodes, delete JSON elements with "down" states:

"collection":{
"shards":{"shard1":{
"range":null,
"state":"active",
"replicas":{
"XXX.XX.X.XX:8983_solr_collection_shard1_replica1":{
"state":"down",
"core":"collection_shard1_replica1",
"node_name":"XXX.XX.X.XX:8983_solr",
"base_url":"http://XXX.XX.X.XX:8983/solr"},
"XXX.XX.X.XX:8983_solr_collection_shard1_replica1":{
"state":"active",
"core":"collection_shard1_replica1",
"node_name":"XXX.XX.X.XX:8983_solr",
"base_url":"http://XXX.XX.X.XX:8983/solr",
"leader":"true"}}}},
view raw gistfile1.txt hosted with ❤ by GitHub

To upload your new clusterstate.json (no need to halt services):

/path/to/zookeeper/bin/zkCli.sh -server 127.0.0.1:2181 set /clusterstate.json "`cat /local/path/to/clusterstate.json`"