From 17303d1524f1ae3102043de84576c421a2c4a057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20M=C3=B6hlmann?= Date: Wed, 9 Oct 2024 11:00:07 +0300 Subject: [PATCH] perf(milestones): remove legacy token event reducer (#8747) # Which Problems Are Solved Since the optiimzation of the token endpoint, we longer push the `user.token.added` event. However, the milestone projection keeps quering for it, including a payload query. This incured a static waste of DB resources. # How the Problems Are Solved Remove the `user.token.added` event reducer from the milestone projection # Additional Changes - none # Additional Context - Related to https://github.com/zitadel/zitadel/issues/8742. Other changes ommitted so this PR can be backported to stable. --- internal/query/projection/milestones.go | 46 -------------------- internal/query/projection/milestones_test.go | 39 ----------------- 2 files changed, 85 deletions(-) diff --git a/internal/query/projection/milestones.go b/internal/query/projection/milestones.go index 7c344001b3..d7ee1997df 100644 --- a/internal/query/projection/milestones.go +++ b/internal/query/projection/milestones.go @@ -12,7 +12,6 @@ import ( "github.com/zitadel/zitadel/internal/repository/milestone" "github.com/zitadel/zitadel/internal/repository/oidcsession" "github.com/zitadel/zitadel/internal/repository/project" - "github.com/zitadel/zitadel/internal/repository/user" ) const ( @@ -94,17 +93,6 @@ func (p *milestoneProjection) Reducers() []handler.AggregateReducer { }, }, }, - { - Aggregate: user.AggregateType, - EventReducers: []handler.EventReducer{ - { - // user.UserTokenAddedType is not emitted on creation of personal access tokens - // PATs have no effect on milestone.AuthenticationSucceededOnApplication or milestone.AuthenticationSucceededOnInstance - Event: user.UserTokenAddedType, - Reduce: p.reduceUserTokenAdded, - }, - }, - }, { Aggregate: oidcsession.AggregateType, EventReducers: []handler.EventReducer{ @@ -193,40 +181,6 @@ func (p *milestoneProjection) reduceAPIConfigAdded(event eventstore.Event) (*han return p.reduceAppConfigAdded(e, e.ClientID) } -func (p *milestoneProjection) reduceUserTokenAdded(event eventstore.Event) (*handler.Statement, error) { - e, err := assertEvent[*user.UserTokenAddedEvent](event) - if err != nil { - return nil, err - } - statements := []func(eventstore.Event) handler.Exec{ - handler.AddUpdateStatement( - []handler.Column{ - handler.NewCol(MilestoneColumnReachedDate, event.CreatedAt()), - }, - []handler.Condition{ - handler.NewCond(MilestoneColumnInstanceID, event.Aggregate().InstanceID), - handler.NewCond(MilestoneColumnType, milestone.AuthenticationSucceededOnInstance), - handler.NewIsNullCond(MilestoneColumnReachedDate), - }, - ), - } - // We ignore authentications without app, for example JWT profile or PAT - if e.ApplicationID != "" { - statements = append(statements, handler.AddUpdateStatement( - []handler.Column{ - handler.NewCol(MilestoneColumnReachedDate, event.CreatedAt()), - }, - []handler.Condition{ - handler.NewCond(MilestoneColumnInstanceID, event.Aggregate().InstanceID), - handler.NewCond(MilestoneColumnType, milestone.AuthenticationSucceededOnApplication), - handler.Not(handler.NewTextArrayContainsCond(MilestoneColumnIgnoreClientIDs, e.ApplicationID)), - handler.NewIsNullCond(MilestoneColumnReachedDate), - }, - )) - } - return handler.NewMultiStatement(e, statements...), nil -} - func (p *milestoneProjection) reduceOIDCSessionAdded(event eventstore.Event) (*handler.Statement, error) { e, err := assertEvent[*oidcsession.AddedEvent](event) if err != nil { diff --git a/internal/query/projection/milestones_test.go b/internal/query/projection/milestones_test.go index f9eb4d40d1..0f443c9f19 100644 --- a/internal/query/projection/milestones_test.go +++ b/internal/query/projection/milestones_test.go @@ -11,7 +11,6 @@ import ( "github.com/zitadel/zitadel/internal/repository/milestone" "github.com/zitadel/zitadel/internal/repository/oidcsession" "github.com/zitadel/zitadel/internal/repository/project" - "github.com/zitadel/zitadel/internal/repository/user" "github.com/zitadel/zitadel/internal/zerrors" ) @@ -257,44 +256,6 @@ func TestMilestonesProjection_reduces(t *testing.T) { }, }, }, - { - name: "reduceUserTokenAdded", - args: args{ - event: getEvent(timedTestEvent( - user.UserTokenAddedType, - user.AggregateType, - []byte(`{"applicationId": "client-id"}`), - now, - ), user.UserTokenAddedEventMapper), - }, - reduce: (&milestoneProjection{}).reduceUserTokenAdded, - want: wantReduce{ - aggregateType: eventstore.AggregateType("user"), - sequence: 15, - executer: &testExecuter{ - // TODO: This can be optimized to only use one statement with OR - executions: []execution{ - { - expectedStmt: "UPDATE projections.milestones SET reached_date = $1 WHERE (instance_id = $2) AND (type = $3) AND (reached_date IS NULL)", - expectedArgs: []interface{}{ - now, - "instance-id", - milestone.AuthenticationSucceededOnInstance, - }, - }, - { - expectedStmt: "UPDATE projections.milestones SET reached_date = $1 WHERE (instance_id = $2) AND (type = $3) AND (NOT (ignore_client_ids @> $4)) AND (reached_date IS NULL)", - expectedArgs: []interface{}{ - now, - "instance-id", - milestone.AuthenticationSucceededOnApplication, - database.TextArray[string]{"client-id"}, - }, - }, - }, - }, - }, - }, { name: "reduceOIDCSessionAdded", args: args{