|
| 1 | +# Copyright 2021 The Feast Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import json |
| 15 | +import logging |
| 16 | +from datetime import datetime |
| 17 | +from typing import Any, Callable, Dict, List, Literal, Optional, Sequence, Tuple |
| 18 | + |
| 19 | +import requests |
| 20 | +from pydantic import StrictStr |
| 21 | + |
| 22 | +from feast import Entity, FeatureView, RepoConfig |
| 23 | +from feast.infra.online_stores.online_store import OnlineStore |
| 24 | +from feast.protos.feast.types.EntityKey_pb2 import EntityKey as EntityKeyProto |
| 25 | +from feast.protos.feast.types.Value_pb2 import Value as ValueProto |
| 26 | +from feast.repo_config import FeastConfigBaseModel |
| 27 | +from feast.type_map import python_values_to_proto_values |
| 28 | +from feast.value_type import ValueType |
| 29 | + |
| 30 | +logger = logging.getLogger(__name__) |
| 31 | + |
| 32 | + |
| 33 | +class RemoteOnlineStoreConfig(FeastConfigBaseModel): |
| 34 | + """Remote Online store config for remote online store""" |
| 35 | + |
| 36 | + type: Literal["remote"] = "remote" |
| 37 | + """Online store type selector""" |
| 38 | + |
| 39 | + path: StrictStr = "http://localhost:6566" |
| 40 | + """ str: Path to metadata store. |
| 41 | + If type is 'remote', then this is a URL for registry server """ |
| 42 | + |
| 43 | + |
| 44 | +class RemoteOnlineStore(OnlineStore): |
| 45 | + """ |
| 46 | + remote online store implementation wrapper to communicate with feast online server. |
| 47 | + """ |
| 48 | + |
| 49 | + def online_write_batch( |
| 50 | + self, |
| 51 | + config: RepoConfig, |
| 52 | + table: FeatureView, |
| 53 | + data: List[ |
| 54 | + Tuple[EntityKeyProto, Dict[str, ValueProto], datetime, Optional[datetime]] |
| 55 | + ], |
| 56 | + progress: Optional[Callable[[int], Any]], |
| 57 | + ) -> None: |
| 58 | + pass |
| 59 | + |
| 60 | + def online_read( |
| 61 | + self, |
| 62 | + config: RepoConfig, |
| 63 | + table: FeatureView, |
| 64 | + entity_keys: List[EntityKeyProto], |
| 65 | + requested_features: Optional[List[str]] = None, |
| 66 | + ) -> List[Tuple[Optional[datetime], Optional[Dict[str, ValueProto]]]]: |
| 67 | + assert isinstance(config.online_store, RemoteOnlineStoreConfig) |
| 68 | + config.online_store.__class__ = RemoteOnlineStoreConfig |
| 69 | + |
| 70 | + req_body = self._construct_online_read_api_json_request( |
| 71 | + entity_keys, table, requested_features |
| 72 | + ) |
| 73 | + response = requests.post( |
| 74 | + f"{config.online_store.path}/get-online-features", data=req_body |
| 75 | + ) |
| 76 | + if response.status_code == 200: |
| 77 | + logger.debug("Able to retrieve the online features from feature server.") |
| 78 | + response_json = json.loads(response.text) |
| 79 | + event_ts = self._get_event_ts(response_json) |
| 80 | + # Iterating over results and converting the API results in column format to row format. |
| 81 | + result_tuples: List[ |
| 82 | + Tuple[Optional[datetime], Optional[Dict[str, ValueProto]]] |
| 83 | + ] = [] |
| 84 | + for feature_value_index in range(len(entity_keys)): |
| 85 | + feature_values_dict: Dict[str, ValueProto] = dict() |
| 86 | + for index, feature_name in enumerate( |
| 87 | + response_json["metadata"]["feature_names"] |
| 88 | + ): |
| 89 | + if ( |
| 90 | + requested_features is not None |
| 91 | + and feature_name in requested_features |
| 92 | + ): |
| 93 | + if ( |
| 94 | + response_json["results"][index]["statuses"][ |
| 95 | + feature_value_index |
| 96 | + ] |
| 97 | + == "PRESENT" |
| 98 | + ): |
| 99 | + message = python_values_to_proto_values( |
| 100 | + [ |
| 101 | + response_json["results"][index]["values"][ |
| 102 | + feature_value_index |
| 103 | + ] |
| 104 | + ], |
| 105 | + ValueType.UNKNOWN, |
| 106 | + ) |
| 107 | + feature_values_dict[feature_name] = message[0] |
| 108 | + else: |
| 109 | + feature_values_dict[feature_name] = ValueProto() |
| 110 | + |
| 111 | + result_tuples.append((event_ts, feature_values_dict)) |
| 112 | + return result_tuples |
| 113 | + else: |
| 114 | + error_msg = f"Unable to retrieve the online store data using feature server API. Error_code={response.status_code}, error_message={response.reason}" |
| 115 | + logger.error(error_msg) |
| 116 | + raise RuntimeError(error_msg) |
| 117 | + |
| 118 | + def _construct_online_read_api_json_request( |
| 119 | + self, |
| 120 | + entity_keys: List[EntityKeyProto], |
| 121 | + table: FeatureView, |
| 122 | + requested_features: Optional[List[str]] = None, |
| 123 | + ): |
| 124 | + api_requested_features = [] |
| 125 | + if requested_features is not None: |
| 126 | + for requested_feature in requested_features: |
| 127 | + api_requested_features.append(f"{table.name}:{requested_feature}") |
| 128 | + |
| 129 | + entity_values = [] |
| 130 | + entity_key = "" |
| 131 | + for row in entity_keys: |
| 132 | + entity_key = row.join_keys[0] |
| 133 | + entity_values.append( |
| 134 | + getattr(row.entity_values[0], row.entity_values[0].WhichOneof("val")) |
| 135 | + ) |
| 136 | + |
| 137 | + req_body = json.dumps( |
| 138 | + { |
| 139 | + "features": api_requested_features, |
| 140 | + "entities": {entity_key: entity_values}, |
| 141 | + } |
| 142 | + ) |
| 143 | + return req_body |
| 144 | + |
| 145 | + def _check_if_feature_requested(self, feature_name, requested_features): |
| 146 | + for requested_feature in requested_features: |
| 147 | + if feature_name in requested_feature: |
| 148 | + return True |
| 149 | + return False |
| 150 | + |
| 151 | + def _get_event_ts(self, response_json) -> datetime: |
| 152 | + event_ts = "" |
| 153 | + if len(response_json["results"]) > 1: |
| 154 | + event_ts = response_json["results"][1]["event_timestamps"][0] |
| 155 | + return datetime.fromisoformat(event_ts.replace("Z", "+00:00")) |
| 156 | + |
| 157 | + def update( |
| 158 | + self, |
| 159 | + config: RepoConfig, |
| 160 | + tables_to_delete: Sequence[FeatureView], |
| 161 | + tables_to_keep: Sequence[FeatureView], |
| 162 | + entities_to_delete: Sequence[Entity], |
| 163 | + entities_to_keep: Sequence[Entity], |
| 164 | + partial: bool, |
| 165 | + ): |
| 166 | + pass |
| 167 | + |
| 168 | + def teardown( |
| 169 | + self, |
| 170 | + config: RepoConfig, |
| 171 | + tables: Sequence[FeatureView], |
| 172 | + entities: Sequence[Entity], |
| 173 | + ): |
| 174 | + pass |
0 commit comments