-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathregistry.go
More file actions
373 lines (337 loc) · 12.6 KB
/
registry.go
File metadata and controls
373 lines (337 loc) · 12.6 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package registry
import (
"errors"
"fmt"
"net/url"
"sync"
"time"
"github.com/feast-dev/feast/go/internal/feast/model"
"github.com/rs/zerolog/log"
"github.com/feast-dev/feast/go/protos/feast/core"
)
var REGISTRY_SCHEMA_VERSION string = "1"
var REGISTRY_STORE_CLASS_FOR_SCHEME map[string]string = map[string]string{
"gs": "GCSRegistryStore",
"s3": "S3RegistryStore",
"file": "FileRegistryStore",
"": "FileRegistryStore",
}
/*
Store protos of FeatureView, FeatureService, Entity, OnDemandFeatureView
but return to user copies of non-proto versions of these objects
*/
type Registry struct {
project string
registryStore RegistryStore
cachedFeatureServices map[string]map[string]*core.FeatureService
cachedEntities map[string]map[string]*core.Entity
cachedFeatureViews map[string]map[string]*core.FeatureView
cachedStreamFeatureViews map[string]map[string]*core.StreamFeatureView
cachedOnDemandFeatureViews map[string]map[string]*core.OnDemandFeatureView
cachedRegistry *core.Registry
cachedRegistryProtoLastUpdated time.Time
cachedRegistryProtoTtl time.Duration
mu sync.RWMutex
}
func NewRegistry(registryConfig *RegistryConfig, repoPath string, project string) (*Registry, error) {
registryStoreType := registryConfig.RegistryStoreType
registryPath := registryConfig.Path
r := &Registry{
project: project,
cachedRegistryProtoTtl: time.Duration(registryConfig.CacheTtlSeconds) * time.Second,
}
if len(registryStoreType) == 0 {
registryStore, err := getRegistryStoreFromScheme(registryPath, registryConfig, repoPath, project)
if err != nil {
return nil, err
}
r.registryStore = registryStore
} else {
registryStore, err := getRegistryStoreFromType(registryStoreType, registryConfig, repoPath, project)
if err != nil {
return nil, err
}
r.registryStore = registryStore
}
return r, nil
}
func (r *Registry) InitializeRegistry() error {
_, err := r.getRegistryProto()
if err != nil {
if _, ok := r.registryStore.(*FileRegistryStore); ok {
log.Error().Err(err).Msg("Registry Initialization Failed")
return err
}
registryProto := &core.Registry{RegistrySchemaVersion: REGISTRY_SCHEMA_VERSION}
r.registryStore.UpdateRegistryProto(registryProto)
}
go r.RefreshRegistryOnInterval()
return nil
}
func (r *Registry) RefreshRegistryOnInterval() {
ticker := time.NewTicker(r.cachedRegistryProtoTtl)
for ; true; <-ticker.C {
err := r.refresh()
if err != nil {
log.Error().Stack().Err(err).Msg("Registry refresh Failed")
}
}
}
func (r *Registry) refresh() error {
_, err := r.getRegistryProto()
return err
}
func (r *Registry) getRegistryProto() (*core.Registry, error) {
expired := r.cachedRegistry == nil || (r.cachedRegistryProtoTtl > 0 && time.Now().After(r.cachedRegistryProtoLastUpdated.Add(r.cachedRegistryProtoTtl)))
if !expired {
return r.cachedRegistry, nil
}
registryProto, err := r.registryStore.GetRegistryProto()
if err != nil {
return nil, err
}
r.load(registryProto)
return registryProto, nil
}
func (r *Registry) load(registry *core.Registry) {
r.mu.Lock()
defer r.mu.Unlock()
r.cachedRegistry = registry
r.cachedFeatureServices = make(map[string]map[string]*core.FeatureService)
r.cachedEntities = make(map[string]map[string]*core.Entity)
r.cachedFeatureViews = make(map[string]map[string]*core.FeatureView)
r.cachedStreamFeatureViews = make(map[string]map[string]*core.StreamFeatureView)
r.cachedOnDemandFeatureViews = make(map[string]map[string]*core.OnDemandFeatureView)
r.loadEntities(registry)
r.loadFeatureServices(registry)
r.loadFeatureViews(registry)
r.loadStreamFeatureViews(registry)
r.loadOnDemandFeatureViews(registry)
r.cachedRegistryProtoLastUpdated = time.Now()
}
func (r *Registry) loadEntities(registry *core.Registry) {
entities := registry.Entities
for _, entity := range entities {
if _, ok := r.cachedEntities[r.project]; !ok {
r.cachedEntities[r.project] = make(map[string]*core.Entity)
}
r.cachedEntities[r.project][entity.Spec.Name] = entity
}
}
func (r *Registry) loadFeatureServices(registry *core.Registry) {
featureServices := registry.FeatureServices
for _, featureService := range featureServices {
if _, ok := r.cachedFeatureServices[r.project]; !ok {
r.cachedFeatureServices[r.project] = make(map[string]*core.FeatureService)
}
r.cachedFeatureServices[r.project][featureService.Spec.Name] = featureService
}
}
func (r *Registry) loadFeatureViews(registry *core.Registry) {
featureViews := registry.FeatureViews
for _, featureView := range featureViews {
if _, ok := r.cachedFeatureViews[r.project]; !ok {
r.cachedFeatureViews[r.project] = make(map[string]*core.FeatureView)
}
r.cachedFeatureViews[r.project][featureView.Spec.Name] = featureView
}
}
func (r *Registry) loadStreamFeatureViews(registry *core.Registry) {
streamFeatureViews := registry.StreamFeatureViews
for _, streamFeatureView := range streamFeatureViews {
if _, ok := r.cachedStreamFeatureViews[r.project]; !ok {
r.cachedStreamFeatureViews[r.project] = make(map[string]*core.StreamFeatureView)
}
r.cachedStreamFeatureViews[r.project][streamFeatureView.Spec.Name] = streamFeatureView
}
}
func (r *Registry) loadOnDemandFeatureViews(registry *core.Registry) {
onDemandFeatureViews := registry.OnDemandFeatureViews
for _, onDemandFeatureView := range onDemandFeatureViews {
if _, ok := r.cachedOnDemandFeatureViews[r.project]; !ok {
r.cachedOnDemandFeatureViews[r.project] = make(map[string]*core.OnDemandFeatureView)
}
r.cachedOnDemandFeatureViews[r.project][onDemandFeatureView.Spec.Name] = onDemandFeatureView
}
}
/*
Look up Entities inside project
Returns empty list if project not found
*/
func (r *Registry) ListEntities(project string) ([]*model.Entity, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if cachedEntities, ok := r.cachedEntities[project]; !ok {
return []*model.Entity{}, nil
} else {
entities := make([]*model.Entity, len(cachedEntities))
index := 0
for _, entityProto := range cachedEntities {
entities[index] = model.NewEntityFromProto(entityProto)
index += 1
}
return entities, nil
}
}
/*
Look up Feature Views inside project
Returns empty list if project not found
*/
func (r *Registry) ListFeatureViews(project string) ([]*model.FeatureView, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if cachedFeatureViews, ok := r.cachedFeatureViews[project]; !ok {
return []*model.FeatureView{}, nil
} else {
featureViews := make([]*model.FeatureView, len(cachedFeatureViews))
index := 0
for _, featureViewProto := range cachedFeatureViews {
featureViews[index] = model.NewFeatureViewFromProto(featureViewProto)
index += 1
}
return featureViews, nil
}
}
/*
Look up Stream Feature Views inside project
Returns empty list if project not found
*/
func (r *Registry) ListStreamFeatureViews(project string) ([]*model.FeatureView, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if cachedStreamFeatureViews, ok := r.cachedStreamFeatureViews[project]; !ok {
return []*model.FeatureView{}, nil
} else {
streamFeatureViews := make([]*model.FeatureView, len(cachedStreamFeatureViews))
index := 0
for _, streamFeatureViewProto := range cachedStreamFeatureViews {
streamFeatureViews[index] = model.NewFeatureViewFromStreamFeatureViewProto(streamFeatureViewProto)
index += 1
}
return streamFeatureViews, nil
}
}
/*
Look up Feature Services inside project
Returns empty list if project not found
*/
func (r *Registry) ListFeatureServices(project string) ([]*model.FeatureService, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if cachedFeatureServices, ok := r.cachedFeatureServices[project]; !ok {
return []*model.FeatureService{}, nil
} else {
featureServices := make([]*model.FeatureService, len(cachedFeatureServices))
index := 0
for _, featureServiceProto := range cachedFeatureServices {
featureServices[index] = model.NewFeatureServiceFromProto(featureServiceProto)
index += 1
}
return featureServices, nil
}
}
/*
Look up On Demand Feature Views inside project
Returns empty list if project not found
*/
func (r *Registry) ListOnDemandFeatureViews(project string) ([]*model.OnDemandFeatureView, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if cachedOnDemandFeatureViews, ok := r.cachedOnDemandFeatureViews[project]; !ok {
return []*model.OnDemandFeatureView{}, nil
} else {
onDemandFeatureViews := make([]*model.OnDemandFeatureView, len(cachedOnDemandFeatureViews))
index := 0
for _, onDemandFeatureViewProto := range cachedOnDemandFeatureViews {
onDemandFeatureViews[index] = model.NewOnDemandFeatureViewFromProto(onDemandFeatureViewProto)
index += 1
}
return onDemandFeatureViews, nil
}
}
func (r *Registry) GetEntity(project, entityName string) (*model.Entity, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if cachedEntities, ok := r.cachedEntities[project]; !ok {
return nil, fmt.Errorf("no cached entities found for project %s", project)
} else {
if entity, ok := cachedEntities[entityName]; !ok {
return nil, fmt.Errorf("no cached entity %s found for project %s", entityName, project)
} else {
return model.NewEntityFromProto(entity), nil
}
}
}
func (r *Registry) GetFeatureView(project, featureViewName string) (*model.FeatureView, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if cachedFeatureViews, ok := r.cachedFeatureViews[project]; !ok {
return nil, fmt.Errorf("no cached feature views found for project %s", project)
} else {
if featureViewProto, ok := cachedFeatureViews[featureViewName]; !ok {
return nil, fmt.Errorf("no cached feature view %s found for project %s", featureViewName, project)
} else {
return model.NewFeatureViewFromProto(featureViewProto), nil
}
}
}
func (r *Registry) GetStreamFeatureView(project, streamFeatureViewName string) (*model.FeatureView, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if cachedStreamFeatureViews, ok := r.cachedStreamFeatureViews[project]; !ok {
return nil, fmt.Errorf("no cached stream feature views found for project %s", project)
} else {
if streamFeatureViewProto, ok := cachedStreamFeatureViews[streamFeatureViewName]; !ok {
return nil, fmt.Errorf("no cached stream feature view %s found for project %s", streamFeatureViewName, project)
} else {
return model.NewFeatureViewFromStreamFeatureViewProto(streamFeatureViewProto), nil
}
}
}
func (r *Registry) GetFeatureService(project, featureServiceName string) (*model.FeatureService, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if cachedFeatureServices, ok := r.cachedFeatureServices[project]; !ok {
return nil, fmt.Errorf("no cached feature services found for project %s", project)
} else {
if featureServiceProto, ok := cachedFeatureServices[featureServiceName]; !ok {
return nil, fmt.Errorf("no cached feature service %s found for project %s", featureServiceName, project)
} else {
return model.NewFeatureServiceFromProto(featureServiceProto), nil
}
}
}
func (r *Registry) GetOnDemandFeatureView(project, onDemandFeatureViewName string) (*model.OnDemandFeatureView, error) {
r.mu.RLock()
defer r.mu.RUnlock()
if cachedOnDemandFeatureViews, ok := r.cachedOnDemandFeatureViews[project]; !ok {
return nil, fmt.Errorf("no cached on demand feature views found for project %s", project)
} else {
if onDemandFeatureViewProto, ok := cachedOnDemandFeatureViews[onDemandFeatureViewName]; !ok {
return nil, fmt.Errorf("no cached on demand feature view %s found for project %s", onDemandFeatureViewName, project)
} else {
return model.NewOnDemandFeatureViewFromProto(onDemandFeatureViewProto), nil
}
}
}
func getRegistryStoreFromScheme(registryPath string, registryConfig *RegistryConfig, repoPath string, project string) (RegistryStore, error) {
uri, err := url.Parse(registryPath)
if err != nil {
return nil, err
}
if registryStoreType, ok := REGISTRY_STORE_CLASS_FOR_SCHEME[uri.Scheme]; ok {
return getRegistryStoreFromType(registryStoreType, registryConfig, repoPath, project)
}
return nil, fmt.Errorf("registry path %s has unsupported scheme %s. Supported schemes are file, s3 and gcs", registryPath, uri.Scheme)
}
func getRegistryStoreFromType(registryStoreType string, registryConfig *RegistryConfig, repoPath string, project string) (RegistryStore, error) {
switch registryStoreType {
case "FileRegistryStore":
return NewFileRegistryStore(registryConfig, repoPath), nil
case "S3RegistryStore":
return NewS3RegistryStore(registryConfig, repoPath), nil
case "GCSRegistryStore":
return NewGCSRegistryStore(registryConfig, repoPath), nil
}
return nil, errors.New("only FileRegistryStore, S3RegistryStore, and GCSRegistryStore are supported at this moment")
}