public void AsyncTask_Should_Run_Actions_In_Created_RoundRobinPool_With_GivenSize2() { var waitHandle = new CountdownEvent(5); Func <int> runTask = () => { waitHandle.Signal(); return(Thread.CurrentThread.ManagedThreadId); }; int poolSize = 2; int threadId1 = 0, threadId2 = 0, threadId3 = 0, threadId4 = 0, threadId5 = 0; lock (_lock) { var pool = new RoundRobinPool(poolSize); AsyncTasks.AddPool("testPool5", pool); } AsyncTasks.Run(() => { threadId1 = runTask(); }, "testPool5"); AsyncTasks.Run(() => { threadId2 = runTask(); }, "testPool5"); AsyncTasks.Run(() => { threadId3 = runTask(); }, "testPool5"); AsyncTasks.Run(() => { threadId4 = runTask(); }, "testPool5"); AsyncTasks.Run(() => { threadId5 = runTask(); }, "testPool5"); waitHandle.Wait(); AsyncTasks.RemoveAll(); Assert.AreEqual(threadId1, threadId3); Assert.AreEqual(threadId1, threadId5); Assert.AreEqual(threadId2, threadId2); Assert.AreNotEqual(threadId1, threadId2); }
private void BoardingPrerequisites(SimulateBoardingOfAccounts cmd) { var props = new RoundRobinPool(Environment.ProcessorCount * 3).Props(Props.Create <BoardAccountActor>()); var router = Context.ActorOf(props, $"Client{cmd.ClientName}Router"); Self.Tell(new BoardClient(cmd, cmd.ClientAccountsFilePath, cmd.ObligationsFilePath, router)); }
public void Tasks_Should_Be_Add_in_RoundRobin_Manner() { //Arrange var threadId = new List <int>(); var lockObject = new Object(); var waitHandle = new CountdownEvent(10); RoundRobinPool roundRobinPool = new RoundRobinPool(3); //Act roundRobinPool.Enqueue(() => getTask(lockObject, waitHandle, threadId)); roundRobinPool.Enqueue(() => getTask(lockObject, waitHandle, threadId)); roundRobinPool.Enqueue(() => getTask(lockObject, waitHandle, threadId)); roundRobinPool.Enqueue(() => getTask(lockObject, waitHandle, threadId)); roundRobinPool.Enqueue(() => getTask(lockObject, waitHandle, threadId)); roundRobinPool.Enqueue(() => getTask(lockObject, waitHandle, threadId)); roundRobinPool.Enqueue(() => getTask(lockObject, waitHandle, threadId)); roundRobinPool.Enqueue(() => getTask(lockObject, waitHandle, threadId)); roundRobinPool.Enqueue(() => getTask(lockObject, waitHandle, threadId)); roundRobinPool.Enqueue(() => getTask(lockObject, waitHandle, threadId)); waitHandle.Wait(); //Assert var threadCounts = threadId.GroupBy(x => x).Select(x => x.Count()).OrderByDescending(x => x); Assert.AreEqual(3, threadCounts.Count()); Assert.AreEqual(4, threadCounts.FirstOrDefault()); Assert.AreEqual(3, threadCounts.LastOrDefault()); }
public void Can_serialize_RoundRobinPoolWithCustomResizer() { var defaultResizer = new DefaultResizer(2, 4, 1, 0.5, 0.2, 0.1, 55); var message = new RoundRobinPool(nrOfInstances: 25, resizer: defaultResizer); AssertEqual(message); }
public void Jobs_on_multiple_threads_should_distribute_in_round_robin_fashion_test() { var even = new List <int>(); var odd = new List <int>(); var pool = new RoundRobinPool(2); var waitHandle = new CountdownEvent(4); Action <List <int> > addItemTo = x => { x.Add(Thread.CurrentThread.ManagedThreadId); waitHandle.Signal(); }; pool.Enqueue(() => addItemTo(odd)); pool.Enqueue(() => addItemTo(even)); pool.Enqueue(() => addItemTo(odd)); pool.Enqueue(() => addItemTo(even)); pool.StopAdding(); waitHandle.Wait(); even.Count.Should().Be(2); odd.Count.Should().Be(2); even[0].Should().Be(even[1]); odd[0].Should().Be(odd[1]); even[0].Should().NotBe(odd[0]); }
public void Can_serialize_ClusterRouterPool() { var roundRobinPool = new RoundRobinPool(nrOfInstances: 4); var clusterRouterPoolSettings = new ClusterRouterPoolSettings(2, 5, true, "Richard, Duke"); var message = new ClusterRouterPool(roundRobinPool, clusterRouterPoolSettings); AssertEqual(message); }
public void Can_serialize_ClusterRouterPoolWithEmptyRole() { var roundRobinPool = new RoundRobinPool(nrOfInstances: 4); var clusterRouterPoolSettings = new ClusterRouterPoolSettings(2, 5, true, null); var message = new ClusterRouterPool(roundRobinPool, clusterRouterPoolSettings); AssertEqual(message); }
public void CanSerializePool() { var message = new RoundRobinPool(10, new DefaultResizer(0, 1)); var serializer = Sys.Serialization.FindSerializerFor(message); var serialized = serializer.ToBinary(message); var deserialized = (RoundRobinPool)serializer.FromBinary(serialized, typeof(RoundRobinPool)); Assert.Equal(message, deserialized); }
public void Adding_item_after_completion_should_throw() { var pool = new RoundRobinPool(1); pool.StopAdding(); Action add = () => pool.Enqueue(Empty); add.ShouldThrow <InvalidOperationException>(); }
public void Single_worker_process_should_use_same_thread_test() { int id1 = 0, id2 = 0; var pool = new RoundRobinPool(1); pool.Enqueue(() => id1 = Thread.CurrentThread.ManagedThreadId); pool.Enqueue(() => id2 = Thread.CurrentThread.ManagedThreadId); pool.StopAdding(); id1.Should().Be(id2); }
public void CanSerializeRoundRobinPool() { var decider = Decider.From( Directive.Restart, Directive.Stop.When<ArgumentException>(), Directive.Stop.When<NullReferenceException>()); var supervisor = new OneForOneStrategy(decider); var message = new RoundRobinPool(10, new DefaultResizer(0, 1), supervisor, "abc"); AssertEqual(message); }
public void Can_serialize_RoundRobinPoolWithCustomDispatcher() { var defaultResizer = new DefaultResizer(2, 4, 1, 0.5, 0.2, 0.1, 55); var message = new RoundRobinPool( nrOfInstances: 25, resizer: defaultResizer, supervisorStrategy: Pool.DefaultSupervisorStrategy, routerDispatcher: "my-dispatcher", usePoolDispatcher: true); AssertEqual(message); }
public static async Task Run() { var dataFactory = GetService <DataFactory>(); var redis = dataFactory.GetRedisClient().GetDatabase(); var pubAndSub = redis.Multiplexer.GetSubscriber(); _log.Information("Running"); await pubAndSub.SubscribeAsync(ChannelConfig.RequestCar, async (channel, message) => { var request = message.ToString().FromJsonTo <RequestCar>(); //Here you can have any proccess you want before checking on redis. string keyToCheck = keyToSayImChecking + request.Id; if (redis.KeyExists(keyToCheck)) { _log.Information("It's already beeing checked | request {0}", request.Id); } else { redis.StringSet(keyToCheck, 1, TimeSpan.FromSeconds(30)); _log.Information("Checking request: {0}", request.Id); List <Car> cars = new List <Car>(); for (int i = request.StartYear; i <= request.EndYear; i++) { string key = request.Automaker + request.Name + i; if (redis.KeyExists(key)) { var car = (await redis.StringGetAsync(key)).ToString().FromJsonTo <Car>(); cars.Add(car); } } var response = new ResponseCar() { Cars = cars }; await pubAndSub.PublishAsync(ChannelConfig.ResponseCar, response.ToJson()); } }); /// Redis Lock Example. var system = GetService <ActorSystem>(); var props = new RoundRobinPool(5).Props(Props.Create <Master>()); system.ActorOf(props, "master"); Console.ReadLine(); }
public static IActorRef CreateCoordinator(ActorSystem actorSystem) { var defaultResizer = new DefaultResizer( lower: PoolRouterHelper.Lower, upper: PoolRouterHelper.Upper, pressureThreshold: 1, rampupRate: 0.2, backoffThreshold: 0.2, backoffRate: 0.1, messagesPerResize: PoolRouterHelper.MessagesPerResize); var pool = new RoundRobinPool( nrOfInstances: PoolRouterHelper.NrOfInstances, resizer: defaultResizer); return(actorSystem.ActorOf( Props.Create(() => new CoordinatorArtespActor()) .WithRouter(pool), ActorsPath.CoordinatorArtespActor.Name)); }
private void Deploy(IEnumerable <WorkerDeploymentInfo> workerDeploymentInfos) { if (workerDeploymentInfos == null || !workerDeploymentInfos.Any()) { throw new ArgumentNullException(nameof(workerDeploymentInfos)); } _workersBalancer = new WorkersBalancer(workerDeploymentInfos); _actorSystem = ActorSystem.Create("MainSystem", ConfigurationFactory.ParseString(_mainSystemHoconConfig)); _resultsDispatchActorRef = _actorSystem.ActorOf(Props.Create(() => new ResultsDispatchActor())); _workerRouters = workerDeploymentInfos.ToDictionary(i => i, i => { var router = new RoundRobinPool(InitialActorsCount, new DefaultResizer(InitialActorsCount, int.MaxValue, pressureThreshold: 0, messagesPerResize: 1)); Address remoteAddress = Address.Parse($"akka.tcp://{Constants.WorkersSystemName}@{i.IP}:{Constants.WorkersPortNumber}"); return(Props.Create(() => new WorkerActor(_resultsDispatchActorRef)) .WithDeploy(Akka.Actor.Deploy.None.WithScope(new RemoteScope(remoteAddress))) .WithRouter(router)); }); Console.WriteLine($"Main system has been deployed successfully."); }
public Luiz() { Receive <InitializeActor>(m => { _log.Information("[Luiz] - Inicializando"); sw.Restart(); var props = new RoundRobinPool(m.WorkerCounter).Props(Props.Create <Worker>()); _worker = Context.ActorOf(props, "worker" + m.WorkerCounter); _currentWorkers = m.WorkerCounter; for (int i = 0; i < _totalInsert * _timesToRun; i++) { Message msg = new Message("MSG" + i.ToString()); msg.Client = new Client() { Id = Guid.NewGuid(), City = "BH" + i, CPF = "01" + i, Name = "Nome" + i }; _worker.Tell(msg); } }); Receive <EndActor>(m => { sw.Stop(); _log.Information("{0} Atores | Tempo gasto no sistema de atores: {1}", _currentWorkers, sw.Elapsed); _actorInserted = 0; //if (_currentWorkers < _workerMax) //{ // Self.Tell(new InitializeActor(_currentWorkers + 5)); //} }); }
private void Must_have_routees_at_startup() { AwaitClusterUp(_config.First, _config.Second, _config.Third); Within(TimeSpan.FromSeconds(20), () => { RunOn(() => { var pool = new RoundRobinPool(10); var routerPoolSettings = new ClusterRouterPoolSettings(1000, 1, true, "b"); var config = new ClusterRouterPool(pool, routerPoolSettings); var router = Sys.ActorOf(Props.Create(() => new EchoActor()).WithRouter(config), "myRouter"); AwaitAssert(() => { router.Tell("i"); ExpectMsg("i", TimeSpan.FromMilliseconds(100)); }, interval: TimeSpan.FromMilliseconds(250)); }, _config.First); EnterBarrier("after-1"); }); }
public void Can_serialize_RoundRobinPool() { var message = new RoundRobinPool(nrOfInstances: 25); AssertEqual(message); }
// // RoundRobinPool // private byte[] RoundRobinPoolToProto(RoundRobinPool roundRobinPool) { return(GenericRoutingPoolBuilder(roundRobinPool).ToByteArray()); }
public void CanSerializeRoundRobinPool() { var message = new RoundRobinPool(10, new DefaultResizer(0, 1)); AssertEqual(message); }