2020-07-01 05:46:38 -05:00
|
|
|
// Copyright 2017-2018 New Vector Ltd
|
|
|
|
// Copyright 2019-2020 The Matrix.org Foundation C.I.C.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
package sqlite3
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2020-07-20 10:55:20 -05:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
2020-07-01 05:46:38 -05:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/internal"
|
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 09:50:33 -05:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2020-07-01 05:46:38 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const queuePDUsSchema = `
|
|
|
|
CREATE TABLE IF NOT EXISTS federationsender_queue_pdus (
|
|
|
|
-- The transaction ID that was generated before persisting the event.
|
|
|
|
transaction_id TEXT NOT NULL,
|
|
|
|
-- The domain part of the user ID the m.room.member event is for.
|
|
|
|
server_name TEXT NOT NULL,
|
|
|
|
-- The JSON NID from the federationsender_queue_pdus_json table.
|
|
|
|
json_nid BIGINT NOT NULL
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS federationsender_queue_pdus_pdus_json_nid_idx
|
|
|
|
ON federationsender_queue_pdus (json_nid, server_name);
|
2022-05-30 05:01:20 -05:00
|
|
|
CREATE INDEX IF NOT EXISTS federationsender_queue_pdus_json_nid_idx
|
|
|
|
ON federationsender_queue_pdus (json_nid);
|
|
|
|
CREATE INDEX IF NOT EXISTS federationsender_queue_pdus_server_name_idx
|
|
|
|
ON federationsender_queue_pdus (server_name);
|
2020-07-01 05:46:38 -05:00
|
|
|
`
|
|
|
|
|
|
|
|
const insertQueuePDUSQL = "" +
|
|
|
|
"INSERT INTO federationsender_queue_pdus (transaction_id, server_name, json_nid)" +
|
|
|
|
" VALUES ($1, $2, $3)"
|
|
|
|
|
2020-07-20 10:55:20 -05:00
|
|
|
const deleteQueuePDUsSQL = "" +
|
|
|
|
"DELETE FROM federationsender_queue_pdus WHERE server_name = $1 AND json_nid IN ($2)"
|
2020-07-01 05:46:38 -05:00
|
|
|
|
|
|
|
const selectQueueNextTransactionIDSQL = "" +
|
|
|
|
"SELECT transaction_id FROM federationsender_queue_pdus" +
|
|
|
|
" WHERE server_name = $1" +
|
|
|
|
" ORDER BY transaction_id ASC" +
|
|
|
|
" LIMIT 1"
|
|
|
|
|
2020-12-09 04:03:22 -06:00
|
|
|
const selectQueuePDUsSQL = "" +
|
2020-07-01 05:46:38 -05:00
|
|
|
"SELECT json_nid FROM federationsender_queue_pdus" +
|
2020-12-09 04:03:22 -06:00
|
|
|
" WHERE server_name = $1" +
|
|
|
|
" LIMIT $2"
|
2020-07-01 05:46:38 -05:00
|
|
|
|
2020-07-20 10:55:20 -05:00
|
|
|
const selectQueuePDUsReferenceJSONCountSQL = "" +
|
2020-07-01 05:46:38 -05:00
|
|
|
"SELECT COUNT(*) FROM federationsender_queue_pdus" +
|
|
|
|
" WHERE json_nid = $1"
|
|
|
|
|
2020-07-20 10:55:20 -05:00
|
|
|
const selectQueuePDUsServerNamesSQL = "" +
|
2020-07-03 05:49:49 -05:00
|
|
|
"SELECT DISTINCT server_name FROM federationsender_queue_pdus"
|
|
|
|
|
2020-07-01 05:46:38 -05:00
|
|
|
type queuePDUsStatements struct {
|
2020-07-20 10:55:20 -05:00
|
|
|
db *sql.DB
|
2020-07-01 05:46:38 -05:00
|
|
|
insertQueuePDUStmt *sql.Stmt
|
|
|
|
selectQueueNextTransactionIDStmt *sql.Stmt
|
2020-12-09 04:03:22 -06:00
|
|
|
selectQueuePDUsStmt *sql.Stmt
|
2020-07-01 05:46:38 -05:00
|
|
|
selectQueueReferenceJSONCountStmt *sql.Stmt
|
2020-07-03 05:49:49 -05:00
|
|
|
selectQueueServerNamesStmt *sql.Stmt
|
2020-07-20 10:55:20 -05:00
|
|
|
// deleteQueuePDUsStmt *sql.Stmt - prepared at runtime due to variadic
|
2020-07-01 05:46:38 -05:00
|
|
|
}
|
|
|
|
|
2020-07-20 10:55:20 -05:00
|
|
|
func NewSQLiteQueuePDUsTable(db *sql.DB) (s *queuePDUsStatements, err error) {
|
|
|
|
s = &queuePDUsStatements{
|
2020-08-21 04:42:08 -05:00
|
|
|
db: db,
|
2020-07-20 10:55:20 -05:00
|
|
|
}
|
2020-07-01 05:46:38 -05:00
|
|
|
_, err = db.Exec(queuePDUsSchema)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-03-23 07:52:53 -05:00
|
|
|
return s, sqlutil.StatementList{
|
|
|
|
{&s.insertQueuePDUStmt, insertQueuePDUSQL},
|
|
|
|
{&s.selectQueueNextTransactionIDStmt, selectQueueNextTransactionIDSQL},
|
|
|
|
{&s.selectQueuePDUsStmt, selectQueuePDUsSQL},
|
|
|
|
{&s.selectQueueReferenceJSONCountStmt, selectQueuePDUsReferenceJSONCountSQL},
|
|
|
|
{&s.selectQueueServerNamesStmt, selectQueuePDUsServerNamesSQL},
|
|
|
|
}.Prepare(db)
|
2020-07-01 05:46:38 -05:00
|
|
|
}
|
|
|
|
|
2020-07-20 10:55:20 -05:00
|
|
|
func (s *queuePDUsStatements) InsertQueuePDU(
|
2020-07-01 05:46:38 -05:00
|
|
|
ctx context.Context,
|
|
|
|
txn *sql.Tx,
|
|
|
|
transactionID gomatrixserverlib.TransactionID,
|
2023-04-19 09:50:33 -05:00
|
|
|
serverName spec.ServerName,
|
2020-07-01 05:46:38 -05:00
|
|
|
nid int64,
|
|
|
|
) error {
|
2020-08-21 04:42:08 -05:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.insertQueuePDUStmt)
|
|
|
|
_, err := stmt.ExecContext(
|
|
|
|
ctx,
|
|
|
|
transactionID, // the transaction ID that we initially attempted
|
|
|
|
serverName, // destination server name
|
|
|
|
nid, // JSON blob NID
|
|
|
|
)
|
|
|
|
return err
|
2020-07-01 05:46:38 -05:00
|
|
|
}
|
|
|
|
|
2020-07-20 10:55:20 -05:00
|
|
|
func (s *queuePDUsStatements) DeleteQueuePDUs(
|
2020-07-01 05:46:38 -05:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
2023-04-19 09:50:33 -05:00
|
|
|
serverName spec.ServerName,
|
2020-07-20 10:55:20 -05:00
|
|
|
jsonNIDs []int64,
|
2020-07-01 05:46:38 -05:00
|
|
|
) error {
|
2020-07-20 10:55:20 -05:00
|
|
|
deleteSQL := strings.Replace(deleteQueuePDUsSQL, "($2)", sqlutil.QueryVariadicOffset(len(jsonNIDs), 1), 1)
|
|
|
|
deleteStmt, err := txn.Prepare(deleteSQL)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("s.deleteQueueJSON s.db.Prepare: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := make([]interface{}, len(jsonNIDs)+1)
|
|
|
|
params[0] = serverName
|
|
|
|
for k, v := range jsonNIDs {
|
|
|
|
params[k+1] = v
|
|
|
|
}
|
|
|
|
|
2020-08-21 04:42:08 -05:00
|
|
|
stmt := sqlutil.TxStmt(txn, deleteStmt)
|
|
|
|
_, err = stmt.ExecContext(ctx, params...)
|
|
|
|
return err
|
2020-07-01 05:46:38 -05:00
|
|
|
}
|
|
|
|
|
2020-07-20 10:55:20 -05:00
|
|
|
func (s *queuePDUsStatements) SelectQueuePDUNextTransactionID(
|
2023-04-19 09:50:33 -05:00
|
|
|
ctx context.Context, txn *sql.Tx, serverName spec.ServerName,
|
2020-07-01 05:46:38 -05:00
|
|
|
) (gomatrixserverlib.TransactionID, error) {
|
|
|
|
var transactionID gomatrixserverlib.TransactionID
|
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectQueueNextTransactionIDStmt)
|
|
|
|
err := stmt.QueryRowContext(ctx, serverName).Scan(&transactionID)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
return transactionID, err
|
|
|
|
}
|
|
|
|
|
2020-07-20 10:55:20 -05:00
|
|
|
func (s *queuePDUsStatements) SelectQueuePDUReferenceJSONCount(
|
2020-07-01 05:46:38 -05:00
|
|
|
ctx context.Context, txn *sql.Tx, jsonNID int64,
|
|
|
|
) (int64, error) {
|
|
|
|
var count int64
|
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectQueueReferenceJSONCountStmt)
|
|
|
|
err := stmt.QueryRowContext(ctx, jsonNID).Scan(&count)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
2020-07-20 10:55:20 -05:00
|
|
|
func (s *queuePDUsStatements) SelectQueuePDUs(
|
2020-07-01 05:46:38 -05:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
2023-04-19 09:50:33 -05:00
|
|
|
serverName spec.ServerName,
|
2020-07-01 05:46:38 -05:00
|
|
|
limit int,
|
|
|
|
) ([]int64, error) {
|
2020-12-09 04:03:22 -06:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectQueuePDUsStmt)
|
|
|
|
rows, err := stmt.QueryContext(ctx, serverName, limit)
|
2020-07-01 05:46:38 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "queueFromStmt: rows.close() failed")
|
|
|
|
var result []int64
|
|
|
|
for rows.Next() {
|
|
|
|
var nid int64
|
|
|
|
if err = rows.Scan(&nid); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, nid)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, rows.Err()
|
|
|
|
}
|
2020-07-03 05:49:49 -05:00
|
|
|
|
2020-07-20 10:55:20 -05:00
|
|
|
func (s *queuePDUsStatements) SelectQueuePDUServerNames(
|
2020-07-03 05:49:49 -05:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
2023-04-19 09:50:33 -05:00
|
|
|
) ([]spec.ServerName, error) {
|
2020-07-03 05:49:49 -05:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectQueueServerNamesStmt)
|
|
|
|
rows, err := stmt.QueryContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "queueFromStmt: rows.close() failed")
|
2023-04-19 09:50:33 -05:00
|
|
|
var result []spec.ServerName
|
2020-07-03 05:49:49 -05:00
|
|
|
for rows.Next() {
|
2023-04-19 09:50:33 -05:00
|
|
|
var serverName spec.ServerName
|
2020-07-03 05:49:49 -05:00
|
|
|
if err = rows.Scan(&serverName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, serverName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, rows.Err()
|
|
|
|
}
|