Skip to content

Commit

Permalink
dont pass config to restore
Browse files Browse the repository at this point in the history
  • Loading branch information
korotkov-aerospike committed Dec 29, 2024
1 parent 2054c99 commit f8b1895
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 51 deletions.
2 changes: 1 addition & 1 deletion cmd/backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func startService(configFile string, remote bool) error {
service.NewMetricsCollector(backupHandlers, restoreJobs).Start(ctx, 1*time.Second)

restoreMgr := service.NewRestoreManager(
backends, config, service.NewRestore(), clientManager, restoreJobs, nsValidator)
backends, service.NewRestore(), clientManager, restoreJobs, nsValidator)

httpService := handlers.NewService(
config,
Expand Down
65 changes: 31 additions & 34 deletions pkg/model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// Config represents the service configuration.
type Config struct {
mu sync.Mutex
sync.Mutex
ServiceConfig BackupServiceConfig
AerospikeClusters map[string]*AerospikeCluster
Storage map[string]Storage // Storage is an interface
Expand All @@ -33,9 +33,6 @@ var (
)

func (c *Config) AddStorage(name string, s Storage) error {
c.mu.Lock()
defer c.mu.Unlock()

if _, exists := c.Storage[name]; exists {
return fmt.Errorf("add storage %q: %w", name, ErrAlreadyExists)
}
Expand All @@ -44,8 +41,8 @@ func (c *Config) AddStorage(name string, s Storage) error {
}

func (c *Config) DeleteStorage(name string) error {
c.mu.Lock()
defer c.mu.Unlock()
c.Lock()
defer c.Unlock()

s, exists := c.Storage[name]
if !exists {
Expand All @@ -59,8 +56,8 @@ func (c *Config) DeleteStorage(name string) error {
}

func (c *Config) UpdateStorage(name string, s Storage) error {
c.mu.Lock()
defer c.mu.Unlock()
c.Lock()
defer c.Unlock()

if _, exists := c.Storage[name]; !exists {
return fmt.Errorf("update storage %q: %w", name, ErrNotFound)
Expand Down Expand Up @@ -88,8 +85,8 @@ func (c *Config) routineUsesStorage(s Storage) string {
}

func (c *Config) AddPolicy(name string, p *BackupPolicy) error {
c.mu.Lock()
defer c.mu.Unlock()
c.Lock()
defer c.Unlock()

if _, exists := c.BackupPolicies[name]; exists {
return fmt.Errorf("add backup policy %q: %w", name, ErrAlreadyExists)
Expand All @@ -99,8 +96,8 @@ func (c *Config) AddPolicy(name string, p *BackupPolicy) error {
}

func (c *Config) DeletePolicy(name string) error {
c.mu.Lock()
defer c.mu.Unlock()
c.Lock()
defer c.Unlock()

p, exists := c.BackupPolicies[name]
if !exists {
Expand All @@ -114,8 +111,8 @@ func (c *Config) DeletePolicy(name string) error {
}

func (c *Config) UpdatePolicy(name string, p *BackupPolicy) error {
c.mu.Lock()
defer c.mu.Unlock()
c.Lock()
defer c.Unlock()

if _, exists := c.BackupPolicies[name]; !exists {
return fmt.Errorf("update backup policy %q: %w", name, ErrNotFound)
Expand All @@ -142,8 +139,8 @@ func (c *Config) routineUsesPolicy(p *BackupPolicy) string {
}

func (c *Config) AddRoutine(name string, r *BackupRoutine) error {
c.mu.Lock()
defer c.mu.Unlock()
c.Lock()
defer c.Unlock()

if _, exists := c.BackupRoutines[name]; exists {
return fmt.Errorf("add backup routine %q: %w", name, ErrAlreadyExists)
Expand All @@ -153,8 +150,8 @@ func (c *Config) AddRoutine(name string, r *BackupRoutine) error {
}

func (c *Config) DeleteRoutine(name string) error {
c.mu.Lock()
defer c.mu.Unlock()
c.Lock()
defer c.Unlock()

if _, exists := c.BackupRoutines[name]; !exists {
return fmt.Errorf("delete backup routine %q: %w", name, ErrNotFound)
Expand All @@ -164,8 +161,8 @@ func (c *Config) DeleteRoutine(name string) error {
}

func (c *Config) UpdateRoutine(name string, r *BackupRoutine) error {
c.mu.Lock()
defer c.mu.Unlock()
c.Lock()
defer c.Unlock()

if _, exists := c.BackupRoutines[name]; !exists {
return fmt.Errorf("update backup routine %q: %w", name, ErrNotFound)
Expand All @@ -175,8 +172,8 @@ func (c *Config) UpdateRoutine(name string, r *BackupRoutine) error {
}

func (c *Config) AddCluster(name string, cluster *AerospikeCluster) error {
c.mu.Lock()
defer c.mu.Unlock()
c.Lock()
defer c.Unlock()

if _, exists := c.AerospikeClusters[name]; exists {
return fmt.Errorf("add Aerospike cluster %q: %w", name, ErrAlreadyExists)
Expand All @@ -186,8 +183,8 @@ func (c *Config) AddCluster(name string, cluster *AerospikeCluster) error {
}

func (c *Config) DeleteCluster(name string) error {
c.mu.Lock()
defer c.mu.Unlock()
c.Lock()
defer c.Unlock()

cluster, exists := c.AerospikeClusters[name]
if !exists {
Expand All @@ -201,8 +198,8 @@ func (c *Config) DeleteCluster(name string) error {
}

func (c *Config) UpdateCluster(name string, cluster *AerospikeCluster) error {
c.mu.Lock()
defer c.mu.Unlock()
c.Lock()
defer c.Unlock()

if _, exists := c.AerospikeClusters[name]; !exists {
return fmt.Errorf("update Aerospike cluster %q: %w", name, ErrNotFound)
Expand Down Expand Up @@ -230,8 +227,8 @@ func (c *Config) routineUsesCluster(cluster *AerospikeCluster) string {
}

func (c *Config) AddSecretAgent(name string, agent *SecretAgent) error {
c.mu.Lock()
defer c.mu.Unlock()
c.Lock()
defer c.Unlock()

if _, exists := c.SecretAgents[name]; exists {
return fmt.Errorf("add Secret agent %q: %w", name, ErrAlreadyExists)
Expand All @@ -241,10 +238,10 @@ func (c *Config) AddSecretAgent(name string, agent *SecretAgent) error {
}

func (c *Config) CopyFrom(other *Config) {
c.mu.Lock()
other.mu.Lock()
defer c.mu.Unlock()
defer other.mu.Unlock()
c.Lock()
other.Lock()
defer c.Unlock()
defer other.Unlock()

c.AerospikeClusters = other.AerospikeClusters
c.Storage = other.Storage
Expand All @@ -268,8 +265,8 @@ func (c *Config) ResolveSecretAgent(name *string, defaultAgent *SecretAgent) (*S

// ToggleRoutineDisabled sets the Disabled field of the BackupRoutine based on the provided state.
func (c *Config) ToggleRoutineDisabled(name string, isDisabled bool) error {
c.mu.Lock()
defer c.mu.Unlock()
c.Lock()
defer c.Unlock()

_, exists := c.BackupRoutines[name]
if !exists {
Expand Down
2 changes: 2 additions & 0 deletions pkg/model/restore_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ func NewRestoreRequest(
policy *RestorePolicy,
sourceStorage Storage,
secretAgent *SecretAgent,
backupDataPath string,
) *RestoreRequest {
return &RestoreRequest{
DestinationCluster: destinationCluster,
Policy: policy,
SourceStorage: sourceStorage,
SecretAgent: secretAgent,
BackupDataPath: backupDataPath,
}
}
1 change: 1 addition & 0 deletions pkg/service/restore_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/aerospike/aerospike-backup-service/v3/pkg/util"
)

// configRetriever is used to read Aerospike configuration from backup.
type configRetriever struct {
backends BackendsHolder
}
Expand Down
25 changes: 9 additions & 16 deletions pkg/service/restore_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func NewErrJobNotFound(id model.RestoreJobID) *ErrJobNotFound {
// Stores job information locally within a map.
type dataRestorer struct {
configRetriever
config *model.Config
restoreJobs *RestoreJobsHolder
restoreService Restore
backends BackendsHolder
Expand All @@ -45,7 +44,6 @@ var _ RestoreManager = (*dataRestorer)(nil)

// NewRestoreManager returns a new dataRestorer instance.
func NewRestoreManager(backends BackendsHolder,
config *model.Config,
restoreService Restore,
clientManager aerospike.ClientManager,
restoreJobs *RestoreJobsHolder,
Expand All @@ -58,7 +56,6 @@ func NewRestoreManager(backends BackendsHolder,
restoreJobs: restoreJobs,
restoreService: restoreService,
backends: backends,
config: config,
clientManager: clientManager,
nsValidator: nsValidator,
}
Expand Down Expand Up @@ -205,7 +202,7 @@ func (r *dataRestorer) restoreNamespace(
continue
}

handler, err := r.restoreFromPath(ctx, client, request, b.Key)
handler, err := r.restoreFromPath(ctx, client, request, b.Key, fullBackup.Storage)
if err != nil {
return err
}
Expand All @@ -231,9 +228,15 @@ func (r *dataRestorer) restoreFromPath(
client *backup.Client,
request *model.RestoreTimestampRequest,
backupPath string,
storage model.Storage,
) (RestoreHandler, error) {
restoreRequest := r.toRestoreRequest(request)
restoreRequest.BackupDataPath = backupPath
restoreRequest := model.NewRestoreRequest(
request.DestinationCluster,
request.Policy,
storage,
request.SecretAgent,
backupPath,
)
handler, err := r.restoreService.Run(ctx, client, restoreRequest)
if err != nil {
return nil, fmt.Errorf("could not start restore from backup at %s: %w", backupPath, err)
Expand All @@ -242,16 +245,6 @@ func (r *dataRestorer) restoreFromPath(
return handler, nil
}

func (r *dataRestorer) toRestoreRequest(request *model.RestoreTimestampRequest) *model.RestoreRequest {
routine := r.config.BackupRoutines[request.RoutineName]
return model.NewRestoreRequest(
request.DestinationCluster,
request.Policy,
routine.Storage,
request.SecretAgent,
)
}

// JobStatus returns the status of the job with the given id.
func (r *dataRestorer) JobStatus(jobID model.RestoreJobID) (*model.RestoreJobStatus, error) {
return r.restoreJobs.getStatus(jobID)
Expand Down

0 comments on commit f8b1895

Please sign in to comment.