mirror of
https://github.com/matrix-org/dendrite
synced 2024-12-15 15:52:48 +00: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>
97 lines
3.3 KiB
Go
97 lines
3.3 KiB
Go
package tables_test
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
"github.com/matrix-org/dendrite/roomserver/storage/postgres"
|
|
"github.com/matrix-org/dendrite/roomserver/storage/sqlite3"
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
|
"github.com/matrix-org/dendrite/setup/config"
|
|
"github.com/matrix-org/dendrite/test"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func mustCreateStateSnapshotTable(t *testing.T, dbType test.DBType) (tab tables.StateSnapshot, close func()) {
|
|
t.Helper()
|
|
connStr, close := test.PrepareDBConnectionString(t, dbType)
|
|
db, err := sqlutil.Open(&config.DatabaseOptions{
|
|
ConnectionString: config.DataSource(connStr),
|
|
}, sqlutil.NewExclusiveWriter())
|
|
assert.NoError(t, err)
|
|
switch dbType {
|
|
case test.DBTypePostgres:
|
|
// for the PostgreSQL history visibility optimisation to work,
|
|
// we also need some other tables to exist
|
|
err = postgres.CreateEventStateKeysTable(db)
|
|
assert.NoError(t, err)
|
|
err = postgres.CreateEventsTable(db)
|
|
assert.NoError(t, err)
|
|
err = postgres.CreateEventJSONTable(db)
|
|
assert.NoError(t, err)
|
|
err = postgres.CreateStateBlockTable(db)
|
|
assert.NoError(t, err)
|
|
// ... and then the snapshot table itself
|
|
err = postgres.CreateStateSnapshotTable(db)
|
|
assert.NoError(t, err)
|
|
tab, err = postgres.PrepareStateSnapshotTable(db)
|
|
case test.DBTypeSQLite:
|
|
err = sqlite3.CreateStateSnapshotTable(db)
|
|
assert.NoError(t, err)
|
|
tab, err = sqlite3.PrepareStateSnapshotTable(db)
|
|
}
|
|
assert.NoError(t, err)
|
|
|
|
return tab, close
|
|
}
|
|
|
|
func TestStateSnapshotTable(t *testing.T) {
|
|
ctx := context.Background()
|
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
|
tab, close := mustCreateStateSnapshotTable(t, dbType)
|
|
defer close()
|
|
|
|
// generate some dummy data
|
|
var stateBlockNIDs types.StateBlockNIDs
|
|
for i := 0; i < 100; i++ {
|
|
stateBlockNIDs = append(stateBlockNIDs, types.StateBlockNID(i))
|
|
}
|
|
stateNID, err := tab.InsertState(ctx, nil, 1, stateBlockNIDs)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, types.StateSnapshotNID(1), stateNID)
|
|
|
|
// verify ON CONFLICT; Note: this updates the sequence!
|
|
stateNID, err = tab.InsertState(ctx, nil, 1, stateBlockNIDs)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, types.StateSnapshotNID(1), stateNID)
|
|
|
|
// create a second snapshot
|
|
var stateBlockNIDs2 types.StateBlockNIDs
|
|
for i := 100; i < 150; i++ {
|
|
stateBlockNIDs2 = append(stateBlockNIDs2, types.StateBlockNID(i))
|
|
}
|
|
|
|
stateNID, err = tab.InsertState(ctx, nil, 1, stateBlockNIDs2)
|
|
assert.NoError(t, err)
|
|
// StateSnapshotNID is now 3, since the DO UPDATE SET statement incremented the sequence
|
|
assert.Equal(t, types.StateSnapshotNID(3), stateNID)
|
|
|
|
nidLists, err := tab.BulkSelectStateBlockNIDs(ctx, nil, []types.StateSnapshotNID{1, 3})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, stateBlockNIDs, types.StateBlockNIDs(nidLists[0].StateBlockNIDs))
|
|
assert.Equal(t, stateBlockNIDs2, types.StateBlockNIDs(nidLists[1].StateBlockNIDs))
|
|
|
|
// check we get an error if the state snapshot does not exist
|
|
_, err = tab.BulkSelectStateBlockNIDs(ctx, nil, []types.StateSnapshotNID{2})
|
|
assert.Error(t, err)
|
|
|
|
// create a second snapshot
|
|
for i := 0; i < 65555; i++ {
|
|
stateBlockNIDs2 = append(stateBlockNIDs2, types.StateBlockNID(i))
|
|
}
|
|
_, err = tab.InsertState(ctx, nil, 1, stateBlockNIDs2)
|
|
assert.NoError(t, err)
|
|
})
|
|
}
|