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"
@@ -31,43 +31,44 @@ const (
PrivacyPolicyOwnerRemovedCol = "owner_removed"
)
type privacyPolicyProjection struct {
crdb.StatementHandler
type privacyPolicyProjection struct{}
func newPrivacyPolicyProjection(ctx context.Context, config handler.Config) *handler.Handler {
return handler.NewHandler(ctx, &config, new(privacyPolicyProjection))
}
func newPrivacyPolicyProjection(ctx context.Context, config crdb.StatementHandlerConfig) *privacyPolicyProjection {
p := new(privacyPolicyProjection)
config.ProjectionName = PrivacyPolicyTable
config.Reducers = p.reducers()
config.InitCheck = crdb.NewTableCheck(
crdb.NewTable([]*crdb.Column{
crdb.NewColumn(PrivacyPolicyIDCol, crdb.ColumnTypeText),
crdb.NewColumn(PrivacyPolicyCreationDateCol, crdb.ColumnTypeTimestamp),
crdb.NewColumn(PrivacyPolicyChangeDateCol, crdb.ColumnTypeTimestamp),
crdb.NewColumn(PrivacyPolicySequenceCol, crdb.ColumnTypeInt64),
crdb.NewColumn(PrivacyPolicyStateCol, crdb.ColumnTypeEnum),
crdb.NewColumn(PrivacyPolicyIsDefaultCol, crdb.ColumnTypeBool, crdb.Default(false)),
crdb.NewColumn(PrivacyPolicyResourceOwnerCol, crdb.ColumnTypeText),
crdb.NewColumn(PrivacyPolicyInstanceIDCol, crdb.ColumnTypeText),
crdb.NewColumn(PrivacyPolicyPrivacyLinkCol, crdb.ColumnTypeText),
crdb.NewColumn(PrivacyPolicyTOSLinkCol, crdb.ColumnTypeText),
crdb.NewColumn(PrivacyPolicyHelpLinkCol, crdb.ColumnTypeText),
crdb.NewColumn(PrivacyPolicySupportEmailCol, crdb.ColumnTypeText),
crdb.NewColumn(PrivacyPolicyOwnerRemovedCol, crdb.ColumnTypeBool, crdb.Default(false)),
func (*privacyPolicyProjection) Name() string {
return PrivacyPolicyTable
}
func (*privacyPolicyProjection) Init() *old_handler.Check {
return handler.NewTableCheck(
handler.NewTable([]*handler.InitColumn{
handler.NewColumn(PrivacyPolicyIDCol, handler.ColumnTypeText),
handler.NewColumn(PrivacyPolicyCreationDateCol, handler.ColumnTypeTimestamp),
handler.NewColumn(PrivacyPolicyChangeDateCol, handler.ColumnTypeTimestamp),
handler.NewColumn(PrivacyPolicySequenceCol, handler.ColumnTypeInt64),
handler.NewColumn(PrivacyPolicyStateCol, handler.ColumnTypeEnum),
handler.NewColumn(PrivacyPolicyIsDefaultCol, handler.ColumnTypeBool, handler.Default(false)),
handler.NewColumn(PrivacyPolicyResourceOwnerCol, handler.ColumnTypeText),
handler.NewColumn(PrivacyPolicyInstanceIDCol, handler.ColumnTypeText),
handler.NewColumn(PrivacyPolicyPrivacyLinkCol, handler.ColumnTypeText),
handler.NewColumn(PrivacyPolicyTOSLinkCol, handler.ColumnTypeText),
handler.NewColumn(PrivacyPolicyHelpLinkCol, handler.ColumnTypeText),
handler.NewColumn(PrivacyPolicySupportEmailCol, handler.ColumnTypeText),
handler.NewColumn(PrivacyPolicyOwnerRemovedCol, handler.ColumnTypeBool, handler.Default(false)),
},
crdb.NewPrimaryKey(PrivacyPolicyInstanceIDCol, PrivacyPolicyIDCol),
crdb.WithIndex(crdb.NewIndex("owner_removed", []string{PrivacyPolicyOwnerRemovedCol})),
handler.NewPrimaryKey(PrivacyPolicyInstanceIDCol, PrivacyPolicyIDCol),
handler.WithIndex(handler.NewIndex("owner_removed", []string{PrivacyPolicyOwnerRemovedCol})),
),
)
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
return p
}
func (p *privacyPolicyProjection) reducers() []handler.AggregateReducer {
func (p *privacyPolicyProjection) Reducers() []handler.AggregateReducer {
return []handler.AggregateReducer{
{
Aggregate: org.AggregateType,
EventRedusers: []handler.EventReducer{
EventReducers: []handler.EventReducer{
{
Event: org.PrivacyPolicyAddedEventType,
Reduce: p.reduceAdded,
@@ -88,7 +89,7 @@ func (p *privacyPolicyProjection) reducers() []handler.AggregateReducer {
},
{
Aggregate: instance.AggregateType,
EventRedusers: []handler.EventReducer{
EventReducers: []handler.EventReducer{
{
Event: instance.PrivacyPolicyAddedEventType,
Reduce: p.reduceAdded,
@@ -119,7 +120,7 @@ func (p *privacyPolicyProjection) reduceAdded(event eventstore.Event) (*handler.
default:
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-kRNh8", "reduce.wrong.event.type %v", []eventstore.EventType{org.PrivacyPolicyAddedEventType, instance.PrivacyPolicyAddedEventType})
}
return crdb.NewCreateStatement(
return handler.NewCreateStatement(
&policyEvent,
[]handler.Column{
handler.NewCol(PrivacyPolicyCreationDateCol, policyEvent.CreationDate()),
@@ -163,7 +164,7 @@ func (p *privacyPolicyProjection) reduceChanged(event eventstore.Event) (*handle
if policyEvent.SupportEmail != nil {
cols = append(cols, handler.NewCol(PrivacyPolicySupportEmailCol, *policyEvent.SupportEmail))
}
return crdb.NewUpdateStatement(
return handler.NewUpdateStatement(
&policyEvent,
cols,
[]handler.Condition{
@@ -177,7 +178,7 @@ func (p *privacyPolicyProjection) reduceRemoved(event eventstore.Event) (*handle
if !ok {
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-FvtGO", "reduce.wrong.event.type %s", org.PrivacyPolicyRemovedEventType)
}
return crdb.NewDeleteStatement(
return handler.NewDeleteStatement(
policyEvent,
[]handler.Condition{
handler.NewCond(PrivacyPolicyIDCol, policyEvent.Aggregate().ID),
@@ -191,7 +192,7 @@ func (p *privacyPolicyProjection) reduceOwnerRemoved(event eventstore.Event) (*h
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-bxJCY", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
}
return crdb.NewDeleteStatement(
return handler.NewDeleteStatement(
e,
[]handler.Condition{
handler.NewCond(PrivacyPolicyInstanceIDCol, e.Aggregate().InstanceID),