Concurrent Programming with Ruby and Tuple Spaces
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Concurrent Programming with Ruby and Tuple Spaces

on

  • 16,616 views

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 ...

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.

Statistics

Views

Total Views
16,616
Views on SlideShare
15,418
Embed Views
1,198

Actions

Likes
20
Downloads
281
Comments
0

4 Embeds 1,198

http://dambalah.com 923
http://luccastera.com 125
http://localhost 78
http://www.slideshare.net 72

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Concurrent Programming with Ruby and Tuple Spaces Presentation Transcript

  • 1. Concurrent Programming with Ruby and Tuple Spaces Luc Castera Founder / messagepub.com
  • 2. The Free Lunch is Over: A Fundamental Turn Toward Concurrency in Software Source: http://www.gotw.ca/publications/concurrency-ddj.htm “ The major processor manufacturers and architectures have run out of room with most of their traditional approaches to boosting CPU performance. Instead of driving clock speeds ands straight-line instruction throughput ever higher, they are instead turning en masse to hyperthreading and multicore architectures. […] And that puts us at a fundamental turning point in software development, at least for the next few years...” – Herb Sutter – March 2005
  • 3. Outline 1. The problem with Ruby Threads 2. Multiple Ruby Processes 3. Inter-process Communication with TupleSpaces
  • 4. PART 1 The Problem With Threads A closer look at the Ruby threading model
  • 5. 3 Types of Threading Models: 1 : N 1 : 1 M : N
  • 6. 3 Types of Threading Models: 1 : N 1 : 1 M : N Kernel Threads User-Space Threads
  • 7. 1 : N -> Green Threads One kernel thread for N user threads aka “lightweight threads”
  • 8.  
  • 9. 10 ms
  • 10. 10 ms
  • 11. 10 ms
  • 12. 10 ms
  • 13. 10 ms
  • 14. 10 ms
  • 15. 10 ms
  • 16. 10 ms
  • 17. RUBY 1.8
  • 18. Pros and Cons
    • Pros:
      • Thread creation, execution, and cleanup are cheap
      • 19. Lots of threads can be created
    • Cons:
      • Not really parallel because kernel scheduler doesn't know about threads and can't schedule them across CPUs or take advantage of SMP
      • 20. Blocking I/O operation can block all green threads
        • Example: C Extension
        • 21. Example: mysql gem (solution: NeverBlock mysqlplus)
  • 22. blocking
  • 23. 1 : 1 -> Native Threads 1 kernel thread for each user thread
  • 24.  
  • 25. Pros and Cons
    • Pros:
      • Threads can execute on different CPUs (truly parallel)
      • 26. Threads do not block each other
    • Cons:
      • Setup Overhead
      • 27. Low limit on number of threads
      • 28. Linux kernel bug with lots of threads
  • 29. RUBY 1.9
  • 30. I lied.
  • 31. Global Interpreter Lock
      A Global Interpreter Lock (GIL) is a mutual exclusion lock held by a programming language interpreter thread to avoid sharing code that is not thread-safe with other threads. There is always one GIL for one interpreter process. Usage of a Global Interpreter Lock in a language effectively limits concurrency of a single interpreter process with multiple threads – there is no or very little increase in speed when running the process on a multiprocessor machine. Source: Wikipedia
  • 32.
      A person (male or female) who intentionally or unintentionally stops the progress of two others getting their game on.
  • 33.  
  • 34. “ Concurrency is a myth in Ruby” – Ilya Grigorik
  • 35. Unless you are using JRuby.
  • 36.  
  • 37. A note on Fibers
    • Ruby 1.9 introduces fibers.
    • 38. Fibers are green threads, but scheduling must be done by the programmer and not the VM.
    • 39. Faster and cheaper then native threads.
    • 40. Implemented for Ruby 1.8 by Aman Gupta.
    • 41. Learn More:
      • http://tinyurl.com/rubyfibers
      • 42. http://all-thing.net/fibers
      • 43. http://all-thing.net/fibers-via-continuations
  • 44. M : N -> Hybrid Model M kernel threads for N user threads “ best of both worlds”
  • 45.  
  • 46. Pros and Cons
    • Pros:
      • Take advantage of multiple CPUs
      • 47. Not all threads are blocked by blocking system calls
      • 48. Cheap creation, execution, and cleanup
    • Cons:
      • Need scheduler in userland and kernel to work with each other
      • 49. Green threads doing blocking I/O operations will block all other green threads sharing same kernel thread
      • 50. Difficult to write, maintain, and debug code
  • 51.  
  • 52. “ Writing multi-threaded code is really, really hard. And it is hard because of Shared Memory.” – Jim Weirich The Other Problem with Threads http://rubyconf2008.confreaks.com/what-all-rubyist-should-know-about-threads.html
  • 53. Multi-Threaded Code is Hard + Concurrency is a myth = FAIL!
  • 54. Stop thinking in threads Design your application to use multiple processes
  • 55. PART 2 Multiple Ruby Processes
  • 56.  
  • 57. Pros and Cons
    • Pros:
      • No longer sharing memory
      • 58. Take advantage of multiple CPUs (Performance)
      • 59. Not all threads are blocked by blocking system calls.
      • 60. Scalability
      • 61. Fault-Tolerance
    • Cons:
      • Process creation, execution and cleanup is expensive
      • 62. Uses a lot of memory (loading Ruby VM for every process)
      • 63. Need a way for processes to communicate!
  • 64. Latency Starting/Stopping Fault-Tolerance Monitoring
  • 65. but we will focus on...
  • 66. How do the processes communicate?
  • 67. Options
    • DRB
    • 68. Sockets
    • 69. Queues
      • RabbitMQ
      • 70. ActiveMQ
    • Key-Value Databases
      • Redis
      • 71. Tokyo Cabinet
      • 72. Memcached
    • Relational Databases
    • 73. XMPP
    • 74. TupleSpaces
  • 75. Examples
  • 76. Rails + Mongrel/Thin
    • Cluster of application servers (Mongrel, Thin...)
    • 77. Communication between processes is done via the database.
  • 78. Nanite
    • A self-assembling fabric of Ruby daemons
    • 79. http://github.com/ezmobius/nanite
    • 80. Uses RabbitMQ/AMQP for IPC
  • 81. Revactor
    • Uses the actor model
    • 82. Actors are kinda like threads, with messaging baked-in.
    • 83. Each Actor has a mailbox.
    • 84. It's like coding erlang in Ruby.
    • 85. Messages are passed between actors using TCP sockets.
    • 86. Good Documentation
    • 87. http://revactor.org/
    • 88. “ Erlang provides a sledgehammer for the problems of concurrent programming. But, sometimes you don't need a sledgehammer... just a flyswatter will do.” – Tony Arcieri
    • 89. Discontinued for Reia
  • 90. Journeta
    • Journeta is a dirt simple library for peer discovery and message passing between Ruby applications on a LAN
    • 91. Uses UDP Sockets for IPC
    • 92. “ Uses the fucked up Ruby socket API”
      • -> from their RDOC
    • Demo(?)
  • 93. PART 3 TupleSpaces I nterprocess Communication with TupleSpaces
  • 94.
      A tuple space provides a repository of tuples that can be accessed concurrently.
  • 95. [:add, 1, 2] [:result, 79] [:add, 60, 5] [:token] [:search, “linda”] [:where_is, :waldo [:subtract, 10, 2] [:save, 7864] The Blackboard Metaphor
  • 96. [:add, 1, 2] [:result, 79] [:add, 60, 5] [:token] [:search, “linda”] [:where_is, :waldo [:subtract, 10, 2] [:save, 7864] The Blackboard Metaphor [:add, nil, nil]
  • 97. [:add, 1, 2] [:result, 79] [:add, 60, 5] [:token] [:search, “linda”] [:where_is, :waldo [:subtract, 10, 2] [:save, 7864] The Blackboard Metaphor [nil]
  • 98. [:add, 1, 2] [:result, 79] [:add, 60, 5] [:token] [:search, “linda”] [:where_is, :waldo [:subtract, 10, 2] [:save, 7864] The Blackboard Metaphor [:where_is, :waldo]
  • 99. About Tuple Spaces
    • First implementation was Linda.
    • 100. Linda was developed by David Gelernter and Nicholas Carriero at Yale University.
    • 101. Implementations exists for most languages.
    • 102. The Ruby implementation is Rinda.
    • 103. Rinda is a built-in library, so no need to install.
  • 104. 5 Basic Operations
    • read
    • 105. read_all
    • 106. write
    • 107. take
    • 108. notify
  • 109. 5 Basic Operations
    • read
    • 110. read_all
    • 111. write
    • 112. take
    • 113. notify
    Reads tuple, but does not remove it. Blocking, by default, but takes an additional timeout argument.
  • 114. 5 Basic Operations
    • read
    • 115. read_all
    • 116. write
    • 117. take
    • 118. notify
    Returns all tuples matching tuple. Does not remove the found tuples.
  • 119. 5 Basic Operations
    • read
    • 120. read_all
    • 121. write
    • 122. take
    • 123. notify
    Adds Tuple Takes an optional timeout parameter
  • 124. 5 Basic Operations
    • read
    • 125. read_all
    • 126. write
    • 127. take
    • 128. notify
    Atomic Read + Delete Blocking, by default, but takes an additional timeout argument.
  • 129. 5 Basic Operations
    • read
    • 130. read_all
    • 131. write
    • 132. take
    • 133. notify
    Registers for notifications of events:
    • Write
    • 134. Take
    • 135. Delete
  • 136. Key Features
    • Spaces are shared
      • Space handles details of concurrent access
    • Spaces are persistent
      • If agent process dies, data is still in space
      • 137. However, if space process dies, data is lost (?)
    • Spaces are associative
      • Associative lookups rather than memory location or identifier
    • Spaces are transactionally secure
      • Atomic Operations
    • Spaces allow us to exchange executable content
  • 138.
      A Rinda tuple can be an array or a hash
  • 139.
      A Rinda tuple can be an array or a hash
    ( But let's stick with the array, I like that better! )
  • 140. Start a Tuple Space on port 1234
  • 141. Clients/Agents
  • 142.  
  • 143. DEMO Rinda
  • 144. RingServer
  • 145.  
  • 146.  
  • 147.  
  • 148.  
  • 149.  
  • 150. This is also a TupleSpace
  • 151. SPOF
  • 152. Rinda is not persistent... If it crashes while you have tuples in the space, you lose them all.
  • 153. Only Ruby
  • 154. Introducing Blackboard
    • TupleSpace implementation on top of Redis
      • -> Persistent
    • Redis is a really fast key-value database.
      • Like memcached but data is not volatile.
    • Same API -> Plug & Play
    • 155. For now, only supports: take, read, and write
    • 156. http://github.com/dambalah/blackboard
  • 157. Server Just start the redis-server: $ redis-server
  • 158. Client/Agents
  • 159. DEMO Blackboard
  • 160. Blackboard Benchmarks
  • 161. Blackboard: Future
    • Move from Redis to a custom based Erlang blackboard implementation.
    • 162. I would like that Erlang implementation to be easily used from other programming languages also.
    • 163. So it's really two projects:
      • Blackboard in erlang
      • 164. Ruby-library to talk to blackboard in erlang
  • 165. Thank you! Luc Castera Founder / messagepub.com
  • 166. Questions?Feedback? [email_address] www.speakerrate.com Luc Castera Founder / messagepub.com
  • 167. Resources / References
    • Part 1: Threading Models
      • http://timetobleed.com/threading-models-so-many-different-ways-to-get-stuff-done/
      • 168. http://envycasts.com/products/scaling-ruby
      • 169. http://www.infoq.com/news/2007/05/ruby-threading-futures
      • 170. http://thebogles.com/blog/2006/11/ruby-threading/
      • 171. http://spec.ruby-doc.org/wiki/Ruby_Threading
      • 172. http://www.bitwiese.de/2007/09/on-processes-and-threads.html
      • 173. http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/
      • 174. http://bartoszmilewski.wordpress.com/2008/08/24/threads-dont-scale-processes-do/
      • 175. http://en.wikipedia.org/wiki/Global_Interpreter_Lock
      • 176. http://www.gotw.ca/publications/concurrency-ddj.htm
      • 177. http://tinyurl.com/rubyfibers
  • 178. Resources / References
    • Part 2: Multiple Processes
      • http://github.com/ezmobius/nanite
      • 179. http://erlang.org/
      • 180. http://www.rabbitmq.com/
      • 181. http://code.google.com/p/redis/
      • 182. http://revactor.org/
      • 183. http://journeta.rubyforge.org/
      • 184. http://home.mindspring.com/~eric_rollins/ParallelRuby.html
  • 185. Resources / References
    • Part 3: TupleSpaces
      • http://c2.com/cgi/wiki?TupleSpace
      • 186. http://en.wikipedia.org/wiki/Tuplespace
      • 187. http://www.julianbrowne.com/article/viewer/space-based-architecture-example
      • 188. http://www.rubyagent.com/
      • 189. http://segment7.net/projects/ruby/drb/
      • 190. http://segment7.net/projects/ruby/drb/rinda/ringserver.html
      • 191. JavaSpaces Principles, Patterns, and Practice – Freeman, Hupfer, et. al.
      • 192. http://www.ruby-doc.org/stdlib/libdoc/rinda/rdoc/index.html
  • 193. Things I wish I had time to spend on
    • MPI and Ruby-MPI
      • http://github.com/abedra/mpi-ruby/tree/master
    • Ruby forkoff:
      • http://tinyurl.com/forkoff