Skip to content

Commit

Permalink
Remove database from Mapper
Browse files Browse the repository at this point in the history
This commit changes the internals of the mapper to
track all the changes to peers over its lifetime.

This means that it no longer depends on the database
and this should hopefully help with locks and timing issues.
When the mapper is created, it needs the current list of peers,
the world view, when the polling session was started. Then as
update changes are called, it tracks the changes and generates
responses based on its internal list.

As a side, the types.Machines and types.MachinesP, as well as
types.Machine being passed as a full struct and pointer has been
changed to always be pointers, everywhere.

Signed-off-by: Kristoffer Dalby <[email protected]>
  • Loading branch information
kradalby committed Sep 19, 2023
1 parent 3b0749a commit 387aa03
Show file tree
Hide file tree
Showing 15 changed files with 251 additions and 236 deletions.
2 changes: 1 addition & 1 deletion hscontrol/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func NewHeadscaleDatabase(
Msg("Failed to normalize machine hostname in DB migration")
}

err = db.RenameMachine(&machines[item], normalizedHostname)
err = db.RenameMachine(machines[item], normalizedHostname)
if err != nil {
log.Error().
Caller().
Expand Down
14 changes: 7 additions & 7 deletions hscontrol/db/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (hsdb *HSDatabase) GetMachine(user string, name string) (*types.Machine, er

for _, m := range machines {
if m.Hostname == name {
return &m, nil
return m, nil
}
}

Expand Down Expand Up @@ -267,7 +267,7 @@ func (hsdb *HSDatabase) SetTags(

hsdb.notifier.NotifyWithIgnore(types.StateUpdate{
Type: types.StatePeerChanged,
Changed: []uint64{machine.ID},
Changed: types.Machines{machine},
}, machine.MachineKey)

return nil
Expand Down Expand Up @@ -303,7 +303,7 @@ func (hsdb *HSDatabase) RenameMachine(machine *types.Machine, newName string) er

hsdb.notifier.NotifyWithIgnore(types.StateUpdate{
Type: types.StatePeerChanged,
Changed: []uint64{machine.ID},
Changed: types.Machines{machine},
}, machine.MachineKey)

return nil
Expand Down Expand Up @@ -332,7 +332,7 @@ func (hsdb *HSDatabase) machineSetExpiry(machine *types.Machine, expiry time.Tim

hsdb.notifier.NotifyWithIgnore(types.StateUpdate{
Type: types.StatePeerChanged,
Changed: []uint64{machine.ID},
Changed: types.Machines{machine},
}, machine.MachineKey)

return nil
Expand Down Expand Up @@ -713,7 +713,7 @@ func (hsdb *HSDatabase) enableRoutes(machine *types.Machine, routeStrs ...string

hsdb.notifier.NotifyWithIgnore(types.StateUpdate{
Type: types.StatePeerChanged,
Changed: []uint64{machine.ID},
Changed: types.Machines{machine},
}, machine.MachineKey)

return nil
Expand Down Expand Up @@ -807,7 +807,7 @@ func (hsdb *HSDatabase) ExpireEphemeralMachines(inactivityThreshhold time.Durati
Str("machine", machine.Hostname).
Msg("Ephemeral client removed from database")

err = hsdb.deleteMachine(&machines[idx])
err = hsdb.deleteMachine(machines[idx])
if err != nil {
log.Error().
Err(err).
Expand Down Expand Up @@ -860,7 +860,7 @@ func (hsdb *HSDatabase) ExpireExpiredMachines(lastCheck time.Time) time.Time {
expired = append(expired, tailcfg.NodeID(machine.ID))

now := time.Now()
err := hsdb.machineSetExpiry(&machines[index], now)
err := hsdb.machineSetExpiry(machines[index], now)
if err != nil {
log.Error().
Err(err).
Expand Down
2 changes: 1 addition & 1 deletion hscontrol/db/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ func (s *Suite) TestAutoApproveRoutes(c *check.C) {

db.db.Save(&machine)

err = db.ProcessMachineRoutes(&machine)
err = db.SaveMachineRoutes(&machine)
c.Assert(err, check.IsNil)

machine0ByID, err := db.GetMachineByID(0)
Expand Down
16 changes: 9 additions & 7 deletions hscontrol/db/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,16 @@ func (hsdb *HSDatabase) GetMachinePrimaryRoutes(machine *types.Machine) (types.R
return routes, nil
}

func (hsdb *HSDatabase) ProcessMachineRoutes(machine *types.Machine) error {
// SaveMachineRoutes takes a machine and updates the database with
// the new routes.
func (hsdb *HSDatabase) SaveMachineRoutes(machine *types.Machine) error {
hsdb.mu.Lock()
defer hsdb.mu.Unlock()

return hsdb.processMachineRoutes(machine)
return hsdb.saveMachineRoutes(machine)
}

func (hsdb *HSDatabase) processMachineRoutes(machine *types.Machine) error {
func (hsdb *HSDatabase) saveMachineRoutes(machine *types.Machine) error {
currentRoutes := types.Routes{}
err := hsdb.db.Where("machine_id = ?", machine.ID).Find(&currentRoutes).Error
if err != nil {
Expand Down Expand Up @@ -332,7 +334,7 @@ func (hsdb *HSDatabase) handlePrimarySubnetFailover() error {
log.Error().Err(err).Msg("error getting routes")
}

changedMachines := make([]uint64, 0)
changedMachines := make(types.Machines, 0)
for pos, route := range routes {
if route.IsExitRoute() {
continue
Expand All @@ -353,7 +355,7 @@ func (hsdb *HSDatabase) handlePrimarySubnetFailover() error {
return err
}

changedMachines = append(changedMachines, route.MachineID)
changedMachines = append(changedMachines, &route.Machine)

continue
}
Expand Down Expand Up @@ -427,7 +429,7 @@ func (hsdb *HSDatabase) handlePrimarySubnetFailover() error {
return err
}

changedMachines = append(changedMachines, route.MachineID)
changedMachines = append(changedMachines, &route.Machine)
}
}

Expand Down Expand Up @@ -488,7 +490,7 @@ func (hsdb *HSDatabase) EnableAutoApprovedRoutes(
approvedRoutes = append(approvedRoutes, advertisedRoute)
} else {
// TODO(kradalby): figure out how to get this to depend on less stuff
approvedIps, err := aclPolicy.ExpandAlias(types.Machines{*machine}, approvedAlias)
approvedIps, err := aclPolicy.ExpandAlias(types.Machines{machine}, approvedAlias)
if err != nil {
log.Err(err).
Str("alias", approvedAlias).
Expand Down
16 changes: 8 additions & 8 deletions hscontrol/db/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *Suite) TestGetRoutes(c *check.C) {
}
db.db.Save(&machine)

err = db.ProcessMachineRoutes(&machine)
err = db.SaveMachineRoutes(&machine)
c.Assert(err, check.IsNil)

advertisedRoutes, err := db.GetAdvertisedRoutes(&machine)
Expand Down Expand Up @@ -91,7 +91,7 @@ func (s *Suite) TestGetEnableRoutes(c *check.C) {
}
db.db.Save(&machine)

err = db.ProcessMachineRoutes(&machine)
err = db.SaveMachineRoutes(&machine)
c.Assert(err, check.IsNil)

availableRoutes, err := db.GetAdvertisedRoutes(&machine)
Expand Down Expand Up @@ -165,7 +165,7 @@ func (s *Suite) TestIsUniquePrefix(c *check.C) {
}
db.db.Save(&machine1)

err = db.ProcessMachineRoutes(&machine1)
err = db.SaveMachineRoutes(&machine1)
c.Assert(err, check.IsNil)

err = db.enableRoutes(&machine1, route.String())
Expand All @@ -190,7 +190,7 @@ func (s *Suite) TestIsUniquePrefix(c *check.C) {
}
db.db.Save(&machine2)

err = db.ProcessMachineRoutes(&machine2)
err = db.SaveMachineRoutes(&machine2)
c.Assert(err, check.IsNil)

err = db.enableRoutes(&machine2, route2.String())
Expand Down Expand Up @@ -252,7 +252,7 @@ func (s *Suite) TestSubnetFailover(c *check.C) {
}
db.db.Save(&machine1)

err = db.ProcessMachineRoutes(&machine1)
err = db.SaveMachineRoutes(&machine1)
c.Assert(err, check.IsNil)

err = db.enableRoutes(&machine1, prefix.String())
Expand Down Expand Up @@ -289,7 +289,7 @@ func (s *Suite) TestSubnetFailover(c *check.C) {
}
db.db.Save(&machine2)

err = db.ProcessMachineRoutes(&machine2)
err = db.SaveMachineRoutes(&machine2)
c.Assert(err, check.IsNil)

err = db.enableRoutes(&machine2, prefix2.String())
Expand Down Expand Up @@ -337,7 +337,7 @@ func (s *Suite) TestSubnetFailover(c *check.C) {
err = db.db.Save(&machine2).Error
c.Assert(err, check.IsNil)

err = db.ProcessMachineRoutes(&machine2)
err = db.SaveMachineRoutes(&machine2)
c.Assert(err, check.IsNil)

err = db.enableRoutes(&machine2, prefix.String())
Expand Down Expand Up @@ -394,7 +394,7 @@ func (s *Suite) TestDeleteRoutes(c *check.C) {
}
db.db.Save(&machine1)

err = db.ProcessMachineRoutes(&machine1)
err = db.SaveMachineRoutes(&machine1)
c.Assert(err, check.IsNil)

err = db.enableRoutes(&machine1, prefix.String())
Expand Down
2 changes: 1 addition & 1 deletion hscontrol/grpcv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func (api headscaleV1APIServer) ListMachines(
for index, machine := range machines {
m := machine.Proto()
validTags, invalidTags := api.h.ACLPolicy.TagsOfMachine(
machine,
&machine,
)
m.InvalidTags = invalidTags
m.ValidTags = validTags
Expand Down
Loading

0 comments on commit 387aa03

Please sign in to comment.