|
| 1 | +import logging |
| 2 | +from concurrent import futures |
| 3 | + |
| 4 | +import grpc |
| 5 | +import pandas as pd |
| 6 | +from grpc_health.v1 import health, health_pb2_grpc |
| 7 | + |
| 8 | +from feast.data_source import PushMode |
| 9 | +from feast.errors import PushSourceNotFoundException |
| 10 | +from feast.feature_store import FeatureStore |
| 11 | +from feast.protos.feast.serving.GrpcServer_pb2 import ( |
| 12 | + PushResponse, |
| 13 | + WriteToOnlineStoreResponse, |
| 14 | +) |
| 15 | +from feast.protos.feast.serving.GrpcServer_pb2_grpc import ( |
| 16 | + GrpcFeatureServerServicer, |
| 17 | + add_GrpcFeatureServerServicer_to_server, |
| 18 | +) |
| 19 | + |
| 20 | + |
| 21 | +def parse(features): |
| 22 | + df = {} |
| 23 | + for i in features.keys(): |
| 24 | + df[i] = [features.get(i)] |
| 25 | + return pd.DataFrame.from_dict(df) |
| 26 | + |
| 27 | + |
| 28 | +class GrpcFeatureServer(GrpcFeatureServerServicer): |
| 29 | + fs: FeatureStore |
| 30 | + |
| 31 | + def __init__(self, fs: FeatureStore): |
| 32 | + self.fs = fs |
| 33 | + super().__init__() |
| 34 | + |
| 35 | + def Push(self, request, context): |
| 36 | + try: |
| 37 | + df = parse(request.features) |
| 38 | + if request.to == "offline": |
| 39 | + to = PushMode.OFFLINE |
| 40 | + elif request.to == "online": |
| 41 | + to = PushMode.ONLINE |
| 42 | + elif request.to == "online_and_offline": |
| 43 | + to = PushMode.ONLINE_AND_OFFLINE |
| 44 | + else: |
| 45 | + raise ValueError( |
| 46 | + f"{request.to} is not a supported push format. Please specify one of these ['online', 'offline', " |
| 47 | + f"'online_and_offline']." |
| 48 | + ) |
| 49 | + self.fs.push( |
| 50 | + push_source_name=request.push_source_name, |
| 51 | + df=df, |
| 52 | + allow_registry_cache=request.allow_registry_cache, |
| 53 | + to=to, |
| 54 | + ) |
| 55 | + except PushSourceNotFoundException as e: |
| 56 | + logging.exception(str(e)) |
| 57 | + context.set_code(grpc.StatusCode.INVALID_ARGUMENT) |
| 58 | + context.set_details(str(e)) |
| 59 | + return PushResponse(status=False) |
| 60 | + except Exception as e: |
| 61 | + logging.exception(str(e)) |
| 62 | + context.set_code(grpc.StatusCode.INTERNAL) |
| 63 | + context.set_details(str(e)) |
| 64 | + return PushResponse(status=False) |
| 65 | + return PushResponse(status=True) |
| 66 | + |
| 67 | + def WriteToOnlineStore(self, request, context): |
| 68 | + logging.warning( |
| 69 | + "write_to_online_store is deprecated. Please consider using Push instead" |
| 70 | + ) |
| 71 | + try: |
| 72 | + df = parse(request.features) |
| 73 | + self.fs.write_to_online_store( |
| 74 | + feature_view_name=request.feature_view_name, |
| 75 | + df=df, |
| 76 | + allow_registry_cache=request.allow_registry_cache, |
| 77 | + ) |
| 78 | + except Exception as e: |
| 79 | + logging.exception(str(e)) |
| 80 | + context.set_code(grpc.StatusCode.INTERNAL) |
| 81 | + context.set_details(str(e)) |
| 82 | + return PushResponse(status=False) |
| 83 | + return WriteToOnlineStoreResponse(status=True) |
| 84 | + |
| 85 | + |
| 86 | +def get_grpc_server(address: str, fs: FeatureStore, max_workers: int): |
| 87 | + server = grpc.server(futures.ThreadPoolExecutor(max_workers=max_workers)) |
| 88 | + add_GrpcFeatureServerServicer_to_server(GrpcFeatureServer(fs), server) |
| 89 | + health_servicer = health.HealthServicer( |
| 90 | + experimental_non_blocking=True, |
| 91 | + experimental_thread_pool=futures.ThreadPoolExecutor(max_workers=max_workers), |
| 92 | + ) |
| 93 | + health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server) |
| 94 | + server.add_insecure_port(address) |
| 95 | + return server |
0 commit comments