|
| 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 agency |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "sync" |
| 26 | + |
| 27 | + "github.com/arangodb/go-driver/agency" |
| 28 | + api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1" |
| 29 | +) |
| 30 | + |
| 31 | +type Cache interface { |
| 32 | + Reload(ctx context.Context, client agency.Agency) (uint64, error) |
| 33 | + Data() (State, bool) |
| 34 | + CommitIndex() uint64 |
| 35 | +} |
| 36 | + |
| 37 | +func NewCache(mode *api.DeploymentMode) Cache { |
| 38 | + if mode.Get() == api.DeploymentModeSingle { |
| 39 | + return NewSingleCache() |
| 40 | + } |
| 41 | + |
| 42 | + return NewAgencyCache() |
| 43 | +} |
| 44 | + |
| 45 | +func NewAgencyCache() Cache { |
| 46 | + return &cache{} |
| 47 | +} |
| 48 | + |
| 49 | +func NewSingleCache() Cache { |
| 50 | + return &cacheSingle{} |
| 51 | +} |
| 52 | + |
| 53 | +type cacheSingle struct { |
| 54 | +} |
| 55 | + |
| 56 | +func (c cacheSingle) CommitIndex() uint64 { |
| 57 | + return 0 |
| 58 | +} |
| 59 | + |
| 60 | +func (c cacheSingle) Reload(ctx context.Context, client agency.Agency) (uint64, error) { |
| 61 | + return 0, nil |
| 62 | +} |
| 63 | + |
| 64 | +func (c cacheSingle) Data() (State, bool) { |
| 65 | + return State{}, true |
| 66 | +} |
| 67 | + |
| 68 | +type cache struct { |
| 69 | + lock sync.Mutex |
| 70 | + |
| 71 | + valid bool |
| 72 | + |
| 73 | + commitIndex uint64 |
| 74 | + |
| 75 | + data State |
| 76 | +} |
| 77 | + |
| 78 | +func (c *cache) CommitIndex() uint64 { |
| 79 | + return c.commitIndex |
| 80 | +} |
| 81 | + |
| 82 | +func (c *cache) Data() (State, bool) { |
| 83 | + c.lock.Lock() |
| 84 | + defer c.lock.Unlock() |
| 85 | + |
| 86 | + return c.data, c.valid |
| 87 | +} |
| 88 | + |
| 89 | +func (c *cache) Reload(ctx context.Context, client agency.Agency) (uint64, error) { |
| 90 | + c.lock.Lock() |
| 91 | + defer c.lock.Unlock() |
| 92 | + |
| 93 | + c.valid = false |
| 94 | + |
| 95 | + cfg, err := getAgencyConfig(ctx, client) |
| 96 | + if err != nil { |
| 97 | + return cfg.CommitIndex, err |
| 98 | + } |
| 99 | + |
| 100 | + if cfg.CommitIndex == c.commitIndex { |
| 101 | + // We are on same index, nothing to do |
| 102 | + return cfg.CommitIndex, err |
| 103 | + } |
| 104 | + |
| 105 | + if data, err := loadState(ctx, client); err != nil { |
| 106 | + return cfg.CommitIndex, err |
| 107 | + } else { |
| 108 | + c.data = data |
| 109 | + c.valid = true |
| 110 | + c.commitIndex = cfg.CommitIndex |
| 111 | + return cfg.CommitIndex, err |
| 112 | + } |
| 113 | +} |
0 commit comments