2020-04-06 04:42:21 +00:00
|
|
|
package eventstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-01-06 07:29:58 +00:00
|
|
|
"database/sql"
|
2021-02-23 14:13:04 +00:00
|
|
|
"encoding/json"
|
|
|
|
"reflect"
|
2023-01-16 11:30:03 +00:00
|
|
|
"sort"
|
2021-02-23 14:13:04 +00:00
|
|
|
"sync"
|
2022-12-15 09:40:13 +00:00
|
|
|
"time"
|
2020-04-06 04:42:21 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/repository"
|
2020-04-06 04:42:21 +00:00
|
|
|
)
|
|
|
|
|
2022-10-26 13:06:48 +00:00
|
|
|
// Eventstore abstracts all functions needed to store valid events
|
2021-02-23 14:13:04 +00:00
|
|
|
// and filters the stored events
|
|
|
|
type Eventstore struct {
|
|
|
|
repo repository.Repository
|
|
|
|
interceptorMutex sync.Mutex
|
|
|
|
eventInterceptors map[EventType]eventTypeInterceptors
|
2023-01-16 11:30:03 +00:00
|
|
|
eventTypes []string
|
|
|
|
aggregateTypes []string
|
2022-12-15 09:40:13 +00:00
|
|
|
PushTimeout time.Duration
|
2020-04-06 04:42:21 +00:00
|
|
|
}
|
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
type eventTypeInterceptors struct {
|
2022-01-03 08:19:07 +00:00
|
|
|
eventMapper func(*repository.Event) (Event, error)
|
2021-02-23 14:13:04 +00:00
|
|
|
}
|
2020-04-06 04:42:21 +00:00
|
|
|
|
2022-12-15 09:40:13 +00:00
|
|
|
func NewEventstore(config *Config) *Eventstore {
|
2021-02-23 14:13:04 +00:00
|
|
|
return &Eventstore{
|
2022-12-15 09:40:13 +00:00
|
|
|
repo: config.repo,
|
2021-02-23 14:13:04 +00:00
|
|
|
eventInterceptors: map[EventType]eventTypeInterceptors{},
|
|
|
|
interceptorMutex: sync.Mutex{},
|
2022-12-15 09:40:13 +00:00
|
|
|
PushTimeout: config.PushTimeout,
|
2021-02-23 14:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-12 21:50:01 +00:00
|
|
|
|
2022-10-26 13:06:48 +00:00
|
|
|
// Health checks if the eventstore can properly work
|
2021-02-23 14:13:04 +00:00
|
|
|
// It checks if the repository can serve load
|
|
|
|
func (es *Eventstore) Health(ctx context.Context) error {
|
|
|
|
return es.repo.Health(ctx)
|
2020-04-06 04:42:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 13:06:48 +00:00
|
|
|
// Push pushes the events in a single transaction
|
2021-02-23 14:13:04 +00:00
|
|
|
// an event needs at least an aggregate
|
2022-01-03 08:19:07 +00:00
|
|
|
func (es *Eventstore) Push(ctx context.Context, cmds ...Command) ([]Event, error) {
|
2022-03-29 09:53:19 +00:00
|
|
|
events, constraints, err := commandsToRepository(authz.GetInstance(ctx).InstanceID(), cmds)
|
2021-02-23 14:13:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-15 09:40:13 +00:00
|
|
|
|
|
|
|
if es.PushTimeout > 0 {
|
|
|
|
var cancel func()
|
|
|
|
ctx, cancel = context.WithTimeout(ctx, es.PushTimeout)
|
|
|
|
defer cancel()
|
|
|
|
}
|
|
|
|
|
2021-05-03 08:15:50 +00:00
|
|
|
err = es.repo.Push(ctx, events, constraints...)
|
2021-02-23 14:13:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-24 12:24:33 +00:00
|
|
|
|
|
|
|
eventReaders, err := es.mapEvents(events)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
go notify(eventReaders)
|
|
|
|
return eventReaders, nil
|
2021-02-23 14:13:04 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 05:42:48 +00:00
|
|
|
func (es *Eventstore) NewInstance(ctx context.Context, instanceID string) error {
|
|
|
|
return es.repo.CreateInstance(ctx, instanceID)
|
|
|
|
}
|
|
|
|
|
2023-01-16 11:30:03 +00:00
|
|
|
func (es *Eventstore) EventTypes() []string {
|
|
|
|
return es.eventTypes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *Eventstore) AggregateTypes() []string {
|
|
|
|
return es.aggregateTypes
|
|
|
|
}
|
|
|
|
|
2022-03-23 08:02:39 +00:00
|
|
|
func commandsToRepository(instanceID string, cmds []Command) (events []*repository.Event, constraints []*repository.UniqueConstraint, err error) {
|
2022-01-03 08:19:07 +00:00
|
|
|
events = make([]*repository.Event, len(cmds))
|
|
|
|
for i, cmd := range cmds {
|
|
|
|
data, err := EventData(cmd)
|
2021-02-23 14:13:04 +00:00
|
|
|
if err != nil {
|
2021-05-03 08:15:50 +00:00
|
|
|
return nil, nil, err
|
2021-02-23 14:13:04 +00:00
|
|
|
}
|
2022-01-03 08:19:07 +00:00
|
|
|
if cmd.Aggregate().ID == "" {
|
2021-12-17 10:11:57 +00:00
|
|
|
return nil, nil, errors.ThrowInvalidArgument(nil, "V2-Afdfe", "aggregate id must not be empty")
|
|
|
|
}
|
2022-01-03 08:19:07 +00:00
|
|
|
if cmd.Aggregate().Type == "" {
|
2021-12-17 10:11:57 +00:00
|
|
|
return nil, nil, errors.ThrowInvalidArgument(nil, "V2-Dfg32", "aggregate type must not be empty")
|
|
|
|
}
|
2022-01-03 08:19:07 +00:00
|
|
|
if cmd.Type() == "" {
|
2021-12-17 10:11:57 +00:00
|
|
|
return nil, nil, errors.ThrowInvalidArgument(nil, "V2-Drg34", "event type must not be empty")
|
|
|
|
}
|
2022-01-03 08:19:07 +00:00
|
|
|
if cmd.Aggregate().Version == "" {
|
2021-12-17 10:11:57 +00:00
|
|
|
return nil, nil, errors.ThrowInvalidArgument(nil, "V2-Dgfg4", "aggregate version must not be empty")
|
|
|
|
}
|
2021-02-23 14:13:04 +00:00
|
|
|
events[i] = &repository.Event{
|
2022-01-03 08:19:07 +00:00
|
|
|
AggregateID: cmd.Aggregate().ID,
|
|
|
|
AggregateType: repository.AggregateType(cmd.Aggregate().Type),
|
2022-01-06 07:29:58 +00:00
|
|
|
ResourceOwner: sql.NullString{String: cmd.Aggregate().ResourceOwner, Valid: cmd.Aggregate().ResourceOwner != ""},
|
2022-04-12 14:20:17 +00:00
|
|
|
InstanceID: instanceID,
|
2022-01-03 08:19:07 +00:00
|
|
|
EditorService: cmd.EditorService(),
|
|
|
|
EditorUser: cmd.EditorUser(),
|
|
|
|
Type: repository.EventType(cmd.Type()),
|
|
|
|
Version: repository.Version(cmd.Aggregate().Version),
|
2021-02-23 14:13:04 +00:00
|
|
|
Data: data,
|
|
|
|
}
|
2022-01-03 08:19:07 +00:00
|
|
|
if len(cmd.UniqueConstraints()) > 0 {
|
2022-03-28 08:05:09 +00:00
|
|
|
constraints = append(constraints, uniqueConstraintsToRepository(instanceID, cmd.UniqueConstraints())...)
|
2021-02-23 14:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 08:15:50 +00:00
|
|
|
return events, constraints, nil
|
2021-02-23 14:13:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
func uniqueConstraintsToRepository(instanceID string, constraints []*EventUniqueConstraint) (uniqueConstraints []*repository.UniqueConstraint) {
|
2021-02-23 14:13:04 +00:00
|
|
|
uniqueConstraints = make([]*repository.UniqueConstraint, len(constraints))
|
|
|
|
for i, constraint := range constraints {
|
2022-05-16 09:52:54 +00:00
|
|
|
var id string
|
|
|
|
if !constraint.IsGlobal {
|
|
|
|
id = instanceID
|
|
|
|
}
|
2021-02-23 14:13:04 +00:00
|
|
|
uniqueConstraints[i] = &repository.UniqueConstraint{
|
|
|
|
UniqueType: constraint.UniqueType,
|
|
|
|
UniqueField: constraint.UniqueField,
|
2022-05-16 09:52:54 +00:00
|
|
|
InstanceID: id,
|
2021-02-23 14:13:04 +00:00
|
|
|
Action: uniqueConstraintActionToRepository(constraint.Action),
|
|
|
|
ErrorMessage: constraint.ErrorMessage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return uniqueConstraints
|
|
|
|
}
|
|
|
|
|
2022-10-26 13:06:48 +00:00
|
|
|
// Filter filters the stored events based on the searchQuery
|
2021-02-23 14:13:04 +00:00
|
|
|
// and maps the events to the defined event structs
|
2022-01-03 08:19:07 +00:00
|
|
|
func (es *Eventstore) Filter(ctx context.Context, queryFactory *SearchQueryBuilder) ([]Event, error) {
|
2022-03-29 09:53:19 +00:00
|
|
|
query, err := queryFactory.build(authz.GetInstance(ctx).InstanceID())
|
2021-02-23 14:13:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
events, err := es.repo.Filter(ctx, query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return es.mapEvents(events)
|
2020-04-06 04:42:21 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func (es *Eventstore) mapEvents(events []*repository.Event) (mappedEvents []Event, err error) {
|
|
|
|
mappedEvents = make([]Event, len(events))
|
2021-02-23 14:13:04 +00:00
|
|
|
|
|
|
|
es.interceptorMutex.Lock()
|
|
|
|
defer es.interceptorMutex.Unlock()
|
|
|
|
|
|
|
|
for i, event := range events {
|
|
|
|
interceptors, ok := es.eventInterceptors[EventType(event.Type)]
|
|
|
|
if !ok || interceptors.eventMapper == nil {
|
|
|
|
mappedEvents[i] = BaseEventFromRepo(event)
|
|
|
|
//TODO: return error if unable to map event
|
|
|
|
continue
|
|
|
|
// return nil, errors.ThrowPreconditionFailed(nil, "V2-usujB", "event mapper not defined")
|
2020-04-06 04:42:21 +00:00
|
|
|
}
|
2021-02-23 14:13:04 +00:00
|
|
|
mappedEvents[i], err = interceptors.eventMapper(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-04-06 04:42:21 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-23 14:13:04 +00:00
|
|
|
|
|
|
|
return mappedEvents, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type reducer interface {
|
|
|
|
//Reduce handles the events of the internal events list
|
|
|
|
// it only appends the newly added events
|
|
|
|
Reduce() error
|
|
|
|
//AppendEvents appends the passed events to an internal list of events
|
2022-01-03 08:19:07 +00:00
|
|
|
AppendEvents(...Event)
|
2021-02-23 14:13:04 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 13:06:48 +00:00
|
|
|
// FilterToReducer filters the events based on the search query, appends all events to the reducer and calls it's reduce function
|
2021-02-23 14:13:04 +00:00
|
|
|
func (es *Eventstore) FilterToReducer(ctx context.Context, searchQuery *SearchQueryBuilder, r reducer) error {
|
2022-01-03 08:19:07 +00:00
|
|
|
events, err := es.Filter(ctx, searchQuery)
|
2020-04-06 04:42:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
r.AppendEvents(events...)
|
|
|
|
|
|
|
|
return r.Reduce()
|
2020-04-06 04:42:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 13:06:48 +00:00
|
|
|
// LatestSequence filters the latest sequence for the given search query
|
2021-02-23 14:13:04 +00:00
|
|
|
func (es *Eventstore) LatestSequence(ctx context.Context, queryFactory *SearchQueryBuilder) (uint64, error) {
|
2022-03-29 09:53:19 +00:00
|
|
|
query, err := queryFactory.build(authz.GetInstance(ctx).InstanceID())
|
2021-02-23 14:13:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2020-04-06 04:42:21 +00:00
|
|
|
}
|
2021-02-23 14:13:04 +00:00
|
|
|
return es.repo.LatestSequence(ctx, query)
|
2020-07-28 07:42:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 13:06:48 +00:00
|
|
|
// InstanceIDs returns the instance ids found by the search query
|
2022-07-22 10:08:39 +00:00
|
|
|
func (es *Eventstore) InstanceIDs(ctx context.Context, queryFactory *SearchQueryBuilder) ([]string, error) {
|
|
|
|
query, err := queryFactory.build(authz.GetInstance(ctx).InstanceID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return es.repo.InstanceIDs(ctx, query)
|
|
|
|
}
|
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
type QueryReducer interface {
|
2021-02-23 14:13:04 +00:00
|
|
|
reducer
|
|
|
|
//Query returns the SearchQueryFactory for the events needed in reducer
|
|
|
|
Query() *SearchQueryBuilder
|
2020-04-06 04:42:21 +00:00
|
|
|
}
|
2020-04-07 11:23:04 +00:00
|
|
|
|
2022-10-26 13:06:48 +00:00
|
|
|
// FilterToQueryReducer filters the events based on the search query of the query function,
|
2021-02-23 14:13:04 +00:00
|
|
|
// appends all events to the reducer and calls it's reduce function
|
2022-04-12 14:20:17 +00:00
|
|
|
func (es *Eventstore) FilterToQueryReducer(ctx context.Context, r QueryReducer) error {
|
2022-01-03 08:19:07 +00:00
|
|
|
events, err := es.Filter(ctx, r.Query())
|
2021-02-23 14:13:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
r.AppendEvents(events...)
|
|
|
|
|
|
|
|
return r.Reduce()
|
|
|
|
}
|
|
|
|
|
2022-10-26 13:06:48 +00:00
|
|
|
// RegisterFilterEventMapper registers a function for mapping an eventstore event to an event
|
2023-01-16 11:30:03 +00:00
|
|
|
func (es *Eventstore) RegisterFilterEventMapper(aggregateType AggregateType, eventType EventType, mapper func(*repository.Event) (Event, error)) *Eventstore {
|
2021-02-23 14:13:04 +00:00
|
|
|
if mapper == nil || eventType == "" {
|
|
|
|
return es
|
|
|
|
}
|
|
|
|
es.interceptorMutex.Lock()
|
|
|
|
defer es.interceptorMutex.Unlock()
|
|
|
|
|
2023-01-16 11:30:03 +00:00
|
|
|
es.appendEventType(eventType)
|
|
|
|
es.appendAggregateType(aggregateType)
|
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
interceptor := es.eventInterceptors[eventType]
|
|
|
|
interceptor.eventMapper = mapper
|
|
|
|
es.eventInterceptors[eventType] = interceptor
|
|
|
|
|
|
|
|
return es
|
|
|
|
}
|
|
|
|
|
2023-01-16 11:30:03 +00:00
|
|
|
func (es *Eventstore) appendEventType(typ EventType) {
|
|
|
|
i := sort.SearchStrings(es.eventTypes, string(typ))
|
2023-01-19 15:50:05 +00:00
|
|
|
if i < len(es.eventTypes) && es.eventTypes[i] == string(typ) {
|
2023-01-16 11:30:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
es.eventTypes = append(es.eventTypes[:i], append([]string{string(typ)}, es.eventTypes[i:]...)...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *Eventstore) appendAggregateType(typ AggregateType) {
|
|
|
|
i := sort.SearchStrings(es.aggregateTypes, string(typ))
|
|
|
|
if len(es.aggregateTypes) > i && es.aggregateTypes[i] == string(typ) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
es.aggregateTypes = append(es.aggregateTypes[:i], append([]string{string(typ)}, es.aggregateTypes[i:]...)...)
|
|
|
|
}
|
|
|
|
|
2022-01-03 08:19:07 +00:00
|
|
|
func EventData(event Command) ([]byte, error) {
|
2021-02-23 14:13:04 +00:00
|
|
|
switch data := event.Data().(type) {
|
|
|
|
case nil:
|
|
|
|
return nil, nil
|
|
|
|
case []byte:
|
|
|
|
if json.Valid(data) {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "V2-6SbbS", "data bytes are not json")
|
|
|
|
}
|
|
|
|
dataType := reflect.TypeOf(event.Data())
|
|
|
|
if dataType.Kind() == reflect.Ptr {
|
|
|
|
dataType = dataType.Elem()
|
|
|
|
}
|
|
|
|
if dataType.Kind() == reflect.Struct {
|
|
|
|
dataBytes, err := json.Marshal(event.Data())
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInvalidArgument(err, "V2-xG87M", "could not marshal data")
|
|
|
|
}
|
|
|
|
return dataBytes, nil
|
|
|
|
}
|
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "V2-91NRm", "wrong type of event data")
|
2020-04-07 11:23:04 +00:00
|
|
|
}
|
2020-11-12 21:50:01 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
func uniqueConstraintActionToRepository(action UniqueConstraintAction) repository.UniqueConstraintAction {
|
|
|
|
switch action {
|
|
|
|
case UniqueConstraintAdd:
|
|
|
|
return repository.UniqueConstraintAdd
|
|
|
|
case UniqueConstraintRemove:
|
|
|
|
return repository.UniqueConstraintRemoved
|
2022-10-26 13:06:48 +00:00
|
|
|
case UniqueConstraintInstanceRemove:
|
|
|
|
return repository.UniqueConstraintInstanceRemoved
|
2021-02-23 14:13:04 +00:00
|
|
|
default:
|
|
|
|
return repository.UniqueConstraintAdd
|
|
|
|
}
|
2020-11-12 21:50:01 +00:00
|
|
|
}
|
2023-04-19 08:46:02 +00:00
|
|
|
|
|
|
|
type BaseEventSetter[T any] interface {
|
|
|
|
Event
|
|
|
|
SetBaseEvent(*BaseEvent)
|
|
|
|
*T
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenericEventMapper[T any, PT BaseEventSetter[T]](event *repository.Event) (Event, error) {
|
|
|
|
e := PT(new(T))
|
|
|
|
e.SetBaseEvent(BaseEventFromRepo(event))
|
|
|
|
|
2023-08-02 16:57:53 +00:00
|
|
|
if len(event.Data) == 0 {
|
|
|
|
return e, nil
|
|
|
|
}
|
2023-04-19 08:46:02 +00:00
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "V2-Thai6", "unable to unmarshal event")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|