Optimise DeleteRecords when explicit values are passed

This commit is contained in:
Adam Harrison committed 2015-06-02 10:09:26 +01:00
1 parent 30447ada52
commit a4582fb529
2 files changed
+60 -80

No files matched your search

+55 -76
View File
@@ -192,15 +192,27 @@ func (n *name) addIP(zr ZoneRecord, now time.Time) (*recordEntry, error) {
return ne, nil return ne, nil
} }
// Delete an IP for this name // Delete by IP. Pass uninitialized value for wildcard
func (n *name) deleteIP(ip IPv4) bool { func (n *name) deleteIP(ip net.IP) int {
if ipRecord, found := n.ipv4[ip]; found { count := 0
ipRecord.notifyIPObservers() if !ip.Equal(net.IP{}) {
n.notifyNameObservers() ipv4 := ipToIPv4(ip)
delete(n.ipv4, ip) if ipv4V, found := n.ipv4[ipv4]; found {
return true ipv4V.notifyIPObservers()
delete(n.ipv4, ipv4)
count++
}
} else {
for ipv4K, ipv4V := range n.ipv4 {
ipv4V.notifyIPObservers()
delete(n.ipv4, ipv4K)
count++
}
} }
return false if count > 0 {
n.notifyNameObservers()
}
return count
} }
// Update the list of IPs for this name, adding new ones and removing old IPs... // Update the list of IPs for this name, adding new ones and removing old IPs...
@@ -330,36 +342,25 @@ func (ns *namesSet) addIPToName(zr ZoneRecord, now time.Time) (*recordEntry, err
return n.addIP(zr, now) return n.addIP(zr, now)
} }
// Delete a name in the names set // Delete by name and IP. Pass uninitialized values for wildcard
func (ns *namesSet) deleteName(n string) error { func (ns namesSet) deleteNameIP(name string, ip net.IP) int {
if name, found := ns.names[n]; found { count := 0
for _, ipRecord := range name.getAllEntries() { if name != "" {
ipRecord.notifyIPObservers() if nameV, found := ns.names[name]; found {
count += nameV.deleteIP(ip)
if nameV.empty() {
delete(ns.names, name)
}
} }
name.notifyNameObservers() } else {
delete(ns.names, n) for nameK, nameV := range ns.names {
return nil count += nameV.deleteIP(ip)
} if nameV.empty() {
return LookupError(n) delete(ns.names, nameK)
} }
// Delete an IPv4 from all names
func (ns *namesSet) deleteIP(ip IPv4) error {
cnt := 0
for nkey, n := range ns.names {
if n.deleteIP(ip) {
cnt++
}
if n.empty() {
delete(ns.names, nkey)
} }
} }
return count
// we must return an error if no records have been found and deleted
if cnt == 0 {
return LookupError(fmt.Sprintf("%+v", ip))
}
return nil
} }
// Get the name query time // Get the name query time
@@ -592,43 +593,14 @@ func (zone *ZoneDb) AddRecord(ident string, name string, ip net.IP) (err error)
// Delete matching records (uninitialised values act as wildcards) // Delete matching records (uninitialised values act as wildcards)
func (zone *ZoneDb) DeleteRecords(ident string, name string, ip net.IP) int { func (zone *ZoneDb) DeleteRecords(ident string, name string, ip net.IP) int {
zone.mx.Lock()
defer zone.mx.Unlock()
count := 0
if name != "" { if name != "" {
name = dns.Fqdn(name) name = dns.Fqdn(name)
} }
for identK, identV := range zone.idents { zone.mx.Lock()
if ident == "" || ident == identK { defer zone.mx.Unlock()
for nameK, nameV := range identV.names {
nameModified := false
if name == "" || name == nameK {
for ipv4K, ipv4V := range nameV.ipv4 {
if ip.Equal(net.IP{}) || ip.Equal(ipv4K.toNetIP()) {
ipv4V.notifyIPObservers()
delete(nameV.ipv4, ipv4K)
nameModified = true
count++
}
}
}
if nameModified {
nameV.notifyNameObservers()
if nameV.empty() {
delete(identV.names, nameK)
}
}
}
}
if identV.empty() {
delete(zone.idents, identK)
}
}
return count return zone.deleteIdentNameIP(ident, name, ip)
} }
// Observe a name. // Observe a name.
@@ -776,16 +748,23 @@ func (zone *ZoneDb) getNamesSet(ident string) *namesSet {
return ns return ns
} }
// Delete a ident // Delete by ident, name and IP. Pass uninitialized value for wildcard
func (zone *ZoneDb) deleteIdent(ident string) { func (zone *ZoneDb) deleteIdentNameIP(ident string, name string, ip net.IP) int {
Info.Printf("[zonedb] Removing everything for ident '%s'", ident) count := 0
if ns, found := zone.idents[ident]; found { if ident != "" {
for _, n := range ns.names { if identV, found := zone.idents[ident]; found {
for _, entry := range n.getAllEntries() { count += identV.deleteNameIP(name, ip)
entry.notifyIPObservers() if identV.empty() {
delete(zone.idents, ident)
}
}
} else {
for identK, identV := range zone.idents {
count += identV.deleteNameIP(name, ip)
if identV.empty() {
delete(zone.idents, identK)
} }
n.notifyNameObservers()
} }
delete(zone.idents, ident)
} }
return count
} }
+5 -4
View File
@@ -5,6 +5,7 @@ import (
"github.com/miekg/dns" "github.com/miekg/dns"
. "github.com/weaveworks/weave/common" . "github.com/weaveworks/weave/common"
"net"
) )
type uniqZoneRecordKey struct { type uniqZoneRecordKey struct {
@@ -128,7 +129,7 @@ func (zone *ZoneDb) DomainLookupName(name string) (res []ZoneRecord, err error)
// locally introduced entries are never expired: they always have TTL=0 // locally introduced entries are never expired: they always have TTL=0
if ze.hasExpired(now) { if ze.hasExpired(now) {
Debug.Printf("[zonedb] '%s': expired entry '%s' ignored: removing", name, ze) Debug.Printf("[zonedb] '%s': expired entry '%s' ignored: removing", name, ze)
nameset.deleteName(name) nameset.deleteNameIP(name, net.IP{})
} else { } else {
uniq.add(ze) uniq.add(ze)
} }
@@ -173,7 +174,7 @@ func (zone *ZoneDb) DomainLookupInaddr(inaddr string) (res []ZoneRecord, err err
// locally introduced entries are never expired: they always have TTL=0 // locally introduced entries are never expired: they always have TTL=0
if ze.hasExpired(now) { if ze.hasExpired(now) {
Debug.Printf("[zonedb] '%s': expired entry '%s' ignored: removing", revIPv4, ze) Debug.Printf("[zonedb] '%s': expired entry '%s' ignored: removing", revIPv4, ze)
nameset.deleteIP(revIPv4) nameset.deleteNameIP("", revIPv4.toNetIP())
} else { } else {
uniq.add(ze) uniq.add(ze)
if identName != defaultRemoteIdent { if identName != defaultRemoteIdent {
@@ -273,7 +274,7 @@ func (zone *ZoneDb) updater(num int) {
if !zone.IsNameRelevant(request.name) || zone.IsNameExpired(request.name) { if !zone.IsNameRelevant(request.name) || zone.IsNameExpired(request.name) {
Debug.Printf("[zonedb] '%s' seem to be irrelevant now: removing any remote information", request.name) Debug.Printf("[zonedb] '%s' seem to be irrelevant now: removing any remote information", request.name)
zone.mx.Lock() zone.mx.Lock()
zone.getNamesSet(defaultRemoteIdent).deleteName(request.name) zone.getNamesSet(defaultRemoteIdent).deleteNameIP(request.name, net.IP{})
zone.mx.Unlock() zone.mx.Unlock()
continue continue
} }
@@ -300,7 +301,7 @@ func (zone *ZoneDb) updater(num int) {
} else { } else {
Debug.Printf("[zonedb] nobody knows about '%s'... removing", name) Debug.Printf("[zonedb] nobody knows about '%s'... removing", name)
zone.mx.Lock() zone.mx.Lock()
zone.getNamesSet(defaultRemoteIdent).deleteName(request.name) zone.getNamesSet(defaultRemoteIdent).deleteNameIP(request.name, net.IP{})
zone.mx.Unlock() zone.mx.Unlock()
} }
} }