Skip to content

Commit 1fa3803

Browse files
RishabhSainibongwoobakvMaroon
authored
fix: clear KV block index entries on endpoint removal (#1738)
* fix: clear KV block index entries on endpoint removal On EventDelete, call Index.Clear with the pod's IP address to remove stale entries that reference blocks no longer cached on the removed pod. Fixes #1736 Signed-off-by: RishabhSaini <rishabhsaini01@gmail.com> * Update pkg/epp/framework/plugins/requestcontrol/dataproducer/preciseprefixcache/extractor.go Co-authored-by: Bongwoo Bak <bongwoobak@gmail.com> Signed-off-by: Rishabh Saini <rishabhsaini01@gmail.com> * fix(test): match Clear assertion to IP:Port PodIdentifier format Signed-off-by: RishabhSaini <rishabhsaini01@gmail.com> * Update pkg/epp/framework/plugins/requestcontrol/dataproducer/preciseprefixcache/extractor.go Co-authored-by: Maroon Ayoub <Maroonay@gmail.com> Signed-off-by: Rishabh Saini <rishabhsaini01@gmail.com> * Update pkg/epp/framework/plugins/requestcontrol/dataproducer/preciseprefixcache/extractor.go Co-authored-by: Maroon Ayoub <Maroonay@gmail.com> Signed-off-by: Rishabh Saini <rishabhsaini01@gmail.com> --------- Signed-off-by: RishabhSaini <rishabhsaini01@gmail.com> Signed-off-by: Rishabh Saini <rishabhsaini01@gmail.com> Co-authored-by: Bongwoo Bak <bongwoobak@gmail.com> Co-authored-by: Maroon Ayoub <Maroonay@gmail.com>
1 parent bcbca9a commit 1fa3803

3 files changed

Lines changed: 59 additions & 3 deletions

File tree

pkg/epp/framework/plugins/requestcontrol/dataproducer/preciseprefixcache/extractor.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ func (p *Producer) Extract(ctx context.Context, event fwkdl.EndpointEvent) error
5252
logger.V(logging.DEBUG).Info("Adding subscriber", "endpoint", endpointKey)
5353
case fwkdl.EventDelete:
5454
p.subscribersManager.RemoveSubscriber(ctx, endpointKey)
55+
if meta.Address != "" {
56+
if err := p.kvCacheIndexer.KVBlockIndex().Clear(ctx, fmt.Sprintf("%s:%s", meta.Address, meta.Port)); err != nil {
57+
logger.Error(err, "Failed to clear index entries for removed endpoint",
58+
"endpoint", endpointKey, "address", meta.Address, "port", meta.Port)
59+
}
60+
}
5561
logger.V(logging.DEBUG).Info("Removed KV-events subscriber", "endpoint", endpointKey)
5662
}
5763
return nil

pkg/epp/framework/plugins/requestcontrol/dataproducer/preciseprefixcache/extractor_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func newExtractorProducer(discoverPods bool) *Producer {
4949
typedName: plugin.TypedName{Type: PluginType, Name: PluginType},
5050
subscribersManager: kvevents.NewSubscriberManager(kvevents.NewPool(cfg, nil, nil, nil)),
5151
kvEventsConfig: cfg,
52+
kvCacheIndexer: &fakeKVCacheIndexer{index: &fakeKVBlockIndex{}},
5253
subscriberCtx: context.Background(),
5354
}
5455
}
@@ -216,6 +217,51 @@ func TestProducer_ExtractEndpoint_SingleRankUsesBaseSocketPort(t *testing.T) {
216217
"single-rank pod (RankIndex=0) must dial the base SocketPort")
217218
}
218219

220+
// EventDelete clears index entries for the removed pod's address.
221+
func TestProducer_ExtractEndpoint_DeleteClearsIndex(t *testing.T) {
222+
ctx := discardCtx(t)
223+
224+
var clearedPod string
225+
fakeIndex := &fakeKVBlockIndex{
226+
clearFn: func(_ context.Context, podIdentifier string) error {
227+
clearedPod = podIdentifier
228+
return nil
229+
},
230+
}
231+
fakeIndexer := &fakeKVCacheIndexer{index: fakeIndex}
232+
233+
cfg := kvevents.DefaultConfig()
234+
cfg.DiscoverPods = true
235+
cfg.PodDiscoveryConfig = kvevents.DefaultPodReconcilerConfig()
236+
cfg.PodDiscoveryConfig.SocketPort = 5557
237+
238+
p := &Producer{
239+
typedName: plugin.TypedName{Type: PluginType, Name: PluginType},
240+
subscribersManager: kvevents.NewSubscriberManager(kvevents.NewPool(cfg, nil, nil, nil)),
241+
kvEventsConfig: cfg,
242+
kvCacheIndexer: fakeIndexer,
243+
subscriberCtx: context.Background(),
244+
}
245+
defer p.subscribersManager.Shutdown(ctx)
246+
247+
ep := newEndpoint("pod-clear", "10.0.0.99")
248+
249+
require.NoError(t, p.Extract(ctx, fwkdl.EndpointEvent{
250+
Type: fwkdl.EventAddOrUpdate,
251+
Endpoint: ep,
252+
}))
253+
254+
require.NoError(t, p.Extract(ctx, fwkdl.EndpointEvent{
255+
Type: fwkdl.EventDelete,
256+
Endpoint: ep,
257+
}))
258+
259+
assert.Equal(t, "10.0.0.99:8080", clearedPod, "index should be cleared using pod IP:Port matching PodIdentifier format")
260+
261+
ids, _ := p.subscribersManager.GetActiveSubscribers()
262+
assert.Empty(t, ids)
263+
}
264+
219265
// Delete by NamespacedName must work even when the event has no address.
220266
func TestProducer_ExtractEndpoint_DeleteWithMissingAddressRemovesExistingSubscriber(t *testing.T) {
221267
ctx := discardCtx(t)

pkg/epp/framework/plugins/requestcontrol/dataproducer/preciseprefixcache/producer_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ func (f *fakeKVCacheIndexer) ComputeBlockKeysFromTokens(ctx context.Context, tok
5252
func (f *fakeKVCacheIndexer) KVBlockIndex() kvblock.Index { return f.index }
5353

5454
type fakeKVBlockIndex struct {
55-
lookup func(ctx context.Context, keys []kvblock.BlockHash, podSet sets.Set[string]) (map[kvblock.BlockHash][]kvblock.PodEntry, error)
56-
addFn func(ctx context.Context, prevKeys, keys []kvblock.BlockHash, entries []kvblock.PodEntry) error
55+
lookup func(ctx context.Context, keys []kvblock.BlockHash, podSet sets.Set[string]) (map[kvblock.BlockHash][]kvblock.PodEntry, error)
56+
addFn func(ctx context.Context, prevKeys, keys []kvblock.BlockHash, entries []kvblock.PodEntry) error
57+
clearFn func(ctx context.Context, podIdentifier string) error
5758
}
5859

5960
func (f *fakeKVBlockIndex) Lookup(ctx context.Context, keys []kvblock.BlockHash, podSet sets.Set[string]) (map[kvblock.BlockHash][]kvblock.PodEntry, error) {
@@ -78,7 +79,10 @@ func (f *fakeKVBlockIndex) GetRequestKey(_ context.Context, _ kvblock.BlockHash)
7879
return kvblock.EmptyBlockHash, nil
7980
}
8081

81-
func (f *fakeKVBlockIndex) Clear(_ context.Context, _ string) error {
82+
func (f *fakeKVBlockIndex) Clear(ctx context.Context, podIdentifier string) error {
83+
if f.clearFn != nil {
84+
return f.clearFn(ctx, podIdentifier)
85+
}
8286
return nil
8387
}
8488

0 commit comments

Comments
 (0)