Multiple Workers a Long File
Hi,
What could be best solution for processing a long file line by line ?
Being a rookie in Orleans I presume there are two scenarios: . Stateless Workers . A limited list of non reentrant Grains. This would be workers too but statefull. I beleive that this would be done to avoid the reentrant grains. This was suggested here https://orleans.codeplex.com/wikipage?title=Reentrant%20Grains&referringTitle=Advanced%20Concepts
There is a third solution that is the main Grain start a limited number of thread for each line. But I do not like it.
Of course with a limited number of Grains (actors) the main Grain would have to do some kind of round robin process to feed line by line the worker Grains.
The workers must process the line and return some information based on the processed line to the main Grain that will return to the client the overall result.
Now assume that there is a lot of Clients sending, at the same time, the same message. How could we make this work efficiently and really Async.
What could be the best solution, the best practice in Orleans ?
- The worker Grains send a message to the Main Grain when they have processed the line, and then receive another line;
- Use Orleans Streams and the Main Grain would the Observer and the worker Grains Observables. The Observer would subscribe the OnNextAsync of the Observables, and then feed the worker with another line. A complete Reactive solution;
Can you help me please ?
Thanks in advance
Lets see if I understood your scenario correctly:
- one or multiple clients are sending file lines, one by one, to Orleans, to process them.
- each line needs to be processes separately and return some result.
- processing of a single line is stateless (received, processed, done, no state left)
- all results from all lines need to be aggregated in one place
- The aggregation result needs to be send back to all or one client. Is that correct? Is it only one aggregation result or multiple periodic results? Is it just one file and that is all, or do the files (lets call it a big Task of multiple jobs/lines) come on all the time? Do lines for different files/Tasks have to be processed by a different grains, or can you reuse the grains? Also please be specific about where the input lines come from. Are they sent by one client, multiple clients or from a grain?
It is a typical problem of Divide and Conquer pattern.
Imagine we have a Web Application that calls a Web API. The Web ApI will then send a message to a grain, lets call it main grain, with a filePath as parameter.
This main grain will read all the lines from the file and ask workers to process one or more lines. It is up to the main grain decide if it will be one or more lines at the same time. The Workers will transform the lines received and return them to the sender (the main grain).
When all the lines have been processed and returned by the workers the main grain will return the result to the Web API service that will then return something to the Web Page. The Web application can have, of course, a lot of users so the Web APi will have to work concurrently.
The problem can be extended so that the message sent to the main grain can have not only one file but more than one file.
I suppose that if there are multiple connections from the Web Client it is more efficient to have a pool of grains to process the files, so instead of reusing the main grain there could be a pool of main grains.
Maybe the best solution could be a hierarchy of three levels:
. Level 1: A pool of Grains will receive messages from the Web API. The file, or files if there are more than one per message, would be sent, one file per worker, to Level 2 and then the transformed lines will be returned to the Web API; . Level 2: StatelessWorkers will read the file and send the lines, one by one, to Level 3 when all the lines have been processed it will return to Level 1; . Level 3 StatelessWorkers will transform the lines and send back to Level 2;
I am a rookie so I do not know if this is the best practice. But it is what I would do.
Do you agree ?
Thanks
@mvbaffa It would help to know where the bottlenecks in this scenario are. Is it transformation of the lines? Is that a CPU intensive operation? Is the transformation functional or depends on a shared state?
There are two scenarios:
- Fast transformation: extract fields from the line and return an object with the extracted info;
- DB Mapping transformation: extract fields from the line and return an object with the extracted info, but the fields must be mapped using database tables (SQL Server);
For 1) I don't immediately see why it would be beneficial to offload processing of lines to other grains. Considering the overhead of messaging, wouldn't it be faster to extract fields inline right after reading from a file?
In 2), is the operation of mapping via database 100% with no data being cached and reused? If so, then it may not matter much which grain makes the database call.
I'm trying to understand if it would make sense to collapse layers 2 and 3. If the transformation is not CPU-intensive and you can do it inline within Layer 2, then case 1) is easy. If calls to the database in 2) are stateless, then 2) becomes very similar to 1), as no CPU work is done while the grain awaits a call to the database.
However, there are potential reasons why such collapsing wouldn't makes sense. For example, if you want to read from different offsets in a single file in parallel.
Ok, I got it,
I will try to implement a small prototype and, as soon as I have it written, I let you know
Thank you very much