Can a Hazelcast ExecutorService be created to exec

2019-07-17 20:49发布

问题:

I'm trying to setup a distributed executor service with hazelcast for my project. Some tasks which will run can only be completed on machines with OS specific utilities. Is there a way to submit a task that will run once on a subset of the cluster? Or to register that it should be used with a specific executor service?

Looking at the API there are a number of options to submit a task, but all the options for submitting to multiple members will run the task on all of those members, not one of them.

I have looked at the javadoc and seen that there are a number of ways to submit Runnable objects to the executor service:

void executeOnMember(Runnable command, Member member) - this only allows me to specify one member, I need to specify a group of members.

void executeOnMembers(Runnable command, Collection<Member> members) - This allows me to specify a collection of members, but runs the task multiple times instead of just once.

void submitToMembers(Runnable task, Collection<Member> members, MultiExecutionCallback callback) - again this specifies a collection of members, but it will run the task multiple times.

回答1:

I'm going to copy the comment as answer, because this question was closed for some time for reasons beyond me.

I would make use of the IExecutorService.executeOnMember. If you want to execute on one of the members of a subset of members, select one member randomly or using some kind of loadbalancing policy (e.g. round robin.. or detect actual load) and then executeOnMember.

You could also wrap the IExecutorService by an Executor decorator that takes care of this:

 class OsSpecificExecutor implements Executor{
     private IExecutorService ex;


     public void execute(Runnable task){
         Set<Member> targetMembers = getYourTargetMembers()
         Member member = random(targetMembers)
         ex.executeOnMember(task, member);
     }
 }

This way you can use it as a normal executor, but underneath you have control on which members + optional load balancing.



回答2:

I would take a different approach than pveentjer, as Random algorithm may at random pick heavily used node and the execution will be delayed. I would submit the job to run on all (or a collection of) members. To actually run a job on one computer only, you can employ atomic integer initially initialized to 0. Each job will atomically increase this atomic integer, but only the job that receives 1 as the result of incrementAndGet() will actually run, other will return immediately with bogus value. Cluster-wide incrementAndGet() is however not instant and will add slight overhead to the job itself.