Re: [akka-user] Cancel Actor job

2018-02-08 Thread Johan Andrén
I'm afraid we haven't gotten to porting it to Java yet.

--
Johan

On Monday, February 5, 2018 at 8:40:03 AM UTC+1, Richard Gong wrote:
>
> Thanks Johan. It looks very good. Is there any Java version? I found one 
> on git hub: 
> https://github.com/typesafehub/activator-akka-distributed-workers-java, 
> yet it's an old version 2 years ago and the api being used are obsoleted.
>>
>>

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


Re: [akka-user] Cancel Actor job

2018-02-04 Thread Richard Gong
Thanks Johan. It looks very good. Is there any Java version? I found one on 
git 
hub: https://github.com/typesafehub/activator-akka-distributed-workers-java, 
yet it's an old version 2 years ago and the api being used are obsoleted.
>
>

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


Re: [akka-user] Cancel Actor job

2018-02-02 Thread Akka Team
Maybe this distributed workers guide could be good inspiration:
https://developer.lightbend.com/guides/akka-distributed-workers-scala/

--
Johan
Akka Team

On Fri, Feb 2, 2018 at 3:10 AM, Konrad “ktoso” Malawski <
konrad.malaw...@lightbend.com> wrote:

> Have a look at "cluster sharding”, it’s smarter than just routers.
>
> --
> Cheers,
> Konrad 'ktoso ' Malawski
> Akka  @ Lightbend 
>
> On February 2, 2018 at 11:09:19, Richard Gong (gong...@gmail.com) wrote:
>
> That works in a single machine. Now take a step forward to have the
> workers in a cluster. The master needs to dispatch jobs to workers. I'm
> looking at the document of router, https://doc.akka.io/
> docs/akka/current/cluster-usage.html?language=scala#
> router-with-pool-of-remote-deployed-routees. My thought is worker code
> can be dynamically deployed and join the cluster when needed. Let's say I
> have these 2 components running in different nodes.
>
>- Master: accepts jobs and dispatches them to WorkerAdvisor.
>- WorkerAdvisor: creates an actual worker to run the job.
>
> Now how the Master knows dispatches a job to a WorkerAdvisor which is
> free. Broadcasting job to all WorkerAdvisors doesn't seem to be a good way
> because we don't want a job to run in multiple places. Maintaining state of
> WorkerAdvisor in Master seems odd to me as well.
> --
> >> Read the docs: http://akka.io/docs/
> >> Check the FAQ: http://doc.akka.io/docs/akka/
> current/additional/faq.html
> >> Search the archives: https://groups.google.com/group/akka-user
> ---
> You received this message because you are subscribed to the Google Groups
> "Akka User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to akka-user+unsubscr...@googlegroups.com.
> To post to this group, send email to akka-user@googlegroups.com.
> Visit this group at https://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>
> --
> >> Read the docs: http://akka.io/docs/
> >> Check the FAQ: http://doc.akka.io/docs/akka/
> current/additional/faq.html
> >> Search the archives: https://groups.google.com/group/akka-user
> ---
> You received this message because you are subscribed to the Google Groups
> "Akka User List" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to akka-user+unsubscr...@googlegroups.com.
> To post to this group, send email to akka-user@googlegroups.com.
> Visit this group at https://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


Re: [akka-user] Cancel Actor job

2018-02-01 Thread Konrad “ktoso” Malawski
Have a look at "cluster sharding”, it’s smarter than just routers.

-- 
Cheers,
Konrad 'ktoso ' Malawski
Akka  @ Lightbend 

On February 2, 2018 at 11:09:19, Richard Gong (gong...@gmail.com) wrote:

That works in a single machine. Now take a step forward to have the workers
in a cluster. The master needs to dispatch jobs to workers. I'm looking at
the document of router,
https://doc.akka.io/docs/akka/current/cluster-usage.html?language=scala#router-with-pool-of-remote-deployed-routees.
My thought is worker code can be dynamically deployed and join the cluster
when needed. Let's say I have these 2 components running in different
nodes.

   - Master: accepts jobs and dispatches them to WorkerAdvisor.
   - WorkerAdvisor: creates an actual worker to run the job.

Now how the Master knows dispatches a job to a WorkerAdvisor which is free.
Broadcasting job to all WorkerAdvisors doesn't seem to be a good way
because we don't want a job to run in multiple places. Maintaining state of
WorkerAdvisor in Master seems odd to me as well.
--
>> Read the docs: http://akka.io/docs/
>> Check the FAQ:
http://doc.akka.io/docs/akka/current/additional/faq.html
>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups
"Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an
email to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


Re: [akka-user] Cancel Actor job

2018-02-01 Thread Richard Gong
That works in a single machine. Now take a step forward to have the workers 
in a cluster. The master needs to dispatch jobs to workers. I'm looking at 
the document of 
router, 
https://doc.akka.io/docs/akka/current/cluster-usage.html?language=scala#router-with-pool-of-remote-deployed-routees.
 
My thought is worker code can be dynamically deployed and join the cluster 
when needed. Let's say I have these 2 components running in different 
nodes. 

   - Master: accepts jobs and dispatches them to WorkerAdvisor.
   - WorkerAdvisor: creates an actual worker to run the job. 

Now how the Master knows dispatches a job to a WorkerAdvisor which is free. 
Broadcasting job to all WorkerAdvisors doesn't seem to be a good way 
because we don't want a job to run in multiple places. Maintaining state of 
WorkerAdvisor in Master seems odd to me as well.

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


Re: [akka-user] Cancel Actor job

2018-01-25 Thread Konrad “ktoso” Malawski
Actors may not be that cheap.

They are ~400 bytes for an Actor; plus whatever you put inside one of
course, but that’s your state, not the Actor’s fault ;)

I tried to create a worker actor every time the master actor received a
start job message and found out that it created a new thread to run it.
Seems to me that the default dispatcher is an unbounded thread pool. It
doesn't look good to me -- it falls back to the thread per request model???

That’s not true. Actors share a thread-pool, the pool may decide more
threads would be a good idea ;-)

if you are doing blocking operations, please follow the advice which I
already posted in the previous email, read about managing blocking:

https://doc.akka.io/docs/akka/current/dispatchers.html#blocking-needs-careful-management

Since the jobs are CPU intensive, I have to control the number of
concurrent jobs.

Sure, which is why I suggested the master actor who can control how many
workers are active at any given time.

Number of threads you can limit by following the docs above, in dispatchers.

Here's an update of the code. It works to me. Just want to clarify if this
a right way to do.

// When master actor receive startJob message, creates a new worker actor
with a predefined dispatcher with a fixed thread pool to control the number
of concurrent jobs.
ActorRef worker =
getContext().actorOf(Props.create(Worker.class).withDispatcher("my-blocking-dispatcher"));
worker.tell(job, self());


Yes, that’s the bigger part of it, read above docs to understand why.

-- 
Cheers,
Konrad 'ktoso ' Malawski
Akka  @ Lightbend 

-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


Re: [akka-user] Cancel Actor job

2018-01-25 Thread Richard Gong
Actors may not be that cheap. I tried to create a worker actor every time 
the master actor received a start job message and found out that it created 
a new thread to run it. Seems to me that the default dispatcher is an 
unbounded thread pool. It doesn't look good to me -- it falls back to the 
thread per request model???

Since the jobs are CPU intensive, I have to control the number of 
concurrent jobs. Here's an update of the code. It works to me. Just want to 
clarify if this a right way to do.

// When master actor receive startJob message, creates a new worker actor 
with a predefined dispatcher with a fixed thread pool to control the number 
of concurrent jobs.
ActorRef worker = 
getContext().actorOf(Props.create(Worker.class).withDispatcher("my-blocking-dispatcher"));
worker.tell(job, self());



-- 
>>  Read the docs: http://akka.io/docs/
>>  Check the FAQ: 
>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>  Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.


Re: [akka-user] Cancel Actor job

2018-01-25 Thread Konrad Malawski
Actors are cheap so you can start one actor (make them child actors if some
actor that manages the work things), so then you don’t have that
multiplexing issue at all.

Remember to put the blocking things onto a dedicated thread pool dispatcher
— read docs about dealing with blocking to see how

-- 
Konrad Malawski

On January 26, 2018 at 3:56:05, Richard Gong (gong...@gmail.com) wrote:

> I'm newbie in Akka. Here's one thing I'm trying to achieve, a simple job
> manager. There are jobs involving SQL queries and CPU intended computation.
> The jobs should be cancellable while they're running. Here's my code to
> simulate it. The worker actor sends "continue" message to itself if no
> cancellation signal. However, the master actor may sends another job to it
> while the current job is still running. Question is how to guarantee the
> jobs don't step on each other? This may not be the way Actors do. Any
> suggestions are very welcome.
>
> import java.io.IOException;
>
> import akka.actor.AbstractActor;
> import akka.actor.ActorRef;
> import akka.actor.ActorSystem;
> import akka.actor.Props;
> import akka.routing.RoundRobinPool;
>
> class StartJob {
> private final String id;
> private final int time;
>
> public StartJob(String id, int time) {
> super();
> this.id = id;
> this.time = time;
> }
>
> public String getId() {
> return id;
> }
>
> public int getTime() {
> return time;
> }
>
> }
>
> class CancelJob {
> private final String id;
>
> public CancelJob(String id) {
> super();
> this.id = id;
> }
>
> public String getId() {
> return id;
> }
>
> }
>
> class Worker extends AbstractActor {
>
> private String jobId;
> private boolean cancelling;
> private int time;
> private int i;
>
> @Override
> public Receive createReceive() {
> return receiveBuilder().match(StartJob.class, job -> {
> jobId = job.getId();
> cancelling = false;
> i = 0;
> time = job.getTime();
> System.out.printf("Worker %s starts with id=%s\n", self(),
> job.getId());
> self().tell("continue", self());
> }).matchEquals("continue", p -> {
> if (cancelling) {
> System.out.printf("Worker %s cancelled.\n", jobId);
> return;
> }
> System.out.printf("Worker %s progress %.0f%%\n", jobId, i *
> 100.0 / time);
> Thread.sleep(1000); // simulate SQL query
> for (int j=0; j<100; j++); // simulate CPU intended job
> i++;
> if (i < time) {
> self().tell("continue", self());
> } else {
> System.out.printf("Worker %s finished.\n", self());
> }
> }).match(CancelJob.class, job -> job.getId().equals(jobId), job ->
> {
> System.out.printf("Worker %s cancelling.\n", job.getId());
> cancelling = true;
> }).build();
> }
>
> }
>
> class Master extends AbstractActor {
>
> private final ActorRef workerRouter;
>
> public Master(int numWorkers) {
> workerRouter =
> this.getContext().actorOf(Props.create(Worker.class).withRouter(new
> RoundRobinPool(numWorkers)),
> "workerRouter");
> }
>
> @Override
> public Receive createReceive() {
> return receiveBuilder().match(StartJob.class, job -> {
> System.out.printf("Submitting job %s\n", job.getId());
> workerRouter.tell(job, self());
> }).match(CancelJob.class, job -> {
> System.out.printf("Cancelling job %s\n", job.getId());
> getContext().actorSelection("workerRouter/*").tell(job,
> self());
> }).build();
> }
> }
>
> public class AnalyticsApp {
> public static void main(String[] args) throws IOException,
> InterruptedException {
> ActorSystem system = ActorSystem.create("JobManager");
>
> ActorRef masterRef = system.actorOf(Props.create(Master.class, 2),
> "job-manager");
> System.out.println("Master: " + masterRef);
>
> for (int i = 0; i < 4; i++) {
> masterRef.tell(new StartJob(Integer.toString(i), 5),
> ActorRef.noSender());
> }
>
> Thread.sleep(1000);
> masterRef.tell(new CancelJob("1"), ActorRef.noSender());
>
> System.out.println(">>> Press ENTER to exit <<<");
> try {
> System.in.read();
> } finally {
> system.terminate();
> }
>
> }
> }
>
> --
> >> Read the docs: http://akka.io/docs/
> >> Check the FAQ:
> http://doc.akka.io/docs/akka/current/additional/faq.html
> >> Search the archives: https://groups.google.com/group/akka-user
> ---
> You received this message because you are subscribed to the Google Groups
> "Akka User List" group.
> To unsubscribe from this group and stop receiving emails from it, sen