Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable require_alias for Bulk requests for all actions when target is a write alias #29879

Merged
merged 6 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d

*Affecting all Beats*

- Enable `require_alias` for Bulk requests for all actions when target is a write alias. {issue}27874[27874] {pull}29879[29879]


*Auditbeat*

Expand Down
9 changes: 5 additions & 4 deletions libbeat/esleg/eslegclient/bulkapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ type BulkDeleteAction struct {
}

type BulkMeta struct {
Index string `json:"_index" struct:"_index"`
DocType string `json:"_type,omitempty" struct:"_type,omitempty"`
Pipeline string `json:"pipeline,omitempty" struct:"pipeline,omitempty"`
ID string `json:"_id,omitempty" struct:"_id,omitempty"`
Index string `json:"_index" struct:"_index"`
DocType string `json:"_type,omitempty" struct:"_type,omitempty"`
Pipeline string `json:"pipeline,omitempty" struct:"pipeline,omitempty"`
ID string `json:"_id,omitempty" struct:"_id,omitempty"`
RequireAlias bool `json:"require_alias,omitempty" struct:"require_alias"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason that it is omitempty for JSON but not for struct?

}

type bulkRequest struct {
Expand Down
4 changes: 4 additions & 0 deletions libbeat/idxmgmt/std.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,17 @@ func (s *ilmIndexSelector) Select(evt *beat.Event) (string, error) {
return idx, err
}

func (s ilmIndexSelector) IsAlias() bool { return true }

func (s indexSelector) Select(evt *beat.Event) (string, error) {
if idx := getEventCustomIndex(evt, s.beatInfo); idx != "" {
return idx, nil
}
return s.sel.Select(evt)
}

func (s indexSelector) IsAlias() bool { return false }

func getEventCustomIndex(evt *beat.Event, beatInfo beat.Info) string {
if len(evt.Meta) == 0 {
return ""
Expand Down
8 changes: 8 additions & 0 deletions libbeat/outputs/elasticsearch/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ func (client *Client) createEventBulkMeta(version common.Version, event *beat.Ev
ID: id,
}

if isRequireAliasSupported(version) {
meta.RequireAlias = client.index.IsAlias()
}

if opType == events.OpTypeDelete {
if id != "" {
return eslegclient.BulkDeleteAction{Delete: meta}, nil
Expand All @@ -333,6 +337,10 @@ func (client *Client) createEventBulkMeta(version common.Version, event *beat.Ev
return eslegclient.BulkIndexAction{Index: meta}, nil
}

func isRequireAliasSupported(version common.Version) bool {
return !version.LessThan(common.MustNewVersion("7.9.0"))
Copy link
Member

@cmacknz cmacknz Jan 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR description mentions ES version 7.10. I'd expect either a <= 7.9.0 check or a < 7.10.0 check from that. This reads as not < v7.9.0 to me which allows v7.9.0.

}

func (client *Client) getPipeline(event *beat.Event) (string, error) {
if event.Meta != nil {
pipeline, err := events.GetMetaStringValue(*event, events.FieldMetaPipeline)
Expand Down
45 changes: 31 additions & 14 deletions libbeat/outputs/elasticsearch/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,22 +409,34 @@ func TestClientWithHeaders(t *testing.T) {

func TestBulkEncodeEvents(t *testing.T) {
cases := map[string]struct {
version string
docType string
config common.MapStr
events []common.MapStr
version string
docType string
config common.MapStr
ilmConfig *common.Config
isAlias bool
events []common.MapStr
}{
"6.x": {
version: "6.8.0",
docType: "doc",
config: common.MapStr{},
events: []common.MapStr{{"message": "test"}},
version: "6.8.0",
docType: "doc",
config: common.MapStr{},
ilmConfig: common.NewConfig(),
events: []common.MapStr{{"message": "test"}},
},
"latest": {
version: version.GetDefaultVersion(),
docType: "",
config: common.MapStr{},
events: []common.MapStr{{"message": "test"}},
"latest with ILM": {
version: version.GetDefaultVersion(),
docType: "",
config: common.MapStr{},
ilmConfig: common.NewConfig(),
isAlias: true,
events: []common.MapStr{{"message": "test"}},
},
"latest without ILM": {
version: version.GetDefaultVersion(),
docType: "",
config: common.MapStr{},
ilmConfig: disabledILMConfig(),
events: []common.MapStr{{"message": "test"}},
},
}

Expand All @@ -437,7 +449,7 @@ func TestBulkEncodeEvents(t *testing.T) {
Version: test.version,
}

im, err := idxmgmt.DefaultSupport(nil, info, common.NewConfig())
im, err := idxmgmt.DefaultSupport(nil, info, test.ilmConfig)
require.NoError(t, err)

index, pipeline, err := buildSelectors(im, info, cfg)
Expand Down Expand Up @@ -479,6 +491,7 @@ func TestBulkEncodeEvents(t *testing.T) {
}

assert.NotEqual(t, "", meta.Index)
assert.Equal(t, test.isAlias, meta.RequireAlias)
assert.Equal(t, test.docType, meta.DocType)
}

Expand All @@ -487,6 +500,10 @@ func TestBulkEncodeEvents(t *testing.T) {
}
}

func disabledILMConfig() *common.Config {
return common.MustNewConfigFrom(map[string]interface{}{"setup": map[string]interface{}{"ilm": map[string]interface{}{"enabled": false}}})
}

func TestBulkEncodeEventsWithOpType(t *testing.T) {
cases := []common.MapStr{
{"_id": "111", "op_type": e.OpTypeIndex, "message": "test 1", "bulkIndex": 0},
Expand Down
2 changes: 2 additions & 0 deletions libbeat/outputs/elasticsearch/death_letter_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ func (d DeadLetterSelector) Select(event *beat.Event) (string, error) {
}
return d.Selector.Select(event)
}

func (d DeadLetterSelector) IsAlias() bool { return false }
2 changes: 2 additions & 0 deletions libbeat/outputs/outil/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ func (s Selector) Select(evt *beat.Event) (string, error) {
return s.sel.sel(evt)
}

func (s Selector) IsAlias() bool { return false }

// IsEmpty checks if the selector is not configured and will always return an empty string.
func (s Selector) IsEmpty() bool {
return s.sel == nilSelector || s.sel == nil
Expand Down
2 changes: 2 additions & 0 deletions libbeat/outputs/output_reg.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ type IndexManager interface {
}

// IndexSelector is used to find the index name an event shall be indexed to.
// It also used to check if during indexing required_alias should be set.
type IndexSelector interface {
Select(event *beat.Event) (string, error)
IsAlias() bool
}

// Group configures and combines multiple clients into load-balanced group of clients
Expand Down