Skip to content

Commit 1a250bd

Browse files
mergify[bot]axw
andauthored
[7.x](backport #26146) libbeat/idxmgmt/ilm: fix alias creation (#26193)
* 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 <[email protected]>
1 parent 455c697 commit 1a250bd

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
179179
- Fix `community_id` processor so that ports greater than 65535 aren't valid. {pull}25409[25409]
180180
- Fix out of date FreeBSD vagrantbox. {pull}25652[25652]
181181
- Fix handling of `file_selectors` in aws-s3 input. {pull}25792[25792]
182+
- Fix ILM alias creation when write alias exists and initial index does not exist {pull}26143[26143]
182183

183184
*Auditbeat*
184185

libbeat/idxmgmt/ilm/client_handler.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,19 +191,18 @@ func (h *ESClientHandler) CreateAlias(alias Alias) error {
191191
}
192192

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

libbeat/idxmgmt/ilm/client_handler_integration_test.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,44 @@ func TestESClientHandler_Alias(t *testing.T) {
136136
assert.True(t, b)
137137
})
138138

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

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

146+
// Second time around creating the alias, ErrAliasAlreadyExists is returned:
147+
// the initial index already exists and the write alias points to it.
148+
err = h.CreateAlias(alias)
149+
require.Error(t, err)
150+
assert.Equal(t, ilm.ErrAliasAlreadyExists, ilm.ErrReason(err))
151+
152+
b, err := h.HasAlias(alias.Name)
153+
assert.NoError(t, err)
154+
assert.True(t, b)
155+
})
156+
157+
t.Run("create alias exists", func(t *testing.T) {
158+
alias := makeAlias("esch-alias-2create")
159+
alias.Pattern = "000001" // no date math, so we get predictable index names
160+
h := newESClientHandler(t)
161+
162+
err := h.CreateAlias(alias)
163+
assert.NoError(t, err)
164+
165+
// Rollover, so write alias points at -000002.
166+
es := newRawESClient(t)
167+
_, _, err = es.Request("POST", "/"+alias.Name+"/_rollover", "", nil, nil)
168+
require.NoError(t, err)
169+
170+
// Delete -000001, simulating ILM delete.
171+
_, _, err = es.Request("DELETE", "/"+alias.Name+"-"+alias.Pattern, "", nil, nil)
172+
require.NoError(t, err)
173+
174+
// Second time around creating the alias, ErrAliasAlreadyExists is returned:
175+
// initial index does not exist, but the write alias exists and points to
176+
// another index.
146177
err = h.CreateAlias(alias)
147178
require.Error(t, err)
148179
assert.Equal(t, ilm.ErrAliasAlreadyExists, ilm.ErrReason(err))

0 commit comments

Comments
 (0)