Rainbows! Rack HTTP server user/dev discussion
 help / color / mirror / code / Atom feed
From: Eric Wong <normalperson-rMlxZR9MS24@public.gmane.org>
To: Rainbows! list <rainbows-talk-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org>
Subject: Re: negative timeout in Rainbows::Fiber::Base
Date: Wed, 5 Sep 2012 23:27:39 +0000	[thread overview]
Message-ID: <20120905232739.GA25153@dcvr.yhbt.net> (raw)
In-Reply-To: <CAA2_N1vfWXGw_CaaMWMijUSdMN2Pz882SYDtNEW2_6YWffgTKQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>

"Lin Jen-Shin (godfat)" <godfat-hOE/xeEBYYIdnm+yROfE0A@public.gmane.org> wrote:
> On Fri, Aug 31, 2012 at 9:37 AM, Eric Wong <normalperson-rMlxZR9MS24@public.gmane.org> wrote:
> > I seem to recall problems with some of the more esoteric test cases in
> > Rainbows! a few years ago.
> >
> > Now that I think more about it, it might've been related to client
> > pipelining.  If a client pipelines requests, I don't think using
> > EM.defer {} makes it easy to guarantee the servers responses are
> > returned in the correct order.
> >
> > This is made worse since (AFAIK) EM provides no easy way to
> > temporarily disable firing read callbacks for a socket, so
> > a client which pipelines aggressively becomes bad news.
> After some experiments, now I understood why it is hard. But I can't
> figure it out by some quick glimpses for how you did solve this problems
> for other concurrency model?

Simple: we don't read from the socket at all while processing a request

Extra data the client sends gets buffered in the kernel, eventually TCP
backoff will kick in and the Internet stays usable :>

With the inability to easily stop read callbacks via EM, the socket
buffers constantly get drained so the clients are able to keep sending
data.  The only option I've found for Rainbows! + EM was to issue
shutdown(SHUT_RD) if a client attempts to pipeline too much (which
never happens with legitimate clients AFAIK).

> One possible and simple way would be... just make piped requests
> sequential, but this would greatly reduce the concurrency ability,
> is it right? At least Puma server runs quite poorly whenever I am
> testing pipeline requests.

Yes, pipelining is handled sequentially.  It's the easiest and safest
way to implement since HTTP/1.1 requires the responses to be returned in
the same order they were sent (as you mention below).

> My test script  is:
> httperf --hog --server localhost --port 8080 --uri /cpu --num-calls 4
> --burst-length 2 --num-conn 2 --rate 8 --print-reply
> But Zbatery runs quite smoothly with ThreadPool and ThreadSpawn.
> I assume it's because Zbatery would handle piped requests concurrently
> and collect responses and reply them with the correct order, though
> I cannot tell from the code, at least from some quick glimpses.

Rainbows!/Zbatery handles pipelined requests sequentially.  They only
have concurrency on a per-socket level.

> At this point I am more confident to say that Unicorn family is the best
> Ruby application servers. :)

Good to hear :)


> To address ordering issue, I guess we can remember the
> index of a certain request, and if there's a request being
> processed which has a lower index, the response shouldn't
> be written back before the lower one has been written.
> Not sure if this is wroth the effort though... This must touch
> Rainbows' internal, and it cannot be easily handled by
> simply extending the client class.

I don't think it is worth the effort.  I'm not even sure how often
pipelining is used in the real world, all I know is Rainbows! can
handle it without falling over.

> > Maybe disabling keepalive/persistent connections will make this work
> > correctly (but you obviously lose latency benefits, too).
> >
> > I also don't think it's possible to say "no pipelining" to a client if
> > we support persistent connections at all.
> I wonder if we always run Nginx or something similar in front of
> Rainbows, does it still matter?

It shouldn't matter for nginx, I don't think nginx will (ever) pipeline
to a backend.  Nowadays nginx can do persistent connections to backends,
though I'm not sure how much of a benefit it is for local sockets.

> I see. Never thought of that EM might be buffering a lot of large
> responses in the memory. As for loading large amounts of data
> into memory, I guess I can't tell. As far as I know, no, but who knows :P
> This must be accidental if there's one...

I think your comment is unfortunately representative of a lot of
software development nowadays.

Embrace pessimism and let it be your guide :)

> > Ruby 1.9 sets stack sizes to 512K regardless of ulimit -s.  At least on
> > Linux, memory defaults to being overcommited and is lazily allocated in
> > increments of PAGE_SIZE (4K on x86*).  It's likely the actual RSS overhead
> > of a native thread stack is <64K.
> >
> > VMSize overhead becomes important on 32-bit with many native threads,
> > though.  In comparison, Fibers use only 4K stack and has no extra
> > overhead in the kernel.
> I see, thanks for the explanation. I guess that does matter a bit, but only
> if we're using thousands of threads/fibers, and it should be quite rarely
> in a web app, I guess.
> Using fibers are also risking from system stack overflow, especially in
> a Rails app with a lot of plugins, I guess... Umm, but I also heard that
> fibers stack is increased a bit in newer Ruby?

You're right, fiber stacks got bigger.  They're 64K on all 1.9.3 and
128K on 64-bit for 2.0.0dev.  So there's even less benefit in using
Fibers nowadays for memory concerns.

> >> Though I really doubt if threads are really that heavy comparing to fibers.
> >> At least in some simple tests, threads are fine and efficient enough.
> >
> > I agree native threads are light enough for most cases (especially since
> > you're already running Ruby :).
> Speaking to this and green threads, I wonder if it's worth the effort to
> implement m:n threading for Ruby? Or we can just compile and
> link against a threading library which supports m:n threading?
> Goroutine? :P

*shrug*  Not worth _my_ effort for m:n threads.
Rainbows! mailing list - rainbows-talk-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org
Do not quote signatures (like this one) or top post when replying

  parent reply	other threads:[~2012-09-05 23:27 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-23 20:36 negative timeout in Rainbows::Fiber::Base Lin Jen-Shin (godfat)
     [not found] ` <CAA2_N1unOXb7Z4Jr8oKoSLu266O9Ko4o=oWzAcMA1w3=9X74KA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-08-25  2:45   ` Eric Wong
     [not found]     ` <20120825024556.GA25977-yBiyF41qdooeIZ0/mPfg9Q@public.gmane.org>
2012-08-26  0:12       ` Lin Jen-Shin (godfat)
     [not found]         ` <CAA2_N1uhfcHDbTvY+ke0Cid6=i7KEhFn8jvEirx+ptYVDacdvA-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-08-26  1:15           ` Eric Wong
2012-08-29 16:00           ` Lin Jen-Shin (godfat)
     [not found]             ` <CAA2_N1thakAOVp7ibCNic+TjEVvXE0OGLgzXH3fJ1c2UTs68oQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-08-29 21:17               ` Eric Wong
     [not found]                 ` <20120829211707.GA22726-yBiyF41qdooeIZ0/mPfg9Q@public.gmane.org>
2012-08-30 21:33                   ` Lin Jen-Shin (godfat)
     [not found]                     ` <CAA2_N1tc=Xx8WHaM8H=EWshyzGEyX04PnkdBGj9Jdb7cSzmbRQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-08-31  1:37                       ` Eric Wong
     [not found]                         ` <20120831013731.GA16613-yBiyF41qdooeIZ0/mPfg9Q@public.gmane.org>
2012-09-05 20:06                           ` Lin Jen-Shin (godfat)
     [not found]                             ` <CAA2_N1vfWXGw_CaaMWMijUSdMN2Pz882SYDtNEW2_6YWffgTKQ-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-09-05 23:27                               ` Eric Wong [this message]
     [not found]                                 ` <20120905232739.GA25153-yBiyF41qdooeIZ0/mPfg9Q@public.gmane.org>
2012-09-22  9:52                                   ` Lin Jen-Shin (godfat)
     [not found]                                     ` <CAA2_N1v460utbL31Qu-JbGuUxav1hY4X5+cEf=Mp2rOC5efzMw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-09-22 19:42                                       ` Eric Wong
     [not found]                                         ` <20120922194222.GA6839-yBiyF41qdooeIZ0/mPfg9Q@public.gmane.org>
2012-09-28 15:14                                           ` Lin Jen-Shin (godfat)
     [not found]                                             ` <CAA2_N1usHJVZgn5n7RaTyDCbK7eu6G4ocZAsvqsVeL6cPERskw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-09-28 19:11                                               ` Eric Wong
     [not found]                                                 ` <20120928191132.GA14292-yBiyF41qdooeIZ0/mPfg9Q@public.gmane.org>
2012-09-28 19:24                                                   ` Eric Wong
     [not found]                                                     ` <20120928192449.GB14292-yBiyF41qdooeIZ0/mPfg9Q@public.gmane.org>
2012-10-31  0:14                                                       ` Lin Jen-Shin (godfat)
2012-12-18 11:09                                                       ` Lin Jen-Shin (godfat)
     [not found]                                                         ` <CAA2_N1tcA-HK20C8Ok1Lv9KWwMD4fctCOPHTLeD9ayRJqWby1Q-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2012-12-18 19:19                                                           ` Eric Wong

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-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: https://yhbt.net/rainbows/

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

  git send-email \
    --in-reply-to=20120905232739.GA25153@dcvr.yhbt.net \
    --to=normalperson-rmlxzr9ms24@public.gmane.org \
    --cc=rainbows-talk-GrnCvJ7WPxnNLxjTenLetw@public.gmane.org \


* 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.
Code repositories for project(s) associated with this public inbox


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