Files
coder/coderd/x/chatd/chatstate/helpers_test.go
T
Callum Styan d412c38afb perf: validate chat transitions from the locked row (#30886)
One of the main costs in terms of DB load from chats is extra round
trips during transactions. This results in locks being held for longer
periods of time -> an increase in lock waits for chats related queries.

One of the opportunities for reducing some of the round trips is to
eliminate extra chat row reads after we have just read the row in order
to lock it.

In this PR `ChatMachine.Update` now seeds the transaction struct with
the row returned by `LockChatAndBumpSnapshotVersion`, plus a queue count
taken right after it. We can then pass it into `Update` as an optional
parameter validate against it, so neither re-reads the chat row from the
database while the row lock is held.

`updateChat` (send, edit, promote, tool results, interrupt, compact,
clear, reconcile), hook failure, family archive, and the worker's
generation and task transitions use it instead of reloading the chat
under the lock.

This PR relative to main:
|measurement | main | this PR | percent change |
|-- | -- | -- | -- |
| Round trips under lock  | 180.9 | 142.0 | -39% |
| Statements under lock  | 86.1 | 66.6 | -23% |
| GetChatByID under lock | 54.1 | 35.8 | -34% |
| GetChatByID total | 75.5 | 57.1 | -25% |
| Statements  | 235.8 | 216.2 | -8% |

Generated by Coder Agents on behalf of @cstyan.
2026-11-13 09:59:39 -07:00

128 lines
4.1 KiB
Go

package chatstate_test
import (
"cmp"
"context"
"slices"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd/database"
coderdpubsub "github.com/coder/coder/v2/coderd/pubsub"
"github.com/coder/coder/v2/coderd/x/chatd/chatstate"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
)
// ownershipPublishCount returns the number of `chat:ownership` messages
// recorded so far on the test publisher. Tests use it to assert that
// transitions do or do not publish an ownership hint.
func (r *recordingPubsub) ownershipPublishCount() int {
count := 0
for _, c := range r.channels {
if c == coderdpubsub.ChatStateOwnershipChannel {
count++
}
}
return count
}
// sendQueuedMessage seeds one queued user message via SendMessage with
// BusyBehaviorQueue. The chat must already be in a state that allows
// SendMessage (typically R0, R1, or I*).
func sendQueuedMessage(t *testing.T, f *testFixture, m *chatstate.ChatMachine, body string) chatstate.SendMessageResult {
t.Helper()
return sendMessageWithBehavior(t, f, m, body, chatstate.BusyBehaviorQueue)
}
func sendInterruptMessage(t *testing.T, f *testFixture, m *chatstate.ChatMachine, chatID uuid.UUID, body string) chatstate.SendMessageResult {
t.Helper()
ctx := testutil.Context(t, testutil.WaitShort)
var send chatstate.SendMessageResult
_, err := m.Update(ctx, func(tx *chatstate.Tx, store database.Store, _ *chatstate.ChatSnapshot) error {
if err := ownChat(ctx, tx, store, chatID); err != nil {
return err
}
if _, err := tx.Interrupt(chatstate.InterruptInput{Reason: "seed"}); err != nil {
return err
}
var err error
send, err = tx.SendMessage(chatstate.SendMessageInput{
Message: userTextMessage(body, f.User.ID, f.Model.ID),
BusyBehavior: chatstate.BusyBehaviorSteer,
MaxQueueSize: codersdk.DefaultChatMaxQueuedMessagesPerChat,
})
return err
})
require.NoError(t, err)
return send
}
func sendMessageWithBehavior(t *testing.T, f *testFixture, m *chatstate.ChatMachine, body string, behavior chatstate.BusyBehavior) chatstate.SendMessageResult {
t.Helper()
ctx := testutil.Context(t, testutil.WaitShort)
var send chatstate.SendMessageResult
_, err := m.Update(ctx, func(tx *chatstate.Tx, store database.Store, _ *chatstate.ChatSnapshot) error {
var err error
send, err = tx.SendMessage(chatstate.SendMessageInput{
Message: userTextMessage(body, f.User.ID, f.Model.ID),
BusyBehavior: behavior,
MaxQueueSize: codersdk.DefaultChatMaxQueuedMessagesPerChat,
})
return err
})
require.NoError(t, err)
return send
}
func queuedRowsByPosition(ctx context.Context, t *testing.T, f *testFixture, chatID uuid.UUID) []database.ChatQueuedMessage {
t.Helper()
rows, err := f.DB.GetChatQueuedMessages(ctx, chatID)
require.NoError(t, err)
slices.SortFunc(rows, func(a, b database.ChatQueuedMessage) int {
return cmp.Or(cmp.Compare(a.Position, b.Position), cmp.Compare(a.ID, b.ID))
})
return rows
}
func queuedIDsByPosition(ctx context.Context, t *testing.T, f *testFixture, chatID uuid.UUID) []int64 {
t.Helper()
rows := queuedRowsByPosition(ctx, t, f, chatID)
ids := make([]int64, len(rows))
for i, r := range rows {
ids[i] = r.ID
}
return ids
}
func historyMessageIDs(ctx context.Context, t *testing.T, f *testFixture, chatID uuid.UUID) []int64 {
t.Helper()
msgs, err := f.DB.GetChatMessagesByChatID(ctx, database.GetChatMessagesByChatIDParams{
ChatID: chatID,
})
require.NoError(t, err)
out := make([]int64, len(msgs))
for i, m := range msgs {
out[i] = m.ID
}
return out
}
// ownChat gives an unowned chat a worker and runner inside tx, as an
// acquired chat has. Interrupting an unowned running chat finishes the
// interruption inline, so tests of the interrupting states need an
// owner.
func ownChat(ctx context.Context, tx *chatstate.Tx, store database.Store, chatID uuid.UUID) error {
chat, err := store.GetChatByID(ctx, chatID)
if err != nil {
return err
}
if chat.WorkerID.Valid {
return nil
}
_, err = tx.Acquire(chatstate.AcquireInput{WorkerID: uuid.New(), RunnerID: uuid.New()})
return err
}