sequential concurrency ... what ???

31
Jitendra Chittoda, Member Development Department @ ION Trading India Pvt. Ltd. Co-leader Delhi and NCR Java User Group (DaNJUG) Email: [email protected] Twitter: @JChittoda Blog: http://chittoda.com Sequential Concurrency…. WHAT ??? 08 May 2013 1 JavaOne India 2013 : COM1240

Upload: jitendra-chittoda

Post on 27-May-2015

1.888 views

Category:

Technology


0 download

DESCRIPTION

Using the ThreadPool, we cannot ensure the execution order of the tasks. There are few situations as per the industry needs, where we need to ensure the task execution order. StripedExecutorService class we are developing, would ensure the task execution order. Example: On an Exchange server we have many products on which users can trade. Transactions on a single product should be performed in FIFO order as per the business needs, and there are many products of such type. So along with maintaining the sequential order of execution of tasks for A product, we are also processing tasks sequentially for other product B as well concurrently. We would be showing Heinz Kabutz implementation.

TRANSCRIPT

Page 1: Sequential Concurrency ... WHAT ???

Jitendra Chittoda, Member Development Department @ ION Trading India Pvt. Ltd.

Co-leader Delhi and NCR Java User Group (DaNJUG)Email: [email protected]

Twitter: @JChittodaBlog: http://chittoda.com

Sequential Concurrency…. WHAT ???

08 May 20131

JavaOne India 2013 : COM1240

Page 2: Sequential Concurrency ... WHAT ???

Delhi & NCR JUG (DaNJUG)

08 May 20132

30+ MembersLooking for more active members

Meetup.com, GroupsIf your friends are in Delhi NCR, please

spread the word

Page 3: Sequential Concurrency ... WHAT ???

AgendaWhat is ThreadPoolBenefits of using ThreadPoolWhat we can’t achieve with ThreadPoolWhy & Where ?Sequential Concurrency FundamentalsSerialExecutorStripedExecutorService DesignFeaturesCurrent State

08 May 20133

Page 4: Sequential Concurrency ... WHAT ???

ThreadPool

08 May 20134

Page 5: Sequential Concurrency ... WHAT ???

Benefits of ThreadPool

08 May 20135

Pre-initialized pool of threadsReusable threadsConfigurable

Page 6: Sequential Concurrency ... WHAT ???

What we can’t achieve with ThreadPool

08 May 20136

As per the business requirement, sometimes you may want to process Tasks in specific order.

Tasks execution order is not maintained by the normal ThreadPools

Processing Not OrderedProcessing of a Task is not ordered, you can

say its uncertain.

Page 7: Sequential Concurrency ... WHAT ???

08 May 20137

Page 8: Sequential Concurrency ... WHAT ???

Why? Industry Examples

08 May 20138

Exchange tradingProcessing of trades on an instrument.

Train ReservationsTicket booking requests for a train must be

processed in FIFO order.Multi Tenancy

Each tenant want some specific execution to be ordered

Page 9: Sequential Concurrency ... WHAT ???

Where ?

08 May 20139

First saw in 2007 then in 2010 and in 2011November 2011: Started writing my own

implementation, published a blog on http://chittoda.com

November 2012:http://

www.javaspecialists.eu/archive/Issue206.htmlHeinz got 5 different implementations after

this issue.

Page 10: Sequential Concurrency ... WHAT ???

Sequential Concurrency Fundamentals

08 May 201310

• Identification

•Maintaining Order•Sequential Processing

•No Dependency

Page 11: Sequential Concurrency ... WHAT ???

Sequential Concurrency Fundamentals

08 May 201311

Identification• Maintain the sequence based on some

Stripe or Key• Stripe: An object which is common among

the Tasks those needs to be processed in sequence.

Page 12: Sequential Concurrency ... WHAT ???

Sequential Concurrency Fundamentals

08 May 201312

Maintaining Order• Maintain the sequential ordering tasks• FIFO Order

Page 13: Sequential Concurrency ... WHAT ???

Sequential Concurrency Fundamentals

08 May 201313

Sequential Processing Process tasks of a Stripe in sequence To maintain this we assign one and only one Thread to

a Queue.

Page 14: Sequential Concurrency ... WHAT ???

Sequential Concurrency Fundamentals

08 May 201314

No Dependency Tasks of one Stripe-X should not be dependent on

Tasks of another Stripe-Y

Page 15: Sequential Concurrency ... WHAT ???

Sequential Concurrency

08 May 201315

12 132

Pool of Threads

Queue of Stripe

StripedExecutorService

T-1

T-2

T-3

Page 16: Sequential Concurrency ... WHAT ???

Executor JavaDoc

08 May 201316

• Many Executor implementations impose some sort of limitation on how and when tasks are scheduled. The executor below serializes the submission of tasks to a second executor, illustrating a composite executor.

class SerialExecutor implements Executor { Queue<Runnable> tasks = new ArrayDeque<>(); Executor executor; Runnable active; SerialExecutor(Executor executor) { this.executor = executor; }

Page 17: Sequential Concurrency ... WHAT ???

SerialExecutor

08 May 201317

public void execute(final Runnable r) { tasks.offer(new Runnable() { public void run() { try { r.run(); } finally { scheduleNext(); } } }); if (active == null) { scheduleNext(); }}

Page 18: Sequential Concurrency ... WHAT ???

SerialExecutor

08 May 201318

protected void scheduleNext() { if ((active = tasks.poll()) != null) { executor.execute(active); } }

Page 19: Sequential Concurrency ... WHAT ???

Pool of SerialExecutor

08 May 201319

SerialExecutor maintain the sequence of the tasks assigned to it and process them in sequence. Using single queue.

BUT we need pool of SerialExecutor, so that we can concurrently execute tasks of two or more Striped Queues

Page 20: Sequential Concurrency ... WHAT ???

Implementation Details

08 May 201320

1) Identification• Stripe: We are using Object as Stripe, you

can define your own.

2) Maintain Order• With separate Queue based on Stripe.• SerialExecutor is maintaining a Queue

3) Sequential Processing• Handled by SerialExecutor

4) No Dependency• Handled by SerialExecutor

Page 21: Sequential Concurrency ... WHAT ???

Design

08 May 201321

Page 22: Sequential Concurrency ... WHAT ???

08 May 201322

Page 23: Sequential Concurrency ... WHAT ???

08 May 201323

Page 24: Sequential Concurrency ... WHAT ???

Inside StripedExecutorService

08 May 201324

class SerialExecutor implements Executor { private BlockingQueue<Runnable> tasks = new LinkedBlockingQueue<>();

private Runnable active; private final Object stripe;

private SerialExecutor(Object stripe) { this.stripe = stripe; }

Page 25: Sequential Concurrency ... WHAT ???

08 May 201325

public void execute(final Runnable r) { lock.lock(); try { tasks.add(new Runnable() { public void run() { try { r.run(); } finally { scheduleNext(); } } }); if (active == null){ scheduleNext(); } } finally { lock.unlock(); }}

Page 26: Sequential Concurrency ... WHAT ???

Inside StripedExecutorService

08 May 201326

private void scheduleNext() { lock.lock(); try { if ((active = tasks.poll()) != null) { executor.execute(active); terminating.signalAll(); } else { removeEmptySerialExecutor(stripe, this); } } finally { lock.unlock(); }}

Page 27: Sequential Concurrency ... WHAT ???

08 May 201327

private final ExecutorService executor;private final Map<Object, SerialExecutor> executors = new IdentityHashMap<>();

public void execute(Runnable command) { lock.lock(); try { Object stripe = getStripe(command); if (stripe != null) { SerialExecutor ser_exec= executors.get(stripe); if (ser_exec == null) { executors.put(stripe, ser_exec= new SerialExecutor(stripe)); } ser_exec.execute(command); } else { executor.execute(command); } } finally { lock.unlock(); }}

Page 28: Sequential Concurrency ... WHAT ???

Features

08 May 201328

Non striped tasks can also be handled with SES

Queues gets created when requiredNo memory leaks in terms of the Queues.

Queues gets removed once all tasks are finished.

Page 29: Sequential Concurrency ... WHAT ???

Current State

08 May 201329

Work In ProgressSeveral other implementations available,

but no one passes the tests written for StripedExecutorService

Performance

Page 30: Sequential Concurrency ... WHAT ???

References

08 May 201330

Delhi & NCR JUGhttps://groups.google.com/d/forum/dncrjug

GitHub : https

://github.com/kabutz/striped-executor-serviceDr. Heinz Kabutz Newsletter

http://www.javaspecialists.eu/archive/Issue206.html

http://www.javaspecialists.eu/Blog

http://chittoda.com

Page 31: Sequential Concurrency ... WHAT ???

08 May 201331