Merge pull request #1173 from weaveworks/1168-checkandpanic

Make nameserver.checkAndPanic look for dups, call it more often.

Fixes #1168.
This commit is contained in:
Matthias Radestock committed 2015-07-17 11:37:34 +01:00
commit 2ca0fb93c9
2 files changed
+18 -5

No files matched your search

+14 -1
View File
@@ -67,10 +67,15 @@ func (es Entries) Len() int { return len(es) }
func (es Entries) Swap(i, j int) { panic("Swap") } 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) 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) { if !sort.IsSorted(es) {
return fmt.Errorf("Not sorted!") 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 return nil
} }
@@ -81,6 +86,7 @@ func (es *Entries) checkAndPanic() {
} }
func (es *Entries) add(hostname, containerid string, origin router.PeerName, addr address.Address) Entry { func (es *Entries) add(hostname, containerid string, origin router.PeerName, addr address.Address) Entry {
es.checkAndPanic()
defer es.checkAndPanic() defer es.checkAndPanic()
entry := Entry{Hostname: hostname, Origin: origin, ContainerID: containerid, Addr: addr} entry := Entry{Hostname: hostname, Origin: origin, ContainerID: containerid, Addr: addr}
@@ -101,6 +107,7 @@ func (es *Entries) add(hostname, containerid string, origin router.PeerName, add
} }
func (es *Entries) merge(incoming Entries) Entries { func (es *Entries) merge(incoming Entries) Entries {
es.checkAndPanic()
defer es.checkAndPanic() defer es.checkAndPanic()
newEntries := Entries{} newEntries := Entries{}
i := 0 i := 0
@@ -125,6 +132,8 @@ func (es *Entries) merge(incoming Entries) Entries {
} }
func (es *Entries) tombstone(ourname router.PeerName, f func(*Entry) bool) *Entries { func (es *Entries) tombstone(ourname router.PeerName, f func(*Entry) bool) *Entries {
es.checkAndPanic()
defer es.checkAndPanic()
tombstoned := Entries{} tombstoned := Entries{}
for i, e := range *es { for i, e := range *es {
if f(&e) && e.Origin == ourname { if f(&e) && e.Origin == ourname {
@@ -138,6 +147,8 @@ func (es *Entries) tombstone(ourname router.PeerName, f func(*Entry) bool) *Entr
} }
func (es *Entries) filter(f func(*Entry) bool) { func (es *Entries) filter(f func(*Entry) bool) {
es.checkAndPanic()
defer es.checkAndPanic()
i := 0 i := 0
for _, e := range *es { for _, e := range *es {
if !f(&e) { if !f(&e) {
@@ -150,6 +161,7 @@ func (es *Entries) filter(f func(*Entry) bool) {
} }
func (es Entries) lookup(hostname string) Entries { func (es Entries) lookup(hostname string) Entries {
es.checkAndPanic()
i := sort.Search(len(es), func(i int) bool { i := sort.Search(len(es), func(i int) bool {
return es[i].Hostname >= hostname return es[i].Hostname >= hostname
}) })
@@ -165,6 +177,7 @@ func (es Entries) lookup(hostname string) Entries {
} }
func (es *Entries) first(f func(*Entry) bool) (*Entry, error) { func (es *Entries) first(f func(*Entry) bool) (*Entry, error) {
es.checkAndPanic()
for _, e := range *es { for _, e := range *es {
if f(&e) { if f(&e) {
return &e, nil return &e, nil
+4 -4
View File
@@ -97,7 +97,7 @@ func TestTombstone(t *testing.T) {
Entry{Hostname: "A"}, Entry{Hostname: "A"},
Entry{Hostname: "B", Version: 1, Tombstone: 1234}, Entry{Hostname: "B", Version: 1, Tombstone: 1234},
} }
require.Equal(t, es, expected) require.Equal(t, expected, es)
} }
func TestDelete(t *testing.T) { func TestDelete(t *testing.T) {
@@ -112,21 +112,21 @@ func TestDelete(t *testing.T) {
expected := Entries{ expected := Entries{
Entry{Hostname: "B"}, Entry{Hostname: "B"},
} }
require.Equal(t, es, expected) require.Equal(t, expected, es)
} }
func TestLookup(t *testing.T) { func TestLookup(t *testing.T) {
es := Entries{ es := Entries{
Entry{Hostname: "A"}, Entry{Hostname: "A"},
Entry{Hostname: "B", ContainerID: "foo"},
Entry{Hostname: "B", ContainerID: "bar"}, Entry{Hostname: "B", ContainerID: "bar"},
Entry{Hostname: "B", ContainerID: "foo"},
Entry{Hostname: "C"}, Entry{Hostname: "C"},
} }
have := es.lookup("B") have := es.lookup("B")
want := Entries{ want := Entries{
Entry{Hostname: "B", ContainerID: "foo"},
Entry{Hostname: "B", ContainerID: "bar"}, Entry{Hostname: "B", ContainerID: "bar"},
Entry{Hostname: "B", ContainerID: "foo"},
} }
require.Equal(t, have, want) require.Equal(t, have, want)
} }