2020-07-01 12:46:38 +02:00
|
|
|
// Copyright 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 postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
|
2020-07-20 17:55:20 +02:00
|
|
|
"github.com/lib/pq"
|
2020-07-01 12:46:38 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
2023-04-19 16:50:33 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2020-07-01 12:46:38 +02: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 destination server that we will send the event to.
|
|
|
|
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 12:01:20 +02: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 12:46:38 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
const insertQueuePDUSQL = "" +
|
|
|
|
"INSERT INTO federationsender_queue_pdus (transaction_id, server_name, json_nid)" +
|
|
|
|
" VALUES ($1, $2, $3)"
|
|
|
|
|
2020-07-20 17:55:20 +02:00
|
|
|
const deleteQueuePDUSQL = "" +
|
|
|
|
"DELETE FROM federationsender_queue_pdus WHERE server_name = $1 AND json_nid = ANY($2)"
|
2020-07-01 12:46:38 +02:00
|
|
|
|
2020-12-09 11:03:22 +01:00
|
|
|
const selectQueuePDUsSQL = "" +
|
2020-07-01 12:46:38 +02:00
|
|
|
"SELECT json_nid FROM federationsender_queue_pdus" +
|
2020-12-09 11:03:22 +01:00
|
|
|
" WHERE server_name = $1" +
|
|
|
|
" LIMIT $2"
|
2020-07-01 12:46:38 +02:00
|
|
|
|
2020-07-20 17:55:20 +02:00
|
|
|
const selectQueuePDUReferenceJSONCountSQL = "" +
|
2020-07-01 12:46:38 +02:00
|
|
|
"SELECT COUNT(*) FROM federationsender_queue_pdus" +
|
|
|
|
" WHERE json_nid = $1"
|
|
|
|
|
2020-07-20 17:55:20 +02:00
|
|
|
const selectQueuePDUServerNamesSQL = "" +
|
2020-07-03 12:49:49 +02:00
|
|
|
"SELECT DISTINCT server_name FROM federationsender_queue_pdus"
|
|
|
|
|
2020-07-01 12:46:38 +02:00
|
|
|
type queuePDUsStatements struct {
|
2020-07-20 17:55:20 +02:00
|
|
|
db *sql.DB
|
|
|
|
insertQueuePDUStmt *sql.Stmt
|
|
|
|
deleteQueuePDUsStmt *sql.Stmt
|
2020-12-09 11:03:22 +01:00
|
|
|
selectQueuePDUsStmt *sql.Stmt
|
2020-07-20 17:55:20 +02:00
|
|
|
selectQueuePDUReferenceJSONCountStmt *sql.Stmt
|
|
|
|
selectQueuePDUServerNamesStmt *sql.Stmt
|
2020-07-01 12:46:38 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 17:55:20 +02:00
|
|
|
func NewPostgresQueuePDUsTable(db *sql.DB) (s *queuePDUsStatements, err error) {
|
|
|
|
s = &queuePDUsStatements{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
_, err = s.db.Exec(queuePDUsSchema)
|
2020-07-01 12:46:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-03-23 13:52:53 +01:00
|
|
|
return s, sqlutil.StatementList{
|
|
|
|
{&s.insertQueuePDUStmt, insertQueuePDUSQL},
|
|
|
|
{&s.deleteQueuePDUsStmt, deleteQueuePDUSQL},
|
|
|
|
{&s.selectQueuePDUsStmt, selectQueuePDUsSQL},
|
|
|
|
{&s.selectQueuePDUReferenceJSONCountStmt, selectQueuePDUReferenceJSONCountSQL},
|
|
|
|
{&s.selectQueuePDUServerNamesStmt, selectQueuePDUServerNamesSQL},
|
|
|
|
}.Prepare(db)
|
2020-07-01 12:46:38 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 17:55:20 +02:00
|
|
|
func (s *queuePDUsStatements) InsertQueuePDU(
|
2020-07-01 12:46:38 +02:00
|
|
|
ctx context.Context,
|
|
|
|
txn *sql.Tx,
|
|
|
|
transactionID gomatrixserverlib.TransactionID,
|
2023-04-19 16:50:33 +02:00
|
|
|
serverName spec.ServerName,
|
2020-07-01 12:46:38 +02:00
|
|
|
nid int64,
|
|
|
|
) error {
|
|
|
|
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-20 17:55:20 +02:00
|
|
|
func (s *queuePDUsStatements) DeleteQueuePDUs(
|
2020-07-01 12:46:38 +02:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
2023-04-19 16:50:33 +02:00
|
|
|
serverName spec.ServerName,
|
2020-07-20 17:55:20 +02:00
|
|
|
jsonNIDs []int64,
|
2020-07-01 12:46:38 +02:00
|
|
|
) error {
|
2020-07-20 17:55:20 +02:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.deleteQueuePDUsStmt)
|
|
|
|
_, err := stmt.ExecContext(ctx, serverName, pq.Int64Array(jsonNIDs))
|
2020-07-01 12:46:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:55:20 +02:00
|
|
|
func (s *queuePDUsStatements) SelectQueuePDUReferenceJSONCount(
|
2020-07-01 12:46:38 +02:00
|
|
|
ctx context.Context, txn *sql.Tx, jsonNID int64,
|
|
|
|
) (int64, error) {
|
|
|
|
var count int64
|
2020-07-20 17:55:20 +02:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectQueuePDUReferenceJSONCountStmt)
|
2020-07-01 12:46:38 +02:00
|
|
|
err := stmt.QueryRowContext(ctx, jsonNID).Scan(&count)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// It's acceptable for there to be no rows referencing a given
|
|
|
|
// JSON NID but it's not an error condition. Just return as if
|
|
|
|
// there's a zero count.
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
2020-07-20 17:55:20 +02:00
|
|
|
func (s *queuePDUsStatements) SelectQueuePDUs(
|
2020-07-01 12:46:38 +02:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
2023-04-19 16:50:33 +02:00
|
|
|
serverName spec.ServerName,
|
2020-07-01 12:46:38 +02:00
|
|
|
limit int,
|
|
|
|
) ([]int64, error) {
|
2020-12-09 11:03:22 +01:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectQueuePDUsStmt)
|
|
|
|
rows, err := stmt.QueryContext(ctx, serverName, limit)
|
2020-07-01 12:46:38 +02: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 12:49:49 +02:00
|
|
|
|
2020-07-20 17:55:20 +02:00
|
|
|
func (s *queuePDUsStatements) SelectQueuePDUServerNames(
|
2020-07-03 12:49:49 +02:00
|
|
|
ctx context.Context, txn *sql.Tx,
|
2023-04-19 16:50:33 +02:00
|
|
|
) ([]spec.ServerName, error) {
|
2020-07-20 17:55:20 +02:00
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectQueuePDUServerNamesStmt)
|
2020-07-03 12:49:49 +02:00
|
|
|
rows, err := stmt.QueryContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "queueFromStmt: rows.close() failed")
|
2023-04-19 16:50:33 +02:00
|
|
|
var result []spec.ServerName
|
2020-07-03 12:49:49 +02:00
|
|
|
for rows.Next() {
|
2023-04-19 16:50:33 +02:00
|
|
|
var serverName spec.ServerName
|
2020-07-03 12:49:49 +02:00
|
|
|
if err = rows.Scan(&serverName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, serverName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, rows.Err()
|
|
|
|
}
|