summaryrefslogtreecommitdiff
path: root/internal/providerhealth/redis_history.go
blob: 322acce08bcefe6c17fc841a532b00bc136a5cf3 (plain)
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package providerhealth

import (
	"context"
	"encoding/json"
	"errors"
	"log/slog"
	"strings"
	"sync"
	"sync/atomic"
	"time"

	"github.com/redis/go-redis/v9"
)

const (
	defaultHistoryStream    = "aigw:provider-health:events"
	defaultHistoryQueueSize = 4096
	defaultHistoryMaxEvents = 20_000
	defaultHistoryTTL       = 15 * time.Minute
	historyPublishBatchSize = 64
	historyCommandTimeout   = 500 * time.Millisecond
	historyRetryCooldown    = 5 * time.Second
)

type HistoryOptions struct {
	Enabled   bool
	RedisURL  string
	Stream    string
	Instance  string
	QueueSize int
	MaxEvents int64
	TTL       time.Duration
	Logger    *slog.Logger
	Client    *redis.Client
	Metrics   SharedHistoryMetrics
}

type SharedHistoryMetrics interface {
	ProviderHealthSharedPublished()
	ProviderHealthSharedImported()
	ProviderHealthSharedDropped()
	ProviderHealthSharedRedisFailure()
	ProviderHealthSharedConnected(bool)
}

type RedisHistory struct {
	redis       *redis.Client
	ownedClient bool
	stream      string
	instance    string
	queue       chan Event
	maxEvents   int64
	ttl         time.Duration
	logger      *slog.Logger
	metrics     SharedHistoryMetrics
	retryAt     atomic.Int64
	dropped     atomic.Uint64
	closed      atomic.Bool
	failureSeen atomic.Bool
}

func NewRedisHistory(options HistoryOptions) *RedisHistory {
	if !options.Enabled || (strings.TrimSpace(options.RedisURL) == "" && options.Client == nil) {
		return nil
	}
	if options.Logger == nil {
		options.Logger = slog.Default()
	}
	if options.Stream == "" {
		options.Stream = defaultHistoryStream
	}
	if options.QueueSize <= 0 {
		options.QueueSize = defaultHistoryQueueSize
	}
	if options.MaxEvents <= 0 {
		options.MaxEvents = defaultHistoryMaxEvents
	}
	if options.TTL <= 0 {
		options.TTL = defaultHistoryTTL
	}
	client := options.Client
	ownedClient := false
	if client == nil {
		redisOptions, err := redis.ParseURL(options.RedisURL)
		if err != nil {
			options.Logger.Warn("provider_health_redis_config_invalid", "error", err, "fallback", "local")
			return nil
		}
		redisOptions.MaxRetries = -1
		redisOptions.DialerRetries = 1
		redisOptions.DialTimeout = historyCommandTimeout
		redisOptions.ReadTimeout = historyCommandTimeout
		redisOptions.WriteTimeout = historyCommandTimeout
		redisOptions.PoolTimeout = historyCommandTimeout
		client = redis.NewClient(redisOptions)
		ownedClient = true
	}
	return &RedisHistory{redis: client, ownedClient: ownedClient, stream: options.Stream, instance: options.Instance,
		queue: make(chan Event, options.QueueSize), maxEvents: options.MaxEvents, ttl: options.TTL,
		logger: options.Logger, metrics: options.Metrics}
}

func (h *RedisHistory) Enqueue(event Event) {
	if h == nil || h.closed.Load() {
		return
	}
	select {
	case h.queue <- event:
	default:
		if h.recordDropped(1) == 1 {
			h.logger.Warn("provider_health_share_queue_full", "fallback", "local")
		}
	}
}

func (h *RedisHistory) Run(ctx context.Context, tracker *Tracker) {
	if h == nil || tracker == nil {
		return
	}
	var workers sync.WaitGroup
	workers.Add(2)
	go func() {
		defer workers.Done()
		h.runPublisher(ctx)
	}()
	go func() {
		defer workers.Done()
		h.runReader(ctx, tracker)
	}()
	workers.Wait()
}

func (h *RedisHistory) runPublisher(ctx context.Context) {
	batch := make([]Event, 0, historyPublishBatchSize)
	for {
		select {
		case <-ctx.Done():
			return
		case event := <-h.queue:
			batch = append(batch[:0], event)
		}
		for len(batch) < historyPublishBatchSize {
			select {
			case event := <-h.queue:
				batch = append(batch, event)
			default:
				h.publishBatch(ctx, batch)
				batch = batch[:0]
				goto nextBatch
			}
		}
		h.publishBatch(ctx, batch)
		batch = batch[:0]
	nextBatch:
	}
}

func (h *RedisHistory) runReader(ctx context.Context, tracker *Tracker) {
	lastID := h.importRecent(ctx, tracker)
	if lastID == "" {
		lastID = "0-0"
	}
	for ctx.Err() == nil {
		if time.Now().UnixNano() < h.retryAt.Load() {
			if !waitHistoryRetry(ctx, 100*time.Millisecond) {
				return
			}
			continue
		}
		lastID = h.read(ctx, tracker, lastID)
	}
}

func (h *RedisHistory) Close() error {
	if h == nil || !h.closed.CompareAndSwap(false, true) || !h.ownedClient {
		return nil
	}
	return h.redis.Close()
}

func (h *RedisHistory) Dropped() uint64 {
	if h == nil {
		return 0
	}
	return h.dropped.Load()
}

func (h *RedisHistory) publishBatch(parent context.Context, events []Event) {
	if len(events) == 0 {
		return
	}
	if time.Now().UnixNano() < h.retryAt.Load() {
		h.recordDropped(uint64(len(events)))
		return
	}
	ctx, cancel := context.WithTimeout(parent, historyCommandTimeout)
	defer cancel()
	pipe := h.redis.Pipeline()
	published := 0
	for _, event := range events {
		payload, err := json.Marshal(event)
		if err != nil {
			h.recordDropped(1)
			continue
		}
		pipe.XAdd(ctx, &redis.XAddArgs{Stream: h.stream, MaxLen: h.maxEvents, Approx: true,
			Values: map[string]any{"instance": h.instance, "event": string(payload)}})
		published++
	}
	if published == 0 {
		pipe.Discard()
		return
	}
	pipe.Expire(ctx, h.stream, h.ttl)
	if _, err := pipe.Exec(ctx); err != nil {
		h.recordDropped(uint64(published))
		h.fail(err)
		return
	}
	h.recovered()
	for range published {
		if h.metrics != nil {
			h.metrics.ProviderHealthSharedPublished()
		}
	}
}

func (h *RedisHistory) importRecent(parent context.Context, tracker *Tracker) string {
	ctx, cancel := context.WithTimeout(parent, historyCommandTimeout)
	defer cancel()
	messages, err := h.redis.XRevRangeN(ctx, h.stream, "+", "-", h.maxEvents).Result()
	if err != nil {
		if !errors.Is(err, redis.Nil) {
			h.fail(err)
		}
		return ""
	}
	if len(messages) == 0 {
		h.recovered()
		return ""
	}
	lastID := messages[0].ID
	for index := len(messages) - 1; index >= 0; index-- {
		h.applyMessage(tracker, messages[index])
	}
	h.recovered()
	return lastID
}

func (h *RedisHistory) read(parent context.Context, tracker *Tracker, lastID string) string {
	if time.Now().UnixNano() < h.retryAt.Load() {
		return lastID
	}
	ctx, cancel := context.WithTimeout(parent, historyCommandTimeout)
	defer cancel()
	streams, err := h.redis.XRead(ctx, &redis.XReadArgs{Streams: []string{h.stream, lastID}, Count: 512, Block: 200 * time.Millisecond}).Result()
	if errors.Is(err, redis.Nil) {
		h.recovered()
		return lastID
	}
	if err != nil {
		h.fail(err)
		return lastID
	}
	h.recovered()
	for _, stream := range streams {
		for _, message := range stream.Messages {
			h.applyMessage(tracker, message)
			lastID = message.ID
		}
	}
	return lastID
}

func (h *RedisHistory) applyMessage(tracker *Tracker, message redis.XMessage) {
	if asString(message.Values["instance"]) == h.instance && h.instance != "" {
		return
	}
	payload := asString(message.Values["event"])
	var event Event
	if payload == "" || json.Unmarshal([]byte(payload), &event) != nil {
		return
	}
	if event.ObservedAt.Before(time.Now().Add(-h.ttl)) {
		return
	}
	tracker.ApplyShared(event)
	if h.metrics != nil {
		h.metrics.ProviderHealthSharedImported()
	}
}

func (h *RedisHistory) fail(err error) {
	h.retryAt.Store(time.Now().Add(historyRetryCooldown).UnixNano())
	if h.metrics != nil {
		h.metrics.ProviderHealthSharedConnected(false)
	}
	if h.failureSeen.CompareAndSwap(false, true) {
		if h.metrics != nil {
			h.metrics.ProviderHealthSharedRedisFailure()
		}
		h.logger.Warn("provider_health_redis_unavailable", "error", err, "fallback", "local", "retry_after", historyRetryCooldown)
	}
}

func (h *RedisHistory) recovered() {
	h.retryAt.Store(0)
	if h.metrics != nil {
		h.metrics.ProviderHealthSharedConnected(true)
	}
	if h.failureSeen.Swap(false) {
		h.logger.Info("provider_health_redis_recovered")
	}
}

func (h *RedisHistory) recordDropped(count uint64) uint64 {
	total := h.dropped.Add(count)
	if h.metrics != nil {
		for range count {
			h.metrics.ProviderHealthSharedDropped()
		}
	}
	return total
}

func waitHistoryRetry(ctx context.Context, delay time.Duration) bool {
	timer := time.NewTimer(delay)
	defer timer.Stop()
	select {
	case <-ctx.Done():
		return false
	case <-timer.C:
		return true
	}
}

func asString(value any) string {
	switch item := value.(type) {
	case string:
		return item
	case []byte:
		return string(item)
	default:
		return ""
	}
}