robotoworks / mechanoid

Eclipse plugin providing a set of DSL's for the rapid development of Android apps
58 stars 26 forks source link

Operations parallel execution #244

Closed hannesa2 closed 9 years ago

hannesa2 commented 10 years ago

In my Activity, I start an operation (with Net-REST call) at the same time

operation search(double lat, double lon, int radius) unique (radius)

twice, a short running and a long running. With this, I've two issues:

Therefore I've a general question: Does Mechanoid-Operations have the ability to run parallel at the background with included REST-calls ?

fluxtah commented 10 years ago

Hi hannesa2,

No operations do not run in parallel by design, however if you need to do something in parallel you can always create a second operation service.

hannesa2 commented 10 years ago

Thank you for your fast response ! I've to clarify : "No operations do not run in parallel .. " means "Every operation do run parallel .." , yes ?

operation search(double lat, double lon, int radius) unique (radius)

will run parallel by design ?

Ops.execute(SearchOperation.newIntent(lat, lon, 10);
Ops.execute(SearchOperation.newIntent(lat, lon, 1000);

if not, it's an issue on my side ?

fluxtah commented 10 years ago

Hi Sorry Hannesa,

I meant operations do not run in parallel (by design to avoid race conditions in chatty applications).

If you want to run ops in parallel, create another operation service.

hannesa2 commented 10 years ago

I think about to make it parallel, but for this I've to understand how it works by design. Several services are a way, but I need a kind of 'load balancer' for it.

When I execute this operation at the same time

operation search(int radius) unique (radius)

with different runtime behavior, say more radius has a longer runtime

Ops.execute(SearchOperation.newIntent(10000); //no1
Ops.execute(SearchOperation.newIntent(10);  //no2
Ops.execute(SearchOperation.newIntent(11);  //no3
Ops.execute(SearchOperation.newIntent(10);  //no4
Ops.execute(SearchOperation.newIntent(10);  //no5
Ops.execute(SearchOperation.newIntent(10000);  //no6
Ops.execute(SearchOperation.newIntent(10);  //no7

how are they executed, in which order ? Which one will be skipped ? As I understand

  1. no1,
  2. no2 waits still no1 is finished
  3. no3 waits still no2 is finished, not no1
  4. no4 waits still no3 is finished (?)
  5. no5 is skipped
  6. no6 waits for no4 no5 (?)
  7. no7 waits still no6 is finished

am I right ?

What do you suggest to make all parallel, as I see dynamic Service count doesn't work because of entry in AndroidManifest.xml Make onExecute in an own thread, with an callback for finish, is a good idea ?

fluxtah commented 10 years ago

Hi,

Effectively, under the hood, a Mechanoid OperaitonService delegates responsibility of operation execution to the OperationProcessor

The Processor receives operation requests and puts them into a cue and they are executed First In First Out (FIFO).

When an operation is executing, and then finishes, it then takes the next operation from the queue and executes it, and continues with this pattern until no operations are left, once all operations are executed the service attempts to shut itself down.

So far this pattern has worked well for all our operations, and we have not needed parallelisation.

Operation uniqueness means that, if an operation is unique, and a currently running (or operation waiting in the queue) then the operation will not be added to the queue and the operation will return the ID of the existing operation it matches in the queue.

You can disable this uniqueness with the 'not unique' constraint so all operations you add will be put into the queue.

operation search(int radius) not unique
hannesa2 commented 10 years ago

thanks for the explanation

hannesa2 commented 10 years ago

Hi, does the operation-service runs in UI-thread ?

fluxtah commented 10 years ago

No, that would be bad :)

You can take a look at the code that executes the operations,

https://github.com/robotoworks/mechanoid/blob/master/libs/mechanoid/src/main/java/com/robotoworks/mechanoid/ops/OperationProcessor.java

With a bit of work, it could be possible to introduce an option to parallize operations, but thats not on the roadmap currently.