Esempio n. 1
0
        public async Task Setup()
        {
            var echoProps = Props.FromFunc(ctx => {
                if (ctx.Sender is not null)
                {
                    ctx.Respond(ctx.Message !);
                }
                return(Task.CompletedTask);
            }
                                           );

            if (RequestDeduplication)
            {
                echoProps = echoProps.WithClusterRequestDeduplication(TimeSpan.FromSeconds(30));
            }

            var echoKind = new ClusterKind(Kind, echoProps);

            if (LocalAffinity)
            {
                echoKind.WithLocalAffinityRelocationStrategy();
            }

            var sys = new ActorSystem(new ActorSystemConfig())
                      .WithRemote(GrpcNetRemoteConfig.BindToLocalhost(9090))
                      .WithCluster(ClusterConfig().WithClusterKind(echoKind));

            _cluster = sys.Cluster();
            await _cluster.StartMemberAsync();

            _id = ClusterIdentity.Create("1", Kind);
            await _cluster.RequestAsync <int>(_id.Identity, _id.Kind, 1, CancellationToken.None);
        }
Esempio n. 2
0
        public static async Task Main()
        {
            /*
             *  docker build . -t rogeralsing/kubdiagg
             *  kubectl apply --filename service.yaml
             *  kubectl get pods -l app=kubdiag
             *  kubectl logs -l app=kubdiag --all-containers
             *
             */

            var l = LoggerFactory.Create(c => c.AddConsole().SetMinimumLevel(LogLevel.Error));

            Log.SetLoggerFactory(l);
            var log = Log.CreateLogger("main");

            var db       = GetMongo();
            var identity = new IdentityStorageLookup(new MongoIdentityStorage("mycluster", db.GetCollection <PidLookupEntity>("pids"), 200));

            var kubernetes      = new Kubernetes(KubernetesClientConfiguration.InClusterConfig());
            var clusterprovider = new KubernetesProvider(kubernetes);

            var port           = int.Parse(Environment.GetEnvironmentVariable("PROTOPORT") !);
            var host           = Environment.GetEnvironmentVariable("PROTOHOST");
            var advertisedHost = Environment.GetEnvironmentVariable("PROTOHOSTPUBLIC");

            log.LogInformation("Host {host}", host);
            log.LogInformation("Port {port}", port);
            log.LogInformation("Advertised Host {advertisedHost}", advertisedHost);

            var system = new ActorSystem()
                         .WithRemote(GrpcNetRemoteConfig
                                     .BindTo(host, port)
                                     .WithAdvertisedHost(advertisedHost)
                                     )
                         .WithCluster(ClusterConfig
                                      .Setup("mycluster", clusterprovider, identity)
                                      .WithClusterKind("empty", Props.Empty)
                                      );

            system.EventStream.Subscribe <ClusterTopology>(e => {
                var members = e.Members;
                var x       = members.Select(m => m.Id).OrderBy(i => i).ToArray();
                var key     = string.Join("", x);
                var hash    = MurmurHash2.Hash(key);

                Console.WriteLine("My members " + hash);

                foreach (var member in members.OrderBy(m => m.Id))
                {
                    Console.WriteLine(member.Id + "\t" + member.Address + "\t" + member.Kinds);
                }
            }
                                                           );

            await system
            .Cluster()
            .StartMemberAsync();

            Thread.Sleep(Timeout.Infinite);
        }
            public Fixture()
            {
                var clientConfig = ConfigureClientRemoteConfig(GrpcNetRemoteConfig.BindToLocalhost(5000));

                (_clientHost, Remote) = GetHostedGrpcNetRemote(clientConfig);
                var serverConfig = ConfigureServerRemoteConfig(GrpcCoreRemoteConfig.BindToLocalhost(5001));

                ServerRemote = GetGrpcCoreRemote(serverConfig);
            }
            public Fixture()
            {
                var clientConfig = ConfigureClientRemoteConfig(GrpcNetRemoteConfig.BindToLocalhost())
                                   .WithSerializer(serializerId: 2, priority: 1000, new CustomSerializer());

                (_clientHost, Remote) = GetHostedGrpcNetRemote(clientConfig);
                var serverConfig = ConfigureServerRemoteConfig(GrpcNetRemoteConfig.BindToLocalhost())
                                   .WithSerializer(serializerId: 2, priority: 1000, new CustomSerializer());

                (_serverHost, ServerRemote) = GetHostedGrpcNetRemote(serverConfig);
            }
Esempio n. 5
0
 /// <summary>
 /// Configures the <see cref="ActorSystem"/> service in the <see cref="ServiceCollection"/>.
 /// </summary>
 /// <param name="builder">The <see cref="IHostBuilder"/>.</param>
 /// <returns>The builder for continuation.</returns>
 public static IHostBuilder AddActorSystem(this IHostBuilder builder)
 => builder.ConfigureServices(_ => _
                              .AddSingleton(provider => new ActorSystem(ActorSystemConfig.Setup())
                                            .WithServiceProvider(provider)
                                            .WithRemote(GrpcNetRemoteConfig
                                                        .BindToLocalhost()
                                                        .WithProtoMessages(provider.GetRequiredService <IProtobufFileDescriptors>().All.ToArray()))
                                            .WithCluster(ClusterConfig.Setup(
                                                             "Dolittle",
                                                             new TestProvider(new TestProviderOptions(), new InMemAgent()),
                                                             new PartitionIdentityLookup())
                                                         .WithDiscoveredClusterKinds(provider)))
                              .AddSingleton(provider => provider.GetRequiredService <ActorSystem>().Root.WithTracing())
                              .AddSingleton(provider => provider.GetRequiredService <ActorSystem>().Cluster()));
Esempio n. 6
0
        private static async Task Main()
        {
            Log.SetLoggerFactory(LoggerFactory.Create(c => c
                                                      .SetMinimumLevel(LogLevel.Information)
                                                      .AddConsole()
                                                      )
                                 );

#if NETCORE
            AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
#endif

            Console.WriteLine("Enter 0 to use GrpcCore provider");
            Console.WriteLine("Enter 1 to use GrpcNet provider");
            if (!int.TryParse(Console.ReadLine(), out var provider))
            {
                provider = 0;
            }

            Console.WriteLine("Enter Advertised Host (Enter = localhost)");
            var advertisedHost = Console.ReadLine().Trim();
            if (string.IsNullOrEmpty(advertisedHost))
            {
                advertisedHost = "127.0.0.1";
            }

            var actorSystemConfig = new ActorSystemConfig()
                                    .WithDeadLetterThrottleCount(10)
                                    .WithDeadLetterThrottleInterval(TimeSpan.FromSeconds(2));
            var     system  = new ActorSystem(actorSystemConfig);
            var     context = new RootContext(system);
            IRemote remote;

            if (provider == 0)
            {
                var remoteConfig = GrpcCoreRemoteConfig
                                   .BindTo(advertisedHost, 12000)
                                   .WithProtoMessages(ProtosReflection.Descriptor)
                                   .WithRemoteKind("echo", Props.FromProducer(() => new EchoActor()));
                remote = new GrpcCoreRemote(system, remoteConfig);
            }
            else
            {
                var remoteConfig = GrpcNetRemoteConfig
                                   .BindTo(advertisedHost, 12000)
                                   .WithChannelOptions(new GrpcChannelOptions
                {
                    CompressionProviders = new[]
                    {
                        new GzipCompressionProvider(CompressionLevel.Fastest)
                    }
                }
                                                       )
                                   .WithProtoMessages(ProtosReflection.Descriptor)
                                   .WithRemoteKind("echo", Props.FromProducer(() => new EchoActor()));
                remote = new GrpcNetRemote(system, remoteConfig);
            }

            await remote.StartAsync();

            context.SpawnNamed(Props.FromProducer(() => new EchoActor()), "remote");
            Console.ReadLine();
            await remote.ShutdownAsync();
        }
Esempio n. 7
0
 public static GrpcNetRemoteConfig WithChannelOptions(this GrpcNetRemoteConfig config, GrpcChannelOptions options)
 => config with
Esempio n. 8
0
 public GrpcNetChannelProvider(GrpcNetRemoteConfig remoteConfig) => _remoteConfig = remoteConfig;
Esempio n. 9
0
    private static async Task Main()
    {
        Log.SetLoggerFactory(LoggerFactory.Create(c => c
                                                  .SetMinimumLevel(LogLevel.Information)
                                                  .AddConsole()
                                                  )
                             );

        var logger = Log.CreateLogger <Program>();

#if NETCORE
        AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
#endif

        Console.WriteLine("Enter 0 to use GrpcCore provider");
        Console.WriteLine("Enter 1 to use GrpcNet provider");
        if (!int.TryParse(Console.ReadLine(), out var provider))
        {
            provider = 0;
        }

        Console.WriteLine("Enter client advertised host (Enter = localhost)");
        var advertisedHost = Console.ReadLine().Trim();
        if (string.IsNullOrEmpty(advertisedHost))
        {
            advertisedHost = "127.0.0.1";
        }

        Console.WriteLine("Enter remote advertised host (Enter = localhost)");
        var remoteAddress = Console.ReadLine().Trim();

        if (string.IsNullOrEmpty(remoteAddress))
        {
            remoteAddress = "127.0.0.1";
        }

        var actorSystemConfig = new ActorSystemConfig()
                                .WithDeadLetterThrottleCount(10)
                                .WithDeadLetterThrottleInterval(TimeSpan.FromSeconds(2));
        var system  = new ActorSystem(actorSystemConfig);
        var context = new RootContext(system);

        IRemote remote;

        if (provider == 0)
        {
            var remoteConfig = GrpcCoreRemoteConfig
                               .BindTo(advertisedHost)
                               .WithProtoMessages(ProtosReflection.Descriptor);
            remote = new GrpcCoreRemote(system, remoteConfig);
        }
        else
        {
            var remoteConfig = GrpcNetRemoteConfig
                               .BindTo(advertisedHost)
                               .WithChannelOptions(new GrpcChannelOptions
            {
                CompressionProviders = new[]
                {
                    new GzipCompressionProvider(CompressionLevel.Fastest)
                }
            }
                                                   )
                               .WithProtoMessages(ProtosReflection.Descriptor);
            remote = new GrpcNetRemote(system, remoteConfig);
        }

        await remote.StartAsync();

        var messageCount            = 1000000;
        var cancellationTokenSource = new CancellationTokenSource();
        _ = SafeTask.Run(async() => {
            while (!cancellationTokenSource.IsCancellationRequested)
            {
                var semaphore = new SemaphoreSlim(0);
                var props     = Props.FromProducer(() => new LocalActor(0, messageCount, semaphore));

                var pid = context.Spawn(props);

                try
                {
                    var actorPidResponse =
                        await remote.SpawnAsync($"{remoteAddress}:12000", "echo", TimeSpan.FromSeconds(1));

                    if (actorPidResponse.StatusCode == (int)ResponseStatusCode.OK)
                    {
                        var remotePid = actorPidResponse.Pid;
                        await context.RequestAsync <Start>(remotePid, new StartRemote {
                            Sender = pid
                        },
                                                           TimeSpan.FromSeconds(1)
                                                           );
                        var stopWatch = new Stopwatch();
                        stopWatch.Start();
                        Console.WriteLine("Starting to send");
                        var msg = new Ping();

                        for (var i = 0; i < messageCount; i++)
                        {
                            context.Send(remotePid, msg);
                        }

                        var linkedTokenSource =
                            CancellationTokenSource.CreateLinkedTokenSource(cancellationTokenSource.Token,
                                                                            new CancellationTokenSource(2000).Token
                                                                            );
                        await semaphore.WaitAsync(linkedTokenSource.Token);
                        stopWatch.Stop();
                        var elapsed = stopWatch.Elapsed;
                        Console.WriteLine("Elapsed {0}", elapsed);

                        var t = messageCount * 2.0 / elapsed.TotalMilliseconds * 1000;
                        Console.Clear();
                        Console.WriteLine("Throughput {0} msg / sec", t);
                        await context.StopAsync(remotePid);
                    }
                }
                catch (OperationCanceledException)
                {
                    await Task.Delay(1000);
                }
                catch (Exception e)
                {
                    logger?.LogError(e, "Error");
                    await Task.Delay(5000);
                }

                await context.PoisonAsync(pid);
            }
        }, cancellationTokenSource.Token
                         );

        Console.ReadLine();
        cancellationTokenSource.Cancel();
        await Task.Delay(1000);

        Console.WriteLine("Press enter to quit");
        Console.ReadLine();
        await remote.ShutdownAsync();
    }