Concurrent Programming with Ruby and Tuple Spaces

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    6 Favorites

    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
        • 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
        • Blocking I/O operation can block all green threads
          • Example: C Extension
          • Example: mysql gem (solution: NeverBlock mysqlplus)
    19. blocking
    20. 1 : 1 -> Native Threads 1 kernel thread for each user thread
    21.  
    22. Pros and Cons
      • Pros:
        • Threads can execute on different CPUs (truly parallel)
        • Threads do not block each other
      • Cons:
        • Setup Overhead
        • Low limit on number of threads
        • Linux kernel bug with lots of threads
    23. RUBY 1.9
    24. I lied.
    25. 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
      • A person (male or female) who intentionally or unintentionally stops the progress of two others getting their game on.
    26.  
    27. “ Concurrency is a myth in Ruby” – Ilya Grigorik
    28. Unless you are using JRuby.
    29.  
    30. A note on Fibers
      • Ruby 1.9 introduces fibers.
      • Fibers are green threads, but scheduling must be done by the programmer and not the VM.
      • Faster and cheaper then native threads.
      • Implemented for Ruby 1.8 by Aman Gupta.
      • Learn More:
        • http://tinyurl.com/rubyfibers
        • http://all-thing.net/fibers
        • http://all-thing.net/fibers-via-continuations
    31. M : N -> Hybrid Model M kernel threads for N user threads “ best of both worlds”
    32.  
    33. Pros and Cons
      • Pros:
        • Take advantage of multiple CPUs
        • Not all threads are blocked by blocking system calls
        • Cheap creation, execution, and cleanup
      • Cons:
        • Need scheduler in userland and kernel to work with each other
        • Green threads doing blocking I/O operations will block all other green threads sharing same kernel thread
        • Difficult to write, maintain, and debug code
    34.  
    35. “ 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
    36. Multi-Threaded Code is Hard + Concurrency is a myth = FAIL!
    37. Stop thinking in threads Design your application to use multiple processes
    38. PART 2 Multiple Ruby Processes
    39.  
    40. Pros and Cons
      • Pros:
        • No longer sharing memory
        • Take advantage of multiple CPUs (Performance)
        • Not all threads are blocked by blocking system calls.
        • Scalability
        • Fault-Tolerance
      • Cons:
        • Process creation, execution and cleanup is expensive
        • Uses a lot of memory (loading Ruby VM for every process)
        • Need a way for processes to communicate!
    41. Latency Starting/Stopping Fault-Tolerance Monitoring
    42. but we will focus on...
    43. How do the processes communicate?
    44. Options
      • DRB
      • Sockets
      • Queues
        • RabbitMQ
        • ActiveMQ
      • Key-Value Databases
        • Redis
        • Tokyo Cabinet
        • Memcached
      • Relational Databases
      • XMPP
      • TupleSpaces
    45. Examples
    46. Rails + Mongrel/Thin
      • Cluster of application servers (Mongrel, Thin...)
      • Communication between processes is done via the database.
    47. Nanite
      • A self-assembling fabric of Ruby daemons
      • http://github.com/ezmobius/nanite
      • Uses RabbitMQ/AMQP for IPC
    48. Revactor
      • Uses the actor model
      • Actors are kinda like threads, with messaging baked-in.
      • Each Actor has a mailbox.
      • It's like coding erlang in Ruby.
      • Messages are passed between actors using TCP sockets.
      • Good Documentation
      • http://revactor.org/
      • “ 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
      • Discontinued for Reia
    49. Journeta
      • Journeta is a dirt simple library for peer discovery and message passing between Ruby applications on a LAN
      • Uses UDP Sockets for IPC
      • “ Uses the fucked up Ruby socket API”
        • -> from their RDOC
      • Demo(?)
    50. PART 3 TupleSpaces I nterprocess Communication with TupleSpaces
      • A tuple space provides a repository of tuples that can be accessed concurrently.
    51. [:add, 1, 2] [:result, 79] [:add, 60, 5] [:token] [:search, “linda”] [:where_is, :waldo [:subtract, 10, 2] [:save, 7864] The Blackboard Metaphor
    52. [: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]
    53. [:add, 1, 2] [:result, 79] [:add, 60, 5] [:token] [:search, “linda”] [:where_is, :waldo [:subtract, 10, 2] [:save, 7864] The Blackboard Metaphor [nil]
    54. [: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]
    55. About Tuple Spaces
      • First implementation was Linda.
      • Linda was developed by David Gelernter and Nicholas Carriero at Yale University.
      • Implementations exists for most languages.
      • The Ruby implementation is Rinda.
      • Rinda is a built-in library, so no need to install.
    56. 5 Basic Operations
      • read
      • read_all
      • write
      • take
      • notify
    57. 5 Basic Operations
      • read
      • read_all
      • write
      • take
      • notify
      Reads tuple, but does not remove it. Blocking, by default, but takes an additional timeout argument.
    58. 5 Basic Operations
      • read
      • read_all
      • write
      • take
      • notify
      Returns all tuples matching tuple. Does not remove the found tuples.
    59. 5 Basic Operations
      • read
      • read_all
      • write
      • take
      • notify
      Adds Tuple Takes an optional timeout parameter
    60. 5 Basic Operations
      • read
      • read_all
      • write
      • take
      • notify
      Atomic Read + Delete Blocking, by default, but takes an additional timeout argument.
    61. 5 Basic Operations
      • read
      • read_all
      • write
      • take
      • notify
      Registers for notifications of events:
      • Write
      • Take
      • Delete
    62. Key Features
      • Spaces are shared
        • Space handles details of concurrent access
      • Spaces are persistent
        • If agent process dies, data is still in space
        • 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
      • A Rinda tuple can be an array or a hash
      • A Rinda tuple can be an array or a hash
      ( But let's stick with the array, I like that better! )
    63. Start a Tuple Space on port 1234
    64. Clients/Agents
    65.  
    66. DEMO Rinda
    67. RingServer
    68.  
    69.  
    70.  
    71.  
    72.  
    73. This is also a TupleSpace
    74. SPOF
    75. Rinda is not persistent... If it crashes while you have tuples in the space, you lose them all.
    76. Only Ruby
    77. 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
      • For now, only supports: take, read, and write
      • http://github.com/dambalah/blackboard
    78. Server Just start the redis-server: $ redis-server
    79. Client/Agents
    80. DEMO Blackboard
    81. Blackboard Benchmarks
    82. Blackboard: Future
      • Move from Redis to a custom based Erlang blackboard implementation.
      • I would like that Erlang implementation to be easily used from other programming languages also.
      • So it's really two projects:
        • Blackboard in erlang
        • Ruby-library to talk to blackboard in erlang
    83. Thank you! Luc Castera Founder / messagepub.com
    84. Questions?Feedback? [email_address] www.speakerrate.com Luc Castera Founder / messagepub.com
    85. Resources / References
      • Part 1: Threading Models
        • http://timetobleed.com/threading-models-so-many-different-ways-to-get-stuff-done/
        • http://envycasts.com/products/scaling-ruby
        • http://www.infoq.com/news/2007/05/ruby-threading-futures
        • http://thebogles.com/blog/2006/11/ruby-threading/
        • http://spec.ruby-doc.org/wiki/Ruby_Threading
        • http://www.bitwiese.de/2007/09/on-processes-and-threads.html
        • http://www.igvita.com/2008/11/13/concurrency-is-a-myth-in-ruby/
        • http://bartoszmilewski.wordpress.com/2008/08/24/threads-dont-scale-processes-do/
        • http://en.wikipedia.org/wiki/Global_Interpreter_Lock
        • http://www.gotw.ca/publications/concurrency-ddj.htm
        • http://tinyurl.com/rubyfibers
    86. Resources / References
      • Part 2: Multiple Processes
        • 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
    87. Resources / References
      • Part 3: TupleSpaces
        • http://c2.com/cgi/wiki?TupleSpace
        • http://en.wikipedia.org/wiki/Tuplespace
        • http://www.julianbrowne.com/article/viewer/space-based-architecture-example
        • http://www.rubyagent.com/
        • http://segment7.net/projects/ruby/drb/
        • http://segment7.net/projects/ruby/drb/rinda/ringserver.html
        • JavaSpaces Principles, Patterns, and Practice – Freeman, Hupfer, et. al.
        • http://www.ruby-doc.org/stdlib/libdoc/rinda/rdoc/index.html
    88. 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

    + luccasteraluccastera, 5 months ago

    custom

    2112 views, 6 favs, 1 embeds more stats

    Ruby threads are limited due to the Global Interpre more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 2112
      • 1977 on SlideShare
      • 135 from embeds
    • Comments 0
    • Favorites 6
    • Downloads 83
    Most viewed embeds
    • 135 views on http://dambalah.com

    more

    All embeds
    • 135 views on http://dambalah.com

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories