Top Banner
Introduce Concurrency Threading in Servers
29

Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

Jan 20, 2018

Download

Documents

Stephanie Holt

Thread Pool Thread pool –Keep a fixed size pool of N threads ready –Accept request and Retrieve a thread from pool to process request Return thread to pool after processing has finished –Result N requests can be processed in parallel Bærbak Christensen3
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

Introduce Concurrency

Threading in Servers

Page 2: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

A Recurring Theme• Concurrent programming is not for the weak

hearted!– Deadlocks, blocked threads, critical regions, …

• Tactics and Patterns– Ressource pool– Introduce Concurrency

CS@AU Henrik Bærbak Christensen 2

Page 3: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

Thread Pool• Thread pool

– Keep a fixed size pool of N threads ready– Accept request and

• Retrieve a thread from pool to process request• Return thread to pool after processing has finished

– Result• N requests can be processed in parallel

CS@AU Henrik Bærbak Christensen 3

Page 4: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

Why Not…• Multi-threaded

– Accept request and• Spawn a new thread• Terminate thread after processing has finished

• Why is this a lesser solution?– Performance (time/space)– Availability

CS@AU Henrik Bærbak Christensen 4

Page 5: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

Java Support• ExecutorService

• Hint: Jenkov has nice tutorial– SocketReactor is modeled over his single-thread srv.

CS@AU Henrik Bærbak Christensen 5

Page 6: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

In Action…

CS@AU Henrik Bærbak Christensen 6

Page 7: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

But…

CS@AU Henrik Bærbak Christensen 7

Page 8: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

Parallel Execution• The nasty-ness of shared resources hits us…

• Let us analyze the flow in SkyCave

– Consider that a max peak we have 15 concurrent threads commited to executing requests from 15 clients

• Can some of these 15 requests represent a request from the same player?

• Some may include ‘login’s?CS@AU Henrik Bærbak Christensen 8

Page 9: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

In Reactor• s = serverSocket.accept()

– readAndDispatch(s)

CS@AU Henrik Bærbak Christensen 9

Any problems?

Page 10: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

In Invoker

CS@AU Henrik Bærbak Christensen 10

Page 11: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

In (Player) Dispatcher

CS@AU Henrik Bærbak Christensen 11

Page 12: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

Ex: digRoom

CS@AU Henrik Bærbak Christensen 12

Page 13: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

MongoDB

• Phew…– Seems we are safe there…

CS@AU Henrik Bærbak Christensen 13

Page 14: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

Java Concurrency

Super fast reiteration

CS@AU Henrik Bærbak Christensen 14

Page 15: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

Monitor• Add ‘syncronized’ to a method

– Becomes critical region• Only one thread allowed to execute at a time

– Multiple threads wait in queue for execution• If several threads call the method at the same time• The ‘queue’ controlling object is the object of the method

– i.e. there is a ‘per object’ queue

– If the method freeze• All queued threads does as well!

CS@AU Henrik Bærbak Christensen 15

Page 16: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

ReentrantLock• You can declare a specific ‘queue/lock’ object

and synchronize on that– More tedios, but much finer control

• Locking across a set of objects– Allows timeout! Avoid ‘blocked threads’

CS@AU Henrik Bærbak Christensen 16

Page 17: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

Idiom

• Alternative: tryLock(timeout)

CS@AU Henrik Bærbak Christensen 17

lock.lock();try { // critical region} finally { lock.unlock();}

Page 18: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

Tutorials…• Jenkov’s tutorials appear very nice…

CS@AU Henrik Bærbak Christensen 18

Page 19: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

Our Tool Stack

Help! Help!

CS@AU Henrik Bærbak Christensen 19

Page 20: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

Blocked Threads!• Load Generator, my faulty first version

– N players execute M sets of operations• Output

CS@AU Henrik Bærbak Christensen 20

Page 21: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

How to diagnose?• ‘jvisualvm’

CS@AU Henrik Bærbak Christensen 21

Page 22: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

Thread Dump• Pick VM, Threads Pane, Click Thead Dump

CS@AU Henrik Bærbak Christensen 22

Page 23: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

Review Dump• Locate the blocking thread, see stack trace !

CS@AU Henrik Bærbak Christensen 23

Page 24: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

Monitoring Performance• Started ‘load.cave’

CS@AU Henrik Bærbak Christensen 24

Page 25: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

More Load• Started SkyCave app server on small machine

CS@AU Henrik Bærbak Christensen 25

Page 26: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

More Load• Started 1 x load.cave of 100 players each

CS@AU Henrik Bærbak Christensen 26

Page 27: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

More Load• Started 15 x load.cave of 100 players each

CS@AU Henrik Bærbak Christensen 27

One server,Approx 13.100 req/s

Page 28: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

The ‘Lyon Airport’ Test• Karibu System: Daemon died from exhausted memory

CS@AU Henrik Bærbak Christensen 28

Page 29: Introduce Concurrency Threading in Servers. A Recurring Theme Concurrent programming is not for the…

Going Further

CS@AU Henrik Bærbak Christensen 29