mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 21:17:32 +00:00
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:
@@ -5,8 +5,8 @@ import (
|
||||
|
||||
"github.com/zitadel/zitadel/internal/errors"
|
||||
"github.com/zitadel/zitadel/internal/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/eventstore/handler"
|
||||
"github.com/zitadel/zitadel/internal/eventstore/handler/crdb"
|
||||
old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
|
||||
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
||||
"github.com/zitadel/zitadel/internal/repository/instance"
|
||||
"github.com/zitadel/zitadel/internal/repository/org"
|
||||
)
|
||||
@@ -24,39 +24,40 @@ const (
|
||||
FlowOwnerRemovedCol = "owner_removed"
|
||||
)
|
||||
|
||||
type flowProjection struct {
|
||||
crdb.StatementHandler
|
||||
type flowProjection struct{}
|
||||
|
||||
func newFlowProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
||||
return handler.NewHandler(ctx, &config, new(flowProjection))
|
||||
}
|
||||
|
||||
func newFlowProjection(ctx context.Context, config crdb.StatementHandlerConfig) *flowProjection {
|
||||
p := new(flowProjection)
|
||||
config.ProjectionName = FlowTriggerTable
|
||||
config.Reducers = p.reducers()
|
||||
config.InitCheck = crdb.NewTableCheck(
|
||||
crdb.NewTable([]*crdb.Column{
|
||||
crdb.NewColumn(FlowTypeCol, crdb.ColumnTypeEnum),
|
||||
crdb.NewColumn(FlowChangeDateCol, crdb.ColumnTypeTimestamp),
|
||||
crdb.NewColumn(FlowSequenceCol, crdb.ColumnTypeInt64),
|
||||
crdb.NewColumn(FlowTriggerTypeCol, crdb.ColumnTypeEnum),
|
||||
crdb.NewColumn(FlowResourceOwnerCol, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(FlowInstanceIDCol, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(FlowActionTriggerSequenceCol, crdb.ColumnTypeInt64),
|
||||
crdb.NewColumn(FlowActionIDCol, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(FlowOwnerRemovedCol, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
func (*flowProjection) Name() string {
|
||||
return FlowTriggerTable
|
||||
}
|
||||
|
||||
func (*flowProjection) Init() *old_handler.Check {
|
||||
return handler.NewTableCheck(
|
||||
handler.NewTable([]*handler.InitColumn{
|
||||
handler.NewColumn(FlowTypeCol, handler.ColumnTypeEnum),
|
||||
handler.NewColumn(FlowChangeDateCol, handler.ColumnTypeTimestamp),
|
||||
handler.NewColumn(FlowSequenceCol, handler.ColumnTypeInt64),
|
||||
handler.NewColumn(FlowTriggerTypeCol, handler.ColumnTypeEnum),
|
||||
handler.NewColumn(FlowResourceOwnerCol, handler.ColumnTypeText),
|
||||
handler.NewColumn(FlowInstanceIDCol, handler.ColumnTypeText),
|
||||
handler.NewColumn(FlowActionTriggerSequenceCol, handler.ColumnTypeInt64),
|
||||
handler.NewColumn(FlowActionIDCol, handler.ColumnTypeText),
|
||||
handler.NewColumn(FlowOwnerRemovedCol, handler.ColumnTypeBool, handler.Default(false)),
|
||||
},
|
||||
crdb.NewPrimaryKey(FlowInstanceIDCol, FlowTypeCol, FlowTriggerTypeCol, FlowResourceOwnerCol, FlowActionIDCol),
|
||||
crdb.WithIndex(crdb.NewIndex("owner_removed", []string{FlowOwnerRemovedCol})),
|
||||
handler.NewPrimaryKey(FlowInstanceIDCol, FlowTypeCol, FlowTriggerTypeCol, FlowResourceOwnerCol, FlowActionIDCol),
|
||||
handler.WithIndex(handler.NewIndex("owner_removed", []string{FlowOwnerRemovedCol})),
|
||||
),
|
||||
)
|
||||
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *flowProjection) reducers() []handler.AggregateReducer {
|
||||
func (p *flowProjection) Reducers() []handler.AggregateReducer {
|
||||
return []handler.AggregateReducer{
|
||||
{
|
||||
Aggregate: org.AggregateType,
|
||||
EventRedusers: []handler.EventReducer{
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: org.TriggerActionsSetEventType,
|
||||
Reduce: p.reduceTriggerActionsSetEventType,
|
||||
@@ -73,7 +74,7 @@ func (p *flowProjection) reducers() []handler.AggregateReducer {
|
||||
},
|
||||
{
|
||||
Aggregate: instance.AggregateType,
|
||||
EventRedusers: []handler.EventReducer{
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: instance.InstanceRemovedEventType,
|
||||
Reduce: reduceInstanceRemovedHelper(FlowInstanceIDCol),
|
||||
@@ -88,8 +89,8 @@ func (p *flowProjection) reduceTriggerActionsSetEventType(event eventstore.Event
|
||||
if !ok {
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-uYq4r", "reduce.wrong.event.type %s", org.TriggerActionsSetEventType)
|
||||
}
|
||||
stmts := make([]func(reader eventstore.Event) crdb.Exec, len(e.ActionIDs)+1)
|
||||
stmts[0] = crdb.AddDeleteStatement(
|
||||
stmts := make([]func(reader eventstore.Event) handler.Exec, len(e.ActionIDs)+1)
|
||||
stmts[0] = handler.AddDeleteStatement(
|
||||
[]handler.Condition{
|
||||
handler.NewCond(FlowTypeCol, e.FlowType),
|
||||
handler.NewCond(FlowTriggerTypeCol, e.TriggerType),
|
||||
@@ -98,7 +99,7 @@ func (p *flowProjection) reduceTriggerActionsSetEventType(event eventstore.Event
|
||||
},
|
||||
)
|
||||
for i, id := range e.ActionIDs {
|
||||
stmts[i+1] = crdb.AddCreateStatement(
|
||||
stmts[i+1] = handler.AddCreateStatement(
|
||||
[]handler.Column{
|
||||
handler.NewCol(FlowResourceOwnerCol, e.Aggregate().ResourceOwner),
|
||||
handler.NewCol(FlowInstanceIDCol, e.Aggregate().InstanceID),
|
||||
@@ -111,7 +112,7 @@ func (p *flowProjection) reduceTriggerActionsSetEventType(event eventstore.Event
|
||||
},
|
||||
)
|
||||
}
|
||||
return crdb.NewMultiStatement(e, stmts...), nil
|
||||
return handler.NewMultiStatement(e, stmts...), nil
|
||||
}
|
||||
|
||||
func (p *flowProjection) reduceFlowClearedEventType(event eventstore.Event) (*handler.Statement, error) {
|
||||
@@ -119,7 +120,7 @@ func (p *flowProjection) reduceFlowClearedEventType(event eventstore.Event) (*ha
|
||||
if !ok {
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-uYq4r", "reduce.wrong.event.type %s", org.FlowClearedEventType)
|
||||
}
|
||||
return crdb.NewDeleteStatement(
|
||||
return handler.NewDeleteStatement(
|
||||
e,
|
||||
[]handler.Condition{
|
||||
handler.NewCond(FlowTypeCol, e.FlowType),
|
||||
@@ -135,7 +136,7 @@ func (p *flowProjection) reduceOwnerRemoved(event eventstore.Event) (*handler.St
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-Yd7WC", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
|
||||
}
|
||||
|
||||
return crdb.NewDeleteStatement(
|
||||
return handler.NewDeleteStatement(
|
||||
e,
|
||||
[]handler.Condition{
|
||||
handler.NewCond(FlowInstanceIDCol, e.Aggregate().InstanceID),
|
||||
|
Reference in New Issue
Block a user