Sometimes, there is a need of removing one or more replicas from your Solr cluster. Either because you want to get rid of some nodes and you want your cluster state to become clean or you want to move some shard to another node and delete the original shard. No matter what the case is, there might come a time where you need to delete a shard. This recipe will show you how to do it.
Before reading further, I advise you to read the Creating a new SolrCloud cluster and Having more than a single shard from a collection on a node recipes in this chapter. These recipes will show you how to create a new SolrCloud cluster and create a collection. We also assume that ZooKeeper is running on 192.168.1.10
and is listening on port 2181
. We already have the configuration called firstcollection
stored in ZooKeeper and we already have four SolrCloud nodes running as a cluster.
curl 'localhost:8983/solr/admin/collections?action=CREATE&name=testCollection&numShards=2&replicationFactor=2&collection.configName=firstcollection'
After the collection is created, we should see the following cluster view when we take a look at http://localhost:8983/solr/#/~cloud
:
Now, let's assume that we want to get rid of two servers in our cluster, 192.168.56.1:8983
and 192.168.56.1:6983
. And we don't want to leave any traces in the cluster state. To do this, we need to remove the replicas that are placed on those nodes.
192.168.56.1:8983_solr
replica by running the following command:curl 'http://localhost:8983/solr/admin/collections?action=DELETEREPLICA&collection=testCollection&shard=shard1&replica=core_node2'
curl 'http://localhost:8983/solr/admin/collections?action=DELETEREPLICA&collection=testCollection&shard=shard2&replica=core_node4'
And again let's take a look at the cluster view:
As we can see, everything worked.
We start by creating a collection that we will use to demonstrate how to remove replicas from the already created collection. Of course, we have our SolrCloud cluster up and running, ZooKeeper installed and running, and the firstcollection
configuration stored in it. The collection creation
command was already mentioned a few times—in our case, we created a collection called testCollection
that is built of four physical shards—two primary shards and one replica for each of them.
What we try to do is delete the replicas of the primary shards, because we want to remove the server on which they are running and throw the hardware away. To do this, we start by removing the replica that is stored on the node running on port 8983
. The thing is that this time we don't specify the node name like during replica creation, but we need to specify the actual shard name that Solr uses internally. We can do this by taking a look at clusterstate.json
in the Solr admin panel (you can do that by going to http://localhost:8983/solr/#/~cloud?view=tree
with your web browser and choosing the mentioned file). In our case, that file looks as follows:
{"testCollection":{ "shards":{ "shard1":{ "range":"80000000-ffffffff", "state":"active", "replicas":{ "core_node1":{ "state":"active", "core":"testCollection_shard1_replica1", "node_name":"192.168.56.1:4983_solr", "base_url":"http://192.168.56.1:4983/solr", "leader":"true"}, "core_node2":{ "state":"active", "core":"testCollection_shard1_replica2", "node_name":"192.168.56.1:8983_solr", "base_url":"http://192.168.56.1:8983/solr"}}}, "shard2":{ "range":"0-7fffffff", "state":"active", "replicas":{ "core_node3":{ "state":"active", "core":"testCollection_shard2_replica1", "node_name":"192.168.56.1:2983_solr", "base_url":"http://192.168.56.1:2983/solr", "leader":"true"}, "core_node4":{ "state":"active", "core":"testCollection_shard2_replica2", "node_name":"192.168.56.1:6983_solr", "base_url":"http://192.168.56.1:6983/solr"}}}}, "maxShardsPerNode":"1", "router":{"name":"compositeId"}, "replicationFactor":"2", "autoAddReplicas":"false"}}
For example, for shard1
, we have two replicas, one named core_node1
and the second one named core_node2
. The first one is the primary shard (leader=true
). For shard2
, we also have two replicas, one named core_node3
and the second one named core_node4
. In this case, the first is also the primary shard.
To delete a shard, we need to send an appropriate request to the Collections API (/admin/collections
) and specify the delete action (action=DELETEREPLICA
), collection (collection=testCollection
), the shard (shard=shard1
), and the name of the replica (replica=core_node2
). In our case, we want to remove the replica from nodes running on port 8983
for shard1
(which means that we remove core_node4
) and on port 6983
for shard2
(which means we remove core_node4
).
As we can see, after both the commands were executed, we are now left with a collection that has only primary shards. If we look at the cluster state now, we will see that there are no trace of the replicas, which means that Solr removed them properly.
3.129.21.47