Files
coder/coderd/x/chatd/chatstate/queue_client_order_test.go
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

112 lines
4.2 KiB
Go

package chatstate_test
import (
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/x/chatd/chatstate"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/testutil"
)
// clientQueueIDs returns the queue in the order clients receive it: the
// stream snapshot (stream_loop.go) and the messages API
// (coderd/exp_chats.go) both read GetChatQueuedMessages.
func clientQueueIDs(t *testing.T, f *testFixture, chatID uuid.UUID) []int64 {
t.Helper()
ctx := testutil.Context(t, testutil.WaitShort)
rows, err := f.DB.GetChatQueuedMessages(ctx, chatID)
require.NoError(t, err)
ids := make([]int64, len(rows))
for i, r := range rows {
ids[i] = r.ID
}
return ids
}
func TestClientQueueOrderMatchesPromotionOrder(t *testing.T) {
t.Parallel()
t.Run("SteerWhileRunning", func(t *testing.T) {
t.Parallel()
f := newTestFixture(t)
ctx := testutil.Context(t, testutil.WaitShort)
created := createTestChat(t, f)
m := chatstate.NewChatMachine(f.DB, f.Pub, created.Chat.ID)
first := sendQueuedMessage(t, f, m, "first")
second := sendQueuedMessage(t, f, m, "second")
require.Equal(t, chatstate.StateR1, f.classify(ctx, t, created.Chat.ID))
_, err := m.Update(ctx, func(tx *chatstate.Tx, _ database.Store, _ *chatstate.ChatSnapshot) error {
_, err := tx.SteerQueuedMessage(chatstate.SteerQueuedMessageInput{
QueuedMessageID: second.QueuedMessage.ID,
})
return err
})
require.NoError(t, err)
require.Equal(t, chatstate.StateR1, f.classify(ctx, t, created.Chat.ID))
require.Equal(t, []int64{first.QueuedMessage.ID, second.QueuedMessage.ID}, queuedIDsByPosition(ctx, t, f, created.Chat.ID))
clientOrder := clientQueueIDs(t, f, created.Chat.ID)
require.Equal(t, []int64{second.QueuedMessage.ID, first.QueuedMessage.ID}, clientOrder,
"clients see the steered message first")
var finish chatstate.FinishTurnResult
_, err = m.Update(ctx, func(tx *chatstate.Tx, _ database.Store, _ *chatstate.ChatSnapshot) error {
var err error
finish, err = tx.FinishTurn(chatstate.FinishTurnInput{})
return err
})
require.NoError(t, err)
require.Len(t, finish.PromotedMessages, 1)
require.Equal(t, clientOrder[0], finish.PromotedMessages[0].QueuedMessageID.Int64,
"the first message clients see is promoted first")
})
// Two senders: the first transaction starts (fixing now() and so
// created_at) before the second, but takes the chat lock after it.
// In production the window is the round trip between BEGIN and the
// lock statement; the outer transaction widens it.
t.Run("ConcurrentSenders", func(t *testing.T) {
t.Parallel()
f := newTestFixture(t)
ctx := testutil.Context(t, testutil.WaitShort)
created := createTestChat(t, f)
m := chatstate.NewChatMachine(f.DB, f.Pub, created.Chat.ID)
var early, late chatstate.SendMessageResult
require.NoError(t, f.DB.InTx(func(tx database.Store) error {
// The first statement pins the transaction start time.
if _, err := tx.GetDatabaseNow(ctx); err != nil {
return err
}
// The other sender commits on its own connection meanwhile.
late = sendQueuedMessage(t, f, m, "committed first")
// Update reuses this transaction and only now locks the chat.
_, err := chatstate.NewChatMachine(tx, f.Pub, created.Chat.ID).Update(ctx, func(stx *chatstate.Tx, _ database.Store, _ *chatstate.ChatSnapshot) error {
var err error
early, err = stx.SendMessage(chatstate.SendMessageInput{
Message: userTextMessage("began first", f.User.ID, f.Model.ID),
BusyBehavior: chatstate.BusyBehaviorQueue,
MaxQueueSize: codersdk.DefaultChatMaxQueuedMessagesPerChat,
})
return err
})
return err
}, nil))
require.NotNil(t, early.QueuedMessage)
require.NotNil(t, late.QueuedMessage)
require.True(t, early.QueuedMessage.CreatedAt.Before(late.QueuedMessage.CreatedAt),
"the scenario needs created_at to disagree with position")
promotionOrder := queuedIDsByPosition(ctx, t, f, created.Chat.ID)
require.Equal(t, []int64{late.QueuedMessage.ID, early.QueuedMessage.ID}, promotionOrder)
require.Equal(t, promotionOrder, clientQueueIDs(t, f, created.Chat.ID),
"clients see the later-locked message first, but it is promoted second")
})
}