Skip to content

Commit bcbf486

Browse files
[e2e][flake] Prevent Windows flare archive collisions (#54911)
<!-- dd-meta {"pullId":"e285912b-314c-4a9c-9246-8b3688311611","source":"chat","resourceId":"c5d0b2d1-7ec1-45c4-a402-380c09336a86","workflowId":"1ac88243-5f8a-42d5-9538-b344b38e3abf","codeChangeId":"1ac88243-5f8a-42d5-9538-b344b38e3abf","sourceType":"assistant"} --> <!--Please give us some feedback on your experience writing this PR ! https://app.datadoghq.com/forms/43db4c02-6837-400c-8083-692e141b1b88 !--> ### What does this PR do? - Adds a process-local atomic counter suffix to flare archive filenames. - Includes the process ID with the counter to keep the suffix readable and avoid same-second collisions with another local process. - Keeps the existing timestamp and log-level filename context. - Adds a deterministic regression test that verifies repeated archive names generated for the same timestamp do not collide. ### Motivation Windows fails `os.Rename` when the destination archive already exists. `TestAgentTaskFlareSourceAndTags` creates several flare archives inside the same test process, with second-precision filenames. Once every ~2k runs, subtests created flares exactly at the same second in time, colliding on the file name. A process-local atomic counter directly addresses those rapid same-process creations. Jira: FLREM-150 ### Describe how you validated your changes - Test passes on the CI Will keep monitoring for further failures. ### Additional Notes None. --- PR with help of Bits - [View session in Datadog](https://app.datadoghq.com/code/c5d0b2d1-7ec1-45c4-a402-380c09336a86) Co-authored-by: datadog-bits <263423550+datadog-bits@users.noreply.github.com> Co-authored-by: paola.ducolin <paola.ducolin@datadoghq.com>
1 parent 7ba1833 commit bcbf486

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

comp/core/flare/helpers/builder.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"regexp"
1717
"strings"
1818
"sync"
19+
"sync/atomic"
1920
"time"
2021

2122
"github.com/DataDog/datadog-agent/comp/core/flare/types"
@@ -31,6 +32,8 @@ const (
3132
filePerm = 0644
3233
)
3334

35+
var archiveNameCounter atomic.Uint64
36+
3437
func newBuilder(root string, hostname string, localFlare bool, flareArgs types.FlareArgs) (*builder, error) {
3538
fb := &builder{
3639
tmpDir: root,
@@ -144,7 +147,10 @@ type builder struct {
144147
}
145148

146149
func getArchiveName() string {
147-
t := time.Now().UTC()
150+
return getArchiveNameForTime(time.Now().UTC(), newArchiveNameID())
151+
}
152+
153+
func getArchiveNameForTime(t time.Time, uniqueSuffix string) string {
148154
timeString := strings.ReplaceAll(t.Format(time.RFC3339), ":", "-")
149155

150156
logLevel, err := log.GetLogLevel()
@@ -153,7 +159,11 @@ func getArchiveName() string {
153159
logLevelString = "-" + logLevel.String()
154160
}
155161

156-
return fmt.Sprintf("datadog-agent-%s%s.zip", timeString, logLevelString)
162+
return fmt.Sprintf("datadog-agent-%s-%s%s.zip", timeString, uniqueSuffix, logLevelString)
163+
}
164+
165+
func newArchiveNameID() string {
166+
return fmt.Sprintf("%d-%d", os.Getpid(), archiveNameCounter.Add(1))
157167
}
158168

159169
func (fb *builder) Save() (string, error) {

comp/core/flare/helpers/builder_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ func TestSave(t *testing.T) {
122122
}
123123
}
124124

125+
func TestGetArchiveNameIsUniqueWithinSameSecond(t *testing.T) {
126+
timestamp := time.Date(2026, time.August, 14, 12, 0, 0, 0, time.UTC)
127+
names := make(map[string]struct{})
128+
129+
for range 100 {
130+
name := getArchiveNameForTime(timestamp, newArchiveNameID())
131+
if _, found := names[name]; found {
132+
t.Fatalf("archive name %q was generated more than once", name)
133+
}
134+
names[name] = struct{}{}
135+
}
136+
}
137+
125138
func TestAddFileFromFunc(t *testing.T) {
126139
fb := getNewBuilder(t)
127140
defer fb.clean()

0 commit comments

Comments
 (0)