From 9d53351dc20283103bf2eec6b92831033d06c5a8 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Fri, 21 Aug 2020 10:42:08 +0100 Subject: Component-wide TransactionWriters (#1290) * Offset updates take place using TransactionWriter * Refactor TransactionWriter in current state server * Refactor TransactionWriter in federation sender * Refactor TransactionWriter in key server * Refactor TransactionWriter in media API * Refactor TransactionWriter in server key API * Refactor TransactionWriter in sync API * Refactor TransactionWriter in user API * Fix deadlocking Sync API tests * Un-deadlock device database * Fix appservice API * Rename TransactionWriters to Writers * Move writers up a layer in sync API * Document sqlutil.Writer interface * Add note to Writer documentation --- appservice/storage/postgres/storage.go | 4 +++- appservice/storage/sqlite3/appservice_events_table.go | 6 +++--- appservice/storage/sqlite3/storage.go | 8 +++++--- appservice/storage/sqlite3/txn_id_counter_table.go | 6 +++--- 4 files changed, 14 insertions(+), 10 deletions(-) (limited to 'appservice') diff --git a/appservice/storage/postgres/storage.go b/appservice/storage/postgres/storage.go index 9fda87ae..95215816 100644 --- a/appservice/storage/postgres/storage.go +++ b/appservice/storage/postgres/storage.go @@ -32,6 +32,7 @@ type Database struct { events eventsStatements txnID txnStatements db *sql.DB + writer sqlutil.Writer } // NewDatabase opens a new database @@ -41,10 +42,11 @@ func NewDatabase(dbProperties *config.DatabaseOptions) (*Database, error) { if result.db, err = sqlutil.Open(dbProperties); err != nil { return nil, err } + result.writer = sqlutil.NewDummyWriter() if err = result.prepare(); err != nil { return nil, err } - if err = result.PartitionOffsetStatements.Prepare(result.db, "appservice"); err != nil { + if err = result.PartitionOffsetStatements.Prepare(result.db, result.writer, "appservice"); err != nil { return nil, err } return &result, nil diff --git a/appservice/storage/sqlite3/appservice_events_table.go b/appservice/storage/sqlite3/appservice_events_table.go index 5cc07ed3..5dfb72f6 100644 --- a/appservice/storage/sqlite3/appservice_events_table.go +++ b/appservice/storage/sqlite3/appservice_events_table.go @@ -67,7 +67,7 @@ const ( type eventsStatements struct { db *sql.DB - writer sqlutil.TransactionWriter + writer sqlutil.Writer selectEventsByApplicationServiceIDStmt *sql.Stmt countEventsByApplicationServiceIDStmt *sql.Stmt insertEventStmt *sql.Stmt @@ -75,9 +75,9 @@ type eventsStatements struct { deleteEventsBeforeAndIncludingIDStmt *sql.Stmt } -func (s *eventsStatements) prepare(db *sql.DB) (err error) { +func (s *eventsStatements) prepare(db *sql.DB, writer sqlutil.Writer) (err error) { s.db = db - s.writer = sqlutil.NewTransactionWriter() + s.writer = writer _, err = db.Exec(appserviceEventsSchema) if err != nil { return diff --git a/appservice/storage/sqlite3/storage.go b/appservice/storage/sqlite3/storage.go index 59af9016..916845ab 100644 --- a/appservice/storage/sqlite3/storage.go +++ b/appservice/storage/sqlite3/storage.go @@ -32,6 +32,7 @@ type Database struct { events eventsStatements txnID txnStatements db *sql.DB + writer sqlutil.Writer } // NewDatabase opens a new database @@ -41,21 +42,22 @@ func NewDatabase(dbProperties *config.DatabaseOptions) (*Database, error) { if result.db, err = sqlutil.Open(dbProperties); err != nil { return nil, err } + result.writer = sqlutil.NewExclusiveWriter() if err = result.prepare(); err != nil { return nil, err } - if err = result.PartitionOffsetStatements.Prepare(result.db, "appservice"); err != nil { + if err = result.PartitionOffsetStatements.Prepare(result.db, result.writer, "appservice"); err != nil { return nil, err } return &result, nil } func (d *Database) prepare() error { - if err := d.events.prepare(d.db); err != nil { + if err := d.events.prepare(d.db, d.writer); err != nil { return err } - return d.txnID.prepare(d.db) + return d.txnID.prepare(d.db, d.writer) } // StoreEvent takes in a gomatrixserverlib.HeaderedEvent and stores it in the database diff --git a/appservice/storage/sqlite3/txn_id_counter_table.go b/appservice/storage/sqlite3/txn_id_counter_table.go index 0ae0feee..b2940e35 100644 --- a/appservice/storage/sqlite3/txn_id_counter_table.go +++ b/appservice/storage/sqlite3/txn_id_counter_table.go @@ -38,13 +38,13 @@ const selectTxnIDSQL = ` type txnStatements struct { db *sql.DB - writer sqlutil.TransactionWriter + writer sqlutil.Writer selectTxnIDStmt *sql.Stmt } -func (s *txnStatements) prepare(db *sql.DB) (err error) { +func (s *txnStatements) prepare(db *sql.DB, writer sqlutil.Writer) (err error) { s.db = db - s.writer = sqlutil.NewTransactionWriter() + s.writer = writer _, err = db.Exec(txnIDSchema) if err != nil { return -- cgit v1.2.3