Skip to content

[Cluster Health API] active_shards_percent_as_number calculation should not allow denominator to be zero #134806

@guluo2016

Description

@guluo2016

Elasticsearch Version

elasticsearch-9.2.0-SNAPSHOT

Installed Plugins

No response

Java Version

bundled

OS Version

Ubuntu 24.04.1 LTS

Problem Description

When restarting an Elasticsearch cluster, if an ES process starts successfully but no shards have been loaded yet, executing _cluster/health may return "active_shards_percent_as_number": "NaN".

This happens because at that moment the cluster has not loaded any shards, so totalShardCount == 0. Since the cluster state is not GREEN, the calculation of active_shards_percent_as_number results in NaN.

// ClusterStateHealth.java
if (computeStatus.equals(ClusterHealthStatus.GREEN)) {
	this.activeShardsPercent = 100;
} else {
	this.activeShardsPercent = (((double) this.activeShards) / totalShardCount) * 100;
}
{
  "cluster_name": "es-cluster",
  "status": "yellow",
  "timed_out": false,
  "number_of_nodes": 3,
  "number_of_data_nodes": 0,
  "active_primary_shards": 0,
  "active_shards": 0,
  "relocating_shards": 0,
  "initializing_shards": 0,
  "unassigned_shards": 0,
  "delayed_unassigned_shards": 0,
  "number_of_pending_tasks": 190,
  "number_of_in_flight_fetch": 0,
  "task_max_waiting_in_queue_millis": 1345,
  "active_shards_percent_as_number": "NaN"
}

Steps to Reproduce

As described above

Logs (if relevant)

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions