Skip to content

Commit

Permalink
apply patches after changes if not in peer list
Browse files Browse the repository at this point in the history
Signed-off-by: Kristoffer Dalby <[email protected]>
  • Loading branch information
kradalby committed Nov 27, 2023
1 parent f72537a commit c6fc3e3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
9 changes: 7 additions & 2 deletions hscontrol/db/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -879,8 +879,12 @@ func (hsdb *HSDatabase) ExpireExpiredNodes(lastCheck time.Time) time.Time {
expired = append(expired, tailcfg.NodeID(node.ID))

now := time.Now()
err := hsdb.nodeSetExpiry(nodes[index], now)
if err != nil {
// Do not use setNodeExpiry as that has a notifier hook, which
// can cause a deadlock, we are updating all changed nodes later
// and there is no point in notifiying twice.
if err := hsdb.db.Model(nodes[index]).Updates(types.Node{
Expiry: &now,
}).Error; err != nil {
log.Error().
Err(err).
Str("node", node.Hostname).
Expand All @@ -895,6 +899,7 @@ func (hsdb *HSDatabase) ExpireExpiredNodes(lastCheck time.Time) time.Time {
}
}

// TODO(kradalby): Should these be removed or changed(expired)?
if len(expired) > 0 {
hsdb.notifier.NotifyAll(types.StateUpdate{
Type: types.StatePeerRemoved,
Expand Down
19 changes: 15 additions & 4 deletions hscontrol/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ func NewMapper(
seq: 0,

// TODO: populate
peers: peers.IDMap(),
peers: peers.IDMap(),
patches: make(map[uint64][]patch),
}
}

Expand Down Expand Up @@ -425,11 +426,20 @@ func (m *Mapper) PeerChangedPatchResponse(
peer.ApplyPeerChange(change)
sendUpdate = true
} else {
log.Trace().Str("node", node.Hostname).Msgf("Node with ID %s is missing from mapper for Node %s, saving change...", change.NodeID, node.Hostname)
m.patches[uint64(change.NodeID)] = append(m.patches[uint64(change.NodeID)], patch{
log.Trace().Str("node", node.Hostname).Msgf("Node with ID %s is missing from mapper for Node %s, saving patch for when node is available", change.NodeID, node.Hostname)

p := patch{
timestamp: time.Now(),
change: change,
})
}

if patches, ok := m.patches[uint64(change.NodeID)]; ok {
patches := append(patches, p)

m.patches[uint64(change.NodeID)] = patches
} else {
m.patches[uint64(change.NodeID)] = []patch{p}
}
}
}

Expand All @@ -455,6 +465,7 @@ func (m *Mapper) PeerRemovedResponse(
// remove from our internal map
for _, id := range removed {
delete(m.peers, uint64(id))
delete(m.patches, uint64(id))
}

resp := m.baseMapResponse()
Expand Down

0 comments on commit c6fc3e3

Please sign in to comment.