control/controlclient: avoid calls to ms.netmap() (#19281)

Instead of generating the full netmap, just fetch the peers out the the
existing peers map.

The extra usage was introduced with netmap caching, but there is no need
to call the netmap to get this information, rather the existing peermap
can be used.

Updates #12639

Signed-off-by: Claus Lensbøl <claus@tailscale.com>
main
Claus Lensbøl 1 week ago committed by GitHub
parent 5341b26328
commit 9e68841939
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 24
      control/controlclient/map.go
  2. 7
      control/controlclient/map_test.go

@ -408,10 +408,9 @@ type updateStats struct {
// removeUnwantedDiscoUpdates goes over the patchified updates and reject items // removeUnwantedDiscoUpdates goes over the patchified updates and reject items
// where the node is offline and has last been seen before the recorded last seen. // where the node is offline and has last been seen before the recorded last seen.
func (ms *mapSession) removeUnwantedDiscoUpdates(resp *tailcfg.MapResponse) { func (ms *mapSession) removeUnwantedDiscoUpdates(resp *tailcfg.MapResponse) {
existingMap := ms.netmap() ms.peersMu.RLock()
if existingMap == nil { defer ms.peersMu.RUnlock()
return
}
acceptedDiscoUpdates := resp.PeersChangedPatch[:0] acceptedDiscoUpdates := resp.PeersChangedPatch[:0]
for _, change := range resp.PeersChangedPatch { for _, change := range resp.PeersChangedPatch {
@ -430,14 +429,13 @@ func (ms *mapSession) removeUnwantedDiscoUpdates(resp *tailcfg.MapResponse) {
continue continue
} }
peerIdx := existingMap.PeerIndexByNodeID(change.NodeID) existingNode, ok := ms.peers[change.NodeID]
// Accept if: // Accept if:
// - Cannot find the peer, don't have enough data // - Cannot find the peer, don't have enough data
if peerIdx < 0 { if !ok {
acceptedDiscoUpdates = append(acceptedDiscoUpdates, change) acceptedDiscoUpdates = append(acceptedDiscoUpdates, change)
continue continue
} }
existingNode := existingMap.Peers[peerIdx]
// Accept if: // Accept if:
// - lastSeen moved forward in time. // - lastSeen moved forward in time.
@ -456,13 +454,12 @@ func (ms *mapSession) removeUnwantedDiscoUpdates(resp *tailcfg.MapResponse) {
// local netmap has a newer key learned via TSMP, overwrite the update with the // local netmap has a newer key learned via TSMP, overwrite the update with the
// key from TSMP. // key from TSMP.
func (ms *mapSession) removeUnwantedDiscoUpdatesFromFullNetmapUpdate(resp *tailcfg.MapResponse) { func (ms *mapSession) removeUnwantedDiscoUpdatesFromFullNetmapUpdate(resp *tailcfg.MapResponse) {
ms.peersMu.RLock()
defer ms.peersMu.RUnlock()
if len(resp.Peers) == 0 { if len(resp.Peers) == 0 {
return return
} }
existingMap := ms.netmap()
if existingMap == nil {
return
}
for _, peer := range resp.Peers { for _, peer := range resp.Peers {
if peer.DiscoKey.IsZero() { if peer.DiscoKey.IsZero() {
continue continue
@ -470,14 +467,13 @@ func (ms *mapSession) removeUnwantedDiscoUpdatesFromFullNetmapUpdate(resp *tailc
// Accept if: // Accept if:
// - peer is new // - peer is new
peerIdx := existingMap.PeerIndexByNodeID(peer.ID) existingNode, ok := ms.peers[peer.ID]
if peerIdx < 0 { if !ok {
continue continue
} }
// Accept if: // Accept if:
// - disco key has not changed // - disco key has not changed
existingNode := existingMap.Peers[peerIdx]
if existingNode.DiscoKey() == peer.DiscoKey { if existingNode.DiscoKey() == peer.DiscoKey {
continue continue
} }

@ -696,13 +696,12 @@ func TestUpdateDiscoForNode(t *testing.T) {
ms.updateDiscoForNode(node.ID, node.Key, newKey.Public(), tt.updateLastSeen, tt.updateOnline) ms.updateDiscoForNode(node.ID, node.Key, newKey.Public(), tt.updateLastSeen, tt.updateOnline)
<-nu.done <-nu.done
nm := ms.netmap() peer, ok := ms.peers[node.ID]
peerIdx := nm.PeerIndexByNodeID(node.ID) if !ok {
if peerIdx == -1 {
t.Fatal("node not found") t.Fatal("node not found")
} }
updated := nm.Peers[peerIdx].DiscoKey().Compare(newKey.Public()) == 0 updated := peer.DiscoKey().Compare(newKey.Public()) == 0
if updated != tt.wantUpdate { if updated != tt.wantUpdate {
t.Fatalf("Disco key update: %t, wanted update: %t", updated, tt.wantUpdate) t.Fatalf("Disco key update: %t, wanted update: %t", updated, tt.wantUpdate)
} }

Loading…
Cancel
Save