Skip to content

Commit

Permalink
Merge pull request #1173 from weaveworks/1168-checkandpanic
Browse files Browse the repository at this point in the history
Make nameserver.checkAndPanic look for dups, call it more often.

Fixes #1168.
  • Loading branch information
rade committed Jul 17, 2015
2 parents 47273d4 + 66e5893 commit 2ca0fb9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
15 changes: 14 additions & 1 deletion nameserver/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,15 @@ func (es Entries) Len() int { return len(es) }
func (es Entries) Swap(i, j int) { panic("Swap") }
func (es Entries) Less(i, j int) bool { return es[i].less(&es[j]) }

func (es *Entries) check() error {
func (es Entries) check() error {
if !sort.IsSorted(es) {
return fmt.Errorf("Not sorted!")
}
for i := 1; i < len(es); i++ {
if es[i].equal(&es[i-1]) {
return fmt.Errorf("Duplicate entry: %d:%v and %d:%v", i-1, es[i-1], i, es[i])
}
}
return nil
}

Expand All @@ -81,6 +86,7 @@ func (es *Entries) checkAndPanic() {
}

func (es *Entries) add(hostname, containerid string, origin router.PeerName, addr address.Address) Entry {
es.checkAndPanic()
defer es.checkAndPanic()

entry := Entry{Hostname: hostname, Origin: origin, ContainerID: containerid, Addr: addr}
Expand All @@ -101,6 +107,7 @@ func (es *Entries) add(hostname, containerid string, origin router.PeerName, add
}

func (es *Entries) merge(incoming Entries) Entries {
es.checkAndPanic()
defer es.checkAndPanic()
newEntries := Entries{}
i := 0
Expand All @@ -125,6 +132,8 @@ func (es *Entries) merge(incoming Entries) Entries {
}

func (es *Entries) tombstone(ourname router.PeerName, f func(*Entry) bool) *Entries {
es.checkAndPanic()
defer es.checkAndPanic()
tombstoned := Entries{}
for i, e := range *es {
if f(&e) && e.Origin == ourname {
Expand All @@ -138,6 +147,8 @@ func (es *Entries) tombstone(ourname router.PeerName, f func(*Entry) bool) *Entr
}

func (es *Entries) filter(f func(*Entry) bool) {
es.checkAndPanic()
defer es.checkAndPanic()
i := 0
for _, e := range *es {
if !f(&e) {
Expand All @@ -150,6 +161,7 @@ func (es *Entries) filter(f func(*Entry) bool) {
}

func (es Entries) lookup(hostname string) Entries {
es.checkAndPanic()
i := sort.Search(len(es), func(i int) bool {
return es[i].Hostname >= hostname
})
Expand All @@ -165,6 +177,7 @@ func (es Entries) lookup(hostname string) Entries {
}

func (es *Entries) first(f func(*Entry) bool) (*Entry, error) {
es.checkAndPanic()
for _, e := range *es {
if f(&e) {
return &e, nil
Expand Down
8 changes: 4 additions & 4 deletions nameserver/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func TestTombstone(t *testing.T) {
Entry{Hostname: "A"},
Entry{Hostname: "B", Version: 1, Tombstone: 1234},
}
require.Equal(t, es, expected)
require.Equal(t, expected, es)
}

func TestDelete(t *testing.T) {
Expand All @@ -112,21 +112,21 @@ func TestDelete(t *testing.T) {
expected := Entries{
Entry{Hostname: "B"},
}
require.Equal(t, es, expected)
require.Equal(t, expected, es)
}

func TestLookup(t *testing.T) {
es := Entries{
Entry{Hostname: "A"},
Entry{Hostname: "B", ContainerID: "foo"},
Entry{Hostname: "B", ContainerID: "bar"},
Entry{Hostname: "B", ContainerID: "foo"},
Entry{Hostname: "C"},
}

have := es.lookup("B")
want := Entries{
Entry{Hostname: "B", ContainerID: "foo"},
Entry{Hostname: "B", ContainerID: "bar"},
Entry{Hostname: "B", ContainerID: "foo"},
}
require.Equal(t, have, want)
}

0 comments on commit 2ca0fb9

Please sign in to comment.