Skip to content

Commit dd8eab5

Browse files
authored
fix: cache disconnected agent names in tailnet coordinator debug (#5870)
1 parent 16d8cc4 commit dd8eab5

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

coderd/workspaceagents.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,15 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request
531531
return
532532
}
533533

534+
owner, err := api.Database.GetUserByID(ctx, workspace.OwnerID)
535+
if err != nil {
536+
httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{
537+
Message: "Internal error fetching user.",
538+
Detail: err.Error(),
539+
})
540+
return
541+
}
542+
534543
// Ensure the resource is still valid!
535544
// We only accept agents for resources on the latest build.
536545
ensureLatestBuild := func() error {
@@ -628,7 +637,9 @@ func (api *API) workspaceAgentCoordinate(rw http.ResponseWriter, r *http.Request
628637
closeChan := make(chan struct{})
629638
go func() {
630639
defer close(closeChan)
631-
err := (*api.TailnetCoordinator.Load()).ServeAgent(wsNetConn, workspaceAgent.ID, fmt.Sprintf("%s-%s", workspace.Name, workspaceAgent.Name))
640+
err := (*api.TailnetCoordinator.Load()).ServeAgent(wsNetConn, workspaceAgent.ID,
641+
fmt.Sprintf("%s-%s-%s", owner.Username, workspace.Name, workspaceAgent.Name),
642+
)
632643
if err != nil {
633644
api.Logger.Warn(ctx, "tailnet coordinator agent error", slog.Error(err))
634645
_ = conn.Close(websocket.StatusInternalError, err.Error())

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ require (
9696
github.com/google/uuid v1.3.0
9797
github.com/hashicorp/go-reap v0.0.0-20170704170343-bf58d8a43e7b
9898
github.com/hashicorp/go-version v1.6.0
99+
github.com/hashicorp/golang-lru/v2 v2.0.1
99100
github.com/hashicorp/hc-install v0.4.1-0.20220912074615-4487b02cbcbb
100101
github.com/hashicorp/hcl/v2 v2.14.0
101102
github.com/hashicorp/terraform-config-inspect v0.0.0-20211115214459-90acf1ca460f

go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,8 @@ github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA
10201020
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
10211021
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
10221022
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
1023+
github.com/hashicorp/golang-lru/v2 v2.0.1 h1:5pv5N1lT1fjLg2VQ5KWc7kmucp2x/kvFOnxuVTqZ6x4=
1024+
github.com/hashicorp/golang-lru/v2 v2.0.1/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
10231025
github.com/hashicorp/hc-install v0.4.1-0.20220912074615-4487b02cbcbb h1:0AmumMAu6gi5zXEyXvLKDu/HALK+rIcVBZU5XJNyjRM=
10241026
github.com/hashicorp/hc-install v0.4.1-0.20220912074615-4487b02cbcbb/go.mod h1:b3vG+IG40BBISnWiQb9/nHqZI/N3oiunwTtyTDaMGOA=
10251027
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w=

tailnet/coordinator.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"time"
1515

1616
"github.com/google/uuid"
17+
lru "github.com/hashicorp/golang-lru/v2"
1718
"golang.org/x/exp/slices"
1819
"golang.org/x/xerrors"
1920
"tailscale.com/tailcfg"
@@ -109,11 +110,17 @@ func ServeCoordinator(conn net.Conn, updateNodes func(node []*Node) error) (func
109110
// coordinator is incompatible with multiple Coder replicas as all node data is
110111
// in-memory.
111112
func NewCoordinator() Coordinator {
113+
cache, err := lru.New[uuid.UUID, string](512)
114+
if err != nil {
115+
panic("make lru cache: " + err.Error())
116+
}
117+
112118
return &coordinator{
113119
closed: false,
114120
nodes: map[uuid.UUID]*Node{},
115121
agentSockets: map[uuid.UUID]*trackedConn{},
116122
agentToConnectionSockets: map[uuid.UUID]map[uuid.UUID]*trackedConn{},
123+
agentNameCache: cache,
117124
}
118125
}
119126

@@ -135,6 +142,10 @@ type coordinator struct {
135142
// agentToConnectionSockets maps agent IDs to connection IDs of conns that
136143
// are subscribed to updates for that agent.
137144
agentToConnectionSockets map[uuid.UUID]map[uuid.UUID]*trackedConn
145+
146+
// agentNameCache holds a cache of agent names. If one of them disappears,
147+
// it's helpful to have a name cached for debugging.
148+
agentNameCache *lru.Cache[uuid.UUID, string]
138149
}
139150

140151
type trackedConn struct {
@@ -288,6 +299,8 @@ func (c *coordinator) ServeAgent(conn net.Conn, id uuid.UUID, name string) error
288299
return xerrors.New("coordinator is closed")
289300
}
290301

302+
c.agentNameCache.Add(id, name)
303+
291304
sockets, ok := c.agentToConnectionSockets[id]
292305
if ok {
293306
// Publish all nodes that want to connect to the
@@ -532,7 +545,13 @@ func (c *coordinator) ServeHTTPDebug(w http.ResponseWriter, _ *http.Request) {
532545
fmt.Fprintln(w, "<ul>")
533546

534547
for _, agentConns := range missingAgents {
535-
fmt.Fprintf(w, "<li style=\"margin-top:4px\"><b>unknown</b> (<code>%s</code>): created ? ago, write ? ago, overwrites ? </li>\n",
548+
agentName, ok := c.agentNameCache.Get(agentConns.id)
549+
if !ok {
550+
agentName = "unknown"
551+
}
552+
553+
fmt.Fprintf(w, "<li style=\"margin-top:4px\"><b>%s</b> (<code>%s</code>): created ? ago, write ? ago, overwrites ? </li>\n",
554+
agentName,
536555
agentConns.id.String(),
537556
)
538557

0 commit comments

Comments
 (0)