mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 01:37:31 +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:
@@ -6,8 +6,8 @@ import (
|
||||
"github.com/zitadel/zitadel/internal/database"
|
||||
"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/user"
|
||||
@@ -28,43 +28,43 @@ const (
|
||||
PersonalAccessTokenColumnOwnerRemoved = "owner_removed"
|
||||
)
|
||||
|
||||
type personalAccessTokenProjection struct {
|
||||
crdb.StatementHandler
|
||||
type personalAccessTokenProjection struct{}
|
||||
|
||||
func newPersonalAccessTokenProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
||||
return handler.NewHandler(ctx, &config, new(personalAccessTokenProjection))
|
||||
}
|
||||
|
||||
func newPersonalAccessTokenProjection(ctx context.Context, config crdb.StatementHandlerConfig) *personalAccessTokenProjection {
|
||||
p := new(personalAccessTokenProjection)
|
||||
config.ProjectionName = PersonalAccessTokenProjectionTable
|
||||
config.Reducers = p.reducers()
|
||||
config.InitCheck = crdb.NewTableCheck(
|
||||
crdb.NewTable([]*crdb.Column{
|
||||
crdb.NewColumn(PersonalAccessTokenColumnID, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(PersonalAccessTokenColumnCreationDate, crdb.ColumnTypeTimestamp),
|
||||
crdb.NewColumn(PersonalAccessTokenColumnChangeDate, crdb.ColumnTypeTimestamp),
|
||||
crdb.NewColumn(PersonalAccessTokenColumnSequence, crdb.ColumnTypeInt64),
|
||||
crdb.NewColumn(PersonalAccessTokenColumnResourceOwner, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(PersonalAccessTokenColumnInstanceID, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(PersonalAccessTokenColumnUserID, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(PersonalAccessTokenColumnExpiration, crdb.ColumnTypeTimestamp),
|
||||
crdb.NewColumn(PersonalAccessTokenColumnScopes, crdb.ColumnTypeTextArray, crdb.Nullable()),
|
||||
crdb.NewColumn(PersonalAccessTokenColumnOwnerRemoved, crdb.ColumnTypeBool, crdb.Default(false)),
|
||||
func (*personalAccessTokenProjection) Name() string {
|
||||
return PersonalAccessTokenProjectionTable
|
||||
}
|
||||
|
||||
func (*personalAccessTokenProjection) Init() *old_handler.Check {
|
||||
return handler.NewTableCheck(
|
||||
handler.NewTable([]*handler.InitColumn{
|
||||
handler.NewColumn(PersonalAccessTokenColumnID, handler.ColumnTypeText),
|
||||
handler.NewColumn(PersonalAccessTokenColumnCreationDate, handler.ColumnTypeTimestamp),
|
||||
handler.NewColumn(PersonalAccessTokenColumnChangeDate, handler.ColumnTypeTimestamp),
|
||||
handler.NewColumn(PersonalAccessTokenColumnSequence, handler.ColumnTypeInt64),
|
||||
handler.NewColumn(PersonalAccessTokenColumnResourceOwner, handler.ColumnTypeText),
|
||||
handler.NewColumn(PersonalAccessTokenColumnInstanceID, handler.ColumnTypeText),
|
||||
handler.NewColumn(PersonalAccessTokenColumnUserID, handler.ColumnTypeText),
|
||||
handler.NewColumn(PersonalAccessTokenColumnExpiration, handler.ColumnTypeTimestamp),
|
||||
handler.NewColumn(PersonalAccessTokenColumnScopes, handler.ColumnTypeTextArray, handler.Nullable()),
|
||||
handler.NewColumn(PersonalAccessTokenColumnOwnerRemoved, handler.ColumnTypeBool, handler.Default(false)),
|
||||
},
|
||||
crdb.NewPrimaryKey(PersonalAccessTokenColumnInstanceID, PersonalAccessTokenColumnID),
|
||||
crdb.WithIndex(crdb.NewIndex("user_id", []string{PersonalAccessTokenColumnUserID})),
|
||||
crdb.WithIndex(crdb.NewIndex("resource_owner", []string{PersonalAccessTokenColumnResourceOwner})),
|
||||
crdb.WithIndex(crdb.NewIndex("owner_removed", []string{PersonalAccessTokenColumnOwnerRemoved})),
|
||||
handler.NewPrimaryKey(PersonalAccessTokenColumnInstanceID, PersonalAccessTokenColumnID),
|
||||
handler.WithIndex(handler.NewIndex("user_id", []string{PersonalAccessTokenColumnUserID})),
|
||||
handler.WithIndex(handler.NewIndex("resource_owner", []string{PersonalAccessTokenColumnResourceOwner})),
|
||||
handler.WithIndex(handler.NewIndex("owner_removed", []string{PersonalAccessTokenColumnOwnerRemoved})),
|
||||
),
|
||||
)
|
||||
|
||||
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *personalAccessTokenProjection) reducers() []handler.AggregateReducer {
|
||||
func (p *personalAccessTokenProjection) Reducers() []handler.AggregateReducer {
|
||||
return []handler.AggregateReducer{
|
||||
{
|
||||
Aggregate: user.AggregateType,
|
||||
EventRedusers: []handler.EventReducer{
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: user.PersonalAccessTokenAddedType,
|
||||
Reduce: p.reducePersonalAccessTokenAdded,
|
||||
@@ -81,7 +81,7 @@ func (p *personalAccessTokenProjection) reducers() []handler.AggregateReducer {
|
||||
},
|
||||
{
|
||||
Aggregate: org.AggregateType,
|
||||
EventRedusers: []handler.EventReducer{
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: org.OrgRemovedEventType,
|
||||
Reduce: p.reduceOwnerRemoved,
|
||||
@@ -90,7 +90,7 @@ func (p *personalAccessTokenProjection) reducers() []handler.AggregateReducer {
|
||||
},
|
||||
{
|
||||
Aggregate: instance.AggregateType,
|
||||
EventRedusers: []handler.EventReducer{
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: instance.InstanceRemovedEventType,
|
||||
Reduce: reduceInstanceRemovedHelper(PersonalAccessTokenColumnInstanceID),
|
||||
@@ -105,7 +105,7 @@ func (p *personalAccessTokenProjection) reducePersonalAccessTokenAdded(event eve
|
||||
if !ok {
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-DVgf7", "reduce.wrong.event.type %s", user.PersonalAccessTokenAddedType)
|
||||
}
|
||||
return crdb.NewCreateStatement(
|
||||
return handler.NewCreateStatement(
|
||||
e,
|
||||
[]handler.Column{
|
||||
handler.NewCol(PersonalAccessTokenColumnID, e.TokenID),
|
||||
@@ -116,7 +116,7 @@ func (p *personalAccessTokenProjection) reducePersonalAccessTokenAdded(event eve
|
||||
handler.NewCol(PersonalAccessTokenColumnSequence, e.Sequence()),
|
||||
handler.NewCol(PersonalAccessTokenColumnUserID, e.Aggregate().ID),
|
||||
handler.NewCol(PersonalAccessTokenColumnExpiration, e.Expiration),
|
||||
handler.NewCol(PersonalAccessTokenColumnScopes, database.StringArray(e.Scopes)),
|
||||
handler.NewCol(PersonalAccessTokenColumnScopes, database.TextArray[string](e.Scopes)),
|
||||
},
|
||||
), nil
|
||||
}
|
||||
@@ -126,7 +126,7 @@ func (p *personalAccessTokenProjection) reducePersonalAccessTokenRemoved(event e
|
||||
if !ok {
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-g7u3F", "reduce.wrong.event.type %s", user.PersonalAccessTokenRemovedType)
|
||||
}
|
||||
return crdb.NewDeleteStatement(
|
||||
return handler.NewDeleteStatement(
|
||||
e,
|
||||
[]handler.Condition{
|
||||
handler.NewCond(PersonalAccessTokenColumnID, e.TokenID),
|
||||
@@ -140,7 +140,7 @@ func (p *personalAccessTokenProjection) reduceUserRemoved(event eventstore.Event
|
||||
if !ok {
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Dff3h", "reduce.wrong.event.type %s", user.UserRemovedType)
|
||||
}
|
||||
return crdb.NewDeleteStatement(
|
||||
return handler.NewDeleteStatement(
|
||||
e,
|
||||
[]handler.Condition{
|
||||
handler.NewCond(PersonalAccessTokenColumnUserID, e.Aggregate().ID),
|
||||
@@ -155,7 +155,7 @@ func (p *personalAccessTokenProjection) reduceOwnerRemoved(event eventstore.Even
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-zQVhl", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
|
||||
}
|
||||
|
||||
return crdb.NewDeleteStatement(
|
||||
return handler.NewDeleteStatement(
|
||||
e,
|
||||
[]handler.Condition{
|
||||
handler.NewCond(PersonalAccessTokenColumnInstanceID, e.Aggregate().InstanceID),
|
||||
|
Reference in New Issue
Block a user