2020-07-20 17:55:20 +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"
|
|
|
|
|
|
|
|
"github.com/lib/pq"
|
2022-08-09 11:15:58 +02:00
|
|
|
|
|
|
|
"github.com/matrix-org/dendrite/federationapi/storage/postgres/deltas"
|
2020-07-20 17:55:20 +02:00
|
|
|
"github.com/matrix-org/dendrite/internal"
|
|
|
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
2023-04-19 16:50:33 +02:00
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
2020-07-20 17:55:20 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const queueEDUsSchema = `
|
|
|
|
CREATE TABLE IF NOT EXISTS federationsender_queue_edus (
|
|
|
|
-- The type of the event (informational).
|
|
|
|
edu_type TEXT NOT NULL,
|
|
|
|
-- The domain part of the user ID the EDU event is for.
|
|
|
|
server_name TEXT NOT NULL,
|
|
|
|
-- The JSON NID from the federationsender_queue_edus_json table.
|
2022-08-09 11:15:58 +02:00
|
|
|
json_nid BIGINT NOT NULL,
|
|
|
|
-- The expiry time of this edu, if any.
|
|
|
|
expires_at BIGINT NOT NULL DEFAULT 0
|
2020-07-20 17:55:20 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE UNIQUE INDEX IF NOT EXISTS federationsender_queue_edus_json_nid_idx
|
|
|
|
ON federationsender_queue_edus (json_nid, server_name);
|
2022-05-30 12:01:20 +02:00
|
|
|
CREATE INDEX IF NOT EXISTS federationsender_queue_edus_nid_idx
|
|
|
|
ON federationsender_queue_edus (json_nid);
|
|
|
|
CREATE INDEX IF NOT EXISTS federationsender_queue_edus_server_name_idx
|
|
|
|
ON federationsender_queue_edus (server_name);
|
2020-07-20 17:55:20 +02:00
|
|
|
`
|
|
|
|
|
|
|
|
const insertQueueEDUSQL = "" +
|
2022-08-09 11:15:58 +02:00
|
|
|
"INSERT INTO federationsender_queue_edus (edu_type, server_name, json_nid, expires_at)" +
|
|
|
|
" VALUES ($1, $2, $3, $4)"
|
2020-07-20 17:55:20 +02:00
|
|
|
|
|
|
|
const deleteQueueEDUSQL = "" +
|
|
|
|
"DELETE FROM federationsender_queue_edus WHERE server_name = $1 AND json_nid = ANY($2)"
|
|
|
|
|
|
|
|
const selectQueueEDUSQL = "" +
|
|
|
|
"SELECT json_nid FROM federationsender_queue_edus" +
|
|
|
|
" WHERE server_name = $1" +
|
|
|
|
" LIMIT $2"
|
|
|
|
|
|
|
|
const selectQueueEDUReferenceJSONCountSQL = "" +
|
|
|
|
"SELECT COUNT(*) FROM federationsender_queue_edus" +
|
|
|
|
" WHERE json_nid = $1"
|
|
|
|
|
|
|
|
const selectQueueServerNamesSQL = "" +
|
|
|
|
"SELECT DISTINCT server_name FROM federationsender_queue_edus"
|
|
|
|
|
2022-08-09 11:15:58 +02:00
|
|
|
const selectExpiredEDUsSQL = "" +
|
|
|
|
"SELECT DISTINCT json_nid FROM federationsender_queue_edus WHERE expires_at > 0 AND expires_at <= $1"
|
|
|
|
|
|
|
|
const deleteExpiredEDUsSQL = "" +
|
|
|
|
"DELETE FROM federationsender_queue_edus WHERE expires_at > 0 AND expires_at <= $1"
|
|
|
|
|
2020-07-20 17:55:20 +02:00
|
|
|
type queueEDUsStatements struct {
|
|
|
|
db *sql.DB
|
|
|
|
insertQueueEDUStmt *sql.Stmt
|
|
|
|
deleteQueueEDUStmt *sql.Stmt
|
|
|
|
selectQueueEDUStmt *sql.Stmt
|
|
|
|
selectQueueEDUReferenceJSONCountStmt *sql.Stmt
|
|
|
|
selectQueueEDUServerNamesStmt *sql.Stmt
|
2022-08-09 11:15:58 +02:00
|
|
|
selectExpiredEDUsStmt *sql.Stmt
|
|
|
|
deleteExpiredEDUsStmt *sql.Stmt
|
2020-07-20 17:55:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewPostgresQueueEDUsTable(db *sql.DB) (s *queueEDUsStatements, err error) {
|
|
|
|
s = &queueEDUsStatements{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
_, err = s.db.Exec(queueEDUsSchema)
|
|
|
|
if err != nil {
|
2022-08-09 11:15:58 +02:00
|
|
|
return s, err
|
2020-07-20 17:55:20 +02:00
|
|
|
}
|
2022-08-09 11:15:58 +02:00
|
|
|
|
|
|
|
m := sqlutil.NewMigrator(db)
|
|
|
|
m.AddMigrations(
|
|
|
|
sqlutil.Migration{
|
|
|
|
Version: "federationapi: add expiresat column",
|
|
|
|
Up: deltas.UpAddexpiresat,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err := m.Up(context.Background()); err != nil {
|
|
|
|
return s, err
|
2020-07-20 17:55:20 +02:00
|
|
|
}
|
2022-08-09 11:15:58 +02:00
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *queueEDUsStatements) Prepare() error {
|
|
|
|
return sqlutil.StatementList{
|
|
|
|
{&s.insertQueueEDUStmt, insertQueueEDUSQL},
|
|
|
|
{&s.deleteQueueEDUStmt, deleteQueueEDUSQL},
|
|
|
|
{&s.selectQueueEDUStmt, selectQueueEDUSQL},
|
|
|
|
{&s.selectQueueEDUReferenceJSONCountStmt, selectQueueEDUReferenceJSONCountSQL},
|
|
|
|
{&s.selectQueueEDUServerNamesStmt, selectQueueServerNamesSQL},
|
|
|
|
{&s.selectExpiredEDUsStmt, selectExpiredEDUsSQL},
|
|
|
|
{&s.deleteExpiredEDUsStmt, deleteExpiredEDUsSQL},
|
|
|
|
}.Prepare(s.db)
|
2020-07-20 17:55:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *queueEDUsStatements) InsertQueueEDU(
|
|
|
|
ctx context.Context,
|
|
|
|
txn *sql.Tx,
|
|
|
|
eduType string,
|
2023-04-19 16:50:33 +02:00
|
|
|
serverName spec.ServerName,
|
2020-07-20 17:55:20 +02:00
|
|
|
nid int64,
|
2023-04-19 16:50:33 +02:00
|
|
|
expiresAt spec.Timestamp,
|
2020-07-20 17:55:20 +02:00
|
|
|
) error {
|
|
|
|
stmt := sqlutil.TxStmt(txn, s.insertQueueEDUStmt)
|
|
|
|
_, err := stmt.ExecContext(
|
|
|
|
ctx,
|
|
|
|
eduType, // the EDU type
|
|
|
|
serverName, // destination server name
|
|
|
|
nid, // JSON blob NID
|
2022-08-09 11:15:58 +02:00
|
|
|
expiresAt, // timestamp of expiry
|
2020-07-20 17:55:20 +02:00
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *queueEDUsStatements) DeleteQueueEDUs(
|
|
|
|
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,
|
|
|
|
) error {
|
|
|
|
stmt := sqlutil.TxStmt(txn, s.deleteQueueEDUStmt)
|
|
|
|
_, err := stmt.ExecContext(ctx, serverName, pq.Int64Array(jsonNIDs))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *queueEDUsStatements) SelectQueueEDUs(
|
|
|
|
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
|
|
|
limit int,
|
|
|
|
) ([]int64, error) {
|
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectQueueEDUStmt)
|
|
|
|
rows, err := stmt.QueryContext(ctx, serverName, limit)
|
|
|
|
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)
|
|
|
|
}
|
2022-08-09 11:15:58 +02:00
|
|
|
return result, rows.Err()
|
2020-07-20 17:55:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *queueEDUsStatements) SelectQueueEDUReferenceJSONCount(
|
|
|
|
ctx context.Context, txn *sql.Tx, jsonNID int64,
|
|
|
|
) (int64, error) {
|
|
|
|
var count int64
|
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectQueueEDUReferenceJSONCountStmt)
|
|
|
|
err := stmt.QueryRowContext(ctx, jsonNID).Scan(&count)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *queueEDUsStatements) SelectQueueEDUServerNames(
|
|
|
|
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.selectQueueEDUServerNamesStmt)
|
|
|
|
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-20 17:55:20 +02:00
|
|
|
for rows.Next() {
|
2023-04-19 16:50:33 +02:00
|
|
|
var serverName spec.ServerName
|
2020-07-20 17:55:20 +02:00
|
|
|
if err = rows.Scan(&serverName); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, serverName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, rows.Err()
|
|
|
|
}
|
2022-08-09 11:15:58 +02:00
|
|
|
|
|
|
|
func (s *queueEDUsStatements) SelectExpiredEDUs(
|
|
|
|
ctx context.Context, txn *sql.Tx,
|
2023-04-19 16:50:33 +02:00
|
|
|
expiredBefore spec.Timestamp,
|
2022-08-09 11:15:58 +02:00
|
|
|
) ([]int64, error) {
|
|
|
|
stmt := sqlutil.TxStmt(txn, s.selectExpiredEDUsStmt)
|
|
|
|
rows, err := stmt.QueryContext(ctx, expiredBefore)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer internal.CloseAndLogIfError(ctx, rows, "SelectExpiredEDUs: rows.close() failed")
|
|
|
|
var result []int64
|
|
|
|
var nid int64
|
|
|
|
for rows.Next() {
|
|
|
|
if err = rows.Scan(&nid); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result = append(result, nid)
|
|
|
|
}
|
|
|
|
return result, rows.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *queueEDUsStatements) DeleteExpiredEDUs(
|
|
|
|
ctx context.Context, txn *sql.Tx,
|
2023-04-19 16:50:33 +02:00
|
|
|
expiredBefore spec.Timestamp,
|
2022-08-09 11:15:58 +02:00
|
|
|
) error {
|
|
|
|
stmt := sqlutil.TxStmt(txn, s.deleteExpiredEDUsStmt)
|
|
|
|
_, err := stmt.ExecContext(ctx, expiredBefore)
|
|
|
|
return err
|
|
|
|
}
|