Deal in subnets, not ranges, when requesting address allocation

This commit is contained in:
Bryan Boreham committed 2016-03-02 17:21:51 -08:00
1 parent b76e2a87cf
commit 73f1a04d5f
4 files changed
+16 -16

No files matched your search

+6 -6
View File
@@ -14,7 +14,7 @@ type allocateResult struct {
type allocate struct { type allocate struct {
resultChan chan<- allocateResult resultChan chan<- allocateResult
ident string ident string
r address.Range // Range we are trying to allocate within r address.CIDR // Subnet we are trying to allocate within
hasBeenCancelled func() bool hasBeenCancelled func() bool
} }
@@ -25,21 +25,21 @@ func (g *allocate) Try(alloc *Allocator) bool {
return true return true
} }
if addrs := alloc.ownedInRange(g.ident, g.r); len(addrs) > 0 { if addrs := alloc.ownedInRange(g.ident, g.r.Range()); len(addrs) > 0 {
// If we had heard that this container died, resurrect it // If we had heard that this container died, resurrect it
delete(alloc.dead, g.ident) // delete is no-op if key not in map delete(alloc.dead, g.ident) // delete is no-op if key not in map
g.resultChan <- allocateResult{addrs[0].Addr, nil} g.resultChan <- allocateResult{addrs[0].Addr, nil}
return true return true
} }
if !alloc.universe.Overlaps(g.r) { if !alloc.universe.Overlaps(g.r.Range()) {
g.resultChan <- allocateResult{err: fmt.Errorf("range %s out of bounds: %s", g.r, alloc.universe)} g.resultChan <- allocateResult{err: fmt.Errorf("range %s out of bounds: %s", g.r, alloc.universe)}
return true return true
} }
alloc.establishRing() alloc.establishRing()
if ok, addr := alloc.space.Allocate(g.r); ok { if ok, addr := alloc.space.Allocate(g.r.HostRange()); ok {
// If caller hasn't supplied a unique ID, file it under the IP address // If caller hasn't supplied a unique ID, file it under the IP address
// which lets the caller then release the address using DELETE /ip/address // which lets the caller then release the address using DELETE /ip/address
if g.ident == "_" { if g.ident == "_" {
@@ -52,9 +52,9 @@ func (g *allocate) Try(alloc *Allocator) bool {
} }
// out of space // out of space
donors := alloc.ring.ChoosePeersToAskForSpace(g.r.Start, g.r.End) donors := alloc.ring.ChoosePeersToAskForSpace(g.r.Addr, g.r.Range().End)
for _, donor := range donors { for _, donor := range donors {
if err := alloc.sendSpaceRequest(donor, g.r); err != nil { if err := alloc.sendSpaceRequest(donor, g.r.Range()); err != nil {
alloc.debugln("Problem asking peer", donor, "for space:", err) alloc.debugln("Problem asking peer", donor, "for space:", err)
} else { } else {
alloc.debugln("Decided to ask peer", donor, "for space in range", g.r) alloc.debugln("Decided to ask peer", donor, "for space in range", g.r)
+1 -1
View File
@@ -195,7 +195,7 @@ func (e *errorCancelled) Error() string {
// Allocate (Sync) - get new IP address for container with given name in range // Allocate (Sync) - get new IP address for container with given name in range
// if there isn't any space in that range we block indefinitely // if there isn't any space in that range we block indefinitely
func (alloc *Allocator) Allocate(ident string, r address.Range, hasBeenCancelled func() bool) (address.Address, error) { func (alloc *Allocator) Allocate(ident string, r address.CIDR, hasBeenCancelled func() bool) (address.Address, error) {
resultChan := make(chan allocateResult) resultChan := make(chan allocateResult)
op := &allocate{resultChan: resultChan, ident: ident, r: r, hasBeenCancelled: hasBeenCancelled} op := &allocate{resultChan: resultChan, ident: ident, r: r, hasBeenCancelled: hasBeenCancelled}
alloc.doOperation(op, &alloc.pendingAllocates) alloc.doOperation(op, &alloc.pendingAllocates)
+8 -8
View File
@@ -41,11 +41,11 @@ func TestAllocFree(t *testing.T) {
_, cidr2, _ := address.ParseCIDR(subnet2) _, cidr2, _ := address.ParseCIDR(subnet2)
alloc.claimRingForTesting() alloc.claimRingForTesting()
addr1, err := alloc.Allocate(container1, cidr1.HostRange(), returnFalse) addr1, err := alloc.Allocate(container1, cidr1, returnFalse)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, testAddr1, addr1.String(), "address") require.Equal(t, testAddr1, addr1.String(), "address")
addr2, err := alloc.Allocate(container1, cidr2.HostRange(), returnFalse) addr2, err := alloc.Allocate(container1, cidr2, returnFalse)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, testAddr2, addr2.String(), "address") require.Equal(t, testAddr2, addr2.String(), "address")
@@ -54,28 +54,28 @@ func TestAllocFree(t *testing.T) {
require.Equal(t, []address.Address{addr1, addr2}, addrs) require.Equal(t, []address.Address{addr1, addr2}, addrs)
// Ask for another address for a different container and check it's different // Ask for another address for a different container and check it's different
addr1b, _ := alloc.Allocate(container2, cidr1.HostRange(), returnFalse) addr1b, _ := alloc.Allocate(container2, cidr1, returnFalse)
if addr1b.String() == testAddr1 { if addr1b.String() == testAddr1 {
t.Fatalf("Expected different address but got %s", addr1b.String()) t.Fatalf("Expected different address but got %s", addr1b.String())
} }
// Ask for the first container again and we should get the same addresses again // Ask for the first container again and we should get the same addresses again
addr1a, _ := alloc.Allocate(container1, cidr1.HostRange(), returnFalse) addr1a, _ := alloc.Allocate(container1, cidr1, returnFalse)
require.Equal(t, testAddr1, addr1a.String(), "address") require.Equal(t, testAddr1, addr1a.String(), "address")
addr2a, _ := alloc.Allocate(container1, cidr2.HostRange(), returnFalse) addr2a, _ := alloc.Allocate(container1, cidr2, returnFalse)
require.Equal(t, testAddr2, addr2a.String(), "address") require.Equal(t, testAddr2, addr2a.String(), "address")
// Now delete the first container, and we should get its addresses back // Now delete the first container, and we should get its addresses back
require.NoError(t, alloc.Delete(container1)) require.NoError(t, alloc.Delete(container1))
addr3, _ := alloc.Allocate(container3, cidr1.HostRange(), returnFalse) addr3, _ := alloc.Allocate(container3, cidr1, returnFalse)
require.Equal(t, testAddr1, addr3.String(), "address") require.Equal(t, testAddr1, addr3.String(), "address")
addr4, _ := alloc.Allocate(container3, cidr2.HostRange(), returnFalse) addr4, _ := alloc.Allocate(container3, cidr2, returnFalse)
require.Equal(t, testAddr2, addr4.String(), "address") require.Equal(t, testAddr2, addr4.String(), "address")
alloc.ContainerDied(container2) alloc.ContainerDied(container2)
// Resurrect // Resurrect
addr1c, err := alloc.Allocate(container2, cidr1.HostRange(), returnFalse) addr1c, err := alloc.Allocate(container2, cidr1, returnFalse)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, addr1b, addr1c, "address") require.Equal(t, addr1b, addr1c, "address")
+1 -1
View File
@@ -40,7 +40,7 @@ func writeAddresses(w http.ResponseWriter, cidrs []address.CIDR) {
func (alloc *Allocator) handleHTTPAllocate(dockerCli *docker.Client, w http.ResponseWriter, ident string, checkAlive bool, subnet address.CIDR) { func (alloc *Allocator) handleHTTPAllocate(dockerCli *docker.Client, w http.ResponseWriter, ident string, checkAlive bool, subnet address.CIDR) {
closedChan := w.(http.CloseNotifier).CloseNotify() closedChan := w.(http.CloseNotifier).CloseNotify()
addr, err := alloc.Allocate(ident, subnet.HostRange(), addr, err := alloc.Allocate(ident, subnet,
func() bool { func() bool {
select { select {
case <-closedChan: case <-closedChan: