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"
@@ -32,44 +32,45 @@ const (
ComplexityPolicyOwnerRemovedCol = "owner_removed"
)
type passwordComplexityProjection struct {
crdb.StatementHandler
type passwordComplexityProjection struct{}
func newPasswordComplexityProjection(ctx context.Context, config handler.Config) *handler.Handler {
return handler.NewHandler(ctx, &config, new(passwordComplexityProjection))
}
func newPasswordComplexityProjection(ctx context.Context, config crdb.StatementHandlerConfig) *passwordComplexityProjection {
p := new(passwordComplexityProjection)
config.ProjectionName = PasswordComplexityTable
config.Reducers = p.reducers()
config.InitCheck = crdb.NewTableCheck(
crdb.NewTable([]*crdb.Column{
crdb.NewColumn(ComplexityPolicyIDCol, crdb.ColumnTypeText),
crdb.NewColumn(ComplexityPolicyCreationDateCol, crdb.ColumnTypeTimestamp),
crdb.NewColumn(ComplexityPolicyChangeDateCol, crdb.ColumnTypeTimestamp),
crdb.NewColumn(ComplexityPolicySequenceCol, crdb.ColumnTypeInt64),
crdb.NewColumn(ComplexityPolicyStateCol, crdb.ColumnTypeEnum),
crdb.NewColumn(ComplexityPolicyIsDefaultCol, crdb.ColumnTypeBool, crdb.Default(false)),
crdb.NewColumn(ComplexityPolicyResourceOwnerCol, crdb.ColumnTypeText),
crdb.NewColumn(ComplexityPolicyInstanceIDCol, crdb.ColumnTypeText),
crdb.NewColumn(ComplexityPolicyMinLengthCol, crdb.ColumnTypeInt64),
crdb.NewColumn(ComplexityPolicyHasLowercaseCol, crdb.ColumnTypeBool),
crdb.NewColumn(ComplexityPolicyHasUppercaseCol, crdb.ColumnTypeBool),
crdb.NewColumn(ComplexityPolicyHasSymbolCol, crdb.ColumnTypeBool),
crdb.NewColumn(ComplexityPolicyHasNumberCol, crdb.ColumnTypeBool),
crdb.NewColumn(ComplexityPolicyOwnerRemovedCol, crdb.ColumnTypeBool, crdb.Default(false)),
func (*passwordComplexityProjection) Name() string {
return PasswordComplexityTable
}
func (*passwordComplexityProjection) Init() *old_handler.Check {
return handler.NewTableCheck(
handler.NewTable([]*handler.InitColumn{
handler.NewColumn(ComplexityPolicyIDCol, handler.ColumnTypeText),
handler.NewColumn(ComplexityPolicyCreationDateCol, handler.ColumnTypeTimestamp),
handler.NewColumn(ComplexityPolicyChangeDateCol, handler.ColumnTypeTimestamp),
handler.NewColumn(ComplexityPolicySequenceCol, handler.ColumnTypeInt64),
handler.NewColumn(ComplexityPolicyStateCol, handler.ColumnTypeEnum),
handler.NewColumn(ComplexityPolicyIsDefaultCol, handler.ColumnTypeBool, handler.Default(false)),
handler.NewColumn(ComplexityPolicyResourceOwnerCol, handler.ColumnTypeText),
handler.NewColumn(ComplexityPolicyInstanceIDCol, handler.ColumnTypeText),
handler.NewColumn(ComplexityPolicyMinLengthCol, handler.ColumnTypeInt64),
handler.NewColumn(ComplexityPolicyHasLowercaseCol, handler.ColumnTypeBool),
handler.NewColumn(ComplexityPolicyHasUppercaseCol, handler.ColumnTypeBool),
handler.NewColumn(ComplexityPolicyHasSymbolCol, handler.ColumnTypeBool),
handler.NewColumn(ComplexityPolicyHasNumberCol, handler.ColumnTypeBool),
handler.NewColumn(ComplexityPolicyOwnerRemovedCol, handler.ColumnTypeBool, handler.Default(false)),
},
crdb.NewPrimaryKey(ComplexityPolicyInstanceIDCol, ComplexityPolicyIDCol),
crdb.WithIndex(crdb.NewIndex("owner_removed", []string{ComplexityPolicyOwnerRemovedCol})),
handler.NewPrimaryKey(ComplexityPolicyInstanceIDCol, ComplexityPolicyIDCol),
handler.WithIndex(handler.NewIndex("owner_removed", []string{ComplexityPolicyOwnerRemovedCol})),
),
)
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
return p
}
func (p *passwordComplexityProjection) reducers() []handler.AggregateReducer {
func (p *passwordComplexityProjection) Reducers() []handler.AggregateReducer {
return []handler.AggregateReducer{
{
Aggregate: org.AggregateType,
EventRedusers: []handler.EventReducer{
EventReducers: []handler.EventReducer{
{
Event: org.PasswordComplexityPolicyAddedEventType,
Reduce: p.reduceAdded,
@@ -90,7 +91,7 @@ func (p *passwordComplexityProjection) reducers() []handler.AggregateReducer {
},
{
Aggregate: instance.AggregateType,
EventRedusers: []handler.EventReducer{
EventReducers: []handler.EventReducer{
{
Event: instance.PasswordComplexityPolicyAddedEventType,
Reduce: p.reduceAdded,
@@ -121,7 +122,7 @@ func (p *passwordComplexityProjection) reduceAdded(event eventstore.Event) (*han
default:
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-KTHmJ", "reduce.wrong.event.type %v", []eventstore.EventType{org.PasswordComplexityPolicyAddedEventType, instance.PasswordComplexityPolicyAddedEventType})
}
return crdb.NewCreateStatement(
return handler.NewCreateStatement(
&policyEvent,
[]handler.Column{
handler.NewCol(ComplexityPolicyCreationDateCol, policyEvent.CreationDate()),
@@ -169,7 +170,7 @@ func (p *passwordComplexityProjection) reduceChanged(event eventstore.Event) (*h
if policyEvent.HasNumber != nil {
cols = append(cols, handler.NewCol(ComplexityPolicyHasNumberCol, *policyEvent.HasNumber))
}
return crdb.NewUpdateStatement(
return handler.NewUpdateStatement(
&policyEvent,
cols,
[]handler.Condition{
@@ -183,7 +184,7 @@ func (p *passwordComplexityProjection) reduceRemoved(event eventstore.Event) (*h
if !ok {
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-wttCd", "reduce.wrong.event.type %s", org.PasswordComplexityPolicyRemovedEventType)
}
return crdb.NewDeleteStatement(
return handler.NewDeleteStatement(
policyEvent,
[]handler.Condition{
handler.NewCond(ComplexityPolicyIDCol, policyEvent.Aggregate().ID),
@@ -197,7 +198,7 @@ func (p *passwordComplexityProjection) reduceOwnerRemoved(event eventstore.Event
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-pGTz9", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
}
return crdb.NewDeleteStatement(
return handler.NewDeleteStatement(
e,
[]handler.Condition{
handler.NewCond(ComplexityPolicyInstanceIDCol, e.Aggregate().InstanceID),