Skip to content

Commit 757ca13

Browse files
committed
Make rotated log filenames collision-free with a per-stream sequence
Rotated filenames used a millisecond timestamp, so two rotations within the same millisecond (observed on fast CI) produced identical names. Append a per-stream incrementing sequence to each rotated filename to guarantee uniqueness. Fixes the intermittent test_size_triggers_rotation failure.
1 parent ec9aa7b commit 757ca13

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

src/tracer/WriterManager.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ def __init__(self, output_dir: str, upload_manager: ObjectStorageManager, automa
199199
self.max_file_age = 20 * 60 # 20 minutes
200200
self.max_file_bytes = 100 * 1024 * 1024 # 100 MB (uncompressed on disk)
201201
self._stream_opened = {key: time.monotonic() for key in self._streams}
202+
# Per-stream rotation sequence, appended to each rotated filename so two
203+
# rotations in the same millisecond can't collide on the timestamp.
204+
self._stream_seq = {key: 0 for key in self._streams}
202205

203206
# Cache sampling configuration
204207
self.cache_sample_rate = 1 # Can be increased to reduce cache event volume
@@ -745,9 +748,11 @@ def _rotate_stream(self, key: str):
745748
return
746749

747750
rotated = cur_file
751+
self._stream_seq[key] += 1
748752
new_file = (
749753
f"{self.output_dir}/{s['subdir']}/{s['prefix']}_"
750-
f"{datetime.now().strftime('%Y%m%d_%H%M%S_%f')[:-3]}.csv"
754+
f"{datetime.now().strftime('%Y%m%d_%H%M%S_%f')[:-3]}_"
755+
f"{self._stream_seq[key]:04d}.csv"
751756
)
752757
setattr(self, s['file'], new_file)
753758
setattr(self, s['handle'], open(new_file, 'a', buffering=8192))

0 commit comments

Comments
 (0)