You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@mesos.apache.org by Joseph Wu <jo...@mesosphere.io> on 2017/03/15 18:01:28 UTC

Re: Review Request 57358: Implemented discard behavior in process::Queue.

-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
https://reviews.apache.org/r/57358/
-----------------------------------------------------------

(Updated March 15, 2017, 11:01 a.m.)


Review request for mesos, Benjamin Mahler and Jie Yu.


Changes
-------

Changed approach.  This commit now modifies `process::Queue` and adds logic to handle discarded futures in the Queue itself.


Summary (updated)
-----------------

Implemented discard behavior in process::Queue.


Bugs: MESOS-6919
    https://issues.apache.org/jira/browse/MESOS-6919


Repository: mesos


Description (updated)
-------

This commit deals with a specific case where the SocketImpl class
passes a self-reference (shared_ptr) into a Future continuation and
then discards the original Future. The behavior of `Future::discard`
is that the `onDiscard` event is only chained to the future immediately
following the discarded future.  i.e.

```
Future<A> top;
top
  .onDiscard([]() { /* This gets executed. */ })

  .then([](const A&) { return Future<B>(); })
  .onDiscard([]() { /* This also gets executed. */ })

  .then([](const B&) { return Future<C>(); })
  .onDiscard([]() { /* But not this. */ });

top.discard();
```

When a Future is discarded, we only clear the `onDiscard` callbacks,
which means that all other continuations will continue to reside in
memory.  In the case of the `Socket` class, the Libevent SSL socket
implementation had several levels of continuations:

```
// Each item in this queue is backed by a Promise<>::Future.
Queue<Future<std::shared_ptr<SocketImpl>>> accept_queue;

// LibeventSSLSocketImpl::accept.
accept_queue.get()
  .then(...)

// Socket::accept. This continuation holds a self-reference
// to the `shared_ptr<SocketImpl>`.
  .then([self](...) {...})
```

Because the second continuation is never discarded nor called, we
end up with a dangling pointer.  For the `Socket` class, this leads
to an FD leak.

This commit fixes the self-reference by implementing discard
handlers in the libprocess Queue class.  When a waiting `Queue::get`
is discarded, the Promise backing the `Queue::get` is lazily dropped
and cleaned up.  Data on the queue will then skip these discarded
Promises and satisfy the next pending waiter.


Diffs (updated)
-----

  3rdparty/libprocess/include/process/queue.hpp ab08e30df742412f22a06202526f8b55350ed435 
  3rdparty/libprocess/src/process.cpp f6ee24e2db43d63d91222549efee85421bbf9bf3 
  3rdparty/libprocess/src/tests/queue_tests.cpp 95b738133fa50641f8f9b83014837d2808e0e4ff 


Diff: https://reviews.apache.org/r/57358/diff/3/

Changes: https://reviews.apache.org/r/57358/diff/2-3/


Testing
-------

cmake .. -DENABLE_LIBEVENT=1 -DENABLE_SSL=1

make libprocess-tests

3rdparty/libprocess/src/tests/libprocess-tests --gtest_filter="Scheme/HTTPTest.Endpoints/0" --gtest_repeat="`ulimit -n`"

make check


Thanks,

Joseph Wu


Re: Review Request 57358: Implemented discard behavior in process::Queue.

Posted by Mesos Reviewbot <re...@mesos.apache.org>.
-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
https://reviews.apache.org/r/57358/#review169658
-----------------------------------------------------------



Patch looks great!

Reviews applied: [57399, 57821, 57358]

Passed command: export OS='ubuntu:14.04' BUILDTOOL='autotools' COMPILER='gcc' CONFIGURATION='--verbose' ENVIRONMENT='GLOG_v=1 MESOS_VERBOSE=1'; ./support/docker-build.sh

- Mesos Reviewbot


On March 21, 2017, 9:40 p.m., Joseph Wu wrote:
> 
> -----------------------------------------------------------
> This is an automatically generated e-mail. To reply, visit:
> https://reviews.apache.org/r/57358/
> -----------------------------------------------------------
> 
> (Updated March 21, 2017, 9:40 p.m.)
> 
> 
> Review request for mesos, Benjamin Mahler and Greg Mann.
> 
> 
> Bugs: MESOS-6919
>     https://issues.apache.org/jira/browse/MESOS-6919
> 
> 
> Repository: mesos
> 
> 
> Description
> -------
> 
> When a consumer calls `Queue::get` on an empty Queue, the Queue creates
> a Promise to back the Future return value.  This Promise currently
> does not have a discard handler, which may cause problems if the
> Future is chained to multiple continuations.  For example, see
> the scenario in MESOS-6919.
> 
> This commit implements an (potentially expensive) discard handler
> on the Queue's Promise.  If the Future return value is discarded,
> the Queue will remove the corresponding Promise from its internal
> queue of promises.  The operation is expensive because it needs
> to reconstruct the entire internal queue of promises.
> 
> 
> Diffs
> -----
> 
>   3rdparty/libprocess/include/process/queue.hpp ab08e30df742412f22a06202526f8b55350ed435 
>   3rdparty/libprocess/src/tests/queue_tests.cpp 95b738133fa50641f8f9b83014837d2808e0e4ff 
> 
> 
> Diff: https://reviews.apache.org/r/57358/diff/4/
> 
> 
> Testing
> -------
> 
> cmake .. -DENABLE_LIBEVENT=1 -DENABLE_SSL=1
> 
> make libprocess-tests
> 
> 3rdparty/libprocess/src/tests/libprocess-tests --gtest_filter="Scheme/HTTPTest.Endpoints/0" --gtest_repeat="`ulimit -n`"
> 
> make check
> 
> 
> Thanks,
> 
> Joseph Wu
> 
>


Re: Review Request 57358: Implemented discard behavior in process::Queue.

Posted by Greg Mann <gr...@mesosphere.io>.
-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
https://reviews.apache.org/r/57358/#review171034
-----------------------------------------------------------




3rdparty/libprocess/include/process/queue.hpp
Lines 73 (patched)
<https://reviews.apache.org/r/57358/#comment243868>

    s/them/then/



3rdparty/libprocess/include/process/queue.hpp
Lines 74 (patched)
<https://reviews.apache.org/r/57358/#comment243869>

    s/interal/internal/



3rdparty/libprocess/src/tests/queue_tests.cpp
Lines 50-51 (patched)
<https://reviews.apache.org/r/57358/#comment243884>

    Both of these could probably be `ASSERT_TRUE`?



3rdparty/libprocess/src/tests/queue_tests.cpp
Lines 60 (patched)
<https://reviews.apache.org/r/57358/#comment243883>

    s/EXPECT_TRUE/ASSERT_TRUE/


- Greg Mann


On March 21, 2017, 9:40 p.m., Joseph Wu wrote:
> 
> -----------------------------------------------------------
> This is an automatically generated e-mail. To reply, visit:
> https://reviews.apache.org/r/57358/
> -----------------------------------------------------------
> 
> (Updated March 21, 2017, 9:40 p.m.)
> 
> 
> Review request for mesos, Benjamin Mahler and Greg Mann.
> 
> 
> Bugs: MESOS-6919
>     https://issues.apache.org/jira/browse/MESOS-6919
> 
> 
> Repository: mesos
> 
> 
> Description
> -------
> 
> When a consumer calls `Queue::get` on an empty Queue, the Queue creates
> a Promise to back the Future return value.  This Promise currently
> does not have a discard handler, which may cause problems if the
> Future is chained to multiple continuations.  For example, see
> the scenario in MESOS-6919.
> 
> This commit implements an (potentially expensive) discard handler
> on the Queue's Promise.  If the Future return value is discarded,
> the Queue will remove the corresponding Promise from its internal
> queue of promises.  The operation is expensive because it needs
> to reconstruct the entire internal queue of promises.
> 
> 
> Diffs
> -----
> 
>   3rdparty/libprocess/include/process/queue.hpp ab08e30df742412f22a06202526f8b55350ed435 
>   3rdparty/libprocess/src/tests/queue_tests.cpp 95b738133fa50641f8f9b83014837d2808e0e4ff 
> 
> 
> Diff: https://reviews.apache.org/r/57358/diff/4/
> 
> 
> Testing
> -------
> 
> cmake .. -DENABLE_LIBEVENT=1 -DENABLE_SSL=1
> 
> make libprocess-tests
> 
> 3rdparty/libprocess/src/tests/libprocess-tests --gtest_filter="Scheme/HTTPTest.Endpoints/0" --gtest_repeat="`ulimit -n`"
> 
> make check
> 
> 
> Thanks,
> 
> Joseph Wu
> 
>


Re: Review Request 57358: Implemented discard behavior in process::Queue.

Posted by Joseph Wu <jo...@mesosphere.io>.

> On March 24, 2017, 9:09 a.m., Greg Mann wrote:
> > 3rdparty/libprocess/include/process/queue.hpp
> > Line 58 (original), 58 (patched)
> > <https://reviews.apache.org/r/57358/diff/4/?file=1671141#file1671141line58>
> >
> >     I was just testing this patch locally, and noticed that after applying it, the NetSocketTests will deadlock. If I run `3rdparty/libprocess/libprocess-tests --gtest_filter="*NetSocketTest.*"` the deadlock appears. Haven't had a chance to dig in yet.

If you apply this: https://reviews.apache.org/r/57821/
The deadlock should go away.


- Joseph


-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
https://reviews.apache.org/r/57358/#review170021
-----------------------------------------------------------


On March 21, 2017, 2:40 p.m., Joseph Wu wrote:
> 
> -----------------------------------------------------------
> This is an automatically generated e-mail. To reply, visit:
> https://reviews.apache.org/r/57358/
> -----------------------------------------------------------
> 
> (Updated March 21, 2017, 2:40 p.m.)
> 
> 
> Review request for mesos, Benjamin Mahler and Greg Mann.
> 
> 
> Bugs: MESOS-6919
>     https://issues.apache.org/jira/browse/MESOS-6919
> 
> 
> Repository: mesos
> 
> 
> Description
> -------
> 
> When a consumer calls `Queue::get` on an empty Queue, the Queue creates
> a Promise to back the Future return value.  This Promise currently
> does not have a discard handler, which may cause problems if the
> Future is chained to multiple continuations.  For example, see
> the scenario in MESOS-6919.
> 
> This commit implements an (potentially expensive) discard handler
> on the Queue's Promise.  If the Future return value is discarded,
> the Queue will remove the corresponding Promise from its internal
> queue of promises.  The operation is expensive because it needs
> to reconstruct the entire internal queue of promises.
> 
> 
> Diffs
> -----
> 
>   3rdparty/libprocess/include/process/queue.hpp ab08e30df742412f22a06202526f8b55350ed435 
>   3rdparty/libprocess/src/tests/queue_tests.cpp 95b738133fa50641f8f9b83014837d2808e0e4ff 
> 
> 
> Diff: https://reviews.apache.org/r/57358/diff/4/
> 
> 
> Testing
> -------
> 
> cmake .. -DENABLE_LIBEVENT=1 -DENABLE_SSL=1
> 
> make libprocess-tests
> 
> 3rdparty/libprocess/src/tests/libprocess-tests --gtest_filter="Scheme/HTTPTest.Endpoints/0" --gtest_repeat="`ulimit -n`"
> 
> make check
> 
> 
> Thanks,
> 
> Joseph Wu
> 
>


Re: Review Request 57358: Implemented discard behavior in process::Queue.

Posted by Greg Mann <gr...@mesosphere.io>.
-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
https://reviews.apache.org/r/57358/#review170021
-----------------------------------------------------------




3rdparty/libprocess/include/process/queue.hpp
Line 58 (original), 58 (patched)
<https://reviews.apache.org/r/57358/#comment242751>

    I was just testing this patch locally, and noticed that after applying it, the NetSocketTests will deadlock. If I run `3rdparty/libprocess/libprocess-tests --gtest_filter="*NetSocketTest.*"` the deadlock appears. Haven't had a chance to dig in yet.


- Greg Mann


On March 21, 2017, 9:40 p.m., Joseph Wu wrote:
> 
> -----------------------------------------------------------
> This is an automatically generated e-mail. To reply, visit:
> https://reviews.apache.org/r/57358/
> -----------------------------------------------------------
> 
> (Updated March 21, 2017, 9:40 p.m.)
> 
> 
> Review request for mesos, Benjamin Mahler and Greg Mann.
> 
> 
> Bugs: MESOS-6919
>     https://issues.apache.org/jira/browse/MESOS-6919
> 
> 
> Repository: mesos
> 
> 
> Description
> -------
> 
> When a consumer calls `Queue::get` on an empty Queue, the Queue creates
> a Promise to back the Future return value.  This Promise currently
> does not have a discard handler, which may cause problems if the
> Future is chained to multiple continuations.  For example, see
> the scenario in MESOS-6919.
> 
> This commit implements an (potentially expensive) discard handler
> on the Queue's Promise.  If the Future return value is discarded,
> the Queue will remove the corresponding Promise from its internal
> queue of promises.  The operation is expensive because it needs
> to reconstruct the entire internal queue of promises.
> 
> 
> Diffs
> -----
> 
>   3rdparty/libprocess/include/process/queue.hpp ab08e30df742412f22a06202526f8b55350ed435 
>   3rdparty/libprocess/src/tests/queue_tests.cpp 95b738133fa50641f8f9b83014837d2808e0e4ff 
> 
> 
> Diff: https://reviews.apache.org/r/57358/diff/4/
> 
> 
> Testing
> -------
> 
> cmake .. -DENABLE_LIBEVENT=1 -DENABLE_SSL=1
> 
> make libprocess-tests
> 
> 3rdparty/libprocess/src/tests/libprocess-tests --gtest_filter="Scheme/HTTPTest.Endpoints/0" --gtest_repeat="`ulimit -n`"
> 
> make check
> 
> 
> Thanks,
> 
> Joseph Wu
> 
>


Re: Review Request 57358: Implemented discard behavior in process::Queue.

Posted by Joseph Wu <jo...@mesosphere.io>.
-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
https://reviews.apache.org/r/57358/
-----------------------------------------------------------

(Updated March 21, 2017, 2:40 p.m.)


Review request for mesos, Benjamin Mahler and Greg Mann.


Changes
-------

Changed implementation of discard to do a (potentially) expensive queue cleanup upon discard.


Bugs: MESOS-6919
    https://issues.apache.org/jira/browse/MESOS-6919


Repository: mesos


Description (updated)
-------

When a consumer calls `Queue::get` on an empty Queue, the Queue creates
a Promise to back the Future return value.  This Promise currently
does not have a discard handler, which may cause problems if the
Future is chained to multiple continuations.  For example, see
the scenario in MESOS-6919.

This commit implements an (potentially expensive) discard handler
on the Queue's Promise.  If the Future return value is discarded,
the Queue will remove the corresponding Promise from its internal
queue of promises.  The operation is expensive because it needs
to reconstruct the entire internal queue of promises.


Diffs (updated)
-----

  3rdparty/libprocess/include/process/queue.hpp ab08e30df742412f22a06202526f8b55350ed435 
  3rdparty/libprocess/src/tests/queue_tests.cpp 95b738133fa50641f8f9b83014837d2808e0e4ff 


Diff: https://reviews.apache.org/r/57358/diff/4/

Changes: https://reviews.apache.org/r/57358/diff/3-4/


Testing
-------

cmake .. -DENABLE_LIBEVENT=1 -DENABLE_SSL=1

make libprocess-tests

3rdparty/libprocess/src/tests/libprocess-tests --gtest_filter="Scheme/HTTPTest.Endpoints/0" --gtest_repeat="`ulimit -n`"

make check


Thanks,

Joseph Wu


Re: Review Request 57358: Implemented discard behavior in process::Queue.

Posted by Joseph Wu <jo...@mesosphere.io>.

> On March 15, 2017, 3:01 p.m., Benjamin Mahler wrote:
> > 3rdparty/libprocess/include/process/queue.hpp
> > Lines 80-86 (patched)
> > <https://reviews.apache.org/r/57358/diff/3/?file=1665030#file1665030line84>
> >
> >     This looks racy:
> >     
> >     (1) `put()` hits line 48, found the promise without a discard request.
> >     
> >     (2) Caller discards future corresponding to the promise found in (1), executes promise->discard.
> >     
> >     (3) `put()` hits line 59 and drops the `t` item on the floor.
> >     
> >     Perhaps we could implement the expensive discard approach with a TODO to optimize it, if it's simpler? Alternatively, we need to lock this section, but we need to make sure we don't deadlock (will let you think more about that).

I've implemented the expensive-discard.  And I think it covers both racing and deadlock cases.

To mitigate races, all operations on the internal queues happen inside the synchronized sections.
To mitigate deadlocks, any setting/discarding of Promises happens outside the synchronized sections.


> On March 15, 2017, 3:01 p.m., Benjamin Mahler wrote:
> > 3rdparty/libprocess/src/process.cpp
> > Lines 942-947 (patched)
> > <https://reviews.apache.org/r/57358/diff/3/?file=1665031#file1665031line942>
> >
> >     Can you comment on who is doing this discard? Or why it surfaces as discarded? The implication in your comment seems to be that this is the case where libprocess is finalizing? Couldn't quite follow why that's the case.

Moved to https://reviews.apache.org/r/57821/


- Joseph


-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
https://reviews.apache.org/r/57358/#review169065
-----------------------------------------------------------


On March 21, 2017, 2:40 p.m., Joseph Wu wrote:
> 
> -----------------------------------------------------------
> This is an automatically generated e-mail. To reply, visit:
> https://reviews.apache.org/r/57358/
> -----------------------------------------------------------
> 
> (Updated March 21, 2017, 2:40 p.m.)
> 
> 
> Review request for mesos, Benjamin Mahler and Greg Mann.
> 
> 
> Bugs: MESOS-6919
>     https://issues.apache.org/jira/browse/MESOS-6919
> 
> 
> Repository: mesos
> 
> 
> Description
> -------
> 
> When a consumer calls `Queue::get` on an empty Queue, the Queue creates
> a Promise to back the Future return value.  This Promise currently
> does not have a discard handler, which may cause problems if the
> Future is chained to multiple continuations.  For example, see
> the scenario in MESOS-6919.
> 
> This commit implements an (potentially expensive) discard handler
> on the Queue's Promise.  If the Future return value is discarded,
> the Queue will remove the corresponding Promise from its internal
> queue of promises.  The operation is expensive because it needs
> to reconstruct the entire internal queue of promises.
> 
> 
> Diffs
> -----
> 
>   3rdparty/libprocess/include/process/queue.hpp ab08e30df742412f22a06202526f8b55350ed435 
>   3rdparty/libprocess/src/tests/queue_tests.cpp 95b738133fa50641f8f9b83014837d2808e0e4ff 
> 
> 
> Diff: https://reviews.apache.org/r/57358/diff/4/
> 
> 
> Testing
> -------
> 
> cmake .. -DENABLE_LIBEVENT=1 -DENABLE_SSL=1
> 
> make libprocess-tests
> 
> 3rdparty/libprocess/src/tests/libprocess-tests --gtest_filter="Scheme/HTTPTest.Endpoints/0" --gtest_repeat="`ulimit -n`"
> 
> make check
> 
> 
> Thanks,
> 
> Joseph Wu
> 
>


Re: Review Request 57358: Implemented discard behavior in process::Queue.

Posted by Benjamin Mahler <bm...@apache.org>.
-----------------------------------------------------------
This is an automatically generated e-mail. To reply, visit:
https://reviews.apache.org/r/57358/#review169065
-----------------------------------------------------------



Can you split the change to add discard semantics to queue? That patch seems orthogonal and doesn't need the big description about the socket problem. I think it's a pretty natural change to describe on its own.


3rdparty/libprocess/include/process/queue.hpp
Lines 80-86 (patched)
<https://reviews.apache.org/r/57358/#comment241381>

    This looks racy:
    
    (1) `put()` hits line 48, found the promise without a discard request.
    
    (2) Caller discards future corresponding to the promise found in (1), executes promise->discard.
    
    (3) `put()` hits line 59 and drops the `t` item on the floor.
    
    Perhaps we could implement the expensive discard approach with a TODO to optimize it, if it's simpler? Alternatively, we need to lock this section, but we need to make sure we don't deadlock (will let you think more about that).



3rdparty/libprocess/src/process.cpp
Lines 942-947 (patched)
<https://reviews.apache.org/r/57358/#comment241379>

    Can you comment on who is doing this discard? Or why it surfaces as discarded? The implication in your comment seems to be that this is the case where libprocess is finalizing? Couldn't quite follow why that's the case.


- Benjamin Mahler


On March 15, 2017, 6:01 p.m., Joseph Wu wrote:
> 
> -----------------------------------------------------------
> This is an automatically generated e-mail. To reply, visit:
> https://reviews.apache.org/r/57358/
> -----------------------------------------------------------
> 
> (Updated March 15, 2017, 6:01 p.m.)
> 
> 
> Review request for mesos, Benjamin Mahler and Jie Yu.
> 
> 
> Bugs: MESOS-6919
>     https://issues.apache.org/jira/browse/MESOS-6919
> 
> 
> Repository: mesos
> 
> 
> Description
> -------
> 
> This commit deals with a specific case where the SocketImpl class
> passes a self-reference (shared_ptr) into a Future continuation and
> then discards the original Future. The behavior of `Future::discard`
> is that the `onDiscard` event is only chained to the future immediately
> following the discarded future.  i.e.
> 
> ```
> Future<A> top;
> top
>   .onDiscard([]() { /* This gets executed. */ })
> 
>   .then([](const A&) { return Future<B>(); })
>   .onDiscard([]() { /* This also gets executed. */ })
> 
>   .then([](const B&) { return Future<C>(); })
>   .onDiscard([]() { /* But not this. */ });
> 
> top.discard();
> ```
> 
> When a Future is discarded, we only clear the `onDiscard` callbacks,
> which means that all other continuations will continue to reside in
> memory.  In the case of the `Socket` class, the Libevent SSL socket
> implementation had several levels of continuations:
> 
> ```
> // Each item in this queue is backed by a Promise<>::Future.
> Queue<Future<std::shared_ptr<SocketImpl>>> accept_queue;
> 
> // LibeventSSLSocketImpl::accept.
> accept_queue.get()
>   .then(...)
> 
> // Socket::accept. This continuation holds a self-reference
> // to the `shared_ptr<SocketImpl>`.
>   .then([self](...) {...})
> ```
> 
> Because the second continuation is never discarded nor called, we
> end up with a dangling pointer.  For the `Socket` class, this leads
> to an FD leak.
> 
> This commit fixes the self-reference by implementing discard
> handlers in the libprocess Queue class.  When a waiting `Queue::get`
> is discarded, the Promise backing the `Queue::get` is lazily dropped
> and cleaned up.  Data on the queue will then skip these discarded
> Promises and satisfy the next pending waiter.
> 
> 
> Diffs
> -----
> 
>   3rdparty/libprocess/include/process/queue.hpp ab08e30df742412f22a06202526f8b55350ed435 
>   3rdparty/libprocess/src/process.cpp f6ee24e2db43d63d91222549efee85421bbf9bf3 
>   3rdparty/libprocess/src/tests/queue_tests.cpp 95b738133fa50641f8f9b83014837d2808e0e4ff 
> 
> 
> Diff: https://reviews.apache.org/r/57358/diff/3/
> 
> 
> Testing
> -------
> 
> cmake .. -DENABLE_LIBEVENT=1 -DENABLE_SSL=1
> 
> make libprocess-tests
> 
> 3rdparty/libprocess/src/tests/libprocess-tests --gtest_filter="Scheme/HTTPTest.Endpoints/0" --gtest_repeat="`ulimit -n`"
> 
> make check
> 
> 
> Thanks,
> 
> Joseph Wu
> 
>