ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: samuel@oriontransfer.net
To: ruby-core@ruby-lang.org
Subject: [ruby-core:92523] [Ruby trunk Feature#14736] Thread selector for flexible cooperative fiber based concurrency
Date: Wed, 01 May 2019 23:03:01 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-77886.20190501230301.ad99a683c4b584a8@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-14736.20180504031225@ruby-lang.org

Issue #14736 has been updated by ioquatix (Samuel Williams).


> Also, should we try to handle IOs which are not explicitly set as #nonblock=true?

I guess in my proof of concept I wanted to do the bare minimum to show if it would work. But using `io/nonblock` is a bit of an ugly hack, so I personally vote to make nonblock the default when it's running in a selector. However, there ARE some situations when you want blocking IO, so I'm not sure what is the best approach. It also doesn't work well with `File.read` and similar things.

Personally, from the user POV, non-blocking vs blocking shouldn't be a concern they have to deal with.

For example, think about Rack middleware. When running in a non-blocking context, all IO should ideally be non-blocking to maximise concurrency. It shouldn't require user to opt in explicitly.

> Mutex, ConditionVariable#wait, Queue#push

I wish I can say, no user should use such a Thread primitive in the context of non-blocking selector. Of course, some synchronisation primitives might be necessary, like condition, semaphore and queue. These can be easily implemented by using Fiber, but the question remains whether `Thread` `Mutex`, `ConditionVariable` and `Queue` should be supported and how should it work.

The implementation of `Fiber` variant is typically based on cooperatively shared resources and `Fiber.yield`/`Fiber#resume`. For example, a queue is just an array, there is no need for `Mutex`. The question is then, how should `Mutex.synchronise` work within a non-blocking context? Well, no change is required, but it will cause contention if it blocks a long time. I think it's acceptable trade off, but users should adopt concurrent primitives (can be supplied by a gem) built on top of `Fiber`.

The only time a user would encounter a problem is if they use `Queue` and have mutiple readers/writers within same non-blocking context. By the current design, it would deadlock in some situations, when a task is waiting for an item, and the producer task cannot run.

The only solution to this kind of problem is to have a more elaborate system like `go` which allows to transfer "coroutines" between threads. We could implement such a system, but the trade off is now that every task must deal with synchronisation issues and users must be aware of such issues.

I tried to find a way of describing such systems. While `go` implements N:M threading model, the proposal here is N:1:M threading model. Because we insert the "1", it's both a bottleneck, and a protection for users, to avoid having to deal with explicit parallelism, while getting many benefits of concurrency.


----------------------------------------
Feature #14736: Thread selector for flexible cooperative fiber based concurrency
https://bugs.ruby-lang.org/issues/14736#change-77886

* Author: ioquatix (Samuel Williams)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Ruby concurrency can be greatly enhanced by concurrent, deterministic IO.

Fibers have been shown many times to be a great abstraction for this purpose. The retain normal code flow and don't require any kind of Thread synchronisation. They are enjoyable to write code with because you don't have to concern yourself with thread synchronisation or other complicated issues.

The basic idea is that if some operation would block, it yields the Fiber, and other Fibers within the thread can continue to execute.

There are a number of ways to implement this. Here is a proof of concept to amend the existing `rb_io_wait_readable`/`rb_io_wait_writable`.

https://github.com/ruby/ruby/pull/1870

This design minimally affects the Ruby implementation and allows flexibility for selector implementation. With a small amount of work, we can support EventMachine (65 million downloads), NIO4r (21 million downloads). It would be trivial to back port.

This PR isn't complete but I am seeking feedback. If it's a good idea, I will do my best to see it through to completion, including support for EventMachine and NIO4r.

---Files--------------------------------
port_scanner_threadlet.rb (925 Bytes)


-- 
https://bugs.ruby-lang.org/

      parent reply	other threads:[~2019-05-01 23:03 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-14736.20180504031225@ruby-lang.org>
2018-05-04  3:12 ` [ruby-core:86873] [Ruby trunk Feature#14736] Thread selector for flexible cooperative fiber based concurrency samuel
2018-05-05  2:39 ` [ruby-core:86881] " samuel
2018-05-05  2:43 ` [ruby-core:86882] " samuel
2018-05-05  2:51 ` [ruby-core:86886] " samuel
2018-06-12  3:57 ` [ruby-core:87477] " samuel
2018-06-13  0:58   ` [ruby-core:87483] " Eric Wong
2018-07-04  8:08 ` [ruby-core:87777] " samuel
2018-07-04  8:17 ` [ruby-core:87778] " samuel
2018-07-04 16:39 ` [ruby-core:87785] " funny.falcon
2018-07-05  4:12 ` [ruby-core:87798] " samuel
2018-07-05  7:25 ` [ruby-core:87804] " funny.falcon
2018-07-05  7:48 ` [ruby-core:87807] " samuel
2018-07-05  8:33 ` [ruby-core:87808] " funny.falcon
2018-07-05  8:36 ` [ruby-core:87809] " samuel
2018-07-08  2:02 ` [ruby-core:87863] " samuel
2019-05-01 15:11 ` [ruby-core:92515] " eregontp
2019-05-01 15:15 ` [ruby-core:92516] " eregontp
2019-05-01 22:41 ` [ruby-core:92522] " samuel
2019-05-01 23:03 ` samuel [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-77886.20190501230301.ad99a683c4b584a8@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).