-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathtoken_tracker.go
401 lines (337 loc) · 11.8 KB
/
token_tracker.go
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
Copyright 2024 The Aibrix Team.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vtc
import (
"container/list"
"context"
"fmt"
"math"
"sync"
"time"
"github.com/vllm-project/aibrix/pkg/utils"
)
// Sliding window configuration
const (
defaultTokenTrackerWindowSize = 5 // Default window size in the configured time units, example: 5 minutes
defaultTokenTrackerMinTokens = 1000.0 // Sensible min default value for adaptive token tracking(see vtc_basic)
defaultTokenTrackerMaxTokens = 8000.0 // Sensible max default value for adaptive token tracking(see vtc_basic)
defaultTimeUnit = "minutes"
)
const (
VTC_TOKEN_TRACKER_WINDOW_SIZE = "AIBRIX_ROUTER_VTC_TOKEN_TRACKER_WINDOW_SIZE"
VTC_TOKEN_TRACKER_TIME_UNIT = "AIBRIX_ROUTER_VTC_TOKEN_TRACKER_TIME_UNIT"
VTC_TOKEN_TRACKER_MIN_TOKENS = "AIBRIX_ROUTER_VTC_TOKEN_TRACKER_MIN_TOKENS"
VTC_TOKEN_TRACKER_MAX_TOKENS = "AIBRIX_ROUTER_VTC_TOKEN_TRACKER_MAX_TOKENS"
)
var (
tokenTrackerWindowSize = utils.LoadEnvInt(VTC_TOKEN_TRACKER_WINDOW_SIZE, defaultTokenTrackerWindowSize)
tokenTrackerMinTokens = utils.LoadEnvFloat(VTC_TOKEN_TRACKER_MIN_TOKENS, defaultTokenTrackerMinTokens)
tokenTrackerMaxTokens = utils.LoadEnvFloat(VTC_TOKEN_TRACKER_MAX_TOKENS, defaultTokenTrackerMaxTokens)
timeUnitStr = utils.LoadEnv(VTC_TOKEN_TRACKER_TIME_UNIT, defaultTimeUnit)
)
type TimeUnit int
const (
Minutes TimeUnit = iota
Seconds
Milliseconds
)
var timeUnitDuration = map[TimeUnit]time.Duration{
Minutes: time.Minute,
Seconds: time.Second,
Milliseconds: time.Millisecond,
}
func (unit TimeUnit) toTimestamp(t time.Time) int64 {
if unit == Milliseconds {
return t.UnixNano() / int64(time.Millisecond)
}
return t.Unix()
}
// bucketNode stores the data for a single time bucket in the linked list.
type bucketNode struct {
timestamp int64
tokenCount float64
}
// userBucketData holds the token tracking data structures for a single user.
type userBucketData struct {
buckets *list.List // Doubly linked list of *bucketNode, ordered by timestamp
lookup map[int64]*list.Element // Maps timestamp to list element for O(1) access
}
// InMemorySlidingWindowTokenTracker tracks tokens per user in a fixed-size sliding window (in-memory, thread-safe).
type InMemorySlidingWindowTokenTracker struct {
mu sync.RWMutex
windowSize time.Duration
bucketUnit TimeUnit
userBucketStore map[string]*userBucketData // Stores bucket list and lookup map per user
userTotals map[string]float64
// Efficient Min/Max Tracking
totalsToUsers map[float64]map[string]struct{} // total -> set of users
minTrackedToken float64
maxTrackedToken float64
config *VTCConfig
}
// TokenTrackerOption is a function that configures a token tracker
type TokenTrackerOption func(*InMemorySlidingWindowTokenTracker)
// updateWindowSize recalculates the window size based on time unit
func (t *InMemorySlidingWindowTokenTracker) updateWindowSize() {
// Set window size based on configured size and time unit
t.windowSize = time.Duration(tokenTrackerWindowSize) * timeUnitDuration[t.bucketUnit]
}
func WithWindowSize(size int) TokenTrackerOption {
return func(t *InMemorySlidingWindowTokenTracker) {
// Override the default window size with the provided value
tokenTrackerWindowSize = size
t.updateWindowSize()
}
}
func WithTimeUnit(unit TimeUnit) TokenTrackerOption {
return func(t *InMemorySlidingWindowTokenTracker) {
t.bucketUnit = unit
t.updateWindowSize()
}
}
// TODO: add redis token tracker so that state is shared across plugin instances
// NewInMemorySlidingWindowTokenTracker creates a new token tracker with configurable options
func NewInMemorySlidingWindowTokenTracker(config *VTCConfig, opts ...TokenTrackerOption) TokenTracker {
defaultUnit := Minutes
// Set default time unit from environment variable
switch timeUnitStr {
case "seconds":
defaultUnit = Seconds
case "milliseconds":
defaultUnit = Milliseconds
}
tracker := &InMemorySlidingWindowTokenTracker{
bucketUnit: defaultUnit,
userBucketStore: make(map[string]*userBucketData),
userTotals: make(map[string]float64),
totalsToUsers: make(map[float64]map[string]struct{}),
minTrackedToken: math.MaxFloat64, // Start high so first positive value becomes min
maxTrackedToken: 0.0, // Start with zero as default max
config: config,
}
for _, opt := range opts {
opt(tracker)
}
return tracker
}
func (t *InMemorySlidingWindowTokenTracker) getCutoffTimestamp() int64 {
cutoffTime := time.Now().Add(-t.windowSize)
return t.bucketUnit.toTimestamp(cutoffTime)
}
// Caller must hold the write lock
func (t *InMemorySlidingWindowTokenTracker) pruneExpiredBucketsAndUpdateState(user string, cutoff int64) {
_, ok := t.userBucketStore[user]
if !ok {
return // User doesn't exist or has no buckets
}
bucketsList := t.userBucketStore[user].buckets
lookupMap := t.userBucketStore[user].lookup
modified := false
oldTotal := t.userTotals[user]
newTotal := oldTotal
// Iterate from the front (oldest) of the list
for el := bucketsList.Front(); el != nil; {
node := el.Value.(*bucketNode)
if node.timestamp < cutoff {
// Remove expired bucket
newTotal -= node.tokenCount
delete(lookupMap, node.timestamp)
next := el.Next()
bucketsList.Remove(el)
el = next
modified = true
} else {
// Stop as soon as we find a non-expired bucket (list is ordered)
break
}
}
if modified {
// Update user total and min/max tracking with correct old and new values
t.updateUserTotalAndMinMax(user, oldTotal, newTotal)
}
}
// Time: Avg O(1) (amortized), Worst O(B_u) where B_u = buckets for user u | Space: O(1)
func (t *InMemorySlidingWindowTokenTracker) GetTokenCount(ctx context.Context, user string) (float64, error) {
t.mu.RLock()
if user == "" {
t.mu.RUnlock()
return 0, nil
}
_, ok := t.userBucketStore[user]
if !ok || t.userBucketStore[user].buckets.Len() == 0 {
t.mu.RUnlock()
// Return cached total if user exists but has no buckets currently, else 0
if ok {
return t.userTotals[user], nil
}
return 0, nil
}
total := t.userTotals[user]
cutoff := t.getCutoffTimestamp()
needsPruning := false
oldestElement := t.userBucketStore[user].buckets.Front()
if oldestElement != nil {
oldestNode := oldestElement.Value.(*bucketNode)
if oldestNode.timestamp < cutoff {
needsPruning = true
}
}
t.mu.RUnlock()
// Only acquire write lock if we need to prune
if needsPruning {
t.mu.Lock()
// Re-check user data existence in case it was deleted between RUnlock and Lock
_, ok = t.userBucketStore[user]
if ok {
t.pruneExpiredBucketsAndUpdateState(user, cutoff)
// Get the potentially updated total after pruning
total = t.userTotals[user]
}
t.mu.Unlock()
}
return total, nil
}
// Time: O(1) | Space: O(1)
func (t *InMemorySlidingWindowTokenTracker) GetMinTokenCount(ctx context.Context) (float64, error) {
t.mu.RLock()
defer t.mu.RUnlock()
// Return default min if no active users (minTrackedToken is still at initialization value)
if t.minTrackedToken == math.MaxFloat64 {
return tokenTrackerMinTokens, nil
}
return t.minTrackedToken, nil
}
// Time: O(1) | Space: O(1)
func (t *InMemorySlidingWindowTokenTracker) GetMaxTokenCount(ctx context.Context) (float64, error) {
t.mu.RLock()
defer t.mu.RUnlock()
// If no active users or all have zero tokens, return default max
if t.maxTrackedToken == 0 {
return tokenTrackerMaxTokens, nil
}
return t.maxTrackedToken, nil
}
// Time: Avg O(1) (amortized), Worst O(B_u) where B_u = buckets for user u | Space: O(1)
func (t *InMemorySlidingWindowTokenTracker) UpdateTokenCount(ctx context.Context, user string, inputTokens, outputTokens float64) error {
if user == "" {
return fmt.Errorf("user ID cannot be empty")
}
t.mu.Lock()
defer t.mu.Unlock()
now := time.Now()
currentTimestamp := t.bucketUnit.toTimestamp(now)
cutoff := t.getCutoffTimestamp()
_, ok := t.userBucketStore[user]
if !ok {
t.userBucketStore[user] = &userBucketData{
buckets: list.New(),
lookup: make(map[int64]*list.Element),
}
}
oldTotal := t.userTotals[user]
// Prune first before adding/updating to maintain window size constraint accurately
t.pruneExpiredBucketsAndUpdateState(user, cutoff)
totalAfterPruning := t.userTotals[user]
// Clamp negative tokens to zero
inputTokens = max(0, inputTokens)
outputTokens = max(0, outputTokens)
newTokens := inputTokens*t.config.InputTokenWeight + outputTokens*t.config.OutputTokenWeight
// Check if a bucket for the current timestamp already exists
if element, exists := t.userBucketStore[user].lookup[currentTimestamp]; exists {
node := element.Value.(*bucketNode)
node.tokenCount += newTokens
} else {
node := &bucketNode{timestamp: currentTimestamp, tokenCount: newTokens}
element := t.userBucketStore[user].buckets.PushBack(node)
t.userBucketStore[user].lookup[currentTimestamp] = element
}
// Calculate the final new total
newTotal := totalAfterPruning + newTokens
// Update user total and global min/max efficiently
t.updateUserTotalAndMinMax(user, oldTotal, newTotal)
return nil
}
// Caller must hold the write lock
func (t *InMemorySlidingWindowTokenTracker) updateUserTotalAndMinMax(user string, oldTotal, newTotal float64) {
if oldTotal == newTotal {
return
}
// Negative totals should be treated as 0
newTotal = max(0, newTotal)
t.userTotals[user] = newTotal
t.removeFromTotals(oldTotal, user)
t.addToTotals(newTotal, user)
t.recalcMin(oldTotal, newTotal)
t.recalcMax(oldTotal, newTotal)
}
// Caller must hold the write lock
func (t *InMemorySlidingWindowTokenTracker) removeFromTotals(oldTotal float64, user string) {
if oldTotal <= 0 {
return
}
if users, ok := t.totalsToUsers[oldTotal]; ok {
delete(users, user)
if len(users) == 0 {
delete(t.totalsToUsers, oldTotal)
}
}
}
// Caller must hold the write lock
func (t *InMemorySlidingWindowTokenTracker) addToTotals(newTotal float64, user string) {
if newTotal <= 0 {
return
}
if _, ok := t.totalsToUsers[newTotal]; !ok {
t.totalsToUsers[newTotal] = make(map[string]struct{})
}
t.totalsToUsers[newTotal][user] = struct{}{}
}
// Caller must hold the write lock
func (t *InMemorySlidingWindowTokenTracker) wasLastUserAtBoundary(value float64, userTotal float64) bool {
// Returns true if userTotal matches the boundary value and no users remain at userTotal.
return userTotal > 0 && userTotal == value && len(t.totalsToUsers[userTotal]) == 0
}
// Caller must hold the write lock
func (t *InMemorySlidingWindowTokenTracker) recalcMin(oldTotal, newTotal float64) {
if t.wasLastUserAtBoundary(t.minTrackedToken, oldTotal) {
// Find new minimum.
newMin := math.MaxFloat64
for total := range t.totalsToUsers {
// keys in totalsToUsers are guaranteed > 0 by addToTotals
if total < newMin {
newMin = total
}
}
t.minTrackedToken = newMin
} else if newTotal > 0 && newTotal < t.minTrackedToken {
t.minTrackedToken = newTotal
}
// Ensure minTrackedToken is MaxFloat64 if no users have positive totals
if len(t.totalsToUsers) == 0 {
t.minTrackedToken = math.MaxFloat64
}
}
// Caller must hold the write lock
func (t *InMemorySlidingWindowTokenTracker) recalcMax(oldTotal, newTotal float64) {
if t.wasLastUserAtBoundary(t.maxTrackedToken, oldTotal) {
// Find new maximum
t.maxTrackedToken = 0
for total := range t.totalsToUsers {
if total > t.maxTrackedToken {
t.maxTrackedToken = total
}
}
} else if newTotal > t.maxTrackedToken {
t.maxTrackedToken = newTotal
}
}