Skip to content

Commit

Permalink
Add a GetShardRecovery with query params function to the ES client
Browse files Browse the repository at this point in the history
  • Loading branch information
hoenn committed Nov 16, 2023
1 parent a7e2e17 commit 20875d3
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions es.go
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,59 @@ func (c *Client) GetShardRecovery(nodes []string, onlyActive bool) ([]ShardRecov
return filteredRecoveries, nil
}

// Get details regarding shard recovery operations across a set of cluster nodes sending the desired query parameters
//
// Use case: You can view the shard recovery progress of the cluster with the bytes=b parameter.
func (c *Client) GetShardRecoveryWithQueryParams(nodes []string, params map[string]string) ([]ShardRecovery, error) {
var allRecoveries []ShardRecovery
uri := "_cat/recovery"

var queryStrings []string

Check failure on line 1521 in es.go

View workflow job for this annotation

GitHub Actions / Vulcanizer tests against go version 1.15.x

Consider preallocating `queryStrings` (prealloc)

Check failure on line 1521 in es.go

View workflow job for this annotation

GitHub Actions / Vulcanizer tests against go version 1.16.x

Consider preallocating `queryStrings` (prealloc)

Check failure on line 1521 in es.go

View workflow job for this annotation

GitHub Actions / Vulcanizer tests against go version 1.17.x

Consider preallocating `queryStrings` (prealloc)
for param, val := range params {
queryStrings = append(queryStrings, fmt.Sprintf("%s=%s", param, val))
}

uri = fmt.Sprintf("%s?%s", uri, strings.Join(queryStrings, "&"))

req := c.buildGetRequest(uri)
err := handleErrWithStruct(req, &allRecoveries)

if err != nil {
return nil, err
}

// No nodes passed, so return all shards
if len(nodes) == 0 {
return allRecoveries, nil
}

var filteredRecoveries []ShardRecovery
nodeRegexps := make([]*regexp.Regexp, 0, len(nodes))

for _, node := range nodes {
nodeRegexp, err := regexp.Compile(node)
if err != nil {
return nil, err
}
nodeRegexps = append(nodeRegexps, nodeRegexp)
}

for _, shard := range allRecoveries {
for _, nodeRegexp := range nodeRegexps {
// Support regexp matching of node name
matchesSource := nodeRegexp.MatchString(shard.SourceNode)
matchesTarget := nodeRegexp.MatchString(shard.TargetNode)

// Return if either source node or target node matches
if matchesSource || matchesTarget {
filteredRecoveries = append(filteredRecoveries, shard)
}
}
}

return filteredRecoveries, nil
}

// GetDuration gets the total duration of a snapshot
func (s *Snapshot) GetDuration() int {
if s.DurationMillis > 0 {
Expand Down

0 comments on commit 20875d3

Please sign in to comment.