feat(eventstore): increase parallel write capabilities (#5940)

This implementation increases parallel write capabilities of the eventstore.
Please have a look at the technical advisories: [05](https://zitadel.com/docs/support/advisory/a10005) and  [06](https://zitadel.com/docs/support/advisory/a10006).
The implementation of eventstore.push is rewritten and stored events are migrated to a new table `eventstore.events2`.
If you are using cockroach: make sure that the database user of ZITADEL has `VIEWACTIVITY` grant. This is used to query events.
This commit is contained in:
Silvan
2023-10-19 12:19:10 +02:00
committed by GitHub
parent 259faba3f0
commit b5564572bc
791 changed files with 30326 additions and 43202 deletions

View File

@@ -3,8 +3,7 @@ package eventstore
import (
"sync"
v1 "github.com/zitadel/zitadel/internal/eventstore/v1"
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
"github.com/zitadel/logging"
)
var (
@@ -17,7 +16,7 @@ type Subscription struct {
types map[AggregateType][]EventType
}
//SubscribeAggregates subscribes for all events on the given aggregates
// SubscribeAggregates subscribes for all events on the given aggregates
func SubscribeAggregates(eventQueue chan Event, aggregates ...AggregateType) *Subscription {
types := make(map[AggregateType][]EventType, len(aggregates))
for _, aggregate := range aggregates {
@@ -38,10 +37,9 @@ func SubscribeAggregates(eventQueue chan Event, aggregates ...AggregateType) *Su
return sub
}
//SubscribeEventTypes subscribes for the given event types
// SubscribeEventTypes subscribes for the given event types
// if no event types are provided the subscription is for all events of the aggregate
func SubscribeEventTypes(eventQueue chan Event, types map[AggregateType][]EventType) *Subscription {
aggregates := make([]AggregateType, len(types))
sub := &Subscription{
Events: eventQueue,
types: types,
@@ -50,15 +48,14 @@ func SubscribeEventTypes(eventQueue chan Event, types map[AggregateType][]EventT
subsMutext.Lock()
defer subsMutext.Unlock()
for _, aggregate := range aggregates {
for aggregate := range types {
subscriptions[aggregate] = append(subscriptions[aggregate], sub)
}
return sub
}
func notify(events []Event) {
go v1.Notify(MapEventsToV1Events(events))
func (es *Eventstore) notify(events []Event) {
subsMutext.Lock()
defer subsMutext.Unlock()
for _, event := range events {
@@ -76,7 +73,11 @@ func notify(events []Event) {
//subscription for certain events
for _, eventType := range eventTypes {
if event.Type() == eventType {
sub.Events <- event
select {
case sub.Events <- event:
default:
logging.Debug("unable to push event")
}
break
}
}
@@ -105,26 +106,3 @@ func (s *Subscription) Unsubscribe() {
close(s.Events)
}
}
func MapEventsToV1Events(events []Event) []*models.Event {
v1Events := make([]*models.Event, len(events))
for i, event := range events {
v1Events[i] = mapEventToV1Event(event)
}
return v1Events
}
func mapEventToV1Event(event Event) *models.Event {
return &models.Event{
Sequence: event.Sequence(),
CreationDate: event.CreationDate(),
Type: models.EventType(event.Type()),
AggregateType: models.AggregateType(event.Aggregate().Type),
AggregateID: event.Aggregate().ID,
ResourceOwner: event.Aggregate().ResourceOwner,
InstanceID: event.Aggregate().InstanceID,
EditorService: event.EditorService(),
EditorUser: event.EditorUser(),
Data: event.DataAsBytes(),
}
}