Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Change Log

## [master](https://github.com/arangodb/kube-arangodb/tree/master) (N/A)
- Add ArangoBackup backoff functionality

## [1.2.5](https://github.com/arangodb/kube-arangodb/tree/1.2.5) (2021-10-25)
- Split & Unify Lifecycle management functionality
Expand Down
2 changes: 0 additions & 2 deletions pkg/apis/backup/v1/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Adam Janikowski
//

package v1

Expand Down
2 changes: 0 additions & 2 deletions pkg/apis/backup/v1/backup_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Adam Janikowski
//

package v1

Expand Down
2 changes: 0 additions & 2 deletions pkg/apis/backup/v1/backup_policy_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Adam Janikowski
//

package v1

Expand Down
2 changes: 0 additions & 2 deletions pkg/apis/backup/v1/backup_policy_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Adam Janikowski
//

package v1

Expand Down
2 changes: 0 additions & 2 deletions pkg/apis/backup/v1/backup_policy_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Adam Janikowski
//

package v1

Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/backup/v1/backup_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Adam Janikowski
//

package v1

Expand All @@ -35,6 +33,8 @@ type ArangoBackupSpec struct {
Upload *ArangoBackupSpecOperation `json:"upload,omitempty"`

PolicyName *string `json:"policyName,omitempty"`

Backoff *ArangoBackupSpecBackOff `json:"backoff,omitempty"`
}

type ArangoBackupSpecDeployment struct {
Expand Down
92 changes: 92 additions & 0 deletions pkg/apis/backup/v1/backup_spec_backoff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//
// DISCLAIMER
//
// Copyright 2016-2021 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package v1

import "time"

type ArangoBackupSpecBackOff struct {
// MinDelay defines minimum delay in seconds. Default to 30
MinDelay *int `json:"min_delay,omitempty"`
// MaxDelay defines maximum delay in seconds. Default to 600
MaxDelay *int `json:"max_delay,omitempty"`
// Iterations defines number of iterations before reaching MaxDelay. Default to 5
Iterations *int `json:"iterations,omitempty"`
}

func (a *ArangoBackupSpecBackOff) GetMaxDelay() int {
if a == nil || a.MaxDelay == nil {
return 600
}

v := *a.MaxDelay

if v < 0 {
return 0
}

return v
}

func (a *ArangoBackupSpecBackOff) GetMinDelay() int {
if a == nil || a.MinDelay == nil {
return 30
}

v := *a.MinDelay

if v < 0 {
return 0
}

if m := a.GetMaxDelay(); m < v {
return m
}

return v
}

func (a *ArangoBackupSpecBackOff) GetIterations() int {
if a == nil || a.Iterations == nil {
return 5
}

v := *a.Iterations

if v < 1 {
return 1
}

return v
}

func (a *ArangoBackupSpecBackOff) Backoff(iteration int) time.Duration {
if maxIterations := a.GetIterations(); maxIterations <= iteration {
return time.Duration(a.GetMaxDelay()) * time.Second
} else {
min, max := a.GetMinDelay(), a.GetMaxDelay()

if min == max {
return time.Duration(min) * time.Second
}

return time.Duration(min+int(float64(iteration)/float64(maxIterations)*float64(max-min))) * time.Second
}
}
89 changes: 89 additions & 0 deletions pkg/apis/backup/v1/backup_spec_backoff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// DISCLAIMER
//
// Copyright 2016-2021 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package v1

import (
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/arangodb/kube-arangodb/pkg/util"
)

func TestArangoBackupSpecBackOff_Backoff(t *testing.T) {
t.Run("Default", func(t *testing.T) {
var b *ArangoBackupSpecBackOff

assert.Equal(t, 30*time.Second, b.Backoff(0))
assert.Equal(t, 144*time.Second, b.Backoff(1))
assert.Equal(t, 258*time.Second, b.Backoff(2))
assert.Equal(t, 372*time.Second, b.Backoff(3))
assert.Equal(t, 486*time.Second, b.Backoff(4))
assert.Equal(t, 600*time.Second, b.Backoff(5))
assert.Equal(t, 600*time.Second, b.Backoff(6))
})
t.Run("Custom", func(t *testing.T) {
b := &ArangoBackupSpecBackOff{
MinDelay: util.NewInt(20),
MaxDelay: util.NewInt(120),
Iterations: util.NewInt(10),
}

assert.Equal(t, 20*time.Second, b.Backoff(0))
assert.Equal(t, 30*time.Second, b.Backoff(1))
assert.Equal(t, 40*time.Second, b.Backoff(2))
assert.Equal(t, 50*time.Second, b.Backoff(3))
assert.Equal(t, 60*time.Second, b.Backoff(4))
assert.Equal(t, 70*time.Second, b.Backoff(5))
assert.Equal(t, 80*time.Second, b.Backoff(6))
assert.Equal(t, 90*time.Second, b.Backoff(7))
assert.Equal(t, 100*time.Second, b.Backoff(8))
assert.Equal(t, 110*time.Second, b.Backoff(9))
assert.Equal(t, 120*time.Second, b.Backoff(10))
assert.Equal(t, 120*time.Second, b.Backoff(11))
})

t.Run("Invalid", func(t *testing.T) {
b := &ArangoBackupSpecBackOff{
MinDelay: util.NewInt(-1),
MaxDelay: util.NewInt(-1),
Iterations: util.NewInt(0),
}

assert.Equal(t, 0, b.GetMinDelay())
assert.Equal(t, 0, b.GetMaxDelay())
assert.Equal(t, 1, b.GetIterations())

assert.Equal(t, 0*time.Second, b.Backoff(12345))
})

t.Run("Max < Min", func(t *testing.T) {
b := &ArangoBackupSpecBackOff{
MinDelay: util.NewInt(50),
MaxDelay: util.NewInt(20),
}

assert.Equal(t, 20, b.GetMinDelay())
assert.Equal(t, 20, b.GetMaxDelay())
assert.Equal(t, 5, b.GetIterations())
})
}
2 changes: 0 additions & 2 deletions pkg/apis/backup/v1/backup_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Adam Janikowski
//

package v1

Expand Down
7 changes: 3 additions & 4 deletions pkg/apis/backup/v1/backup_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Adam Janikowski
//

package v1

Expand All @@ -31,8 +29,9 @@ import (
// an ArangoBackup.
type ArangoBackupStatus struct {
ArangoBackupState `json:",inline"`
Backup *ArangoBackupDetails `json:"backup,omitempty"`
Available bool `json:"available"`
Backup *ArangoBackupDetails `json:"backup,omitempty"`
Available bool `json:"available"`
Backoff *ArangoBackupStatusBackOff `json:"backoff,omitempty"`
}

func (a *ArangoBackupStatus) Equal(b *ArangoBackupStatus) bool {
Expand Down
59 changes: 59 additions & 0 deletions pkg/apis/backup/v1/backup_status_backoff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// DISCLAIMER
//
// Copyright 2016-2021 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package v1

import (
"time"

meta "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type ArangoBackupStatusBackOff struct {
Iterations int `json:"iterations,omitempty"`
Next meta.Time `json:"next,omitempty"`
}

func (a *ArangoBackupStatusBackOff) GetIterations() int {
if a == nil {
return 0
}

if a.Iterations < 0 {
return 0
}

return a.Iterations
}

func (a *ArangoBackupStatusBackOff) GetNext() meta.Time {
if a == nil {
return meta.Time{}
}

return a.Next
}

func (a *ArangoBackupStatusBackOff) Backoff(spec *ArangoBackupSpecBackOff) *ArangoBackupStatusBackOff {
return &ArangoBackupStatusBackOff{
Iterations: a.GetIterations() + 1,
Next: meta.Time{Time: time.Now().Add(spec.Backoff(a.GetIterations()))},
}
}
40 changes: 40 additions & 0 deletions pkg/apis/backup/v1/backup_status_backoff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// DISCLAIMER
//
// Copyright 2016-2021 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//

package v1

import (
"testing"
"time"

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

func TestArangoBackupStatusBackOff_Backoff(t *testing.T) {
t.Run("Default", func(t *testing.T) {
var spec *ArangoBackupSpecBackOff
var status *ArangoBackupStatusBackOff

n := status.Backoff(spec)

require.Equal(t, 1, n.GetIterations())
require.True(t, n.GetNext().After(time.Now().Add(time.Duration(9.9*float64(time.Second)))))
})
}
2 changes: 0 additions & 2 deletions pkg/apis/backup/v1/backup_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// Author Adam Janikowski
//

package v1

Expand Down
Loading