/
omap.go
338 lines (264 loc) · 7.38 KB
/
omap.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0
package omap
import (
"context"
"encoding/json"
"errors"
"fmt"
"sync"
"time"
"github.com/ceph/go-ceph/rados"
"github.com/ironcore-dev/ceph-provider/api"
"github.com/ironcore-dev/ceph-provider/internal/store"
utilssync "github.com/ironcore-dev/ceph-provider/internal/sync"
"github.com/ironcore-dev/ceph-provider/internal/utils"
"k8s.io/apimachinery/pkg/util/sets"
)
type CreateStrategy[E api.Object] interface {
PrepareForCreate(obj E)
}
type Options[E api.Object] struct {
OmapName string
NewFunc func() E
CreateStrategy CreateStrategy[E]
}
func New[E api.Object](conn *rados.Conn, pool string, opts Options[E]) (*Store[E], error) {
if conn == nil {
return nil, fmt.Errorf("must specify conn")
}
if pool == "" {
return nil, fmt.Errorf("must specify pool")
}
if opts.OmapName == "" {
return nil, fmt.Errorf("must specify opts.OmapName")
}
if opts.NewFunc == nil {
return nil, fmt.Errorf("must specify opts.NewFunc")
}
return &Store[E]{
idMu: utilssync.NewMutexMap[string](),
conn: conn,
pool: pool,
omapName: opts.OmapName,
watches: sets.New[*watch[E]](),
newFunc: opts.NewFunc,
createStrategy: opts.CreateStrategy,
}, nil
}
type Store[E api.Object] struct {
idMu *utilssync.MutexMap[string]
conn *rados.Conn
pool string
omapName string
newFunc func() E
createStrategy CreateStrategy[E]
watchesMu sync.RWMutex
watches sets.Set[*watch[E]]
}
func (s *Store[E]) enqueue(evt store.WatchEvent[E]) {
for _, handler := range s.watchHandlers() {
select {
case handler.events <- evt:
default:
}
}
}
func (s *Store[E]) watchHandlers() []*watch[E] {
s.watchesMu.RLock()
defer s.watchesMu.RUnlock()
return s.watches.UnsortedList()
}
func (s *Store[E]) getSingleOmapValue(ioCtx *rados.IOContext, omapName, key string) ([]byte, error) {
omap, err := ioCtx.GetAllOmapValues(omapName, "", key, 10)
if err != nil {
return nil, err
}
value, ok := omap[key]
if !ok {
return nil, rados.ErrNotFound
}
return value, nil
}
func (s *Store[E]) deleteOmapValue(ioCtx *rados.IOContext, omapName, key string) error {
if err := ioCtx.RmOmapKeys(omapName, []string{key}); err != nil {
return fmt.Errorf("unable to delete mapping omap value: %w", err)
}
return nil
}
func (s *Store[E]) setOmapValue(ioCtx *rados.IOContext, omapName, key string, value []byte) error {
if err := ioCtx.SetOmap(omapName, map[string][]byte{
key: value,
}); err != nil {
return fmt.Errorf("unable to set omap values: %w", err)
}
return nil
}
func (s *Store[E]) Create(ctx context.Context, obj E) (E, error) {
s.idMu.Lock(obj.GetID())
defer s.idMu.Unlock(obj.GetID())
ioCtx, err := s.conn.OpenIOContext(s.pool)
if err != nil {
return utils.Zero[E](), fmt.Errorf("unable to get io context: %w", err)
}
defer ioCtx.Destroy()
_, err = s.get(ioCtx, obj.GetID())
switch {
case err == nil:
return utils.Zero[E](), fmt.Errorf("object with id %q %w", obj.GetID(), store.ErrAlreadyExists)
case errors.Is(err, store.ErrNotFound):
default:
return utils.Zero[E](), fmt.Errorf("failed to get object with id %q %w", obj.GetID(), err)
}
if s.createStrategy != nil {
s.createStrategy.PrepareForCreate(obj)
}
obj.SetCreatedAt(time.Now())
obj, err = s.set(ioCtx, obj)
if err != nil {
return utils.Zero[E](), err
}
s.enqueue(store.WatchEvent[E]{
Type: store.WatchEventTypeCreated,
Object: obj,
})
return obj, nil
}
func (s *Store[E]) Delete(ctx context.Context, id string) error {
s.idMu.Lock(id)
defer s.idMu.Unlock(id)
ioCtx, err := s.conn.OpenIOContext(s.pool)
if err != nil {
return fmt.Errorf("unable to get io context: %w", err)
}
defer ioCtx.Destroy()
obj, err := s.get(ioCtx, id)
if err != nil {
return err
}
if len(obj.GetFinalizers()) == 0 {
return s.delete(ioCtx, id)
}
now := time.Now()
obj.SetDeletedAt(&now)
if _, err := s.set(ioCtx, obj); err != nil {
return fmt.Errorf("failed to set object metadata: %w", err)
}
s.enqueue(store.WatchEvent[E]{
Type: store.WatchEventTypeDeleted,
Object: obj,
})
return nil
}
func (s *Store[E]) delete(ioCtx *rados.IOContext, id string) error {
if err := s.deleteOmapValue(ioCtx, s.omapName, id); err != nil {
return fmt.Errorf("failed to delete object from omap: %w", err)
}
return nil
}
func (s *Store[E]) Get(ctx context.Context, id string) (E, error) {
ioCtx, err := s.conn.OpenIOContext(s.pool)
if err != nil {
return utils.Zero[E](), fmt.Errorf("unable to get io context: %w", err)
}
defer ioCtx.Destroy()
return s.get(ioCtx, id)
}
func (s *Store[E]) Update(ctx context.Context, obj E) (E, error) {
s.idMu.Lock(obj.GetID())
defer s.idMu.Unlock(obj.GetID())
ioCtx, err := s.conn.OpenIOContext(s.pool)
if err != nil {
return utils.Zero[E](), fmt.Errorf("unable to get io context: %w", err)
}
defer ioCtx.Destroy()
_, err = s.get(ioCtx, obj.GetID())
if err != nil {
return utils.Zero[E](), err
}
if obj.GetDeletedAt() != nil && len(obj.GetFinalizers()) == 0 {
if err := s.delete(ioCtx, obj.GetID()); err != nil {
return utils.Zero[E](), fmt.Errorf("failed to delete object metadata: %w", err)
}
return obj, nil
}
//Todo: update version
obj, err = s.set(ioCtx, obj)
if err != nil {
return utils.Zero[E](), err
}
s.enqueue(store.WatchEvent[E]{
Type: store.WatchEventTypeUpdated,
Object: obj,
})
return obj, nil
}
type watch[E api.Object] struct {
store *Store[E]
events chan store.WatchEvent[E]
}
func (w *watch[E]) Stop() {
w.store.watchesMu.Lock()
defer w.store.watchesMu.Unlock()
w.store.watches.Delete(w)
}
func (w *watch[E]) Events() <-chan store.WatchEvent[E] {
return w.events
}
func (s *Store[E]) Watch(ctx context.Context) (store.Watch[E], error) {
s.watchesMu.Lock()
defer s.watchesMu.Unlock()
w := &watch[E]{
store: s,
events: make(chan store.WatchEvent[E]),
}
s.watches.Insert(w)
return w, nil
}
func (s *Store[E]) List(ctx context.Context) ([]E, error) {
ioCtx, err := s.conn.OpenIOContext(s.pool)
if err != nil {
return nil, fmt.Errorf("unable to get io context: %w", err)
}
defer ioCtx.Destroy()
omap, err := ioCtx.GetAllOmapValues(s.omapName, "", "", 10)
if err != nil {
if errors.Is(err, rados.ErrNotFound) {
return nil, nil
}
return nil, err
}
var objs []E
for _, v := range omap {
obj := s.newFunc()
if err := json.Unmarshal(v, &obj); err != nil {
return nil, fmt.Errorf("failed to unmarshal object: %w", err)
}
objs = append(objs, obj)
}
return objs, nil
}
func (s *Store[E]) set(ioCtx *rados.IOContext, obj E) (E, error) {
data, err := json.Marshal(obj)
if err != nil {
return utils.Zero[E](), fmt.Errorf("failed to marshal obj: %w", err)
}
if err := s.setOmapValue(ioCtx, s.omapName, obj.GetID(), data); err != nil {
return utils.Zero[E](), fmt.Errorf("failed to put os object mapping: %w", err)
}
return obj, nil
}
func (s *Store[E]) get(ioCtx *rados.IOContext, id string) (E, error) {
data, err := s.getSingleOmapValue(ioCtx, s.omapName, id)
if err != nil {
if !errors.Is(err, rados.ErrNotFound) {
return utils.Zero[E](), fmt.Errorf("failed to fetch omap value: %w", err)
}
return utils.Zero[E](), fmt.Errorf("object with id %q %w", id, store.ErrNotFound)
}
obj := s.newFunc()
if err := json.Unmarshal(data, obj); err != nil {
return utils.Zero[E](), fmt.Errorf("failed to unmarshal object: %w", err)
}
return obj, nil
}