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
|
|
|
"sort"
|
|
|
|
|
2017-02-15 15:43:19 +01:00
|
|
|
"github.com/lib/pq"
|
|
|
|
"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
|
|
|
|
-- without hitting the database in the common case.
|
|
|
|
-- 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 (
|
2017-02-15 15:43:19 +01:00
|
|
|
-- Local numeric ID for this state data.
|
|
|
|
state_block_nid bigint NOT NULL,
|
|
|
|
event_type_nid bigint NOT NULL,
|
|
|
|
event_state_key_nid bigint NOT NULL,
|
|
|
|
event_nid bigint NOT NULL,
|
|
|
|
UNIQUE (state_block_nid, event_type_nid, event_state_key_nid)
|
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
const insertStateDataSQL = "" +
|
2017-08-07 12:51:46 +02:00
|
|
|
"INSERT INTO roomserver_state_block (state_block_nid, event_type_nid, event_state_key_nid, event_nid)" +
|
2017-02-15 15:43:19 +01:00
|
|
|
" VALUES ($1, $2, $3, $4)"
|
|
|
|
|
|
|
|
const selectNextStateBlockNIDSQL = "" +
|
2017-08-07 12:51:46 +02:00
|
|
|
"SELECT nextval('roomserver_state_block_nid_seq')"
|
2017-02-15 15:43:19 +01:00
|
|
|
|
2017-03-09 16:07:18 +01:00
|
|
|
// Bulk state lookup by numeric state block ID.
|
2017-02-15 15:43:19 +01:00
|
|
|
// Sort by the state_block_nid, event_type_nid, event_state_key_nid
|
|
|
|
// This means that all the entries for a given state_block_nid will appear
|
|
|
|
// together in the list and those entries will sorted by event_type_nid
|
|
|
|
// and event_state_key_nid. This property makes it easier to merge two
|
|
|
|
// state data blocks together.
|
2017-03-09 16:07:18 +01:00
|
|
|
const bulkSelectStateBlockEntriesSQL = "" +
|
2017-02-15 15:43:19 +01:00
|
|
|
"SELECT state_block_nid, event_type_nid, event_state_key_nid, event_nid" +
|
2017-08-07 12:51:46 +02:00
|
|
|
" FROM roomserver_state_block WHERE state_block_nid = ANY($1)" +
|
2017-02-15 15:43:19 +01:00
|
|
|
" ORDER BY state_block_nid, event_type_nid, event_state_key_nid"
|
|
|
|
|
2017-03-09 16:07:18 +01:00
|
|
|
// Bulk state lookup by numeric state block ID.
|
|
|
|
// Filters the rows in each block to the requested types and state keys.
|
|
|
|
// We would like to restrict to particular type state key pairs but we are
|
|
|
|
// restricted by the query language to pull the cross product of a list
|
|
|
|
// of types and a list state_keys. So we have to filter the result in the
|
|
|
|
// application to restrict it to the list of event types and state keys we
|
|
|
|
// actually wanted.
|
|
|
|
const bulkSelectFilteredStateBlockEntriesSQL = "" +
|
|
|
|
"SELECT state_block_nid, event_type_nid, event_state_key_nid, event_nid" +
|
2017-08-07 12:51:46 +02:00
|
|
|
" FROM roomserver_state_block WHERE state_block_nid = ANY($1)" +
|
2017-03-09 16:07:18 +01:00
|
|
|
" AND event_type_nid = ANY($2) AND event_state_key_nid = ANY($3)" +
|
|
|
|
" ORDER BY state_block_nid, event_type_nid, event_state_key_nid"
|
|
|
|
|
2017-02-15 15:43:19 +01:00
|
|
|
type stateBlockStatements struct {
|
2017-03-09 16:07:18 +01:00
|
|
|
insertStateDataStmt *sql.Stmt
|
|
|
|
selectNextStateBlockNIDStmt *sql.Stmt
|
|
|
|
bulkSelectStateBlockEntriesStmt *sql.Stmt
|
|
|
|
bulkSelectFilteredStateBlockEntriesStmt *sql.Stmt
|
2017-02-15 15:43:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stateBlockStatements) prepare(db *sql.DB) (err error) {
|
|
|
|
_, err = db.Exec(stateDataSchema)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2017-03-07 11:37:41 +01:00
|
|
|
|
|
|
|
return statementList{
|
|
|
|
{&s.insertStateDataStmt, insertStateDataSQL},
|
|
|
|
{&s.selectNextStateBlockNIDStmt, selectNextStateBlockNIDSQL},
|
2017-03-09 16:07:18 +01:00
|
|
|
{&s.bulkSelectStateBlockEntriesStmt, bulkSelectStateBlockEntriesSQL},
|
|
|
|
{&s.bulkSelectFilteredStateBlockEntriesStmt, bulkSelectFilteredStateBlockEntriesSQL},
|
2017-03-07 11:37:41 +01:00
|
|
|
}.prepare(db)
|
2017-02-15 15:43:19 +01:00
|
|
|
}
|
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
func (s *stateBlockStatements) bulkInsertStateData(
|
|
|
|
ctx context.Context,
|
|
|
|
stateBlockNID types.StateBlockNID,
|
|
|
|
entries []types.StateEntry,
|
|
|
|
) error {
|
2017-02-15 15:43:19 +01:00
|
|
|
for _, entry := range entries {
|
2017-09-13 17:30:19 +02:00
|
|
|
_, err := s.insertStateDataStmt.ExecContext(
|
|
|
|
ctx,
|
2017-02-15 15:43:19 +01:00
|
|
|
int64(stateBlockNID),
|
|
|
|
int64(entry.EventTypeNID),
|
|
|
|
int64(entry.EventStateKeyNID),
|
|
|
|
int64(entry.EventNID),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
func (s *stateBlockStatements) selectNextStateBlockNID(
|
|
|
|
ctx context.Context,
|
|
|
|
) (types.StateBlockNID, error) {
|
2017-02-15 15:43:19 +01:00
|
|
|
var stateBlockNID int64
|
2017-09-13 17:30:19 +02:00
|
|
|
err := s.selectNextStateBlockNIDStmt.QueryRowContext(ctx).Scan(&stateBlockNID)
|
2017-02-15 15:43:19 +01:00
|
|
|
return types.StateBlockNID(stateBlockNID), err
|
|
|
|
}
|
|
|
|
|
2017-09-13 17:30:19 +02:00
|
|
|
func (s *stateBlockStatements) bulkSelectStateBlockEntries(
|
|
|
|
ctx context.Context, stateBlockNIDs []types.StateBlockNID,
|
|
|
|
) ([]types.StateEntryList, error) {
|
2017-02-15 15:43:19 +01:00
|
|
|
nids := make([]int64, len(stateBlockNIDs))
|
|
|
|
for i := range stateBlockNIDs {
|
|
|
|
nids[i] = int64(stateBlockNIDs[i])
|
|
|
|
}
|
2017-09-13 17:30:19 +02:00
|
|
|
rows, err := s.bulkSelectStateBlockEntriesStmt.QueryContext(ctx, pq.Int64Array(nids))
|
2017-02-15 15:43:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-20 11:59:19 +02:00
|
|
|
defer rows.Close() // nolint: errcheck
|
2017-02-15 15:43:19 +01:00
|
|
|
|
|
|
|
results := make([]types.StateEntryList, len(stateBlockNIDs))
|
|
|
|
// current is a pointer to the StateEntryList to append the state entries to.
|
|
|
|
var current *types.StateEntryList
|
|
|
|
i := 0
|
|
|
|
for rows.Next() {
|
|
|
|
var (
|
|
|
|
stateBlockNID int64
|
|
|
|
eventTypeNID int64
|
|
|
|
eventStateKeyNID int64
|
|
|
|
eventNID int64
|
|
|
|
entry types.StateEntry
|
|
|
|
)
|
2020-02-11 15:12:21 +01:00
|
|
|
if err = rows.Scan(
|
2017-02-15 15:43:19 +01:00
|
|
|
&stateBlockNID, &eventTypeNID, &eventStateKeyNID, &eventNID,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
entry.EventTypeNID = types.EventTypeNID(eventTypeNID)
|
|
|
|
entry.EventStateKeyNID = types.EventStateKeyNID(eventStateKeyNID)
|
|
|
|
entry.EventNID = types.EventNID(eventNID)
|
|
|
|
if current == nil || types.StateBlockNID(stateBlockNID) != current.StateBlockNID {
|
|
|
|
// The state entry row is for a different state data block to the current one.
|
|
|
|
// So we start appending to the next entry in the list.
|
|
|
|
current = &results[i]
|
|
|
|
current.StateBlockNID = types.StateBlockNID(stateBlockNID)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
current.StateEntries = append(current.StateEntries, entry)
|
|
|
|
}
|
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 (s *stateBlockStatements) bulkSelectFilteredStateBlockEntries(
|
2017-09-13 17:30:19 +02:00
|
|
|
ctx context.Context,
|
|
|
|
stateBlockNIDs []types.StateBlockNID,
|
|
|
|
stateKeyTuples []types.StateKeyTuple,
|
2017-03-09 16:07:18 +01:00
|
|
|
) ([]types.StateEntryList, error) {
|
|
|
|
tuples := stateKeyTupleSorter(stateKeyTuples)
|
|
|
|
// Sort the tuples so that we can run binary search against them as we filter the rows returned by the db.
|
|
|
|
sort.Sort(tuples)
|
|
|
|
|
|
|
|
eventTypeNIDArray, eventStateKeyNIDArray := tuples.typesAndStateKeysAsArrays()
|
2017-09-13 17:30:19 +02:00
|
|
|
rows, err := s.bulkSelectFilteredStateBlockEntriesStmt.QueryContext(
|
|
|
|
ctx,
|
|
|
|
stateBlockNIDsAsArray(stateBlockNIDs),
|
|
|
|
eventTypeNIDArray,
|
|
|
|
eventStateKeyNIDArray,
|
2017-03-09 16:07:18 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-20 11:59:19 +02:00
|
|
|
defer rows.Close() // nolint: errcheck
|
2017-03-09 16:07:18 +01:00
|
|
|
|
|
|
|
var results []types.StateEntryList
|
|
|
|
var current types.StateEntryList
|
|
|
|
for rows.Next() {
|
|
|
|
var (
|
|
|
|
stateBlockNID int64
|
|
|
|
eventTypeNID int64
|
|
|
|
eventStateKeyNID int64
|
|
|
|
eventNID int64
|
|
|
|
entry types.StateEntry
|
|
|
|
)
|
|
|
|
if err := rows.Scan(
|
|
|
|
&stateBlockNID, &eventTypeNID, &eventStateKeyNID, &eventNID,
|
|
|
|
); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
entry.EventTypeNID = types.EventTypeNID(eventTypeNID)
|
|
|
|
entry.EventStateKeyNID = types.EventStateKeyNID(eventStateKeyNID)
|
|
|
|
entry.EventNID = types.EventNID(eventNID)
|
|
|
|
|
|
|
|
// We can use binary search here because we sorted the tuples earlier
|
|
|
|
if !tuples.contains(entry.StateKeyTuple) {
|
|
|
|
// The select will return the cross product of types and state keys.
|
|
|
|
// So we need to check if type of the entry is in the list.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if types.StateBlockNID(stateBlockNID) != current.StateBlockNID {
|
|
|
|
// The state entry row is for a different state data block to the current one.
|
|
|
|
// So we append the current entry to the results and start adding to a new one.
|
|
|
|
// The first time through the loop current will be empty.
|
|
|
|
if current.StateEntries != nil {
|
|
|
|
results = append(results, current)
|
|
|
|
}
|
|
|
|
current = types.StateEntryList{StateBlockNID: types.StateBlockNID(stateBlockNID)}
|
|
|
|
}
|
|
|
|
current.StateEntries = append(current.StateEntries, entry)
|
|
|
|
}
|
|
|
|
// Add the last entry to the list if it is not empty.
|
|
|
|
if current.StateEntries != nil {
|
|
|
|
results = append(results, current)
|
|
|
|
}
|
2020-02-11 15:12:21 +01:00
|
|
|
return results, rows.Err()
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
type stateKeyTupleSorter []types.StateKeyTuple
|
|
|
|
|
|
|
|
func (s stateKeyTupleSorter) Len() int { return len(s) }
|
|
|
|
func (s stateKeyTupleSorter) Less(i, j int) bool { return s[i].LessThan(s[j]) }
|
|
|
|
func (s stateKeyTupleSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|
|
|
|
|
|
|
|
// Check whether a tuple is in the list. Assumes that the list is sorted.
|
|
|
|
func (s stateKeyTupleSorter) contains(value types.StateKeyTuple) bool {
|
|
|
|
i := sort.Search(len(s), func(i int) bool { return !s[i].LessThan(value) })
|
|
|
|
return i < len(s) && s[i] == value
|
|
|
|
}
|
|
|
|
|
|
|
|
// List the unique eventTypeNIDs and eventStateKeyNIDs.
|
|
|
|
// Assumes that the list is sorted.
|
|
|
|
func (s stateKeyTupleSorter) typesAndStateKeysAsArrays() (eventTypeNIDs pq.Int64Array, eventStateKeyNIDs pq.Int64Array) {
|
|
|
|
eventTypeNIDs = make(pq.Int64Array, len(s))
|
|
|
|
eventStateKeyNIDs = make(pq.Int64Array, len(s))
|
|
|
|
for i := range s {
|
|
|
|
eventTypeNIDs[i] = int64(s[i].EventTypeNID)
|
|
|
|
eventStateKeyNIDs[i] = int64(s[i].EventStateKeyNID)
|
|
|
|
}
|
|
|
|
eventTypeNIDs = eventTypeNIDs[:util.SortAndUnique(int64Sorter(eventTypeNIDs))]
|
|
|
|
eventStateKeyNIDs = eventStateKeyNIDs[:util.SortAndUnique(int64Sorter(eventStateKeyNIDs))]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type int64Sorter []int64
|
|
|
|
|
|
|
|
func (s int64Sorter) Len() int { return len(s) }
|
|
|
|
func (s int64Sorter) Less(i, j int) bool { return s[i] < s[j] }
|
|
|
|
func (s int64Sorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
|