Skip to content

Commit

Permalink
Change the type of entry.Free to address.Count
Browse files Browse the repository at this point in the history
This change is backward compatible, because the underlying types of
address.Offset (the previous type) and address.Count are the same
and the Gob encoder does not require types to exactly correspond.
  • Loading branch information
brb committed May 13, 2016
1 parent 6c7f860 commit fa490ae
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
5 changes: 2 additions & 3 deletions ipam/ring/entry.go
Expand Up @@ -14,16 +14,15 @@ type entry struct {
Token address.Address // The start of this range
Peer mesh.PeerName // Who owns this range
Version uint32 // Version of this range
Free address.Offset // Number of free IPs in this range
// Note: Free should perhaps be an address.Count, but we can't change the wire protocol
Free address.Count // Number of free IPs in this range
}

func (e *entry) Equal(e2 *entry) bool {
return e.Token == e2.Token && e.Peer == e2.Peer &&
e.Version == e2.Version
}

func (e *entry) update(peername mesh.PeerName, free address.Offset) {
func (e *entry) update(peername mesh.PeerName, free address.Count) {
e.Peer = peername
e.Version++
e.Free = free
Expand Down
18 changes: 9 additions & 9 deletions ipam/ring/ring.go
Expand Up @@ -110,12 +110,12 @@ func (r *Ring) Range() address.Range {

// Returns the distance between two tokens on this ring, dealing
// with ranges which cross the origin
func (r *Ring) distance(start, end address.Address) address.Offset {
func (r *Ring) distance(start, end address.Address) address.Count {
if end > start {
return address.Offset(end - start)
return address.Count(end - start)
}

return address.Offset((r.End - start) + (end - r.Start))
return address.Count((r.End - start) + (end - r.Start))
}

// GrantRangeToHost modifies the ring such that range [start, end)
Expand Down Expand Up @@ -357,12 +357,12 @@ func (r *Ring) ClaimForPeers(peers []mesh.PeerName) {
defer r.updateExportedVariables()

totalSize := r.distance(r.Start, r.End)
share := totalSize/address.Offset(len(peers)) + 1
remainder := totalSize % address.Offset(len(peers))
share := totalSize/address.Count(len(peers)) + 1
remainder := totalSize % address.Count(len(peers))
pos := r.Start

for i, peer := range peers {
if address.Offset(i) == remainder {
if address.Count(i) == remainder {
share--
if share == 0 {
break
Expand Down Expand Up @@ -432,11 +432,11 @@ func (r *Ring) ReportFree(freespace map[address.Address]address.Count) {
maxSize := r.distance(entry.Token, next.Token)
common.Assert(free <= address.Count(maxSize))

if address.Count(entries[i].Free) == free {
if entries[i].Free == free {
return
}

entries[i].Free = address.Offset(free)
entries[i].Free = free
entries[i].Version++
}
}
Expand All @@ -455,7 +455,7 @@ func (ws weightedPeers) Swap(i, j int) { ws[i], ws[j] = ws[j], ws[i] }
// ChoosePeersToAskForSpace returns all peers we can ask for space in
// the range [start, end), in weighted-random order. Assumes start<end.
func (r *Ring) ChoosePeersToAskForSpace(start, end address.Address) []mesh.PeerName {
totalSpacePerPeer := make(map[mesh.PeerName]address.Offset)
totalSpacePerPeer := make(map[mesh.PeerName]address.Count)

// iterate through tokens
for i, entry := range r.Entries {
Expand Down
2 changes: 1 addition & 1 deletion net/address/address.go
Expand Up @@ -122,7 +122,7 @@ func Length(a, b Address) Count {
return Count(a - b)
}

func Min(a, b Offset) Offset {
func Min(a, b Count) Count {
if a > b {
return b
}
Expand Down

0 comments on commit fa490ae

Please sign in to comment.