Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle gappy state blocks in a performant way #329

Closed
wants to merge 62 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
d4eace7
Tweak existing E2E test
Sep 21, 2023
1bd4f12
Write integration test too
Sep 21, 2023
44ef0c3
move keys() helper to internal
Oct 4, 2023
c1b41f4
New Storage functions
Oct 4, 2023
638554c
Handle state in Initialise after first snapshot
Sep 21, 2023
2f6a5cf
Emit cache invalidation for a replaced snapshot
Sep 22, 2023
d08f82a
Reminder for me in the future
Oct 3, 2023
32479e2
Move NewEventData to free function
Oct 4, 2023
db7e94d
Make UserCaches respond to gappy memberships
Sep 27, 2023
c2f99a6
Fix stupid syntax
Oct 10, 2023
19859fe
Fixes to the test
Oct 10, 2023
4cc3c31
Add comments for our future selves
Oct 10, 2023
6e03c2d
Select less data
Oct 10, 2023
899000c
Fix stupid bug in internal.Keys
Oct 10, 2023
08d1034
Merge remote-tracking branch 'origin/main' into dmr/resnapshot-2
Oct 11, 2023
a1ded0c
Update tests. prevBatches are broken
Oct 12, 2023
abeb273
Add nil guards to loops
Oct 12, 2023
ac17cb4
Merge branch 'main' into dmr/resnapshot-2
kegsay Oct 17, 2023
c3dfd66
Change the test to have a timeline with 10 msgs
Oct 13, 2023
ae9777d
ConnState: only call load once
Oct 13, 2023
4b3a617
WIP big-ass table test
Oct 17, 2023
5a661f7
Merge remote-tracking branch 'origin/main' into dmr/resnapshot-2
Oct 17, 2023
d82be3a
Fixup TestConnStateInitial
Oct 17, 2023
10b38ab
Fix a segfault when reloading the JRT
Oct 17, 2023
0e3b294
Queue up the post-gap membership
Oct 17, 2023
cada224
WIP
Oct 18, 2023
e2761be
Matchers
Oct 18, 2023
66dc048
Fix missing locks in v3 test
Oct 18, 2023
7205340
WIP snapshot
Oct 18, 2023
1da56ae
MatchRoomSubscription: report all errors
Oct 18, 2023
193e45d
Improve comments
Oct 18, 2023
0e21262
More matcher improvements
Oct 18, 2023
ea8506f
Improve tests again
Oct 18, 2023
51fc05c
Remove redundant bit
Oct 18, 2023
ec95409
Add in the required state check again
Oct 18, 2023
30f2d2d
Add in the sentinel check
Oct 18, 2023
c2045bf
Minimised test case
Oct 19, 2023
8c88f76
Merge remote-tracking branch 'origin/main' into dmr/resnapshot-2
Oct 19, 2023
b12c672
Remove unused chris and chrisToken
Oct 19, 2023
540006e
Additional comment
Oct 19, 2023
a54de00
Don't change Alice's room subscription.
Oct 25, 2023
4230323
Merge branch 'main' into dmr/resnapshot-2
Oct 31, 2023
c872511
Implement latest plan
Oct 31, 2023
4abc811
Track no. conns nuked due to room invalidation
Oct 31, 2023
def9e28
TODO note
Oct 31, 2023
3b3f092
Re-track gappy state block sizes
Oct 31, 2023
1f31550
Test rework part 1 of N
Nov 1, 2023
530ef78
2 of N
Nov 1, 2023
94f6518
3 of N
Nov 1, 2023
110bb81
4 of N
Nov 1, 2023
faf8caa
Extra assertion of N
Nov 1, 2023
3f2644e
6 of N
Nov 1, 2023
4d40f41
Minor formatting
Nov 1, 2023
5b0c8e3
Update existing integration tests
Nov 1, 2023
ebe2641
Merge remote-tracking branch 'origin/main' into dmr/resnapshot-2
Nov 2, 2023
9a40fb7
Fix test typo
Nov 2, 2023
8ba3661
Also test that bob sees prev batch
Nov 2, 2023
b72d682
Simplify FetchMembership
Nov 2, 2023
518f1a6
Merge branch 'main' into dmr/resnapshot-2
Nov 2, 2023
eaeff79
Merge remote-tracking branch 'origin/main' into dmr/resnapshot-2
Nov 2, 2023
fe7c5a2
Fix bad merge
Nov 3, 2023
b8e34a7
Remove uninsteresting gofmt change
Nov 3, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion state/invites_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package state
import (
"database/sql"
"encoding/json"

"github.com/jmoiron/sqlx"
)

Expand Down Expand Up @@ -74,6 +73,31 @@ func (t *InvitesTable) SelectInviteState(userID, roomID string) (inviteState []j
return inviteState, nil
}

func (t *InvitesTable) SelectInviteStateInRoom(roomID string) (map[string][]json.RawMessage, error) {
DMRobertson marked this conversation as resolved.
Show resolved Hide resolved
var rows []struct {
State json.RawMessage `db:"invite_state"`
UserID string `db:"user_id"`
}
err := t.db.Select(&rows, `
SELECT invite_state, user_id
FROM syncv3_invites
WHERE room_id=$1
`, roomID)
if err != nil {
return nil, err
}

result := make(map[string][]json.RawMessage, len(rows))
for _, row := range rows {
var inviteState []json.RawMessage
if err := json.Unmarshal(row.State, &inviteState); err != nil {
return nil, err
}
result[row.UserID] = inviteState
}
return result, nil
}

// Select all invites for this user. Returns a map of room ID to invite_state (json array).
func (t *InvitesTable) SelectAllInvitesForUser(userID string) (map[string][]json.RawMessage, error) {
rows, err := t.db.Query(`SELECT room_id, invite_state FROM syncv3_invites WHERE user_id = $1`, userID)
Expand Down
52 changes: 52 additions & 0 deletions state/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,58 @@ func (s *Storage) ResetMetadataState(metadata *internal.RoomMetadata) error {
return nil
}

// TODO: there is a very similar query in ResetMetadataState which also selects events
// events row for memberships. It is a shame to have to do this twice---can we query
// once and pass the data around?
func (s *Storage) FetchMemberships(roomID string) (
joins map[string]Event,
invites map[string][]json.RawMessage,
leaves map[string]json.RawMessage,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May not need the data here---just the user IDs.

err error,
) {
var events []Event
err = s.DB.Select(&events, `
WITH snapshot(membership_nids) AS (
SELECT membership_events
FROM syncv3_snapshots
JOIN syncv3_rooms ON snapshot_id = current_snapshot_id
WHERE syncv3_rooms.room_id = $1
)
SELECT event_nid, event_type, state_key, membership, is_state, before_state_snapshot_id, event_replaces_nid, event_id, room_id, prev_batch, missing_previous
FROM syncv3_events JOIN snapshot ON (
event_nid = ANY( membership_nids )
)
WHERE membership NOT IN ('invite', '_invite')
`, roomID)
if err != nil {
return nil, nil, nil, err
}

joins = make(map[string]Event, len(events))
leaves = make(map[string]json.RawMessage, len(events))

for _, e := range events {
switch e.Membership {
case "_join":
fallthrough
case "join":
joins[e.StateKey] = e
case "_invite":
fallthrough
case "invite":
// should not happen!
default:
leaves[e.StateKey] = e.JSON
}
}

invites, err = s.InvitesTable.SelectInviteStateInRoom(roomID)
if err != nil {
return nil, nil, nil, err
}
return
}

// Returns all current NOT MEMBERSHIP state events matching the event types given in all rooms. Returns a map of
// room ID to events in that room.
func (s *Storage) currentNotMembershipStateEventsInAllRooms(txn *sqlx.Tx, eventTypes []string) (map[string][]Event, error) {
Expand Down