Skip to content

Commit

Permalink
[7.x](backport #26146) libbeat/idxmgmt/ilm: fix alias creation (#26193)
Browse files Browse the repository at this point in the history
* libbeat/idxmgmt/ilm: fix alias creation (#26146)

When creating the initial index/write alias fails, don't
check the status code, just check if the alias exists
regardless of the error. This fixes a bug where alias
creation fails when the alias exists but points to another
index, and the initial index does not exist (e.g. due to
ILM deletion.)

(cherry picked from commit 7bd96c2)

* Update CHANGELOG.next.asciidoc

Co-authored-by: Andrew Wilkins <axw@elastic.co>
  • Loading branch information
mergify[bot] and axw authored Jun 8, 2021
1 parent 455c697 commit 1a250bd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix `community_id` processor so that ports greater than 65535 aren't valid. {pull}25409[25409]
- Fix out of date FreeBSD vagrantbox. {pull}25652[25652]
- Fix handling of `file_selectors` in aws-s3 input. {pull}25792[25792]
- Fix ILM alias creation when write alias exists and initial index does not exist {pull}26143[26143]

*Auditbeat*

Expand Down
17 changes: 8 additions & 9 deletions libbeat/idxmgmt/ilm/client_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,18 @@ func (h *ESClientHandler) CreateAlias(alias Alias) error {
}

// Note: actual aliases are accessible via the index
status, res, err := h.client.Request("PUT", "/"+firstIndex, "", nil, body)
if status == 400 {
// HasAlias fails if there is an index with the same name, that is
// what we want to check here.
_, err := h.HasAlias(alias.Name)
if err != nil {
if _, res, err := h.client.Request("PUT", "/"+firstIndex, "", nil, body); err != nil {
// Creating the index may fail for multiple reasons, e.g. because
// the index exists, or because the write alias exists and points
// to another index.
if ok, err := h.HasAlias(alias.Name); err != nil {
// HasAlias fails if there is an index with the same name.
return err
} else if ok {
return errOf(ErrAliasAlreadyExists)
}
return errOf(ErrAliasAlreadyExists)
} else if err != nil {
return wrapErrf(err, ErrAliasCreateFailed, "failed to create alias: %s", res)
}

return nil
}

Expand Down
33 changes: 32 additions & 1 deletion libbeat/idxmgmt/ilm/client_handler_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,44 @@ func TestESClientHandler_Alias(t *testing.T) {
assert.True(t, b)
})

t.Run("second create", func(t *testing.T) {
t.Run("create index exists", func(t *testing.T) {
alias := makeAlias("esch-alias-2create")
h := newESClientHandler(t)

err := h.CreateAlias(alias)
assert.NoError(t, err)

// Second time around creating the alias, ErrAliasAlreadyExists is returned:
// the initial index already exists and the write alias points to it.
err = h.CreateAlias(alias)
require.Error(t, err)
assert.Equal(t, ilm.ErrAliasAlreadyExists, ilm.ErrReason(err))

b, err := h.HasAlias(alias.Name)
assert.NoError(t, err)
assert.True(t, b)
})

t.Run("create alias exists", func(t *testing.T) {
alias := makeAlias("esch-alias-2create")
alias.Pattern = "000001" // no date math, so we get predictable index names
h := newESClientHandler(t)

err := h.CreateAlias(alias)
assert.NoError(t, err)

// Rollover, so write alias points at -000002.
es := newRawESClient(t)
_, _, err = es.Request("POST", "/"+alias.Name+"/_rollover", "", nil, nil)
require.NoError(t, err)

// Delete -000001, simulating ILM delete.
_, _, err = es.Request("DELETE", "/"+alias.Name+"-"+alias.Pattern, "", nil, nil)
require.NoError(t, err)

// Second time around creating the alias, ErrAliasAlreadyExists is returned:
// initial index does not exist, but the write alias exists and points to
// another index.
err = h.CreateAlias(alias)
require.Error(t, err)
assert.Equal(t, ilm.ErrAliasAlreadyExists, ilm.ErrReason(err))
Expand Down

0 comments on commit 1a250bd

Please sign in to comment.