|
| 1 | +package server |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/feast-dev/feast/go/internal/feast" |
| 8 | + "github.com/feast-dev/feast/go/internal/feast/model" |
| 9 | + "github.com/feast-dev/feast/go/internal/feast/server/logging" |
| 10 | + "github.com/feast-dev/feast/go/protos/feast/serving" |
| 11 | + prototypes "github.com/feast-dev/feast/go/protos/feast/types" |
| 12 | + "github.com/feast-dev/feast/go/types" |
| 13 | + "net/http" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +type httpServer struct { |
| 18 | + fs *feast.FeatureStore |
| 19 | + loggingService *logging.LoggingService |
| 20 | + server *http.Server |
| 21 | +} |
| 22 | + |
| 23 | +// Some Feast types aren't supported during JSON conversion |
| 24 | +type repeatedValue struct { |
| 25 | + stringVal []string |
| 26 | + int64Val []int64 |
| 27 | + doubleVal []float64 |
| 28 | + boolVal []bool |
| 29 | + stringListVal [][]string |
| 30 | + int64ListVal [][]int64 |
| 31 | + doubleListVal [][]float64 |
| 32 | + boolListVal [][]bool |
| 33 | +} |
| 34 | + |
| 35 | +func (u *repeatedValue) UnmarshalJSON(data []byte) error { |
| 36 | + isString := false |
| 37 | + isDouble := false |
| 38 | + isInt64 := false |
| 39 | + isArray := false |
| 40 | + openBraketCounter := 0 |
| 41 | + for _, b := range data { |
| 42 | + if b == '"' { |
| 43 | + isString = true |
| 44 | + } |
| 45 | + if b == '.' { |
| 46 | + isDouble = true |
| 47 | + } |
| 48 | + if b >= '0' && b <= '9' { |
| 49 | + isInt64 = true |
| 50 | + } |
| 51 | + if b == '[' { |
| 52 | + openBraketCounter++ |
| 53 | + if openBraketCounter > 1 { |
| 54 | + isArray = true |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + var err error |
| 59 | + if !isArray { |
| 60 | + if isString { |
| 61 | + err = json.Unmarshal(data, &u.stringVal) |
| 62 | + } else if isDouble { |
| 63 | + err = json.Unmarshal(data, &u.doubleVal) |
| 64 | + } else if isInt64 { |
| 65 | + err = json.Unmarshal(data, &u.int64Val) |
| 66 | + } else { |
| 67 | + err = json.Unmarshal(data, &u.boolVal) |
| 68 | + } |
| 69 | + } else { |
| 70 | + if isString { |
| 71 | + err = json.Unmarshal(data, &u.stringListVal) |
| 72 | + } else if isDouble { |
| 73 | + err = json.Unmarshal(data, &u.doubleListVal) |
| 74 | + } else if isInt64 { |
| 75 | + err = json.Unmarshal(data, &u.int64ListVal) |
| 76 | + } else { |
| 77 | + err = json.Unmarshal(data, &u.boolListVal) |
| 78 | + } |
| 79 | + } |
| 80 | + return err |
| 81 | +} |
| 82 | + |
| 83 | +func (u *repeatedValue) ToProto() *prototypes.RepeatedValue { |
| 84 | + proto := new(prototypes.RepeatedValue) |
| 85 | + if u.stringVal != nil { |
| 86 | + for _, val := range u.stringVal { |
| 87 | + proto.Val = append(proto.Val, &prototypes.Value{Val: &prototypes.Value_StringVal{StringVal: val}}) |
| 88 | + } |
| 89 | + } |
| 90 | + if u.int64Val != nil { |
| 91 | + for _, val := range u.int64Val { |
| 92 | + proto.Val = append(proto.Val, &prototypes.Value{Val: &prototypes.Value_Int64Val{Int64Val: val}}) |
| 93 | + } |
| 94 | + } |
| 95 | + if u.doubleVal != nil { |
| 96 | + for _, val := range u.doubleVal { |
| 97 | + proto.Val = append(proto.Val, &prototypes.Value{Val: &prototypes.Value_DoubleVal{DoubleVal: val}}) |
| 98 | + } |
| 99 | + } |
| 100 | + if u.boolVal != nil { |
| 101 | + for _, val := range u.boolVal { |
| 102 | + proto.Val = append(proto.Val, &prototypes.Value{Val: &prototypes.Value_BoolVal{BoolVal: val}}) |
| 103 | + } |
| 104 | + } |
| 105 | + if u.stringListVal != nil { |
| 106 | + for _, val := range u.stringListVal { |
| 107 | + proto.Val = append(proto.Val, &prototypes.Value{Val: &prototypes.Value_StringListVal{StringListVal: &prototypes.StringList{Val: val}}}) |
| 108 | + } |
| 109 | + } |
| 110 | + if u.int64ListVal != nil { |
| 111 | + for _, val := range u.int64ListVal { |
| 112 | + proto.Val = append(proto.Val, &prototypes.Value{Val: &prototypes.Value_Int64ListVal{Int64ListVal: &prototypes.Int64List{Val: val}}}) |
| 113 | + } |
| 114 | + } |
| 115 | + if u.doubleListVal != nil { |
| 116 | + for _, val := range u.doubleListVal { |
| 117 | + proto.Val = append(proto.Val, &prototypes.Value{Val: &prototypes.Value_DoubleListVal{DoubleListVal: &prototypes.DoubleList{Val: val}}}) |
| 118 | + } |
| 119 | + } |
| 120 | + if u.boolListVal != nil { |
| 121 | + for _, val := range u.boolListVal { |
| 122 | + proto.Val = append(proto.Val, &prototypes.Value{Val: &prototypes.Value_BoolListVal{BoolListVal: &prototypes.BoolList{Val: val}}}) |
| 123 | + } |
| 124 | + } |
| 125 | + return proto |
| 126 | +} |
| 127 | + |
| 128 | +type getOnlineFeaturesRequest struct { |
| 129 | + FeatureService *string `json:"feature_service"` |
| 130 | + Features []string `json:"features"` |
| 131 | + Entities map[string]repeatedValue `json:"entities"` |
| 132 | + FullFeatureNames bool `json:"full_feature_names"` |
| 133 | + RequestContext map[string]repeatedValue `json:"request_context"` |
| 134 | +} |
| 135 | + |
| 136 | +func NewHttpServer(fs *feast.FeatureStore, loggingService *logging.LoggingService) *httpServer { |
| 137 | + return &httpServer{fs: fs, loggingService: loggingService} |
| 138 | +} |
| 139 | + |
| 140 | +func (s *httpServer) getOnlineFeatures(w http.ResponseWriter, r *http.Request) { |
| 141 | + if r.Method != "POST" { |
| 142 | + http.NotFound(w, r) |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + decoder := json.NewDecoder(r.Body) |
| 147 | + var request getOnlineFeaturesRequest |
| 148 | + err := decoder.Decode(&request) |
| 149 | + if err != nil { |
| 150 | + http.Error(w, fmt.Sprintf("Error decoding JSON request data: %+v", err), http.StatusInternalServerError) |
| 151 | + return |
| 152 | + } |
| 153 | + var featureService *model.FeatureService |
| 154 | + if request.FeatureService != nil { |
| 155 | + featureService, err = s.fs.GetFeatureService(*request.FeatureService) |
| 156 | + if err != nil { |
| 157 | + http.Error(w, fmt.Sprintf("Error getting feature service from registry: %+v", err), http.StatusInternalServerError) |
| 158 | + return |
| 159 | + } |
| 160 | + } |
| 161 | + entitiesProto := make(map[string]*prototypes.RepeatedValue) |
| 162 | + for key, value := range request.Entities { |
| 163 | + entitiesProto[key] = value.ToProto() |
| 164 | + } |
| 165 | + requestContextProto := make(map[string]*prototypes.RepeatedValue) |
| 166 | + for key, value := range request.RequestContext { |
| 167 | + requestContextProto[key] = value.ToProto() |
| 168 | + } |
| 169 | + |
| 170 | + featureVectors, err := s.fs.GetOnlineFeatures( |
| 171 | + r.Context(), |
| 172 | + request.Features, |
| 173 | + featureService, |
| 174 | + entitiesProto, |
| 175 | + requestContextProto, |
| 176 | + request.FullFeatureNames) |
| 177 | + |
| 178 | + if err != nil { |
| 179 | + http.Error(w, fmt.Sprintf("Error getting feature vector: %+v", err), http.StatusInternalServerError) |
| 180 | + return |
| 181 | + } |
| 182 | + |
| 183 | + var featureNames []string |
| 184 | + var results []map[string]interface{} |
| 185 | + for _, vector := range featureVectors { |
| 186 | + featureNames = append(featureNames, vector.Name) |
| 187 | + result := make(map[string]interface{}) |
| 188 | + var statuses []string |
| 189 | + for _, status := range vector.Statuses { |
| 190 | + statuses = append(statuses, status.String()) |
| 191 | + } |
| 192 | + var timestamps []string |
| 193 | + for _, timestamp := range vector.Timestamps { |
| 194 | + timestamps = append(timestamps, timestamp.AsTime().Format(time.RFC3339)) |
| 195 | + } |
| 196 | + |
| 197 | + result["statuses"] = statuses |
| 198 | + result["event_timestamps"] = timestamps |
| 199 | + // Note, that vector.Values is an Arrow Array, but this type implements JSON Marshaller. |
| 200 | + // So, it's not necessary to pre-process it in any way. |
| 201 | + result["values"] = vector.Values |
| 202 | + |
| 203 | + results = append(results, result) |
| 204 | + } |
| 205 | + |
| 206 | + response := map[string]interface{}{ |
| 207 | + "metadata": map[string]interface{}{ |
| 208 | + "feature_names": featureNames, |
| 209 | + }, |
| 210 | + "results": results, |
| 211 | + } |
| 212 | + |
| 213 | + err = json.NewEncoder(w).Encode(response) |
| 214 | + |
| 215 | + if err != nil { |
| 216 | + http.Error(w, fmt.Sprintf("Error encoding response: %+v", err), http.StatusInternalServerError) |
| 217 | + return |
| 218 | + } |
| 219 | + |
| 220 | + w.Header().Set("Content-Type", "application/json") |
| 221 | + |
| 222 | + if featureService != nil && featureService.LoggingConfig != nil && s.loggingService != nil { |
| 223 | + logger, err := s.loggingService.GetOrCreateLogger(featureService) |
| 224 | + if err != nil { |
| 225 | + http.Error(w, fmt.Sprintf("Couldn't instantiate logger for feature service %s: %+v", featureService.Name, err), http.StatusInternalServerError) |
| 226 | + return |
| 227 | + } |
| 228 | + |
| 229 | + requestId := GenerateRequestId() |
| 230 | + |
| 231 | + // Note: we're converting arrow to proto for feature logging. In the future we should |
| 232 | + // base feature logging on arrow so that we don't have to do this extra conversion. |
| 233 | + var featureVectorProtos []*serving.GetOnlineFeaturesResponse_FeatureVector |
| 234 | + for _, vector := range featureVectors[len(request.Entities):] { |
| 235 | + values, err := types.ArrowValuesToProtoValues(vector.Values) |
| 236 | + if err != nil { |
| 237 | + http.Error(w, fmt.Sprintf("Couldn't convert arrow values into protobuf: %+v", err), http.StatusInternalServerError) |
| 238 | + return |
| 239 | + } |
| 240 | + featureVectorProtos = append(featureVectorProtos, &serving.GetOnlineFeaturesResponse_FeatureVector{ |
| 241 | + Values: values, |
| 242 | + Statuses: vector.Statuses, |
| 243 | + EventTimestamps: vector.Timestamps, |
| 244 | + }) |
| 245 | + } |
| 246 | + |
| 247 | + err = logger.Log(entitiesProto, featureVectorProtos, featureNames[len(request.Entities):], requestContextProto, requestId) |
| 248 | + if err != nil { |
| 249 | + http.Error(w, fmt.Sprintf("LoggerImpl error[%s]: %+v", featureService.Name, err), http.StatusInternalServerError) |
| 250 | + return |
| 251 | + } |
| 252 | + } |
| 253 | +} |
| 254 | + |
| 255 | +func (s *httpServer) Serve(host string, port int) error { |
| 256 | + s.server = &http.Server{Addr: fmt.Sprintf("%s:%d", host, port), Handler: nil} |
| 257 | + http.HandleFunc("/get-online-features", s.getOnlineFeatures) |
| 258 | + err := s.server.ListenAndServe() |
| 259 | + // Don't return the error if it's caused by graceful shutdown using Stop() |
| 260 | + if err == http.ErrServerClosed { |
| 261 | + return nil |
| 262 | + } |
| 263 | + return err |
| 264 | +} |
| 265 | +func (s *httpServer) Stop() error { |
| 266 | + if s.server != nil { |
| 267 | + return s.server.Shutdown(context.Background()) |
| 268 | + } |
| 269 | + return nil |
| 270 | +} |
0 commit comments