You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@reef.apache.org by Ashvin A <aa...@gmail.com> on 2016/04/26 00:29:54 UTC
Accessing Driver instance using Injector.bindVolatileInstance
Hi,
In my use case I have a class, Scheduler, that uses Driver to manage task
containers as needed. The Scheduler is instantiated by a library. As I
result I cannot directly pass instance of Driver when Scheduler is
constructed. Can I use Tang to register instance of Driver and then get the
Driver instance in Scheduler?
For e.g.
Bind instance of Driver:
Tang.Factory.getTang().newInjector().bindVolatileInstance(MyDriver.class,
thisDriverInstance)
and then access it in Scheduler
Tang.Factory.getTang().newInjector().getInstance(MyrDriver.class)
The above does not work. It tries to create a new instance of MyDriver
which is not desired.
1. Is this a valid use of Tang and Injector?
2. Currently I have added a static method in MyDriver to get its instance.
Is there a better way to achieve this in REEF.
Thanks,
Ashvin
Re: Accessing Driver instance using Injector.bindVolatileInstance
Posted by Markus Weimer <ma...@weimo.de>.
On 4/26/2016 10:37 AM, Ashvin A wrote:
> Currently Scheduler initialization happens after Driver is started,
> which means Scheduler cannot be a constructor parameter. I will think
> more and try to use this feature.
Maybe use a `Future<Scheduler>` to be injected?
Markus
Re: Accessing Driver instance using Injector.bindVolatileInstance
Posted by Ashvin A <aa...@gmail.com>.
>
> Now to the idea: You could use such an external constructor to hide the
> static field. The `Scheduler` registers itself with the external
> constructor, and the Driver then gets a `Scheduler` injected as a
> constructor parameter as if `Scheduler` as created by Tang. That way, the
> REEF side of the code stays "clean" in the sense that everything is
> dependency-injected. And the one class that manages the static variable can
> ensure important invariants (e.g. only allowing assign-once or maybe even
> read-once).
>
> Would this help?
This sounds like a good idea. There is one issue though. Currently
Scheduler initialization happens after Driver is started, which means
Scheduler cannot be a constructor parameter. I will think more and try to
use this feature. Thanks for all the great suggestions !
Re: Accessing Driver instance using Injector.bindVolatileInstance
Posted by Markus Weimer <ma...@weimo.de>.
On 2016-04-25 16:45, Ashvin A wrote:
>> Is there a place in your code that knows both?
>
>
> No. I am trying to define one. REEF instantiates Driver. Scheduler is
> initialized by its library. The two need to discover each other using some
> "registry". Injector/Tang looked like a way to go since it is already
> linking other components.
I have an idea. First, some background: Tang supports external
constructors. They are meant for cases where you'd like to make a class
injectable via Tang, but can't change its constructors (and its
dependencies) to use Tang. An external constructor is a class with a
single method that produces the instance of that type. The external
constructor is created via Tang and can therefore take part in Tang's
configuration mechanism.
Now to the idea: You could use such an external constructor to hide the
static field. The `Scheduler` registers itself with the external
constructor, and the Driver then gets a `Scheduler` injected as a
constructor parameter as if `Scheduler` as created by Tang. That way,
the REEF side of the code stays "clean" in the sense that everything is
dependency-injected. And the one class that manages the static variable
can ensure important invariants (e.g. only allowing assign-once or maybe
even read-once).
Would this help?
Markus
Re: Accessing Driver instance using Injector.bindVolatileInstance
Posted by Ashvin A <aa...@gmail.com>.
>
>> No. I cannot instantiate the Scheduler. It is independently instantiated
>> by
>> a third party class.
>>
>
> Is there a place in your code that knows both?
No. I am trying to define one. REEF instantiates Driver. Scheduler is
initialized by its library. The two need to discover each other using some
"registry". Injector/Tang looked like a way to go since it is already
linking other components.
Thanks,
Ashvin
Re: Accessing Driver instance using Injector.bindVolatileInstance
Posted by Markus Weimer <ma...@weimo.de>.
On 2016-04-25 16:02, Ashvin A wrote:
> Can I "get" reference to the injector which has the state? Something like
> Tang.Factory.getTang.getInjector(some-literal). Although this would be same
> as what I have implemented.
We don't have that, and I agree: That would be slightly hidden static
variable anyhow :)
>> Can you instantiate the `Scheduler` in the Driver? Then, you can pass a
>> reference to `this` to the Scheduler.
>
>
> No. I cannot instantiate the Scheduler. It is independently instantiated by
> a third party class.
Is there a place in your code that knows both?
Markus
Re: Accessing Driver instance using Injector.bindVolatileInstance
Posted by Ashvin A <aa...@gmail.com>.
On Mon, Apr 25, 2016 at 3:50 PM, Markus Weimer <ma...@weimo.de> wrote:
> On 4/25/2016 3:29 PM, Ashvin A wrote:
>
>> For e.g. Bind instance of Driver:
>> Tang.Factory.getTang().newInjector().bindVolatileInstance(MyDriver.class,
>> thisDriverInstance) and then access it in Scheduler
>> Tang.Factory.getTang().newInjector().getInstance(MyrDriver.class)
>>
>> The above does not work.
>>
>
> This can't work: Objects created by or added via `boundVolatile()` to an
> injector are local to that injector. They are also a singleton with
> respect to that Injector. There is no `static` state in Tang, so nothing
> is shared between Injectors.
Can I "get" reference to the injector which has the state? Something like
Tang.Factory.getTang.getInjector(some-literal). Although this would be same
as what I have implemented.
> It tries to create a new instance of MyDriver which is not desired.
>>
>
> Yes, because that second injector doesn't know about the first.
>
> 2. Currently I have added a static method in MyDriver to get its
>> instance. Is there a better way to achieve this in REEF.
>>
>
> Can you instantiate the `Scheduler` in the Driver? Then, you can pass a
> reference to `this` to the Scheduler.
No. I cannot instantiate the Scheduler. It is independently instantiated by
a third party class.
>
Thanks,
Ashvin
Re: Accessing Driver instance using Injector.bindVolatileInstance
Posted by Markus Weimer <ma...@weimo.de>.
On 4/25/2016 3:29 PM, Ashvin A wrote:
> For e.g. Bind instance of Driver:
> Tang.Factory.getTang().newInjector().bindVolatileInstance(MyDriver.class,
> thisDriverInstance) and then access it in Scheduler
> Tang.Factory.getTang().newInjector().getInstance(MyrDriver.class)
>
> The above does not work.
This can't work: Objects created by or added via `boundVolatile()` to an
injector are local to that injector. They are also a singleton with
respect to that Injector. There is no `static` state in Tang, so nothing
is shared between Injectors.
> It tries to create a new instance of MyDriver which is not desired.
Yes, because that second injector doesn't know about the first.
> 2. Currently I have added a static method in MyDriver to get its
> instance. Is there a better way to achieve this in REEF.
Can you instantiate the `Scheduler` in the Driver? Then, you can pass a
reference to `this` to the Scheduler.
Markus