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

@@ -6,8 +6,8 @@ import (
"github.com/zitadel/zitadel/internal/domain"
"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"
"github.com/zitadel/zitadel/internal/repository/policy"
@@ -29,41 +29,42 @@ const (
LockoutPolicyOwnerRemovedCol = "owner_removed"
)
type lockoutPolicyProjection struct {
crdb.StatementHandler
type lockoutPolicyProjection struct{}
func newLockoutPolicyProjection(ctx context.Context, config handler.Config) *handler.Handler {
return handler.NewHandler(ctx, &config, new(lockoutPolicyProjection))
}
func newLockoutPolicyProjection(ctx context.Context, config crdb.StatementHandlerConfig) *lockoutPolicyProjection {
p := new(lockoutPolicyProjection)
config.ProjectionName = LockoutPolicyTable
config.Reducers = p.reducers()
config.InitCheck = crdb.NewTableCheck(
crdb.NewTable([]*crdb.Column{
crdb.NewColumn(LockoutPolicyIDCol, crdb.ColumnTypeText),
crdb.NewColumn(LockoutPolicyCreationDateCol, crdb.ColumnTypeTimestamp),
crdb.NewColumn(LockoutPolicyChangeDateCol, crdb.ColumnTypeTimestamp),
crdb.NewColumn(LockoutPolicySequenceCol, crdb.ColumnTypeInt64),
crdb.NewColumn(LockoutPolicyStateCol, crdb.ColumnTypeEnum),
crdb.NewColumn(LockoutPolicyIsDefaultCol, crdb.ColumnTypeBool, crdb.Default(false)),
crdb.NewColumn(LockoutPolicyResourceOwnerCol, crdb.ColumnTypeText),
crdb.NewColumn(LockoutPolicyInstanceIDCol, crdb.ColumnTypeText),
crdb.NewColumn(LockoutPolicyMaxPasswordAttemptsCol, crdb.ColumnTypeInt64),
crdb.NewColumn(LockoutPolicyShowLockOutFailuresCol, crdb.ColumnTypeBool),
crdb.NewColumn(LockoutPolicyOwnerRemovedCol, crdb.ColumnTypeBool, crdb.Default(false)),
func (*lockoutPolicyProjection) Name() string {
return LockoutPolicyTable
}
func (*lockoutPolicyProjection) Init() *old_handler.Check {
return handler.NewTableCheck(
handler.NewTable([]*handler.InitColumn{
handler.NewColumn(LockoutPolicyIDCol, handler.ColumnTypeText),
handler.NewColumn(LockoutPolicyCreationDateCol, handler.ColumnTypeTimestamp),
handler.NewColumn(LockoutPolicyChangeDateCol, handler.ColumnTypeTimestamp),
handler.NewColumn(LockoutPolicySequenceCol, handler.ColumnTypeInt64),
handler.NewColumn(LockoutPolicyStateCol, handler.ColumnTypeEnum),
handler.NewColumn(LockoutPolicyIsDefaultCol, handler.ColumnTypeBool, handler.Default(false)),
handler.NewColumn(LockoutPolicyResourceOwnerCol, handler.ColumnTypeText),
handler.NewColumn(LockoutPolicyInstanceIDCol, handler.ColumnTypeText),
handler.NewColumn(LockoutPolicyMaxPasswordAttemptsCol, handler.ColumnTypeInt64),
handler.NewColumn(LockoutPolicyShowLockOutFailuresCol, handler.ColumnTypeBool),
handler.NewColumn(LockoutPolicyOwnerRemovedCol, handler.ColumnTypeBool, handler.Default(false)),
},
crdb.NewPrimaryKey(LockoutPolicyInstanceIDCol, LockoutPolicyIDCol),
crdb.WithIndex(crdb.NewIndex("owner_removed", []string{LockoutPolicyOwnerRemovedCol})),
handler.NewPrimaryKey(LockoutPolicyInstanceIDCol, LockoutPolicyIDCol),
handler.WithIndex(handler.NewIndex("owner_removed", []string{LockoutPolicyOwnerRemovedCol})),
),
)
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
return p
}
func (p *lockoutPolicyProjection) reducers() []handler.AggregateReducer {
func (p *lockoutPolicyProjection) Reducers() []handler.AggregateReducer {
return []handler.AggregateReducer{
{
Aggregate: org.AggregateType,
EventRedusers: []handler.EventReducer{
EventReducers: []handler.EventReducer{
{
Event: org.LockoutPolicyAddedEventType,
Reduce: p.reduceAdded,
@@ -84,7 +85,7 @@ func (p *lockoutPolicyProjection) reducers() []handler.AggregateReducer {
},
{
Aggregate: instance.AggregateType,
EventRedusers: []handler.EventReducer{
EventReducers: []handler.EventReducer{
{
Event: instance.LockoutPolicyAddedEventType,
Reduce: p.reduceAdded,
@@ -115,7 +116,7 @@ func (p *lockoutPolicyProjection) reduceAdded(event eventstore.Event) (*handler.
default:
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-d8mZO", "reduce.wrong.event.type, %v", []eventstore.EventType{org.LockoutPolicyAddedEventType, instance.LockoutPolicyAddedEventType})
}
return crdb.NewCreateStatement(
return handler.NewCreateStatement(
&policyEvent,
[]handler.Column{
handler.NewCol(LockoutPolicyCreationDateCol, policyEvent.CreationDate()),
@@ -151,7 +152,7 @@ func (p *lockoutPolicyProjection) reduceChanged(event eventstore.Event) (*handle
if policyEvent.ShowLockOutFailures != nil {
cols = append(cols, handler.NewCol(LockoutPolicyShowLockOutFailuresCol, *policyEvent.ShowLockOutFailures))
}
return crdb.NewUpdateStatement(
return handler.NewUpdateStatement(
&policyEvent,
cols,
[]handler.Condition{
@@ -165,7 +166,7 @@ func (p *lockoutPolicyProjection) reduceRemoved(event eventstore.Event) (*handle
if !ok {
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-Bqut9", "reduce.wrong.event.type %s", org.LockoutPolicyRemovedEventType)
}
return crdb.NewDeleteStatement(
return handler.NewDeleteStatement(
policyEvent,
[]handler.Condition{
handler.NewCond(LockoutPolicyIDCol, policyEvent.Aggregate().ID),
@@ -179,7 +180,7 @@ func (p *lockoutPolicyProjection) reduceOwnerRemoved(event eventstore.Event) (*h
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-IoW0x", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
}
return crdb.NewDeleteStatement(
return handler.NewDeleteStatement(
e,
[]handler.Condition{
handler.NewCond(LockoutPolicyInstanceIDCol, e.Aggregate().InstanceID),