Concurrent Programming with Ruby and Tuple Spaces

  • Upload
    luc

  • View
    628

  • Download
    4

Embed Size (px)

DESCRIPTION

Ruby threads are limited due to the Global Interpreter Lock. Therefore, the best way to do parallel computing with Ruby is to use multiple processes but how do you get these processes to communicate?This session will provide some strategies for handling multi-process communication in Ruby, with a focus on the use of TupleSpaces. A TupleSpace provides a repository of tuples that can be accessed concurrently to implement a Blackboard system. Ruby ships with a built-in implementation of a TupleSpace with the Rinda library.During the session, Luc will demonstrate how to use Rinda and will highlight other libraries/projects that facilitate interprocess communication and parallel computing in Ruby.

Citation preview

ConcurrentProgramming withRubyandTupleSpaces

LucCastera Founder/messagepub.com

AFundamentalTurnTowardConcurrencyinSoftware Themajorprocessormanufacturersandarchitectures haverunoutofroomwithmostoftheirtraditional approachestoboostingCPUperformance.Insteadof drivingclockspeedsandsstraightlineinstruction throughputeverhigher,theyareinsteadturningenmasse tohyperthreadingandmulticorearchitectures.[]And thatputsusatafundamentalturningpointinsoftware development,atleastforthenextfewyears... HerbSutterMarch2005Source:http://www.gotw.ca/publications/concurrencyddj.htm

TheFreeLunchisOver:

Outline1.TheproblemwithRubyThreads 2.MultipleRubyProcesses 3.InterprocessCommunication withTupleSpaces

PART1

TheProblemWithThreadsAcloserlookattheRubythreadingmodel

3TypesofThreadingModels:

1:N 1:1 M:N

3TypesofThreadingModels:

1:N 1:1 M:NKernelThreads UserSpaceThreads

1:NGreenThreadsOnekernelthreadforNuserthreads akalightweightthreads

10 ms

10 ms

10 ms

10 ms

10 ms

10 ms

10 ms

10 ms

RU BY 1. 8

ProsandConsPros:Threadcreation,execution,andcleanuparecheap Lotsofthreadscanbecreated

Cons:Notreallyparallelbecausekernelschedulerdoesn't knowaboutthreadsandcan'tschedulethemacross CPUsortakeadvantageofSMP BlockingI/OoperationcanblockallgreenthreadsExample:CExtension Example:mysqlgem(solution:NeverBlockmysqlplus)

blocking

1:1NativeThreads1kernelthreadforeachuserthread

ProsandConsPros:ThreadscanexecuteondifferentCPUs(trulyparallel) Threadsdonotblockeachother

Cons:SetupOverhead Lowlimitonnumberofthreads Linuxkernelbugwithlotsofthreads

RU BY 1 . 9

Ilied.

GlobalInterpreterLockAGlobalInterpreterLock(GIL)isamutual exclusionlockheldbyaprogramminglanguage interpreterthreadtoavoidsharingcodethatis notthreadsafewithotherthreads.Thereis alwaysoneGILforoneinterpreterprocess. UsageofaGlobalInterpreterLockinalanguage effectivelylimitsconcurrencyofasingle interpreterprocesswithmultiplethreadsthereis noorverylittleincreaseinspeedwhenrunning

Aperson(maleorfemale)whointentionallyor unintentionallystopstheprogressoftwoothers gettingtheirgameon.

Concurrencyisa mythinRubyIlyaGrigorik

Unlessyouare usingJRuby.

AnoteonFibersRuby1.9introducesfibers. Fibersaregreenthreads,butscheduling mustbedonebytheprogrammerandnot theVM. Fasterandcheaperthennativethreads. ImplementedforRuby1.8byAmanGupta. LearnMore:http://tinyurl.com/rubyfibers http://allthing.net/fibers

M:NHybridModelM kernel threads for N user threads best of both worlds

ProsandConsPros: TakeadvantageofmultipleCPUs Notallthreadsareblockedbyblocking systemcalls Cheapcreation,execution,andcleanup Cons: Needschedulerinuserlandandkernelto

TheOtherProblemwithThreads

Writingmultithreadedcodeisreally, reallyhard.Anditishardbecauseof SharedMemory.JimWeirich

http://rubyconf2008.confreaks.com/what-all-rubyist-should-know-about-threads.html

MultiThreaded CodeisHard

+Concurrencyisa myth

= FAIL!

Stopthinkinginthreads Designyourapplication tousemultipleprocesses

PART2

Multiple Ruby Processes

ProsandConsPros: Nolongersharingmemory TakeadvantageofmultipleCPUs (Performance) Notallthreadsareblockedbyblocking systemcalls. Scalability FaultTolerance Cons:

Monitoring FaultTolerance Starting/Stopping Latency

butwewillfocuson...

Howdotheprocessescommunicate?

OptionsDRB Sockets Queues RabbitMQ ActiveMQ KeyValueDatabases Redis

Examples

Rails+Mongrel/ThinClusterofapplicationservers(Mongrel,Thin...) Communicationbetweenprocessesisdoneviathe database.

NaniteAselfassemblingfabricofRubydaemons http://github.com/ezmobius/nanite UsesRabbitMQ/AMQPforIPC

RevactorUsestheactormodel Actorsarekindalikethreads,withmessaging bakedin. EachActorhasamailbox. It'slikecodingerlanginRuby. MessagesarepassedbetweenactorsusingTCP sockets. GoodDocumentation

JournetaJournetaisadirtsimplelibraryforpeer discoveryandmessagepassingbetweenRuby applicationsonaLAN UsesUDPSocketsforIPC UsesthefuckedupRubysocketAPIfromtheirRDOC

Demo(?)

PART3

TupleSpacesInterprocessCommunicationwithTupleSpaces

Atuplespaceprovidesa repositoryoftuples thatcanbeaccessed concurrently.

TheBlackboardMetaphor[:a dd, 1, 2] [:a dd, 60 , 5] [:resu lt, 79]

[:s ubtra ct, 10, 2 ] [:s ear ch, linda ] [:tok en] [:s ave, 78 64 ] [:wh ere_ is, :wa ldo

TheBlackboardMetaphor[:a dd, ni l, ni l] [:a dd, 1, 2] [:a dd, 60 , 5] [:resu lt, 79]

[:s ubtra ct, 10, 2 ] [:s ear ch, linda ] [:tok en] [:s ave, 78 64 ] [:wh ere_ is, :wa ldo

TheBlackboardMetaphor[:a dd, 1, 2] [:a dd, 60 , 5] [:resu lt, 79]

[:s ubtra ct, 10, 2 ] [:s ear ch, linda ] [:tok en] [ni l] [:s ave, 78 64 ] [:wh ere_ is, :wa ldo

TheBlackboardMetaphor[:a dd, 1, 2] [:a dd, 60 , 5] [:resu lt, 79]

[:s ubtra ct, 10, 2 ] [:s ear ch, linda ] [:tok en] [:s ave, 78 64 ] [:wh er e_i s, :waldo] [:wh ere_ is, :wa ldo

AboutTupleSpacesFirstimplementationwasLinda. LindawasdevelopedbyDavidGelernterand NicholasCarrieroatYaleUniversity. Implementationsexistsformostlanguages. TheRubyimplementationisRinda. Rindaisabuiltinlibrary,sononeedtoinstall.

5BasicOperationsread read_all write take notify

5BasicOperationsread read_all write take notify

Readstuple,butdoesnotremoveit. Blocking,bydefault,buttakesan additionaltimeoutargument.

5BasicOperationsread read_all write take notify

Returnsalltuplesmatchingtuple. Doesnotremovethefoundtuples.

5BasicOperationsread read_all write take notify

AddsTuple Takesanoptionaltimeoutparameter

5BasicOperationsread read_all write take notify

AtomicRead+Delete Blocking,bydefault,buttakesan additionaltimeoutargument.

5BasicOperationsread read_all write take notify

Registersfornotificationsofevents: Write Take Delete

KeyFeaturesSpacesaresharedSpacehandlesdetailsofconcurrentaccess

SpacesarepersistentIfagentprocessdies,dataisstillinspace However,ifspaceprocessdies,dataislost(?)

SpacesareassociativeAssociativelookupsratherthanmemorylocationor identifier

Spacesaretransactionallysecure

ARindatuplecanbean arrayorahash

ARindatuplecanbean arrayorahash

( But let's stick with the array, I like that better! )

StartaTupleSpaceonport1234

Clients/Agents

DEMORinda

RingServer

ThisisalsoaTupleSpace

SPOF

Rindaisnotpersistent...Ifitcrasheswhileyouhavetuplesin thespace,youlosethemall.

OnlyRuby

IntroducingBlackboardTupleSpaceimplementationontopofRedis Persistent Redisisareallyfastkeyvaluedatabase. Likememcachedbutdataisnotvolatile. SameAPIPlug&Play Fornow,onlysupports:take,read,andwrite http://github.com/dambalah/blackboard

Server

Juststarttheredisserver: $ redis-server

Client/Agents

Blackboard

DEMO

BlackboardBenchmarksWrite (1000) Take (500) Read (500) Rinda 0.042749 0.082744 0.020098 Blackboard 0.253068 15.844250 20.098478

Blackboard:FutureMovefromRedistoacustombasedErlang blackboardimplementation. IwouldlikethatErlangimplementationtobe easilyusedfromotherprogramminglanguages also. Soit'sreallytwoprojects:Blackboardinerlang Rubylibrarytotalktoblackboardinerlang

Thankyou!

LucCastera Founder/messagepub.com

Questions?Feedback? [email protected] www.speakerrate.com

LucCastera Founder/messagepub.com

Resources/ReferencesPart1:ThreadingModels http://timetobleed.com/threadingmodelssomany differentwaystogetstuffdone/ http://envycasts.com/products/scalingruby http://www.infoq.com/news/2007/05/rubythreading futures http://thebogles.com/blog/2006/11/rubythreading/ http://spec.rubydoc.org/wiki/Ruby_Threading http://www.bitwiese.de/2007/09/onprocessesand threads.html http://www.igvita.com/2008/11/13/concurrencyisa

Resources/ReferencesPart2:MultipleProcesses http://github.com/ezmobius/nanite http://erlang.org/ http://www.rabbitmq.com/ http://code.google.com/p/redis/ http://revactor.org/ http://journeta.rubyforge.org/http://home.mindspring.com/~eric_rollins/ParallelRuby.html

Resources/ReferencesPart3:TupleSpaces http://c2.com/cgi/wiki?TupleSpace http://en.wikipedia.org/wiki/Tuplespace http://www.julianbrowne.com/article/viewer/spacebased architectureexample http://www.rubyagent.com/ http://segment7.net/projects/ruby/drb/ http://segment7.net/projects/ruby/drb/rinda/ringserver.html JavaSpacesPrinciples,Patterns,andPracticeFreeman,Hupfer,et.al. http://www.rubydoc.org/stdlib/libdoc/rinda/rdoc/index.html

ThingsIwishIhadtimetospendonMPIandRubyMPI http://github.com/abedra/mpiruby/tree/master Rubyforkoff: http://tinyurl.com/forkoff