-
Notifications
You must be signed in to change notification settings - Fork 370
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A NetworkPolicy's span is calculated in internalNetworkPolicyWorker, based on the span of the AppliedToGroups it refers to, while the span of AppliedToGroup is calculated in appliedToGroupWorker which runs in parallel with internalNetworkPolicyWorker. It could happen that the calcuated span is out of date if AppliedToGroups' span is updated after internalNetworkPolicyWorker calculates a NetworkPolicy's span, and the NetworkPolicy wouldn't be enqueued for another sync if it's not committed to the storage yet. On the other hand, if we commit the NetworkPolicy to the storage before calculating the NetworkPolicy's span, it would have to use a stale span first and might need to update the NetworkPolicy twice and generate two update events in one sync. To fix the issue without generating extra events, we introduce a separate subscription mechanism that allows subscribing to update of AppliedToGroup for NetworkPolicy. With the subscription, we can still calculate the NetworkPolicy's span first, then commit it to the storage. If any of the subscribed AppliedToGroups are updated, the NetworkPolicy will be notified and resynced. Signed-off-by: Quan Tian <[email protected]>
- Loading branch information
Showing
4 changed files
with
388 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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,77 @@ | ||
// Copyright 2023 Antrea Authors | ||
// | ||
// 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 networkpolicy | ||
|
||
import "sync" | ||
|
||
// notifier notifies multiple subscribers about any events that happen to the objects they have subscribed. | ||
type notifier struct { | ||
mutex sync.RWMutex | ||
subscribers map[string]map[string]func() | ||
} | ||
|
||
func newNotifier() *notifier { | ||
return ¬ifier{subscribers: map[string]map[string]func(){}} | ||
} | ||
|
||
// Subscribe the subscriber to the given resourceID with a callback. | ||
// If the subscription already exists, it does nothing. | ||
func (n *notifier) subscribe(resourceID, subscriberID string, callback func()) { | ||
n.mutex.Lock() | ||
defer n.mutex.Unlock() | ||
subscribers, exists := n.subscribers[resourceID] | ||
if !exists { | ||
subscribers = map[string]func(){} | ||
n.subscribers[resourceID] = subscribers | ||
} | ||
_, subscribed := subscribers[subscriberID] | ||
if subscribed { | ||
return | ||
} | ||
subscribers[subscriberID] = callback | ||
} | ||
|
||
// unsubscribe cancels the subscription. | ||
// If the subscription does not exist, it does nothing. | ||
func (n *notifier) unsubscribe(resourceID, subscriberID string) { | ||
n.mutex.Lock() | ||
defer n.mutex.Unlock() | ||
subscribers, exists := n.subscribers[resourceID] | ||
if !exists { | ||
return | ||
} | ||
_, subscribed := subscribers[subscriberID] | ||
if !subscribed { | ||
return | ||
} | ||
delete(subscribers, subscriberID) | ||
// If the resource is no longer subscribed by any notifier, remove its key. | ||
if len(subscribers) == 0 { | ||
delete(n.subscribers, resourceID) | ||
} | ||
} | ||
|
||
// Notify the subscribers by calling the callbacks they registered. | ||
func (n *notifier) notify(resourceID string) { | ||
n.mutex.RLock() | ||
defer n.mutex.RUnlock() | ||
subscribers, exists := n.subscribers[resourceID] | ||
if !exists { | ||
return | ||
} | ||
for _, callback := range subscribers { | ||
callback() | ||
} | ||
} |
Oops, something went wrong.