9
9
"k8s.io/apimachinery/pkg/types"
10
10
"sigs.k8s.io/controller-runtime/pkg/log"
11
11
errutil "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/error"
12
+ "sigs.k8s.io/gateway-api-inference-extension/pkg/epp/util/logging"
12
13
)
13
14
14
15
// PrefixEntry represents a single entry in the prefix store
@@ -35,6 +36,8 @@ type PrefixStore struct {
35
36
36
37
// NewPrefixStore creates a new PrefixStore with the given configuration
37
38
func NewPrefixStore (config PrefixStoreConfig ) * PrefixStore {
39
+ logger := log .FromContext (context .Background ())
40
+ logger .V (logging .DEBUG ).Info ("Creating new PrefixStore" , "config" , config )
38
41
return & PrefixStore {
39
42
tree : radix .New (),
40
43
config : config ,
@@ -56,13 +59,15 @@ func (ps *PrefixStore) AddPrefix(ctx context.Context, prefix string, pod types.N
56
59
}
57
60
}
58
61
if len (prefix ) > ps .config .MaxPrefixLen {
62
+ logger .V (logging .DEBUG ).Info ("Truncating prefix" , "originalLength" , len (prefix ), "maxLength" , ps .config .MaxPrefixLen )
59
63
prefix = prefix [:ps .config .MaxPrefixLen ]
60
64
}
61
65
62
66
// Check if we're updating an existing entry
63
67
if val , exists := ps .tree .Get (prefix ); exists {
64
68
entry := val .(* PrefixEntry )
65
69
if entry .PodRef == pod && entry .ModelName == modelName {
70
+ logger .V (logging .DEBUG ).Info ("Updating existing entry" , "prefix" , prefix , "pod" , pod .String ())
66
71
entry .LastUsed = time .Now ()
67
72
ps .tree .Insert (prefix , entry )
68
73
return nil
@@ -71,6 +76,7 @@ func (ps *PrefixStore) AddPrefix(ctx context.Context, prefix string, pod types.N
71
76
72
77
// Check total entries limit
73
78
if ps .tree .Len () >= ps .config .MaxEntries {
79
+ logger .V (logging .DEBUG ).Info ("Store at capacity, evicting oldest entry" , "currentSize" , ps .tree .Len (), "maxSize" , ps .config .MaxEntries )
74
80
ps .evictOldest ()
75
81
}
76
82
@@ -82,7 +88,7 @@ func (ps *PrefixStore) AddPrefix(ctx context.Context, prefix string, pod types.N
82
88
}
83
89
ps .tree .Insert (prefix , entry )
84
90
85
- logger .Info ("Added prefix entry" , "prefix" , prefix , "pod" , pod .String (), "model" , modelName )
91
+ logger .V ( logging . DEBUG ). Info ("Successfully added new prefix entry" , "prefix" , prefix , "pod" , pod .String (), "model" , modelName , "totalEntries" , ps . tree . Len () )
86
92
return nil
87
93
}
88
94
@@ -94,32 +100,36 @@ func (ps *PrefixStore) FindPodForPrefix(ctx context.Context, prefix string, mode
94
100
logger := log .FromContext (ctx )
95
101
96
102
if len (prefix ) < ps .config .MinPrefixLen {
103
+ logger .V (logging .DEBUG ).Info ("Prefix too short" , "prefix" , prefix , "minLength" , ps .config .MinPrefixLen )
97
104
return types.NamespacedName {}, false
98
105
}
99
106
100
107
if len (prefix ) > ps .config .MaxPrefixLen {
108
+ logger .V (logging .DEBUG ).Info ("Truncating prefix" , "originalLength" , len (prefix ), "maxLength" , ps .config .MaxPrefixLen )
101
109
prefix = prefix [:ps .config .MaxPrefixLen ]
102
110
}
103
111
104
112
// Use LongestPrefix to find the best match
105
113
matchedPrefix , val , found := ps .tree .LongestPrefix (prefix )
106
114
if ! found {
115
+ logger .V (logging .DEBUG ).Info ("No matching prefix found" , "prefix" , prefix )
107
116
return types.NamespacedName {}, false
108
117
}
109
118
110
119
entry := val .(* PrefixEntry )
111
120
112
121
// Check if entry has expired or model doesn't match
113
- if time .Since (entry .LastUsed ) > ps .config .EntryTTL || entry .ModelName != modelName {
114
- // Don't remove here to avoid write lock
122
+ if time .Since (entry .LastUsed ) > ps .config .EntryTTL {
123
+ return types.NamespacedName {}, false
124
+ }
125
+ if entry .ModelName != modelName {
115
126
return types.NamespacedName {}, false
116
127
}
117
128
118
129
// Update LastUsed time for the matched entry
119
130
entry .LastUsed = time .Now ()
120
131
ps .tree .Insert (matchedPrefix , entry )
121
132
122
- logger .Info ("Found pod for prefix" , "prefix" , prefix , "matchedPrefix" , matchedPrefix , "pod" , entry .PodRef .String (), "model" , modelName )
123
133
return entry .PodRef , true
124
134
}
125
135
@@ -169,7 +179,9 @@ func (ps *PrefixStore) cleanupExpired(ctx context.Context) {
169
179
}
170
180
171
181
if len (keysToDelete ) > 0 {
172
- logger .Info ("Cleaned up expired entries" , "count" , len (keysToDelete ))
182
+ logger .V (logging .DEBUG ).Info ("Cleaned up expired entries" , "count" , len (keysToDelete ), "remainingEntries" , ps .tree .Len ())
183
+ } else {
184
+ logger .V (logging .DEBUG ).Info ("No expired entries found" , "totalEntries" , ps .tree .Len ())
173
185
}
174
186
}
175
187
@@ -179,14 +191,14 @@ func (ps *PrefixStore) RunMaintenance(ctx context.Context) {
179
191
ticker := time .NewTicker (ps .config .EntryTTL / 2 )
180
192
defer ticker .Stop ()
181
193
194
+ logger .V (logging .DEBUG ).Info ("Starting maintenance routine" , "interval" , ps .config .EntryTTL / 2 )
195
+
182
196
for {
183
197
select {
184
198
case <- ctx .Done ():
185
- logger .Info ("Maintenance routine stopping" )
186
199
return
187
200
case <- ticker .C :
188
201
ps .cleanupExpired (ctx )
189
- logger .Info ("Completed maintenance cycle" )
190
202
}
191
203
}
192
204
}
0 commit comments