sqlite: fixes from sytest (#872)

* bugfix: fix panic on new invite events from sytest

I'm unsure why the previous code didn't work, but it's
clearer, quicker and easier to read the `LastInsertID()` way.
Previously, the code would panic as the SELECT would fail
to find the last inserted row ID.

* sqlite: Fix UNIQUE violations and close more cursors

- Add missing `defer rows.Close()`
- Do not have the state block NID as a PRIMARY KEY else it breaks for blocks
  with >1 state event in them. Instead, rejig the queries so we can still
  have monotonically increasing integers without using AUTOINCREMENT (which
  mandates PRIMARY KEY).

* sqlite: Add missing variadic function

* Use LastInsertId because empirically it works over the SELECT form (though I don't know why that is)

* sqlite: Fix invite table by using the global stream pos rather than one specific to invites

If we don't use the global, clients don't get notified about any invites
because the position is too low.

* linting: shadowing

* sqlite: do not use last rowid, we already know the stream pos!

* sqlite: Fix account data table in syncapi by commiting insert txns!

* sqlite: Fix failing federation invite

Was failing with 'database is locked' due to multiple write txns
being taken out.

* sqlite: Ensure we return exactly the number of events found in the database

Previously we would return exactly the number of *requested* events, which
meant that several zero-initialised events would bubble through the system,
failing at JSON serialisation time.

* sqlite: let's just ignore the problem for now....

* linting
This commit is contained in:
Kegsay 2020-02-20 09:28:03 +00:00 committed by GitHub
parent 3dabf4d4ed
commit 5caae6f3a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 145 additions and 137 deletions

View File

@ -18,6 +18,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"fmt"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
) )
@ -47,14 +48,10 @@ const selectFilterIDByContentSQL = "" +
const insertFilterSQL = "" + const insertFilterSQL = "" +
"INSERT INTO account_filter (filter, localpart) VALUES ($1, $2)" "INSERT INTO account_filter (filter, localpart) VALUES ($1, $2)"
const selectLastInsertedFilterIDSQL = "" +
"SELECT id FROM account_filter WHERE rowid = last_insert_rowid()"
type filterStatements struct { type filterStatements struct {
selectFilterStmt *sql.Stmt selectFilterStmt *sql.Stmt
selectLastInsertedFilterIDStmt *sql.Stmt selectFilterIDByContentStmt *sql.Stmt
selectFilterIDByContentStmt *sql.Stmt insertFilterStmt *sql.Stmt
insertFilterStmt *sql.Stmt
} }
func (s *filterStatements) prepare(db *sql.DB) (err error) { func (s *filterStatements) prepare(db *sql.DB) (err error) {
@ -65,9 +62,6 @@ func (s *filterStatements) prepare(db *sql.DB) (err error) {
if s.selectFilterStmt, err = db.Prepare(selectFilterSQL); err != nil { if s.selectFilterStmt, err = db.Prepare(selectFilterSQL); err != nil {
return return
} }
if s.selectLastInsertedFilterIDStmt, err = db.Prepare(selectLastInsertedFilterIDSQL); err != nil {
return
}
if s.selectFilterIDByContentStmt, err = db.Prepare(selectFilterIDByContentSQL); err != nil { if s.selectFilterIDByContentStmt, err = db.Prepare(selectFilterIDByContentSQL); err != nil {
return return
} }
@ -128,12 +122,14 @@ func (s *filterStatements) insertFilter(
} }
// Otherwise insert the filter and return the new ID // Otherwise insert the filter and return the new ID
if _, err = s.insertFilterStmt.ExecContext(ctx, filterJSON, localpart); err != nil { res, err := s.insertFilterStmt.ExecContext(ctx, filterJSON, localpart)
if err != nil {
return "", err return "", err
} }
row := s.selectLastInsertedFilterIDStmt.QueryRowContext(ctx) rowid, err := res.LastInsertId()
if err := row.Scan(&filterID); err != nil { if err != nil {
return "", err return "", err
} }
filterID = fmt.Sprintf("%d", rowid)
return return
} }

View File

@ -52,16 +52,18 @@ const selectInviteActiveForUserInRoomSQL = "" +
// However the matrix protocol doesn't give us a way to reliably identify the // However the matrix protocol doesn't give us a way to reliably identify the
// invites that were retired, so we are forced to retire all of them. // invites that were retired, so we are forced to retire all of them.
const updateInviteRetiredSQL = ` const updateInviteRetiredSQL = `
UPDATE roomserver_invites SET retired = TRUE UPDATE roomserver_invites SET retired = TRUE WHERE room_nid = $1 AND target_nid = $2 AND NOT retired
WHERE room_nid = $1 AND target_nid = $2 AND NOT retired; `
SELECT invite_event_id FROM roomserver_invites
WHERE rowid = last_insert_rowid(); const selectInvitesAboutToRetireSQL = `
SELECT invite_event_id FROM roomserver_invites WHERE room_nid = $1 AND target_nid = $2 AND NOT retired
` `
type inviteStatements struct { type inviteStatements struct {
insertInviteEventStmt *sql.Stmt insertInviteEventStmt *sql.Stmt
selectInviteActiveForUserInRoomStmt *sql.Stmt selectInviteActiveForUserInRoomStmt *sql.Stmt
updateInviteRetiredStmt *sql.Stmt updateInviteRetiredStmt *sql.Stmt
selectInvitesAboutToRetireStmt *sql.Stmt
} }
func (s *inviteStatements) prepare(db *sql.DB) (err error) { func (s *inviteStatements) prepare(db *sql.DB) (err error) {
@ -74,6 +76,7 @@ func (s *inviteStatements) prepare(db *sql.DB) (err error) {
{&s.insertInviteEventStmt, insertInviteEventSQL}, {&s.insertInviteEventStmt, insertInviteEventSQL},
{&s.selectInviteActiveForUserInRoomStmt, selectInviteActiveForUserInRoomSQL}, {&s.selectInviteActiveForUserInRoomStmt, selectInviteActiveForUserInRoomSQL},
{&s.updateInviteRetiredStmt, updateInviteRetiredSQL}, {&s.updateInviteRetiredStmt, updateInviteRetiredSQL},
{&s.selectInvitesAboutToRetireStmt, selectInvitesAboutToRetireSQL},
}.prepare(db) }.prepare(db)
} }
@ -102,7 +105,8 @@ func (s *inviteStatements) updateInviteRetired(
ctx context.Context, ctx context.Context,
txn *sql.Tx, roomNID types.RoomNID, targetUserNID types.EventStateKeyNID, txn *sql.Tx, roomNID types.RoomNID, targetUserNID types.EventStateKeyNID,
) (eventIDs []string, err error) { ) (eventIDs []string, err error) {
stmt := common.TxStmt(txn, s.updateInviteRetiredStmt) // gather all the event IDs we will retire
stmt := txn.Stmt(s.selectInvitesAboutToRetireStmt)
rows, err := stmt.QueryContext(ctx, roomNID, targetUserNID) rows, err := stmt.QueryContext(ctx, roomNID, targetUserNID)
if err != nil { if err != nil {
return nil, err return nil, err
@ -110,11 +114,15 @@ func (s *inviteStatements) updateInviteRetired(
defer (func() { err = rows.Close() })() defer (func() { err = rows.Close() })()
for rows.Next() { for rows.Next() {
var inviteEventID string var inviteEventID string
if err := rows.Scan(&inviteEventID); err != nil { if err = rows.Scan(&inviteEventID); err != nil {
return nil, err return nil, err
} }
eventIDs = append(eventIDs, inviteEventID) eventIDs = append(eventIDs, inviteEventID)
} }
// now retire the invites
stmt = txn.Stmt(s.updateInviteRetiredStmt)
_, err = stmt.ExecContext(ctx, roomNID, targetUserNID)
return return
} }

View File

@ -103,6 +103,8 @@ func (s *roomAliasesStatements) selectAliasesFromRoomID(
return return
} }
defer rows.Close() // nolint: errcheck
for rows.Next() { for rows.Next() {
var alias string var alias string
if err = rows.Scan(&alias); err != nil { if err = rows.Scan(&alias); err != nil {

View File

@ -30,7 +30,7 @@ import (
const stateDataSchema = ` const stateDataSchema = `
CREATE TABLE IF NOT EXISTS roomserver_state_block ( CREATE TABLE IF NOT EXISTS roomserver_state_block (
state_block_nid INTEGER PRIMARY KEY AUTOINCREMENT, state_block_nid INTEGER NOT NULL,
event_type_nid INTEGER NOT NULL, event_type_nid INTEGER NOT NULL,
event_state_key_nid INTEGER NOT NULL, event_state_key_nid INTEGER NOT NULL,
event_nid INTEGER NOT NULL, event_nid INTEGER NOT NULL,
@ -43,10 +43,7 @@ const insertStateDataSQL = "" +
" VALUES ($1, $2, $3, $4)" " VALUES ($1, $2, $3, $4)"
const selectNextStateBlockNIDSQL = ` const selectNextStateBlockNIDSQL = `
SELECT COALESCE(( SELECT IFNULL(MAX(state_block_nid), 0) + 1 FROM roomserver_state_block
SELECT seq+1 AS state_block_nid FROM sqlite_sequence
WHERE name = 'roomserver_state_block'), 1
) AS state_block_nid
` `
// Bulk state lookup by numeric state block ID. // Bulk state lookup by numeric state block ID.
@ -98,11 +95,19 @@ func (s *stateBlockStatements) prepare(db *sql.DB) (err error) {
func (s *stateBlockStatements) bulkInsertStateData( func (s *stateBlockStatements) bulkInsertStateData(
ctx context.Context, txn *sql.Tx, ctx context.Context, txn *sql.Tx,
stateBlockNID types.StateBlockNID,
entries []types.StateEntry, entries []types.StateEntry,
) error { ) (types.StateBlockNID, error) {
if len(entries) == 0 {
return 0, nil
}
var stateBlockNID types.StateBlockNID
err := txn.Stmt(s.selectNextStateBlockNIDStmt).QueryRowContext(ctx).Scan(&stateBlockNID)
if err != nil {
return 0, err
}
for _, entry := range entries { for _, entry := range entries {
_, err := common.TxStmt(txn, s.insertStateDataStmt).ExecContext( _, err := txn.Stmt(s.insertStateDataStmt).ExecContext(
ctx, ctx,
int64(stateBlockNID), int64(stateBlockNID),
int64(entry.EventTypeNID), int64(entry.EventTypeNID),
@ -110,20 +115,10 @@ func (s *stateBlockStatements) bulkInsertStateData(
int64(entry.EventNID), int64(entry.EventNID),
) )
if err != nil { if err != nil {
return err return 0, err
} }
} }
return nil return stateBlockNID, nil
}
func (s *stateBlockStatements) selectNextStateBlockNID(
ctx context.Context,
txn *sql.Tx,
) (types.StateBlockNID, error) {
var stateBlockNID int64
selectStmt := common.TxStmt(txn, s.selectNextStateBlockNIDStmt)
err := selectStmt.QueryRowContext(ctx).Scan(&stateBlockNID)
return types.StateBlockNID(stateBlockNID), err
} }
func (s *stateBlockStatements) bulkSelectStateBlockEntries( func (s *stateBlockStatements) bulkSelectStateBlockEntries(

View File

@ -54,7 +54,12 @@ func Open(dataSourceName string) (*Database, error) {
} }
//d.db.Exec("PRAGMA journal_mode=WAL;") //d.db.Exec("PRAGMA journal_mode=WAL;")
//d.db.Exec("PRAGMA read_uncommitted = true;") //d.db.Exec("PRAGMA read_uncommitted = true;")
d.db.SetMaxOpenConns(2)
// FIXME: We are leaking connections somewhere. Setting this to 2 will eventually
// cause the roomserver to be unresponsive to new events because something will
// acquire the global mutex and never unlock it because it is waiting for a connection
// which it will never obtain.
d.db.SetMaxOpenConns(20)
if err = d.statements.prepare(d.db); err != nil { if err = d.statements.prepare(d.db); err != nil {
return nil, err return nil, err
} }
@ -253,12 +258,13 @@ func (d *Database) Events(
) ([]types.Event, error) { ) ([]types.Event, error) {
var eventJSONs []eventJSONPair var eventJSONs []eventJSONPair
var err error var err error
results := make([]types.Event, len(eventNIDs)) var results []types.Event
err = common.WithTransaction(d.db, func(txn *sql.Tx) error { err = common.WithTransaction(d.db, func(txn *sql.Tx) error {
eventJSONs, err = d.statements.bulkSelectEventJSON(ctx, txn, eventNIDs) eventJSONs, err = d.statements.bulkSelectEventJSON(ctx, txn, eventNIDs)
if err != nil || len(eventJSONs) == 0 { if err != nil || len(eventJSONs) == 0 {
return nil return nil
} }
results = make([]types.Event, len(eventJSONs))
for i, eventJSON := range eventJSONs { for i, eventJSON := range eventJSONs {
result := &results[i] result := &results[i]
result.EventNID = eventJSON.EventNID result.EventNID = eventJSON.EventNID
@ -286,13 +292,10 @@ func (d *Database) AddState(
err = common.WithTransaction(d.db, func(txn *sql.Tx) error { err = common.WithTransaction(d.db, func(txn *sql.Tx) error {
if len(state) > 0 { if len(state) > 0 {
var stateBlockNID types.StateBlockNID var stateBlockNID types.StateBlockNID
stateBlockNID, err = d.statements.selectNextStateBlockNID(ctx, txn) stateBlockNID, err = d.statements.bulkInsertStateData(ctx, txn, state)
if err != nil { if err != nil {
return err return err
} }
if err = d.statements.bulkInsertStateData(ctx, txn, stateBlockNID, state); err != nil {
return err
}
stateBlockNIDs = append(stateBlockNIDs[:len(stateBlockNIDs):len(stateBlockNIDs)], stateBlockNID) stateBlockNIDs = append(stateBlockNIDs[:len(stateBlockNIDs):len(stateBlockNIDs)], stateBlockNID)
} }
stateNID, err = d.statements.insertState(ctx, txn, roomNID, stateBlockNIDs) stateNID, err = d.statements.insertState(ctx, txn, roomNID, stateBlockNIDs)
@ -602,8 +605,9 @@ func (d *Database) StateEntriesForTuples(
// MembershipUpdater implements input.RoomEventDatabase // MembershipUpdater implements input.RoomEventDatabase
func (d *Database) MembershipUpdater( func (d *Database) MembershipUpdater(
ctx context.Context, roomID, targetUserID string, ctx context.Context, roomID, targetUserID string,
) (types.MembershipUpdater, error) { ) (updater types.MembershipUpdater, err error) {
txn, err := d.db.Begin() var txn *sql.Tx
txn, err = d.db.Begin()
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -611,6 +615,18 @@ func (d *Database) MembershipUpdater(
defer func() { defer func() {
if !succeeded { if !succeeded {
txn.Rollback() // nolint: errcheck txn.Rollback() // nolint: errcheck
} else {
// TODO: We should be holding open this transaction but we cannot have
// multiple write transactions on sqlite. The code will perform additional
// write transactions independent of this one which will consistently cause
// 'database is locked' errors. For now, we'll break up the transaction and
// hope we don't race too catastrophically. Long term, we should be able to
// thread in txn objects where appropriate (either at the interface level or
// bring matrix business logic into the storage layer).
txerr := txn.Commit()
if err == nil && txerr != nil {
err = txerr
}
} }
}() }()
@ -624,7 +640,7 @@ func (d *Database) MembershipUpdater(
return nil, err return nil, err
} }
updater, err := d.membershipUpdaterTxn(ctx, txn, roomNID, targetUserNID) updater, err = d.membershipUpdaterTxn(ctx, txn, roomNID, targetUserNID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -658,7 +674,8 @@ func (d *Database) membershipUpdaterTxn(
} }
return &membershipUpdater{ return &membershipUpdater{
transaction{ctx, txn}, d, roomNID, targetUserNID, membership, // purposefully set the txn to nil so if we try to use it we panic and fail fast
transaction{ctx, nil}, d, roomNID, targetUserNID, membership,
}, nil }, nil
} }

View File

@ -158,6 +158,7 @@ func (s *OutputRoomEventConsumer) onNewInviteEvent(
// panic rather than continue with an inconsistent database // panic rather than continue with an inconsistent database
log.WithFields(log.Fields{ log.WithFields(log.Fields{
"event": string(msg.Event.JSON()), "event": string(msg.Event.JSON()),
"pdupos": pduPos,
log.ErrorKey: err, log.ErrorKey: err,
}).Panicf("roomserver output log: write invite failure") }).Panicf("roomserver output log: write invite failure")
return nil return nil

View File

@ -19,15 +19,13 @@ import (
"context" "context"
"database/sql" "database/sql"
"github.com/lib/pq"
"github.com/matrix-org/dendrite/common"
"github.com/matrix-org/dendrite/syncapi/types" "github.com/matrix-org/dendrite/syncapi/types"
"github.com/matrix-org/gomatrixserverlib" "github.com/matrix-org/gomatrixserverlib"
) )
const accountDataSchema = ` const accountDataSchema = `
CREATE TABLE IF NOT EXISTS syncapi_account_data_type ( CREATE TABLE IF NOT EXISTS syncapi_account_data_type (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY,
user_id TEXT NOT NULL, user_id TEXT NOT NULL,
room_id TEXT NOT NULL, room_id TEXT NOT NULL,
type TEXT NOT NULL, type TEXT NOT NULL,
@ -43,9 +41,7 @@ const insertAccountDataSQL = "" +
const selectAccountDataInRangeSQL = "" + const selectAccountDataInRangeSQL = "" +
"SELECT room_id, type FROM syncapi_account_data_type" + "SELECT room_id, type FROM syncapi_account_data_type" +
" WHERE user_id = $1 AND id > $2 AND id <= $3" + " WHERE user_id = $1 AND id > $2 AND id <= $3" +
" AND ( $4 IS NULL OR type IN ($4) )" + " ORDER BY id ASC"
" AND ( $5 IS NULL OR NOT(type IN ($5)) )" +
" ORDER BY id ASC LIMIT $6"
const selectMaxAccountDataIDSQL = "" + const selectMaxAccountDataIDSQL = "" +
"SELECT MAX(id) FROM syncapi_account_data_type" "SELECT MAX(id) FROM syncapi_account_data_type"
@ -53,8 +49,8 @@ const selectMaxAccountDataIDSQL = "" +
type accountDataStatements struct { type accountDataStatements struct {
streamIDStatements *streamIDStatements streamIDStatements *streamIDStatements
insertAccountDataStmt *sql.Stmt insertAccountDataStmt *sql.Stmt
selectAccountDataInRangeStmt *sql.Stmt
selectMaxAccountDataIDStmt *sql.Stmt selectMaxAccountDataIDStmt *sql.Stmt
selectAccountDataInRangeStmt *sql.Stmt
} }
func (s *accountDataStatements) prepare(db *sql.DB, streamID *streamIDStatements) (err error) { func (s *accountDataStatements) prepare(db *sql.DB, streamID *streamIDStatements) (err error) {
@ -66,10 +62,10 @@ func (s *accountDataStatements) prepare(db *sql.DB, streamID *streamIDStatements
if s.insertAccountDataStmt, err = db.Prepare(insertAccountDataSQL); err != nil { if s.insertAccountDataStmt, err = db.Prepare(insertAccountDataSQL); err != nil {
return return
} }
if s.selectAccountDataInRangeStmt, err = db.Prepare(selectAccountDataInRangeSQL); err != nil { if s.selectMaxAccountDataIDStmt, err = db.Prepare(selectMaxAccountDataIDSQL); err != nil {
return return
} }
if s.selectMaxAccountDataIDStmt, err = db.Prepare(selectMaxAccountDataIDSQL); err != nil { if s.selectAccountDataInRangeStmt, err = db.Prepare(selectAccountDataInRangeSQL); err != nil {
return return
} }
return return
@ -83,8 +79,7 @@ func (s *accountDataStatements) insertAccountData(
if err != nil { if err != nil {
return return
} }
insertStmt := common.TxStmt(txn, s.insertAccountDataStmt) _, err = txn.Stmt(s.insertAccountDataStmt).ExecContext(ctx, pos, userID, roomID, dataType)
_, err = insertStmt.ExecContext(ctx, pos, userID, roomID, dataType)
return return
} }
@ -103,14 +98,13 @@ func (s *accountDataStatements) selectAccountDataInRange(
oldPos-- oldPos--
} }
rows, err := s.selectAccountDataInRangeStmt.QueryContext(ctx, userID, oldPos, newPos, rows, err := s.selectAccountDataInRangeStmt.QueryContext(ctx, userID, oldPos, newPos)
pq.StringArray(filterConvertTypeWildcardToSQL(accountDataFilterPart.Types)),
pq.StringArray(filterConvertTypeWildcardToSQL(accountDataFilterPart.NotTypes)),
accountDataFilterPart.Limit,
)
if err != nil { if err != nil {
return return
} }
defer rows.Close() // nolint: errcheck
var entries int
for rows.Next() { for rows.Next() {
var dataType string var dataType string
@ -120,22 +114,41 @@ func (s *accountDataStatements) selectAccountDataInRange(
return return
} }
// check if we should add this by looking at the filter.
// It would be nice if we could do this in SQL-land, but the mix of variadic
// and positional parameters makes the query annoyingly hard to do, it's easier
// and clearer to do it in Go-land. If there are no filters for [not]types then
// this gets skipped.
for _, includeType := range accountDataFilterPart.Types {
if includeType != dataType { // TODO: wildcard support
continue
}
}
for _, excludeType := range accountDataFilterPart.NotTypes {
if excludeType == dataType { // TODO: wildcard support
continue
}
}
if len(data[roomID]) > 0 { if len(data[roomID]) > 0 {
data[roomID] = append(data[roomID], dataType) data[roomID] = append(data[roomID], dataType)
} else { } else {
data[roomID] = []string{dataType} data[roomID] = []string{dataType}
} }
entries++
if entries >= accountDataFilterPart.Limit {
break
}
} }
return return data, nil
} }
func (s *accountDataStatements) selectMaxAccountDataID( func (s *accountDataStatements) selectMaxAccountDataID(
ctx context.Context, txn *sql.Tx, ctx context.Context, txn *sql.Tx,
) (id int64, err error) { ) (id int64, err error) {
var nullableID sql.NullInt64 var nullableID sql.NullInt64
stmt := common.TxStmt(txn, s.selectMaxAccountDataIDStmt) err = txn.Stmt(s.selectMaxAccountDataIDStmt).QueryRowContext(ctx).Scan(&nullableID)
err = stmt.QueryRowContext(ctx).Scan(&nullableID)
if nullableID.Valid { if nullableID.Valid {
id = nullableID.Int64 id = nullableID.Int64
} }

View File

@ -19,6 +19,7 @@ import (
"context" "context"
"database/sql" "database/sql"
"encoding/json" "encoding/json"
"strings"
"github.com/lib/pq" "github.com/lib/pq"
"github.com/matrix-org/dendrite/common" "github.com/matrix-org/dendrite/common"
@ -88,7 +89,6 @@ type currentRoomStateStatements struct {
selectRoomIDsWithMembershipStmt *sql.Stmt selectRoomIDsWithMembershipStmt *sql.Stmt
selectCurrentStateStmt *sql.Stmt selectCurrentStateStmt *sql.Stmt
selectJoinedUsersStmt *sql.Stmt selectJoinedUsersStmt *sql.Stmt
selectEventsWithEventIDsStmt *sql.Stmt
selectStateEventStmt *sql.Stmt selectStateEventStmt *sql.Stmt
} }
@ -113,9 +113,6 @@ func (s *currentRoomStateStatements) prepare(db *sql.DB, streamID *streamIDState
if s.selectJoinedUsersStmt, err = db.Prepare(selectJoinedUsersSQL); err != nil { if s.selectJoinedUsersStmt, err = db.Prepare(selectJoinedUsersSQL); err != nil {
return return
} }
if s.selectEventsWithEventIDsStmt, err = db.Prepare(selectEventsWithEventIDsSQL); err != nil {
return
}
if s.selectStateEventStmt, err = db.Prepare(selectStateEventSQL); err != nil { if s.selectStateEventStmt, err = db.Prepare(selectStateEventSQL); err != nil {
return return
} }
@ -233,8 +230,12 @@ func (s *currentRoomStateStatements) upsertRoomState(
func (s *currentRoomStateStatements) selectEventsWithEventIDs( func (s *currentRoomStateStatements) selectEventsWithEventIDs(
ctx context.Context, txn *sql.Tx, eventIDs []string, ctx context.Context, txn *sql.Tx, eventIDs []string,
) ([]types.StreamEvent, error) { ) ([]types.StreamEvent, error) {
stmt := common.TxStmt(txn, s.selectEventsWithEventIDsStmt) iEventIDs := make([]interface{}, len(eventIDs))
rows, err := stmt.QueryContext(ctx, pq.StringArray(eventIDs)) for k, v := range eventIDs {
iEventIDs[k] = v
}
query := strings.Replace(selectEventsWithEventIDsSQL, "($1)", common.QueryVariadic(len(iEventIDs)), 1)
rows, err := txn.QueryContext(ctx, query, iEventIDs...)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -26,7 +26,7 @@ import (
const inviteEventsSchema = ` const inviteEventsSchema = `
CREATE TABLE IF NOT EXISTS syncapi_invite_events ( CREATE TABLE IF NOT EXISTS syncapi_invite_events (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY,
event_id TEXT NOT NULL, event_id TEXT NOT NULL,
room_id TEXT NOT NULL, room_id TEXT NOT NULL,
target_user_id TEXT NOT NULL, target_user_id TEXT NOT NULL,
@ -39,11 +39,8 @@ CREATE INDEX IF NOT EXISTS syncapi_invites_event_id_idx ON syncapi_invite_events
const insertInviteEventSQL = "" + const insertInviteEventSQL = "" +
"INSERT INTO syncapi_invite_events" + "INSERT INTO syncapi_invite_events" +
" (room_id, event_id, target_user_id, event_json)" + " (id, room_id, event_id, target_user_id, event_json)" +
" VALUES ($1, $2, $3, $4)" " VALUES ($1, $2, $3, $4, $5)"
const selectLastInsertedInviteEventSQL = "" +
"SELECT id FROM syncapi_invite_events WHERE rowid = last_insert_rowid()"
const deleteInviteEventSQL = "" + const deleteInviteEventSQL = "" +
"DELETE FROM syncapi_invite_events WHERE event_id = $1" "DELETE FROM syncapi_invite_events WHERE event_id = $1"
@ -57,12 +54,11 @@ const selectMaxInviteIDSQL = "" +
"SELECT MAX(id) FROM syncapi_invite_events" "SELECT MAX(id) FROM syncapi_invite_events"
type inviteEventsStatements struct { type inviteEventsStatements struct {
streamIDStatements *streamIDStatements streamIDStatements *streamIDStatements
insertInviteEventStmt *sql.Stmt insertInviteEventStmt *sql.Stmt
selectLastInsertedInviteEventStmt *sql.Stmt selectInviteEventsInRangeStmt *sql.Stmt
selectInviteEventsInRangeStmt *sql.Stmt deleteInviteEventStmt *sql.Stmt
deleteInviteEventStmt *sql.Stmt selectMaxInviteIDStmt *sql.Stmt
selectMaxInviteIDStmt *sql.Stmt
} }
func (s *inviteEventsStatements) prepare(db *sql.DB, streamID *streamIDStatements) (err error) { func (s *inviteEventsStatements) prepare(db *sql.DB, streamID *streamIDStatements) (err error) {
@ -74,9 +70,6 @@ func (s *inviteEventsStatements) prepare(db *sql.DB, streamID *streamIDStatement
if s.insertInviteEventStmt, err = db.Prepare(insertInviteEventSQL); err != nil { if s.insertInviteEventStmt, err = db.Prepare(insertInviteEventSQL); err != nil {
return return
} }
if s.selectLastInsertedInviteEventStmt, err = db.Prepare(selectLastInsertedInviteEventSQL); err != nil {
return
}
if s.selectInviteEventsInRangeStmt, err = db.Prepare(selectInviteEventsInRangeSQL); err != nil { if s.selectInviteEventsInRangeStmt, err = db.Prepare(selectInviteEventsInRangeSQL); err != nil {
return return
} }
@ -90,19 +83,16 @@ func (s *inviteEventsStatements) prepare(db *sql.DB, streamID *streamIDStatement
} }
func (s *inviteEventsStatements) insertInviteEvent( func (s *inviteEventsStatements) insertInviteEvent(
ctx context.Context, inviteEvent gomatrixserverlib.Event, ctx context.Context, txn *sql.Tx, inviteEvent gomatrixserverlib.Event, streamPos types.StreamPosition,
) (streamPos types.StreamPosition, err error) { ) (err error) {
_, err = s.insertInviteEventStmt.ExecContext( _, err = txn.Stmt(s.insertInviteEventStmt).ExecContext(
ctx, ctx,
streamPos,
inviteEvent.RoomID(), inviteEvent.RoomID(),
inviteEvent.EventID(), inviteEvent.EventID(),
*inviteEvent.StateKey(), *inviteEvent.StateKey(),
inviteEvent.JSON(), inviteEvent.JSON(),
) )
if err != nil {
return
}
err = s.selectLastInsertedInviteEventStmt.QueryRowContext(ctx).Scan(&streamPos)
return return
} }

View File

@ -54,9 +54,6 @@ const insertEventSQL = "" +
") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) " + ") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) " +
"ON CONFLICT (event_id) DO UPDATE SET exclude_from_sync = $11" "ON CONFLICT (event_id) DO UPDATE SET exclude_from_sync = $11"
const selectLastInsertedEventSQL = "" +
"SELECT id FROM syncapi_output_room_events WHERE rowid = last_insert_rowid()"
const selectEventsSQL = "" + const selectEventsSQL = "" +
"SELECT id, event_json, session_id, exclude_from_sync, transaction_id FROM syncapi_output_room_events WHERE event_id = $1" "SELECT id, event_json, session_id, exclude_from_sync, transaction_id FROM syncapi_output_room_events WHERE event_id = $1"
@ -105,7 +102,6 @@ const selectStateInRangeSQL = "" +
type outputRoomEventsStatements struct { type outputRoomEventsStatements struct {
streamIDStatements *streamIDStatements streamIDStatements *streamIDStatements
insertEventStmt *sql.Stmt insertEventStmt *sql.Stmt
selectLastInsertedEventStmt *sql.Stmt
selectEventsStmt *sql.Stmt selectEventsStmt *sql.Stmt
selectMaxEventIDStmt *sql.Stmt selectMaxEventIDStmt *sql.Stmt
selectRecentEventsStmt *sql.Stmt selectRecentEventsStmt *sql.Stmt
@ -123,9 +119,6 @@ func (s *outputRoomEventsStatements) prepare(db *sql.DB, streamID *streamIDState
if s.insertEventStmt, err = db.Prepare(insertEventSQL); err != nil { if s.insertEventStmt, err = db.Prepare(insertEventSQL); err != nil {
return return
} }
if s.selectLastInsertedEventStmt, err = db.Prepare(selectLastInsertedEventSQL); err != nil {
return
}
if s.selectEventsStmt, err = db.Prepare(selectEventsSQL); err != nil { if s.selectEventsStmt, err = db.Prepare(selectEventsSQL); err != nil {
return return
} }
@ -270,7 +263,6 @@ func (s *outputRoomEventsStatements) insertEvent(
} }
insertStmt := common.TxStmt(txn, s.insertEventStmt) insertStmt := common.TxStmt(txn, s.insertEventStmt)
selectStmt := common.TxStmt(txn, s.selectLastInsertedEventStmt)
_, err = insertStmt.ExecContext( _, err = insertStmt.ExecContext(
ctx, ctx,
streamPos, streamPos,
@ -286,10 +278,6 @@ func (s *outputRoomEventsStatements) insertEvent(
txnID, txnID,
excludeFromSync, excludeFromSync,
) )
if err != nil {
return
}
err = selectStmt.QueryRowContext(ctx).Scan(&streamPos)
return return
} }

View File

@ -193,24 +193,20 @@ func (d *SyncServerDatasource) WriteEvent(
ctx, txn, ev, addStateEventIDs, removeStateEventIDs, transactionID, excludeFromSync, ctx, txn, ev, addStateEventIDs, removeStateEventIDs, transactionID, excludeFromSync,
) )
if err != nil { if err != nil {
fmt.Println("d.events.insertEvent:", err)
return err return err
} }
pduPosition = pos pduPosition = pos
if err = d.topology.insertEventInTopology(ctx, txn, ev); err != nil { if err = d.topology.insertEventInTopology(ctx, txn, ev); err != nil {
fmt.Println("d.topology.insertEventInTopology:", err)
return err return err
} }
if err = d.handleBackwardExtremities(ctx, txn, ev); err != nil { if err = d.handleBackwardExtremities(ctx, txn, ev); err != nil {
fmt.Println("d.handleBackwardExtremities:", err)
return err return err
} }
if len(addStateEvents) == 0 && len(removeStateEventIDs) == 0 { if len(addStateEvents) == 0 && len(removeStateEventIDs) == 0 {
// Nothing to do, the event may have just been a message event. // Nothing to do, the event may have just been a message event.
fmt.Println("nothing to do")
return nil return nil
} }
@ -340,8 +336,12 @@ func (d *SyncServerDatasource) GetEventsInRange(
} }
// SyncPosition returns the latest positions for syncing. // SyncPosition returns the latest positions for syncing.
func (d *SyncServerDatasource) SyncPosition(ctx context.Context) (types.PaginationToken, error) { func (d *SyncServerDatasource) SyncPosition(ctx context.Context) (tok types.PaginationToken, err error) {
return d.syncPositionTx(ctx, nil) err = common.WithTransaction(d.db, func(txn *sql.Tx) error {
tok, err = d.syncPositionTx(ctx, txn)
return err
})
return
} }
// BackwardExtremitiesForRoom returns the event IDs of all of the backward // BackwardExtremitiesForRoom returns the event IDs of all of the backward
@ -380,8 +380,12 @@ func (d *SyncServerDatasource) EventPositionInTopology(
} }
// SyncStreamPosition returns the latest position in the sync stream. Returns 0 if there are no events yet. // SyncStreamPosition returns the latest position in the sync stream. Returns 0 if there are no events yet.
func (d *SyncServerDatasource) SyncStreamPosition(ctx context.Context) (types.StreamPosition, error) { func (d *SyncServerDatasource) SyncStreamPosition(ctx context.Context) (pos types.StreamPosition, err error) {
return d.syncStreamPositionTx(ctx, nil) err = common.WithTransaction(d.db, func(txn *sql.Tx) error {
pos, err = d.syncStreamPositionTx(ctx, txn)
return err
})
return
} }
func (d *SyncServerDatasource) syncStreamPositionTx( func (d *SyncServerDatasource) syncStreamPositionTx(
@ -625,18 +629,15 @@ func (d *SyncServerDatasource) getResponseWithPDUsForCompleteSync(
if err != nil { if err != nil {
return return
} }
fmt.Println("Joined rooms:", joinedRoomIDs)
stateFilterPart := gomatrixserverlib.DefaultStateFilter() // TODO: use filter provided in request stateFilterPart := gomatrixserverlib.DefaultStateFilter() // TODO: use filter provided in request
// Build up a /sync response. Add joined rooms. // Build up a /sync response. Add joined rooms.
for _, roomID := range joinedRoomIDs { for _, roomID := range joinedRoomIDs {
fmt.Println("WE'RE ON", roomID)
var stateEvents []gomatrixserverlib.Event var stateEvents []gomatrixserverlib.Event
stateEvents, err = d.roomstate.selectCurrentState(ctx, txn, roomID, &stateFilterPart) stateEvents, err = d.roomstate.selectCurrentState(ctx, txn, roomID, &stateFilterPart)
if err != nil { if err != nil {
fmt.Println("d.roomstate.selectCurrentState:", err)
return return
} }
//fmt.Println("State events:", stateEvents) //fmt.Println("State events:", stateEvents)
@ -648,7 +649,6 @@ func (d *SyncServerDatasource) getResponseWithPDUsForCompleteSync(
numRecentEventsPerRoom, true, true, numRecentEventsPerRoom, true, true,
) )
if err != nil { if err != nil {
fmt.Println("d.events.selectRecentEvents:", err)
return return
} }
//fmt.Println("Recent stream events:", recentStreamEvents) //fmt.Println("Recent stream events:", recentStreamEvents)
@ -658,10 +658,9 @@ func (d *SyncServerDatasource) getResponseWithPDUsForCompleteSync(
var backwardTopologyPos types.StreamPosition var backwardTopologyPos types.StreamPosition
backwardTopologyPos, err = d.topology.selectPositionInTopology(ctx, txn, recentStreamEvents[0].EventID()) backwardTopologyPos, err = d.topology.selectPositionInTopology(ctx, txn, recentStreamEvents[0].EventID())
if err != nil { if err != nil {
fmt.Println("d.topology.selectPositionInTopology:", err)
return nil, types.PaginationToken{}, []string{}, err return nil, types.PaginationToken{}, []string{}, err
} }
fmt.Println("Backward topology position:", backwardTopologyPos)
if backwardTopologyPos-1 <= 0 { if backwardTopologyPos-1 <= 0 {
backwardTopologyPos = types.StreamPosition(1) backwardTopologyPos = types.StreamPosition(1)
} else { } else {
@ -683,7 +682,6 @@ func (d *SyncServerDatasource) getResponseWithPDUsForCompleteSync(
} }
if err = d.addInvitesToResponse(ctx, txn, userID, 0, toPos.PDUPosition, res); err != nil { if err = d.addInvitesToResponse(ctx, txn, userID, 0, toPos.PDUPosition, res); err != nil {
fmt.Println("d.addInvitesToResponse:", err)
return return
} }
@ -744,18 +742,10 @@ func (d *SyncServerDatasource) GetAccountDataInRange(
func (d *SyncServerDatasource) UpsertAccountData( func (d *SyncServerDatasource) UpsertAccountData(
ctx context.Context, userID, roomID, dataType string, ctx context.Context, userID, roomID, dataType string,
) (sp types.StreamPosition, err error) { ) (sp types.StreamPosition, err error) {
txn, err := d.db.BeginTx(ctx, nil) err = common.WithTransaction(d.db, func(txn *sql.Tx) error {
if err != nil { sp, err = d.accountData.insertAccountData(ctx, txn, userID, roomID, dataType)
return types.StreamPosition(0), err return err
} })
var succeeded bool
defer func() {
txerr := common.EndTransaction(txn, &succeeded)
if err == nil && txerr != nil {
err = txerr
}
}()
sp, err = d.accountData.insertAccountData(ctx, txn, userID, roomID, dataType)
return return
} }
@ -764,8 +754,15 @@ func (d *SyncServerDatasource) UpsertAccountData(
// Returns an error if there was a problem communicating with the database. // Returns an error if there was a problem communicating with the database.
func (d *SyncServerDatasource) AddInviteEvent( func (d *SyncServerDatasource) AddInviteEvent(
ctx context.Context, inviteEvent gomatrixserverlib.Event, ctx context.Context, inviteEvent gomatrixserverlib.Event,
) (types.StreamPosition, error) { ) (streamPos types.StreamPosition, err error) {
return d.invites.insertInviteEvent(ctx, inviteEvent) err = common.WithTransaction(d.db, func(txn *sql.Tx) error {
streamPos, err = d.streamID.nextStreamID(ctx, txn)
if err != nil {
return err
}
return d.invites.insertInviteEvent(ctx, txn, inviteEvent, streamPos)
})
return
} }
// RetireInviteEvent removes an old invite event from the database. // RetireInviteEvent removes an old invite event from the database.