-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathfeaturestore.go
More file actions
366 lines (319 loc) · 11.6 KB
/
featurestore.go
File metadata and controls
366 lines (319 loc) · 11.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
package feast
import (
"context"
"errors"
"github.com/apache/arrow/go/v17/arrow/memory"
//"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"go.opentelemetry.io/otel"
"github.com/feast-dev/feast/go/internal/feast/model"
"github.com/feast-dev/feast/go/internal/feast/onlineserving"
"github.com/feast-dev/feast/go/internal/feast/onlinestore"
"github.com/feast-dev/feast/go/internal/feast/registry"
"github.com/feast-dev/feast/go/internal/feast/transformation"
"github.com/feast-dev/feast/go/protos/feast/serving"
prototypes "github.com/feast-dev/feast/go/protos/feast/types"
)
var tracer = otel.Tracer("github.com/feast-dev/feast/go/feast")
type FeatureStore struct {
config *registry.RepoConfig
registry *registry.Registry
onlineStore onlinestore.OnlineStore
transformationCallback transformation.TransformationCallback
transformationService *transformation.GrpcTransformationService
}
// A Features struct specifies a list of features to be retrieved from the online store. These features
// can be specified either as a list of string feature references or as a feature service. String
// feature references must have format "feature_view:feature", e.g. "customer_fv:daily_transactions".
type Features struct {
FeaturesRefs []string
FeatureService *model.FeatureService
}
func (fs *FeatureStore) Registry() *registry.Registry {
return fs.registry
}
func (fs *FeatureStore) GetRepoConfig() *registry.RepoConfig {
return fs.config
}
// NewFeatureStore constructs a feature store fat client using the
// repo config (contents of feature_store.yaml converted to JSON map).
func NewFeatureStore(config *registry.RepoConfig, callback transformation.TransformationCallback) (*FeatureStore, error) {
onlineStore, err := onlinestore.NewOnlineStore(config)
if err != nil {
return nil, err
}
registryConfig, err := config.GetRegistryConfig()
if err != nil {
return nil, err
}
registry, err := registry.NewRegistry(registryConfig, config.RepoPath, config.Project)
if err != nil {
return nil, err
}
err = registry.InitializeRegistry()
if err != nil {
return nil, err
}
var transformationService *transformation.GrpcTransformationService
if transformationServerEndpoint, ok := config.FeatureServer["transformation_service_endpoint"]; ok {
// Use a scalable transformation service like Python Transformation Service.
// Assume the user will define the "transformation_service_endpoint" in the feature_store.yaml file
// under the "feature_server" section.
transformationService, _ = transformation.NewGrpcTransformationService(config, transformationServerEndpoint.(string))
}
return &FeatureStore{
config: config,
registry: registry,
onlineStore: onlineStore,
transformationCallback: callback,
transformationService: transformationService,
}, nil
}
// TODO: Review all functions that use ODFV and Request FV since these have not been tested
// ToDo: Split GetOnlineFeatures interface into two: GetOnlinFeaturesByFeatureService and GetOnlineFeaturesByFeatureRefs
func (fs *FeatureStore) GetOnlineFeatures(
ctx context.Context,
featureRefs []string,
featureService *model.FeatureService,
joinKeyToEntityValues map[string]*prototypes.RepeatedValue,
requestData map[string]*prototypes.RepeatedValue,
fullFeatureNames bool) ([]*onlineserving.FeatureVector, error) {
fvs, odFvs, err := fs.listAllViews()
if err != nil {
return nil, err
}
entities, err := fs.ListEntities(false)
if err != nil {
return nil, err
}
var requestedFeatureViews []*onlineserving.FeatureViewAndRefs
var requestedOnDemandFeatureViews []*model.OnDemandFeatureView
if featureService != nil {
requestedFeatureViews, requestedOnDemandFeatureViews, err =
onlineserving.GetFeatureViewsToUseByService(featureService, fvs, odFvs)
} else {
requestedFeatureViews, requestedOnDemandFeatureViews, err =
onlineserving.GetFeatureViewsToUseByFeatureRefs(featureRefs, fvs, odFvs)
}
if err != nil {
return nil, err
}
if len(requestedOnDemandFeatureViews) > 0 && fs.transformationService == nil {
return nil, FeastTransformationServiceNotConfigured{}
}
entityNameToJoinKeyMap, expectedJoinKeysSet, err := onlineserving.GetEntityMaps(requestedFeatureViews, entities)
if err != nil {
return nil, err
}
err = onlineserving.ValidateFeatureRefs(requestedFeatureViews, fullFeatureNames)
if err != nil {
return nil, err
}
numRows, err := onlineserving.ValidateEntityValues(joinKeyToEntityValues, requestData, expectedJoinKeysSet)
if err != nil {
return nil, err
}
err = transformation.EnsureRequestedDataExist(requestedOnDemandFeatureViews, requestData)
if err != nil {
return nil, err
}
result := make([]*onlineserving.FeatureVector, 0)
arrowMemory := memory.NewGoAllocator()
featureViews := make([]*model.FeatureView, len(requestedFeatureViews))
index := 0
for _, featuresAndView := range requestedFeatureViews {
featureViews[index] = featuresAndView.View
index += 1
}
entitylessCase := false
for _, featureView := range featureViews {
if featureView.HasEntity(model.DUMMY_ENTITY_NAME) {
entitylessCase = true
break
}
}
if entitylessCase {
dummyEntityColumn := &prototypes.RepeatedValue{Val: make([]*prototypes.Value, numRows)}
for index := 0; index < numRows; index++ {
dummyEntityColumn.Val[index] = &model.DUMMY_ENTITY_VALUE
}
joinKeyToEntityValues[model.DUMMY_ENTITY_ID] = dummyEntityColumn
}
groupedRefs, err := onlineserving.GroupFeatureRefs(requestedFeatureViews, joinKeyToEntityValues, entityNameToJoinKeyMap, fullFeatureNames)
if err != nil {
return nil, err
}
for _, groupRef := range groupedRefs {
featureData, err := fs.readFromOnlineStore(ctx, groupRef.EntityKeys, groupRef.FeatureViewNames, groupRef.FeatureNames)
if err != nil {
return nil, err
}
vectors, err := onlineserving.TransposeFeatureRowsIntoColumns(
featureData,
groupRef,
requestedFeatureViews,
arrowMemory,
numRows,
)
if err != nil {
return nil, err
}
result = append(result, vectors...)
}
if fs.transformationCallback != nil || fs.transformationService != nil {
onDemandFeatures, err := transformation.AugmentResponseWithOnDemandTransforms(
ctx,
requestedOnDemandFeatureViews,
requestData,
joinKeyToEntityValues,
result,
fs.transformationCallback,
fs.transformationService,
arrowMemory,
numRows,
fullFeatureNames,
)
if err != nil {
return nil, err
}
result = append(result, onDemandFeatures...)
}
result, err = onlineserving.KeepOnlyRequestedFeatures(result, featureRefs, featureService, fullFeatureNames)
if err != nil {
return nil, err
}
entityColumns, err := onlineserving.EntitiesToFeatureVectors(joinKeyToEntityValues, arrowMemory, numRows)
result = append(entityColumns, result...)
return result, nil
}
func (fs *FeatureStore) DestructOnlineStore() {
fs.onlineStore.Destruct()
}
// ParseFeatures parses the kind field of a GetOnlineFeaturesRequest protobuf message
// and populates a Features struct with the result.
func (fs *FeatureStore) ParseFeatures(kind interface{}) (*Features, error) {
if featureList, ok := kind.(*serving.GetOnlineFeaturesRequest_Features); ok {
return &Features{FeaturesRefs: featureList.Features.GetVal(), FeatureService: nil}, nil
}
if featureServiceRequest, ok := kind.(*serving.GetOnlineFeaturesRequest_FeatureService); ok {
featureService, err := fs.registry.GetFeatureService(fs.config.Project, featureServiceRequest.FeatureService)
if err != nil {
return nil, err
}
return &Features{FeaturesRefs: nil, FeatureService: featureService}, nil
}
return nil, errors.New("cannot parse kind from GetOnlineFeaturesRequest")
}
func (fs *FeatureStore) GetFeatureService(name string) (*model.FeatureService, error) {
return fs.registry.GetFeatureService(fs.config.Project, name)
}
func (fs *FeatureStore) listAllViews() (map[string]*model.FeatureView, map[string]*model.OnDemandFeatureView, error) {
fvs := make(map[string]*model.FeatureView)
odFvs := make(map[string]*model.OnDemandFeatureView)
featureViews, err := fs.ListFeatureViews()
if err != nil {
return nil, nil, err
}
for _, featureView := range featureViews {
fvs[featureView.Base.Name] = featureView
}
streamFeatureViews, err := fs.ListStreamFeatureViews()
if err != nil {
return nil, nil, err
}
for _, streamFeatureView := range streamFeatureViews {
fvs[streamFeatureView.Base.Name] = streamFeatureView
}
onDemandFeatureViews, err := fs.registry.ListOnDemandFeatureViews(fs.config.Project)
if err != nil {
return nil, nil, err
}
for _, onDemandFeatureView := range onDemandFeatureViews {
odFvs[onDemandFeatureView.Base.Name] = onDemandFeatureView
}
return fvs, odFvs, nil
}
func (fs *FeatureStore) ListFeatureViews() ([]*model.FeatureView, error) {
featureViews, err := fs.registry.ListFeatureViews(fs.config.Project)
if err != nil {
return featureViews, err
}
return featureViews, nil
}
func (fs *FeatureStore) ListStreamFeatureViews() ([]*model.FeatureView, error) {
streamFeatureViews, err := fs.registry.ListStreamFeatureViews(fs.config.Project)
if err != nil {
return streamFeatureViews, err
}
return streamFeatureViews, nil
}
func (fs *FeatureStore) ListEntities(hideDummyEntity bool) ([]*model.Entity, error) {
allEntities, err := fs.registry.ListEntities(fs.config.Project)
if err != nil {
return allEntities, err
}
entities := make([]*model.Entity, 0)
for _, entity := range allEntities {
if entity.Name != model.DUMMY_ENTITY_NAME || !hideDummyEntity {
entities = append(entities, entity)
}
}
return entities, nil
}
func (fs *FeatureStore) ListOnDemandFeatureViews() ([]*model.OnDemandFeatureView, error) {
return fs.registry.ListOnDemandFeatureViews(fs.config.Project)
}
/*
Group feature views that share the same set of join keys. For each group, we store only unique rows and save indices to retrieve those
rows for each requested feature
*/
func (fs *FeatureStore) GetFeatureView(featureViewName string, hideDummyEntity bool) (*model.FeatureView, error) {
fv, err := fs.registry.GetFeatureView(fs.config.Project, featureViewName)
if err != nil {
return nil, err
}
if fv.HasEntity(model.DUMMY_ENTITY_NAME) && hideDummyEntity {
fv.EntityNames = []string{}
}
return fv, nil
}
func (fs *FeatureStore) readFromOnlineStore(ctx context.Context, entityRows []*prototypes.EntityKey,
requestedFeatureViewNames []string,
requestedFeatureNames []string,
) ([][]onlinestore.FeatureData, error) {
// Create a Datadog span from context
ctx, span := tracer.Start(ctx, "fs.readFromOnlineStore")
defer span.End()
numRows := len(entityRows)
entityRowsValue := make([]*prototypes.EntityKey, numRows)
for index, entityKey := range entityRows {
entityRowsValue[index] = &prototypes.EntityKey{JoinKeys: entityKey.JoinKeys, EntityValues: entityKey.EntityValues}
}
return fs.onlineStore.OnlineRead(ctx, entityRowsValue, requestedFeatureViewNames, requestedFeatureNames)
}
func (fs *FeatureStore) GetFcosMap() (map[string]*model.Entity, map[string]*model.FeatureView, map[string]*model.OnDemandFeatureView, error) {
odfvs, err := fs.ListOnDemandFeatureViews()
if err != nil {
return nil, nil, nil, err
}
fvs, err := fs.ListFeatureViews()
if err != nil {
return nil, nil, nil, err
}
entities, err := fs.ListEntities(true)
if err != nil {
return nil, nil, nil, err
}
entityMap := make(map[string]*model.Entity)
for _, entity := range entities {
entityMap[entity.Name] = entity
}
fvMap := make(map[string]*model.FeatureView)
for _, fv := range fvs {
fvMap[fv.Base.Name] = fv
}
odfvMap := make(map[string]*model.OnDemandFeatureView)
for _, odfv := range odfvs {
odfvMap[odfv.Base.Name] = odfv
}
return entityMap, fvMap, odfvMap, nil
}