|
| 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 member |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "sync" |
| 26 | + |
| 27 | + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" |
| 28 | + "github.com/arangodb/kube-arangodb/pkg/deployment/reconciler" |
| 29 | + "github.com/arangodb/kube-arangodb/pkg/util/globals" |
| 30 | +) |
| 31 | + |
| 32 | +type StateInspector interface { |
| 33 | + RefreshState(ctx context.Context, members api.DeploymentStatusMemberElements) |
| 34 | + MemberState(id string) (State, bool) |
| 35 | +} |
| 36 | + |
| 37 | +func NewStateInspector(client reconciler.DeploymentMemberClient) StateInspector { |
| 38 | + return &stateInspector{ |
| 39 | + client: client, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +type stateInspector struct { |
| 44 | + lock sync.Mutex |
| 45 | + |
| 46 | + members map[string]State |
| 47 | + |
| 48 | + client reconciler.DeploymentMemberClient |
| 49 | +} |
| 50 | + |
| 51 | +func (s *stateInspector) RefreshState(ctx context.Context, members api.DeploymentStatusMemberElements) { |
| 52 | + s.lock.Lock() |
| 53 | + defer s.lock.Unlock() |
| 54 | + |
| 55 | + results := make([]State, len(members)) |
| 56 | + |
| 57 | + nctx, cancel := globals.GetGlobalTimeouts().ArangoDCheck().WithTimeout(ctx) |
| 58 | + defer cancel() |
| 59 | + |
| 60 | + members.ForEach(func(id int) { |
| 61 | + c, err := s.client.GetServerClient(nctx, members[id].Group, members[id].Member.ID) |
| 62 | + if err != nil { |
| 63 | + results[id].Reachable = false |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + if _, err := c.Version(nctx); err != nil { |
| 68 | + results[id].Reachable = false |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + results[id].Reachable = true |
| 73 | + }) |
| 74 | + |
| 75 | + current := map[string]State{} |
| 76 | + |
| 77 | + for id := range members { |
| 78 | + current[members[id].Member.ID] = results[id] |
| 79 | + } |
| 80 | + |
| 81 | + s.members = current |
| 82 | +} |
| 83 | + |
| 84 | +func (s *stateInspector) MemberState(id string) (State, bool) { |
| 85 | + s.lock.Lock() |
| 86 | + defer s.lock.Unlock() |
| 87 | + |
| 88 | + if s.members == nil { |
| 89 | + return State{}, false |
| 90 | + } |
| 91 | + |
| 92 | + v, ok := s.members[id] |
| 93 | + |
| 94 | + return v, ok |
| 95 | +} |
| 96 | + |
| 97 | +type State struct { |
| 98 | + Reachable bool |
| 99 | +} |
0 commit comments