-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasrun.go
More file actions
326 lines (280 loc) · 8.45 KB
/
Copy pathasrun.go
File metadata and controls
326 lines (280 loc) · 8.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package playout
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
const defaultRingCapacity = 10000
// asRunWriteBuffer bounds the number of events queued for durable JSONL
// writing. Sized to absorb realistic bursts (pod-board fires, item
// boundaries) while a slow/stalled disk catches up. On sustained overflow the
// writer drops the oldest queued event rather than blocking the realtime tick
// goroutine.
const asRunWriteBuffer = 4096
// AsRunLoggerConfig configures the as-run event logger.
type AsRunLoggerConfig struct {
Dir string
RingCapacity int
RetentionDays int
}
// AsRunQuery filters as-run events for retrieval.
type AsRunQuery struct {
Type AsRunEventType
ChannelID *int
Since *time.Time
Until *time.Time
Limit int
}
// AsRunLogger writes as-run events to an in-memory ring buffer and
// append-only JSONL files for durable storage.
type AsRunLogger struct {
mu sync.Mutex
config AsRunLoggerConfig
ring []AsRunEvent
head int // next write position
count int // number of valid entries (up to cap)
stopRetention chan struct{}
// Durable JSONL writes are decoupled from the hot path: Log pushes events
// onto writeCh, which a dedicated writer goroutine drains and encodes to
// disk. This keeps a stalled disk (e.g. NFS as-run dir) off the realtime
// tick goroutine and avoids serializing concurrent loggers on file I/O.
// file/enc are owned exclusively by the writer goroutine after construction.
file *os.File
enc *json.Encoder
writeCh chan AsRunEvent
writerWG sync.WaitGroup
closeOnce sync.Once
}
// NewAsRunLogger creates a new as-run logger. It creates the output
// directory if needed and opens a date-stamped JSONL file for appending.
func NewAsRunLogger(cfg AsRunLoggerConfig) (*AsRunLogger, error) {
if cfg.RingCapacity <= 0 {
cfg.RingCapacity = defaultRingCapacity
}
if err := os.MkdirAll(cfg.Dir, 0o755); err != nil {
return nil, fmt.Errorf("asrun: create dir: %w", err)
}
filename := time.Now().UTC().Format("asrun-2006-01-02") + ".jsonl"
path := filepath.Join(cfg.Dir, filename)
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
if err != nil {
return nil, fmt.Errorf("asrun: open file: %w", err)
}
l := &AsRunLogger{
config: cfg,
ring: make([]AsRunEvent, cfg.RingCapacity),
file: f,
enc: json.NewEncoder(f),
writeCh: make(chan AsRunEvent, asRunWriteBuffer),
}
l.writerWG.Add(1)
// Pass the channel explicitly so the writer ranges over the real channel
// value rather than re-reading l.writeCh (which Close sets to nil).
go l.runWriter(l.writeCh)
return l, nil
}
// runWriter drains queued events and encodes them to the JSONL file. It owns
// l.file/l.enc and runs until ch is closed and drained (by Close).
func (l *AsRunLogger) runWriter(ch <-chan AsRunEvent) {
defer l.writerWG.Done()
for ev := range ch {
if err := l.enc.Encode(ev); err != nil {
slog.Error("asrun: write JSONL", "error", err)
}
}
}
// Log records an event in the in-memory ring buffer and queues it for durable
// JSONL writing. The ring update is synchronous; the file write is performed
// asynchronously by a dedicated writer goroutine so a stalled disk never blocks
// the realtime tick goroutine. If the write queue is saturated (sustained disk
// stall), the oldest queued event is dropped to keep Log non-blocking.
func (l *AsRunLogger) Log(ev AsRunEvent) {
l.mu.Lock()
defer l.mu.Unlock()
// Write to ring buffer.
l.ring[l.head] = ev
l.head = (l.head + 1) % len(l.ring)
if l.count < len(l.ring) {
l.count++
}
if l.writeCh == nil {
return // closed
}
// Queue for durable write. The send is non-blocking (buffered channel +
// select/default), so holding l.mu here never stalls on disk I/O — the
// writer goroutine performs the actual file write without the lock. Holding
// the lock also makes the send safe against Close closing the channel. On
// overflow, drop the oldest queued event and retry so the newest event is
// still written.
select {
case l.writeCh <- ev:
default:
select {
case dropped := <-l.writeCh:
slog.Warn("asrun: write queue full, dropping oldest event", "type", dropped.Type)
default:
}
select {
case l.writeCh <- ev:
default:
// Still full (other goroutines refilled it); drop this event.
slog.Warn("asrun: write queue full, dropping event", "type", ev.Type)
}
}
}
// Query returns events from the ring buffer matching the given filters,
// in chronological (oldest-first) order.
func (l *AsRunLogger) Query(q AsRunQuery) []AsRunEvent {
l.mu.Lock()
defer l.mu.Unlock()
var result []AsRunEvent
// Determine the start position for reading in chronological order.
start := 0
if l.count == len(l.ring) {
// Ring is full; oldest event is at head.
start = l.head
}
for i := 0; i < l.count; i++ {
idx := (start + i) % len(l.ring)
ev := l.ring[idx]
// Apply type filter.
if q.Type != "" && ev.Type != q.Type {
continue
}
// Apply channel filter.
if q.ChannelID != nil && ev.ChannelID != *q.ChannelID {
continue
}
// Apply time range filters.
if q.Since != nil && ev.Timestamp.Before(*q.Since) {
continue
}
if q.Until != nil && !ev.Timestamp.Before(*q.Until) {
continue
}
result = append(result, ev)
// Apply limit.
if q.Limit > 0 && len(result) >= q.Limit {
break
}
}
return result
}
// All returns all events in the ring buffer in chronological order.
func (l *AsRunLogger) All() []AsRunEvent {
return l.Query(AsRunQuery{})
}
// Reset clears the in-memory ring buffer. The on-disk JSONL file is
// unaffected (it is an append-only historical record).
func (l *AsRunLogger) Reset() {
l.mu.Lock()
defer l.mu.Unlock()
l.head = 0
l.count = 0
for i := range l.ring {
l.ring[i] = AsRunEvent{}
}
}
// CleanupOldFiles removes as-run JSONL files older than RetentionDays.
// Returns the number of files deleted. Returns 0 immediately if retention
// is disabled (RetentionDays <= 0).
func (l *AsRunLogger) CleanupOldFiles() int {
if l.config.RetentionDays <= 0 {
return 0
}
cutoff := time.Now().UTC().AddDate(0, 0, -l.config.RetentionDays)
entries, err := os.ReadDir(l.config.Dir)
if err != nil {
slog.Error("asrun: read dir for cleanup", "error", err)
return 0
}
deleted := 0
for _, entry := range entries {
if entry.IsDir() {
continue
}
name := entry.Name()
// Match pattern: asrun-YYYY-MM-DD.jsonl
if !strings.HasPrefix(name, "asrun-") || !strings.HasSuffix(name, ".jsonl") {
continue
}
dateStr := strings.TrimPrefix(name, "asrun-")
dateStr = strings.TrimSuffix(dateStr, ".jsonl")
fileDate, err := time.Parse("2006-01-02", dateStr)
if err != nil {
continue // not a valid date, skip
}
if fileDate.Before(cutoff) {
path := filepath.Join(l.config.Dir, name)
if err := os.Remove(path); err != nil {
slog.Error("asrun: remove old file", "path", path, "error", err)
continue
}
deleted++
}
}
return deleted
}
// StartRetention launches a background goroutine that periodically runs
// CleanupOldFiles. It runs cleanup immediately once, then on the given
// interval. If RetentionDays <= 0 or interval <= 0, defaults to 24 hours.
func (l *AsRunLogger) StartRetention(interval time.Duration) {
if l.config.RetentionDays <= 0 {
return
}
if interval <= 0 {
interval = 24 * time.Hour
}
l.stopRetention = make(chan struct{})
stop := l.stopRetention
// Run once immediately.
l.CleanupOldFiles()
go func() {
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-stop:
return
case <-ticker.C:
l.CleanupOldFiles()
}
}
}()
}
// Close flushes queued events, closes the JSONL file, and stops the retention
// goroutine. It waits for the writer goroutine to drain all queued events so
// durability is preserved (events logged before Close are written to disk).
// Safe to call more than once.
func (l *AsRunLogger) Close() error {
var err error
l.closeOnce.Do(func() {
l.mu.Lock()
if l.stopRetention != nil {
close(l.stopRetention)
l.stopRetention = nil
}
// Close the write channel under the lock so it cannot race a concurrent
// Log send; setting writeCh to nil first makes later Log calls no-op.
ch := l.writeCh
l.writeCh = nil
l.mu.Unlock()
if ch != nil {
close(ch)
}
// Wait outside the lock: the writer drains remaining events and exits.
l.writerWG.Wait()
l.mu.Lock()
if l.file != nil {
err = l.file.Close()
l.file = nil
}
l.mu.Unlock()
})
return err
}