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

[abandoned][es] Add index rollover mode that can choose day and hour #2935

Closed
wants to merge 12 commits into from
Closed
1 change: 1 addition & 0 deletions pkg/es/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type Configuration struct {
BulkFlushInterval time.Duration `mapstructure:"-"`
IndexPrefix string `mapstructure:"index_prefix"`
IndexDateLayout string `mapstructure:"index_date_layout"`
IndexRotate string `mapstructure:"-"`
Copy link
Contributor

Choose a reason for hiding this comment

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

ES uses the term Rollover, I think we should stay consistent with terminology.

Tags TagsAsFields `mapstructure:"tags_as_fields"`
Enabled bool `mapstructure:"-"`
TLS tlscfg.Options `mapstructure:"tls"`
Expand Down
29 changes: 26 additions & 3 deletions plugin/storage/es/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
suffixTimeout = ".timeout"
suffixIndexPrefix = ".index-prefix"
suffixIndexDateSeparator = ".index-date-separator"
suffixIndexRotate = ".index-rotate"
suffixTagsAsFields = ".tags-as-fields"
suffixTagsAsFieldsAll = suffixTagsAsFields + ".all"
suffixTagsAsFieldsInclude = suffixTagsAsFields + ".include"
Expand All @@ -65,6 +66,8 @@ const (
defaultRemoteReadClusters = ""
// default separator for Elasticsearch index date layout.
defaultIndexDateSeparator = "-"

defaultIndexRotate = "day"
)

// TODO this should be moved next to config.Configuration struct (maybe ./flags package)
Expand Down Expand Up @@ -205,7 +208,11 @@ func addFlags(flagSet *flag.FlagSet, nsConfig *namespaceConfig) {
flagSet.String(
nsConfig.namespace+suffixIndexDateSeparator,
defaultIndexDateSeparator,
"Optional date separator of Jaeger indices. For example \".\" creates \"jaeger-span-2020.11.20 \".")
"Optional date separator of Jaeger indices. For example \".\" creates \"jaeger-span-2020.11.20\".")
flagSet.String(
nsConfig.namespace+suffixIndexRotate,
defaultIndexRotate,
"Optional rotation opportunity of Jaeger indices. For example \"day\" creates \"jaeger-span-yyyy-HH-dd\" every day after UTC 12AM. Valid opportunity: [hour, day]")
Copy link
Contributor

Choose a reason for hiding this comment

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

s/Optional rotation opportunity of Jaeger indices/Rotates Jaeger indices over the given period/
s/Valid opportunity/Valid options/

As you suggested, I would add a reference to the alternative esRollover and ILM solutions.

flagSet.Bool(
nsConfig.namespace+suffixTagsAsFieldsAll,
nsConfig.Tags.AllAsFields,
Expand Down Expand Up @@ -295,7 +302,6 @@ func initFromViper(cfg *namespaceConfig, v *viper.Viper) {
cfg.BulkFlushInterval = v.GetDuration(cfg.namespace + suffixBulkFlushInterval)
cfg.Timeout = v.GetDuration(cfg.namespace + suffixTimeout)
cfg.IndexPrefix = v.GetString(cfg.namespace + suffixIndexPrefix)
cfg.IndexDateLayout = initDateLayout(v.GetString(cfg.namespace + suffixIndexDateSeparator))
cfg.Tags.AllAsFields = v.GetBool(cfg.namespace + suffixTagsAsFieldsAll)
cfg.Tags.Include = v.GetString(cfg.namespace + suffixTagsAsFieldsInclude)
cfg.Tags.File = v.GetString(cfg.namespace + suffixTagsFile)
Expand All @@ -317,6 +323,19 @@ func initFromViper(cfg *namespaceConfig, v *viper.Viper) {
if len(remoteReadClusters) > 0 {
cfg.RemoteReadClusters = strings.Split(remoteReadClusters, ",")
}

indexRotate := strings.ToLower(v.GetString(cfg.namespace + suffixIndexRotate))

switch indexRotate {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can DRY and simplify to something like:

	separator := v.GetString(cfg.namespace + suffixIndexDateSeparator)
	rotateBy := strings.ToLower(v.GetString(cfg.namespace + suffixIndexRotate))
	cfg.IndexDateLayout = initDateLayout(rotateBy, separator)
...
func initDateLayout(rotateBy, separator string) string {
	seps := []interface{}{separator, separator}
	indexLayout := "2006%s01%s02"
	if rotateBy == "hour" {
		indexLayout = "2006%s01%s02%s15"
		seps = append(seps, separator)
	}
	return fmt.Sprintf(indexLayout, seps...)
}

case "day":
cfg.IndexDateLayout = initDateLayoutDay(v.GetString(cfg.namespace + suffixIndexDateSeparator))

case "hour":
cfg.IndexDateLayout = initDateLayoutHours(v.GetString(cfg.namespace + suffixIndexDateSeparator))

default:
cfg.IndexDateLayout = initDateLayoutDay(v.GetString(cfg.namespace + suffixIndexDateSeparator))
}
}

// GetPrimary returns primary configuration.
Expand All @@ -343,6 +362,10 @@ func stripWhiteSpace(str string) string {
return strings.Replace(str, " ", "", -1)
}

func initDateLayout(separator string) string {
func initDateLayoutDay(separator string) string {
return fmt.Sprintf("2006%s01%s02", separator, separator)
}

func initDateLayoutHours(separator string) string {
return fmt.Sprintf("2006%s01%s02%s15", separator, separator, separator)
}
24 changes: 24 additions & 0 deletions plugin/storage/es/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,27 @@ func TestIndexDateSeparator(t *testing.T) {
})
}
}

func TestIndexRotate(t *testing.T) {
testCases := []struct {
name string
flags []string
wantDateLayout string
}{
{"not defined (default)", []string{}, "2006-01-02"},
{"day rotation", []string{"--es.index-rotate=day"}, "2006-01-02"},
{"hour rotation", []string{"--es.index-rotate=hour"}, "2006-01-02-15"},
{"error rotation change default", []string{"--es.index-rotate=hours"}, "2006-01-02"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
opts := NewOptions("es")
v, command := config.Viperize(opts.AddFlags)
command.ParseFlags(tc.flags)
opts.InitFromViper(v)
primary := opts.GetPrimary()
assert.Equal(t, tc.wantDateLayout, primary.IndexDateLayout)

})
}
}
8 changes: 6 additions & 2 deletions plugin/storage/es/spanstore/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,12 @@ func timeRangeIndices(indexName, indexDateLayout string, startTime time.Time, en
firstIndex := indexWithDate(indexName, indexDateLayout, startTime)
currentIndex := indexWithDate(indexName, indexDateLayout, endTime)
for currentIndex != firstIndex {
indices = append(indices, currentIndex)
endTime = endTime.Add(-24 * time.Hour)
if len(indices) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could be simplified:

if len(indices) == 0 || indices[len(indices)-1] != currentIndex {
    indices = append(indices, currentIndex)
}

indices = append(indices, currentIndex)
} else if indices[len(indices)-1] != currentIndex {
indices = append(indices, currentIndex)
}
endTime = endTime.Add(-1 * time.Hour)
Copy link
Member

Choose a reason for hiding this comment

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

So this will do 24 iterations on every invocation, even though the default is daily rotation? Could we make this more efficient?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok let me take a look

currentIndex = indexWithDate(indexName, indexDateLayout, endTime)
}
indices = append(indices, firstIndex)
Expand Down