Refactor to collect all code that touches alloc.owned
This commit is contained in:
1 parent
faa7bad8d8
commit
71da517e88
2 files changed
+42
-21
No files matched your search
+1
-1
@@ -25,7 +25,7 @@ func (g *allocate) Try(alloc *Allocator) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if addr, found := alloc.lookupOwned(g.ident, g.r); found {
|
if addr, found := alloc.ownedInRange(g.ident, g.r); found {
|
||||||
// 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{addr, nil}
|
g.resultChan <- allocateResult{addr, nil}
|
||||||
|
|||||||
+41
-20
@@ -209,7 +209,7 @@ func (alloc *Allocator) Allocate(ident string, r address.Range, hasBeenCancelled
|
|||||||
func (alloc *Allocator) Lookup(ident string, r address.Range) (address.Address, error) {
|
func (alloc *Allocator) Lookup(ident string, r address.Range) (address.Address, error) {
|
||||||
resultChan := make(chan allocateResult)
|
resultChan := make(chan allocateResult)
|
||||||
alloc.actionChan <- func() {
|
alloc.actionChan <- func() {
|
||||||
if addr, found := alloc.lookupOwned(ident, r); found {
|
if addr, found := alloc.ownedInRange(ident, r); found {
|
||||||
resultChan <- allocateResult{addr: addr}
|
resultChan <- allocateResult{addr: addr}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -230,7 +230,7 @@ func (alloc *Allocator) Claim(ident string, addr address.Address, noErrorOnUnkno
|
|||||||
// ContainerDied called from the updater interface. Async.
|
// ContainerDied called from the updater interface. Async.
|
||||||
func (alloc *Allocator) ContainerDied(ident string) {
|
func (alloc *Allocator) ContainerDied(ident string) {
|
||||||
alloc.actionChan <- func() {
|
alloc.actionChan <- func() {
|
||||||
if _, found := alloc.lookupOwned(ident, alloc.universe); found {
|
if alloc.hasOwned(ident) {
|
||||||
alloc.debugln("Container", ident, "died; noting to remove later")
|
alloc.debugln("Container", ident, "died; noting to remove later")
|
||||||
alloc.dead[ident] = alloc.now()
|
alloc.dead[ident] = alloc.now()
|
||||||
}
|
}
|
||||||
@@ -243,7 +243,7 @@ func (alloc *Allocator) ContainerDied(ident string) {
|
|||||||
// ContainerDestroyed called from the updater interface. Async.
|
// ContainerDestroyed called from the updater interface. Async.
|
||||||
func (alloc *Allocator) ContainerDestroyed(ident string) {
|
func (alloc *Allocator) ContainerDestroyed(ident string) {
|
||||||
alloc.actionChan <- func() {
|
alloc.actionChan <- func() {
|
||||||
if _, found := alloc.lookupOwned(ident, alloc.universe); found {
|
if alloc.hasOwned(ident) {
|
||||||
alloc.debugln("Container", ident, "destroyed; removing addresses")
|
alloc.debugln("Container", ident, "destroyed; removing addresses")
|
||||||
alloc.delete(ident)
|
alloc.delete(ident)
|
||||||
delete(alloc.dead, ident)
|
delete(alloc.dead, ident)
|
||||||
@@ -275,13 +275,12 @@ func (alloc *Allocator) Delete(ident string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (alloc *Allocator) delete(ident string) error {
|
func (alloc *Allocator) delete(ident string) error {
|
||||||
addrs, found := alloc.owned[ident]
|
addrs := alloc.removeAllOwned(ident)
|
||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
alloc.space.Free(addr)
|
alloc.space.Free(addr)
|
||||||
}
|
}
|
||||||
delete(alloc.owned, ident)
|
|
||||||
|
|
||||||
if !found {
|
if len(addrs) == 0 {
|
||||||
return fmt.Errorf("Delete: no addresses for %s", ident)
|
return fmt.Errorf("Delete: no addresses for %s", ident)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -291,19 +290,11 @@ func (alloc *Allocator) delete(ident string) error {
|
|||||||
func (alloc *Allocator) Free(ident string, addrToFree address.Address) error {
|
func (alloc *Allocator) Free(ident string, addrToFree address.Address) error {
|
||||||
errChan := make(chan error)
|
errChan := make(chan error)
|
||||||
alloc.actionChan <- func() {
|
alloc.actionChan <- func() {
|
||||||
addrs := alloc.owned[ident]
|
if alloc.removeOwned(ident, addrToFree) {
|
||||||
for i, ownedAddr := range addrs {
|
alloc.debugln("Freed", addrToFree, "for", ident)
|
||||||
if ownedAddr == addrToFree {
|
alloc.space.Free(addrToFree)
|
||||||
alloc.debugln("Freed", addrToFree, "for", ident)
|
errChan <- nil
|
||||||
if len(addrs) == 1 {
|
return
|
||||||
delete(alloc.owned, ident)
|
|
||||||
} else {
|
|
||||||
alloc.owned[ident] = append(addrs[:i], addrs[i+1:]...)
|
|
||||||
}
|
|
||||||
alloc.space.Free(addrToFree)
|
|
||||||
errChan <- nil
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errChan <- fmt.Errorf("Free: address %s not found for %s", addrToFree, ident)
|
errChan <- fmt.Errorf("Free: address %s not found for %s", addrToFree, ident)
|
||||||
@@ -767,12 +758,42 @@ func (alloc *Allocator) reportFreeSpace() {
|
|||||||
|
|
||||||
// Owned addresses
|
// Owned addresses
|
||||||
|
|
||||||
|
func (alloc *Allocator) allOwned(ident string) []address.Address {
|
||||||
|
return alloc.owned[ident]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (alloc *Allocator) hasOwned(ident string) bool {
|
||||||
|
_, b := alloc.owned[ident]
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
// NB: addr must not be owned by ident already
|
// NB: addr must not be owned by ident already
|
||||||
func (alloc *Allocator) addOwned(ident string, addr address.Address) {
|
func (alloc *Allocator) addOwned(ident string, addr address.Address) {
|
||||||
alloc.owned[ident] = append(alloc.owned[ident], addr)
|
alloc.owned[ident] = append(alloc.owned[ident], addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (alloc *Allocator) lookupOwned(ident string, r address.Range) (address.Address, bool) {
|
func (alloc *Allocator) removeAllOwned(ident string) []address.Address {
|
||||||
|
a := alloc.owned[ident]
|
||||||
|
delete(alloc.owned, ident)
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
func (alloc *Allocator) removeOwned(ident string, addrToFree address.Address) bool {
|
||||||
|
addrs, _ := alloc.owned[ident]
|
||||||
|
for i, ownedAddr := range addrs {
|
||||||
|
if ownedAddr == addrToFree {
|
||||||
|
if len(addrs) == 1 {
|
||||||
|
delete(alloc.owned, ident)
|
||||||
|
} else {
|
||||||
|
alloc.owned[ident] = append(addrs[:i], addrs[i+1:]...)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (alloc *Allocator) ownedInRange(ident string, r address.Range) (address.Address, bool) {
|
||||||
for _, addr := range alloc.owned[ident] {
|
for _, addr := range alloc.owned[ident] {
|
||||||
if r.Contains(addr) {
|
if r.Contains(addr) {
|
||||||
return addr, true
|
return addr, true
|
||||||
|
|||||||
Reference in new issue
Block a user