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

refactor: changed transaction handling related to Push package #1481

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
124 changes: 17 additions & 107 deletions pkg/push/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func WithLogger(l *zap.Logger) Option {

type PushService struct {
mysqlClient mysql.Client
pushStorage v2ps.PushStorage
featureClient featureclient.Client
experimentClient experimentclient.Client
accountClient accountclient.Client
Expand All @@ -86,6 +87,7 @@ func NewPushService(
}
return &PushService{
mysqlClient: mysqlClient,
pushStorage: v2ps.NewPushStorage(mysqlClient),
featureClient: featureClient,
experimentClient: experimentClient,
accountClient: accountClient,
Expand Down Expand Up @@ -183,26 +185,8 @@ func (s *PushService) CreatePush(
}
return nil, dt.Err()
}
tx, err := s.mysqlClient.BeginTx(ctx)
if err != nil {
s.logger.Error(
"Failed to begin transaction",
log.FieldsFromImcomingContext(ctx).AddFields(
zap.Error(err),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
}
err = s.mysqlClient.RunInTransaction(ctx, tx, func() error {
pushStorage := v2ps.NewPushStorage(tx)
if err := pushStorage.CreatePush(ctx, push, req.EnvironmentId); err != nil {
err = s.mysqlClient.RunInTransactionV2(ctx, func(contextWithTx context.Context, _ mysql.Transaction) error {
if err := s.pushStorage.CreatePush(contextWithTx, push, req.EnvironmentId); err != nil {
return err
}
handler, err := command.NewPushCommandHandler(editor, push, s.publisher, req.EnvironmentId)
Expand Down Expand Up @@ -330,26 +314,8 @@ func (s *PushService) createPushNoCommand(
}

var event *eventproto.Event
tx, err := s.mysqlClient.BeginTx(ctx)
if err != nil {
s.logger.Error(
"Failed to begin transaction",
log.FieldsFromImcomingContext(ctx).AddFields(
zap.Error(err),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
}
err = s.mysqlClient.RunInTransaction(ctx, tx, func() error {
pushStorage := v2ps.NewPushStorage(tx)
if err := pushStorage.CreatePush(ctx, push, req.EnvironmentId); err != nil {
err = s.mysqlClient.RunInTransactionV2(ctx, func(contextWithTx context.Context, _ mysql.Transaction) error {
if err := s.pushStorage.CreatePush(contextWithTx, push, req.EnvironmentId); err != nil {
return err
}
prev := &domain.Push{}
Expand Down Expand Up @@ -506,26 +472,8 @@ func (s *PushService) UpdatePush(

var updatedPushPb *pushproto.Push
commands := s.createUpdatePushCommands(req)
tx, err := s.mysqlClient.BeginTx(ctx)
if err != nil {
s.logger.Error(
"Failed to begin transaction",
log.FieldsFromImcomingContext(ctx).AddFields(
zap.Error(err),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
}
err = s.mysqlClient.RunInTransaction(ctx, tx, func() error {
pushStorage := v2ps.NewPushStorage(tx)
push, err := pushStorage.GetPush(ctx, req.Id, req.EnvironmentId)
err = s.mysqlClient.RunInTransactionV2(ctx, func(contextWithTx context.Context, _ mysql.Transaction) error {
push, err := s.pushStorage.GetPush(contextWithTx, req.Id, req.EnvironmentId)
if err != nil {
return err
}
Expand All @@ -539,7 +487,7 @@ func (s *PushService) UpdatePush(
}
}
updatedPushPb = push.Push
return pushStorage.UpdatePush(ctx, push, req.EnvironmentId)
return s.pushStorage.UpdatePush(contextWithTx, push, req.EnvironmentId)
})
if err != nil {
switch {
Expand Down Expand Up @@ -599,26 +547,8 @@ func (s *PushService) updatePushNoCommand(
}
var updatedPushPb *pushproto.Push
var updatePushEvent *eventproto.Event
tx, err := s.mysqlClient.BeginTx(ctx)
if err != nil {
s.logger.Error(
"Failed to begin transaction",
log.FieldsFromImcomingContext(ctx).AddFields(
zap.Error(err),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
}
err = s.mysqlClient.RunInTransaction(ctx, tx, func() error {
pushStorage := v2ps.NewPushStorage(tx)
push, err := pushStorage.GetPush(ctx, req.Id, req.EnvironmentId)
err := s.mysqlClient.RunInTransactionV2(ctx, func(contextWithTx context.Context, _ mysql.Transaction) error {
push, err := s.pushStorage.GetPush(contextWithTx, req.Id, req.EnvironmentId)
if err != nil {
return err
}
Expand Down Expand Up @@ -648,7 +578,7 @@ func (s *PushService) updatePushNoCommand(
}
updatedPushPb = updated.Push

return pushStorage.UpdatePush(ctx, updated, req.EnvironmentId)
return s.pushStorage.UpdatePush(contextWithTx, updated, req.EnvironmentId)
})
if err != nil {
switch {
Expand Down Expand Up @@ -842,26 +772,8 @@ func (s *PushService) DeletePush(
}

var event *eventproto.Event
tx, err := s.mysqlClient.BeginTx(ctx)
if err != nil {
s.logger.Error(
"Failed to begin transaction",
log.FieldsFromImcomingContext(ctx).AddFields(
zap.Error(err),
)...,
)
dt, err := statusInternal.WithDetails(&errdetails.LocalizedMessage{
Locale: localizer.GetLocale(),
Message: localizer.MustLocalize(locale.InternalServerError),
})
if err != nil {
return nil, statusInternal.Err()
}
return nil, dt.Err()
}
err = s.mysqlClient.RunInTransaction(ctx, tx, func() error {
pushStorage := v2ps.NewPushStorage(tx)
push, err := pushStorage.GetPush(ctx, req.Id, req.EnvironmentId)
err = s.mysqlClient.RunInTransactionV2(ctx, func(contextWithTx context.Context, _ mysql.Transaction) error {
push, err := s.pushStorage.GetPush(contextWithTx, req.Id, req.EnvironmentId)
if err != nil {
return err
}
Expand Down Expand Up @@ -890,7 +802,7 @@ func (s *PushService) DeletePush(
if err = s.publisher.Publish(ctx, event); err != nil {
return err
}
return pushStorage.UpdatePush(ctx, push, req.EnvironmentId)
return s.pushStorage.UpdatePush(contextWithTx, push, req.EnvironmentId)
})
if err != nil {
switch {
Expand Down Expand Up @@ -935,8 +847,7 @@ func (s *PushService) GetPush(
return nil, err
}

pushStorage := v2ps.NewPushStorage(s.mysqlClient)
push, err := pushStorage.GetPush(ctx, req.Id, req.EnvironmentId)
push, err := s.pushStorage.GetPush(ctx, req.Id, req.EnvironmentId)
if err != nil {
if errors.Is(err, v2ps.ErrPushNotFound) {
dt, err := statusNotFound.WithDetails(&errdetails.LocalizedMessage{
Expand Down Expand Up @@ -1266,8 +1177,7 @@ func (s *PushService) listPushes(
}
return nil, "", 0, dt.Err()
}
pushStorage := v2ps.NewPushStorage(s.mysqlClient)
pushes, nextCursor, totalCount, err := pushStorage.ListPushes(
pushes, nextCursor, totalCount, err := s.pushStorage.ListPushes(
ctx,
whereParts,
orders,
Expand Down
Loading
Loading