mirror of
https://github.com/matrix-org/dendrite.git
synced 2024-11-23 14:51:56 -06:00
eb29a31550
Should fix the following issues or make a lot less worse when using Postgres: The main issue behind #2911: The client gives up after a certain time, causing a cascade of context errors, because the response couldn't be built up fast enough. This mostly happens on accounts with many rooms, due to the inefficient way we're getting recent events and current state For #2777: The queries for getting the membership events for history visibility were being executed for each room (I think 185?), resulting in a whooping 2k queries for membership events. (Getting the statesnapshot -> block nids -> actual wanted membership event) Both should now be better by: - Using a LATERAL join to get all recent events for all joined rooms in one go (TODO: maybe do the same for room summary and current state etc) - If we're lazy loading on initial syncs, we're now not getting the whole current state, just to drop the majority of it because we're lazy loading members - we add a filter to exclude membership events on the first call to `CurrentState`. - Using an optimized query to get the membership events needed to calculate history visibility --------- Co-authored-by: kegsay <kegan@matrix.org>
129 lines
4.1 KiB
Go
129 lines
4.1 KiB
Go
package tables_test
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
"github.com/matrix-org/dendrite/setup/config"
|
|
"github.com/matrix-org/dendrite/syncapi/storage/postgres"
|
|
"github.com/matrix-org/dendrite/syncapi/storage/sqlite3"
|
|
"github.com/matrix-org/dendrite/syncapi/storage/tables"
|
|
"github.com/matrix-org/dendrite/syncapi/types"
|
|
"github.com/matrix-org/dendrite/test"
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
)
|
|
|
|
func newCurrentRoomStateTable(t *testing.T, dbType test.DBType) (tables.CurrentRoomState, *sql.DB, func()) {
|
|
t.Helper()
|
|
connStr, close := test.PrepareDBConnectionString(t, dbType)
|
|
db, err := sqlutil.Open(&config.DatabaseOptions{
|
|
ConnectionString: config.DataSource(connStr),
|
|
}, sqlutil.NewExclusiveWriter())
|
|
if err != nil {
|
|
t.Fatalf("failed to open db: %s", err)
|
|
}
|
|
|
|
var tab tables.CurrentRoomState
|
|
switch dbType {
|
|
case test.DBTypePostgres:
|
|
tab, err = postgres.NewPostgresCurrentRoomStateTable(db)
|
|
case test.DBTypeSQLite:
|
|
var stream sqlite3.StreamIDStatements
|
|
if err = stream.Prepare(db); err != nil {
|
|
t.Fatalf("failed to prepare stream stmts: %s", err)
|
|
}
|
|
tab, err = sqlite3.NewSqliteCurrentRoomStateTable(db, &stream)
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("failed to make new table: %s", err)
|
|
}
|
|
return tab, db, close
|
|
}
|
|
|
|
func TestCurrentRoomStateTable(t *testing.T) {
|
|
ctx := context.Background()
|
|
alice := test.NewUser(t)
|
|
room := test.NewRoom(t, alice)
|
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
|
tab, db, close := newCurrentRoomStateTable(t, dbType)
|
|
defer close()
|
|
events := room.CurrentState()
|
|
err := sqlutil.WithTransaction(db, func(txn *sql.Tx) error {
|
|
for i, ev := range events {
|
|
err := tab.UpsertRoomState(ctx, txn, ev, nil, types.StreamPosition(i))
|
|
if err != nil {
|
|
return fmt.Errorf("failed to UpsertRoomState: %w", err)
|
|
}
|
|
}
|
|
wantEventIDs := []string{
|
|
events[0].EventID(), events[1].EventID(), events[2].EventID(), events[3].EventID(),
|
|
}
|
|
gotEvents, err := tab.SelectEventsWithEventIDs(ctx, txn, wantEventIDs)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to SelectEventsWithEventIDs: %w", err)
|
|
}
|
|
if len(gotEvents) != len(wantEventIDs) {
|
|
return fmt.Errorf("SelectEventsWithEventIDs\ngot %d, want %d results", len(gotEvents), len(wantEventIDs))
|
|
}
|
|
gotEventIDs := make(map[string]struct{}, len(gotEvents))
|
|
for _, event := range gotEvents {
|
|
if event.ExcludeFromSync {
|
|
return fmt.Errorf("SelectEventsWithEventIDs ExcludeFromSync should be false for current room state event %+v", event)
|
|
}
|
|
gotEventIDs[event.EventID()] = struct{}{}
|
|
}
|
|
for _, id := range wantEventIDs {
|
|
if _, ok := gotEventIDs[id]; !ok {
|
|
return fmt.Errorf("SelectEventsWithEventIDs\nexpected id %q not returned", id)
|
|
}
|
|
}
|
|
|
|
testCurrentState(t, ctx, txn, tab, room)
|
|
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("err: %v", err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func testCurrentState(t *testing.T, ctx context.Context, txn *sql.Tx, tab tables.CurrentRoomState, room *test.Room) {
|
|
t.Run("test currentState", func(t *testing.T) {
|
|
// returns the complete state of the room with a default filter
|
|
filter := gomatrixserverlib.DefaultStateFilter()
|
|
evs, err := tab.SelectCurrentState(ctx, txn, room.ID, &filter, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
expectCount := 5
|
|
if gotCount := len(evs); gotCount != expectCount {
|
|
t.Fatalf("expected %d state events, got %d", expectCount, gotCount)
|
|
}
|
|
// When lazy loading, we expect no membership event, so only 4 events
|
|
filter.LazyLoadMembers = true
|
|
expectCount = 4
|
|
evs, err = tab.SelectCurrentState(ctx, txn, room.ID, &filter, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if gotCount := len(evs); gotCount != expectCount {
|
|
t.Fatalf("expected %d state events, got %d", expectCount, gotCount)
|
|
}
|
|
// same as above, but with existing NotTypes defined
|
|
notTypes := []string{gomatrixserverlib.MRoomMember}
|
|
filter.NotTypes = ¬Types
|
|
evs, err = tab.SelectCurrentState(ctx, txn, room.ID, &filter, nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if gotCount := len(evs); gotCount != expectCount {
|
|
t.Fatalf("expected %d state events, got %d", expectCount, gotCount)
|
|
}
|
|
})
|
|
|
|
}
|