From de16731f8cab8393fedfb57fbe39318ccec7f389 Mon Sep 17 00:00:00 2001 From: silenceqi Date: Thu, 19 Dec 2024 21:03:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20recording=20cluster=20allocation=20expl?= =?UTF-8?q?ain=20to=20activity=20after=20cluster=20hea=E2=80=A6=20(#18)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: saving cluster allocation explain to activity after cluster health changed to `red` * chore: update release notes --- core/elastic/api.go | 1 + docs/content.en/docs/release-notes/_index.md | 6 ++++++ modules/elastic/adapter/elasticsearch/v0.go | 19 +++++++++++++++++++ modules/elastic/metadata.go | 15 +++++++++++++++ 4 files changed, 41 insertions(+) diff --git a/core/elastic/api.go b/core/elastic/api.go index 430799b4..1af3f1f9 100755 --- a/core/elastic/api.go +++ b/core/elastic/api.go @@ -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 { diff --git a/docs/content.en/docs/release-notes/_index.md b/docs/content.en/docs/release-notes/_index.md index 846d7b1b..fa4ef72e 100644 --- a/docs/content.en/docs/release-notes/_index.md +++ b/docs/content.en/docs/release-notes/_index.md @@ -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 diff --git a/modules/elastic/adapter/elasticsearch/v0.go b/modules/elastic/adapter/elasticsearch/v0.go index a01a0449..132bc804 100755 --- a/modules/elastic/adapter/elasticsearch/v0.go +++ b/modules/elastic/adapter/elasticsearch/v0.go @@ -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 } \ No newline at end of file diff --git a/modules/elastic/metadata.go b/modules/elastic/metadata.go index 0a3c56f1..3d6989f6 100644 --- a/modules/elastic/metadata.go +++ b/modules/elastic/metadata.go @@ -28,6 +28,7 @@ package elastic import ( + "context" "errors" "fmt" "reflect" @@ -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)