Skip to content

Commit

Permalink
make fields private
Browse files Browse the repository at this point in the history
  • Loading branch information
korotkov-aerospike committed Dec 25, 2024
1 parent 611c884 commit 26d1482
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 30 deletions.
4 changes: 2 additions & 2 deletions pkg/dto/current_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func NewCurrentBackupsFromModel(m *model.CurrentBackups) *CurrentBackups {
func (c *CurrentBackups) fromModel(m *model.CurrentBackups) {
c.Full = NewRunningJobFromModel(m.Full)
c.Incremental = NewRunningJobFromModel(m.Incremental)
c.LastFull = m.LastRunTime.Full
c.LastIncremental = m.LastRunTime.Incremental
c.LastFull = m.LastRunTime.FullBackupTime()
c.LastIncremental = m.LastRunTime.IncrementalBackupTime()
}

// RunningJob tracks progress of currently running job.
Expand Down
34 changes: 23 additions & 11 deletions pkg/model/last_backup_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,54 @@ import (
type LastBackupRun struct {
mu sync.RWMutex
// Last time the Full backup was performed.
Full *time.Time
full *time.Time
// Last time the Incremental backup was performed.
Incremental *time.Time
incremental *time.Time
}

func NewLastRun(lastFullBackup *time.Time, lastIncrBackup *time.Time) *LastBackupRun {
func NewLastBackupRun(lastFullBackup *time.Time, lastIncrBackup *time.Time) *LastBackupRun {
return &LastBackupRun{
Full: lastFullBackup,
Incremental: lastIncrBackup,
full: lastFullBackup,
incremental: lastIncrBackup,
}
}

func (r *LastBackupRun) NoFullBackup() bool {
r.mu.RLock()
defer r.mu.RUnlock()
return r.Full == nil
return r.full == nil
}

func (r *LastBackupRun) LatestRun() *time.Time {
r.mu.RLock()
defer r.mu.RUnlock()

if r.Incremental != nil && r.Full != nil && r.Incremental.After(*r.Full) {
return r.Incremental
if r.incremental != nil && r.full != nil && r.incremental.After(*r.full) {
return r.incremental
}
return r.Full
return r.full
}

func (r *LastBackupRun) SetFullBackupTime(t *time.Time) {
r.mu.Lock()
defer r.mu.Unlock()
r.Full = t
r.full = t
}

func (r *LastBackupRun) SetIncrementalBackupTime(t *time.Time) {
r.mu.Lock()
defer r.mu.Unlock()
r.Incremental = t
r.incremental = t
}

func (r *LastBackupRun) FullBackupTime() *time.Time {
r.mu.RLock()
defer r.mu.RUnlock()
return r.full
}

func (r *LastBackupRun) IncrementalBackupTime() *time.Time {
r.mu.RLock()
defer r.mu.RUnlock()
return r.incremental
}
2 changes: 1 addition & 1 deletion pkg/service/backup_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (b *BackupBackend) findLastRun(ctx context.Context) *model.LastBackupRun {
incrementalBackupList, _ := b.IncrementalBackupList(ctx, model.TimeBounds{FromTime: lastFullBackup})
lastIncrBackup := lastBackupTime(incrementalBackupList)

return model.NewLastRun(lastFullBackup, lastIncrBackup)
return model.NewLastBackupRun(lastFullBackup, lastIncrBackup)
}

func lastBackupTime(b []model.BackupDetails) *time.Time {
Expand Down
18 changes: 5 additions & 13 deletions pkg/service/backup_routine_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,7 @@ func TestRunIncrementalBackup_SkipIfFullBackupInProgress(t *testing.T) {
retentionManager := new(mockRetentionManager)

handler := setupTestHandler(backupService, clientManager, metadataWriter, configWriter, retentionManager)
handler.lastRun = &model.LastBackupRun{
Full: util.Ptr(time.Now()), // Set last full run
}
handler.lastRun = model.NewLastBackupRun(util.Ptr(time.Now()), nil)

handler.fullBackupHandlers["ns1"] = &mockBackupHandler{}

Expand All @@ -259,9 +257,7 @@ func TestRunIncrementalBackup_SkipIfIncrementalBackupInProgress(t *testing.T) {
retentionManager := new(mockRetentionManager)

handler := setupTestHandler(backupService, clientManager, metadataWriter, configWriter, retentionManager)
handler.lastRun = &model.LastBackupRun{
Full: util.Ptr(time.Now()), // Set last full run
}
handler.lastRun = model.NewLastBackupRun(util.Ptr(time.Now()), nil)

handler.incrBackupHandlers["test"] = &mockBackupHandler{}

Expand All @@ -279,9 +275,7 @@ func TestRunIncrementalBackup_ClientError(t *testing.T) {
retentionManager := new(mockRetentionManager)

handler := setupTestHandler(backupService, clientManager, metadataWriter, configWriter, retentionManager)
handler.lastRun = &model.LastBackupRun{
Full: util.Ptr(time.Now()),
}
handler.lastRun = model.NewLastBackupRun(util.Ptr(time.Now()), nil)

expectedErr := errors.New("client error")
clientManager.On("GetClient", mock.Anything).Return(nil, expectedErr)
Expand All @@ -302,9 +296,7 @@ func TestRunIncrementalBackup_Success(t *testing.T) {
handler := setupTestHandler(backupService, clientManager, metadataWriter, configWriter, retentionManager)
now := time.Now()
lastRun := now.Add(-1 * time.Hour)
handler.lastRun = &model.LastBackupRun{
Full: &lastRun,
}
handler.lastRun = model.NewLastBackupRun(&lastRun, nil)

backupHandler := new(mockBackupHandler)
stats := &models.BackupStats{}
Expand Down Expand Up @@ -347,7 +339,7 @@ func TestRunIncrementalBackup_Success(t *testing.T) {
clientManager.AssertExpectations(t)
backupService.AssertExpectations(t)
backupHandler.AssertExpectations(t)
assert.Equal(t, now, *handler.CurrentStat().LastRunTime.Incremental)
assert.Equal(t, now, *handler.CurrentStat().LastRunTime.IncrementalBackupTime())
}

func TestRunFullBackup_PartialFailure(t *testing.T) {
Expand Down
4 changes: 1 addition & 3 deletions pkg/service/backup_scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ func TestDisabledRoutine(t *testing.T) {

handlers := BackupHandlerHolder{
"routine1": &BackupRoutineHandler{},
"routine2": &BackupRoutineHandler{lastRun: &model.LastBackupRun{
Full: util.Ptr(time.Now()),
}},
"routine2": &BackupRoutineHandler{lastRun: model.NewLastBackupRun(util.Ptr(time.Now()), nil)},
}

err := scheduleRoutines(mockScheduler, config.BackupRoutines, handlers)
Expand Down

0 comments on commit 26d1482

Please sign in to comment.