Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: batch identity created event #4111

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions persistence/sql/identity/persister_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -584,12 +584,24 @@ func (p *IdentityPersister) CreateIdentities(ctx context.Context, identities ...
}
}

if err := p.Transaction(ctx, func(ctx context.Context, tx *pop.Connection) error {
var succeededIDs []uuid.UUID

defer func() {
// Report succeeded identities as created.
for _, identID := range succeededIDs {
span.AddEvent(events.NewIdentityCreated(ctx, identID))
}
}()

return p.Transaction(ctx, func(ctx context.Context, tx *pop.Connection) error {
conn := &batch.TracerConnection{
Tracer: p.r.Tracer(ctx),
Connection: tx,
}

succeededIDs = make([]uuid.UUID, 0, len(identities))
failedIdentityIDs := make(map[uuid.UUID]struct{})

// Don't use batch.WithPartialInserts, because identities have no other
// constraints other than the primary key that could cause conflicts.
if err := batch.Create(ctx, conn, identities); err != nil {
Expand All @@ -598,8 +610,6 @@ func (p *IdentityPersister) CreateIdentities(ctx context.Context, identities ...

p.normalizeAllAddressess(ctx, identities...)

failedIdentityIDs := make(map[uuid.UUID]struct{})

if err = p.createVerifiableAddresses(ctx, tx, identities...); err != nil {
if paritalErr := new(batch.PartialConflictError[identity.VerifiableAddress]); errors.As(err, &paritalErr) {
for _, k := range paritalErr.Failed {
Expand Down Expand Up @@ -645,32 +655,31 @@ func (p *IdentityPersister) CreateIdentities(ctx context.Context, identities ...
if len(failedIdentityIDs) > 0 {
partialErr := &identity.CreateIdentitiesError{}
failedIDs := make([]uuid.UUID, 0, len(failedIdentityIDs))

for _, ident := range identities {
if _, ok := failedIdentityIDs[ident.ID]; ok {
partialErr.AddFailedIdentity(ident, sqlcon.ErrUniqueViolation)
failedIDs = append(failedIDs, ident.ID)
} else {
succeededIDs = append(succeededIDs, ident.ID)
}
}
// Manually roll back by deleting the identities that were inserted before the
// error occurred.
if err := p.DeleteIdentities(ctx, failedIDs); err != nil {
return sqlcon.HandleError(err)
}
// Wrap the partial error with the first error that occurred, so that the caller
// can continue to handle the error either as a partial error or a full error.

return partialErr
} else {
// No failures: report all identities as created.
for _, ident := range identities {
succeededIDs = append(succeededIDs, ident.ID)
}
}

return nil
}); err != nil {
return err
}

for _, ident := range identities {
span.AddEvent(events.NewIdentityCreated(ctx, ident.ID))
}

return nil
})
}

func (p *IdentityPersister) HydrateIdentityAssociations(ctx context.Context, i *identity.Identity, expand identity.Expandables) (err error) {
Expand Down
Loading