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.
340 lines
13 KiB
Go
340 lines
13 KiB
Go
package chatstate_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/xerrors"
|
|
|
|
"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/testutil"
|
|
)
|
|
|
|
// publishedOn returns the indices into f.Pub.channels (and f.Pub.payloads)
|
|
// that match the given channel name, in order.
|
|
func publishedOn(f *testFixture, channel string) []int {
|
|
var idx []int
|
|
for i, c := range f.Pub.channels {
|
|
if c == channel {
|
|
idx = append(idx, i)
|
|
}
|
|
}
|
|
return idx
|
|
}
|
|
|
|
// TestCreateChatPublishesAfterCommit asserts that a successful
|
|
// CreateChat call publishes exactly one chat:update message on the
|
|
// per-chat channel after the inner transaction commits.
|
|
func TestCreateChatPublishesAfterCommit(t *testing.T) {
|
|
t.Parallel()
|
|
f := newTestFixture(t)
|
|
res := createTestChat(t, f)
|
|
|
|
channel := coderdpubsub.ChatStateUpdateChannel(res.Chat.ID)
|
|
idx := publishedOn(f, channel)
|
|
require.Len(t, idx, 1, "exactly one chat:update for the new chat")
|
|
|
|
var msg coderdpubsub.ChatStateUpdateMessage
|
|
require.NoError(t, json.Unmarshal(f.Pub.payloads[idx[0]], &msg))
|
|
require.Equal(t, res.Chat.SnapshotVersion, msg.SnapshotVersion)
|
|
require.Equal(t, string(database.ChatStatusRunning), msg.Status)
|
|
}
|
|
|
|
// TestUpdatePublishesAfterCommit asserts that ChatMachine.Update
|
|
// publishes one chat:update on the per-chat channel after the inner
|
|
// transaction commits, even when the callback performs no transition.
|
|
func TestUpdatePublishesAfterCommit(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)
|
|
|
|
createIdx := publishedOn(f, coderdpubsub.ChatStateUpdateChannel(created.Chat.ID))
|
|
require.Len(t, createIdx, 1, "create published one chat:update")
|
|
|
|
_, err := m.Update(ctx, func(_ *chatstate.Tx, _ database.Store, _ *chatstate.ChatSnapshot) error { return nil })
|
|
require.NoError(t, err)
|
|
|
|
updIdx := publishedOn(f, coderdpubsub.ChatStateUpdateChannel(created.Chat.ID))
|
|
require.Len(t, updIdx, 2, "no-op Update still publishes a chat:update")
|
|
|
|
after, err := f.DB.GetChatByID(ctx, created.Chat.ID)
|
|
require.NoError(t, err)
|
|
var msg coderdpubsub.ChatStateUpdateMessage
|
|
require.NoError(t, json.Unmarshal(f.Pub.payloads[updIdx[1]], &msg))
|
|
require.Equal(t, after.SnapshotVersion, msg.SnapshotVersion)
|
|
}
|
|
|
|
// TestUpdatePublishesOneFinalChatUpdateForTransitionBundle bundles
|
|
// several transitions inside one Update callback and verifies the
|
|
// commit publishes exactly one chat:update on the per-chat channel
|
|
// (not one per transition).
|
|
func TestUpdatePublishesOneFinalChatUpdateForTransitionBundle(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)
|
|
|
|
baseUpdates := len(publishedOn(f, coderdpubsub.ChatStateUpdateChannel(created.Chat.ID)))
|
|
|
|
// chatstate.StateR0 -> chatstate.StateW (FinishTurn) ->
|
|
// chatstate.StateXW (SetArchived true) -> chatstate.StateW
|
|
// (SetArchived false).
|
|
_, err := m.Update(ctx, func(tx *chatstate.Tx, store database.Store, _ *chatstate.ChatSnapshot) error {
|
|
if _, err := tx.FinishTurn(chatstate.FinishTurnInput{}); err != nil {
|
|
return err
|
|
}
|
|
if _, err := tx.SetArchived(chatstate.SetArchivedInput{Archived: true}); err != nil {
|
|
return err
|
|
}
|
|
if _, err := tx.SetArchived(chatstate.SetArchivedInput{Archived: false}); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
updIdx := publishedOn(f, coderdpubsub.ChatStateUpdateChannel(created.Chat.ID))
|
|
require.Equal(t, baseUpdates+1, len(updIdx),
|
|
"three-transition bundle publishes exactly one final chat:update")
|
|
|
|
after, err := f.DB.GetChatByID(ctx, created.Chat.ID)
|
|
require.NoError(t, err)
|
|
require.Equal(t, database.ChatStatusWaiting, after.Status,
|
|
"ends in "+chatstate.StateW.String())
|
|
}
|
|
|
|
// TestUpdateAppliesTransitionBundleSequentially verifies that
|
|
// transitions chained inside a single Update callback see each
|
|
// other's effects: later transitions validate against the state
|
|
// produced by earlier ones (chatstate.StateR0 -> chatstate.StateW
|
|
// is rejected when called twice because the second call sees
|
|
// chatstate.StateW and FinishTurn is no longer allowed).
|
|
func TestUpdateAppliesTransitionBundleSequentially(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)
|
|
|
|
_, err := m.Update(ctx, func(tx *chatstate.Tx, store database.Store, _ *chatstate.ChatSnapshot) error {
|
|
if _, err := tx.FinishTurn(chatstate.FinishTurnInput{}); err != nil {
|
|
return err
|
|
}
|
|
// Second FinishTurn should fail because state is now chatstate.StateW.
|
|
_, err := tx.FinishTurn(chatstate.FinishTurnInput{})
|
|
return err
|
|
})
|
|
require.Error(t, err)
|
|
require.ErrorIs(t, err, chatstate.ErrTransitionNotAllowed)
|
|
|
|
// Failed bundle rolls back: state must not have advanced past
|
|
// chatstate.StateR0.
|
|
require.Equal(t, chatstate.StateR0, f.classify(ctx, t, created.Chat.ID),
|
|
"failed bundle rolls back the whole transaction")
|
|
}
|
|
|
|
// TestFailedUpdatePublishesNothing verifies that a callback error
|
|
// rolls back the snapshot bump and publishes nothing.
|
|
func TestFailedUpdatePublishesNothing(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)
|
|
|
|
publishedBefore := len(f.Pub.channels)
|
|
beforeChat := f.readChat(ctx, t, created.Chat.ID)
|
|
|
|
sentinel := xerrors.New("forced failure")
|
|
_, err := m.Update(ctx, func(_ *chatstate.Tx, _ database.Store, _ *chatstate.ChatSnapshot) error { return sentinel })
|
|
require.ErrorIs(t, err, sentinel)
|
|
require.Equal(t, publishedBefore, len(f.Pub.channels), "failed update publishes nothing")
|
|
|
|
after := f.readChat(ctx, t, created.Chat.ID)
|
|
require.Equal(t, beforeChat.SnapshotVersion, after.SnapshotVersion,
|
|
"failed update rolls back snapshot bump")
|
|
}
|
|
|
|
// TestChatUpdateMessagePayloadShape verifies the JSON shape of the
|
|
// chat:update payload contains every field consumers depend on:
|
|
// snapshot_version, history_version, queue_version,
|
|
// retry_state_version, generation_attempt, status, archived, and
|
|
// worker_id / runner_id, with explicit nulls when unowned.
|
|
func TestChatUpdateMessagePayloadShape(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)
|
|
channel := coderdpubsub.ChatStateUpdateChannel(created.Chat.ID)
|
|
|
|
// The create update is unowned and must still include explicit
|
|
// null ownership fields.
|
|
createIdx := publishedOn(f, channel)
|
|
require.NotEmpty(t, createIdx)
|
|
var createRaw map[string]json.RawMessage
|
|
require.NoError(t, json.Unmarshal(f.Pub.payloads[createIdx[0]], &createRaw))
|
|
require.JSONEq(t, `null`, string(createRaw["worker_id"]))
|
|
require.JSONEq(t, `null`, string(createRaw["runner_id"]))
|
|
|
|
// Acquire ownership so worker_id and runner_id are present.
|
|
worker := uuid.New()
|
|
runner := uuid.New()
|
|
_, err := m.Update(ctx, func(tx *chatstate.Tx, store database.Store, _ *chatstate.ChatSnapshot) error {
|
|
_, err := tx.Acquire(chatstate.AcquireInput{WorkerID: worker, RunnerID: runner})
|
|
return err
|
|
})
|
|
require.NoError(t, err)
|
|
|
|
// Find the last chat:update message.
|
|
idx := publishedOn(f, channel)
|
|
require.NotEmpty(t, idx)
|
|
last := f.Pub.payloads[idx[len(idx)-1]]
|
|
|
|
// Strict-decode against the typed struct.
|
|
var typed coderdpubsub.ChatStateUpdateMessage
|
|
require.NoError(t, json.Unmarshal(last, &typed))
|
|
require.Greater(t, typed.SnapshotVersion, int64(0))
|
|
require.NotNil(t, typed.WorkerID)
|
|
require.Equal(t, worker, *typed.WorkerID)
|
|
require.NotNil(t, typed.RunnerID)
|
|
require.Equal(t, runner, *typed.RunnerID)
|
|
require.Equal(t, string(database.ChatStatusRunning), typed.Status)
|
|
require.False(t, typed.Archived)
|
|
|
|
// Permissive decode to assert exact JSON keys.
|
|
var raw map[string]json.RawMessage
|
|
require.NoError(t, json.Unmarshal(last, &raw))
|
|
for _, key := range []string{
|
|
"snapshot_version",
|
|
"history_version",
|
|
"queue_version",
|
|
"retry_state_version",
|
|
"generation_attempt",
|
|
"status",
|
|
"archived",
|
|
"worker_id",
|
|
"runner_id",
|
|
} {
|
|
_, ok := raw[key]
|
|
require.True(t, ok, "payload missing key %q", key)
|
|
}
|
|
}
|
|
|
|
// TestChatOwnershipMessagePayloadShape verifies the JSON shape of
|
|
// chat:ownership: chat_id and snapshot_version.
|
|
func TestChatOwnershipMessagePayloadShape(t *testing.T) {
|
|
t.Parallel()
|
|
f := newTestFixture(t)
|
|
// CreateChat publishes one ownership hint because the new chat is
|
|
// unowned and runnable.
|
|
created := createTestChat(t, f)
|
|
|
|
idx := publishedOn(f, coderdpubsub.ChatStateOwnershipChannel)
|
|
require.NotEmpty(t, idx, "CreateChat publishes at least one chat:ownership hint")
|
|
|
|
payload := f.Pub.payloads[idx[len(idx)-1]]
|
|
var typed coderdpubsub.ChatStateOwnershipMessage
|
|
require.NoError(t, json.Unmarshal(payload, &typed))
|
|
require.Equal(t, created.Chat.ID, typed.ChatID)
|
|
require.Greater(t, typed.SnapshotVersion, int64(0))
|
|
|
|
var raw map[string]json.RawMessage
|
|
require.NoError(t, json.Unmarshal(payload, &raw))
|
|
for _, key := range []string{"chat_id", "snapshot_version"} {
|
|
_, ok := raw[key]
|
|
require.True(t, ok, "ownership payload missing key %q", key)
|
|
}
|
|
}
|
|
|
|
// TestOwnershipNotificationUsesDatabaseHeartbeatStaleness verifies
|
|
// that an ownership hint fires when the heartbeat is stale by the
|
|
// database's clock, regardless of what the local Go clock says. We
|
|
// rewrite the heartbeat row to a deterministically old timestamp via
|
|
// raw SQL and confirm the post-commit hint is sent on a subsequent
|
|
// runnable Update.
|
|
func TestOwnershipNotificationUsesDatabaseHeartbeatStaleness(t *testing.T) {
|
|
t.Parallel()
|
|
tf := newTriggerFixture(t)
|
|
f := tf.f
|
|
ctx := testutil.Context(t, testutil.WaitShort)
|
|
created := createTestChat(t, f)
|
|
m := chatstate.NewChatMachine(f.DB, f.Pub, created.Chat.ID)
|
|
|
|
// Acquire ownership; this writes a fresh heartbeat.
|
|
worker := uuid.New()
|
|
runner := uuid.New()
|
|
_, err := m.Update(ctx, func(tx *chatstate.Tx, store database.Store, _ *chatstate.ChatSnapshot) error {
|
|
_, err := tx.Acquire(chatstate.AcquireInput{WorkerID: worker, RunnerID: runner})
|
|
return err
|
|
})
|
|
require.NoError(t, err)
|
|
hb, err := f.DB.GetChatHeartbeat(ctx, database.GetChatHeartbeatParams{
|
|
ChatID: created.Chat.ID,
|
|
RunnerID: runner,
|
|
})
|
|
require.NoError(t, err)
|
|
require.WithinDuration(t, time.Now(), hb.HeartbeatAt, time.Minute,
|
|
"Acquire wrote a fresh heartbeat")
|
|
|
|
// Snapshot ownership-hint count before the test trigger.
|
|
ownershipBefore := f.Pub.ownershipPublishCount()
|
|
|
|
// Force the heartbeat to a deterministically old time.
|
|
_, err = tf.sqlDB.ExecContext(ctx, `
|
|
UPDATE chat_heartbeats
|
|
SET heartbeat_at = NOW() - INTERVAL '1 hour'
|
|
WHERE chat_id = $1 AND runner_id = $2
|
|
`, created.Chat.ID, runner)
|
|
require.NoError(t, err)
|
|
|
|
// Confirm database-side staleness check agrees.
|
|
stale, err := f.DB.IsChatHeartbeatStale(ctx, database.IsChatHeartbeatStaleParams{
|
|
ChatID: created.Chat.ID,
|
|
RunnerID: runner,
|
|
StaleSeconds: chatstate.HeartbeatStaleSeconds,
|
|
})
|
|
require.NoError(t, err)
|
|
require.True(t, stale, "heartbeat is stale per database time")
|
|
|
|
// Run a no-op Update. The chat is runnable (chatstate.StateR0)
|
|
// and the heartbeat is stale, so post-commit logic must publish
|
|
// exactly one chat:ownership hint.
|
|
_, err = m.Update(ctx, func(_ *chatstate.Tx, _ database.Store, _ *chatstate.ChatSnapshot) error { return nil })
|
|
require.NoError(t, err)
|
|
|
|
ownershipAfter := f.Pub.ownershipPublishCount()
|
|
require.Equal(t, ownershipBefore+1, ownershipAfter,
|
|
"stale heartbeat triggers a fresh ownership hint")
|
|
}
|
|
|
|
// TestUpdateContextCancellationPublishesNothing verifies that
|
|
// canceling the caller's context (between the inner commit and the
|
|
// publish loop's first call) does not corrupt state. We exercise the
|
|
// simpler observable contract: when the user cancels before Update
|
|
// gets to do anything, nothing is published. The strict before-publish
|
|
// race is exercised in concurrency tests with channel sync.
|
|
func TestUpdateContextCancellationPublishesNothing(t *testing.T) {
|
|
t.Parallel()
|
|
f := newTestFixture(t)
|
|
created := createTestChat(t, f)
|
|
m := chatstate.NewChatMachine(f.DB, f.Pub, created.Chat.ID)
|
|
|
|
publishedBefore := len(f.Pub.channels)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
cancel()
|
|
_, err := m.Update(ctx, func(_ *chatstate.Tx, _ database.Store, _ *chatstate.ChatSnapshot) error { return nil })
|
|
require.Error(t, err)
|
|
require.Equal(t, publishedBefore, len(f.Pub.channels),
|
|
"caller-aborted update publishes nothing")
|
|
}
|