Skip to content

Commit

Permalink
EVEREST-788 Wait uploadInterval after backup (#237)
Browse files Browse the repository at this point in the history
* EVEREST-788 wait uploadInterval after backup

* EVEREST-788 consider custom upload interval

(cherry picked from commit 9404331)
  • Loading branch information
oksana-grishchenko committed Apr 26, 2024
1 parent 793b64b commit 87a06cd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
25 changes: 16 additions & 9 deletions api/database_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,27 +178,31 @@ func (e *EverestServer) GetDatabaseClusterPitr(ctx echo.Context, namespace, name

latestBackup := latestSuccessfulBackup(backups.Items, databaseCluster.Spec.Engine.Type)

uploadInterval := getDefaultUploadInterval(databaseCluster.Spec.Engine.Type)
uploadInterval := getDefaultUploadInterval(databaseCluster.Spec.Engine.Type, databaseCluster.Spec.Backup.PITR.UploadIntervalSec)
backupTime := latestBackup.Status.CreatedAt.UTC()
latest := latestRestorableDate(time.Now(), backupTime, uploadInterval)

response.LatestDate = &latest
response.EarliestDate = &backupTime
response.LatestDate = latest
if response.LatestDate != nil {
response.EarliestDate = &backupTime
}
response.LatestBackupName = &latestBackup.Name
response.Gaps = &latestBackup.Status.Gaps

return ctx.JSON(http.StatusOK, response)
}

func latestRestorableDate(now, latestBackupTime time.Time, uploadInterval int) time.Time {
func latestRestorableDate(now, latestBackupTime time.Time, uploadInterval int) *time.Time {
// delete nanoseconds since they're not accepted by restoration
now = now.Truncate(time.Duration(now.Nanosecond()) * time.Nanosecond)
// heuristic: latest restorable date is now minus uploadInterval
earliest := now.Add(-time.Duration(uploadInterval) * time.Second).UTC()
if latestBackupTime.After(earliest) {
return latestBackupTime
date := now.Add(-time.Duration(uploadInterval) * time.Second).UTC()
// not able to restore if after the latest backup passed less than uploadInterval time,
// so in that case return nil
if latestBackupTime.After(date) {
return nil
}
return earliest
return &date
}

func latestSuccessfulBackup(backups []everestv1alpha1.DatabaseClusterBackup, engineType everestv1alpha1.EngineType) *everestv1alpha1.DatabaseClusterBackup {
Expand Down Expand Up @@ -237,7 +241,10 @@ func successStatus(state everestv1alpha1.BackupState, engineType everestv1alpha1
return string(state) == successState
}

func getDefaultUploadInterval(engineType everestv1alpha1.EngineType) int {
func getDefaultUploadInterval(engineType everestv1alpha1.EngineType, uploadInterval *int) int {
if uploadInterval != nil {
return *uploadInterval
}
switch engineType {
case everestv1alpha1.DatabaseEnginePXC:
// PXC default upload interval
Expand Down
7 changes: 4 additions & 3 deletions api/database_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"github.com/AlekSi/pointer"
"github.com/stretchr/testify/require"
)

Expand All @@ -13,7 +14,7 @@ func TestLatestRestorableDate(t *testing.T) {
uploadInterval int
latestBackupTime time.Time
now time.Time
expected time.Time
expected *time.Time
name string
}

Expand All @@ -24,14 +25,14 @@ func TestLatestRestorableDate(t *testing.T) {
uploadInterval: 600,
latestBackupTime: now.Add(-300 * time.Second),
now: now,
expected: now.Add(-300 * time.Second),
expected: nil,
},
{
name: "backup 15 min ago, upload interval 10 min",
uploadInterval: 600,
latestBackupTime: now.Add(-900 * time.Second),
now: now,
expected: now.Add(-600 * time.Second),
expected: pointer.ToTime(now.Add(-600 * time.Second)),
},
}

Expand Down

0 comments on commit 87a06cd

Please sign in to comment.