ソースを参照

Add numerical ids manager

Signed-off-by: Alessandro Boch <aboch@docker.com>
Alessandro Boch 10 年 前
コミット
d1a16bbb84

+ 8 - 8
libnetwork/bitseq/sequence.go

@@ -134,13 +134,13 @@ func (s *Sequence) FromByteArray(data []byte) error {
 }
 }
 
 
 // GetFirstAvailable returns the byte and bit position of the first unset bit
 // GetFirstAvailable returns the byte and bit position of the first unset bit
-func (h *Handle) GetFirstAvailable() (int, int) {
+func (h *Handle) GetFirstAvailable() (int, int, error) {
 	return GetFirstAvailable(h.Head)
 	return GetFirstAvailable(h.Head)
 }
 }
 
 
 // CheckIfAvailable checks if the bit correspondent to the specified ordinal is unset
 // CheckIfAvailable checks if the bit correspondent to the specified ordinal is unset
 // If the ordinal is beyond the Sequence limits, a negative response is returned
 // If the ordinal is beyond the Sequence limits, a negative response is returned
-func (h *Handle) CheckIfAvailable(ordinal int) (int, int) {
+func (h *Handle) CheckIfAvailable(ordinal int) (int, int, error) {
 	return CheckIfAvailable(h.Head, ordinal)
 	return CheckIfAvailable(h.Head, ordinal)
 }
 }
 
 
@@ -150,23 +150,23 @@ func (h *Handle) PushReservation(bytePos, bitPos int, release bool) {
 }
 }
 
 
 // GetFirstAvailable looks for the first unset bit in passed mask
 // GetFirstAvailable looks for the first unset bit in passed mask
-func GetFirstAvailable(head *Sequence) (int, int) {
+func GetFirstAvailable(head *Sequence) (int, int, error) {
 	byteIndex := 0
 	byteIndex := 0
 	current := head
 	current := head
 	for current != nil {
 	for current != nil {
 		if current.Block != blockMAX {
 		if current.Block != blockMAX {
 			bytePos, bitPos := current.GetAvailableBit()
 			bytePos, bitPos := current.GetAvailableBit()
-			return byteIndex + bytePos, bitPos
+			return byteIndex + bytePos, bitPos, nil
 		}
 		}
 		byteIndex += int(current.Count * blockBytes)
 		byteIndex += int(current.Count * blockBytes)
 		current = current.Next
 		current = current.Next
 	}
 	}
-	return -1, -1
+	return -1, -1, fmt.Errorf("no bit available")
 }
 }
 
 
 // CheckIfAvailable checks if the bit correspondent to the specified ordinal is unset
 // CheckIfAvailable checks if the bit correspondent to the specified ordinal is unset
 // If the ordinal is beyond the Sequence limits, a negative response is returned
 // If the ordinal is beyond the Sequence limits, a negative response is returned
-func CheckIfAvailable(head *Sequence, ordinal int) (int, int) {
+func CheckIfAvailable(head *Sequence, ordinal int) (int, int, error) {
 	bytePos := ordinal / 8
 	bytePos := ordinal / 8
 	bitPos := ordinal % 8
 	bitPos := ordinal % 8
 
 
@@ -177,11 +177,11 @@ func CheckIfAvailable(head *Sequence, ordinal int) (int, int) {
 		// Check whether the bit corresponding to the ordinal address is unset
 		// Check whether the bit corresponding to the ordinal address is unset
 		bitSel := uint32(blockFirstBit >> uint(inBlockBytePos*8+bitPos))
 		bitSel := uint32(blockFirstBit >> uint(inBlockBytePos*8+bitPos))
 		if current.Block&bitSel == 0 {
 		if current.Block&bitSel == 0 {
-			return bytePos, bitPos
+			return bytePos, bitPos, nil
 		}
 		}
 	}
 	}
 
 
-	return -1, -1
+	return -1, -1, fmt.Errorf("requested bit is not available")
 }
 }
 
 
 // Given the byte position and the sequences list head, return the pointer to the
 // Given the byte position and the sequences list head, return the pointer to the

+ 2 - 2
libnetwork/bitseq/sequence_test.go

@@ -127,7 +127,7 @@ func TestGetFirstAvailable(t *testing.T) {
 	}
 	}
 
 
 	for n, i := range input {
 	for n, i := range input {
-		bytePos, bitPos := GetFirstAvailable(i.mask)
+		bytePos, bitPos, _ := GetFirstAvailable(i.mask)
 		if bytePos != i.bytePos || bitPos != i.bitPos {
 		if bytePos != i.bytePos || bitPos != i.bitPos {
 			t.Fatalf("Error in (%d) getFirstAvailable(). Expected (%d, %d). Got (%d, %d)", n, i.bytePos, i.bitPos, bytePos, bitPos)
 			t.Fatalf("Error in (%d) getFirstAvailable(). Expected (%d, %d). Got (%d, %d)", n, i.bytePos, i.bitPos, bytePos, bitPos)
 		}
 		}
@@ -201,7 +201,7 @@ func TestCheckIfAvailable(t *testing.T) {
 	}
 	}
 
 
 	for n, i := range input {
 	for n, i := range input {
-		bytePos, bitPos := CheckIfAvailable(i.head, i.ordinal)
+		bytePos, bitPos, _ := CheckIfAvailable(i.head, i.ordinal)
 		if bytePos != i.bytePos || bitPos != i.bitPos {
 		if bytePos != i.bytePos || bitPos != i.bitPos {
 			t.Fatalf("Error in (%d) checkIfAvailable(ord:%d). Expected (%d, %d). Got (%d, %d)", n, i.ordinal, i.bytePos, i.bitPos, bytePos, bitPos)
 			t.Fatalf("Error in (%d) checkIfAvailable(ord:%d). Expected (%d, %d). Got (%d, %d)", n, i.ordinal, i.bytePos, i.bitPos, bytePos, bitPos)
 		}
 		}

+ 72 - 0
libnetwork/idm/idm.go

@@ -0,0 +1,72 @@
+// Package idm manages resevation/release of numerical ids from a configured set of contiguos ids
+package idm
+
+import (
+	"fmt"
+
+	"github.com/docker/libnetwork/bitseq"
+)
+
+// Idm manages the reservation/release of numerical ids from a contiguos set
+type Idm struct {
+	start  uint32
+	end    uint32
+	handle *bitseq.Handle
+}
+
+// New returns an instance of id manager for a set of [start-end] numerical ids
+func New(id string, start, end uint32) (*Idm, error) {
+	if id == "" {
+		return nil, fmt.Errorf("Invalid id")
+	}
+	if end <= start {
+		return nil, fmt.Errorf("Invalid set range: [%d, %d]", start, end)
+	}
+	return &Idm{start: start, end: end, handle: bitseq.NewHandle(id, 1+end-start)}, nil
+}
+
+// GetID returns the first available id in the set
+func (i *Idm) GetID() (uint32, error) {
+	if i.handle == nil {
+		return 0, fmt.Errorf("ID set is not initialized")
+	}
+
+	bytePos, bitPos, err := i.handle.GetFirstAvailable()
+	if err != nil {
+		return 0, fmt.Errorf("no available ids")
+	}
+	id := i.start + uint32(bitPos+bytePos*8)
+
+	// for sets which length is non multiple of 32 this check is needed
+	if i.end < id {
+		return 0, fmt.Errorf("no available ids")
+	}
+
+	i.handle.PushReservation(bytePos, bitPos, false)
+
+	return id, nil
+}
+
+// GetSpecificID tries to reserve the specified id
+func (i *Idm) GetSpecificID(id uint32) error {
+	if i.handle == nil {
+		return fmt.Errorf("ID set is not initialized")
+	}
+
+	if id < i.start || id > i.end {
+		return fmt.Errorf("Requested id does not belong to the set")
+	}
+
+	if bytePos, bitPos, err := i.handle.CheckIfAvailable(int(id - i.start)); err == nil {
+		i.handle.PushReservation(bytePos, bitPos, false)
+		return nil
+	}
+
+	return fmt.Errorf("requested id is not available")
+}
+
+// Release releases the specified id
+func (i *Idm) Release(id uint32) {
+	ordinal := id - i.start
+	i.handle.PushReservation(int(ordinal/8), int(ordinal%8), true)
+}

+ 108 - 0
libnetwork/idm/idm_test.go

@@ -0,0 +1,108 @@
+package idm
+
+import (
+	"testing"
+)
+
+func TestNew(t *testing.T) {
+	_, err := New("", 0, 1)
+	if err == nil {
+		t.Fatalf("Expected failure, but succeeded")
+	}
+
+	_, err = New("myset", 1<<10, 0)
+	if err == nil {
+		t.Fatalf("Expected failure, but succeeded")
+	}
+
+	i, err := New("myset", 0, 10)
+	if err != nil {
+		t.Fatalf("Unexpected failure: %v", err)
+	}
+	if i.handle == nil {
+		t.Fatalf("set is not initialized")
+	}
+	if i.start != 0 {
+		t.Fatalf("unexpected start")
+	}
+	if i.end != 10 {
+		t.Fatalf("unexpected end")
+	}
+}
+
+func TestAllocate(t *testing.T) {
+	i, err := New("myids", 50, 52)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if err = i.GetSpecificID(49); err == nil {
+		t.Fatalf("Expected failure but succeeded")
+	}
+
+	if err = i.GetSpecificID(53); err == nil {
+		t.Fatalf("Expected failure but succeeded")
+	}
+
+	o, err := i.GetID()
+	if err != nil {
+		t.Fatal(err)
+	}
+	if o != 50 {
+		t.Fatalf("Unexpected first id returned: %d", o)
+	}
+
+	err = i.GetSpecificID(50)
+	if err == nil {
+		t.Fatal(err)
+	}
+
+	o, err = i.GetID()
+	if err != nil {
+		t.Fatal(err)
+	}
+	if o != 51 {
+		t.Fatalf("Unexpected id returned: %d", o)
+	}
+
+	o, err = i.GetID()
+	if err != nil {
+		t.Fatal(err)
+	}
+	if o != 52 {
+		t.Fatalf("Unexpected id returned: %d", o)
+	}
+
+	o, err = i.GetID()
+	if err == nil {
+		t.Fatalf("Expected failure but succeeded: %d", o)
+	}
+
+	i.Release(50)
+
+	o, err = i.GetID()
+	if err != nil {
+		t.Fatal(err)
+	}
+	if o != 50 {
+		t.Fatalf("Unexpected id returned")
+	}
+
+	i.Release(52)
+	err = i.GetSpecificID(52)
+	if err != nil {
+		t.Fatal(err)
+	}
+}
+
+func TestUninitialized(t *testing.T) {
+	i := &Idm{}
+
+	if _, err := i.GetID(); err == nil {
+		t.Fatalf("Expected failure but succeeded")
+	}
+
+	if err := i.GetSpecificID(44); err == nil {
+		t.Fatalf("Expected failure but succeeded")
+	}
+}

+ 4 - 3
libnetwork/ipam/allocator.go

@@ -355,6 +355,7 @@ func (a *Allocator) getSubnetList(addrSpace AddressSpace, ver ipVersion) []subne
 func (a *Allocator) getAddress(smallSubnet *bitmask, prefAddress net.IP, ver ipVersion) (net.IP, error) {
 func (a *Allocator) getAddress(smallSubnet *bitmask, prefAddress net.IP, ver ipVersion) (net.IP, error) {
 	var (
 	var (
 		bytePos, bitPos int
 		bytePos, bitPos int
+		err             error
 	)
 	)
 	// Look for free IP, skip .0 and .255, they will be automatically reserved
 	// Look for free IP, skip .0 and .255, they will be automatically reserved
 again:
 again:
@@ -362,12 +363,12 @@ again:
 		return nil, ErrNoAvailableIPs
 		return nil, ErrNoAvailableIPs
 	}
 	}
 	if prefAddress == nil {
 	if prefAddress == nil {
-		bytePos, bitPos = smallSubnet.addressMask.GetFirstAvailable()
+		bytePos, bitPos, err = smallSubnet.addressMask.GetFirstAvailable()
 	} else {
 	} else {
 		ordinal := ipToInt(getHostPortionIP(prefAddress, smallSubnet.subnet))
 		ordinal := ipToInt(getHostPortionIP(prefAddress, smallSubnet.subnet))
-		bytePos, bitPos = smallSubnet.addressMask.CheckIfAvailable(ordinal)
+		bytePos, bitPos, err = smallSubnet.addressMask.CheckIfAvailable(ordinal)
 	}
 	}
-	if bytePos == -1 {
+	if err != nil {
 		return nil, ErrNoAvailableIPs
 		return nil, ErrNoAvailableIPs
 	}
 	}