-
Notifications
You must be signed in to change notification settings - Fork 84
Handle WebSub delta updates without full undeploy/redeploy #1935
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
senthuran16
wants to merge
18
commits into
main
Choose a base branch
from
websub-kafka-improvements-undeploy-redeploy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
68e17d1
Fix websub undeploy, update gateway websub handler to use websub serv…
AnujaKalahara99 d2c5854
Fix Websub api delta update on event-gateway
AnujaKalahara99 726229f
Confirm that ApplyBindingDelta must synchronize mutations of e.channe…
AnujaKalahara99 5db29b3
Release r.mu before broker-driver and receiver operations
AnujaKalahara99 cda94bc
Build the replacement policy chains before applying the live delta
AnujaKalahara99 032a375
Use the configured subscription-sync topic helper in UpdateWebsubBinding
AnujaKalahara99 0e9ab55
Return immediately after the 404 response
AnujaKalahara99 e40edf8
Preserve the existing handle when metadata.name is omitted
AnujaKalahara99 b3beed5
Fix possibility of rendered WebSub config beign written back to storage
AnujaKalahara99 00832a9
Merge pull request #1934 from AnujaKalahara99/egw/fix-undeploy
senthuran16 e67c99c
Fixed Handler Test
AnujaKalahara99 f96e37c
Merge pull request #1936 from AnujaKalahara99/egw/fix-undeploy
senthuran16 70efaf8
Multiple Fix of Tombstone publish failure aborts mid-delta and can le…
AnujaKalahara99 070db12
UNSUBSCRIBE route keys are missing from both active-key construction …
AnujaKalahara99 4bf3acc
Fix Int parsing reflect.DeepEqual on BrokerDriverSpec.Config
AnujaKalahara99 7f32362
Fix Side effects occur before the post-lock snapshot guard
AnujaKalahara99 ea9f6cf
Use r.runCtx for ApplyBindingDelta instead of context.Background in R…
AnujaKalahara99 1019e9f
Merge pull request #1937 from AnujaKalahara99/egw/fix-undeploy
senthuran16 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
event-gateway/gateway-runtime/internal/connectors/receiver/websub/delta.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you 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 websub | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "log/slog" | ||
| ) | ||
|
|
||
| // ApplyBindingDelta mutates the live receiver for channel add/remove changes | ||
| // without recreating the receiver or the subscription sync topic. | ||
| func (e *WebSubReceiver) ApplyBindingDelta(ctx context.Context, removedChannels map[string]string, addedChannels map[string]string) error { | ||
| var tombstoneErrs []error | ||
|
|
||
| for channelName := range removedChannels { | ||
| subscriptions := e.store.GetByTopic(channelName) | ||
| for _, sub := range subscriptions { | ||
| if e.syncProducer != nil { | ||
| if err := e.syncProducer.PublishTombstone(ctx, channelName, sub.CallbackURL); err != nil { | ||
| slog.Error("Failed to tombstone subscription for removed WebSub channel", | ||
| "api", e.channel.Name, | ||
| "channel", channelName, | ||
| "callback", sub.CallbackURL, | ||
| "error", err) | ||
| tombstoneErrs = append(tombstoneErrs, | ||
| fmt.Errorf("failed to tombstone subscription for removed channel %q callback %q: %w", channelName, sub.CallbackURL, err)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for channelName, kafkaTopic := range removedChannels { | ||
| e.topics.Deregister(channelName) | ||
|
|
||
| subscriptions := e.store.GetByTopic(channelName) | ||
| for _, sub := range subscriptions { | ||
| if err := e.consumerMgr.RemoveSubscription(sub.CallbackURL, kafkaTopic); err != nil { | ||
| slog.Error("Failed to remove consumer for deleted WebSub channel", | ||
| "api", e.channel.Name, | ||
| "channel", channelName, | ||
| "callback", sub.CallbackURL, | ||
| "error", err) | ||
| } | ||
| if err := e.store.Remove(channelName, sub.CallbackURL); err != nil { | ||
| slog.Error("Failed to remove subscription for deleted WebSub channel", | ||
| "api", e.channel.Name, | ||
| "channel", channelName, | ||
| "callback", sub.CallbackURL, | ||
| "error", err) | ||
| } | ||
| } | ||
|
|
||
| e.channelMu.Lock() | ||
| delete(e.channel.Channels, channelName) | ||
| e.channelMu.Unlock() | ||
| } | ||
|
|
||
| for channelName, kafkaTopic := range addedChannels { | ||
| e.channelMu.Lock() | ||
| e.channel.Channels[channelName] = kafkaTopic | ||
| e.channelMu.Unlock() | ||
| e.topics.Register(channelName) | ||
| } | ||
|
|
||
| if len(tombstoneErrs) > 0 { | ||
| return errors.Join(tombstoneErrs...) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
162 changes: 162 additions & 0 deletions
162
event-gateway/gateway-runtime/internal/connectors/receiver/websub/delta_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| package websub | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| "github.com/wso2/api-platform/event-gateway/gateway-runtime/internal/connectors" | ||
| "github.com/wso2/api-platform/event-gateway/gateway-runtime/internal/subscription" | ||
| ) | ||
|
|
||
| type deltaTestBrokerDriver struct { | ||
| publishErrs map[string]error | ||
| published []string | ||
| } | ||
|
|
||
| func (d *deltaTestBrokerDriver) Publish(_ context.Context, topic string, msg *connectors.Message) error { | ||
| key := string(msg.Key) | ||
| d.published = append(d.published, key) | ||
| if err, ok := d.publishErrs[key]; ok { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (d *deltaTestBrokerDriver) Subscribe(string, []string, connectors.MessageHandler) (connectors.Receiver, error) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| func (d *deltaTestBrokerDriver) SubscribeManual(string, []string, connectors.MessageHandler) (connectors.Receiver, error) { | ||
| return nil, nil | ||
| } | ||
|
|
||
| func (d *deltaTestBrokerDriver) Replay(context.Context, string, connectors.MessageHandler) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (d *deltaTestBrokerDriver) TopicExists(context.Context, string) (bool, error) { | ||
| return false, nil | ||
| } | ||
|
|
||
| func (d *deltaTestBrokerDriver) EnsureTopics(context.Context, []string) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (d *deltaTestBrokerDriver) EnsureCompactedTopic(context.Context, string) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (d *deltaTestBrokerDriver) DeleteTopics(context.Context, []string) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (d *deltaTestBrokerDriver) Close() error { | ||
| return nil | ||
| } | ||
|
|
||
| type deltaTestReceiver struct { | ||
| stopCalls int | ||
| } | ||
|
|
||
| func (r *deltaTestReceiver) Start(context.Context) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (r *deltaTestReceiver) Stop(context.Context) error { | ||
| r.stopCalls++ | ||
| return nil | ||
| } | ||
|
|
||
| func TestApplyBindingDeltaContinuesAfterTombstoneFailure(t *testing.T) { | ||
| store := subscription.NewInMemoryStore("runtime-1") | ||
| require.NoError(t, store.Add(&subscription.Subscription{ | ||
| ID: "sub-1", | ||
| Topic: "removed-a", | ||
| CallbackURL: "https://callback-a.example", | ||
| State: subscription.StateActive, | ||
| })) | ||
| require.NoError(t, store.Add(&subscription.Subscription{ | ||
| ID: "sub-2", | ||
| Topic: "removed-b", | ||
| CallbackURL: "https://callback-b.example", | ||
| State: subscription.StateActive, | ||
| })) | ||
|
|
||
| topics := NewTopicRegistry() | ||
| topics.Register("removed-a") | ||
| topics.Register("removed-b") | ||
|
|
||
| consumerA := &deltaTestReceiver{} | ||
| consumerB := &deltaTestReceiver{} | ||
| consumerMgr := &ConsumerManager{ | ||
| consumers: map[string]*managedConsumer{ | ||
| "https://callback-a.example": { | ||
| consumer: consumerA, | ||
| topics: map[string]bool{"kafka-a": true}, | ||
| }, | ||
| "https://callback-b.example": { | ||
| consumer: consumerB, | ||
| topics: map[string]bool{"kafka-b": true}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| driver := &deltaTestBrokerDriver{ | ||
| publishErrs: map[string]error{ | ||
| "removed-a:https://callback-a.example": errors.New("boom"), | ||
| }, | ||
| } | ||
|
|
||
| receiver := &WebSubReceiver{ | ||
| topics: topics, | ||
| store: store, | ||
| consumerMgr: consumerMgr, | ||
| syncProducer: subscription.NewSyncProducer( | ||
| driver, | ||
| "runtime-1", | ||
| "internal-sub-topic", | ||
| ), | ||
| channel: connectors.ChannelInfo{ | ||
| Name: "test-api", | ||
| Channels: map[string]string{ | ||
| "removed-a": "kafka-a", | ||
| "removed-b": "kafka-b", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| err := receiver.ApplyBindingDelta(context.Background(), | ||
| map[string]string{ | ||
| "removed-a": "kafka-a", | ||
| "removed-b": "kafka-b", | ||
| }, | ||
| map[string]string{ | ||
| "added-c": "kafka-c", | ||
| }, | ||
| ) | ||
|
|
||
| require.Error(t, err) | ||
| require.ErrorContains(t, err, `failed to tombstone subscription for removed channel "removed-a"`) | ||
|
|
||
| require.ElementsMatch(t, []string{ | ||
| "removed-a:https://callback-a.example", | ||
| "removed-b:https://callback-b.example", | ||
| }, driver.published) | ||
|
|
||
| require.Nil(t, store.GetByTopic("removed-a")) | ||
| require.Nil(t, store.GetByTopic("removed-b")) | ||
|
|
||
| require.Equal(t, 1, consumerA.stopCalls) | ||
| require.Equal(t, 1, consumerB.stopCalls) | ||
| require.Empty(t, consumerMgr.consumers) | ||
|
|
||
| require.NotContains(t, receiver.channel.Channels, "removed-a") | ||
| require.NotContains(t, receiver.channel.Channels, "removed-b") | ||
| require.Equal(t, "kafka-c", receiver.channel.Channels["added-c"]) | ||
|
|
||
| require.False(t, topics.IsRegistered("removed-a")) | ||
| require.False(t, topics.IsRegistered("removed-b")) | ||
| require.True(t, topics.IsRegistered("added-c")) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.