Skip to content

fix: fix race in PGCoord at startup #9144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 63 additions & 38 deletions enterprise/tailnet/pgcoord.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,15 @@ func (b *binder) writeOne(bnd binding) error {
CoordinatorID: b.coordinatorID,
Node: nodeRaw,
})
b.logger.Debug(b.ctx, "upserted agent binding",
slog.F("agent_id", bnd.agent), slog.F("node", nodeRaw), slog.Error(err))
case bnd.isAgent() && len(nodeRaw) == 0:
_, err = b.store.DeleteTailnetAgent(b.ctx, database.DeleteTailnetAgentParams{
ID: bnd.agent,
CoordinatorID: b.coordinatorID,
})
b.logger.Debug(b.ctx, "deleted agent binding",
slog.F("agent_id", bnd.agent), slog.Error(err))
if xerrors.Is(err, sql.ErrNoRows) {
// treat deletes as idempotent
err = nil
Expand All @@ -419,11 +423,16 @@ func (b *binder) writeOne(bnd binding) error {
AgentID: bnd.agent,
Node: nodeRaw,
})
b.logger.Debug(b.ctx, "upserted client binding",
slog.F("agent_id", bnd.agent), slog.F("client_id", bnd.client),
slog.F("node", nodeRaw), slog.Error(err))
case bnd.isClient() && len(nodeRaw) == 0:
_, err = b.store.DeleteTailnetClient(b.ctx, database.DeleteTailnetClientParams{
ID: bnd.client,
CoordinatorID: b.coordinatorID,
})
b.logger.Debug(b.ctx, "deleted client binding",
slog.F("agent_id", bnd.agent), slog.F("client_id", bnd.client), slog.Error(err))
if xerrors.Is(err, sql.ErrNoRows) {
// treat deletes as idempotent
err = nil
Expand Down Expand Up @@ -620,7 +629,7 @@ func newQuerier(
updates: updates,
healthy: true, // assume we start healthy
}
go q.subscribe()
q.subscribe()
go q.handleConnIO()
for i := 0; i < numWorkers; i++ {
go q.worker()
Expand Down Expand Up @@ -748,6 +757,8 @@ func (q *querier) query(mk mKey) error {

func (q *querier) queryClientsOfAgent(agent uuid.UUID) ([]mapping, error) {
clients, err := q.store.GetTailnetClientsForAgent(q.ctx, agent)
q.logger.Debug(q.ctx, "queried clients of agent",
slog.F("agent_id", agent), slog.F("num_clients", len(clients)), slog.Error(err))
if err != nil {
return nil, err
}
Expand All @@ -772,6 +783,8 @@ func (q *querier) queryClientsOfAgent(agent uuid.UUID) ([]mapping, error) {

func (q *querier) queryAgent(agentID uuid.UUID) ([]mapping, error) {
agents, err := q.store.GetTailnetAgents(q.ctx, agentID)
q.logger.Debug(q.ctx, "queried agents",
slog.F("agent_id", agentID), slog.F("num_agents", len(agents)), slog.Error(err))
if err != nil {
return nil, err
}
Expand All @@ -793,50 +806,62 @@ func (q *querier) queryAgent(agentID uuid.UUID) ([]mapping, error) {
return mappings, nil
}

// subscribe starts our subscriptions to client and agent updates in a new goroutine, and returns once we are subscribed
// or the querier context is canceled.
func (q *querier) subscribe() {
eb := backoff.NewExponentialBackOff()
eb.MaxElapsedTime = 0 // retry indefinitely
eb.MaxInterval = dbMaxBackoff
bkoff := backoff.WithContext(eb, q.ctx)
var cancelClient context.CancelFunc
err := backoff.Retry(func() error {
cancelFn, err := q.pubsub.SubscribeWithErr(eventClientUpdate, q.listenClient)
subscribed := make(chan struct{})
go func() {
defer close(subscribed)
eb := backoff.NewExponentialBackOff()
eb.MaxElapsedTime = 0 // retry indefinitely
eb.MaxInterval = dbMaxBackoff
bkoff := backoff.WithContext(eb, q.ctx)
var cancelClient context.CancelFunc
err := backoff.Retry(func() error {
cancelFn, err := q.pubsub.SubscribeWithErr(eventClientUpdate, q.listenClient)
if err != nil {
q.logger.Warn(q.ctx, "failed to subscribe to client updates", slog.Error(err))
return err
}
cancelClient = cancelFn
return nil
}, bkoff)
if err != nil {
q.logger.Warn(q.ctx, "failed to subscribe to client updates", slog.Error(err))
return err
}
cancelClient = cancelFn
return nil
}, bkoff)
if err != nil {
if q.ctx.Err() == nil {
q.logger.Error(q.ctx, "code bug: retry failed before context canceled", slog.Error(err))
if q.ctx.Err() == nil {
q.logger.Error(q.ctx, "code bug: retry failed before context canceled", slog.Error(err))
}
return
}
return
}
defer cancelClient()
bkoff.Reset()
defer cancelClient()
bkoff.Reset()
q.logger.Debug(q.ctx, "subscribed to client updates")

var cancelAgent context.CancelFunc
err = backoff.Retry(func() error {
cancelFn, err := q.pubsub.SubscribeWithErr(eventAgentUpdate, q.listenAgent)
var cancelAgent context.CancelFunc
err = backoff.Retry(func() error {
cancelFn, err := q.pubsub.SubscribeWithErr(eventAgentUpdate, q.listenAgent)
if err != nil {
q.logger.Warn(q.ctx, "failed to subscribe to agent updates", slog.Error(err))
return err
}
cancelAgent = cancelFn
return nil
}, bkoff)
if err != nil {
q.logger.Warn(q.ctx, "failed to subscribe to agent updates", slog.Error(err))
return err
}
cancelAgent = cancelFn
return nil
}, bkoff)
if err != nil {
if q.ctx.Err() == nil {
q.logger.Error(q.ctx, "code bug: retry failed before context canceled", slog.Error(err))
if q.ctx.Err() == nil {
q.logger.Error(q.ctx, "code bug: retry failed before context canceled", slog.Error(err))
}
return
}
return
}
defer cancelAgent()
defer cancelAgent()
q.logger.Debug(q.ctx, "subscribed to agent updates")

// hold subscriptions open until context is canceled
<-q.ctx.Done()
// unblock the outer function from returning
subscribed <- struct{}{}

// hold subscriptions open until context is canceled
<-q.ctx.Done()
}()
<-subscribed
}

func (q *querier) listenClient(_ context.Context, msg []byte, err error) {
Expand Down
38 changes: 19 additions & 19 deletions enterprise/tailnet/pgcoord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func TestPGCoordinatorSingle_AgentWithoutClients(t *testing.T) {
require.NoError(t, err)
defer coordinator.Close()

agent := newTestAgent(t, coordinator)
agent := newTestAgent(t, coordinator, "agent")
defer agent.close()
agent.sendNode(&agpl.Node{PreferredDERP: 10})
require.Eventually(t, func() bool {
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestPGCoordinatorSingle_AgentWithClient(t *testing.T) {
require.NoError(t, err)
defer coordinator.Close()

agent := newTestAgent(t, coordinator)
agent := newTestAgent(t, coordinator, "original")
defer agent.close()
agent.sendNode(&agpl.Node{PreferredDERP: 10})

Expand Down Expand Up @@ -151,7 +151,7 @@ func TestPGCoordinatorSingle_AgentWithClient(t *testing.T) {
agent.waitForClose(ctx, t)

// Create a new agent connection. This is to simulate a reconnect!
agent = newTestAgent(t, coordinator, agent.id)
agent = newTestAgent(t, coordinator, "reconnection", agent.id)
// Ensure the existing listening connIO sends its node immediately!
clientNodes = agent.recvNodes(ctx, t)
require.Len(t, clientNodes, 1)
Expand Down Expand Up @@ -200,7 +200,7 @@ func TestPGCoordinatorSingle_MissedHeartbeats(t *testing.T) {
require.NoError(t, err)
defer coordinator.Close()

agent := newTestAgent(t, coordinator)
agent := newTestAgent(t, coordinator, "agent")
defer agent.close()
agent.sendNode(&agpl.Node{PreferredDERP: 10})

Expand Down Expand Up @@ -333,16 +333,16 @@ func TestPGCoordinatorDual_Mainline(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitSuperLong)
defer cancel()
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
coord1, err := tailnet.NewPGCoord(ctx, logger, ps, store)
coord1, err := tailnet.NewPGCoord(ctx, logger.Named("coord1"), ps, store)
require.NoError(t, err)
defer coord1.Close()
coord2, err := tailnet.NewPGCoord(ctx, logger, ps, store)
coord2, err := tailnet.NewPGCoord(ctx, logger.Named("coord2"), ps, store)
require.NoError(t, err)
defer coord2.Close()

agent1 := newTestAgent(t, coord1)
agent1 := newTestAgent(t, coord1, "agent1")
defer agent1.close()
agent2 := newTestAgent(t, coord2)
agent2 := newTestAgent(t, coord2, "agent2")
defer agent2.close()

client11 := newTestClient(t, coord1, agent1.id)
Expand Down Expand Up @@ -460,19 +460,19 @@ func TestPGCoordinator_MultiAgent(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitSuperLong)
defer cancel()
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)
coord1, err := tailnet.NewPGCoord(ctx, logger, ps, store)
coord1, err := tailnet.NewPGCoord(ctx, logger.Named("coord1"), ps, store)
require.NoError(t, err)
defer coord1.Close()
coord2, err := tailnet.NewPGCoord(ctx, logger, ps, store)
coord2, err := tailnet.NewPGCoord(ctx, logger.Named("coord2"), ps, store)
require.NoError(t, err)
defer coord2.Close()
coord3, err := tailnet.NewPGCoord(ctx, logger, ps, store)
coord3, err := tailnet.NewPGCoord(ctx, logger.Named("coord3"), ps, store)
require.NoError(t, err)
defer coord3.Close()

agent1 := newTestAgent(t, coord1)
agent1 := newTestAgent(t, coord1, "agent1")
defer agent1.close()
agent2 := newTestAgent(t, coord2, agent1.id)
agent2 := newTestAgent(t, coord2, "agent2", agent1.id)
defer agent2.close()

client := newTestClient(t, coord3, agent1.id)
Expand Down Expand Up @@ -552,7 +552,7 @@ func TestPGCoordinator_Unhealthy(t *testing.T) {
err := uut.Close()
require.NoError(t, err)
}()
agent1 := newTestAgent(t, uut)
agent1 := newTestAgent(t, uut, "agent1")
defer agent1.close()
for i := 0; i < 3; i++ {
select {
Expand All @@ -566,7 +566,7 @@ func TestPGCoordinator_Unhealthy(t *testing.T) {
agent1.waitForClose(ctx, t)

// new agent should immediately disconnect
agent2 := newTestAgent(t, uut)
agent2 := newTestAgent(t, uut, "agent2")
defer agent2.close()
agent2.waitForClose(ctx, t)

Expand All @@ -579,7 +579,7 @@ func TestPGCoordinator_Unhealthy(t *testing.T) {
// OK
}
}
agent3 := newTestAgent(t, uut)
agent3 := newTestAgent(t, uut, "agent3")
defer agent3.close()
select {
case <-agent3.closeChan:
Expand Down Expand Up @@ -618,10 +618,10 @@ func newTestConn(ids []uuid.UUID) *testConn {
return a
}

func newTestAgent(t *testing.T, coord agpl.Coordinator, id ...uuid.UUID) *testConn {
func newTestAgent(t *testing.T, coord agpl.Coordinator, name string, id ...uuid.UUID) *testConn {
a := newTestConn(id)
go func() {
err := coord.ServeAgent(a.serverWS, a.id, "")
err := coord.ServeAgent(a.serverWS, a.id, name)
assert.NoError(t, err)
close(a.closeChan)
}()
Expand All @@ -636,7 +636,7 @@ func (c *testConn) recvNodes(ctx context.Context, t *testing.T) []*agpl.Node {
t.Helper()
select {
case <-ctx.Done():
t.Fatal("timeout receiving nodes")
t.Fatalf("testConn id %s: timeout receiving nodes ", c.id)
return nil
case nodes := <-c.nodeChan:
return nodes
Expand Down