2020-01-03 15:07:05 +01:00
|
|
|
// Copyright 2017-2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
2017-04-21 00:40:52 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2020-01-03 15:07:05 +01:00
|
|
|
package postgres
|
2017-02-15 15:43:19 +01:00
|
|
|
|
|
|
|
import (
|
2017-09-13 17:30:19 +02:00
|
|
|
"context"
|
2017-02-15 15:43:19 +01:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2017-08-07 12:51:46 +02:00
|
|
|
|
2017-02-15 15:43:19 +01:00
|
|
|
"github.com/lib/pq"
|
2020-06-12 15:55:57 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
2021-07-28 19:30:04 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2020-05-27 10:36:09 +02:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/storage/tables"
|
2017-02-15 15:43:19 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/types"
|
2017-03-09 16:07:18 +01:00
|
|
|
"github.com/matrix-org/util"
|
2017-02-15 15:43:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const stateDataSchema = `
|
|
|
|
-- The state data map.
|
|
|
|
-- Designed to give enough information to run the state resolution algorithm
|
2020-05-21 15:40:13 +02:00
|
|
|
-- without hitting the database in the internal case.
|
2017-02-15 15:43:19 +01:00
|
|
|
-- TODO: Is it worth replacing the unique btree index with a covering index so
|
|
|
|
-- that postgres could lookup the state using an index-only scan?
|
|
|
|
-- The type and state_key are included in the index to make it easier to
|
|
|
|
-- lookup a specific (type, state_key) pair for an event. It also makes it easy
|
|
|
|
-- to read the state for a given state_block_nid ordered by (type, state_key)
|
|
|
|
-- which in turn makes it easier to merge state data blocks.
|
2017-08-07 12:51:46 +02:00
|
|
|
CREATE SEQUENCE IF NOT EXISTS roomserver_state_block_nid_seq;
|
|
|
|
CREATE TABLE IF NOT EXISTS roomserver_state_block (
|
2021-04-26 14:25:57 +02:00
|
|
|
-- The state snapshot NID that identifies this snapshot.
|
|
|
|
state_block_nid bigint PRIMARY KEY DEFAULT nextval('roomserver_state_block_nid_seq'),
|
|
|
|
-- The hash of the state block, which is used to enforce uniqueness. The hash is
|
|
|
|
-- generated in Dendrite and passed through to the database, as a btree index over
|
|
|
|
-- this column is cheap and fits within the maximum index size.
|
|
|
|
state_block_hash BYTEA UNIQUE,
|
|
|
|
-- The event NIDs contained within the state block.
|
|
|
|
event_nids bigint[] NOT NULL
|
2017-02-15 15:43:19 +01:00
|
|
|
);
|
|
|
|
`
|
|
|
|
|
2021-04-26 14:25:57 +02:00
|
|
|
// Insert a new state block. If we conflict on the hash column then
|
|
|
|
// we must perform an update so that the RETURNING statement returns the
|
|
|
|
// ID of the row that we conflicted with, so that we can then refer to
|
|
|
|
// the original block.
|
2017-02-15 15:43:19 +01:00
|
|
|
const insertStateDataSQL = "" +
|
2021-04-26 14:25:57 +02:00
|
|
|
"INSERT INTO roomserver_state_block (state_block_hash, event_nids)" +
|
|
|
|
" VALUES ($1, $2)" +
|
|
|
|
" ON CONFLICT (state_block_hash) DO UPDATE SET event_nids=$2" +
|
|
|
|
" RETURNING state_block_nid"
|
2017-02-15 15:43:19 +01:00
|
|
|
|
2017-03-09 16:07:18 +01:00
|
|
|
const bulkSelectStateBlockEntriesSQL = "" +
|
2021-04-26 14:25:57 +02:00
|
|
|
"SELECT state_block_nid, event_nids" +
|
2021-07-09 11:49:49 +02:00
|
|
|
" FROM roomserver_state_block WHERE state_block_nid = ANY($1) ORDER BY state_block_nid ASC"
|
2017-03-09 16:07:18 +01:00
|
|
|
|
2017-02-15 15:43:19 +01:00
|
|
|
type stateBlockStatements struct {
|
2021-04-26 14:25:57 +02:00
|
|
|
insertStateDataStmt *sql.Stmt
|
|
|
|
bulkSelectStateBlockEntriesStmt *sql.Stmt
|
2017-02-15 15:43:19 +01:00
|
|
|
}
|
|
|
|
|
2022-05-16 19:33:16 +02:00
|
|
|
func CreateStateBlockTable(db *sql.DB) error {
|
2020-05-27 10:36:09 +02:00
|
|
|
_, err := db.Exec(stateDataSchema)
|
2021-04-26 14:25:57 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-16 19:33:16 +02:00
|
|
|
func PrepareStateBlockTable(db *sql.DB) (tables.StateBlock, error) {
|
2021-04-26 14:25:57 +02:00
|
|
|
s := &stateBlockStatements{}
|
2017-03-07 11:37:41 +01:00
|
|
|
|
2021-07-28 19:30:04 +02:00
|
|
|
return s, sqlutil.StatementList{
|
2017-03-07 11:37:41 +01:00
|
|
|
{&s.insertStateDataStmt, insertStateDataSQL},
|
2017-03-09 16:07:18 +01:00
|
|
|
{&s.bulkSelectStateBlockEntriesStmt, bulkSelectStateBlockEntriesSQL},
|
2020-05-27 12:03:47 +02:00
|
|
|
}.Prepare(db)
|
2017-02-15 15:43:19 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 10:36:09 +02:00
|
|
|
func (s *stateBlockStatements) BulkInsertStateData(
|
2022-02-04 11:39:34 +01:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
2021-04-26 14:25:57 +02:00
|
|
|
entries types.StateEntries,
|
|
|
|
) (id types.StateBlockNID, err error) {
|
|
|
|
entries = entries[:util.SortAndUnique(entries)]
|
2022-05-16 19:33:16 +02:00
|
|
|
nids := make(types.EventNIDs, entries.Len())
|
|
|
|
for i := range entries {
|
|
|
|
nids[i] = entries[i].EventNID
|
2017-02-15 15:43:19 +01:00
|
|
|
}
|
2022-02-04 11:39:34 +01:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.insertStateDataStmt)
|
|
|
|
err = stmt.QueryRowContext(
|
2021-04-26 14:25:57 +02:00
|
|
|
ctx, nids.Hash(), eventNIDsAsArray(nids),
|
|
|
|
).Scan(&id)
|
|
|
|
return
|
2017-02-15 15:43:19 +01:00
|
|
|
}
|
|
|
|
|
2020-05-27 10:36:09 +02:00
|
|
|
func (s *stateBlockStatements) BulkSelectStateBlockEntries(
|
2022-02-04 11:39:34 +01:00
|
|
|
ctx context.Context, txn *sql.Tx, stateBlockNIDs types.StateBlockNIDs,
|
2021-04-26 14:25:57 +02:00
|
|
|
) ([][]types.EventNID, error) {
|
2022-02-04 11:39:34 +01:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.bulkSelectStateBlockEntriesStmt)
|
|
|
|
rows, err := stmt.QueryContext(ctx, stateBlockNIDsAsArray(stateBlockNIDs))
|
2017-02-15 15:43:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-21 15:40:13 +02:00
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "bulkSelectStateBlockEntries: rows.close() failed")
|
2017-02-15 15:43:19 +01:00
|
|
|
|
2021-04-26 14:25:57 +02:00
|
|
|
results := make([][]types.EventNID, len(stateBlockNIDs))
|
2017-02-15 15:43:19 +01:00
|
|
|
i := 0
|
2022-05-16 19:33:16 +02:00
|
|
|
var stateBlockNID types.StateBlockNID
|
|
|
|
var result pq.Int64Array
|
2021-04-26 14:25:57 +02:00
|
|
|
for ; rows.Next(); i++ {
|
|
|
|
if err = rows.Scan(&stateBlockNID, &result); err != nil {
|
2017-02-15 15:43:19 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-16 19:33:16 +02:00
|
|
|
r := make([]types.EventNID, len(result))
|
|
|
|
for x := range result {
|
|
|
|
r[x] = types.EventNID(result[x])
|
2017-02-15 15:43:19 +01:00
|
|
|
}
|
2021-04-26 14:25:57 +02:00
|
|
|
results[i] = r
|
2017-02-15 15:43:19 +01:00
|
|
|
}
|
2020-02-11 15:12:21 +01:00
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-15 15:43:19 +01:00
|
|
|
if i != len(stateBlockNIDs) {
|
|
|
|
return nil, fmt.Errorf("storage: state data NIDs missing from the database (%d != %d)", i, len(stateBlockNIDs))
|
|
|
|
}
|
2020-02-11 15:12:21 +01:00
|
|
|
return results, err
|
2017-02-15 15:43:19 +01:00
|
|
|
}
|
2017-03-09 16:07:18 +01:00
|
|
|
|
|
|
|
func stateBlockNIDsAsArray(stateBlockNIDs []types.StateBlockNID) pq.Int64Array {
|
|
|
|
nids := make([]int64, len(stateBlockNIDs))
|
|
|
|
for i := range stateBlockNIDs {
|
|
|
|
nids[i] = int64(stateBlockNIDs[i])
|
|
|
|
}
|
|
|
|
return pq.Int64Array(nids)
|
|
|
|
}
|