Extract lookup function for owned addresses

This commit is contained in:
Bryan Boreham committed 2015-06-10 17:27:07 +01:00
1 parent 7d881a0f0c
commit c27cd5b4ab
2 files changed
+18 -15

No files matched your search

+3 -8
View File
@@ -24,14 +24,9 @@ func (g *allocate) Try(alloc *Allocator) bool {
return true return true
} }
// If we have previously stored an address for this container in this subnet, return it. if addr, found := alloc.lookupOwned(g.ident, g.r); found {
if addrs, found := alloc.owned[g.ident]; found { g.resultChan <- allocateResult{addr, nil}
for _, addr := range addrs { return true
if g.r.Contains(addr) {
g.resultChan <- allocateResult{addr, nil}
return true
}
}
} }
if !alloc.universe.Overlaps(g.r) { if !alloc.universe.Overlaps(g.r) {
+15 -7
View File
@@ -48,7 +48,7 @@ type Allocator struct {
universe address.Range // superset of all ranges universe address.Range // superset of all ranges
ring *ring.Ring // information on ranges owned by all peers ring *ring.Ring // information on ranges owned by all peers
space space.Space // more detail on ranges owned by us space space.Space // more detail on ranges owned by us
owned map[string][]address.Address // who owns what address, indexed by container-ID owned map[string][]address.Address // who owns what addresses, indexed by container-ID
nicknames map[router.PeerName]string // so we can map nicknames for rmpeer nicknames map[router.PeerName]string // so we can map nicknames for rmpeer
pendingAllocates []operation // held until we get some free space pendingAllocates []operation // held until we get some free space
pendingClaims []operation // held until we know who owns the space pendingClaims []operation // held until we know who owns the space
@@ -191,13 +191,11 @@ func (alloc *Allocator) Allocate(ident string, r address.Range, cancelChan <-cha
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() {
for _, addr := range alloc.owned[ident] { if addr, found := alloc.lookupOwned(ident, r); found {
if r.Contains(addr) { resultChan <- allocateResult{addr: addr}
resultChan <- allocateResult{addr: addr} return
return
}
} }
resultChan <- allocateResult{err: fmt.Errorf("lookup: no address found")} resultChan <- allocateResult{err: fmt.Errorf("lookup: no address found for %s in range %s", ident, r)}
} }
result := <-resultChan result := <-resultChan
return result.addr, result.err return result.addr, result.err
@@ -689,10 +687,20 @@ func (alloc *Allocator) reportFreeSpace() {
// Owned addresses // Owned addresses
// 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) {
for _, addr := range alloc.owned[ident] {
if r.Contains(addr) {
return addr, true
}
}
return 0, false
}
func (alloc *Allocator) findOwner(addr address.Address) string { func (alloc *Allocator) findOwner(addr address.Address) string {
for ident, addrs := range alloc.owned { for ident, addrs := range alloc.owned {
for _, candidate := range addrs { for _, candidate := range addrs {