Skip to content

Commit

Permalink
move lock to service
Browse files Browse the repository at this point in the history
  • Loading branch information
korotkov-aerospike committed Dec 29, 2024
1 parent d7511ea commit 14d871d
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 76 deletions.
3 changes: 1 addition & 2 deletions cmd/backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,12 @@ func startService(configFile string, remote bool) error {

configApplier := service.NewDefaultConfigApplier(
scheduler,
config,
backends,
clientManager,
backupHandlers,
)

err = configApplier.ApplyNewConfig(ctx)
err = configApplier.ApplyNewConfig(ctx, config)
if err != nil {
return fmt.Errorf("failed to apply new config: %w", err)
}
Expand Down
15 changes: 15 additions & 0 deletions internal/server/handlers/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func (s *Service) GetIncrementalBackupsForRoutine(w http.ResponseWriter, r *http
}

func (s *Service) readAllBackups(w http.ResponseWriter, r *http.Request, isFullBackup bool) {
s.RLock()
defer s.RUnlock()

hLogger := s.logger.With(slog.String("handler", "readAllBackups"))

from := r.URL.Query().Get("from")
Expand Down Expand Up @@ -125,6 +128,9 @@ func (s *Service) readAllBackups(w http.ResponseWriter, r *http.Request, isFullB

//nolint:funlen // Function is long because of logging.
func (s *Service) readBackupsForRoutine(w http.ResponseWriter, r *http.Request, isFullBackup bool) {
s.RLock()
defer s.RUnlock()

hLogger := s.logger.With(slog.String("handler", "readBackupsForRoutine"))

from := r.URL.Query().Get("from")
Expand Down Expand Up @@ -231,6 +237,9 @@ func backupsReadFunction(
// @Failure 404 {string} string
// @Failure 500 {string} string
func (s *Service) ScheduleFullBackup(w http.ResponseWriter, r *http.Request) {
s.RLock()
defer s.RUnlock()

hLogger := s.logger.With(slog.String("handler", "ScheduleFullBackup"))

routineName := mux.Vars(r)["name"]
Expand Down Expand Up @@ -293,6 +302,9 @@ func (s *Service) ScheduleFullBackup(w http.ResponseWriter, r *http.Request) {
// @Failure 400 {string} string
// @Failure 500 {string} string
func (s *Service) GetCurrentBackupInfo(w http.ResponseWriter, r *http.Request) {
s.RLock()
defer s.RUnlock()

hLogger := s.logger.With(slog.String("handler", "GetCurrentBackupInfo"))

routineName := mux.Vars(r)["name"]
Expand Down Expand Up @@ -342,6 +354,9 @@ func (s *Service) GetCurrentBackupInfo(w http.ResponseWriter, r *http.Request) {
// @Failure 404 {string} string
// @Failure 500 {string} string
func (s *Service) CancelCurrentBackup(w http.ResponseWriter, r *http.Request) {
s.RLock()
defer s.RUnlock()

hLogger := s.logger.With(slog.String("handler", "CancelCurrentBackup"))

routineName := mux.Vars(r)["name"]
Expand Down
28 changes: 14 additions & 14 deletions internal/server/handlers/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func (s *Service) ConfigActionHandler(w http.ResponseWriter, r *http.Request) {
// @Success 200 {object} dto.Config
// @Failure 500 {string} string
func (s *Service) readConfig(w http.ResponseWriter) {
s.RLock()
defer s.RUnlock()

hLogger := s.logger.With(slog.String("handler", "readConfig"))

configuration, err := dto.Serialize(dto.NewConfigFromModel(s.config), dto.JSON)
Expand Down Expand Up @@ -60,6 +63,9 @@ func (s *Service) readConfig(w http.ResponseWriter) {
// @Success 200
// @Failure 400 {string} string
func (s *Service) updateConfig(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()

hLogger := s.logger.With(slog.String("handler", "updateConfig"))

newConfig, err := dto.NewConfigFromReader(r.Body, dto.JSON)
Expand Down Expand Up @@ -101,6 +107,9 @@ func (s *Service) updateConfig(w http.ResponseWriter, r *http.Request) {
// @Success 200
// @Failure 400 {string} string
func (s *Service) ApplyConfig(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()

hLogger := s.logger.With(slog.String("handler", "ApplyConfig"))

config, err := s.configurationManager.Read(r.Context())
Expand All @@ -112,6 +121,7 @@ func (s *Service) ApplyConfig(w http.ResponseWriter, r *http.Request) {
return
}

// validate static fields.
newConfig := dto.NewConfigFromModel(s.config)
oldConfig := dto.NewConfigFromModel(config)
if err := validation.ValidateStaticFieldChanges(oldConfig, newConfig); err != nil {
Expand All @@ -123,7 +133,9 @@ func (s *Service) ApplyConfig(w http.ResponseWriter, r *http.Request) {
return
}

err = s.applyConfig(r.Context(), config)
s.config.CopyFrom(config)
err = s.configApplier.ApplyNewConfig(r.Context(), s.config)

if err != nil {
hLogger.Error("failed to apply config",
slog.Any("error", err),
Expand All @@ -136,9 +148,6 @@ func (s *Service) ApplyConfig(w http.ResponseWriter, r *http.Request) {
}

func (s *Service) changeConfig(ctx context.Context, updateFunc func(*model.Config) error) error {
s.Lock()
defer s.Unlock()

err := updateFunc(s.config)
if err != nil {
return fmt.Errorf("cannot update configuration: %w", err)
Expand All @@ -149,19 +158,10 @@ func (s *Service) changeConfig(ctx context.Context, updateFunc func(*model.Confi
return fmt.Errorf("failed to write configuration: %w", err)
}

err = s.configApplier.ApplyNewConfig(ctx)
err = s.configApplier.ApplyNewConfig(ctx, s.config)
if err != nil {
return fmt.Errorf("failed to apply new configuration: %w", err)
}

return nil
}

func (s *Service) applyConfig(ctx context.Context, c *model.Config) error {
s.Lock()
defer s.Unlock()

s.config.CopyFrom(c)

return s.configApplier.ApplyNewConfig(ctx)
}
17 changes: 17 additions & 0 deletions internal/server/handlers/config_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func (s *Service) ConfigClusterActionHandler(w http.ResponseWriter, r *http.Requ
// @Failure 400 {string} string
// @Failure 500 {string} string
func (s *Service) addAerospikeCluster(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()

hLogger := s.logger.With(slog.String("handler", "addAerospikeCluster"))

newCluster, err := dto.NewClusterFromReader(r.Body, dto.JSON)
Expand Down Expand Up @@ -87,6 +90,9 @@ func (s *Service) addAerospikeCluster(w http.ResponseWriter, r *http.Request) {
// @Success 200 {object} map[string]dto.AerospikeCluster
// @Failure 500 {string} string
func (s *Service) ReadAerospikeClusters(w http.ResponseWriter, _ *http.Request) {
s.RLock()
defer s.RUnlock()

hLogger := s.logger.With(slog.String("handler", "ReadAerospikeClusters"))

toDTO := dto.ConvertModelMapToDTO(s.config.AerospikeClusters, func(m *model.AerospikeCluster) *dto.AerospikeCluster {
Expand Down Expand Up @@ -124,6 +130,9 @@ func (s *Service) ReadAerospikeClusters(w http.ResponseWriter, _ *http.Request)
// @Failure 404 {string} string "The specified cluster could not be found"
// @Failure 500 {string} string "The specified cluster could not be found"
func (s *Service) readAerospikeCluster(w http.ResponseWriter, r *http.Request) {
s.RLock()
defer s.RUnlock()

hLogger := s.logger.With(slog.String("handler", "readAerospikeCluster"))

clusterName := mux.Vars(r)["name"]
Expand Down Expand Up @@ -171,6 +180,9 @@ func (s *Service) readAerospikeCluster(w http.ResponseWriter, r *http.Request) {
// @Success 200
// @Failure 400 {string} string
func (s *Service) updateAerospikeCluster(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()

hLogger := s.logger.With(slog.String("handler", "updateAerospikeCluster"))

updatedCluster, err := dto.NewClusterFromReader(r.Body, dto.JSON)
Expand Down Expand Up @@ -227,7 +239,12 @@ func (s *Service) updateAerospikeCluster(w http.ResponseWriter, r *http.Request)
// @Param name path string true "Aerospike cluster name"
// @Success 204
// @Failure 400 {string} string
//
//nolint:dupl
func (s *Service) deleteAerospikeCluster(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()

hLogger := s.logger.With(slog.String("handler", "deleteAerospikeCluster"))

clusterName := mux.Vars(r)["name"]
Expand Down
19 changes: 19 additions & 0 deletions internal/server/handlers/config_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (s *Service) ConfigPolicyActionHandler(w http.ResponseWriter, r *http.Reque
//
//nolint:dupl
func (s *Service) addPolicy(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()

hLogger := s.logger.With(slog.String("handler", "addPolicy"))

newPolicy, err := dto.NewBackupPolicyFromReader(r.Body, dto.JSON)
Expand Down Expand Up @@ -78,7 +81,12 @@ func (s *Service) addPolicy(w http.ResponseWriter, r *http.Request) {
// @Produce json
// @Success 200 {object} map[string]dto.BackupPolicy
// @Failure 500 {string} string
//
//nolint:dupl
func (s *Service) ReadPolicies(w http.ResponseWriter, _ *http.Request) {
s.RLock()
defer s.RUnlock()

hLogger := s.logger.With(slog.String("handler", "ReadPolicies"))

policies := dto.ConvertModelMapToDTO(s.config.BackupPolicies, dto.NewBackupPolicyFromModel)
Expand Down Expand Up @@ -113,6 +121,9 @@ func (s *Service) ReadPolicies(w http.ResponseWriter, _ *http.Request) {
// @Failure 404 {string} string "The specified policy could not be found"
// @Failure 500 {string} string "The specified policy could not be found"
func (s *Service) readPolicy(w http.ResponseWriter, r *http.Request) {
s.RLock()
defer s.Unlock()

hLogger := s.logger.With(slog.String("handler", "readPolicy"))

policyName := mux.Vars(r)["name"]
Expand Down Expand Up @@ -159,6 +170,9 @@ func (s *Service) readPolicy(w http.ResponseWriter, r *http.Request) {
//
//nolint:dupl
func (s *Service) updatePolicy(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()

hLogger := s.logger.With(slog.String("handler", "updatePolicy"))

updatedPolicy, err := dto.NewBackupPolicyFromReader(r.Body, dto.JSON)
Expand Down Expand Up @@ -200,7 +214,12 @@ func (s *Service) updatePolicy(w http.ResponseWriter, r *http.Request) {
// @Param name path string true "Backup policy name"
// @Success 204
// @Failure 400 {string} string
//
//nolint:dupl
func (s *Service) deletePolicy(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()

hLogger := s.logger.With(slog.String("handler", "deletePolicy"))

policyName := mux.Vars(r)["name"]
Expand Down
23 changes: 23 additions & 0 deletions internal/server/handlers/config_routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (s *Service) ConfigRoutineActionHandler(w http.ResponseWriter, r *http.Requ
//
//nolint:dupl
func (s *Service) addRoutine(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()

hLogger := s.logger.With(slog.String("handler", "addRoutine"))

newRoutine, err := dto.NewRoutineFromReader(r.Body, dto.JSON)
Expand Down Expand Up @@ -89,6 +92,9 @@ func (s *Service) addRoutine(w http.ResponseWriter, r *http.Request) {
// @Success 200 {object} map[string]dto.BackupRoutine
// @Failure 400 {string} string
func (s *Service) ReadRoutines(w http.ResponseWriter, _ *http.Request) {
s.RLock()
defer s.RUnlock()

hLogger := s.logger.With(slog.String("handler", "ReadRoutines"))

toDTO := dto.ConvertModelMapToDTO(s.config.BackupRoutines, func(m *model.BackupRoutine) *dto.BackupRoutine {
Expand Down Expand Up @@ -127,6 +133,9 @@ func (s *Service) ReadRoutines(w http.ResponseWriter, _ *http.Request) {
//
//nolint:dupl
func (s *Service) readRoutine(w http.ResponseWriter, r *http.Request) {
s.RLock()
defer s.RUnlock()

hLogger := s.logger.With(slog.String("handler", "readRoutine"))

routineName := mux.Vars(r)["name"]
Expand Down Expand Up @@ -172,6 +181,9 @@ func (s *Service) readRoutine(w http.ResponseWriter, r *http.Request) {
//
//nolint:dupl
func (s *Service) updateRoutine(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()

hLogger := s.logger.With(slog.String("handler", "updateRoutine"))

updatedRoutine, err := dto.NewRoutineFromReader(r.Body, dto.JSON)
Expand Down Expand Up @@ -223,7 +235,12 @@ func (s *Service) updateRoutine(w http.ResponseWriter, r *http.Request) {
// @Param name path string true "Backup routine name"
// @Success 204
// @Failure 400 {string} string
//
//nolint:dupl
func (s *Service) deleteRoutine(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()

hLogger := s.logger.With(slog.String("handler", "deleteRoutine"))

routineName := mux.Vars(r)["name"]
Expand Down Expand Up @@ -257,6 +274,9 @@ func (s *Service) deleteRoutine(w http.ResponseWriter, r *http.Request) {
// @Failure 404 {string} string
// @Router /v1/config/routines/{name}/enable [put]
func (s *Service) EnableRoutine(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()

hLogger := s.logger.With(slog.String("handler", "enableRoutine"))

routineName := mux.Vars(r)["name"]
Expand Down Expand Up @@ -297,6 +317,9 @@ func (s *Service) EnableRoutine(w http.ResponseWriter, r *http.Request) {
// @Failure 500 {string} string "Unexpected error occurred."
// @Router /v1/config/routines/{name}/disable [put]
func (s *Service) DisableRoutine(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()

hLogger := s.logger.With(slog.String("handler", "disableRoutine"))

routineName := mux.Vars(r)["name"]
Expand Down
19 changes: 19 additions & 0 deletions internal/server/handlers/config_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func (s *Service) ConfigStorageActionHandler(w http.ResponseWriter, r *http.Requ
// @Success 201
// @Failure 400 {string} string
func (s *Service) addStorage(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()

hLogger := s.logger.With(slog.String("handler", "addStorage"))

newStorage, err := dto.NewStorageFromReader(r.Body, dto.JSON)
Expand Down Expand Up @@ -79,7 +82,12 @@ func (s *Service) addStorage(w http.ResponseWriter, r *http.Request) {
// @Produce json
// @Success 200 {object} map[string]dto.Storage
// @Failure 500 {string} string
//
//nolint:dupl
func (s *Service) ReadAllStorage(w http.ResponseWriter, _ *http.Request) {
s.RLock()
defer s.RUnlock()

hLogger := s.logger.With(slog.String("handler", "ReadAllStorage"))

toDTO := dto.ConvertStorageMapToDTO(s.config.Storage, s.config)
Expand Down Expand Up @@ -116,6 +124,9 @@ func (s *Service) ReadAllStorage(w http.ResponseWriter, _ *http.Request) {
//
//nolint:dupl
func (s *Service) readStorage(w http.ResponseWriter, r *http.Request) {
s.RLock()
defer s.RUnlock()

hLogger := s.logger.With(slog.String("handler", "readStorage"))

storageName := mux.Vars(r)["name"]
Expand Down Expand Up @@ -160,6 +171,9 @@ func (s *Service) readStorage(w http.ResponseWriter, r *http.Request) {
// @Success 200
// @Failure 400 {string} string
func (s *Service) updateStorage(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()

hLogger := s.logger.With(slog.String("handler", "updateStorage"))

updatedStorage, err := dto.NewStorageFromReader(r.Body, dto.JSON)
Expand Down Expand Up @@ -203,7 +217,12 @@ func (s *Service) updateStorage(w http.ResponseWriter, r *http.Request) {
// @Param name path string true "Backup storage name"
// @Success 204
// @Failure 400 {string} string
//
//nolint:dupl
func (s *Service) deleteStorage(w http.ResponseWriter, r *http.Request) {
s.Lock()
defer s.Unlock()

hLogger := s.logger.With(slog.String("handler", "deleteStorage"))

storageName := mux.Vars(r)["name"]
Expand Down
2 changes: 1 addition & 1 deletion internal/server/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func (mock configurationManagerMock) Write(_ context.Context, config *model.Conf

type MockConfigApplier struct{}

func (a *MockConfigApplier) ApplyNewConfig(context.Context) error {
func (a *MockConfigApplier) ApplyNewConfig(_ context.Context, _ *model.Config) error {
return nil
}

Expand Down
Loading

0 comments on commit 14d871d

Please sign in to comment.