go-waku/waku/v2/protocol/store/waku_store_client.go

422 lines
10 KiB
Go
Raw Normal View History

2022-11-25 20:54:11 +00:00
package store
import (
"context"
"encoding/hex"
"errors"
"math"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-msgio/pbio"
2022-11-25 20:54:11 +00:00
"go.uber.org/zap"
"github.com/waku-org/go-waku/logging"
"github.com/waku-org/go-waku/waku/v2/metrics"
"github.com/waku-org/go-waku/waku/v2/protocol"
wpb "github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/protocol/store/pb"
2022-11-25 20:54:11 +00:00
"github.com/waku-org/go-waku/waku/v2/utils"
)
type Query struct {
Topic string
ContentTopics []string
StartTime int64
EndTime int64
}
// Result represents a valid response from a store node
type Result struct {
2023-01-08 15:46:17 +00:00
started bool
Messages []*wpb.WakuMessage
2023-01-08 15:46:17 +00:00
store Store
query *pb.HistoryQuery
cursor *pb.Index
peerId peer.ID
2022-11-25 20:54:11 +00:00
}
func (r *Result) Cursor() *pb.Index {
return r.cursor
}
func (r *Result) IsComplete() bool {
return r.cursor == nil
}
func (r *Result) PeerID() peer.ID {
return r.peerId
}
func (r *Result) Query() *pb.HistoryQuery {
return r.query
}
2023-01-08 15:46:17 +00:00
func (r *Result) Next(ctx context.Context) (bool, error) {
if !r.started {
r.started = true
return len(r.Messages) != 0, nil
}
if r.IsComplete() {
return false, nil
}
newResult, err := r.store.Next(ctx, r)
if err != nil {
return false, err
}
r.cursor = newResult.cursor
r.Messages = newResult.Messages
return true, nil
}
func (r *Result) GetMessages() []*wpb.WakuMessage {
2023-01-08 15:46:17 +00:00
if !r.started {
return nil
}
return r.Messages
}
type criteriaFN = func(msg *wpb.WakuMessage) (bool, error)
2022-11-25 20:54:11 +00:00
type HistoryRequestParameters struct {
selectedPeer peer.ID
localQuery bool
requestID []byte
2022-11-25 20:54:11 +00:00
cursor *pb.Index
pageSize uint64
asc bool
s *WakuStore
}
type HistoryRequestOption func(*HistoryRequestParameters)
// WithPeer is an option used to specify the peerID to request the message history
func WithPeer(p peer.ID) HistoryRequestOption {
return func(params *HistoryRequestParameters) {
params.selectedPeer = p
}
}
// WithAutomaticPeerSelection is an option used to randomly select a peer from the peer store
// to request the message history. If a list of specific peers is passed, the peer will be chosen
// from that list assuming it supports the chosen protocol, otherwise it will chose a peer
// from the node peerstore
func WithAutomaticPeerSelection(fromThesePeers ...peer.ID) HistoryRequestOption {
return func(params *HistoryRequestParameters) {
2023-02-16 16:17:52 +00:00
p, err := utils.SelectPeer(params.s.h, StoreID_v20beta4, fromThesePeers, params.s.log)
2022-11-25 20:54:11 +00:00
if err == nil {
params.selectedPeer = p
2022-11-25 20:54:11 +00:00
} else {
params.s.log.Info("selecting peer", zap.Error(err))
}
}
}
// WithFastestPeerSelection is an option used to select a peer from the peer store
// with the lowest ping. If a list of specific peers is passed, the peer will be chosen
// from that list assuming it supports the chosen protocol, otherwise it will chose a peer
// from the node peerstore
func WithFastestPeerSelection(ctx context.Context, fromThesePeers ...peer.ID) HistoryRequestOption {
return func(params *HistoryRequestParameters) {
2023-02-16 16:17:52 +00:00
p, err := utils.SelectPeerWithLowestRTT(ctx, params.s.h, StoreID_v20beta4, fromThesePeers, params.s.log)
2022-11-25 20:54:11 +00:00
if err == nil {
params.selectedPeer = p
2022-11-25 20:54:11 +00:00
} else {
params.s.log.Info("selecting peer", zap.Error(err))
}
}
}
// WithRequestID is an option to set a specific request ID to be used when
// creating a store request
func WithRequestID(requestID []byte) HistoryRequestOption {
2022-11-25 20:54:11 +00:00
return func(params *HistoryRequestParameters) {
params.requestID = requestID
2022-11-25 20:54:11 +00:00
}
}
// WithAutomaticRequestID is an option to automatically generate a request ID
// when creating a store request
func WithAutomaticRequestID() HistoryRequestOption {
2022-11-25 20:54:11 +00:00
return func(params *HistoryRequestParameters) {
params.requestID = protocol.GenerateRequestId()
2022-11-25 20:54:11 +00:00
}
}
func WithCursor(c *pb.Index) HistoryRequestOption {
return func(params *HistoryRequestParameters) {
params.cursor = c
}
}
// WithPaging is an option used to specify the order and maximum number of records to return
func WithPaging(asc bool, pageSize uint64) HistoryRequestOption {
return func(params *HistoryRequestParameters) {
params.asc = asc
params.pageSize = pageSize
}
}
func WithLocalQuery() HistoryRequestOption {
return func(params *HistoryRequestParameters) {
params.localQuery = true
}
}
2022-11-25 20:54:11 +00:00
// Default options to be used when querying a store node for results
func DefaultOptions() []HistoryRequestOption {
return []HistoryRequestOption{
WithAutomaticRequestID(),
2022-11-25 20:54:11 +00:00
WithAutomaticPeerSelection(),
WithPaging(true, MaxPageSize),
}
}
func (store *WakuStore) queryFrom(ctx context.Context, q *pb.HistoryQuery, selectedPeer peer.ID, requestID []byte) (*pb.HistoryResponse, error) {
2022-11-25 20:54:11 +00:00
logger := store.log.With(logging.HostID("peer", selectedPeer))
logger.Info("querying message history")
connOpt, err := store.h.NewStream(ctx, selectedPeer, StoreID_v20beta4)
if err != nil {
logger.Error("creating stream to peer", zap.Error(err))
metrics.RecordStoreError(store.ctx, "dial_failure")
2022-11-25 20:54:11 +00:00
return nil, err
}
defer connOpt.Close()
defer func() {
_ = connOpt.Reset()
}()
historyRequest := &pb.HistoryRPC{Query: q, RequestId: hex.EncodeToString(requestID)}
2022-11-25 20:54:11 +00:00
writer := pbio.NewDelimitedWriter(connOpt)
reader := pbio.NewDelimitedReader(connOpt, math.MaxInt32)
2022-11-25 20:54:11 +00:00
err = writer.WriteMsg(historyRequest)
if err != nil {
logger.Error("writing request", zap.Error(err))
metrics.RecordStoreError(store.ctx, "write_request_failure")
2022-11-25 20:54:11 +00:00
return nil, err
}
historyResponseRPC := &pb.HistoryRPC{RequestId: historyRequest.RequestId}
2022-11-25 20:54:11 +00:00
err = reader.ReadMsg(historyResponseRPC)
if err != nil {
logger.Error("reading response", zap.Error(err))
metrics.RecordStoreError(store.ctx, "decode_rpc_failure")
2022-11-25 20:54:11 +00:00
return nil, err
}
if historyResponseRPC.Response == nil {
// Empty response
return &pb.HistoryResponse{
PagingInfo: &pb.PagingInfo{},
}, nil
2022-11-25 20:54:11 +00:00
}
return historyResponseRPC.Response, nil
}
func (store *WakuStore) localQuery(query *pb.HistoryQuery, requestID []byte) (*pb.HistoryResponse, error) {
logger := store.log
logger.Info("querying local message history")
if !store.started {
return nil, errors.New("not running local store")
}
historyResponseRPC := &pb.HistoryRPC{
RequestId: hex.EncodeToString(requestID),
Response: store.FindMessages(query),
}
if historyResponseRPC.Response == nil {
// Empty response
return &pb.HistoryResponse{
PagingInfo: &pb.PagingInfo{},
}, nil
}
return historyResponseRPC.Response, nil
}
2022-11-25 20:54:11 +00:00
func (store *WakuStore) Query(ctx context.Context, query Query, opts ...HistoryRequestOption) (*Result, error) {
q := &pb.HistoryQuery{
PubsubTopic: query.Topic,
ContentFilters: []*pb.ContentFilter{},
StartTime: query.StartTime,
EndTime: query.EndTime,
PagingInfo: &pb.PagingInfo{},
}
for _, cf := range query.ContentTopics {
q.ContentFilters = append(q.ContentFilters, &pb.ContentFilter{ContentTopic: cf})
}
if len(q.ContentFilters) > MaxContentFilters {
return nil, ErrMaxContentFilters
}
params := new(HistoryRequestParameters)
params.s = store
optList := DefaultOptions()
optList = append(optList, opts...)
for _, opt := range optList {
opt(params)
}
if !params.localQuery && params.selectedPeer == "" {
metrics.RecordStoreError(ctx, "peer_not_found_failure")
2022-11-25 20:54:11 +00:00
return nil, ErrNoPeersAvailable
}
if len(params.requestID) == 0 {
2022-11-25 20:54:11 +00:00
return nil, ErrInvalidId
}
if params.cursor != nil {
q.PagingInfo.Cursor = params.cursor
}
if params.asc {
q.PagingInfo.Direction = pb.PagingInfo_FORWARD
} else {
q.PagingInfo.Direction = pb.PagingInfo_BACKWARD
}
pageSize := params.pageSize
if pageSize == 0 || pageSize > uint64(MaxPageSize) {
pageSize = MaxPageSize
}
q.PagingInfo.PageSize = pageSize
var response *pb.HistoryResponse
var err error
if params.localQuery {
response, err = store.localQuery(q, params.requestID)
} else {
response, err = store.queryFrom(ctx, q, params.selectedPeer, params.requestID)
}
2022-11-25 20:54:11 +00:00
if err != nil {
return nil, err
}
if response.Error == pb.HistoryResponse_INVALID_CURSOR {
return nil, errors.New("invalid cursor")
}
result := &Result{
2023-01-08 15:46:17 +00:00
store: store,
2022-11-25 20:54:11 +00:00
Messages: response.Messages,
query: q,
peerId: params.selectedPeer,
}
2022-12-12 15:40:28 +00:00
if response.PagingInfo != nil {
result.cursor = response.PagingInfo.Cursor
}
2022-11-25 20:54:11 +00:00
return result, nil
}
// Find the first message that matches a criteria. criteriaCB is a function that will be invoked for each message and returns true if the message matches the criteria
func (store *WakuStore) Find(ctx context.Context, query Query, cb criteriaFN, opts ...HistoryRequestOption) (*wpb.WakuMessage, error) {
2022-11-25 20:54:11 +00:00
if cb == nil {
return nil, errors.New("callback can't be null")
}
result, err := store.Query(ctx, query, opts...)
if err != nil {
return nil, err
}
for {
for _, m := range result.Messages {
found, err := cb(m)
if err != nil {
return nil, err
}
if found {
return m, nil
}
}
if result.IsComplete() {
break
}
result, err = store.Next(ctx, result)
if err != nil {
return nil, err
}
}
return nil, nil
}
// Next is used with to retrieve the next page of rows from a query response.
// If no more records are found, the result will not contain any messages.
// This function is useful for iterating over results without having to manually
// specify the cursor and pagination order and max number of results
func (store *WakuStore) Next(ctx context.Context, r *Result) (*Result, error) {
if r.IsComplete() {
return &Result{
2023-01-08 15:46:17 +00:00
store: store,
started: true,
Messages: []*wpb.WakuMessage{},
2022-11-25 20:54:11 +00:00
cursor: nil,
query: r.query,
peerId: r.PeerID(),
}, nil
}
q := &pb.HistoryQuery{
PubsubTopic: r.Query().PubsubTopic,
ContentFilters: r.Query().ContentFilters,
StartTime: r.Query().StartTime,
EndTime: r.Query().EndTime,
PagingInfo: &pb.PagingInfo{
PageSize: r.Query().PagingInfo.PageSize,
Direction: r.Query().PagingInfo.Direction,
Cursor: &pb.Index{
Digest: r.Cursor().Digest,
ReceiverTime: r.Cursor().ReceiverTime,
SenderTime: r.Cursor().SenderTime,
PubsubTopic: r.Cursor().PubsubTopic,
},
},
}
response, err := store.queryFrom(ctx, q, r.PeerID(), protocol.GenerateRequestId())
if err != nil {
return nil, err
}
if response.Error == pb.HistoryResponse_INVALID_CURSOR {
return nil, errors.New("invalid cursor")
}
2022-12-20 16:37:16 +00:00
result := &Result{
2023-01-08 15:46:17 +00:00
started: true,
store: store,
2022-11-25 20:54:11 +00:00
Messages: response.Messages,
query: q,
peerId: r.PeerID(),
2022-12-20 16:37:16 +00:00
}
if response.PagingInfo != nil {
result.cursor = response.PagingInfo.Cursor
}
return result, nil
2022-11-25 20:54:11 +00:00
}