| index 899a1b1f9..a3d8ed537 100644 |
| --- a/internal/console/opstore_test.go |
| +++ b/internal/console/opstore_test.go |
| @@ -140,18 +140,18 @@ var opQueryPlans = []opPlanCase{ |
| "SEARCH memberships USING INDEX memberships_by_address (address=?)", |
| "SEARCH accounts USING INTEGER PRIMARY KEY (rowid=?)", |
| "SEARCH apps USING INDEX apps_by_creator (created_by=?)"}, |
| - notWant: []string{"SCAN memberships", "SCAN apps", "SCAN accounts"}}, |
| + notWant: []string{"SCAN memberships", "SCAN apps", "SCAN accounts", "SEARCH page USING AUTOMATIC"}}, |
| {name: "people page", tieSort: true, sql: opSQLPeoplePage, args: []any{"", "%%", 50, 0}, |
| want: []string{"SCAN users USING INDEX users_by_created", |
| "SEARCH memberships USING INDEX memberships_by_address (address=?)", |
| "SEARCH accounts USING INTEGER PRIMARY KEY (rowid=?)", |
| "SEARCH apps USING INDEX apps_by_creator (created_by=?)"}, |
| - notWant: []string{"SCAN memberships", "SCAN apps", "SCAN accounts"}}, |
| + notWant: []string{"SCAN memberships", "SCAN apps", "SCAN accounts", "SEARCH page USING AUTOMATIC"}}, |
| {name: "people page searched", tieSort: true, sql: opSQLPeoplePage, args: []any{"exam", `%exam%`, 50, 0}, |
| want: []string{"SCAN users", |
| "SEARCH memberships USING INDEX memberships_by_address (address=?)", |
| "SEARCH apps USING INDEX apps_by_creator (created_by=?)"}, |
| - notWant: []string{"SCAN memberships", "SCAN apps"}}, |
| + notWant: []string{"SCAN memberships", "SCAN apps", "SEARCH page USING AUTOMATIC"}}, |
| {name: "accounts total", sql: opSQLAccountsTotal, args: []any{"", "%%"}, |
| want: []string{"SCAN accounts"}}, |
| {name: "accounts page", sql: opSQLAccountsPage, args: []any{"", "%%", 50, 0}, |
| @@ -159,7 +159,7 @@ var opQueryPlans = []opPlanCase{ |
| "SEARCH account_plans USING INTEGER PRIMARY KEY (rowid=?)", |
| "SEARCH apps USING INDEX sqlite_autoindex_apps_1 (account_id=?)", |
| "SEARCH memberships USING COVERING INDEX sqlite_autoindex_memberships_1 (account_id=?)"}, |
| - notWant: []string{"SCAN apps", "SCAN memberships", "SCAN account_plans"}}, |
| + notWant: []string{"SCAN apps", "SCAN memberships", "SCAN account_plans", "SEARCH page USING AUTOMATIC"}}, |
| {name: "account by sqid", sql: opSQLAccountBySqid, args: []any{"abcd"}, |
| want: []string{"SEARCH accounts USING INDEX sqlite_autoindex_accounts_1 (sqid=?)"}}, |
| {name: "touch last seen", sql: opSQLTouchLastSeen, args: []any{int64(2), "ann@example.com", int64(2)}, |
| @@ -202,7 +202,7 @@ var opSQLByName = map[string]string{ |
| "opSQLFeedAll": opSQLFeedAll, "opSQLFeedBefore": opSQLFeedBefore, "opSQLFeedAccount": opSQLFeedAccount, |
| "opSQLFeedAccountActions": opSQLFeedAccountActions, "opSQLFeedAccountOther": opSQLFeedAccountOther, |
| "opSQLFeedAction": opSQLFeedAction, "opSQLFeedOther": opSQLFeedOther, "opSQLFeedOtherBefore": opSQLFeedOtherBefore, |
| - "opSQLPersonColumns": "", "opSQLPersonJoins": "", "opSQLFeedColumns": "", |
| + "opSQLPersonPage": "", "opSQLPersonLookups": "", "opSQLFeedColumns": "", |
| } |
| |
| // opPlan is the EXPLAIN QUERY PLAN detail lines of one statement. |
| @@ -312,6 +312,13 @@ WHERE last_seen >= ?1 OR EXISTS (SELECT 1 FROM sessions WHERE sessions.address = |
| if _, problems, err := opPlanProblems(s.db, exists); err != nil || !opHas(problems, "has CORRELATED") { |
| t.Errorf("the EXISTS form was not refused: %v %v", problems, err) |
| } |
| + // With a plain JOIN in place of CROSS JOIN, the people lists' lookups |
| + // walk all of memberships and apps with the page as an automatic index. |
| + plain := opPlanCase{name: "newest people, plain JOIN", sql: strings.ReplaceAll(opSQLNewestPeople, "CROSS JOIN", "JOIN"), |
| + args: []any{50}, tieSort: true, notWant: []string{"SCAN memberships", "SCAN apps"}} |
| + if _, problems, err := opPlanProblems(s.db, plain); err != nil || !opHas(problems, "has SCAN memberships") { |
| + t.Errorf("the plain JOIN form was not refused: %v %v", problems, err) |
| + } |
| // Without sessions_by_time, the active count's sessions half scans. |
| _, err = s.db.Exec(`DROP INDEX sessions_by_time`) |
| opMust(t, err) |
| @@ -424,18 +431,18 @@ func TestNewPeoplePerDayFillsZeroDaysAndIgnoresFuture(t *testing.T) { |
| |
| func TestNewestPeopleAndPeoplePage(t *testing.T) { |
| s := openOpStore(t) |
| - for i, a := range []string{"ann@example.com", "bob@keymail.dev", "cy@keymail.dev", "di@keymail.dev", "ed@keymail.dev"} { |
| + for i, a := range []string{"ann@example.com", "bob@example.org", "cy@example.net", "di@example.org", "ed@example.net"} { |
| opUser(t, s, a, opNow.Add(time.Duration(i-10)*time.Hour)) |
| } |
| acct := testAccountID() |
| - opMust(t, s.CreateAccount(acct, "aaaa", "shop", "ed@keymail.dev", opNow.Add(-time.Hour))) |
| - if _, err := s.CreateAppIn(acct, "shop", "ed@keymail.dev", opNow); err != nil { |
| + opMust(t, s.CreateAccount(acct, "aaaa", "shop", "ed@example.net", opNow.Add(-time.Hour))) |
| + if _, err := s.CreateAppIn(acct, "shop", "ed@example.net", opNow); err != nil { |
| t.Fatal(err) |
| } |
| - opMust(t, s.TouchLastSeen("ed@keymail.dev", opNow)) |
| + opMust(t, s.TouchLastSeen("ed@example.net", opNow)) |
| newest, err := s.NewestPeople(2) |
| opMust(t, err) |
| - if len(newest) != 2 || newest[0].Address != "ed@keymail.dev" || newest[1].Address != "di@keymail.dev" { |
| + if len(newest) != 2 || newest[0].Address != "ed@example.net" || newest[1].Address != "di@example.org" { |
| t.Fatalf("NewestPeople(2) = %+v", newest) |
| } |
| ed := newest[0] |
| @@ -448,7 +455,7 @@ func TestNewestPeopleAndPeoplePage(t *testing.T) { |
| } |
| page, total, err := s.PeoplePage("", 0, 2) |
| opMust(t, err) |
| - if total != 5 || len(page) != 2 || page[0].Address != "ed@keymail.dev" { |
| + if total != 5 || len(page) != 2 || page[0].Address != "ed@example.net" { |
| t.Fatalf("PeoplePage = %+v, %d", page, total) |
| } |
| page, total, err = s.PeoplePage("", 4, 2) |
| @@ -456,10 +463,10 @@ func TestNewestPeopleAndPeoplePage(t *testing.T) { |
| if total != 5 || len(page) != 1 || page[0].Address != "ann@example.com" { |
| t.Fatalf("PeoplePage offset 4 = %+v, %d", page, total) |
| } |
| - page, total, err = s.PeoplePage("exam", 0, 50) |
| + page, total, err = s.PeoplePage("n@exam", 0, 50) |
| opMust(t, err) |
| if total != 1 || len(page) != 1 || page[0].Address != "ann@example.com" { |
| - t.Fatalf("PeoplePage(exam) = %+v, %d", page, total) |
| + t.Fatalf("PeoplePage(n@exam) = %+v, %d", page, total) |
| } |
| } |
| |
| @@ -754,7 +761,9 @@ func opSeedScale(t *testing.T, s *Store, now time.Time) (account int64) { |
| // counts deduplicate every address signed in during the window: 60,000 |
| // sessions for the 30-day count, which this SQLite does as a MERGE of two |
| // sorted halves in about 40 ms on its own (reading the same 60,000 index |
| -// entries without the dedup takes 2 ms). The other reads keep 20 ms. |
| +// entries without the dedup takes 2 ms). Allowed by controller ruling |
| +// 2026-10-26: an operator-only page, still inside the 150 ms screen budget. |
| +// The other reads keep 20 ms. |
| const opCountsScaleBudget = 100 * time.Millisecond |
| |
| // TestOperatorCountsAtScale times every operator read on a production-sized |
| @@ -858,3 +867,121 @@ func TestFirstAccountIsTheEarliestLiveMembership(t *testing.T) { |
| t.Fatalf("NewestPeople = %+v, want first account shop, 2 accounts, 2 apps", got) |
| } |
| } |
| + |
| +// TestPeopleAndAccountsWithConcentratedOwnership: one person in 2,000 |
| +// accounts who created 8,000 apps, and one account holding all those apps |
| +// and 2,000 members. Each relationship is counted on its own, so neither |
| +// list multiplies one by the other; under CI each page answers within |
| +// 20 ms. |
| +func TestPeopleAndAccountsWithConcentratedOwnership(t *testing.T) { |
| + s := openOpStore(t) |
| + db, ok := s.db.(*sql.DB) |
| + if !ok { |
| + t.Fatalf("store handle is %T", s.db) |
| + } |
| + const big = "owner@example.com" |
| + const hub = int64(5000) |
| + n := opNow.Unix() |
| + tx, err := db.Begin() |
| + opMust(t, err) |
| + defer tx.Rollback() |
| + ins := func(q string, args ...any) { |
| + if _, err := tx.Exec(q, args...); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + ins(`INSERT INTO users (address, created_at) VALUES (?, ?)`, big, n) |
| + for i := 0; i < 2000; i++ { |
| + id := hub + int64(i) |
| + ins(`INSERT INTO accounts (id, sqid, name, created_at) VALUES (?, ?, ?, ?)`, id, fmt.Sprintf("c%04d", i), fmt.Sprintf("shop %d", i), n) |
| + ins(`INSERT INTO memberships (account_id, address, role, added_at) VALUES (?, ?, ?, ?)`, id, big, RoleOwner, n-int64(i)) |
| + if i > 0 { |
| + who := fmt.Sprintf("m%04d@example.org", i) |
| + ins(`INSERT INTO users (address, created_at) VALUES (?, ?)`, who, n-int64(i)) |
| + ins(`INSERT INTO memberships (account_id, address, role, added_at) VALUES (?, ?, ?, ?)`, hub, who, RoleMember, n) |
| + } |
| + } |
| + for i := 0; i < 8000; i++ { |
| + if _, err := insertAppRow(tx, newAppRow{AccountID: hub, App: fmt.Sprintf("app%04d", i), CreatedBy: big, CreatedAt: n}); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + opMust(t, tx.Commit()) |
| + |
| + timed := func(name string, run func() error) { |
| + t.Helper() |
| + opMust(t, run()) // warm |
| + start := time.Now() |
| + opMust(t, run()) |
| + took := time.Since(start) |
| + t.Logf("%-12s %8.2f ms", name, float64(took.Microseconds())/1000) |
| + if perfEnforce() && took > 20*time.Millisecond { |
| + t.Errorf("%s took %v, over 20 ms", name, took) |
| + } |
| + } |
| + var people []PersonRow |
| + timed("NewestPeople", func() (err error) { people, err = s.NewestPeople(1); return }) |
| + if len(people) != 1 || people[0].Address != big || people[0].Accounts != 2000 || people[0].Apps != 8000 || |
| + people[0].FirstAccount != "shop 1999" { |
| + t.Fatalf("NewestPeople(1) = %+v", people) |
| + } |
| + timed("PeoplePage", func() (err error) { people, _, err = s.PeoplePage("owner", 0, 50); return }) |
| + if len(people) != 1 || people[0].Accounts != 2000 || people[0].Apps != 8000 { |
| + t.Fatalf("PeoplePage(owner) = %+v", people) |
| + } |
| + var accounts []AccountRow |
| + timed("AccountsPage", func() (err error) { accounts, _, err = s.AccountsPage("c0000", 0, 50); return }) |
| + if len(accounts) != 1 || accounts[0].ID != hub || accounts[0].Apps != 8000 || accounts[0].Members != 2000 { |
| + t.Fatalf("AccountsPage(c0000) = %+v", accounts) |
| + } |
| + timed("AccountsPage all", func() (err error) { accounts, _, err = s.AccountsPage("", 1950, 50); return }) |
| + if len(accounts) != 50 || accounts[49].ID != hub || accounts[49].Apps != 8000 || accounts[49].Members != 2000 || |
| + accounts[0].Members != 1 || accounts[0].Apps != 0 { |
| + t.Fatalf("AccountsPage last page = first %+v, last %+v", accounts[0], accounts[len(accounts)-1]) |
| + } |
| +} |
| + |
| +// TestMigrateOperatorPagesOnAPopulatedStore runs the migration on a store |
| +// already holding the scale fixture: the column and the eight indexes are |
| +// taken away, the store is closed, and the next Open puts them back. Both |
| +// opens are timed and printed (the second is the no-op baseline). |
| +func TestMigrateOperatorPagesOnAPopulatedStore(t *testing.T) { |
| + path := filepath.Join(t.TempDir(), "console.db") |
| + s, err := Open(path) |
| + opMust(t, err) |
| + opSeedScale(t, s, opNow) |
| + for _, idx := range []string{"audit_by_time", "audit_by_account", "audit_by_action", "sessions_by_time", |
| + "users_by_created", "users_by_last_seen", "memberships_by_address", "apps_by_creator"} { |
| + _, err := s.db.Exec(`DROP INDEX ` + idx) |
| + opMust(t, err) |
| + } |
| + _, err = s.db.Exec(`ALTER TABLE users DROP COLUMN last_seen`) |
| + opMust(t, err) |
| + opMust(t, s.Close()) |
| + |
| + start := time.Now() |
| + s, err = Open(path) |
| + migrated := time.Since(start) |
| + opMust(t, err) |
| + opMust(t, s.Close()) |
| + start = time.Now() |
| + s, err = Open(path) |
| + again := time.Since(start) |
| + opMust(t, err) |
| + defer s.Close() |
| + t.Logf("open with the migration: %v; open again (nothing to do): %v", migrated, again) |
| + if perfEnforce() && migrated > 10*time.Second { |
| + t.Errorf("the migration took %v on the scale fixture", migrated) |
| + } |
| + for table, want := range map[string]int{"users": 20000, "sessions": 60000, "audit": 200000, "apps": 8000} { |
| + var n int |
| + opMust(t, s.db.QueryRow(`SELECT COUNT(*) FROM `+table).Scan(&n)) |
| + if n != want { |
| + t.Errorf("%s has %d rows after the migration, want %d", table, n, want) |
| + } |
| + } |
| + plan, problems, err := opPlanProblems(s.db, opQueryPlans[2]) |
| + if err != nil || len(problems) > 0 { |
| + t.Fatalf("active count after the migration: %v %v\n%s", problems, err, strings.Join(plan, "\n")) |
| + } |
| +} |