bring triggers back

This commit is contained in:
Tim Möhlmann
2023-11-15 17:15:47 +02:00
parent fc55c04bda
commit 5ae29c077f
7 changed files with 62 additions and 21 deletions

View File

@@ -23,6 +23,10 @@ func (s *Server) Introspect(ctx context.Context, r *op.Request[op.IntrospectionR
if s.features.LegacyIntrospection { if s.features.LegacyIntrospection {
return s.LegacyServer.Introspect(ctx, r) return s.LegacyServer.Introspect(ctx, r)
} }
if s.features.TriggerIntrospectionProjections {
// Execute all triggers in one concurrent sweep.
ctx = query.TriggerIntrospectionProjections(ctx)
}
ctx, cancel := context.WithCancel(ctx) ctx, cancel := context.WithCancel(ctx)
defer cancel() defer cancel()

View File

@@ -456,3 +456,8 @@ func (h *Handler) eventQuery(currentState *state) *eventstore.SearchQueryBuilder
return builder return builder
} }
// ProjectionName returns the name of the unlying projection.
func (h *Handler) ProjectionName() string {
return h.projection.Name()
}

View File

@@ -8,9 +8,20 @@ import (
"github.com/zitadel/zitadel/internal/api/authz" "github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/crypto" "github.com/zitadel/zitadel/internal/crypto"
"github.com/zitadel/zitadel/internal/database" "github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/query/projection"
"github.com/zitadel/zitadel/internal/telemetry/tracing" "github.com/zitadel/zitadel/internal/telemetry/tracing"
) )
var introspectionTriggerHandlers = append(oidcUserInfoTriggerHandlers,
projection.AppProjection,
projection.OIDCSettingsProjection,
projection.AuthNKeyProjection,
)
func TriggerIntrospectionProjections(ctx context.Context) context.Context {
return triggerBatch(ctx, introspectionTriggerHandlers...)
}
type IntrospectionClient struct { type IntrospectionClient struct {
ClientID string ClientID string
ClientSecret *crypto.CryptoValue ClientSecret *crypto.CryptoValue

View File

@@ -11,6 +11,8 @@ import (
"github.com/rakyll/statik/fs" "github.com/rakyll/statik/fs"
"golang.org/x/text/language" "golang.org/x/text/language"
"github.com/zitadel/logging"
"github.com/zitadel/zitadel/internal/api/authz" "github.com/zitadel/zitadel/internal/api/authz"
internal_authz "github.com/zitadel/zitadel/internal/api/authz" internal_authz "github.com/zitadel/zitadel/internal/api/authz"
sd "github.com/zitadel/zitadel/internal/config/systemdefaults" sd "github.com/zitadel/zitadel/internal/config/systemdefaults"
@@ -18,6 +20,7 @@ import (
"github.com/zitadel/zitadel/internal/database" "github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/domain" "github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/eventstore" "github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
"github.com/zitadel/zitadel/internal/query/projection" "github.com/zitadel/zitadel/internal/query/projection"
"github.com/zitadel/zitadel/internal/repository/action" "github.com/zitadel/zitadel/internal/repository/action"
"github.com/zitadel/zitadel/internal/repository/authrequest" "github.com/zitadel/zitadel/internal/repository/authrequest"
@@ -32,6 +35,7 @@ import (
"github.com/zitadel/zitadel/internal/repository/session" "github.com/zitadel/zitadel/internal/repository/session"
usr_repo "github.com/zitadel/zitadel/internal/repository/user" usr_repo "github.com/zitadel/zitadel/internal/repository/user"
"github.com/zitadel/zitadel/internal/repository/usergrant" "github.com/zitadel/zitadel/internal/repository/usergrant"
"github.com/zitadel/zitadel/internal/telemetry/tracing"
) )
type Queries struct { type Queries struct {
@@ -146,3 +150,25 @@ func init() {
&authRequestByIDQuery, &authRequestByIDQuery,
) )
} }
// triggerBatch calls Trigger on every handler in a seperate Go routine.
// The returned context is the context returned by the Trigger that finishes last.
func triggerBatch(ctx context.Context, handlers ...*handler.Handler) context.Context {
ctxChan := make(chan context.Context)
for _, h := range handlers {
go func(ctx context.Context, h *handler.Handler, ctxChan chan<- context.Context) {
name := h.ProjectionName()
_, traceSpan := tracing.NewNamedSpan(ctx, fmt.Sprintf("Trigger%s", name))
newCtx, err := h.Trigger(ctx, handler.WithAwaitRunning())
logging.OnError(err).WithField("projection", name).Debug("trigger failed")
traceSpan.EndWithError(err)
ctxChan <- newCtx
}(ctx, h, ctxChan)
}
for i := 0; i < len(handlers); i++ {
ctx = <-ctxChan
}
return ctx
}

View File

@@ -5,11 +5,9 @@ import (
"database/sql" "database/sql"
errs "errors" errs "errors"
"strings" "strings"
"sync"
"time" "time"
sq "github.com/Masterminds/squirrel" sq "github.com/Masterminds/squirrel"
"github.com/zitadel/logging"
"golang.org/x/text/language" "golang.org/x/text/language"
"github.com/zitadel/zitadel/internal/api/authz" "github.com/zitadel/zitadel/internal/api/authz"
@@ -17,7 +15,6 @@ import (
"github.com/zitadel/zitadel/internal/database" "github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/domain" "github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/errors" "github.com/zitadel/zitadel/internal/errors"
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
"github.com/zitadel/zitadel/internal/query/projection" "github.com/zitadel/zitadel/internal/query/projection"
"github.com/zitadel/zitadel/internal/telemetry/tracing" "github.com/zitadel/zitadel/internal/telemetry/tracing"
) )
@@ -709,24 +706,8 @@ func NewUserLoginNameExistsQuery(value string, comparison TextComparison) (Searc
) )
} }
func triggerUserProjections(ctx context.Context) { func triggerUserProjections(ctx context.Context) context.Context {
wg := sync.WaitGroup{} return triggerBatch(ctx, projection.UserProjection, projection.LoginNameProjection)
wg.Add(2)
func() {
_, traceSpan := tracing.NewNamedSpan(ctx, "TriggerUserProjection")
_, err := projection.UserProjection.Trigger(ctx, handler.WithAwaitRunning())
logging.OnError(err).Debug("trigger failed")
traceSpan.EndWithError(err)
wg.Done()
}()
func() {
_, traceSpan := tracing.NewNamedSpan(ctx, "TriggerLoginNameProjection")
_, err := projection.LoginNameProjection.Trigger(ctx, handler.WithAwaitRunning())
traceSpan.EndWithError(err)
logging.OnError(err).Debug("trigger failed")
wg.Done()
}()
wg.Wait()
} }
func prepareLoginNamesQuery() (string, []interface{}, error) { func prepareLoginNamesQuery() (string, []interface{}, error) {

View File

@@ -9,9 +9,23 @@ import (
"github.com/zitadel/zitadel/internal/api/authz" "github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/database" "github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/errors" "github.com/zitadel/zitadel/internal/errors"
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
"github.com/zitadel/zitadel/internal/query/projection"
"github.com/zitadel/zitadel/internal/telemetry/tracing" "github.com/zitadel/zitadel/internal/telemetry/tracing"
) )
var oidcUserInfoTriggerHandlers = []*handler.Handler{
projection.UserProjection,
projection.UserMetadataProjection,
projection.UserGrantProjection,
projection.OrgProjection,
projection.ProjectProjection,
}
func TriggerOIDCUserInfoProjections(ctx context.Context) context.Context {
return triggerBatch(ctx, oidcUserInfoTriggerHandlers...)
}
//go:embed embed/userinfo_by_id.sql //go:embed embed/userinfo_by_id.sql
var oidcUserInfoQuery string var oidcUserInfoQuery string