Skip to content

Commit

Permalink
polish review
Browse files Browse the repository at this point in the history
  • Loading branch information
JaredTan95 committed Aug 13, 2024
1 parent de8af19 commit 628c2ac
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 60 deletions.
54 changes: 44 additions & 10 deletions cmd/es-rollover/app/init/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,21 @@ package init

import (
"flag"
"log"

"github.com/spf13/viper"

"github.com/jaegertracing/jaeger/cmd/es-rollover/app"
cfg "github.com/jaegertracing/jaeger/pkg/es/config"
)

const (
shards = "shards"
replicas = "replicas"
prioritySpanTemplate = "priority-span-template"
priorityServiceTemplate = "priority-service-template"
)

// Config holds configuration for index cleaner binary.
type Config struct {
app.Config
Expand All @@ -31,6 +39,11 @@ type Config struct {

// AddFlags adds flags for TLS to the FlagSet.
func (*Config) AddFlags(flags *flag.FlagSet) {
flags.Int(shards, 5, "(deprecated, will be replaced with num-shards-span,num-shards-service,num-shards-sampling,num-shards-dependencies, if .num-shards set, it will be used as global settings for span,service,sampling,dependencies index) Number of shards")
flags.Int(replicas, 1, "(deprecated, will be replaced with num-replicas-span,num-replicas-service,num-replicas-sampling,num-replicas-dependencies, if .num-replicas set, it will be used as global settings for span,service,sampling,dependencies index) Number of replicas")
flags.Int(prioritySpanTemplate, 0, "(deprecated, will be replaced with priority-spans-template) Priority of jaeger-span index template (ESv8 only)")
flags.Int(priorityServiceTemplate, 0, "(deprecated, will be replaced with priority-services-template) Priority of jaeger-service index template (ESv8 only)")

flags.Int64(cfg.NumShardSpanFlag(), 5, "Number of span index shards")
flags.Int64(cfg.NumShardServiceFlag(), 5, "Number of service index shards")
flags.Int64(cfg.NumShardDependenciesFlag(), 5, "Number of dependencies index shards")
Expand All @@ -49,16 +62,37 @@ func (*Config) AddFlags(flags *flag.FlagSet) {

// InitFromViper initializes config from viper.Viper.
func (c *Config) InitFromViper(v *viper.Viper) {
c.Indices.Spans.TemplateNumShards = v.GetInt64(cfg.NumShardSpanFlag())
c.Indices.Spans.TemplateNumReplicas = v.GetInt64(cfg.NumReplicaSpanFlag())
c.Indices.Services.TemplateNumShards = v.GetInt64(cfg.NumShardServiceFlag())
c.Indices.Services.TemplateNumReplicas = v.GetInt64(cfg.NumReplicaServiceFlag())
c.Indices.Dependencies.TemplateNumShards = v.GetInt64(cfg.NumShardDependenciesFlag())
c.Indices.Dependencies.TemplateNumReplicas = v.GetInt64(cfg.NumReplicaDependenciesFlag())
c.Indices.Sampling.TemplateNumShards = v.GetInt64(cfg.NumShardSamplingFlag())
c.Indices.Sampling.TemplateNumReplicas = v.GetInt64(cfg.NumShardSamplingFlag())
c.Indices.Spans.TemplatePriority = v.GetInt64(cfg.PrioritySpanTemplateFlag())
c.Indices.Services.TemplatePriority = v.GetInt64(cfg.PriorityServiceTemplateFlag())

deprecatedNumShards := v.GetInt(shards)
deprecatedReplicaShards := v.GetInt(replicas)

if deprecatedReplicaShards > 0 || deprecatedNumShards > 0 {
log.Printf("using deprecated %s or %s", shards, replicas)
}

c.Indices.Spans.TemplateNumShards = cfg.LegacyFlagValue(deprecatedNumShards, v.GetInt(cfg.NumShardSpanFlag()))
c.Indices.Services.TemplateNumShards = cfg.LegacyFlagValue(deprecatedNumShards, v.GetInt(cfg.NumShardServiceFlag()))
c.Indices.Dependencies.TemplateNumShards = cfg.LegacyFlagValue(deprecatedNumShards, v.GetInt(cfg.NumShardDependenciesFlag()))
c.Indices.Sampling.TemplateNumShards = cfg.LegacyFlagValue(deprecatedNumShards, v.GetInt(cfg.NumShardSamplingFlag()))

c.Indices.Spans.TemplateNumReplicas = cfg.LegacyFlagValue(deprecatedReplicaShards, v.GetInt(cfg.NumReplicaSpanFlag()))
c.Indices.Services.TemplateNumReplicas = cfg.LegacyFlagValue(deprecatedReplicaShards, v.GetInt(cfg.NumReplicaServiceFlag()))
c.Indices.Dependencies.TemplateNumReplicas = cfg.LegacyFlagValue(deprecatedReplicaShards, v.GetInt(cfg.NumReplicaDependenciesFlag()))
c.Indices.Sampling.TemplateNumReplicas = cfg.LegacyFlagValue(deprecatedReplicaShards, v.GetInt(cfg.NumShardSamplingFlag()))

if v.GetInt(prioritySpanTemplate) > 0 || v.GetInt(priorityServiceTemplate) > 0 {
log.Printf("using deprecated %s or %s", shards, replicas)
}

deprecatedPrioritySpanTemplate := v.GetInt(prioritySpanTemplate)
deprecatedPriorityServiceTemplate := v.GetInt(priorityServiceTemplate)

if deprecatedPriorityServiceTemplate > 0 || deprecatedPrioritySpanTemplate > 0 {
log.Printf("using deprecated %s or %s", prioritySpanTemplate, priorityServiceTemplate)
}

c.Indices.Spans.TemplatePriority = cfg.LegacyFlagValue(deprecatedPrioritySpanTemplate, v.GetInt(cfg.PrioritySpanTemplateFlag()))
c.Indices.Services.TemplatePriority = cfg.LegacyFlagValue(deprecatedPriorityServiceTemplate, v.GetInt(cfg.PriorityServiceTemplateFlag()))
c.Indices.Dependencies.TemplatePriority = v.GetInt64(cfg.PriorityDependenciesTemplateFlag())
c.Indices.Sampling.TemplatePriority = v.GetInt64(cfg.PrioritySamplingTemplateFlag())
}
8 changes: 4 additions & 4 deletions cmd/es-rollover/app/init/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ func TestBindFlags(t *testing.T) {
v.BindPFlags(command.PersistentFlags())

err := command.ParseFlags([]string{
"--num-shards-spans=8",
"--num-replicas-spans=16",
"--priority-spans-template=300",
"--priority-services-template=301",
"--shards=8",
"--replicas=16",
"--priority-span-template=300",
"--priority-service-template=301",
"--priority-dependencies-template=302",
"--priority-sampling-template=303",
})
Expand Down
54 changes: 42 additions & 12 deletions cmd/esmapping-generator/app/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package app

import (
"github.com/spf13/cobra"

"github.com/jaegertracing/jaeger/pkg/es/config"
"github.com/spf13/cobra"
"log"
)

// Options represent configurable parameters for jaeger-esmapping-generator
Expand All @@ -33,13 +33,16 @@ type Options struct {
const (
mappingFlag = "mapping"
esVersionFlag = "es-version"
shardsFlag = "shards"
replicasFlag = "replicas"
indexPrefixFlag = "index-prefix"
useILMFlag = "use-ilm"
ilmPolicyNameFlag = "ilm-policy-name"
)

// AddFlags adds flags for esmapping-generator main program
func (o *Options) AddFlags(command *cobra.Command) {
var deprecatedNumShards, deprecatedReplicaShards int64
command.Flags().StringVar(
&o.Mapping,
mappingFlag,
Expand All @@ -50,42 +53,53 @@ func (o *Options) AddFlags(command *cobra.Command) {
esVersionFlag,
7,
"The major Elasticsearch version")
command.Flags().IntVar(
command.Flags().Int64Var(
&deprecatedNumShards,
shardsFlag,
5,
"(deprecated, will be replaced with num-shards-span,num-shards-service,num-shards-sampling,num-shards-dependencies, if .num-shards set, it will be used as global settings for span,service,sampling,dependencies index) The number of shards per index in Elasticsearch")
command.Flags().Int64Var(
&deprecatedReplicaShards,
replicasFlag,
1,
"(deprecated, will be replaced with num-replicas-span,num-replicas-service,num-replicas-sampling,num-replicas-dependencies, if .num-replicas set, it will be used as global settings for span,service,sampling,dependencies index) The number of replicas per index in Elasticsearch")

command.Flags().Int64Var(
&o.Indices.Spans.TemplateNumShards,
config.GetNumShardSpanFlag(),
config.NumShardSpanFlag(),
5,
"The number of shards per span index in Elasticsearch")
command.Flags().IntVar(
command.Flags().Int64Var(
&o.Indices.Spans.TemplateNumReplicas,
config.NumReplicaSpanFlag(),
1,
"The number of replicas per index in Elasticsearch")
command.Flags().IntVar(
command.Flags().Int64Var(
&o.Indices.Services.TemplateNumShards,
config.GetNumShardServiceFlag(),
config.NumShardServiceFlag(),
5,
"The number of shards per service index in Elasticsearch")
command.Flags().IntVar(
command.Flags().Int64Var(
&o.Indices.Services.TemplateNumReplicas,
config.NumReplicaServiceFlag(),
1,
"The number of replicas per service index in Elasticsearch")
command.Flags().IntVar(
command.Flags().Int64Var(
&o.Indices.Dependencies.TemplateNumShards,
config.NumShardDependenciesFlag(),
5,
"The number of shards per dependencies index in Elasticsearch")
command.Flags().IntVar(
command.Flags().Int64Var(
&o.Indices.Dependencies.TemplateNumReplicas,
config.NumReplicaDependenciesFlag(),
1,
"The number of replicas per dependencies index in Elasticsearch")
command.Flags().IntVar(
command.Flags().Int64Var(
&o.Indices.Sampling.TemplateNumShards,
config.NumShardSamplingFlag(),
5,
"The number of shards per sampling index in Elasticsearch")
command.Flags().IntVar(
command.Flags().Int64Var(
&o.Indices.Sampling.TemplateNumReplicas,
config.NumReplicaSamplingFlag(),
1,
Expand All @@ -106,6 +120,22 @@ func (o *Options) AddFlags(command *cobra.Command) {
"jaeger-ilm-policy",
"The name of the ILM policy to use if ILM is active")

if deprecatedNumShards > 0 {
log.Printf("using deprecated %s", shardsFlag)
o.Indices.Spans.TemplateNumShards = deprecatedNumShards
o.Indices.Services.TemplateNumShards = deprecatedNumShards
o.Indices.Dependencies.TemplateNumShards = deprecatedNumShards
o.Indices.Sampling.TemplateNumShards = deprecatedNumShards
}

if deprecatedReplicaShards > 0 {
log.Printf("using deprecated %s", replicasFlag)
o.Indices.Spans.TemplateNumReplicas = deprecatedReplicaShards
o.Indices.Services.TemplateNumReplicas = deprecatedReplicaShards
o.Indices.Dependencies.TemplateNumReplicas = deprecatedReplicaShards
o.Indices.Sampling.TemplateNumReplicas = deprecatedReplicaShards
}

// mark mapping flag as mandatory
command.MarkFlagRequired(mappingFlag)
}
32 changes: 16 additions & 16 deletions cmd/esmapping-generator/app/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ func TestOptionsWithDefaultFlags(t *testing.T) {

assert.Equal(t, "", o.Mapping)
assert.Equal(t, uint(7), o.EsVersion)
assert.Equal(t, 5, o.Indices.Spans.TemplateOptions.NumShards)
assert.Equal(t, 5, o.Indices.Services.TemplateOptions.NumShards)
assert.Equal(t, 5, o.Indices.Dependencies.TemplateOptions.NumShards)
assert.Equal(t, 5, o.Indices.Sampling.TemplateOptions.NumShards)
assert.Equal(t, int64(5), o.Indices.Spans.TemplateNumShards)
assert.Equal(t, int64(5), o.Indices.Services.TemplateNumShards)
assert.Equal(t, int64(5), o.Indices.Dependencies.TemplateNumShards)
assert.Equal(t, int64(5), o.Indices.Sampling.TemplateNumShards)

assert.Equal(t, 1, o.Indices.Spans.TemplateOptions.NumReplicas)
assert.Equal(t, 1, o.Indices.Services.TemplateOptions.NumReplicas)
assert.Equal(t, 1, o.Indices.Dependencies.TemplateOptions.NumReplicas)
assert.Equal(t, 1, o.Indices.Sampling.TemplateOptions.NumReplicas)
assert.Equal(t, int64(1), o.Indices.Spans.TemplateNumReplicas)
assert.Equal(t, int64(1), o.Indices.Services.TemplateNumReplicas)
assert.Equal(t, int64(1), o.Indices.Dependencies.TemplateNumReplicas)
assert.Equal(t, int64(1), o.Indices.Sampling.TemplateNumReplicas)
assert.Equal(t, "", o.IndexPrefix)
assert.Equal(t, "false", o.UseILM)
assert.Equal(t, "jaeger-ilm-policy", o.ILMPolicyName)
Expand All @@ -62,15 +62,15 @@ func TestOptionsWithFlags(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "jaeger-span", o.Mapping)
assert.Equal(t, uint(7), o.EsVersion)
assert.Equal(t, 5, o.Indices.Spans.TemplateOptions.NumShards)
assert.Equal(t, 5, o.Indices.Services.TemplateOptions.NumShards)
assert.Equal(t, 5, o.Indices.Dependencies.TemplateOptions.NumShards)
assert.Equal(t, 5, o.Indices.Sampling.TemplateOptions.NumShards)
assert.Equal(t, int64(5), o.Indices.Spans.TemplateNumShards)
assert.Equal(t, int64(5), o.Indices.Services.TemplateNumShards)
assert.Equal(t, int64(5), o.Indices.Dependencies.TemplateNumShards)
assert.Equal(t, int64(5), o.Indices.Sampling.TemplateNumShards)

assert.Equal(t, 1, o.Indices.Spans.TemplateOptions.NumReplicas)
assert.Equal(t, 1, o.Indices.Services.TemplateOptions.NumReplicas)
assert.Equal(t, 1, o.Indices.Dependencies.TemplateOptions.NumReplicas)
assert.Equal(t, 1, o.Indices.Sampling.TemplateOptions.NumReplicas)
assert.Equal(t, int64(1), o.Indices.Spans.TemplateNumReplicas)
assert.Equal(t, int64(1), o.Indices.Services.TemplateNumReplicas)
assert.Equal(t, int64(1), o.Indices.Dependencies.TemplateNumReplicas)
assert.Equal(t, int64(1), o.Indices.Sampling.TemplateNumReplicas)
assert.Equal(t, "test", o.IndexPrefix)
assert.Equal(t, "true", o.UseILM)
assert.Equal(t, "jaeger-test-policy", o.ILMPolicyName)
Expand Down
2 changes: 1 addition & 1 deletion cmd/esmapping-generator/app/renderer/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestIsValidOption(t *testing.T) {
}

func Test_getMappingAsString(t *testing.T) {
indicesOption := config.Indices{Spans: config.IndexOptions{TemplateOptions: config.TemplateOptions{NumShards: 5, NumReplicas: 1}}}
indicesOption := config.Indices{Spans: config.IndexOptions{TemplateNumShards: 5, TemplateNumReplicas: 1}}
tests := []struct {
name string
args app.Options
Expand Down
8 changes: 8 additions & 0 deletions pkg/es/config/flagutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,11 @@ func IndexRolloverFrequencySamplingFlag() string {
func IndexRolloverFrequencyDependenciesFlag() string {
return rolloverFrequency(DEPENDENCIES)
}

func LegacyFlagValue(deprecatedFlagValue, newValue int) int64 {
if deprecatedFlagValue > 0 {
return int64(deprecatedFlagValue)
} else {
return int64(newValue)
}
}
27 changes: 10 additions & 17 deletions plugin/storage/es/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,6 @@ func (opt *Options) InitFromViper(v *viper.Viper) {
}

func initFromViper(cfg *namespaceConfig, v *viper.Viper) {
overrideIndexShardsNums := func(deprecatedNum, newNum int) int64 {
if deprecatedNum > 0 {
return int64(deprecatedNum)
} else {
return int64(newNum)
}
}

cfg.Username = v.GetString(cfg.namespace + suffixUsername)
cfg.Password = v.GetString(cfg.namespace + suffixPassword)
cfg.TokenFilePath = v.GetString(cfg.namespace + suffixTokenPath)
Expand All @@ -376,15 +368,16 @@ func initFromViper(cfg *namespaceConfig, v *viper.Viper) {
if deprecatedReplicaShards > 0 || deprecatedNumShards > 0 {
log.Printf("using deprecated %s or %s", suffixNumShards, suffixNumReplicas)
}
cfg.Indices.Spans.TemplateNumShards = overrideIndexShardsNums(deprecatedNumShards, v.GetInt(cfg.namespace+suffix+config.NumShardSpanFlag()))
cfg.Indices.Services.TemplateNumShards = overrideIndexShardsNums(deprecatedNumShards, v.GetInt(cfg.namespace+suffix+config.NumShardServiceFlag()))
cfg.Indices.Sampling.TemplateNumShards = overrideIndexShardsNums(deprecatedNumShards, v.GetInt(cfg.namespace+suffix+config.NumShardSamplingFlag()))
cfg.Indices.Dependencies.TemplateNumShards = overrideIndexShardsNums(deprecatedNumShards, v.GetInt(cfg.namespace+suffix+config.NumShardDependenciesFlag()))

cfg.Indices.Spans.TemplateNumReplicas = overrideIndexShardsNums(deprecatedReplicaShards, v.GetInt(cfg.namespace+suffix+config.NumReplicaSpanFlag()))
cfg.Indices.Services.TemplateNumReplicas = overrideIndexShardsNums(deprecatedReplicaShards, v.GetInt(cfg.namespace+suffix+config.NumReplicaServiceFlag()))
cfg.Indices.Sampling.TemplateNumReplicas = overrideIndexShardsNums(deprecatedReplicaShards, v.GetInt(cfg.namespace+suffix+config.NumReplicaSamplingFlag()))
cfg.Indices.Dependencies.TemplateNumReplicas = overrideIndexShardsNums(deprecatedReplicaShards, v.GetInt(cfg.namespace+suffix+config.NumReplicaDependenciesFlag()))

cfg.Indices.Spans.TemplateNumShards = config.LegacyFlagValue(deprecatedNumShards, v.GetInt(cfg.namespace+suffix+config.NumShardSpanFlag()))
cfg.Indices.Services.TemplateNumShards = config.LegacyFlagValue(deprecatedNumShards, v.GetInt(cfg.namespace+suffix+config.NumShardServiceFlag()))
cfg.Indices.Sampling.TemplateNumShards = config.LegacyFlagValue(deprecatedNumShards, v.GetInt(cfg.namespace+suffix+config.NumShardSamplingFlag()))
cfg.Indices.Dependencies.TemplateNumShards = config.LegacyFlagValue(deprecatedNumShards, v.GetInt(cfg.namespace+suffix+config.NumShardDependenciesFlag()))

cfg.Indices.Spans.TemplateNumReplicas = config.LegacyFlagValue(deprecatedReplicaShards, v.GetInt(cfg.namespace+suffix+config.NumReplicaSpanFlag()))
cfg.Indices.Services.TemplateNumReplicas = config.LegacyFlagValue(deprecatedReplicaShards, v.GetInt(cfg.namespace+suffix+config.NumReplicaServiceFlag()))
cfg.Indices.Sampling.TemplateNumReplicas = config.LegacyFlagValue(deprecatedReplicaShards, v.GetInt(cfg.namespace+suffix+config.NumReplicaSamplingFlag()))
cfg.Indices.Dependencies.TemplateNumReplicas = config.LegacyFlagValue(deprecatedReplicaShards, v.GetInt(cfg.namespace+suffix+config.NumReplicaDependenciesFlag()))

cfg.Indices.Spans.TemplatePriority = v.GetInt64(cfg.namespace + suffix + config.PrioritySpanTemplateFlag())
cfg.Indices.Services.TemplatePriority = v.GetInt64(cfg.namespace + suffix + config.PriorityServiceTemplateFlag())
Expand Down

0 comments on commit 628c2ac

Please sign in to comment.