From a56694e9f0b9a41f3d1a9fe665d012076464fd43 Mon Sep 17 00:00:00 2001 From: Steffen Siering Date: Fri, 26 Mar 2021 17:02:56 +0100 Subject: [PATCH 1/2] Improve ILM policy and alias setup log output (#24480) (cherry picked from commit 4edc6dc782d611454ac815e6020404c468f7e0a4) --- CHANGELOG.next.asciidoc | 3 ++ libbeat/idxmgmt/ilm/ilm_test.go | 25 ++++++++++- libbeat/idxmgmt/ilm/std.go | 73 +++++++++++++++++++++++--------- libbeat/idxmgmt/std.go | 12 ++---- libbeat/tests/system/test_ilm.py | 18 +++++--- 5 files changed, 95 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index b5ab6e6be4e..a838dc5cb8f 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -139,6 +139,9 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Fix panic due to unhandled DeletedFinalStateUnknown in k8s OnDelete {pull}23419[23419] - Fix error loop with runaway CPU use when the Kafka output encounters some connection errors {pull}23484[23484] - Allow configuring credential_profile_name and shared_credential_file when using role_arn. {pull}24174[24174] +- Fix ILM setup log reporting that a policy or an alias was created, even though the creation of any resource was disabled. {issue}24046[24046] {pull}24480[24480] +- Fix ILM alias not being created if `setup.ilm.check_exists: false` and `setup.ilm.overwrite: true` has been configured. {pull}24480[24480] +- Fix issue discovering docker containers and metadata after reconnections {pull}24318[24318] - Allow cgroup self-monitoring to see alternate `hostfs` paths {pull}24334[24334] diff --git a/libbeat/idxmgmt/ilm/ilm_test.go b/libbeat/idxmgmt/ilm/ilm_test.go index e47a9c0d06b..8ca9b9e6c43 100644 --- a/libbeat/idxmgmt/ilm/ilm_test.go +++ b/libbeat/idxmgmt/ilm/ilm_test.go @@ -238,6 +238,27 @@ func TestDefaultSupport_Manager_EnsureAlias(t *testing.T) { }, fail: ErrRequestFailed, }, + "overwrite non-existent": { + calls: []onCall{ + onCreateAlias(alias).Return(nil), + }, + fail: nil, + cfg: map[string]interface{}{"check_exists": false, "overwrite": true}, + }, + "try overwrite existing": { + calls: []onCall{ + onCreateAlias(alias).Return(errOf(ErrAliasAlreadyExists)), + }, + fail: nil, // we detect that that the alias exists, and call it a day. + cfg: map[string]interface{}{"check_exists": false, "overwrite": true}, + }, + "fail to overwrite": { + calls: []onCall{ + onCreateAlias(alias).Return(errOf(ErrAliasCreateFailed)), + }, + fail: ErrAliasCreateFailed, + cfg: map[string]interface{}{"check_exists": false, "overwrite": true}, + }, } for name, test := range cases { @@ -283,12 +304,14 @@ func TestDefaultSupport_Manager_EnsurePolicy(t *testing.T) { }, }, "policy already exists": { + create: false, calls: []onCall{ onHasILMPolicy(testPolicy.Name).Return(true, nil), }, }, - "overwrite existing": { + "overwrite": { overwrite: true, + create: true, calls: []onCall{ onCreateILMPolicy(testPolicy).Return(nil), }, diff --git a/libbeat/idxmgmt/ilm/std.go b/libbeat/idxmgmt/ilm/std.go index 0e33fc00f3e..1f6a0151afc 100644 --- a/libbeat/idxmgmt/ilm/std.go +++ b/libbeat/idxmgmt/ilm/std.go @@ -103,42 +103,77 @@ func (m *stdManager) CheckEnabled() (bool, error) { } func (m *stdManager) EnsureAlias() error { - if !m.checkExists { - return nil - } + log := m.log + overwrite := m.Overwrite() + name := m.alias.Name - b, err := m.client.HasAlias(m.alias.Name) - if err != nil { - return err + var exists bool + if m.checkExists && !overwrite { + var err error + exists, err = m.client.HasAlias(name) + if err != nil { + return err + } } - if b { + + switch { + case exists && !overwrite: + log.Infof("Index Alias %v exists already.", name) return nil - } - // This always assume it's a date pattern by sourrounding it by <...> - return m.client.CreateAlias(m.alias) + case !exists || overwrite: + err := m.client.CreateAlias(m.alias) + if err != nil { + if ErrReason(err) != ErrAliasAlreadyExists { + log.Errorf("Index Alias %v setup failed: %v.", name, err) + return err + } + log.Infof("Index Alias %v exists already.", name) + return nil + } + + log.Infof("Index Alias %v successfully created.", name) + return nil + + default: + m.log.Infof("ILM index alias not created: exists=%v, overwrite=%v", exists, overwrite) + return nil + } } func (m *stdManager) EnsurePolicy(overwrite bool) (bool, error) { log := m.log overwrite = overwrite || m.Overwrite() + name := m.policy.Name - exists := true + var exists bool if m.checkExists && !overwrite { - b, err := m.client.HasILMPolicy(m.policy.Name) + var err error + exists, err = m.client.HasILMPolicy(name) if err != nil { return false, err } - exists = b } - if !exists || overwrite { - return !exists, m.client.CreateILMPolicy(m.policy) - } + switch { + case exists && !overwrite: + log.Infof("ILM policy %v exists already.", name) + return false, nil + + case !exists || overwrite: + err := m.client.CreateILMPolicy(m.policy) + if err != nil { + log.Errorf("ILM policy %v creation failed: %v", name, err) + return false, err + } - log.Infof("do not generate ilm policy: exists=%v, overwrite=%v", - exists, overwrite) - return false, nil + log.Infof("ILM policy %v successfully created.", name) + return true, err + + default: + log.Infof("ILM policy not created: exists=%v, overwrite=%v.", exists, overwrite) + return false, nil + } } func (c *infoCache) Valid() bool { diff --git a/libbeat/idxmgmt/std.go b/libbeat/idxmgmt/std.go index 538fc7d2262..99cf5890353 100644 --- a/libbeat/idxmgmt/std.go +++ b/libbeat/idxmgmt/std.go @@ -271,7 +271,6 @@ func (m *indexManager) Setup(loadTemplate, loadILM LoadMode) error { if err != nil { return err } - log.Info("ILM policy successfully loaded.") // The template should be updated if a new policy is created. if policyCreated && templateComponent.enabled { @@ -299,14 +298,9 @@ func (m *indexManager) Setup(loadTemplate, loadILM LoadMode) error { } if ilmComponent.load { - // ensure alias is created after the template is created - if err := m.ilm.EnsureAlias(); err != nil { - if ilm.ErrReason(err) != ilm.ErrAliasAlreadyExists { - return err - } - log.Info("Write alias exists already") - } else { - log.Info("Write alias successfully generated.") + err := m.ilm.EnsureAlias() + if err != nil { + return err } } diff --git a/libbeat/tests/system/test_ilm.py b/libbeat/tests/system/test_ilm.py index 2913893e11c..12c79fad61d 100644 --- a/libbeat/tests/system/test_ilm.py +++ b/libbeat/tests/system/test_ilm.py @@ -3,6 +3,7 @@ import logging import os import pytest +import re import shutil import unittest @@ -12,6 +13,9 @@ INTEGRATION_TESTS = os.environ.get('INTEGRATION_TESTS', False) +MSG_ILM_POLICY_LOADED = re.compile('ILM policy .* successfully created.') + + class TestRunILM(BaseTest): def setUp(self): @@ -46,7 +50,7 @@ def test_ilm_default(self): self.render_config() proc = self.start_beat() self.wait_until(lambda: self.log_contains("mockbeat start running.")) - self.wait_until(lambda: self.log_contains("ILM policy successfully loaded")) + self.wait_until(lambda: self.log_contains(MSG_ILM_POLICY_LOADED)) self.wait_until(lambda: self.log_contains("PublishEvents: 1 events have been published")) proc.check_kill_and_wait() @@ -84,7 +88,7 @@ def test_policy_name(self): proc = self.start_beat() self.wait_until(lambda: self.log_contains("mockbeat start running.")) - self.wait_until(lambda: self.log_contains("ILM policy successfully loaded")) + self.wait_until(lambda: self.log_contains(MSG_ILM_POLICY_LOADED)) self.wait_until(lambda: self.log_contains("PublishEvents: 1 events have been published")) proc.check_kill_and_wait() @@ -103,7 +107,7 @@ def test_rollover_alias(self): proc = self.start_beat() self.wait_until(lambda: self.log_contains("mockbeat start running.")) - self.wait_until(lambda: self.log_contains("ILM policy successfully loaded")) + self.wait_until(lambda: self.log_contains(MSG_ILM_POLICY_LOADED)) self.wait_until(lambda: self.log_contains("PublishEvents: 1 events have been published")) proc.check_kill_and_wait() @@ -123,7 +127,7 @@ def test_pattern(self): proc = self.start_beat() self.wait_until(lambda: self.log_contains("mockbeat start running.")) - self.wait_until(lambda: self.log_contains("ILM policy successfully loaded")) + self.wait_until(lambda: self.log_contains(MSG_ILM_POLICY_LOADED)) self.wait_until(lambda: self.log_contains("PublishEvents: 1 events have been published")) proc.check_kill_and_wait() @@ -143,7 +147,7 @@ def test_pattern_date(self): proc = self.start_beat() self.wait_until(lambda: self.log_contains("mockbeat start running.")) - self.wait_until(lambda: self.log_contains("ILM policy successfully loaded")) + self.wait_until(lambda: self.log_contains(MSG_ILM_POLICY_LOADED)) self.wait_until(lambda: self.log_contains("PublishEvents: 1 events have been published")) proc.check_kill_and_wait() @@ -286,12 +290,12 @@ def setUp(self): self.cmd = "ilm-policy" def assert_log_contains_policy(self): - assert self.log_contains('ILM policy successfully loaded.') + assert self.log_contains(MSG_ILM_POLICY_LOADED) assert self.log_contains('"max_age": "30d"') assert self.log_contains('"max_size": "50gb"') def assert_log_contains_write_alias(self): - assert self.log_contains('Write alias successfully generated.') + assert self.log_contains(re.compile('Index Alias .* successfully created.')) def test_default(self): """ From 4866f85fc2a045a12bad8ee68fe824f163d970f4 Mon Sep 17 00:00:00 2001 From: urso Date: Fri, 26 Mar 2021 17:04:48 +0100 Subject: [PATCH 2/2] fix changelog --- CHANGELOG.next.asciidoc | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index a838dc5cb8f..b36be87a155 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -141,7 +141,6 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Allow configuring credential_profile_name and shared_credential_file when using role_arn. {pull}24174[24174] - Fix ILM setup log reporting that a policy or an alias was created, even though the creation of any resource was disabled. {issue}24046[24046] {pull}24480[24480] - Fix ILM alias not being created if `setup.ilm.check_exists: false` and `setup.ilm.overwrite: true` has been configured. {pull}24480[24480] -- Fix issue discovering docker containers and metadata after reconnections {pull}24318[24318] - Allow cgroup self-monitoring to see alternate `hostfs` paths {pull}24334[24334]