Skip to content

Commit

Permalink
feat: recording cluster allocation explain to activity after cluster …
Browse files Browse the repository at this point in the history
…hea… (#18)

* feat: saving cluster allocation explain to activity after cluster health changed to `red`

* chore: update release notes
  • Loading branch information
silenceqi authored Dec 19, 2024
1 parent a9b0736 commit de16731
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/elastic/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ type API interface {
DeleteILMPolicy(target string) error
GetRemoteInfo()([]byte, error)
Flush(indexName string) ([]byte, error)
ClusterAllocationExplain(ctx context.Context, body []byte, params url.Values)([]byte,error)
}

type TemplateAPI interface {
Expand Down
6 changes: 6 additions & 0 deletions docs/content.en/docs/release-notes/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ Information about release notes of INFINI Framework is provided here.

### Improvements

## v1.0.1
### Bug fix
### Features
- Record cluster allocation explain to activity after cluster health status changed to `red`
- Add elastic api method `ClusterAllocationExplain`

## v1.0.0

### 🚀 Features
Expand Down
19 changes: 19 additions & 0 deletions modules/elastic/adapter/elasticsearch/v0.go
Original file line number Diff line number Diff line change
Expand Up @@ -1906,4 +1906,23 @@ func (c *ESAPIV0) PutScript(scriptName string, script []byte)([]byte,error){

func (c *ESAPIV0)SearchByTemplate(indexName,scriptName string,params map[string]interface{}) (*elastic.SearchResponse, error) {
panic("not implemented")
}

func (c *ESAPIV0) ClusterAllocationExplain(ctx context.Context, body []byte, params url.Values)([]byte,error){
url := fmt.Sprintf("%s/_cluster/allocation/explain", c.GetEndpoint())
if len(params) > 0 {
url = fmt.Sprintf("%s?%s", url, params.Encode())
}
method := util.Verb_GET
if len(body) > 0 {
method = util.Verb_POST
}
resp, err := c.Request(ctx, method, url, body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf(string(resp.Body))
}
return resp.Body, nil
}
15 changes: 15 additions & 0 deletions modules/elastic/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package elastic

import (
"context"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -155,6 +156,20 @@ func updateClusterHealthStatus(clusterID string, healthStatus string) {
},
},
}
if healthStatus == "red" {
targetClient := elastic.GetClient(clusterID)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := targetClient.ClusterAllocationExplain(ctx, nil, nil)
if err != nil {
log.Errorf("get cluster allocation explain of cluster [%s] error: %v", clusterID, err)
}
activityInfo.Fields = util.MapStr{
"cluster_health": util.MapStr{
"allocation_explain": string(res),
},
}
}
_, err = client.Index(orm.GetIndexName(activityInfo), "", activityInfo.ID, activityInfo, "")
if err != nil {
log.Error(err)
Expand Down

0 comments on commit de16731

Please sign in to comment.