Skip to content

Commit

Permalink
Fix cluster settings collection when max_shards_per_node is manually …
Browse files Browse the repository at this point in the history
…set (#603)

Signed-off-by: akazs <[email protected]>
  • Loading branch information
akazs authored Jul 20, 2022
1 parent d9b9ecd commit 102a9d3
Show file tree
Hide file tree
Showing 4 changed files with 1,542 additions and 8 deletions.
8 changes: 5 additions & 3 deletions collector/cluster_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ func (cs *ClusterSettings) Collect(ch chan<- prometheus.Metric) {

cs.shardAllocationEnabled.Set(float64(shardAllocationMap[csr.Cluster.Routing.Allocation.Enabled]))

maxShardsPerNode, err := strconv.ParseInt(csr.Cluster.MaxShardsPerNode, 10, 64)
if err == nil {
cs.maxShardsPerNode.Set(float64(maxShardsPerNode))
if maxShardsPerNodeString, ok := csr.Cluster.MaxShardsPerNode.(string); ok {
maxShardsPerNode, err := strconv.ParseInt(maxShardsPerNodeString, 10, 64)
if err == nil {
cs.maxShardsPerNode.Set(float64(maxShardsPerNode))
}
}
}
5 changes: 3 additions & 2 deletions collector/cluster_settings_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ type ClusterSettingsResponse struct {

// Cluster is a representation of a Elasticsearch Cluster Settings
type Cluster struct {
Routing Routing `json:"routing"`
MaxShardsPerNode string `json:"max_shards_per_node"`
Routing Routing `json:"routing"`
// This can be either a JSON object (which does not contain the value we are interested in) or a string
MaxShardsPerNode interface{} `json:"max_shards_per_node"`
}

// Routing is a representation of a Elasticsearch Cluster shard routing configuration
Expand Down
10 changes: 7 additions & 3 deletions collector/cluster_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,22 @@ func TestClusterSettingsStats(t *testing.T) {
if nsr.Cluster.Routing.Allocation.Enabled != "ALL" {
t.Errorf("Wrong setting for cluster routing allocation enabled")
}
if nsr.Cluster.MaxShardsPerNode != "" {
if nsr.Cluster.MaxShardsPerNode != nil {
t.Errorf("MaxShardsPerNode should be empty on older releases")
}
}
}
}

func TestClusterMaxShardsPerNode(t *testing.T) {
// Testcases created using:
// settings-7.3.0.json testcase created using:
// docker run -d -p 9200:9200 elasticsearch:VERSION-alpine
// curl http://localhost:9200/_cluster/settings/?include_defaults=true
files := []string{"../fixtures/settings-7.3.0.json"}
// settings-persistent-clustermaxshartspernode-7.17.json testcase created using:
// docker run -d -p 9200:9200 elasticsearch:VERSION
// curl -X PUT http://localhost:9200/_cluster/settings -H 'Content-Type: application/json' -d '{"persistent":{"cluster.max_shards_per_node":1000}}'
// curl http://localhost:9200/_cluster/settings/?include_defaults=true
files := []string{"../fixtures/settings-7.3.0.json", "../fixtures/settings-persistent-clustermaxshartspernode-7.17.5.json"}
for _, filename := range files {
f, _ := os.Open(filename)
defer f.Close()
Expand Down
Loading

0 comments on commit 102a9d3

Please sign in to comment.