/
localkeys.go
167 lines (136 loc) · 4.28 KB
/
localkeys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2016-2019 Authors of Cilium
//
// 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.
package allocator
import (
"fmt"
"github.com/cilium/cilium/pkg/idpool"
"github.com/cilium/cilium/pkg/kvstore"
"github.com/cilium/cilium/pkg/lock"
"github.com/sirupsen/logrus"
)
type localKey struct {
val idpool.ID
key AllocatorKey
refcnt uint64
// verified is true when the key has been synced with the kvstore
verified bool
}
// localKeys is a map of keys in use locally. Keys can be used multiple times.
// A refcnt is managed to know when a key is no longer in use
type localKeys struct {
lock.RWMutex
keys map[string]*localKey
ids map[idpool.ID]*localKey
}
func newLocalKeys() *localKeys {
return &localKeys{
keys: map[string]*localKey{},
ids: map[idpool.ID]*localKey{},
}
}
// allocate creates an entry for key in localKeys if needed and increments the
// refcnt. The value associated with the key must match the local cache or an
// error is returned
func (lk *localKeys) allocate(keyString string, key AllocatorKey, val idpool.ID) (idpool.ID, bool, error) {
lk.Lock()
defer lk.Unlock()
var firstUse bool
if k, ok := lk.keys[keyString]; ok {
if val != k.val {
return idpool.NoID, firstUse, fmt.Errorf("local key already allocated with different value (%s != %s)", val, k.val)
}
k.refcnt++
kvstore.Trace("Incremented local key refcnt", nil, logrus.Fields{fieldKey: keyString, fieldID: val, fieldRefCnt: k.refcnt})
return k.val, firstUse, nil
}
firstUse = true
k := &localKey{key: key, val: val, refcnt: 1}
lk.keys[keyString] = k
lk.ids[val] = k
kvstore.Trace("New local key", nil, logrus.Fields{fieldKey: keyString, fieldID: val, fieldRefCnt: 1})
return val, firstUse, nil
}
func (lk *localKeys) verify(key string) error {
lk.Lock()
defer lk.Unlock()
if k, ok := lk.keys[key]; ok {
k.verified = true
kvstore.Trace("Local key verified", nil, logrus.Fields{fieldKey: key})
return nil
}
return fmt.Errorf("key %s not found", key)
}
// lookupKey returns the idpool.ID of the key is present in the map of keys.
// if it isn't present, returns idpool.NoID
func (lk *localKeys) lookupKey(key string) idpool.ID {
lk.RLock()
defer lk.RUnlock()
if k, ok := lk.keys[key]; ok {
return k.val
}
return idpool.NoID
}
// lookupID returns the key for a given ID or an empty string
func (lk *localKeys) lookupID(id idpool.ID) AllocatorKey {
lk.RLock()
defer lk.RUnlock()
if k, ok := lk.ids[id]; ok {
return k.key
}
return nil
}
// use increments the refcnt of the key and returns its value
func (lk *localKeys) use(key string) idpool.ID {
lk.Lock()
defer lk.Unlock()
if k, ok := lk.keys[key]; ok {
// unverified keys behave as if they do not exist
if !k.verified {
return idpool.NoID
}
k.refcnt++
kvstore.Trace("Incremented local key refcnt", nil, logrus.Fields{fieldKey: key, fieldID: k.val, fieldRefCnt: k.refcnt})
return k.val
}
return idpool.NoID
}
// release releases the refcnt of a key. It returns the ID associated with the
// given key. When the last reference was released, the key is deleted and the
// returned lastUse value is true.
func (lk *localKeys) release(key string) (lastUse bool, id idpool.ID, err error) {
lk.Lock()
defer lk.Unlock()
if k, ok := lk.keys[key]; ok {
k.refcnt--
kvstore.Trace("Decremented local key refcnt", nil, logrus.Fields{fieldKey: key, fieldID: k.val, fieldRefCnt: k.refcnt})
if k.refcnt == 0 {
delete(lk.keys, key)
delete(lk.ids, k.val)
return true, k.val, nil
}
return false, k.val, nil
}
return false, idpool.NoID, fmt.Errorf("unable to find key in local cache")
}
func (lk *localKeys) getVerifiedIDs() map[idpool.ID]AllocatorKey {
ids := map[idpool.ID]AllocatorKey{}
lk.RLock()
for id, localKey := range lk.ids {
if localKey.verified {
ids[id] = localKey.key
}
}
lk.RUnlock()
return ids
}