瀏覽代碼

libnet/ipam: get rid of superfluous closure

The two-phase commit dance serves no purpose with the current IPAM
allocator implementation. There are no fallible operations between the
call to aSpace.updatePoolDBOnAdd() and invoking the returned closure.
Allocate the subnet in the address space immediately when called and get
rid of the closure return.

Signed-off-by: Cory Snider <csnider@mirantis.com>
Cory Snider 2 年之前
父節點
當前提交
18ac200efe
共有 2 個文件被更改,包括 13 次插入14 次删除
  1. 3 4
      libnetwork/ipam/allocator.go
  2. 10 10
      libnetwork/ipam/structures.go

+ 3 - 4
libnetwork/ipam/allocator.go

@@ -89,8 +89,7 @@ retry:
 		return "", nil, nil, err
 		return "", nil, nil, err
 	}
 	}
 
 
-	insert, err := aSpace.updatePoolDBOnAdd(*k, nw, ipr, pdf)
-	if err != nil {
+	if err := aSpace.allocateSubnet(*k, nw, ipr, pdf); err != nil {
 		if _, ok := err.(types.MaskableError); ok {
 		if _, ok := err.(types.MaskableError); ok {
 			logrus.Debugf("Retrying predefined pool search: %v", err)
 			logrus.Debugf("Retrying predefined pool search: %v", err)
 			goto retry
 			goto retry
@@ -98,7 +97,7 @@ retry:
 		return "", nil, nil, err
 		return "", nil, nil, err
 	}
 	}
 
 
-	return k.String(), nw, nil, insert()
+	return k.String(), nw, nil, nil
 }
 }
 
 
 // ReleasePool releases the address pool identified by the passed id
 // ReleasePool releases the address pool identified by the passed id
@@ -114,7 +113,7 @@ func (a *Allocator) ReleasePool(poolID string) error {
 		return err
 		return err
 	}
 	}
 
 
-	return aSpace.updatePoolDBOnRemoval(k)
+	return aSpace.releaseSubnet(k)
 }
 }
 
 
 // Given the address space, returns the local or global PoolConfig based on whether the
 // Given the address space, returns the local or global PoolConfig based on whether the

+ 10 - 10
libnetwork/ipam/structures.go

@@ -78,29 +78,29 @@ func (p *PoolData) String() string {
 		p.ParentKey.String(), p.Pool.String(), p.Range, p.RefCount)
 		p.ParentKey.String(), p.Pool.String(), p.Range, p.RefCount)
 }
 }
 
 
-// updatePoolDBOnAdd returns a closure which will add the subnet k to the address space when executed.
-func (aSpace *addrSpace) updatePoolDBOnAdd(k SubnetKey, nw *net.IPNet, ipr *AddressRange, pdf bool) (func() error, error) {
+// allocateSubnet adds the subnet k to the address space.
+func (aSpace *addrSpace) allocateSubnet(k SubnetKey, nw *net.IPNet, ipr *AddressRange, pdf bool) error {
 	aSpace.Lock()
 	aSpace.Lock()
 	defer aSpace.Unlock()
 	defer aSpace.Unlock()
 
 
 	// Check if already allocated
 	// Check if already allocated
 	if _, ok := aSpace.subnets[k]; ok {
 	if _, ok := aSpace.subnets[k]; ok {
 		if pdf {
 		if pdf {
-			return nil, types.InternalMaskableErrorf("predefined pool %s is already reserved", nw)
+			return types.InternalMaskableErrorf("predefined pool %s is already reserved", nw)
 		}
 		}
-		// This means the same pool is already allocated. updatePoolDBOnAdd is called when there
+		// This means the same pool is already allocated. allocateSubnet is called when there
 		// is request for a pool/subpool. It should ensure there is no overlap with existing pools
 		// is request for a pool/subpool. It should ensure there is no overlap with existing pools
-		return nil, ipamapi.ErrPoolOverlap
+		return ipamapi.ErrPoolOverlap
 	}
 	}
 
 
 	// If master pool, check for overlap
 	// If master pool, check for overlap
 	if ipr == nil {
 	if ipr == nil {
 		if aSpace.contains(k.AddressSpace, nw) {
 		if aSpace.contains(k.AddressSpace, nw) {
-			return nil, ipamapi.ErrPoolOverlap
+			return ipamapi.ErrPoolOverlap
 		}
 		}
 		// This is a new master pool, add it along with corresponding bitmask
 		// This is a new master pool, add it along with corresponding bitmask
 		aSpace.subnets[k] = &PoolData{Pool: nw, RefCount: 1}
 		aSpace.subnets[k] = &PoolData{Pool: nw, RefCount: 1}
-		return func() error { return aSpace.alloc.insertBitMask(k, nw) }, nil
+		return aSpace.alloc.insertBitMask(k, nw)
 	}
 	}
 
 
 	// This is a new non-master pool (subPool)
 	// This is a new non-master pool (subPool)
@@ -116,15 +116,15 @@ func (aSpace *addrSpace) updatePoolDBOnAdd(k SubnetKey, nw *net.IPNet, ipr *Addr
 	pp, ok := aSpace.subnets[p.ParentKey]
 	pp, ok := aSpace.subnets[p.ParentKey]
 	if ok {
 	if ok {
 		aSpace.incRefCount(pp, 1)
 		aSpace.incRefCount(pp, 1)
-		return func() error { return nil }, nil
+		return nil
 	}
 	}
 
 
 	// Parent pool does not exist, add it along with corresponding bitmask
 	// Parent pool does not exist, add it along with corresponding bitmask
 	aSpace.subnets[p.ParentKey] = &PoolData{Pool: nw, RefCount: 1}
 	aSpace.subnets[p.ParentKey] = &PoolData{Pool: nw, RefCount: 1}
-	return func() error { return aSpace.alloc.insertBitMask(p.ParentKey, nw) }, nil
+	return aSpace.alloc.insertBitMask(p.ParentKey, nw)
 }
 }
 
 
-func (aSpace *addrSpace) updatePoolDBOnRemoval(k SubnetKey) error {
+func (aSpace *addrSpace) releaseSubnet(k SubnetKey) error {
 	aSpace.Lock()
 	aSpace.Lock()
 	defer aSpace.Unlock()
 	defer aSpace.Unlock()