Skip to content

Commit 1ced857

Browse files
authored
[Feature] Agency ArangoSync State check (#1165)
1 parent 8af2e61 commit 1ced857

10 files changed

+157
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- (Refactoring) Extract kerrors package
88
- (Refactoring) Extract Inspector Definitions package
99
- (Bugfix) Fix PDBs Version discovery
10+
- (Feature) Agency ArangoSync State check
1011

1112
## [1.2.20](https://github.com/arangodb/kube-arangodb/tree/1.2.20) (2022-10-25)
1213
- (Feature) Add action progress

pkg/deployment/agency/arangosync.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2016-2022 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 agency
22+
23+
import "encoding/json"
24+
25+
var (
26+
_ json.Unmarshaler = &ArangoSyncLazy{}
27+
)
28+
29+
// ArangoSyncLazy allows failure during load of the Sync state
30+
type ArangoSyncLazy struct {
31+
Error error
32+
*ArangoSync
33+
}
34+
35+
func (a *ArangoSyncLazy) UnmarshalJSON(bytes []byte) error {
36+
var s ArangoSync
37+
38+
if err := json.Unmarshal(bytes, &s); err != nil {
39+
a.ArangoSync = nil
40+
a.Error = err
41+
} else {
42+
a.ArangoSync = &s
43+
a.Error = nil
44+
}
45+
46+
return nil
47+
}
48+
49+
type ArangoSync struct {
50+
State ArangoSyncState `json:"synchronizationState"`
51+
}
52+
53+
func (a *ArangoSync) IsSyncInProgress() bool {
54+
if a == nil {
55+
return false
56+
}
57+
58+
// Check Incoming
59+
if s := a.State.Incoming.State; s != nil && *s != "inactive" && *s != "" {
60+
return true
61+
}
62+
63+
if a.State.Outgoing.Targets.Exists() {
64+
return true
65+
}
66+
67+
return false
68+
}
69+
70+
type ArangoSyncState struct {
71+
Incoming ArangoSyncIncomingState `json:"incoming"`
72+
Outgoing ArangoSyncOutgoingState `json:"outgoing"`
73+
}
74+
75+
type ArangoSyncIncomingState struct {
76+
State *string `json:"state,omitempty"`
77+
}
78+
79+
type ArangoSyncOutgoingState struct {
80+
Targets StateExists `json:"targets,omitempty"`
81+
}
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// DISCLAIMER
3+
//
4+
// Copyright 2016-2022 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 agency
22+
23+
import (
24+
"encoding/json"
25+
"testing"
26+
27+
"github.com/stretchr/testify/require"
28+
)
29+
30+
func Test_Sync_Target(t *testing.T) {
31+
var s DumpState
32+
require.NoError(t, json.Unmarshal(syncSource, &s))
33+
34+
require.NotNil(t, s.Agency.ArangoDB.ArangoSync.ArangoSync)
35+
require.True(t, s.Agency.ArangoDB.ArangoSync.ArangoSync.State.Outgoing.Targets.Exists())
36+
require.True(t, s.Agency.ArangoDB.ArangoSync.IsSyncInProgress())
37+
}
38+
39+
func Test_Sync_Source(t *testing.T) {
40+
var s DumpState
41+
require.NoError(t, json.Unmarshal(syncTarget, &s))
42+
43+
require.NotNil(t, s.Agency.ArangoDB.ArangoSync.ArangoSync)
44+
require.NotNil(t, s.Agency.ArangoDB.ArangoSync.ArangoSync.State.Incoming.State)
45+
require.Equal(t, "running", *s.Agency.ArangoDB.ArangoSync.ArangoSync.State.Incoming.State)
46+
require.True(t, s.Agency.ArangoDB.ArangoSync.IsSyncInProgress())
47+
}

pkg/deployment/agency/plan_databases_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,6 @@ func Test_Databases(t *testing.T) {
3232
require.NoError(t, json.Unmarshal(agencyDump39HotBackup, &s))
3333

3434
require.Contains(t, s.Agency.Arango.Plan.Databases, "_system")
35+
36+
require.False(t, s.Agency.ArangoDB.ArangoSync.IsSyncInProgress())
3537
}

pkg/deployment/agency/state.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,18 @@ func (c *cache) loadState(ctx context.Context, client agency.Agency) (State, err
8787
type StateRoots []StateRoot
8888

8989
type StateRoot struct {
90-
Arango State `json:"arango"`
90+
Arango State `json:"arango"`
91+
ArangoDB StateDB `json:"arangodb,omitempty"`
9192
}
9293

9394
type DumpState struct {
9495
Agency StateRoot `json:"agency"`
9596
}
9697

98+
type StateDB struct {
99+
ArangoSync ArangoSyncLazy `json:"arangosync,omitempty"`
100+
}
101+
97102
type State struct {
98103
Supervision StateSupervision `json:"Supervision"`
99104
Plan StatePlan `json:"Plan"`

pkg/deployment/agency/state_exists.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020

2121
package agency
2222

23-
import "github.com/arangodb/kube-arangodb/pkg/util"
23+
import (
24+
"crypto/sha256"
25+
26+
"github.com/arangodb/kube-arangodb/pkg/util"
27+
)
2428

2529
type StateExists []byte
2630

@@ -42,10 +46,9 @@ func (d *StateExists) UnmarshalJSON(bytes []byte) error {
4246
return nil
4347
}
4448

45-
z := make([]byte, len(bytes))
46-
47-
copy(z, bytes)
49+
data := sha256.Sum256(bytes)
50+
allData := data[:]
4851

49-
*d = z
52+
*d = allData
5053
return nil
5154
}

pkg/deployment/agency/state_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ var agencyDump39Jobs []byte
5252
//go:embed testdata/longdata.json
5353
var longData []byte
5454

55+
//go:embed testdata/sync.source.json
56+
var syncSource []byte
57+
58+
//go:embed testdata/sync.target.json
59+
var syncTarget []byte
60+
5561
var (
5662
data = map[string][]byte{
5763
"3.6": agencyDump36,

pkg/deployment/agency/target_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ func Test_Target_HotBackup(t *testing.T) {
3535
require.True(t, s.Agency.Arango.Target.HotBackup.Create.Exists())
3636

3737
t.Log(s.Agency.Arango.Target.HotBackup.Create.time.String())
38+
39+
require.False(t, s.Agency.ArangoDB.ArangoSync.IsSyncInProgress())
3840
})
3941
t.Run("Does Not Exists", func(t *testing.T) {
4042
var s DumpState
4143
require.NoError(t, json.Unmarshal(agencyDump39Satellite, &s))
4244

4345
require.False(t, s.Agency.Arango.Target.HotBackup.Create.Exists())
46+
47+
require.False(t, s.Agency.ArangoDB.ArangoSync.IsSyncInProgress())
4448
})
4549
}

pkg/deployment/agency/testdata/sync.source.json

+1
Large diffs are not rendered by default.

pkg/deployment/agency/testdata/sync.target.json

+1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)