{"action":"created","issue":{"url":"https://api.github.com/repos/nodejs/help/issues/310","repository_url":"https://api.github.com/repos/nodejs/help","labels_url":"https://api.github.com/repos/nodejs/help/issues/310/labels{/name}","comments_url":"https://api.github.com/repos/nodejs/help/issues/310/comments","events_url":"https://api.github.com/repos/nodejs/help/issues/310/events","html_url":"https://github.com/nodejs/help/issues/310","id":180549342,"number":310,"title":"How to ensure Single Thread execution model in NodeJS?","user":{"login":"galvesribeiro","id":4714040,"avatar_url":"https://avatars.githubusercontent.com/u/4714040?v=3","gravatar_id":"","url":"https://api.github.com/users/galvesribeiro","html_url":"https://github.com/galvesribeiro","followers_url":"https://api.github.com/users/galvesribeiro/followers","following_url":"https://api.github.com/users/galvesribeiro/following{/other_user}","gists_url":"https://api.github.com/users/galvesribeiro/gists{/gist_id}","starred_url":"https://api.github.com/users/galvesribeiro/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/galvesribeiro/subscriptions","organizations_url":"https://api.github.com/users/galvesribeiro/orgs","repos_url":"https://api.github.com/users/galvesribeiro/repos","events_url":"https://api.github.com/users/galvesribeiro/events{/privacy}","received_events_url":"https://api.github.com/users/galvesribeiro/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-10-03T01:37:11Z","updated_at":"2016-10-03T14:46:30Z","closed_at":null,"body":"Hello guys.\r\n\r\nI'm trying to implement the [Distributed Virtual Actors model](http://research.microsoft.com/pubs/210931/Orleans-MSR-TR-2014-41.pdf) which is an enhanced implementation of the [Actor model](https://en.wikipedia.org/wiki/Actor_model) and is the core of [Orleans](http://dotnet.github.io/orleans/).\r\n\r\nThe idea is to implement the Virtual Actors with NodeJS just like EA Games did for java with their port called [Orbit](https://github.com/orbit/orbit) so we would bring distributed computing to NodeJS community.\r\n\r\nIn my initial research, the first barrier I found was the Single Thread guarantee defined in the Virtual Actors model. With this guarantee, each actor activation can only process a single message at time, removing problems like race conditions and help ensure the Actor state is consistent across the system (more details check the [Turns](http://dotnet.github.io/orleans/Documentation/Getting-Started-With-Orleans/Grains.html) in Orleans docs).\r\n\r\nSo, how can I ensure that in JavaScript and NodeJS? In Orleans implementation there is a custom Task Scheduler that make sure there is a single Actor message being processed at a time on an Actor instance.\r\n\r\nCan anyone point me out the right resources?\r\n\r\nThanks!"},"comment":{"url":"https://api.github.com/repos/nodejs/help/issues/comments/251125472","html_url":"https://github.com/nodejs/help/issues/310#issuecomment-251125472","issue_url":"https://api.github.com/repos/nodejs/help/issues/310","id":251125472,"user":{"login":"galvesribeiro","id":4714040,"avatar_url":"https://avatars.githubusercontent.com/u/4714040?v=3","gravatar_id":"","url":"https://api.github.com/users/galvesribeiro","html_url":"https://github.com/galvesribeiro","followers_url":"https://api.github.com/users/galvesribeiro/followers","following_url":"https://api.github.com/users/galvesribeiro/following{/other_user}","gists_url":"https://api.github.com/users/galvesribeiro/gists{/gist_id}","starred_url":"https://api.github.com/users/galvesribeiro/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/galvesribeiro/subscriptions","organizations_url":"https://api.github.com/users/galvesribeiro/orgs","repos_url":"https://api.github.com/users/galvesribeiro/repos","events_url":"https://api.github.com/users/galvesribeiro/events{/privacy}","received_events_url":"https://api.github.com/users/galvesribeiro/received_events","type":"User","site_admin":false},"created_at":"2016-10-03T14:46:30Z","updated_at":"2016-10-03T14:46:30Z","body":"Yeah but in Orleans one thread run multiple workitem cooperatively. So we can multiplex the thread with multiple actor activations and all the code must be async for that. \r\n\r\nSo, I know the userland in nodejs is a single thread but the point is that inside that single thread I need to ensure that one Actor can execute one message at a time. I could use 1 thread/worker per actor but that obviously would not scale. By having the cooperative multitask in Orleans we have hundreds of thousands of actors in a single server using multiple cores and the reason it work, is because of the Turn-based execution model, which makes the actors to cooperatively multitask in a single thread.\r\n\r\nSo the basic idea is that an actor class would have a mailbox that can process a single message at time asynchronously. So it would have an internal lock on this mailbox avoiding subsequent messages to being processed on that same actor while it is processing another message. All it asynchronously, so other actors in the same thread can process its messages n parallel while it is asynchronously awaiting the execution of other messages in other actors.\r\n\r\nDoes that make sense?"}}
{"actor":{"display_login":"galvesribeiro"}}