|
| 1 | +package onlinestore |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/hex" |
| 6 | + "fmt" |
| 7 | + awsConfig "github.com/aws/aws-sdk-go-v2/config" |
| 8 | + "github.com/aws/aws-sdk-go-v2/service/dynamodb" |
| 9 | + dtypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" |
| 10 | + "github.com/feast-dev/feast/go/internal/feast/registry" |
| 11 | + "github.com/feast-dev/feast/go/protos/feast/serving" |
| 12 | + "github.com/feast-dev/feast/go/protos/feast/types" |
| 13 | + "github.com/roberson-io/mmh3" |
| 14 | + "golang.org/x/sync/errgroup" |
| 15 | + "golang.org/x/sync/semaphore" |
| 16 | + "google.golang.org/protobuf/proto" |
| 17 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 18 | + "runtime" |
| 19 | + "sync" |
| 20 | + "time" |
| 21 | +) |
| 22 | + |
| 23 | +type batchResult struct { |
| 24 | + index int |
| 25 | + response *dynamodb.BatchGetItemOutput |
| 26 | + err error |
| 27 | +} |
| 28 | + |
| 29 | +type DynamodbOnlineStore struct { |
| 30 | + // Feast project name |
| 31 | + // TODO: Should we remove project as state that is tracked at the store level? |
| 32 | + project string |
| 33 | + |
| 34 | + client *dynamodb.Client |
| 35 | + |
| 36 | + config *registry.RepoConfig |
| 37 | + |
| 38 | + // dynamodb configuration |
| 39 | + consistentRead *bool |
| 40 | + batchSize *int |
| 41 | +} |
| 42 | + |
| 43 | +func NewDynamodbOnlineStore(project string, config *registry.RepoConfig, onlineStoreConfig map[string]interface{}) (*DynamodbOnlineStore, error) { |
| 44 | + store := DynamodbOnlineStore{ |
| 45 | + project: project, |
| 46 | + config: config, |
| 47 | + } |
| 48 | + |
| 49 | + // aws configuration |
| 50 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 51 | + defer cancel() |
| 52 | + cfg, err := awsConfig.LoadDefaultConfig(ctx) |
| 53 | + if err != nil { |
| 54 | + panic(err) |
| 55 | + } |
| 56 | + store.client = dynamodb.NewFromConfig(cfg) |
| 57 | + |
| 58 | + // dynamodb configuration |
| 59 | + consistentRead, ok := onlineStoreConfig["consistent_reads"].(bool) |
| 60 | + if !ok { |
| 61 | + consistentRead = false |
| 62 | + } |
| 63 | + store.consistentRead = &consistentRead |
| 64 | + |
| 65 | + var batchSize int |
| 66 | + if batchSizeFloat, ok := onlineStoreConfig["batch_size"].(float64); ok { |
| 67 | + batchSize = int(batchSizeFloat) |
| 68 | + } else { |
| 69 | + batchSize = 40 |
| 70 | + } |
| 71 | + store.batchSize = &batchSize |
| 72 | + |
| 73 | + return &store, nil |
| 74 | +} |
| 75 | + |
| 76 | +func (d *DynamodbOnlineStore) OnlineRead(ctx context.Context, entityKeys []*types.EntityKey, featureViewNames []string, featureNames []string) ([][]FeatureData, error) { |
| 77 | + // prevent resource waste in case context is canceled earlier |
| 78 | + if ctx.Err() != nil { |
| 79 | + return nil, ctx.Err() |
| 80 | + } |
| 81 | + |
| 82 | + results := make([][]FeatureData, len(entityKeys)) |
| 83 | + |
| 84 | + // serialize entity key into entity hash id |
| 85 | + entityIndexMap := make(map[string]int) |
| 86 | + entityIds := make([]string, 0, len(entityKeys)) |
| 87 | + unprocessedEntityIds := make(map[string]bool) |
| 88 | + for i, entityKey := range entityKeys { |
| 89 | + serKey, err := serializeEntityKey(entityKey, d.config.EntityKeySerializationVersion) |
| 90 | + if err != nil { |
| 91 | + return nil, err |
| 92 | + } |
| 93 | + entityId := hex.EncodeToString(mmh3.Hashx64_128(*serKey, 0)) |
| 94 | + entityIds = append(entityIds, entityId) |
| 95 | + entityIndexMap[entityId] = i |
| 96 | + unprocessedEntityIds[entityId] = false |
| 97 | + } |
| 98 | + |
| 99 | + // metadata from feature views, feature names |
| 100 | + featureMap, featureNamesIndex, err := makeFeatureMeta(featureViewNames, featureNames) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + |
| 105 | + // initialize `FeatureData` slice |
| 106 | + featureCount := len(featureNamesIndex) |
| 107 | + for i := 0; i < len(results); i++ { |
| 108 | + results[i] = make([]FeatureData, featureCount) |
| 109 | + } |
| 110 | + |
| 111 | + // controls the maximum number of concurrent goroutines sending requests to DynamoDB using a semaphore |
| 112 | + cpuCount := runtime.NumCPU() |
| 113 | + sem := semaphore.NewWeighted(int64(cpuCount * 2)) |
| 114 | + |
| 115 | + var mu sync.Mutex |
| 116 | + for featureViewName, featureNames := range featureMap { |
| 117 | + tableName := fmt.Sprintf("%s.%s", d.project, featureViewName) |
| 118 | + |
| 119 | + var batchGetItemInputs []*dynamodb.BatchGetItemInput |
| 120 | + batchSize := *d.batchSize |
| 121 | + for i := 0; i < len(entityIds); i += batchSize { |
| 122 | + end := i + batchSize |
| 123 | + if end > len(entityIds) { |
| 124 | + end = len(entityIds) |
| 125 | + } |
| 126 | + batchEntityIds := entityIds[i:end] |
| 127 | + entityIdBatch := make([]map[string]dtypes.AttributeValue, len(batchEntityIds)) |
| 128 | + for i, entityId := range batchEntityIds { |
| 129 | + entityIdBatch[i] = map[string]dtypes.AttributeValue{ |
| 130 | + "entity_id": &dtypes.AttributeValueMemberS{Value: entityId}, |
| 131 | + } |
| 132 | + } |
| 133 | + batchGetItemInput := &dynamodb.BatchGetItemInput{ |
| 134 | + RequestItems: map[string]dtypes.KeysAndAttributes{ |
| 135 | + tableName: { |
| 136 | + Keys: entityIdBatch, |
| 137 | + ConsistentRead: d.consistentRead, |
| 138 | + }, |
| 139 | + }, |
| 140 | + } |
| 141 | + batchGetItemInputs = append(batchGetItemInputs, batchGetItemInput) |
| 142 | + } |
| 143 | + |
| 144 | + // goroutines sending requests to DynamoDB |
| 145 | + errGroup, ctx := errgroup.WithContext(ctx) |
| 146 | + for i, batchGetItemInput := range batchGetItemInputs { |
| 147 | + _, batchGetItemInput := i, batchGetItemInput |
| 148 | + errGroup.Go(func() error { |
| 149 | + if err := sem.Acquire(ctx, 1); err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + defer sem.Release(1) |
| 153 | + |
| 154 | + resp, err := d.client.BatchGetItem(ctx, batchGetItemInput) |
| 155 | + if err != nil { |
| 156 | + return err |
| 157 | + } |
| 158 | + |
| 159 | + // in case there is no entity id of a feature view in dynamodb |
| 160 | + batchSize := len(resp.Responses[tableName]) |
| 161 | + if batchSize == 0 { |
| 162 | + return nil |
| 163 | + } |
| 164 | + |
| 165 | + // process response from dynamodb |
| 166 | + for j := 0; j < batchSize; j++ { |
| 167 | + entityId := resp.Responses[tableName][j]["entity_id"].(*dtypes.AttributeValueMemberS).Value |
| 168 | + timestampString := resp.Responses[tableName][j]["event_ts"].(*dtypes.AttributeValueMemberS).Value |
| 169 | + t, err := time.Parse("2006-01-02 15:04:05-07:00", timestampString) |
| 170 | + if err != nil { |
| 171 | + return err |
| 172 | + } |
| 173 | + timeStamp := timestamppb.New(t) |
| 174 | + |
| 175 | + featureValues := resp.Responses[tableName][j]["values"].(*dtypes.AttributeValueMemberM).Value |
| 176 | + entityIndex := entityIndexMap[entityId] |
| 177 | + |
| 178 | + for _, featureName := range featureNames { |
| 179 | + featureValue := featureValues[featureName].(*dtypes.AttributeValueMemberB).Value |
| 180 | + var value types.Value |
| 181 | + if err := proto.Unmarshal(featureValue, &value); err != nil { |
| 182 | + return err |
| 183 | + } |
| 184 | + featureIndex := featureNamesIndex[featureName] |
| 185 | + |
| 186 | + mu.Lock() |
| 187 | + results[entityIndex][featureIndex] = FeatureData{Reference: serving.FeatureReferenceV2{FeatureViewName: featureViewName, FeatureName: featureName}, |
| 188 | + Timestamp: timestamppb.Timestamp{Seconds: timeStamp.Seconds, Nanos: timeStamp.Nanos}, |
| 189 | + Value: types.Value{Val: value.Val}, |
| 190 | + } |
| 191 | + mu.Unlock() |
| 192 | + } |
| 193 | + |
| 194 | + mu.Lock() |
| 195 | + delete(unprocessedEntityIds, entityId) |
| 196 | + mu.Unlock() |
| 197 | + } |
| 198 | + return nil |
| 199 | + }) |
| 200 | + } |
| 201 | + if err := errGroup.Wait(); err != nil { |
| 202 | + return nil, err |
| 203 | + } |
| 204 | + |
| 205 | + // process null imputation for entity ids that don't exist in dynamodb |
| 206 | + currentTime := timestamppb.Now() // TODO: should use a different timestamp? |
| 207 | + for entityId, _ := range unprocessedEntityIds { |
| 208 | + entityIndex := entityIndexMap[entityId] |
| 209 | + for _, featureName := range featureNames { |
| 210 | + featureIndex := featureNamesIndex[featureName] |
| 211 | + results[entityIndex][featureIndex] = FeatureData{Reference: serving.FeatureReferenceV2{FeatureViewName: featureViewName, FeatureName: featureName}, |
| 212 | + Timestamp: timestamppb.Timestamp{Seconds: currentTime.Seconds, Nanos: currentTime.Nanos}, |
| 213 | + Value: types.Value{Val: &types.Value_NullVal{NullVal: types.Null_NULL}}, |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + return results, nil |
| 220 | +} |
| 221 | + |
| 222 | +func (d *DynamodbOnlineStore) Destruct() { |
| 223 | + |
| 224 | +} |
| 225 | + |
| 226 | +func makeFeatureMeta(featureViewNames []string, featureNames []string) (map[string][]string, map[string]int, error) { |
| 227 | + if len(featureViewNames) != len(featureNames) { |
| 228 | + return nil, nil, fmt.Errorf("the lengths of featureViewNames and featureNames must be the same. got=%d, %d", len(featureViewNames), len(featureNames)) |
| 229 | + } |
| 230 | + featureMap := make(map[string][]string) |
| 231 | + featureNamesIndex := make(map[string]int) |
| 232 | + for i := 0; i < len(featureViewNames); i++ { |
| 233 | + featureViewName := featureViewNames[i] |
| 234 | + featureName := featureNames[i] |
| 235 | + |
| 236 | + featureMap[featureViewName] = append(featureMap[featureViewName], featureName) |
| 237 | + featureNamesIndex[featureName] = i |
| 238 | + } |
| 239 | + return featureMap, featureNamesIndex, nil |
| 240 | +} |
0 commit comments