2022-08-19 16:34:07 +00:00
package persistence
import (
2023-03-27 13:51:55 +00:00
"context"
2022-08-19 16:34:07 +00:00
"database/sql"
"errors"
"fmt"
"strings"
"sync"
"time"
2022-11-22 22:05:54 +00:00
gowakuPersistence "github.com/waku-org/go-waku/waku/persistence"
"github.com/waku-org/go-waku/waku/v2/protocol"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
2023-02-22 21:58:17 +00:00
storepb "github.com/waku-org/go-waku/waku/v2/protocol/store/pb"
2022-12-09 18:06:36 +00:00
"github.com/waku-org/go-waku/waku/v2/timesource"
2022-11-22 22:05:54 +00:00
"github.com/waku-org/go-waku/waku/v2/utils"
2022-08-19 16:34:07 +00:00
"go.uber.org/zap"
)
var ErrInvalidCursor = errors . New ( "invalid cursor" )
2023-05-08 21:43:27 +00:00
var ErrFutureMessage = errors . New ( "message timestamp in the future" )
var ErrMessageTooOld = errors . New ( "message too old" )
// MaxTimeVariance is the maximum duration in the future allowed for a message timestamp
const MaxTimeVariance = time . Duration ( 20 ) * time . Second
2022-08-19 16:34:07 +00:00
// DBStore is a MessageProvider that has a *sql.DB connection
type DBStore struct {
db * sql . DB
log * zap . Logger
maxMessages int
maxDuration time . Duration
2023-03-27 13:51:55 +00:00
wg sync . WaitGroup
cancel context . CancelFunc
2022-08-19 16:34:07 +00:00
}
// DBOption is an optional setting that can be used to configure the DBStore
type DBOption func ( * DBStore ) error
// WithDB is a DBOption that lets you use any custom *sql.DB with a DBStore.
func WithDB ( db * sql . DB ) DBOption {
return func ( d * DBStore ) error {
d . db = db
return nil
}
}
// WithRetentionPolicy is a DBOption that specifies the max number of messages
// to be stored and duration before they're removed from the message store
func WithRetentionPolicy ( maxMessages int , maxDuration time . Duration ) DBOption {
return func ( d * DBStore ) error {
d . maxDuration = maxDuration
d . maxMessages = maxMessages
return nil
}
}
// Creates a new DB store using the db specified via options.
// It will create a messages table if it does not exist and
// clean up records according to the retention policy used
func NewDBStore ( log * zap . Logger , options ... DBOption ) ( * DBStore , error ) {
result := new ( DBStore )
result . log = log . Named ( "dbstore" )
for _ , opt := range options {
err := opt ( result )
if err != nil {
return nil , err
}
}
2022-12-09 18:06:36 +00:00
return result , nil
}
2023-03-27 13:51:55 +00:00
func ( d * DBStore ) Start ( ctx context . Context , timesource timesource . Timesource ) error {
ctx , cancel := context . WithCancel ( ctx )
d . cancel = cancel
2022-12-09 18:06:36 +00:00
err := d . cleanOlderRecords ( )
2022-08-19 16:34:07 +00:00
if err != nil {
2022-12-09 18:06:36 +00:00
return err
2022-08-19 16:34:07 +00:00
}
2022-12-09 18:06:36 +00:00
d . wg . Add ( 1 )
2023-03-27 13:51:55 +00:00
go d . checkForOlderRecords ( ctx , 60 * time . Second )
2022-08-19 16:34:07 +00:00
2022-12-09 18:06:36 +00:00
return nil
2022-08-19 16:34:07 +00:00
}
2023-05-08 21:43:27 +00:00
func ( d * DBStore ) Validate ( env * protocol . Envelope ) error {
n := time . Unix ( 0 , env . Index ( ) . ReceiverTime )
upperBound := n . Add ( MaxTimeVariance )
lowerBound := n . Add ( - MaxTimeVariance )
// Ensure that messages don't "jump" to the front of the queue with future timestamps
2023-12-05 04:29:27 +00:00
if env . Message ( ) . GetTimestamp ( ) > upperBound . UnixNano ( ) {
2023-05-08 21:43:27 +00:00
return ErrFutureMessage
}
2023-12-05 04:29:27 +00:00
if env . Message ( ) . GetTimestamp ( ) < lowerBound . UnixNano ( ) {
2023-05-08 21:43:27 +00:00
return ErrMessageTooOld
}
return nil
}
2022-08-19 16:34:07 +00:00
func ( d * DBStore ) cleanOlderRecords ( ) error {
d . log . Debug ( "Cleaning older records..." )
// Delete older messages
if d . maxDuration > 0 {
start := time . Now ( )
sqlStmt := ` DELETE FROM store_messages WHERE receiverTimestamp < ? `
_ , err := d . db . Exec ( sqlStmt , utils . GetUnixEpochFrom ( time . Now ( ) . Add ( - d . maxDuration ) ) )
if err != nil {
return err
}
elapsed := time . Since ( start )
d . log . Debug ( "deleting older records from the DB" , zap . Duration ( "duration" , elapsed ) )
}
// Limit number of records to a max N
if d . maxMessages > 0 {
start := time . Now ( )
sqlStmt := ` DELETE FROM store_messages WHERE id IN (SELECT id FROM store_messages ORDER BY receiverTimestamp DESC LIMIT -1 OFFSET ?) `
_ , err := d . db . Exec ( sqlStmt , d . maxMessages )
if err != nil {
return err
}
elapsed := time . Since ( start )
d . log . Debug ( "deleting excess records from the DB" , zap . Duration ( "duration" , elapsed ) )
}
return nil
}
2023-03-27 13:51:55 +00:00
func ( d * DBStore ) checkForOlderRecords ( ctx context . Context , t time . Duration ) {
2022-08-19 16:34:07 +00:00
defer d . wg . Done ( )
ticker := time . NewTicker ( t )
defer ticker . Stop ( )
for {
select {
2023-03-27 13:51:55 +00:00
case <- ctx . Done ( ) :
2022-08-19 16:34:07 +00:00
return
case <- ticker . C :
err := d . cleanOlderRecords ( )
if err != nil {
d . log . Error ( "cleaning older records" , zap . Error ( err ) )
}
}
}
}
// Stop closes a DB connection
func ( d * DBStore ) Stop ( ) {
2023-03-27 13:51:55 +00:00
if d . cancel == nil {
return
}
d . cancel ( )
2022-08-19 16:34:07 +00:00
d . wg . Wait ( )
d . db . Close ( )
}
// Put inserts a WakuMessage into the DB
func ( d * DBStore ) Put ( env * protocol . Envelope ) error {
stmt , err := d . db . Prepare ( "INSERT INTO store_messages (id, receiverTimestamp, senderTimestamp, contentTopic, pubsubTopic, payload, version) VALUES (?, ?, ?, ?, ?, ?, ?)" )
if err != nil {
return err
}
cursor := env . Index ( )
2024-01-05 20:43:05 +00:00
dbKey := NewDBKey ( uint64 ( cursor . SenderTime ) , uint64 ( env . Index ( ) . ReceiverTime ) , env . PubsubTopic ( ) , env . Index ( ) . Digest )
2022-08-19 16:34:07 +00:00
_ , err = stmt . Exec ( dbKey . Bytes ( ) , cursor . ReceiverTime , env . Message ( ) . Timestamp , env . Message ( ) . ContentTopic , env . PubsubTopic ( ) , env . Message ( ) . Payload , env . Message ( ) . Version )
if err != nil {
return err
}
err = stmt . Close ( )
if err != nil {
return err
}
return nil
}
// Query retrieves messages from the DB
2023-02-22 21:58:17 +00:00
func ( d * DBStore ) Query ( query * storepb . HistoryQuery ) ( * storepb . Index , [ ] gowakuPersistence . StoredMessage , error ) {
2022-08-19 16:34:07 +00:00
start := time . Now ( )
defer func ( ) {
elapsed := time . Since ( start )
d . log . Info ( fmt . Sprintf ( "Loading records from the DB took %s" , elapsed ) )
} ( )
sqlQuery := ` SELECT id , receiverTimestamp , senderTimestamp , contentTopic , pubsubTopic , payload , version
FROM store_messages
% s
2023-01-07 15:25:55 +00:00
ORDER BY senderTimestamp % s , id % s , pubsubTopic % s , receiverTimestamp % s `
2022-08-19 16:34:07 +00:00
var conditions [ ] string
var parameters [ ] interface { }
2023-01-07 15:25:55 +00:00
paramCnt := 0
2022-08-19 16:34:07 +00:00
if query . PubsubTopic != "" {
2023-01-07 15:25:55 +00:00
paramCnt ++
conditions = append ( conditions , fmt . Sprintf ( "pubsubTopic = $%d" , paramCnt ) )
2022-08-19 16:34:07 +00:00
parameters = append ( parameters , query . PubsubTopic )
}
if len ( query . ContentFilters ) != 0 {
var ctPlaceHolder [ ] string
for _ , ct := range query . ContentFilters {
if ct . ContentTopic != "" {
2023-01-07 15:25:55 +00:00
paramCnt ++
ctPlaceHolder = append ( ctPlaceHolder , fmt . Sprintf ( "$%d" , paramCnt ) )
2022-08-19 16:34:07 +00:00
parameters = append ( parameters , ct . ContentTopic )
}
}
conditions = append ( conditions , "contentTopic IN (" + strings . Join ( ctPlaceHolder , ", " ) + ")" )
}
2023-01-07 15:25:55 +00:00
usesCursor := false
2022-08-19 16:34:07 +00:00
if query . PagingInfo . Cursor != nil {
2023-01-07 15:25:55 +00:00
usesCursor = true
2022-08-19 16:34:07 +00:00
var exists bool
2024-01-05 20:43:05 +00:00
cursorDBKey := NewDBKey ( uint64 ( query . PagingInfo . Cursor . SenderTime ) , uint64 ( query . PagingInfo . Cursor . ReceiverTime ) , query . PagingInfo . Cursor . PubsubTopic , query . PagingInfo . Cursor . Digest )
2022-08-19 16:34:07 +00:00
2023-01-07 15:25:55 +00:00
err := d . db . QueryRow ( "SELECT EXISTS(SELECT 1 FROM store_messages WHERE id = $1)" ,
2022-08-19 16:34:07 +00:00
cursorDBKey . Bytes ( ) ,
) . Scan ( & exists )
if err != nil {
2022-09-16 13:09:13 +00:00
return nil , nil , err
2022-08-19 16:34:07 +00:00
}
if exists {
eqOp := ">"
2023-02-22 21:58:17 +00:00
if query . PagingInfo . Direction == storepb . PagingInfo_BACKWARD {
2022-08-19 16:34:07 +00:00
eqOp = "<"
}
2023-01-07 15:25:55 +00:00
paramCnt ++
conditions = append ( conditions , fmt . Sprintf ( "id %s $%d" , eqOp , paramCnt ) )
2022-08-19 16:34:07 +00:00
parameters = append ( parameters , cursorDBKey . Bytes ( ) )
} else {
2022-09-16 13:09:13 +00:00
return nil , nil , ErrInvalidCursor
2022-08-19 16:34:07 +00:00
}
}
2023-12-05 04:29:27 +00:00
if query . GetStartTime ( ) != 0 {
2023-02-22 21:58:17 +00:00
if ! usesCursor || query . PagingInfo . Direction == storepb . PagingInfo_BACKWARD {
2023-01-07 15:25:55 +00:00
paramCnt ++
conditions = append ( conditions , fmt . Sprintf ( "id >= $%d" , paramCnt ) )
2024-01-05 20:43:05 +00:00
startTimeDBKey := NewDBKey ( uint64 ( query . GetStartTime ( ) ) , uint64 ( query . GetStartTime ( ) ) , "" , [ ] byte { } )
2023-01-07 15:25:55 +00:00
parameters = append ( parameters , startTimeDBKey . Bytes ( ) )
}
}
2023-12-05 04:29:27 +00:00
if query . GetEndTime ( ) != 0 {
2023-02-22 21:58:17 +00:00
if ! usesCursor || query . PagingInfo . Direction == storepb . PagingInfo_FORWARD {
2023-01-07 15:25:55 +00:00
paramCnt ++
conditions = append ( conditions , fmt . Sprintf ( "id <= $%d" , paramCnt ) )
2024-01-05 20:43:05 +00:00
endTimeDBKey := NewDBKey ( uint64 ( query . GetEndTime ( ) ) , uint64 ( query . GetEndTime ( ) ) , "" , [ ] byte { } )
2023-01-07 15:25:55 +00:00
parameters = append ( parameters , endTimeDBKey . Bytes ( ) )
}
}
2022-08-19 16:34:07 +00:00
conditionStr := ""
if len ( conditions ) != 0 {
conditionStr = "WHERE " + strings . Join ( conditions , " AND " )
}
orderDirection := "ASC"
2023-02-22 21:58:17 +00:00
if query . PagingInfo . Direction == storepb . PagingInfo_BACKWARD {
2022-08-19 16:34:07 +00:00
orderDirection = "DESC"
}
2023-01-07 15:25:55 +00:00
paramCnt ++
sqlQuery += fmt . Sprintf ( "LIMIT $%d" , paramCnt )
sqlQuery = fmt . Sprintf ( sqlQuery , conditionStr , orderDirection , orderDirection , orderDirection , orderDirection )
2022-08-19 16:34:07 +00:00
stmt , err := d . db . Prepare ( sqlQuery )
if err != nil {
2022-09-16 13:09:13 +00:00
return nil , nil , err
2022-08-19 16:34:07 +00:00
}
defer stmt . Close ( )
2023-01-07 15:25:55 +00:00
pageSize := query . PagingInfo . PageSize + 1
parameters = append ( parameters , pageSize )
2022-08-19 16:34:07 +00:00
rows , err := stmt . Query ( parameters ... )
if err != nil {
2022-09-16 13:09:13 +00:00
return nil , nil , err
2022-08-19 16:34:07 +00:00
}
var result [ ] gowakuPersistence . StoredMessage
for rows . Next ( ) {
record , err := d . GetStoredMessage ( rows )
if err != nil {
2022-09-16 13:09:13 +00:00
return nil , nil , err
2022-08-19 16:34:07 +00:00
}
result = append ( result , record )
}
defer rows . Close ( )
2023-02-22 21:58:17 +00:00
var cursor * storepb . Index
2022-09-16 13:09:13 +00:00
if len ( result ) != 0 {
2023-01-07 15:25:55 +00:00
if len ( result ) > int ( query . PagingInfo . PageSize ) {
result = result [ 0 : query . PagingInfo . PageSize ]
lastMsgIdx := len ( result ) - 1
cursor = protocol . NewEnvelope ( result [ lastMsgIdx ] . Message , result [ lastMsgIdx ] . ReceiverTime , result [ lastMsgIdx ] . PubsubTopic ) . Index ( )
}
2022-09-16 13:09:13 +00:00
}
// The retrieved messages list should always be in chronological order
2023-02-22 21:58:17 +00:00
if query . PagingInfo . Direction == storepb . PagingInfo_BACKWARD {
2022-09-16 13:09:13 +00:00
for i , j := 0 , len ( result ) - 1 ; i < j ; i , j = i + 1 , j - 1 {
result [ i ] , result [ j ] = result [ j ] , result [ i ]
}
}
return cursor , result , nil
2022-08-19 16:34:07 +00:00
}
// MostRecentTimestamp returns an unix timestamp with the most recent senderTimestamp
// in the message table
func ( d * DBStore ) MostRecentTimestamp ( ) ( int64 , error ) {
result := sql . NullInt64 { }
err := d . db . QueryRow ( ` SELECT max(senderTimestamp) FROM store_messages ` ) . Scan ( & result )
if err != nil && err != sql . ErrNoRows {
return 0 , err
}
return result . Int64 , nil
}
// Count returns the number of rows in the message table
func ( d * DBStore ) Count ( ) ( int , error ) {
var result int
err := d . db . QueryRow ( ` SELECT COUNT(*) FROM store_messages ` ) . Scan ( & result )
if err != nil && err != sql . ErrNoRows {
return 0 , err
}
return result , nil
}
// GetAll returns all the stored WakuMessages
func ( d * DBStore ) GetAll ( ) ( [ ] gowakuPersistence . StoredMessage , error ) {
start := time . Now ( )
defer func ( ) {
elapsed := time . Since ( start )
d . log . Info ( "loading records from the DB" , zap . Duration ( "duration" , elapsed ) )
} ( )
rows , err := d . db . Query ( "SELECT id, receiverTimestamp, senderTimestamp, contentTopic, pubsubTopic, payload, version FROM store_messages ORDER BY senderTimestamp ASC" )
if err != nil {
return nil , err
}
var result [ ] gowakuPersistence . StoredMessage
defer rows . Close ( )
for rows . Next ( ) {
record , err := d . GetStoredMessage ( rows )
if err != nil {
return nil , err
}
result = append ( result , record )
}
d . log . Info ( "DB returned records" , zap . Int ( "count" , len ( result ) ) )
err = rows . Err ( )
if err != nil {
return nil , err
}
return result , nil
}
// GetStoredMessage is a helper function used to convert a `*sql.Rows` into a `StoredMessage`
func ( d * DBStore ) GetStoredMessage ( row * sql . Rows ) ( gowakuPersistence . StoredMessage , error ) {
var id [ ] byte
var receiverTimestamp int64
var senderTimestamp int64
var contentTopic string
var payload [ ] byte
var version uint32
var pubsubTopic string
err := row . Scan ( & id , & receiverTimestamp , & senderTimestamp , & contentTopic , & pubsubTopic , & payload , & version )
if err != nil {
d . log . Error ( "scanning messages from db" , zap . Error ( err ) )
return gowakuPersistence . StoredMessage { } , err
}
msg := new ( pb . WakuMessage )
msg . ContentTopic = contentTopic
msg . Payload = payload
2023-12-05 04:29:27 +00:00
msg . Timestamp = & senderTimestamp
msg . Version = & version
2022-08-19 16:34:07 +00:00
record := gowakuPersistence . StoredMessage {
ID : id ,
PubsubTopic : pubsubTopic ,
ReceiverTime : receiverTimestamp ,
Message : msg ,
}
return record , nil
}