Skip to content

Commit

Permalink
avoid concurrent subnet status update (#1976)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzujian committed Oct 19, 2022
1 parent 9e249b3 commit d433f25
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type Controller struct {
deleteRouteQueue workqueue.RateLimitingInterface
updateSubnetStatusQueue workqueue.RateLimitingInterface
syncVirtualPortsQueue workqueue.RateLimitingInterface
subnetStatusKeyMutex *keymutex.KeyMutex

ipsLister kubeovnlister.IPLister
ipSynced cache.InformerSynced
Expand Down Expand Up @@ -242,6 +243,7 @@ func NewController(config *Configuration) *Controller {
deleteRouteQueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "DeleteRoute"),
updateSubnetStatusQueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "UpdateSubnetStatus"),
syncVirtualPortsQueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "SyncVirtualPort"),
subnetStatusKeyMutex: keymutex.New(97),

ipsLister: ipInformer.Lister(),
ipSynced: ipInformer.Informer().HasSynced,
Expand Down
25 changes: 25 additions & 0 deletions pkg/controller/subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,9 @@ func (c *Controller) handleAddOrUpdateSubnet(key string) error {
}

func (c *Controller) handleUpdateSubnetStatus(key string) error {
c.subnetStatusKeyMutex.Lock(key)
defer c.subnetStatusKeyMutex.Unlock(key)

orisubnet, err := c.subnetsLister.Get(key)
subnet := orisubnet.DeepCopy()
if err != nil {
Expand Down Expand Up @@ -1398,6 +1401,13 @@ func calcDualSubnetStatusIP(subnet *kubeovnv1.Subnet, c *Controller) error {
v6availableIPs = 0
}

if subnet.Status.V4AvailableIPs == v4availableIPs &&
subnet.Status.V6AvailableIPs == v6availableIPs &&
subnet.Status.V4UsingIPs == float64(len(v4UsingIPs)) &&
subnet.Status.V6UsingIPs == float64(len(v6UsingIPs)) {
return nil
}

subnet.Status.V4AvailableIPs = v4availableIPs
subnet.Status.V6AvailableIPs = v6availableIPs
subnet.Status.V4UsingIPs = float64(len(v4UsingIPs))
Expand Down Expand Up @@ -1460,6 +1470,13 @@ func calcSubnetStatusIP(subnet *kubeovnv1.Subnet, c *Controller) error {
if availableIPs < 0 {
availableIPs = 0
}

cachedFields := [4]float64{
subnet.Status.V4AvailableIPs,
subnet.Status.V4UsingIPs,
subnet.Status.V6AvailableIPs,
subnet.Status.V6UsingIPs,
}
if util.CheckProtocol(subnet.Spec.CIDRBlock) == kubeovnv1.ProtocolIPv4 {
subnet.Status.V4AvailableIPs = availableIPs
subnet.Status.V4UsingIPs = usingIPs
Expand All @@ -1471,6 +1488,14 @@ func calcSubnetStatusIP(subnet *kubeovnv1.Subnet, c *Controller) error {
subnet.Status.V4AvailableIPs = 0
subnet.Status.V4UsingIPs = 0
}
if cachedFields == [4]float64{
subnet.Status.V4AvailableIPs,
subnet.Status.V4UsingIPs,
subnet.Status.V6AvailableIPs,
subnet.Status.V6UsingIPs,
} {
return nil
}

bytes, err := subnet.Status.Bytes()
if err != nil {
Expand Down

0 comments on commit d433f25

Please sign in to comment.