[ 
https://issues.apache.org/jira/browse/MESOS-1384?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14074885#comment-14074885
 ] 

Niklas Quarfot Nielsen commented on MESOS-1384:
-----------------------------------------------

Here is a small write up on the proposed design:

h2. Motivation

The motivation behind mesos modules is to make it possible to implement 
abstract interfaces to internal Mesos components outside the Mesos binary, as 
dynamic loadable libraries.
Instead of mapping each and every call to C++ mangled (or self mangled) 
symbols: http://mentorembedded.github.io/cxx-abi/abi.html and 
https://github.com/nqn/dylib .
We can use the already existing vtable layout to dispatch to implementations 
within a loaded library (just like our current use of JNI).

{code}
                          Mesos              +  External Library (.so / .dylib)
                                             |
                                             |                            
Implementation
Pure virtual class +----------------------+  |  
+-------------------------------------+
                   | IsolatorModule       |  |  | IsolatorModuleImpl : 
IsolatorModule |
                   |                      |  |  |                               
      |
                   | + Module metadata    |  |  | + Module metadata (Version: 
XXX)    |
                   | + create(library)    |  |  |                               
      |
                   |                      |  |  |                               
      |
                   | +------------------+ |  |  | 
+---------------------------------+ |
                   | |Isolator          | |  |  | | IsolatorImpl                
    | |
                   | |                  | |  |  | |                             
    | |
                   | |                  | |  |  | |                             
    | |
                   | |                  | |  |  | |                             
    | |
                   | |                  | |  |  | |                             
    | |
                   | +------------------+ |  |  | 
+---------------------------------+ |
                   +-+------------------+-+  |  
+-+---------------------------------+-+
                                             |
                                             |
Try<shared_ptr<Isolator> > isolator =        |  extern "C" {
  IsolatorModule::create("libxxx.so");       |    void* 
create_isolator_module(void*) {
// Use isolator as if it was hosted by Mesos |      return new 
IsolatorModuleImpl;
                                             |    }
                                             |  }
                                             |
                                             +


{code}

This design has been implemented as a proof-of-concept in this branch:  
https://github.com/nqn/mesos/tree/niklas/mesos_module

The sequence is as follows (taken IsolatorModule as an example):
1) Use stout's DynamicLibrary abstraction to get a library object
2) Call (new and dedicated) module shims initialize method 
(IsolatorModule::create(), AllocatorModule::create(), 
AuthenticatorModule::create(), ...)
  2.a) Behind the scenes, the create() method localize the proper symbol in the 
library
  2.b) calls it (with optional arguments as void*)
  2.c) Verifies it through metadata located between the isolator module object 
start and the isolator offset.
  2.d) returns a try to the isolator (compiler rebase from isolator module to 
isolator)
3) Use returned isolator object as usual.

h3. Notes to drawing

The definition of IsolatorModule is shared (included) by both Mesos and library 
implementer.
The IsolatorModule::create() is static and knows which symbol to look for.
A standard scheme of "create_XXXXX_module" could be used, so one library can be 
used to multiple module implementations.
create() is called init() in the proof-of-concept: 
https://github.com/nqn/mesos/tree/niklas/mesos_module

h2. Q&A

h4. 1) Why do we return a void pointer across the library border?

We need to be able to locate the right symbol in the library to generate a 
module object.
This means that we can / should not use mangled names (which C++ types, like 
classes) requires if it were a return type.

h4. 2) Why don't we cast the void pointer with dynamic_cast for type 
verification.

GCC doesn't support this on void pointers unfortunately.

h4. 3) Why does create() / init() require a path to the library file instead of 
a DynamicLibrary type?

We want to be able to control the life-cycle of the loaded library separately 
from the life-cycle of a module object.
Multiple modules can be provided by one library file.
A short-cut could be to overload the create() / init() methods and add the path 
constructor as a 2nd option.

h4. 4) Why do we use multiple inheritance? How about having the isolator as a 
member variable instead?

We get an _IsolatorModule_ from the library. This contains a small shim around 
a real Isolator object which encodes a small amount of metadata for module 
verification.
Why not a member variable? This is mostly tied to clean up. If Mesos only calls 
delete() on the Isolator object, the IsolatorModule object will leak its 
metadata.
The metadata could be cleaned up during initialization. I have no strong 
opinion on this, but making it inherited seemed cleaner.

h4. 5) How do we verify that the API didn't change since the external library 
was built?

Adding enough metadata to do verification of the loaded module. This metadata 
could be mesos version, API version, compiler and architecture and so on.

> Add support for loadable MesosModule
> ------------------------------------
>
>                 Key: MESOS-1384
>                 URL: https://issues.apache.org/jira/browse/MESOS-1384
>             Project: Mesos
>          Issue Type: Improvement
>    Affects Versions: 0.19.0
>            Reporter: Timothy St. Clair
>            Assignee: Niklas Quarfot Nielsen
>
> I think we should break this into multiple phases.
> -(1) Let's get the dynamic library loading via a "stout-ified" version of 
> https://github.com/timothysc/tests/blob/master/plugin_modules/DynamicLibrary.h.
>  -
> *DONE*
> (2) Use (1) to instantiate some classes in Mesos (like an Authenticator 
> and/or isolator) from a dynamic library. This will give us some more 
> experience with how we want to name the underlying library symbol, how we 
> want to specify flags for finding the library, what types of validation we 
> want when loading a library.
> *TARGET* 
> (3) After doing (2) for one or two classes in Mesos I think we can formalize 
> the approach in a "mesos-ified" version of 
> https://github.com/timothysc/tests/blob/master/plugin_modules/MesosModule.h.
> *NEXT*



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to