Skip to content

Commit

Permalink
Fix possible missing 'columns' call after 'tags' call
Browse files Browse the repository at this point in the history
Signed-off-by: Łukasz Gryglicki <[email protected]>
  • Loading branch information
lukaszgryglicki committed Aug 22, 2022
1 parent 21e451a commit 16ebbcc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
20 changes: 12 additions & 8 deletions cmd/gha2db_sync/gha2db_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ type metric struct {
// randomize - shufflues array of metrics to calculate, making sure that ctx.LastSeries is still last
func (m *metrics) randomize(ctx *lib.Ctx) {
lib.Printf("Randomizing metrics calculation order\n")
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(m.Metrics), func(i, j int) { m.Metrics[i], m.Metrics[j] = m.Metrics[j], m.Metrics[i] })
idx := -1
lastI := len(m.Metrics) - 1
Expand Down Expand Up @@ -401,6 +400,11 @@ func sync(ctx *lib.Ctx, args []string) {
}

// Calc metric
dailyRecalcHour := 0
ranTags := false
if ctx.RandComputeAtThisDate {
dailyRecalcHour = rand.Intn(6)
}
if !ctx.SkipTSDB || ctx.UseESOnly {
metricsDir := dataPrefix + "metrics"
if ctx.Project != "" {
Expand All @@ -416,24 +420,25 @@ func sync(ctx *lib.Ctx, args []string) {

// TSDB tags (repo groups template variable currently)
if !ctx.SkipTags {
if ctx.ResetTSDB || nowHour == 0 {
if ctx.ResetTSDB || nowHour == dailyRecalcHour {
_, err := lib.ExecCommand(ctx, []string{cmdPrefix + "tags"}, nil)
lib.FatalOnError(err)
ranTags = true
} else {
lib.Printf("Skipping `tags` recalculation, it is only computed once per day\n")
lib.Printf("Skipping `tags` recalculation, it is only computed once per day hour=%d\n", dailyRecalcHour)
}
}
// When resetting all TSDB data, adding new TS points will race for update TSDB structure
// While we can just run "columns" once to ensure thay match tags output
// Event if there are new columns after that - they will be very few not all of them to add at once
// Even if there are new columns after that - they will be very few not all of them to add at once
if ctx.ResetTSDB && !ctx.SkipColumns {
_, err := lib.ExecCommand(ctx, []string{cmdPrefix + "columns"}, nil)
lib.FatalOnError(err)
}

// Annotations
if !ctx.SkipAnnotations {
if ctx.Project != "" && (ctx.ResetTSDB || nowHour == 0) {
if ctx.Project != "" && (ctx.ResetTSDB || nowHour == dailyRecalcHour) {
_, err := lib.ExecCommand(
ctx,
[]string{
Expand Down Expand Up @@ -615,7 +620,6 @@ func sync(ctx *lib.Ctx, args []string) {
recalc := lib.ComputePeriodAtThisDate(ctx, period, to, metric.Histogram)
// Because sync probab can be less than 100% and that may cause gaps, we should eventually let do recalculation even if that period doesn't need it
if !recalc {
rand.Seed(time.Now().UnixNano())
val := rand.Intn(ctx.RecalcReciprocal)
if val == 0 {
recalc = true
Expand Down Expand Up @@ -706,7 +710,6 @@ func sync(ctx *lib.Ctx, args []string) {
// randomize histograms
if !ctx.SkipRand {
lib.Printf("Randomizing histogram metrics calculation order\n")
rand.Seed(time.Now().UnixNano())
rand.Shuffle(
len(hists),
func(i, j int) {
Expand Down Expand Up @@ -762,7 +765,7 @@ func sync(ctx *lib.Ctx, args []string) {

// TSDB ensure that calculated metric have all columns from tags
if !ctx.SkipColumns {
if ctx.RunColumns || ctx.ResetTSDB || nowHour == 0 {
if ctx.RunColumns || ctx.ResetTSDB || ranTags || nowHour == dailyRecalcHour {
_, err := lib.ExecCommand(ctx, []string{cmdPrefix + "columns"}, nil)
lib.FatalOnError(err)
} else {
Expand Down Expand Up @@ -905,6 +908,7 @@ func main() {
var ctx lib.Ctx
ctx.Init()
lib.SetupTimeoutSignal(&ctx)
rand.Seed(time.Now().UnixNano())
sync(&ctx, getSyncArgs(&ctx, os.Args))
dtEnd := time.Now()
lib.Printf("Time: %v\n", dtEnd.Sub(dtStart))
Expand Down
3 changes: 0 additions & 3 deletions time.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,6 @@ func ComputePeriodAtThisDate(ctx *Ctx, period string, idt time.Time, hist bool)
if ctx.RandComputeAtThisDate {
return Probab(29)
}
if ctx.RandComputeAtThisDate {
return Probab(5)
}
return h%7 == 0
} else if periodStart == "m" || periodStart == "q" || periodStart == "y" {
if ctx.RandComputeAtThisDate {
Expand Down

0 comments on commit 16ebbcc

Please sign in to comment.