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
+14 -11

No files matched your search

+1 -6
View File
@@ -24,15 +24,10 @@ 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 {
for _, addr := range addrs {
if g.r.Contains(addr) {
g.resultChan <- allocateResult{addr, nil} g.resultChan <- allocateResult{addr, nil}
return true return true
} }
}
}
if !alloc.universe.Overlaps(g.r) { if !alloc.universe.Overlaps(g.r) {
g.resultChan <- allocateResult{0, fmt.Errorf("range %s out of bounds: %s", g.r, alloc.universe)} g.resultChan <- allocateResult{0, fmt.Errorf("range %s out of bounds: %s", g.r, alloc.universe)}
+13 -5
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 for %s in range %s", ident, r)}
resultChan <- allocateResult{err: fmt.Errorf("lookup: no address found")}
} }
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 {