Example #1
0
def test_can_serialize_and_deserialize_protobuf():
    type_name = "actor.PID"
    pid = PID(address='123', id='456')
    data = Serialization().serialize(pid, 0)
    deserialized = Serialization().deserialize(type_name, data, 0)
    assert deserialized.address == "123"
    assert deserialized.id == "456"
Example #2
0
def test_can_serialize_and_deserialize_json_pid():
    type_name = "actor.PID"
    json = JsonMessage(type_name, "{ \"address\":\"123\", \"id\":\"456\"}")
    data = Serialization().serialize(json, 1)
    deserialized = Serialization().deserialize(type_name, data, 1)
    assert deserialized.address == "123"
    assert deserialized.id == "456"
Example #3
0
    async def receive(self, context: AbstractContext) -> None:
        message = context.message
        if isinstance(message, Started):
            await self.__started_async()
        elif isinstance(message, Stopped):
            await self.__stopped_async()
        elif isinstance(message, Restarting):
            await self.__restarting_async()
        elif isinstance(message, EndpointTerminatedEvent):
            context.my_self.stop()
        elif isinstance(message, list) and all(isinstance(x, RemoteDeliver) for x in message):
            envelopes = []
            type_names = {}
            target_names = {}
            type_name_list = []
            target_name_list = []

            for rd in message:
                target_name = rd.target.id
                if rd.serializer_id == -1:
                    serializer_id = self._serializer_id
                else:
                    serializer_id = rd.serializer_id

                if target_name not in target_names:
                    target_id = target_names[target_name] = len(target_names)
                    target_name_list.append(target_name)
                else:
                    target_id = target_names[target_name]

                type_name = Serialization().get_type_name(rd.message, serializer_id)
                if type_name not in type_names:
                    type_id = type_names[type_name] = len(type_names)
                    type_name_list.append(type_name)
                else:
                    type_id = type_names[type_name]

                header = None
                if rd.header is not None and rd.header.count > 0:
                    header = MessageHeader(header_data=rd.header)

                message_data = Serialization().serialize(rd.message, serializer_id)

                envelope = MessageEnvelope(type_id=type_id,
                                           message_data=message_data,
                                           target=target_id,
                                           sender=rd.sender,
                                           serializer_id=serializer_id,
                                           message_header=header)

                envelopes.append(envelope)

            batch = MessageBatch()
            batch.target_names.extend(target_name_list)
            batch.type_names.extend(type_name_list)
            batch.envelopes.extend(envelopes)

            await self.__send_envelopes_async(batch, context)
Example #4
0
async def main():
    tracer = init_jaeger_tracer()
    opentracing.set_global_tracer(tracer)

    context = RootContext()

    Serialization().register_file_descriptor(DESCRIPTOR)
    Remote().start("127.0.0.1", 8000)
    clients = []

    async def process_message(ctx: AbstractContext):
        msg = ctx.message
        if isinstance(msg, Connect):
            print(f'Client {msg.sender} connected')
            clients.append(msg.sender)
            await ctx.send(msg.sender, Connected(message='Welcome!'))
        elif isinstance(msg, SayRequest):
            for client in clients:
                await ctx.send(client, SayResponse(user_name=msg.user_name, message=msg.message))
        elif isinstance(msg, NickRequest):
            for client in clients:
                await ctx.send(client, NickResponse(old_user_name=msg.old_user_name, new_user_name=msg.new_user_name))

    props = OpenTracingFactory.get_props_with_open_tracing(Props.from_func(process_message), span_setup, span_setup)
    context.spawn_named(props, 'chatserver')

    input()
Example #5
0
async def start(argv):
    tracer = init_jaeger_tracer()
    opentracing.set_global_tracer(tracer)

    middleware = open_tracing_middleware.open_tracing_sender_middleware(tracer)

    Serialization().register_file_descriptor(DESCRIPTOR)
    Remote().start("127.0.0.1", 12001)
    server = PID(address='127.0.0.1:8000', id='chatserver')
    context = RootContext(MessageHeader(), [middleware])

    props = OpenTracingFactory.get_props_with_open_tracing(Props.from_func(process_message), span_setup, span_setup,
                                                           tracer)

    client = context.spawn(props)
    await context.send(server, Connect(sender=client))

    nick = 'Alex'
    while True:
        text = input()
        if text == '/exit':
            return
        elif text.startswith('/nick '):
            new_nick = text.split(' ')[1]
            await context.send(server, NickRequest(old_user_name=nick, new_user_name=new_nick))
            nick = new_nick
        else:
            await context.send(server, SayRequest(user_name=nick, message=text))
Example #6
0
    async def Receive(self, stream) -> None:
        targets = []
        async for batch in stream:
            if self.__suspended:
                await stream.send_message(Unit())

            for i in range(len(batch.target_names)):
                targets.append(PID(address=ProcessRegistry().address, id=batch.target_names[i]))

            type_names = list(batch.type_names)
            for envelope in batch.envelopes:
                target = targets[envelope.target]
                type_name = type_names[envelope.type_id]
                message = Serialization().deserialize(type_name, envelope.message_data, envelope.serializer_id)

                if isinstance(message, Terminated):
                    await EndpointManager().remote_terminate(RemoteTerminate(target, message.who))
                elif isinstance(message, Watch):
                    await target.send_system_message(message)
                elif isinstance(message, Unwatch):
                    await target.send_system_message(message)
                elif isinstance(message, Stop):
                    await target.send_system_message(message)
                else:
                    header = None
                    if envelope.message_header is not None:
                        header = proto.MessageHeader(envelope.message_header.header_data)
                    local_envelope = proto.MessageEnvelope(message, envelope.sender, header)
                    await GlobalRootContext().instance.send(target, local_envelope)

        await stream.send_message(Unit())
Example #7
0
async def main():
    context = RootContext()
    Serialization().register_file_descriptor(DESCRIPTOR)
    Remote().start("192.168.1.129", 12001)

    wg = asyncio.Event()
    message_count = 10000

    props = Props.from_producer(lambda: LocalClient(0, message_count, wg, asyncio.get_event_loop()))

    pid = context.spawn(props)
    remote = PID(address="192.168.1.77:12000", id="remote")

    await context.request_future(remote, StartRemote(Sender=pid))

    start = datetime.datetime.now()
    print('Starting to send')
    for i in range(message_count):
        await context.send(remote, Ping())
    await wg.wait()

    elapsed = datetime.datetime.now() - start
    print(f'Elapsed {elapsed}')

    t = message_count * 2.0 / elapsed.total_seconds()
    print(f'Throughput {t} msg / sec')

    input()
Example #8
0
async def start(argv):
    Serialization().register_file_descriptor(DESCRIPTOR)

    Grains.hello_grain_factory(HelloGrain())

    await Cluster.start(
        'MyCluster', '192.168.1.129', 12000,
        ConsulProvider(
            ConsulClientConfiguration(f'http://192.168.1.35:8500/')))

    await asyncio.sleep(10000)
    input()
    print('Shutting Down...')
    await Cluster.shutdown()
Example #9
0
async def start(argv):
    Serialization().register_file_descriptor(DESCRIPTOR)
    await Cluster.start(
        'MyCluster', '127.0.0.1', 12001,
        ConsulProvider(
            ConsulClientConfiguration(f'http://192.168.1.35:8500/')))

    client = Grains.hello_grain("Roger")
    res = await client.say_hello(HelloRequest())
    print(res.message)
    input()

    res = await client.say_hello(HelloRequest())
    print(res.message)
    input()

    print('Shutting Down...')
    await Cluster.shutdown()
Example #10
0
async def start(argv):
    host = None
    port = None
    opts, args = getopt.getopt(argv, "hp", ["host=", "port="])
    for opt, arg in opts:
        if opt == '--host':
            host = arg
        elif opt == '--port':
            port = arg

    Serialization().register_file_descriptor(DESCRIPTOR)

    context = RootContext()
    Remote().start(host, port)
    props = Props().from_producer(lambda: EchoActor(host, port))
    Remote().register_known_kind('EchoActor', props)
    context.spawn_named(props, "EchoActorInstance")

    input()
Example #11
0
    async def start_with_config(self, config: ClusterConfig) -> None:
        self._config = config

        Remote().start(self._config.address, self._config.port,
                       self._config.remote_config)

        Serialization().register_file_descriptor(DESCRIPTOR)
        # self._logger.log_information('Starting Proto.Actor cluster')
        host, port = self.__parse_address(ProcessRegistry().address)
        kinds = Remote().get_known_kinds()

        await Partition.setup(kinds)
        await PidCache().setup()
        MemberList.setup()

        await self._config.cluster_provider.register_member_async(
            self._config.name, host, port, kinds,
            self._config.initial_member_status_value,
            self._config.member_status_value_serializer)

        await self._config.cluster_provider.monitor_member_status_changes()
Example #12
0
async def start(argv):
    context = RootContext()
    Serialization().register_file_descriptor(DESCRIPTOR)
    parsed_args = parse_args(argv)

    # await Cluster.start('MyCluster', parsed_args.server_name, 12002, SingleRemoteInstanceProvider('192.168.1.72', 12000))

    await Cluster.start(
        'MyCluster', parsed_args.server_name, 12001,
        ConsulProvider(
            ConsulClientConfiguration(
                f'http://{parsed_args.consul_url}:8500/')))

    pid, sc = await Cluster.get_async("TheName", "HelloKind")
    while sc != ResponseStatusCode.OK:
        await asyncio.sleep(0.5)
        pid, sc = await Cluster.get_async("TheName", "HelloKind")

    res = await context.request_future(pid, HelloRequest())
    print(res.message)
    await asyncio.sleep(timedelta(days=180).total_seconds())
    print('Shutting Down...')
    await Cluster.shutdown()
    def __init__(self, server_host: str, server_port: int):
        self._kinds_responder = 'remote_kinds_responder'
        self._timeout = timedelta(seconds=10)

        self._server_host = server_host
        self._server_port = server_port
        self._server_address = '%s:%s' % (server_host, str(server_port))

        self._kinds = []
        self._ok_status = None
        self._ko_status = None

        self._is_server = None
        self._shutdown = None

        async def fn(ctx: AbstractContext):
            if isinstance(ctx.message, GetKinds) and ctx.sender is not None:
                await ctx.respond(GetKindsResponse(kinds=self._kinds))

        props = Props.from_func(fn)

        Serialization().register_file_descriptor(DESCRIPTOR)
        Remote().register_known_kind(self._kinds_responder, props)
Example #14
0
async def start(argv):
    Serialization().register_file_descriptor(DESCRIPTOR)

    async def fn(ctx: AbstractContext):
        if isinstance(ctx.message, HelloRequest):
            await ctx.respond(HelloResponse(message='Hello from node 2'))

    props = Props.from_func(fn)
    parsed_args = parse_args(argv)
    Remote().register_known_kind("HelloKind", props)

    # await Cluster.start('MyCluster', parsed_args.server_name, 12000,
    #                     SingleRemoteInstanceProvider(parsed_args.server_name, 12000))

    await Cluster.start(
        'MyCluster', parsed_args.server_name, 12000,
        ConsulProvider(
            ConsulClientConfiguration(
                f'http://{parsed_args.consul_url}:8500/')))

    await asyncio.sleep(timedelta(days=180).total_seconds())
    print('Shutting Down...')
    await Cluster.shutdown()
Example #15
0
async def main():
    context = RootContext()
    Serialization().register_file_descriptor(DESCRIPTOR)
    Remote().start("192.168.1.77", 12000)
    context.spawn_named(Props.from_producer(lambda: EchoActor()), 'remote')
    input()
Example #16
0
def register_file_descriptor():
    Serialization().register_file_descriptor(DESCRIPTOR)
Example #17
0
def test_can_serialize_and_deserialize_json():
    type_name = "remote_test_messages.Ping"
    json = JsonMessage(type_name, "{ \"message\":\"Hello\"}")
    data = Serialization().serialize(json, 1)
    deserialized = Serialization().deserialize(type_name, data, 1)
    assert deserialized.message == "Hello"
Example #18
0
 async def Connect(self, stream):
     if self.__suspended:
         raise GRPCError(Status.CANCELLED, "Suspended")
     await stream.send_message(ConnectResponse(default_serializer_id=Serialization().default_serializer_id))