Silvan b5564572bc
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.
2023-10-19 12:19:10 +02:00

113 lines
2.6 KiB
Go

package model
import (
"encoding/json"
"strings"
"github.com/zitadel/logging"
"github.com/zitadel/zitadel/internal/errors"
caos_errs "github.com/zitadel/zitadel/internal/errors"
es_models "github.com/zitadel/zitadel/internal/eventstore/v1/models"
"github.com/zitadel/zitadel/internal/repository/user"
"github.com/zitadel/zitadel/internal/user/model"
)
const (
UserVersion = "v2"
)
type User struct {
es_models.ObjectRoot
State int32 `json:"-"`
UserName string `json:"userName"`
*Human
*Machine
}
func (u *User) AppendEvents(events ...*es_models.Event) error {
for _, event := range events {
if err := u.AppendEvent(event); err != nil {
return err
}
}
return nil
}
func (u *User) AppendEvent(event *es_models.Event) error {
u.ObjectRoot.AppendEvent(event)
switch event.Type() {
case user.UserV1AddedType,
user.HumanAddedType,
user.MachineAddedEventType,
user.UserV1RegisteredType,
user.HumanRegisteredType,
user.UserV1ProfileChangedType,
user.UserDomainClaimedType,
user.UserUserNameChangedType:
err := u.setData(event)
if err != nil {
return err
}
case user.UserDeactivatedType:
u.appendDeactivatedEvent()
case user.UserReactivatedType:
u.appendReactivatedEvent()
case user.UserLockedType:
u.appendLockedEvent()
case user.UserUnlockedType:
u.appendUnlockedEvent()
case user.UserRemovedType:
u.appendRemovedEvent()
}
if u.Human != nil {
u.Human.user = u
return u.Human.AppendEvent(event)
} else if u.Machine != nil {
u.Machine.user = u
return u.Machine.AppendEvent(event)
}
if strings.HasPrefix(string(event.Typ), "user.human") || event.AggregateVersion == "v1" {
u.Human = &Human{user: u}
return u.Human.AppendEvent(event)
}
if strings.HasPrefix(string(event.Typ), "user.machine") {
u.Machine = &Machine{user: u}
return u.Machine.AppendEvent(event)
}
return errors.ThrowNotFound(nil, "MODEL-x9TaX", "Errors.UserType.Undefined")
}
func (u *User) setData(event *es_models.Event) error {
if err := json.Unmarshal(event.Data, u); err != nil {
logging.Log("EVEN-ZDzQy").WithError(err).Error("could not unmarshal event data")
return caos_errs.ThrowInternal(err, "MODEL-yGmhh", "could not unmarshal event")
}
return nil
}
func (u *User) appendDeactivatedEvent() {
u.State = int32(model.UserStateInactive)
}
func (u *User) appendReactivatedEvent() {
u.State = int32(model.UserStateActive)
}
func (u *User) appendLockedEvent() {
u.State = int32(model.UserStateLocked)
}
func (u *User) appendUnlockedEvent() {
u.State = int32(model.UserStateActive)
}
func (u *User) appendRemovedEvent() {
u.State = int32(model.UserStateDeleted)
}