Пример #1
0
def grpc_serve():
    # the +5 you see below re: max_workers is a hack to avoid thread starvation
    # working on a proper workaround
    server = grpc.server(
        futures.ThreadPoolExecutor(max_workers=multiprocessing.cpu_count() +
                                   5))

    # Add the application servicer to the server.
    whereami_pb2_grpc.add_WhereamiServicer_to_server(WhereamigRPC(), server)

    # Create a health check servicer. We use the non-blocking implementation
    # to avoid thread starvation.
    health_servicer = health.HealthServicer(
        experimental_non_blocking=True,
        experimental_thread_pool=futures.ThreadPoolExecutor(max_workers=1))
    health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)

    # Create a tuple of all of the services we want to export via reflection.
    services = tuple(
        service.full_name
        for service in whereami_pb2.DESCRIPTOR.services_by_name.values()) + (
            reflection.SERVICE_NAME, health.SERVICE_NAME)

    # Add the reflection service to the server.
    reflection.enable_server_reflection(services, server)
    server.add_insecure_port('[::]:9090')
    server.start()

    # Mark all services as healthy.
    overall_server_health = ""
    for service in services + (overall_server_health, ):
        health_servicer.set(service, health_pb2.HealthCheckResponse.SERVING)

    # Park the main application thread.
    server.wait_for_termination()
Пример #2
0
def serve(port: int, hostname: str):
    server = grpc.server(
        futures.ThreadPoolExecutor(max_workers=multiprocessing.cpu_count()))

    # Add the application servicer to the server.
    helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(hostname),
                                                      server)

    # Create a health check servicer. We use the non-blocking implementation
    # to avoid thread starvation.
    health_servicer = health.HealthServicer(
        experimental_non_blocking=True,
        experimental_thread_pool=futures.ThreadPoolExecutor(max_workers=1))
    health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)

    # Create a tuple of all of the services we want to export via reflection.
    services = tuple(
        service.full_name
        for service in helloworld_pb2.DESCRIPTOR.services_by_name.values()) + (
            reflection.SERVICE_NAME, health.SERVICE_NAME)

    # Add the reflection service to the server.
    reflection.enable_server_reflection(services, server)
    server.add_insecure_port(f"[::]:{port}")
    server.start()

    # Mark all services as healthy.
    overall_server_health = ""
    for service in services + (overall_server_health, ):
        health_servicer.set(service, health_pb2.HealthCheckResponse.SERVING)

    # Park the main application thread.
    server.wait_for_termination()
Пример #3
0
 def __init__(self,
              host: str,
              *,
              port: int = 0,
              register: bool = False,
              workers: int = 10,
              write_address: bool = False,
              config: 'Optional[mtap.Config]' = None):
     if host is None:
         host = '127.0.0.1'
     if port is None:
         port = 0
     self.write_address = write_address
     thread_pool = ThreadPoolExecutor(max_workers=workers)
     if config is None:
         config = _config.Config()
     server = grpc.server(thread_pool,
                          options=[
                              ('grpc.max_send_message_length',
                               config['grpc.max_send_message_length']),
                              ('grpc.max_receive_message_length',
                               config['grpc.max_receive_message_length'])
                          ])
     servicer = EventsServicer()
     events_pb2_grpc.add_EventsServicer_to_server(servicer, server)
     health_servicer = health.HealthServicer()
     health_servicer.set('', 'SERVING')
     health_servicer.set(constants.EVENTS_SERVICE_NAME, 'SERVING')
     health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)
     self._port = server.add_insecure_port(host + ':' + str(port))
     self._server = server
     self._address = host
     self._config = _config.Config()
     self._register = register
     self._address_file = None
Пример #4
0
def serve(host='[::]',
          port=5000,
          max_workers=4,
          max_receive_message_length=MAX_RECEIVE_MESSAGE_LENGTH,
          max_send_message_length=MAX_SEND_MESSAGE_LENGTH):

    servicer = health.HealthServicer()

    servicer.set('', health_pb2.HealthCheckResponse.SERVING)

    # Asset
    servicer.set('get_asset', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('add_asset', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('update_asset', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('remove_asset', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('exists_asset', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('get_asset_info', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('get_task_state', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('get_bucket', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('query', health_pb2.HealthCheckResponse.SERVING)

    # Bucket
    servicer.set('add_bucket', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('update_bucket', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('remove_bucket', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('exists_bucket', health_pb2.HealthCheckResponse.SERVING)
    servicer.set('get_assets', health_pb2.HealthCheckResponse.SERVING)

    options = [('grpc.max_receive_message_length', max_receive_message_length),
               ('grpc.max_send_message_length', max_send_message_length)]

    server = grpc.server(futures.ThreadPoolExecutor(max_workers=max_workers),
                         options=options)

    opac_pb2.add_AssetServiceServicer_to_server(Asset(), server)
    opac_pb2.add_BucketServiceServicer_to_server(AssetBucket(), server)

    # Health service
    health_pb2.add_HealthServicer_to_server(servicer, server)

    # Set port and Start Server
    server.add_insecure_port('{0}:{1}'.format(host, port))
    server.start()

    logging.info(
        'Started GRPC server on localhost, port: {0}, accept connections!'.
        format(port))

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        logging.info('User stopping server...')
        server.stop(0)
        logging.info('Server stopped; exiting.')
    except Exception as e:
        logging.info('Caught exception "%s"; stopping server...', e)
        server.stop(0)
        logging.info('Server stopped; exiting.')
 def _add_servicers(self):
     logger.info("Adding servicers")
     logger.debug("Adding HealthServicer")
     health_servicer = health.HealthServicer()
     health_pb2_grpc.add_HealthServicer_to_server(health_servicer,
                                                  self.server)
     logger.debug("Adding StatisticsServicer")
     add_StatisticsProcesserServicer_to_server(Calculator(), self.server)
Пример #6
0
 def __init__(self, name: str, max_workers: int = 10):
     self.name = name
     self.port = GRPC_PORT
     self.server = grpc.server(
         futures.ThreadPoolExecutor(max_workers=max_workers))
     self.healthServer = health.HealthServicer()
     health_pb2_grpc.add_HealthServicer_to_server(self.healthServer,
                                                  self.server)
     self.status = Status.UNKNOWN
Пример #7
0
 def __init__(
     self,
     args: argparse.Namespace,
     **kwargs,
 ):
     """Initialize grpc and data request handling.
     :param args: args from CLI
     :param kwargs: keyword args
     """
     self._health_servicer = health.HealthServicer(experimental_non_blocking=True)
     super().__init__(args, **kwargs)
Пример #8
0
    def _run(
        cls,
        config: DefaultConfig,
        target: str,
        server_credentials: Optional[grpc.ServerCredentials] = None,
        block: Optional[bool] = None,
    ):
        self = cls(config)

        for bp_cls in self.get_blueprints():
            self.register_blueprint(bp_cls)

        services: Tuple[str,
                        ...] = (reflection.SERVICE_NAME, health.SERVICE_NAME)
        for name, bp in self.blueprints.items():
            services += add_blueprint_to_server(self.config, bp, self)

        if self.config.GRPC_HEALTH_CHECKING_ENABLE:
            health_service = health.HealthServicer(
                experimental_non_blocking=True,
                experimental_thread_pool=futures.ThreadPoolExecutor(
                    max_workers=self.config.
                    GRPC_HEALTH_CHECKING_THREAD_POOL_NUM),
            )
            health_pb2_grpc.add_HealthServicer_to_server(health_service, self)

        if self.config.GRPC_SEVER_REFLECTION_ENABLE:
            reflection.enable_server_reflection(services, self)

        if block is None:
            block = self.config.GRPC_SERVER_RUN_WITH_BLOCK

        self.before_server_start()

        if server_credentials:
            self.add_secure_port(target.encode("utf-8"),
                                 server_credentials=server_credentials)
        else:
            self.add_insecure_port(target.encode("utf-8"))

        self.start()

        self.logger.info(f"gRPC server is running on {target}")

        if block:  # pragma: no cover
            try:
                while True:
                    time.sleep(_ONE_DAY_IN_SECONDS)
            except:
                pass
            finally:
                self.stop(0)
        return self
Пример #9
0
 def add_to_server(self, server):
     if self.config['enable']:
         health_servicer = health.HealthServicer(
             experimental_non_blocking=True)
         health_servicer.set('', HealthCheckResponse.SERVING)
         for svc in self.app.service_names:
             if svc in self.config['not_serving']:
                 status = HealthCheckResponse.NOT_SERVING
             else:
                 status = HealthCheckResponse.SERVING
             health_servicer.set(svc, status)
         health_pb2_grpc.add_HealthServicer_to_server(
             health_servicer, server)
Пример #10
0
def create_server(server_address):
    interceptors = [ExceptionToStatusInterceptor()]
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10),
                         interceptors=interceptors)
    project_pb2_grpc.add_ProjectServicesServicer_to_server(
        ProjectServicer(), server)
    # Create a health check servicer. We use the non-blocking implementation
    # to avoid thread starvation.
    health_servicer = health.HealthServicer(
        experimental_non_blocking=True,
        experimental_thread_pool=futures.ThreadPoolExecutor(max_workers=1))
    health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)
    port = server.add_insecure_port(server_address)
    return server, port
Пример #11
0
    def setUp(self):
        servicer = health.HealthServicer()
        servicer.set('', health_pb2.HealthCheckResponse.SERVING)
        servicer.set('grpc.test.TestServiceServing',
                     health_pb2.HealthCheckResponse.SERVING)
        servicer.set('grpc.test.TestServiceUnknown',
                     health_pb2.HealthCheckResponse.UNKNOWN)
        servicer.set('grpc.test.TestServiceNotServing',
                     health_pb2.HealthCheckResponse.NOT_SERVING)
        self._server = test_common.test_server()
        port = self._server.add_insecure_port('[::]:0')
        health_pb2_grpc.add_HealthServicer_to_server(servicer, self._server)
        self._server.start()

        channel = grpc.insecure_channel('localhost:%d' % port)
        self._stub = health_pb2_grpc.HealthStub(channel)
Пример #12
0
    def setUp(self):
        servicer = health.HealthServicer()
        servicer.set('', health_pb2.HealthCheckResponse.SERVING)
        servicer.set('grpc.test.TestServiceServing',
                     health_pb2.HealthCheckResponse.SERVING)
        servicer.set('grpc.test.TestServiceUnknown',
                     health_pb2.HealthCheckResponse.UNKNOWN)
        servicer.set('grpc.test.TestServiceNotServing',
                     health_pb2.HealthCheckResponse.NOT_SERVING)
        server_pool = logging_pool.pool(test_constants.THREAD_CONCURRENCY)
        self._server = grpc.server(server_pool)
        port = self._server.add_insecure_port('[::]:0')
        health_pb2.add_HealthServicer_to_server(servicer, self._server)
        self._server.start()

        channel = grpc.insecure_channel('localhost:%d' % port)
        self._stub = health_pb2.HealthStub(channel)
Пример #13
0
 def __init__(self,
              address: str,
              *,
              port: int,
              register: bool = False,
              workers: int = 10):
     thread_pool = ThreadPoolExecutor(max_workers=workers)
     server = grpc.server(thread_pool)
     servicer = EventsServicer()
     events_pb2_grpc.add_EventsServicer_to_server(servicer, server)
     health_servicer = health.HealthServicer()
     health_servicer.set('', 'SERVING')
     health_servicer.set(constants.EVENTS_SERVICE_NAME, 'SERVING')
     health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)
     self._port = server.add_insecure_port(address + ':' + str(port))
     self._server = server
     self._address = address
     self._config = Config()
     self._register = register
Пример #14
0
        def start_server(self, non_blocking=False, thread_pool=None):
            self._thread_pool = thread_pool
            self._servicer = health.HealthServicer(
                experimental_non_blocking=non_blocking,
                experimental_thread_pool=thread_pool)
            self._servicer.set(_SERVING_SERVICE,
                               health_pb2.HealthCheckResponse.SERVING)
            self._servicer.set(_UNKNOWN_SERVICE,
                               health_pb2.HealthCheckResponse.UNKNOWN)
            self._servicer.set(_NOT_SERVING_SERVICE,
                               health_pb2.HealthCheckResponse.NOT_SERVING)
            self._server = test_common.test_server()
            port = self._server.add_insecure_port('[::]:0')
            health_pb2_grpc.add_HealthServicer_to_server(
                self._servicer, self._server)
            self._server.start()

            self._channel = grpc.insecure_channel('localhost:%d' % port)
            self._stub = health_pb2_grpc.HealthStub(self._channel)
Пример #15
0
def main(dim, save_path, keys_path, log, debug, updateable, max_workers, nprobe):
    if log:
        handler = logging.FileHandler(filename=log)
    else:
        handler = logging.StreamHandler(sys.stdout)
    formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(name)s - %(message)s')
    handler.setFormatter(formatter)
    root = logging.getLogger()
    level = debug and logging.DEBUG or logging.INFO
    root.setLevel(level)
    root.addHandler(handler)

    logging.info('server loading...')
    logging.info('max workers: %d', max_workers)

    server = grpc.server(futures.ThreadPoolExecutor(max_workers=max_workers))

    health_servicer = health.HealthServicer()
    health_servicer.set('', health_pb2.HealthCheckResponse.SERVING)
    health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)

    servicer = FaissServer(dim, save_path, keys_path, nprobe)
    pb2_grpc.add_ServerServicer_to_server(servicer, server)

    server.add_insecure_port('[::]:50051')
    server.start()
    logging.info('server started')

    def stop_serve(signum, frame):
        raise KeyboardInterrupt
    signal.signal(signal.SIGINT, stop_serve)
    signal.signal(signal.SIGTERM, stop_serve)

    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        health_servicer.enter_graceful_shutdown()
        server.stop(0)
        if updateable:
            servicer.save()
        logging.info('server stopped')
Пример #16
0
def serve(args):
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

    health_servicer = health.HealthServicer()
    health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)

    namer_pb2_grpc.add_NamerServicer_to_server(Namer(), server)

    ca_cert = None
    client_auth = False
    if args.ca_cert:
        ca_cert = open(args.ca_cert, 'rb').read()
        client_auth = True

    if args.server_cert and args.server_key:
        private_key = open(args.server_key, 'rb').read()
        certificate_chain = open(args.server_cert, 'rb').read()

        credentials = grpc.ssl_server_credentials(
            [(private_key, certificate_chain)],
            root_certificates=ca_cert,
            require_client_auth=client_auth
        )
        server.add_secure_port('[::]:' + str(args.port), credentials)
    else:
        server.add_insecure_port('[::]:' + str(args.port))

    if args.metrics_port:
        print('Starting metrics server. Listening on port {}...'.format(
            args.metrics_port))
        start_http_server(args.metrics_port)

    print('Starting server. Listening on port {}...'.format(args.port))
    server.start()
    health_servicer.set('', health_pb2.HealthCheckResponse.SERVING)
    try:
        while True:
            time.sleep(86400)
    except KeyboardInterrupt:
        health_servicer.set('', health_pb2.HealthCheckResponse.NOT_SERVING)
        time.sleep(10)
        server.stop(1)
Пример #17
0
def _configure_maintenance_server(server: grpc.Server,
                                  maintenance_port: int) -> None:
    channelz.add_channelz_servicer(server)
    listen_address = f"{_LISTEN_HOST}:{maintenance_port}"
    server.add_insecure_port(listen_address)
    health_servicer = grpc_health.HealthServicer(
        experimental_non_blocking=True,
        experimental_thread_pool=futures.ThreadPoolExecutor(
            max_workers=_THREAD_POOL_SIZE))

    health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)
    SERVICE_NAMES = (
        test_pb2.DESCRIPTOR.services_by_name["TestService"].full_name,
        health_pb2.DESCRIPTOR.services_by_name["Health"].full_name,
        channelz_pb2.DESCRIPTOR.services_by_name["Channelz"].full_name,
        reflection.SERVICE_NAME,
    )
    for service in SERVICE_NAMES:
        health_servicer.set(service, health_pb2.HealthCheckResponse.SERVING)
    reflection.enable_server_reflection(SERVICE_NAMES, server)
Пример #18
0
    def __init__(self,
                 runner: 'mtap.processing.ProcessingComponent',
                 host: str,
                 port: int = 0,
                 *,
                 register: bool = False,
                 workers: Optional[int] = None,
                 write_address: bool = False,
                 config: 'Optional[mtap.Config]' = None):
        self.host = host
        self._port = port
        self.processor_id = runner.processor_id
        self.write_address = write_address

        if config is None:
            config = _config.Config()

        self._health_servicer = health.HealthServicer()
        self._health_servicer.set('', 'SERVING')
        self._servicer = _ProcessorServicer(
            config=config,
            address=host,
            runner=runner,
            health_servicer=self._health_servicer,
            register=register)
        workers = workers or 10
        thread_pool = thread.ThreadPoolExecutor(max_workers=workers)
        self._server = grpc.server(
            thread_pool,
            options=[('grpc.max_send_message_length',
                      config.get('grpc.max_send_message_length')),
                     ('grpc.max_receive_message_length',
                      config.get('grpc.max_receive_message_length'))])
        health_pb2_grpc.add_HealthServicer_to_server(self._health_servicer,
                                                     self._server)
        processing_pb2_grpc.add_ProcessorServicer_to_server(
            self._servicer, self._server)
        self._port = self._server.add_insecure_port("{}:{}".format(
            self.host, self.port))
        self._stopped_event = threading.Event()
        self._address_file = None
Пример #19
0
async def launch_server(bind_address, port, server_id):
    """Start async grpcio server sub process"""
    server = grpc.aio.server(futures.ThreadPoolExecutor(max_workers=5, ))
    takeorders_pb2_grpc.add_OrderServicer_to_server(
        StoreService.Store(server_id, port, API_URL, API_KEY), server)

    health_servicer = health.HealthServicer(
        experimental_non_blocking=True,
        experimental_thread_pool=futures.ThreadPoolExecutor(max_workers=1))
    health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)
    services = tuple(service.full_name for service in takeorders_pb2.DESCRIPTOR.services_by_name.values()) \
               + (reflection.SERVICE_NAME, health.SERVICE_NAME)
    reflection.enable_server_reflection(services, server)
    server.add_insecure_port(bind_address)

    await server.start()

    # Mark all services as healthy.
    overall_server_health = ""
    for service in services + (overall_server_health, ):
        health_servicer.set(service, health_pb2.HealthCheckResponse.SERVING)
    await server.wait_for_termination()
Пример #20
0
    def __init__(self,
                 proc: EventProcessor,
                 address: str,
                 port: int = 0,
                 *,
                 register: bool = False,
                 events_address: Optional[str] = None,
                 processor_id: Optional[str] = None,
                 workers: Optional[int] = None,
                 params: Optional[Mapping[str, Any]] = None):
        self.pr = proc
        self.address = address
        self._port = port
        self.processor_id = processor_id or proc.metadata['name']
        self.params = params or {}
        self.events_address = events_address

        self._health_servicer = health.HealthServicer()
        self._health_servicer.set('', 'SERVING')
        self._servicer = _ProcessorServicer(
            config=Config(),
            pr=proc,
            address=address,
            health_servicer=self._health_servicer,
            register=register,
            processor_id=processor_id,
            params=params,
            events_address=events_address)
        workers = workers or 10
        thread_pool = ThreadPoolExecutor(max_workers=workers)
        self._server = grpc.server(thread_pool)
        health_pb2_grpc.add_HealthServicer_to_server(self._health_servicer,
                                                     self._server)
        processing_pb2_grpc.add_ProcessorServicer_to_server(
            self._servicer, self._server)
        self._port = self._server.add_insecure_port("{}:{}".format(
            self.address, self.port))
        self._stopped_event = threading.Event()
Пример #21
0
def _configure_maintenance_server(server: grpc.Server,
                                  maintenance_port: int) -> None:
    listen_address = f"{_LISTEN_HOST}:{maintenance_port}"
    server.add_insecure_port(listen_address)

    # Create a health check servicer. We use the non-blocking implementation
    # to avoid thread starvation.
    health_servicer = health.HealthServicer(
        experimental_non_blocking=True,
        experimental_thread_pool=futures.ThreadPoolExecutor(
            max_workers=_THREAD_POOL_SIZE))

    # Create a tuple of all of the services we want to export via reflection.
    services = tuple(
        service.full_name
        for service in helloworld_pb2.DESCRIPTOR.services_by_name.values()) + (
            reflection.SERVICE_NAME, health.SERVICE_NAME)

    # Mark all services as healthy.
    health_pb2_grpc.add_HealthServicer_to_server(health_servicer, server)
    for service in services:
        health_servicer.set(service, health_pb2.HealthCheckResponse.SERVING)
    reflection.enable_server_reflection(services, server)
Пример #22
0
 def __init__(self):
     self._impls = {}
     self._health_servicer = health.HealthServicer()
Пример #23
0
    def __init__(
        self,
        host="localhost",
        port=None,
        socket=None,
        max_workers=1,
        loadable_target_origin=None,
        heartbeat=False,
        heartbeat_timeout=30,
        lazy_load_user_code=False,
        ipc_output_file=None,
        fixed_server_id=None,
    ):
        check.opt_str_param(host, "host")
        check.opt_int_param(port, "port")
        check.opt_str_param(socket, "socket")
        check.int_param(max_workers, "max_workers")
        check.opt_inst_param(loadable_target_origin, "loadable_target_origin", LoadableTargetOrigin)
        check.invariant(
            port is not None if seven.IS_WINDOWS else True,
            "You must pass a valid `port` on Windows: `socket` not supported.",
        )
        check.invariant(
            (port or socket) and not (port and socket),
            "You must pass one and only one of `port` or `socket`.",
        )
        check.invariant(
            host is not None if port else True, "Must provide a host when serving on a port",
        )
        check.bool_param(heartbeat, "heartbeat")
        check.int_param(heartbeat_timeout, "heartbeat_timeout")
        self._ipc_output_file = check.opt_str_param(ipc_output_file, "ipc_output_file")
        check.opt_str_param(fixed_server_id, "fixed_server_id")

        check.invariant(heartbeat_timeout > 0, "heartbeat_timeout must be greater than 0")
        check.invariant(
            max_workers > 1 if heartbeat else True,
            "max_workers must be greater than 1 if heartbeat is True",
        )

        self.server = grpc.server(ThreadPoolExecutor(max_workers=max_workers))
        self._server_termination_event = threading.Event()

        self._api_servicer = DagsterApiServer(
            server_termination_event=self._server_termination_event,
            loadable_target_origin=loadable_target_origin,
            heartbeat=heartbeat,
            heartbeat_timeout=heartbeat_timeout,
            lazy_load_user_code=lazy_load_user_code,
            fixed_server_id=fixed_server_id,
        )

        # Create a health check servicer
        self._health_servicer = health.HealthServicer()
        health_pb2_grpc.add_HealthServicer_to_server(self._health_servicer, self.server)

        add_DagsterApiServicer_to_server(self._api_servicer, self.server)

        if port:
            server_address = host + ":" + str(port)
        else:
            server_address = "unix:" + os.path.abspath(socket)

        # grpc.Server.add_insecure_port returns:
        # - 0 on failure
        # - port number when a port is successfully bound
        # - 1 when a UDS is successfully bound
        res = self.server.add_insecure_port(server_address)
        if socket and res != 1:
            if self._ipc_output_file:
                with ipc_write_stream(self._ipc_output_file) as ipc_stream:
                    ipc_stream.send(GrpcServerFailedToBindEvent())
            raise CouldNotBindGrpcServerToAddress(socket)
        if port and res != port:
            if self._ipc_output_file:
                with ipc_write_stream(self._ipc_output_file) as ipc_stream:
                    ipc_stream.send(GrpcServerFailedToBindEvent())
            raise CouldNotBindGrpcServerToAddress(port)
Пример #24
0
    def __init__(
        self,
        host="localhost",
        port=None,
        socket=None,
        max_workers=None,
        loadable_target_origin=None,
        heartbeat=False,
        heartbeat_timeout=30,
        lazy_load_user_code=False,
        ipc_output_file=None,
        fixed_server_id=None,
        entry_point=None,
        container_context=None,
    ):
        check.opt_str_param(host, "host")
        check.opt_int_param(port, "port")
        check.opt_str_param(socket, "socket")
        check.opt_int_param(max_workers, "max_workers")
        check.opt_inst_param(loadable_target_origin, "loadable_target_origin",
                             LoadableTargetOrigin)
        check.invariant(
            port is not None if seven.IS_WINDOWS else True,
            "You must pass a valid `port` on Windows: `socket` not supported.",
        )
        check.invariant(
            (port or socket) and not (port and socket),
            "You must pass one and only one of `port` or `socket`.",
        )
        check.invariant(
            host is not None if port else True,
            "Must provide a host when serving on a port",
        )
        check.bool_param(heartbeat, "heartbeat")
        check.int_param(heartbeat_timeout, "heartbeat_timeout")
        self._ipc_output_file = check.opt_str_param(ipc_output_file,
                                                    "ipc_output_file")
        check.opt_str_param(fixed_server_id, "fixed_server_id")

        check.invariant(heartbeat_timeout > 0,
                        "heartbeat_timeout must be greater than 0")
        check.invariant(
            max_workers is None or max_workers > 1 if heartbeat else True,
            "max_workers must be greater than 1 or set to None if heartbeat is True. "
            "If set to None, the server will use the gRPC default.",
        )

        self.server = grpc.server(
            ThreadPoolExecutor(max_workers=max_workers),
            compression=grpc.Compression.Gzip,
            options=[
                ("grpc.max_send_message_length", max_send_bytes()),
                ("grpc.max_receive_message_length", max_rx_bytes()),
            ],
        )
        self._server_termination_event = threading.Event()

        try:
            self._api_servicer = DagsterApiServer(
                server_termination_event=self._server_termination_event,
                loadable_target_origin=loadable_target_origin,
                heartbeat=heartbeat,
                heartbeat_timeout=heartbeat_timeout,
                lazy_load_user_code=lazy_load_user_code,
                fixed_server_id=fixed_server_id,
                entry_point=entry_point,
                container_context=container_context,
            )
        except Exception:
            if self._ipc_output_file:
                with ipc_write_stream(self._ipc_output_file) as ipc_stream:
                    ipc_stream.send(
                        GrpcServerLoadErrorEvent(
                            error_info=serializable_error_info_from_exc_info(
                                sys.exc_info())))
            raise

        # Create a health check servicer
        self._health_servicer = health.HealthServicer()
        health_pb2_grpc.add_HealthServicer_to_server(self._health_servicer,
                                                     self.server)

        add_DagsterApiServicer_to_server(self._api_servicer, self.server)

        if port:
            server_address = host + ":" + str(port)
        else:
            server_address = "unix:" + os.path.abspath(socket)

        # grpc.Server.add_insecure_port returns:
        # - 0 on failure
        # - port number when a port is successfully bound
        # - 1 when a UDS is successfully bound
        res = self.server.add_insecure_port(server_address)
        if socket and res != 1:
            if self._ipc_output_file:
                with ipc_write_stream(self._ipc_output_file) as ipc_stream:
                    ipc_stream.send(GrpcServerFailedToBindEvent())
            raise CouldNotBindGrpcServerToAddress(socket)
        if port and res != port:
            if self._ipc_output_file:
                with ipc_write_stream(self._ipc_output_file) as ipc_stream:
                    ipc_stream.send(GrpcServerFailedToBindEvent())
            raise CouldNotBindGrpcServerToAddress(port)
Пример #25
0
 def __init__(self, tmp_dir="/tmp"):
     self._impls = {}
     self._health_servicer = health.HealthServicer()
     self.tmp_dir = tmp_dir
Пример #26
0
    def __init__(
        self,
        args: argparse.Namespace,
        **kwargs,
    ):
        """Initialize grpc server for the head runtime.
        :param args: args from CLI
        :param kwargs: keyword args
        """
        self._health_servicer = health.HealthServicer(
            experimental_non_blocking=True)

        super().__init__(args, **kwargs)
        if args.name is None:
            args.name = ''
        self.name = args.name
        self._deployment_name = os.getenv('JINA_DEPLOYMENT_NAME', 'worker')
        self.connection_pool = GrpcConnectionPool(
            logger=self.logger,
            compression=args.compression,
            metrics_registry=self.metrics_registry,
        )
        self._retries = self.args.retries

        if self.metrics_registry:
            with ImportExtensions(
                    required=True,
                    help_text=
                    'You need to install the `prometheus_client` to use the montitoring functionality of jina',
            ):
                from prometheus_client import Summary

            self._summary = (Summary(
                'receiving_request_seconds',
                'Time spent processing request',
                registry=self.metrics_registry,
                namespace='jina',
                labelnames=('runtime_name', ),
            ).labels(self.args.name).time())
        else:
            self._summary = contextlib.nullcontext()

        polling = getattr(args, 'polling', self.DEFAULT_POLLING.name)
        try:
            # try loading the polling args as json
            endpoint_polling = json.loads(polling)
            # '*' is used a wildcard and will match all endpoints, except /index, /search and explicitly defined endpoins
            default_polling = (PollingType.from_string(endpoint_polling['*'])
                               if '*' in endpoint_polling else
                               self.DEFAULT_POLLING)
            self._polling = self._default_polling_dict(default_polling)
            for endpoint in endpoint_polling:
                self._polling[endpoint] = PollingType(
                    endpoint_polling[endpoint] if type(
                        endpoint_polling[endpoint]) == int else PollingType.
                    from_string(endpoint_polling[endpoint]))
        except (ValueError, TypeError):
            # polling args is not a valid json, try interpreting as a polling enum type
            default_polling = (polling if type(polling) == PollingType else
                               PollingType.from_string(polling))
            self._polling = self._default_polling_dict(default_polling)

        if hasattr(args, 'connection_list') and args.connection_list:
            connection_list = json.loads(args.connection_list)
            for shard_id in connection_list:
                shard_connections = connection_list[shard_id]
                if isinstance(shard_connections, str):
                    self.connection_pool.add_connection(
                        deployment=self._deployment_name,
                        address=shard_connections,
                        shard_id=int(shard_id),
                    )
                else:
                    for connection in shard_connections:
                        self.connection_pool.add_connection(
                            deployment=self._deployment_name,
                            address=connection,
                            shard_id=int(shard_id),
                        )

        self.uses_before_address = args.uses_before_address
        self.timeout_send = args.timeout_send
        if self.timeout_send:
            self.timeout_send /= 1e3  # convert ms to seconds

        if self.uses_before_address:
            self.connection_pool.add_connection(
                deployment='uses_before', address=self.uses_before_address)
        self.uses_after_address = args.uses_after_address
        if self.uses_after_address:
            self.connection_pool.add_connection(
                deployment='uses_after', address=self.uses_after_address)
        self._reduce = not args.disable_reduce