Running a Cron job on Hazelcast DurableExecutorService

679 views
Skip to first unread message

riny...@gmail.com

unread,
Mar 14, 2018, 6:09:24 AM3/14/18
to Hazelcast
I am trying to run cron schedules on DurableExecutorService on Hazelcast. My idea is that if one node goes down with its schedule, other nod having backup can pickup and resume the CRON. This is what I am doing

String cron = "0/5 * * * * *";

 config
.setInstanceName(name);
 config
.getDurableExecutorConfig("exec").setCapacity(200).setDurability(2).setPoolSize(8);

 
HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
 
DurableExecutorService executorService = instance.getDurableExecutorService("exec");

 executorService
.executeOnKeyOwner(new EchoTask(name, cron), name);

Then I use a Spring CRON scheduler to actually run the CRON job.

public void run() { ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler(); scheduler.initialize(); scheduler.schedule(new Runnable() { @Override public void run() { System.out.println("Executing" + msg); } }, new CronTrigger(cronExpression)); }

Now, I run this twice. So in effect, there are 2 instances running.

  1. Executinginstance-1 on one instance
  2. Executinginstance-2 on another instance

Now, My understanding is that if I go and kill one instance, lets say 1, then the CRON of node1 should migrate to node2. However, this is not happening.

I do get this message though when I kill the node


INFO: [192.168.122.1]:5707 [dev] [3.9.3] Committing/rolling-back alive transactions of Member [192.168.122.1]:5709 - 26ed879b-8ce5-4d58-832c-28d2df3f7f87, UUID: 26ed879b-8ce5-4d58-832c-28d2df3f7f87


I am sure, I am missing something here. Can someone pls guide?

Thomas Kountis

unread,
Mar 14, 2018, 6:16:04 AM3/14/18
to haze...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Hazelcast" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hazelcast+unsubscribe@googlegroups.com.
To post to this group, send email to haze...@googlegroups.com.
Visit this group at https://groups.google.com/group/hazelcast.
To view this discussion on the web visit https://groups.google.com/d/msgid/hazelcast/9182a5ab-581a-41bd-a330-5d79a22a234f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jaromir Hamala

unread,
Mar 14, 2018, 6:21:09 AM3/14/18
to haze...@googlegroups.com
Hi,

I am not sure if I am fully following. You execute a task, this task uses a spring scheduler to schedule another task and finish. At this point Hazelcast see the task as completed. 
Have you seen Hazelcast Scheduled Executor Service? It does not support cron expressions (yet), but perhaps it's good enough for you. 

Cheers,
Jaromir

--
You received this message because you are subscribed to the Google Groups "Hazelcast" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hazelcast+...@googlegroups.com.
Message has been deleted

Yogesh Singh

unread,
Mar 14, 2018, 7:22:33 AM3/14/18
to Hazelcast
I want a fault tolerant CRON scheduler on Hazelcast. Since there is no native support, I am using Spring Scheduler.
As I understand, since this is a CRON, the task is still ongoing and not finished (I have scheduled it for every 5 seconds).
So If I terminate this in middle, the other node should resume the CRON.
Is this understanding flawed? If yes, How can I achieve this.
I was thinking wrapping a Spring CRON inside DurableExecutor should have worked.

Yogesh Singh

unread,
Mar 14, 2018, 7:25:28 AM3/14/18
to Hazelcast
I am pasting code again,

Yogesh Singh

unread,
Mar 14, 2018, 7:32:47 AM3/14/18
to Hazelcast
Yes, But I want a CRON scheduler.
Is there any issue in my approach?
I was thinking that wrapping a Spring CRON task inside DurableExecutorService should have worked.

Yogesh Singh

unread,
Mar 14, 2018, 10:17:21 AM3/14/18
to Hazelcast

Edit 1: I verified that for normal tasks this behavior works, but it does not for some reason work for Spring CRON

Edit 2 One doubt I have is, that ThreadPoolTaskScheduler is not serializable for some reason.

Failed to serialize org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler

I suspect, that why it is not getting persisted on the ringbuffer. Any idea how I can make it Serializable. I checked the code and ThreadPoolTaskScheduler already implements Serializable

Thomas Kountis

unread,
Mar 14, 2018, 7:46:44 PM3/14/18
to haze...@googlegroups.com
Hi,

As Jaromir said above, you are basically executing this line in your runnable `scheduler.schedule(...)`. Once this instruction is finished, your Spring scheduler is working in the background, but from the DurableExecutor perspective the task is finished.
Once a task is finished, it gets replaced by its result, in your case there is no result, so `null`. If you kill that owner member, the backup is promoted, but there is no task anymore, since we replaced it in the previous step. The backup knows, that the task completed.
Does this make sense? 

I can't thing of any non-abusive way to accomplish what you are looking for, except from maybe a naive `CronScheduler`inside the`IScheduledExecutor`.
Imagine a periodic task, that runs every second or so, and it holds a `Map<CronExpression, Runnable>` in it. During its `run` cycle, it checks the expression to assert if there is any runnable ready, and if so, runs it another Executor (Durable or Scheduled). 
Since this is a periodic task, it will work well with out `IScheduledExecutor` and it will be durable upon failures.

Hope that helps! We will be looking in adding native support of cron-expressions in `IScheduledExecutor` in the future.

Thanks

--
You received this message because you are subscribed to the Google Groups "Hazelcast" group.
To unsubscribe from this group and stop receiving emails from it, send an email to hazelcast+unsubscribe@googlegroups.com.
To post to this group, send email to haze...@googlegroups.com.
Visit this group at https://groups.google.com/group/hazelcast.

Yogesh Singh

unread,
Mar 15, 2018, 12:23:04 AM3/15/18
to Hazelcast
Hi Thomas,
Thanks for your reply. I understood why this is not working. 
If it is not too much too ask, could you pls give some code snippet of your proposed approach.
This will help me understand. I actually tried putting Runnable inside Map. But I was getting this error Failed to serialize org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler

I am not sure if I am doing it correctly.



Reply all
Reply to author
Forward
0 new messages