Skip to content

Commit d93167f

Browse files
authored
[Feature] [Backup] Backoff logic (#844)
1 parent 7f10f26 commit d93167f

23 files changed

+435
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Change Log
22

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

56
## [1.2.5](https://github.com/arangodb/kube-arangodb/tree/1.2.5) (2021-10-25)
67
- Split & Unify Lifecycle management functionality

pkg/apis/backup/v1/backup.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
//
1818
// Copyright holder is ArangoDB GmbH, Cologne, Germany
1919
//
20-
// Author Adam Janikowski
21-
//
2220

2321
package v1
2422

pkg/apis/backup/v1/backup_policy.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
//
1818
// Copyright holder is ArangoDB GmbH, Cologne, Germany
1919
//
20-
// Author Adam Janikowski
21-
//
2220

2321
package v1
2422

pkg/apis/backup/v1/backup_policy_spec.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
//
1818
// Copyright holder is ArangoDB GmbH, Cologne, Germany
1919
//
20-
// Author Adam Janikowski
21-
//
2220

2321
package v1
2422

pkg/apis/backup/v1/backup_policy_status.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
//
1818
// Copyright holder is ArangoDB GmbH, Cologne, Germany
1919
//
20-
// Author Adam Janikowski
21-
//
2220

2321
package v1
2422

pkg/apis/backup/v1/backup_policy_validate.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
//
1818
// Copyright holder is ArangoDB GmbH, Cologne, Germany
1919
//
20-
// Author Adam Janikowski
21-
//
2220

2321
package v1
2422

pkg/apis/backup/v1/backup_spec.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
//
1818
// Copyright holder is ArangoDB GmbH, Cologne, Germany
1919
//
20-
// Author Adam Janikowski
21-
//
2220

2321
package v1
2422

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

3735
PolicyName *string `json:"policyName,omitempty"`
36+
37+
Backoff *ArangoBackupSpecBackOff `json:"backoff,omitempty"`
3838
}
3939

4040
type ArangoBackupSpecDeployment struct {
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2016-2021 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package v1
22+
23+
import "time"
24+
25+
type ArangoBackupSpecBackOff struct {
26+
// MinDelay defines minimum delay in seconds. Default to 30
27+
MinDelay *int `json:"min_delay,omitempty"`
28+
// MaxDelay defines maximum delay in seconds. Default to 600
29+
MaxDelay *int `json:"max_delay,omitempty"`
30+
// Iterations defines number of iterations before reaching MaxDelay. Default to 5
31+
Iterations *int `json:"iterations,omitempty"`
32+
}
33+
34+
func (a *ArangoBackupSpecBackOff) GetMaxDelay() int {
35+
if a == nil || a.MaxDelay == nil {
36+
return 600
37+
}
38+
39+
v := *a.MaxDelay
40+
41+
if v < 0 {
42+
return 0
43+
}
44+
45+
return v
46+
}
47+
48+
func (a *ArangoBackupSpecBackOff) GetMinDelay() int {
49+
if a == nil || a.MinDelay == nil {
50+
return 30
51+
}
52+
53+
v := *a.MinDelay
54+
55+
if v < 0 {
56+
return 0
57+
}
58+
59+
if m := a.GetMaxDelay(); m < v {
60+
return m
61+
}
62+
63+
return v
64+
}
65+
66+
func (a *ArangoBackupSpecBackOff) GetIterations() int {
67+
if a == nil || a.Iterations == nil {
68+
return 5
69+
}
70+
71+
v := *a.Iterations
72+
73+
if v < 1 {
74+
return 1
75+
}
76+
77+
return v
78+
}
79+
80+
func (a *ArangoBackupSpecBackOff) Backoff(iteration int) time.Duration {
81+
if maxIterations := a.GetIterations(); maxIterations <= iteration {
82+
return time.Duration(a.GetMaxDelay()) * time.Second
83+
} else {
84+
min, max := a.GetMinDelay(), a.GetMaxDelay()
85+
86+
if min == max {
87+
return time.Duration(min) * time.Second
88+
}
89+
90+
return time.Duration(min+int(float64(iteration)/float64(maxIterations)*float64(max-min))) * time.Second
91+
}
92+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2016-2021 ArangoDB GmbH, Cologne, Germany
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
// Copyright holder is ArangoDB GmbH, Cologne, Germany
19+
//
20+
21+
package v1
22+
23+
import (
24+
"testing"
25+
"time"
26+
27+
"github.com/stretchr/testify/assert"
28+
29+
"github.com/arangodb/kube-arangodb/pkg/util"
30+
)
31+
32+
func TestArangoBackupSpecBackOff_Backoff(t *testing.T) {
33+
t.Run("Default", func(t *testing.T) {
34+
var b *ArangoBackupSpecBackOff
35+
36+
assert.Equal(t, 30*time.Second, b.Backoff(0))
37+
assert.Equal(t, 144*time.Second, b.Backoff(1))
38+
assert.Equal(t, 258*time.Second, b.Backoff(2))
39+
assert.Equal(t, 372*time.Second, b.Backoff(3))
40+
assert.Equal(t, 486*time.Second, b.Backoff(4))
41+
assert.Equal(t, 600*time.Second, b.Backoff(5))
42+
assert.Equal(t, 600*time.Second, b.Backoff(6))
43+
})
44+
t.Run("Custom", func(t *testing.T) {
45+
b := &ArangoBackupSpecBackOff{
46+
MinDelay: util.NewInt(20),
47+
MaxDelay: util.NewInt(120),
48+
Iterations: util.NewInt(10),
49+
}
50+
51+
assert.Equal(t, 20*time.Second, b.Backoff(0))
52+
assert.Equal(t, 30*time.Second, b.Backoff(1))
53+
assert.Equal(t, 40*time.Second, b.Backoff(2))
54+
assert.Equal(t, 50*time.Second, b.Backoff(3))
55+
assert.Equal(t, 60*time.Second, b.Backoff(4))
56+
assert.Equal(t, 70*time.Second, b.Backoff(5))
57+
assert.Equal(t, 80*time.Second, b.Backoff(6))
58+
assert.Equal(t, 90*time.Second, b.Backoff(7))
59+
assert.Equal(t, 100*time.Second, b.Backoff(8))
60+
assert.Equal(t, 110*time.Second, b.Backoff(9))
61+
assert.Equal(t, 120*time.Second, b.Backoff(10))
62+
assert.Equal(t, 120*time.Second, b.Backoff(11))
63+
})
64+
65+
t.Run("Invalid", func(t *testing.T) {
66+
b := &ArangoBackupSpecBackOff{
67+
MinDelay: util.NewInt(-1),
68+
MaxDelay: util.NewInt(-1),
69+
Iterations: util.NewInt(0),
70+
}
71+
72+
assert.Equal(t, 0, b.GetMinDelay())
73+
assert.Equal(t, 0, b.GetMaxDelay())
74+
assert.Equal(t, 1, b.GetIterations())
75+
76+
assert.Equal(t, 0*time.Second, b.Backoff(12345))
77+
})
78+
79+
t.Run("Max < Min", func(t *testing.T) {
80+
b := &ArangoBackupSpecBackOff{
81+
MinDelay: util.NewInt(50),
82+
MaxDelay: util.NewInt(20),
83+
}
84+
85+
assert.Equal(t, 20, b.GetMinDelay())
86+
assert.Equal(t, 20, b.GetMaxDelay())
87+
assert.Equal(t, 5, b.GetIterations())
88+
})
89+
}

pkg/apis/backup/v1/backup_state.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
//
1818
// Copyright holder is ArangoDB GmbH, Cologne, Germany
1919
//
20-
// Author Adam Janikowski
21-
//
2220

2321
package v1
2422

0 commit comments

Comments
 (0)