static async Task Main(string[] args)
        {
            var system = new ActorSystem();

            var remoteConfig = GrpcCoreRemoteConfig
                               .BindToLocalhost(12000)
                               .WithProtoMessages(ProtosReflection.Descriptor);

            var consulProvider =
                new ConsulProvider(new ConsulProviderConfig(), c => c.Address = new Uri("http://consul:8500/"));

            var clusterConfig =
                ClusterConfig.Setup("MyCluster", consulProvider, new PartitionIdentityLookup());

            system
            .WithRemote(remoteConfig)
            .WithCluster(clusterConfig);

            var grains = new Grains(system.Cluster());

            grains.HelloGrainFactory(() => new HelloGrain());

            await system.Cluster().StartMemberAsync();

            Console.CancelKeyPress += async(e, y) =>
            {
                Console.WriteLine("Shutting Down...");
                await system.Cluster().ShutdownAsync();
            };
            await Task.Delay(-1);
        }
Exemplo n.º 2
0
    private static async Task Main()
    {
        var system = new ActorSystem()
                     .WithRemote(GrpcCoreRemoteConfig
                                 .BindToLocalhost()
                                 .WithProtoMessages(ProtosReflection.Descriptor))
                     .WithCluster(ClusterConfig
                                  .Setup("MyCluster", new ConsulProvider(new ConsulProviderConfig()), new PartitionIdentityLookup()));

        await system
        .Cluster()
        .StartClientAsync();

        Console.WriteLine("Started");
        await Task.Delay(2000);


        var helloGrain = system.Cluster().GetHelloGrain("MyGrain");

        var res = await helloGrain.SayHello(new HelloRequest(), WithTimeout(5000));

        Console.WriteLine(res.Message);

        res = await helloGrain.SayHello(new HelloRequest(), WithTimeout(5000));

        Console.WriteLine(res.Message);

        Console.CancelKeyPress += async(e, y) => {
            Console.WriteLine("Shutting Down...");
            await system.Cluster().ShutdownAsync();
        };

        await Task.Delay(-1);
    }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
        static async Task Main(string[] args)
        {
            var log = LoggerFactory.Create(x => x.AddConsole().SetMinimumLevel(LogLevel.Debug));

            Log.SetLoggerFactory(log);
            Console.WriteLine("Starting Node2");

            var system        = new ActorSystem();
            var serialization = new Serialization();
            var context       = new RootContext(system);

            serialization.RegisterFileDescriptor(ProtosReflection.Descriptor);


            var props = Props.FromFunc(
                ctx =>
            {
                switch (ctx.Message)
                {
                case HelloRequest _:
                    ctx.Respond(new HelloResponse {
                        Message = "Hello from node 2"
                    });
                    break;
                }

                return(Task.CompletedTask);
            }
                );


            var remoteConfig = GrpcCoreRemoteConfig
                               .BindToLocalhost(12001)
                               .WithProtoMessages(ProtosReflection.Descriptor);

            var clusterConfig = ClusterConfig.Setup("MyCluster",
                                                    new ConsulProvider(new ConsulProviderConfig(), c => c.Address = new Uri("http://consul:8500/")),
                                                    new PartitionIdentityLookup()
                                                    ).WithClusterKind("HelloKind", props);

            system
            .WithRemote(remoteConfig)
            .WithCluster(clusterConfig);


            // CONSUL
            await system
            .Cluster()
            .StartMemberAsync();

            await Task.Delay(-1);

            Console.WriteLine("Shutting Down...");
            await system
            .Cluster()
            .ShutdownAsync();
        }
Exemplo n.º 5
0
        private static ClusterConfig GetClusterConfig(string clusterName) => ClusterConfig
        .Setup(clusterName, GetClusterProvider(), new IdentityStorageLookup(GetIdentityLookup(clusterName)))
        .WithClusterKind("device", Props.FromProducer(() => new DeviceActor())
                         //TODO: Uncomment to enable tracing
                         // .WithOpenTracing()

                         //TODO: Uncomment to enable local affinity
                         // .WithPoisonOnRemoteTraffic(0.1f)
                         // .WithPidCacheInvalidation()
                         );
        private static ClusterConfig GetClusterConfig(
            IClusterProvider clusterProvider,
            IIdentityLookup identityLookup
            )
        {
            var helloProps = Props.FromProducer(() => new WorkerActor());

            return(ClusterConfig
                   .Setup("mycluster", clusterProvider, identityLookup)
                   .WithClusterKind("hello", helloProps));
        }
Exemplo n.º 7
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()));
        static async Task Main(string[] args)
        {
            var log = LoggerFactory.Create(x => x.AddConsole().SetMinimumLevel(LogLevel.Debug));

            Log.SetLoggerFactory(log);

            Console.WriteLine("Starting Node1");
            var system = new ActorSystem();

            var context = new RootContext(system);

            var remoteConfig = GrpcCoreRemoteConfig
                               .BindToLocalhost(12000)
                               .WithProtoMessages(ProtosReflection.Descriptor);

            var clusterConfig = ClusterConfig.Setup("MyCluster",
                                                    new ConsulProvider(new ConsulProviderConfig(), c => c.Address = new Uri("http://consul:8500/")),
                                                    new PartitionIdentityLookup()
                                                    );

            system
            .WithRemote(remoteConfig)
            .WithCluster(clusterConfig);

            // CONSUL

            await system
            .Cluster()
            .StartMemberAsync();

            var i = 10000;

            while (i-- > 0)
            {
                var res = await system
                          .Cluster()
                          .RequestAsync <HelloResponse>("TheName", "HelloKind", new HelloRequest(), CancellationToken.None);

                Console.WriteLine(res.Message);
                await Task.Delay(500);
            }

            await Task.Delay(-1);

            Console.WriteLine("Shutting Down...");

            await system.Cluster().ShutdownAsync();
        }
Exemplo n.º 9
0
        private static ClusterConfig GetClusterConfig()
        {
            var consulProvider =
                new ConsulProvider(new ConsulProviderConfig());

            //use an empty store, no persistence
            var store = new EmptyKeyValueStore <Subscribers>();

            var clusterConfig =
                ClusterConfig
                .Setup("MyCluster", consulProvider, new PartitionIdentityLookup())
                .WithClusterKind("topic", Props.FromProducer(() => new TopicActor(store)))
                .WithPubSubBatchSize(batchSize);

            return(clusterConfig);
        }
Exemplo n.º 10
0
        public static (ClusterConfig, GrpcCoreRemoteConfig) CreateClusterConfig(IClusterSettings clusterSettings, IClusterProvider clusterProvider, IIdentityLookup identityLookup, IDescriptorProvider descriptorProvider, ILogger _logger)
        {
            //var portStr = Environment.GetEnvironmentVariable("PROTOPORT") ?? $"{RemoteConfigBase.AnyFreePort}";

            var clusterName = clusterSettings.ClusterName;

            var hostip         = Environment.GetEnvironmentVariable("PROTOHOST");
            var host           = clusterSettings.ClusterHost;
            var advertisedHost = clusterSettings.ClusterHost;
            var port           = clusterSettings.ClusterPort;

            if ("protohost".Equals(host))
            {
                host           = hostip;
                advertisedHost = hostip;
                _logger.LogDebug($"Using PROTOHOST");
            }
            _logger.LogDebug($"BindTo to {host} port {port}");
            _logger.LogDebug($"WithAdvertisedHost to {advertisedHost}");

            FileDescriptor[] descriptors = descriptorProvider.GetDescriptors();


            // TOOD: This doesn't seem to work. Why?
            List <ChannelOption> options = new List <ChannelOption>()
            {
                new ChannelOption(ChannelOptions.MaxSendMessageLength, (100 * 1024 * 1024)),
                new ChannelOption(ChannelOptions.MaxReceiveMessageLength, (100 * 1024 * 1024))
            };


            GrpcCoreRemoteConfig remoteConfig = GrpcCoreRemoteConfig.BindTo(host, port)
                                                .WithEndpointWriterMaxRetries(0)
                                                .WithRemoteDiagnostics(true)
                                                .WithAdvertisedHost(advertisedHost)
                                                .WithProtoMessages(descriptors)
                                                .WithChannelOptions(options);

            var clusterConfig = ClusterConfig.Setup(clusterName, clusterProvider, identityLookup);

            return(clusterConfig, remoteConfig);
        }
Exemplo n.º 11
0
        private static async Task Main()
        {
            var remoteConfig = GrpcCoreRemoteConfig
                               .BindToLocalhost()
                               .WithProtoMessages(ProtosReflection.Descriptor);

            var consulProvider =
                new ConsulProvider(new ConsulProviderConfig(), c => c.Address = new Uri("http://consul:8500/"));

            var clusterConfig =
                ClusterConfig
                .Setup("MyCluster", consulProvider, new PartitionIdentityLookup());

            var system = new ActorSystem()
                         .WithRemote(remoteConfig)
                         .WithCluster(clusterConfig);

            await system
            .Cluster()
            .StartMemberAsync();

            //subscribe to the eventstream via type, just like you do locally
            system.EventStream.SubscribeToTopic <SomeMessage>("MyTopic.*", x => Console.WriteLine($"Got message for {x.Name}"));

            //publish messages onto the eventstream on Subtopic1 on MyTopic root
            //but do this using cluster broadcasting. this will publish the event
            //to the event stream on all the members in the cluster
            //this is best effort only, if some member is unavailable, there is no guarantee associated here
            system.Cluster().BroadcastEvent(new SomeMessage("ProtoActor", "MyTopic.Subtopic1"));

            //this message is published on a topic that is not subscribed to, and nothing will happen
            system.Cluster().BroadcastEvent(new SomeMessage("Asynkron", "AnotherTopic"));

            //send a message to the same root topic, but another child topic
            system.Cluster().BroadcastEvent(new SomeMessage("Do we get this?", "MyTopic.Subtopic1"));

            //this example is local only.
            //see ClusterEventStream for cluster broadcast onto the eventstream

            Console.ReadLine();
        }
Exemplo n.º 12
0
        public static void Run()
        {
            var l = LoggerFactory.Create(x => x.AddConsole().SetMinimumLevel(LogLevel.Information));

            Log.SetLoggerFactory(l);
            var config = ActorSystemConfig.Setup().WithMetricsProviders(new PrometheusConfigurator());

            var remoteConfig = GrpcCoreRemoteConfig
                               .BindToLocalhost()
                               .WithProtoMessages(MessagesReflection.Descriptor);

            var clusterConfig =
                ClusterConfig
                .Setup("MyCluster", new ConsulProvider(new ConsulProviderConfig(), c => c.Address = new Uri("http://127.0.0.1:8500/")),
                       new PartitionIdentityLookup()
                       );

            var system = new ActorSystem(config)
                         .WithRemote(remoteConfig)
                         .WithCluster(clusterConfig);

            system
            .Cluster()
            .StartMemberAsync();

            var props = Props.FromProducer(() => new MyActor());

            var config2 = ActorSystemConfig.Setup().WithMetricsProviders(new PrometheusConfigurator());

            var remoteConfig2 = GrpcCoreRemoteConfig
                                .BindToLocalhost()
                                .WithProtoMessages(MessagesReflection.Descriptor);

            var clusterConfig2 =
                ClusterConfig
                .Setup("MyCluster", new ConsulProvider(new ConsulProviderConfig(), c => c.Address = new Uri("http://127.0.0.1:8500/")),
                       new PartitionIdentityLookup()
                       )
                .WithClusterKind("somekind", props);

            var system2 = new ActorSystem(config2)
                          .WithRemote(remoteConfig2)
                          .WithCluster(clusterConfig2);

            system2
            .Cluster()
            .StartMemberAsync();

            _ = SafeTask.Run(async() => {
                var r = new Random();

                while (true)
                {
                    await Task.Delay(r.Next(1, 2000));
                    await system.Cluster().RequestAsync <SomeResponse>($"someactor{r.Next(1, 100)}", "somekind", new SomeRequest(),
                                                                       CancellationTokens.WithTimeout(5000)
                                                                       );
                }
            }
                             );
        }