Skip to content

Commit 557df94

Browse files
Asatyamyurishkuro
authored andcommitted
[fix] Allow es-index-cleaner to delete indices based on current time (jaegertracing#6790)
## Which problem is this PR solving? - Resolves jaegertracing#6236 ## Description of the changes - Changes the es-index-cleaner to remove indices older than 24 hours from the time it runs, so deletions happen hourly instead of just at midnight. ## How was this change tested? - CI ## Checklist - [x] I have read https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md - [x] I have signed all commits - [x] I have added unit tests for the new functionality - [x] I have run lint and test steps successfully - for `jaeger`: `make lint test` - for `jaeger-ui`: `npm run lint` and `npm run test` --------- Signed-off-by: SatyamAgrawal <[email protected]> Signed-off-by: Yuri Shkuro <[email protected]> Signed-off-by: Yuri Shkuro <[email protected]> Co-authored-by: Yuri Shkuro <[email protected]> Co-authored-by: Yuri Shkuro <[email protected]>
1 parent 229dc03 commit 557df94

File tree

3 files changed

+110
-3
lines changed

3 files changed

+110
-3
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2025 The Jaeger Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package app
5+
6+
import (
7+
"time"
8+
)
9+
10+
func CalculateDeletionCutoff(currTime time.Time, numOfDays int, relativeIndexEnabled bool) time.Time {
11+
year, month, day := currTime.Date()
12+
// tomorrow midnight
13+
cutoffTime := time.Date(year, month, day, 0, 0, 0, 0, time.UTC).AddDate(0, 0, 1)
14+
if relativeIndexEnabled {
15+
cutoffTime = currTime
16+
}
17+
return cutoffTime.Add(-time.Hour * 24 * time.Duration(numOfDays))
18+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) 2025 The Jaeger Authors.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package app
5+
6+
import (
7+
"testing"
8+
"time"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestCalculateDeletionCutoff(t *testing.T) {
14+
time20250309163053 := time.Date(2025, time.March, 9, 16, 30, 53, 0, time.UTC)
15+
tests := []struct {
16+
name string
17+
currTime time.Time
18+
numOfDays int
19+
relativeIndexEnabled bool
20+
expectedCutoff time.Time
21+
}{
22+
{
23+
name: "get today's midnight",
24+
currTime: time20250309163053,
25+
numOfDays: 1,
26+
relativeIndexEnabled: false,
27+
expectedCutoff: time.Date(2025, time.March, 9, 0, 0, 0, 0, time.UTC),
28+
},
29+
{
30+
name: "get exactly 24 hours before execution time",
31+
currTime: time20250309163053,
32+
numOfDays: 1,
33+
relativeIndexEnabled: true,
34+
expectedCutoff: time.Date(2025, time.March, 8, 16, 30, 53, 0, time.UTC),
35+
},
36+
{
37+
name: "get the current time if numOfDays is 0 and relativeIndexEnabled is true",
38+
currTime: time20250309163053,
39+
numOfDays: 0,
40+
relativeIndexEnabled: true,
41+
expectedCutoff: time20250309163053,
42+
},
43+
{
44+
name: "get tomorrow's midnight if numOfDays is 0 relativeIndexEnabled is False",
45+
currTime: time20250309163053,
46+
numOfDays: 0,
47+
relativeIndexEnabled: false,
48+
expectedCutoff: time.Date(2025, time.March, 10, 0, 0, 0, 0, time.UTC),
49+
},
50+
{
51+
name: "get (numOfDays-1)'s midnight",
52+
currTime: time20250309163053,
53+
numOfDays: 3,
54+
relativeIndexEnabled: false,
55+
expectedCutoff: time.Date(2025, time.March, 7, 0, 0, 0, 0, time.UTC),
56+
},
57+
{
58+
name: "get exactly (24*numOfDays) hours before execution time",
59+
currTime: time20250309163053,
60+
numOfDays: 3,
61+
relativeIndexEnabled: true,
62+
expectedCutoff: time.Date(2025, time.March, 6, 16, 30, 53, 0, time.UTC),
63+
},
64+
}
65+
66+
for _, test := range tests {
67+
t.Run(test.name, func(t *testing.T) {
68+
cutoffTime := CalculateDeletionCutoff(time20250309163053, test.numOfDays, test.relativeIndexEnabled)
69+
assert.Equal(t, test.expectedCutoff, cutoffTime)
70+
})
71+
}
72+
}

cmd/es-index-cleaner/main.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,39 @@ import (
77
"context"
88
"encoding/base64"
99
"errors"
10+
"flag"
1011
"fmt"
1112
"log"
1213
"net/http"
1314
"strconv"
1415
"time"
1516

1617
"github.com/spf13/cobra"
18+
"github.com/spf13/pflag"
1719
"github.com/spf13/viper"
20+
"go.opentelemetry.io/collector/featuregate"
1821
"go.uber.org/zap"
1922

2023
"github.com/jaegertracing/jaeger/cmd/es-index-cleaner/app"
2124
"github.com/jaegertracing/jaeger/pkg/config"
2225
"github.com/jaegertracing/jaeger/pkg/es/client"
2326
)
2427

28+
var relativeIndexCleaner *featuregate.Gate
29+
30+
func init() {
31+
relativeIndexCleaner = featuregate.GlobalRegistry().MustRegister(
32+
"es.index.relativeTimeIndexDeletion",
33+
featuregate.StageAlpha,
34+
featuregate.WithRegisterFromVersion("v2.5.0"),
35+
featuregate.WithRegisterDescription("Controls whether the indices will be deleted relative to the current time or tomorrow midnight."),
36+
featuregate.WithRegisterReferenceURL("https://github.com/jaegertracing/jaeger/issues/6236"),
37+
)
38+
featureGateFlagSet := flag.NewFlagSet("feature-gates", flag.ExitOnError)
39+
featuregate.GlobalRegistry().RegisterFlags(featureGateFlagSet)
40+
pflag.CommandLine.AddGoFlagSet(featureGateFlagSet)
41+
}
42+
2543
func main() {
2644
logger, _ := zap.NewProduction()
2745
v := viper.New()
@@ -70,9 +88,7 @@ func main() {
7088
return err
7189
}
7290

73-
year, month, day := time.Now().UTC().Date()
74-
tomorrowMidnight := time.Date(year, month, day, 0, 0, 0, 0, time.UTC).AddDate(0, 0, 1)
75-
deleteIndicesBefore := tomorrowMidnight.Add(-time.Hour * 24 * time.Duration(numOfDays))
91+
deleteIndicesBefore := app.CalculateDeletionCutoff(time.Now().UTC(), numOfDays, relativeIndexCleaner.IsEnabled())
7692
logger.Info("Indices before this date will be deleted", zap.String("date", deleteIndicesBefore.Format(time.RFC3339)))
7793

7894
filter := &app.IndexFilter{
@@ -100,6 +116,7 @@ func main() {
100116
cfg.AddFlags,
101117
)
102118

119+
command.Flags().AddFlagSet(pflag.CommandLine)
103120
if err := command.Execute(); err != nil {
104121
log.Fatalln(err)
105122
}

0 commit comments

Comments
 (0)