Change invariant fail on Merge() from panic to error

This commit is contained in:
Bryan Boreham committed 2016-03-23 15:26:26 +00:00
1 parent 537868008f
commit 6bc199a8bc
1 file changed
+16 -9
+16 -9
View File
@@ -46,33 +46,37 @@ var (
) )
func (r *Ring) checkInvariants() error { func (r *Ring) checkInvariants() error {
if !sort.IsSorted(r.Entries) { return r.checkEntries(r.Entries)
}
func (r *Ring) checkEntries(entries entries) error {
if !sort.IsSorted(entries) {
return ErrNotSorted return ErrNotSorted
} }
// Check no token appears twice // Check no token appears twice
// We know it's sorted... // We know it's sorted...
for i := 1; i < len(r.Entries); i++ { for i := 1; i < len(entries); i++ {
if r.Entries[i-1].Token == r.Entries[i].Token { if entries[i-1].Token == entries[i].Token {
return ErrTokenRepeated return ErrTokenRepeated
} }
} }
if len(r.Entries) == 0 { if len(entries) == 0 {
return nil return nil
} }
// Check tokens are in range // Check tokens are in range
if r.Entries.entry(0).Token < r.Start { if entries.entry(0).Token < r.Start {
return ErrTokenOutOfRange return ErrTokenOutOfRange
} }
if r.Entries.entry(-1).Token >= r.End { if entries.entry(-1).Token >= r.End {
return ErrTokenOutOfRange return ErrTokenOutOfRange
} }
// Check all the freespaces are in range // Check all the freespaces are in range
for i, entry := range r.Entries { for i, entry := range entries {
next := r.Entries.entry(i + 1) next := entries.entry(i + 1)
distance := r.distance(entry.Token, next.Token) distance := r.distance(entry.Token, next.Token)
if entry.Free > distance { if entry.Free > distance {
@@ -181,7 +185,6 @@ func (r *Ring) GrantRangeToHost(start, end address.Address, peer mesh.PeerName)
// Merge the given ring into this ring and return any new ranges added // Merge the given ring into this ring and return any new ranges added
func (r *Ring) Merge(gossip Ring) error { func (r *Ring) Merge(gossip Ring) error {
r.assertInvariants() r.assertInvariants()
defer r.assertInvariants()
defer r.updateExportedVariables() defer r.updateExportedVariables()
// Don't panic when checking the gossiped in ring. // Don't panic when checking the gossiped in ring.
@@ -267,6 +270,10 @@ func (r *Ring) Merge(gossip Ring) error {
previousOwner = nil previousOwner = nil
} }
if err := r.checkEntries(result); err != nil {
return fmt.Errorf("Merge of incoming data causes: %s", err)
}
if len(r.Seeds) == 0 { if len(r.Seeds) == 0 {
r.Seeds = gossip.Seeds r.Seeds = gossip.Seeds
} }