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"
@@ -30,42 +30,43 @@ const (
DomainPolicyOwnerRemovedCol = "owner_removed"
)
type domainPolicyProjection struct {
crdb.StatementHandler
type domainPolicyProjection struct{}
func newDomainPolicyProjection(ctx context.Context, config handler.Config) *handler.Handler {
return handler.NewHandler(ctx, &config, new(domainPolicyProjection))
}
func newDomainPolicyProjection(ctx context.Context, config crdb.StatementHandlerConfig) *domainPolicyProjection {
p := new(domainPolicyProjection)
config.ProjectionName = DomainPolicyTable
config.Reducers = p.reducers()
config.InitCheck = crdb.NewTableCheck(
crdb.NewTable([]*crdb.Column{
crdb.NewColumn(DomainPolicyIDCol, crdb.ColumnTypeText),
crdb.NewColumn(DomainPolicyCreationDateCol, crdb.ColumnTypeTimestamp),
crdb.NewColumn(DomainPolicyChangeDateCol, crdb.ColumnTypeTimestamp),
crdb.NewColumn(DomainPolicySequenceCol, crdb.ColumnTypeInt64),
crdb.NewColumn(DomainPolicyStateCol, crdb.ColumnTypeEnum),
crdb.NewColumn(DomainPolicyUserLoginMustBeDomainCol, crdb.ColumnTypeBool),
crdb.NewColumn(DomainPolicyValidateOrgDomainsCol, crdb.ColumnTypeBool),
crdb.NewColumn(DomainPolicySMTPSenderAddressMatchesInstanceDomainCol, crdb.ColumnTypeBool),
crdb.NewColumn(DomainPolicyIsDefaultCol, crdb.ColumnTypeBool, crdb.Default(false)),
crdb.NewColumn(DomainPolicyResourceOwnerCol, crdb.ColumnTypeText),
crdb.NewColumn(DomainPolicyInstanceIDCol, crdb.ColumnTypeText),
crdb.NewColumn(DomainPolicyOwnerRemovedCol, crdb.ColumnTypeBool, crdb.Default(false)),
func (*domainPolicyProjection) Name() string {
return DomainPolicyTable
}
func (*domainPolicyProjection) Init() *old_handler.Check {
return handler.NewTableCheck(
handler.NewTable([]*handler.InitColumn{
handler.NewColumn(DomainPolicyIDCol, handler.ColumnTypeText),
handler.NewColumn(DomainPolicyCreationDateCol, handler.ColumnTypeTimestamp),
handler.NewColumn(DomainPolicyChangeDateCol, handler.ColumnTypeTimestamp),
handler.NewColumn(DomainPolicySequenceCol, handler.ColumnTypeInt64),
handler.NewColumn(DomainPolicyStateCol, handler.ColumnTypeEnum),
handler.NewColumn(DomainPolicyUserLoginMustBeDomainCol, handler.ColumnTypeBool),
handler.NewColumn(DomainPolicyValidateOrgDomainsCol, handler.ColumnTypeBool),
handler.NewColumn(DomainPolicySMTPSenderAddressMatchesInstanceDomainCol, handler.ColumnTypeBool),
handler.NewColumn(DomainPolicyIsDefaultCol, handler.ColumnTypeBool, handler.Default(false)),
handler.NewColumn(DomainPolicyResourceOwnerCol, handler.ColumnTypeText),
handler.NewColumn(DomainPolicyInstanceIDCol, handler.ColumnTypeText),
handler.NewColumn(DomainPolicyOwnerRemovedCol, handler.ColumnTypeBool, handler.Default(false)),
},
crdb.NewPrimaryKey(DomainPolicyInstanceIDCol, DomainPolicyIDCol),
crdb.WithIndex(crdb.NewIndex("owner_removed", []string{DomainPolicyOwnerRemovedCol})),
handler.NewPrimaryKey(DomainPolicyInstanceIDCol, DomainPolicyIDCol),
handler.WithIndex(handler.NewIndex("owner_removed", []string{DomainPolicyOwnerRemovedCol})),
),
)
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
return p
}
func (p *domainPolicyProjection) reducers() []handler.AggregateReducer {
func (p *domainPolicyProjection) Reducers() []handler.AggregateReducer {
return []handler.AggregateReducer{
{
Aggregate: org.AggregateType,
EventRedusers: []handler.EventReducer{
EventReducers: []handler.EventReducer{
{
Event: org.DomainPolicyAddedEventType,
Reduce: p.reduceAdded,
@@ -86,7 +87,7 @@ func (p *domainPolicyProjection) reducers() []handler.AggregateReducer {
},
{
Aggregate: instance.AggregateType,
EventRedusers: []handler.EventReducer{
EventReducers: []handler.EventReducer{
{
Event: instance.DomainPolicyAddedEventType,
Reduce: p.reduceAdded,
@@ -117,7 +118,7 @@ func (p *domainPolicyProjection) reduceAdded(event eventstore.Event) (*handler.S
default:
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-CSE7A", "reduce.wrong.event.type %v", []eventstore.EventType{org.DomainPolicyAddedEventType, instance.DomainPolicyAddedEventType})
}
return crdb.NewCreateStatement(
return handler.NewCreateStatement(
&policyEvent,
[]handler.Column{
handler.NewCol(DomainPolicyCreationDateCol, policyEvent.CreationDate()),
@@ -157,7 +158,7 @@ func (p *domainPolicyProjection) reduceChanged(event eventstore.Event) (*handler
if policyEvent.SMTPSenderAddressMatchesInstanceDomain != nil {
cols = append(cols, handler.NewCol(DomainPolicySMTPSenderAddressMatchesInstanceDomainCol, *policyEvent.SMTPSenderAddressMatchesInstanceDomain))
}
return crdb.NewUpdateStatement(
return handler.NewUpdateStatement(
&policyEvent,
cols,
[]handler.Condition{
@@ -171,7 +172,7 @@ func (p *domainPolicyProjection) reduceRemoved(event eventstore.Event) (*handler
if !ok {
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-JAENd", "reduce.wrong.event.type %s", org.DomainPolicyRemovedEventType)
}
return crdb.NewDeleteStatement(
return handler.NewDeleteStatement(
policyEvent,
[]handler.Condition{
handler.NewCond(DomainPolicyIDCol, policyEvent.Aggregate().ID),
@@ -185,7 +186,7 @@ func (p *domainPolicyProjection) reduceOwnerRemoved(event eventstore.Event) (*ha
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-JYD2K", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
}
return crdb.NewDeleteStatement(
return handler.NewDeleteStatement(
e,
[]handler.Condition{
handler.NewCond(DomainPolicyInstanceIDCol, e.Aggregate().InstanceID),