You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Reto Bachmann-Gmür <re...@trialox.org> on 2009/03/05 10:52:02 UTC
declarative services and concurrency
I guess a simple question: is it guaranteed that the
activate,deactivate, the bind- and unbind methods of a component are not
called synchronously?
Cheers,
Reto
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re: declarative services and concurrency
Posted by Reto Bachmann-Gmür <re...@trialox.org>.
Yes, sorry with "felix " I was actually meaning "Felix Declarative
Services".
Regards,
Reto
Richard S. Hall said the following on 03/07/2009 05:52 PM:
> I am not sure if this has anything to do with Felix per se, it sounds
> like more of a feature of the SCR implementation. Perhaps SCR queues
> its work, in which case, the activate would be processed before the bind.
>
> -> richard
>
> On 3/7/09 9:35 AM, Reto Bachmann-Gmür wrote:
>> experimenting with felix I found that dynamic dependencies are not
>> injected while the acivate method is being executed.
>>
>> The experiment code
>>
>> /**
>> * @scr.component
>> * @scr.reference name="component" cardinality="0..n" policy="dynamic"
>> * interface="java.lang.Object" target="(testing=true)"
>> */
>> public class Main {
>>
>> protected void bindComponent(Object component) {
>> System.out.print("binding component");
>> }
>>
>> protected void unbindComponent(Object component) {
>> System.out.print("unbinding component");
>> }
>>
>> protected void activate(ComponentContext context) {
>> System.out.print("starting");
>> try {
>> Thread.sleep(30 * 1000);
>> } catch (InterruptedException ex) {
>> ex.printStackTrace();
>> }
>> System.out.print("finished starting");
>> }
>> }
>>
>> when I activate the above component and an injectable component
>> immediately after, the output "binding component" will only appear after
>> "finished starting".
>>
>> Is this a felix specific behaviour that is not guaranteed by the
>> specifications?
>>
>> Regards,
>> Reto
>>
>>
>>
>> Neil Bartlett said the following on 03/05/2009 11:09 AM:
>>
>>> It depends. If your component uses the "static policy" then the
>>> binds/unbinds will not happen concurrently with the
>>> activate/deactivate methods.
>>>
>>> However, if you use "dynamic" policy then they certainly can happen
>>> concurrently and you need to write your component to be thread-safe.
>>>
>>> Regards
>>> Neil
>>>
>>> On Thu, Mar 5, 2009 at 9:52 AM, Reto Bachmann-Gmür
>>> <re...@trialox.org> wrote:
>>>
>>>
>>>> I guess a simple question: is it guaranteed that the
>>>> activate,deactivate, the bind- and unbind methods of a component
>>>> are not
>>>> called synchronously?
>>>>
>>>> Cheers,
>>>> Reto
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>>>> For additional commands, e-mail: users-help@felix.apache.org
>>>>
>>>>
>>>>
>>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>>> For additional commands, e-mail: users-help@felix.apache.org
>>>
>>>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>> For additional commands, e-mail: users-help@felix.apache.org
>>
>>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re: declarative services and concurrency
Posted by Felix Meschberger <fm...@gmail.com>.
This is exactly the case. Our Declarative Services implementation
maintains a single background thread which is responsible for binding
references and activating components.
So, yes, here you generally will not get a bind or unbind call while in
the activate method.
I chose this implementation to minimize the risks for deadlocks to the
absolute possible minimum. For example a service REGISTERED event is
received in the thread which registers the service, which may well be
the thread starting the bundle (if the service registered by a bundle
activator).
Such a service registration may lead to the satisfaction of a component
and thus to another serice registration and bind etc. etc. So instead of
just letting this waterfall flow, each bind and/or undind is queue and
then sequentally handled.
But again: Don't take this for granted, this is an implementation
speciality of the Felix SCR implementation.
At always keep in mind: A service may come or may go at any point in time.
Regards
Felix
Richard S. Hall schrieb:
> I am not sure if this has anything to do with Felix per se, it sounds
> like more of a feature of the SCR implementation. Perhaps SCR queues its
> work, in which case, the activate would be processed before the bind.
>
> -> richard
>
> On 3/7/09 9:35 AM, Reto Bachmann-Gmür wrote:
>> experimenting with felix I found that dynamic dependencies are not
>> injected while the acivate method is being executed.
>>
>> The experiment code
>>
>> /**
>> * @scr.component
>> * @scr.reference name="component" cardinality="0..n" policy="dynamic"
>> * interface="java.lang.Object" target="(testing=true)"
>> */
>> public class Main {
>>
>> protected void bindComponent(Object component) {
>> System.out.print("binding component");
>> }
>>
>> protected void unbindComponent(Object component) {
>> System.out.print("unbinding component");
>> }
>>
>> protected void activate(ComponentContext context) {
>> System.out.print("starting");
>> try {
>> Thread.sleep(30 * 1000);
>> } catch (InterruptedException ex) {
>> ex.printStackTrace();
>> }
>> System.out.print("finished starting");
>> }
>> }
>>
>> when I activate the above component and an injectable component
>> immediately after, the output "binding component" will only appear after
>> "finished starting".
>>
>> Is this a felix specific behaviour that is not guaranteed by the
>> specifications?
>>
>> Regards,
>> Reto
>>
>>
>>
>> Neil Bartlett said the following on 03/05/2009 11:09 AM:
>>
>>> It depends. If your component uses the "static policy" then the
>>> binds/unbinds will not happen concurrently with the
>>> activate/deactivate methods.
>>>
>>> However, if you use "dynamic" policy then they certainly can happen
>>> concurrently and you need to write your component to be thread-safe.
>>>
>>> Regards
>>> Neil
>>>
>>> On Thu, Mar 5, 2009 at 9:52 AM, Reto Bachmann-Gmür
>>> <re...@trialox.org> wrote:
>>>
>>>
>>>> I guess a simple question: is it guaranteed that the
>>>> activate,deactivate, the bind- and unbind methods of a component are
>>>> not
>>>> called synchronously?
>>>>
>>>> Cheers,
>>>> Reto
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>>>> For additional commands, e-mail: users-help@felix.apache.org
>>>>
>>>>
>>>>
>>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>>> For additional commands, e-mail: users-help@felix.apache.org
>>>
>>>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>> For additional commands, e-mail: users-help@felix.apache.org
>>
>>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re: declarative services and concurrency
Posted by "Richard S. Hall" <he...@ungoverned.org>.
I am not sure if this has anything to do with Felix per se, it sounds
like more of a feature of the SCR implementation. Perhaps SCR queues its
work, in which case, the activate would be processed before the bind.
-> richard
On 3/7/09 9:35 AM, Reto Bachmann-Gmür wrote:
> experimenting with felix I found that dynamic dependencies are not
> injected while the acivate method is being executed.
>
> The experiment code
>
> /**
> * @scr.component
> * @scr.reference name="component" cardinality="0..n" policy="dynamic"
> * interface="java.lang.Object" target="(testing=true)"
> */
> public class Main {
>
> protected void bindComponent(Object component) {
> System.out.print("binding component");
> }
>
> protected void unbindComponent(Object component) {
> System.out.print("unbinding component");
> }
>
> protected void activate(ComponentContext context) {
> System.out.print("starting");
> try {
> Thread.sleep(30 * 1000);
> } catch (InterruptedException ex) {
> ex.printStackTrace();
> }
> System.out.print("finished starting");
> }
> }
>
> when I activate the above component and an injectable component
> immediately after, the output "binding component" will only appear after
> "finished starting".
>
> Is this a felix specific behaviour that is not guaranteed by the
> specifications?
>
> Regards,
> Reto
>
>
>
> Neil Bartlett said the following on 03/05/2009 11:09 AM:
>
>> It depends. If your component uses the "static policy" then the
>> binds/unbinds will not happen concurrently with the
>> activate/deactivate methods.
>>
>> However, if you use "dynamic" policy then they certainly can happen
>> concurrently and you need to write your component to be thread-safe.
>>
>> Regards
>> Neil
>>
>> On Thu, Mar 5, 2009 at 9:52 AM, Reto Bachmann-Gmür
>> <re...@trialox.org> wrote:
>>
>>
>>> I guess a simple question: is it guaranteed that the
>>> activate,deactivate, the bind- and unbind methods of a component are not
>>> called synchronously?
>>>
>>> Cheers,
>>> Reto
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>>> For additional commands, e-mail: users-help@felix.apache.org
>>>
>>>
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>> For additional commands, e-mail: users-help@felix.apache.org
>>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>
>
Re: declarative services and concurrency
Posted by Reto Bachmann-Gmür <re...@trialox.org>.
But as in the scenario of the experiment felix is actually waiting with
enabling (and injecting) the second component till the first one (the
one with the Thread.sleep) finished activating. Why does felix wait half
a minute if not forced by specification?
Regards,
reto
Neil Bartlett said the following on 03/07/2009 03:48 PM:
> Indeed, this is NOT guaranteed.
>
> What you will find in small-scale testing is that it's actually quite
> hard to force the concurrency to happen, because you can't get the
> thread scheduler to switch contexts when you want it to. But the bugs
> are only hiding, and are likely to appear when the component is
> running under high load with lots of threads... i.e. when it is in
> production, the worst possible time to find such problems!
>
> Because of the probabilistic nature of concurrency bugs, it is very
> hard to build tests that are adequate for finding them.
>
> Regards,
> Neil
>
> On Sat, Mar 7, 2009 at 2:35 PM, Reto Bachmann-Gmür
> <re...@trialox.org> wrote:
>
>> experimenting with felix I found that dynamic dependencies are not
>> injected while the acivate method is being executed.
>>
>> The experiment code
>>
>> /**
>> * @scr.component
>> * @scr.reference name="component" cardinality="0..n" policy="dynamic"
>> * interface="java.lang.Object" target="(testing=true)"
>> */
>> public class Main {
>>
>> protected void bindComponent(Object component) {
>> System.out.print("binding component");
>> }
>>
>> protected void unbindComponent(Object component) {
>> System.out.print("unbinding component");
>> }
>>
>> protected void activate(ComponentContext context) {
>> System.out.print("starting");
>> try {
>> Thread.sleep(30 * 1000);
>> } catch (InterruptedException ex) {
>> ex.printStackTrace();
>> }
>> System.out.print("finished starting");
>> }
>> }
>>
>> when I activate the above component and an injectable component
>> immediately after, the output "binding component" will only appear after
>> "finished starting".
>>
>> Is this a felix specific behaviour that is not guaranteed by the
>> specifications?
>>
>> Regards,
>> Reto
>>
>>
>>
>> Neil Bartlett said the following on 03/05/2009 11:09 AM:
>>
>>> It depends. If your component uses the "static policy" then the
>>> binds/unbinds will not happen concurrently with the
>>> activate/deactivate methods.
>>>
>>> However, if you use "dynamic" policy then they certainly can happen
>>> concurrently and you need to write your component to be thread-safe.
>>>
>>> Regards
>>> Neil
>>>
>>> On Thu, Mar 5, 2009 at 9:52 AM, Reto Bachmann-Gmür
>>> <re...@trialox.org> wrote:
>>>
>>>
>>>> I guess a simple question: is it guaranteed that the
>>>> activate,deactivate, the bind- and unbind methods of a component are not
>>>> called synchronously?
>>>>
>>>> Cheers,
>>>> Reto
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>>>> For additional commands, e-mail: users-help@felix.apache.org
>>>>
>>>>
>>>>
>>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>>> For additional commands, e-mail: users-help@felix.apache.org
>>>
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>> For additional commands, e-mail: users-help@felix.apache.org
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re: declarative services and concurrency
Posted by Neil Bartlett <nj...@gmail.com>.
Indeed, this is NOT guaranteed.
What you will find in small-scale testing is that it's actually quite
hard to force the concurrency to happen, because you can't get the
thread scheduler to switch contexts when you want it to. But the bugs
are only hiding, and are likely to appear when the component is
running under high load with lots of threads... i.e. when it is in
production, the worst possible time to find such problems!
Because of the probabilistic nature of concurrency bugs, it is very
hard to build tests that are adequate for finding them.
Regards,
Neil
On Sat, Mar 7, 2009 at 2:35 PM, Reto Bachmann-Gmür
<re...@trialox.org> wrote:
> experimenting with felix I found that dynamic dependencies are not
> injected while the acivate method is being executed.
>
> The experiment code
>
> /**
> * @scr.component
> * @scr.reference name="component" cardinality="0..n" policy="dynamic"
> * interface="java.lang.Object" target="(testing=true)"
> */
> public class Main {
>
> protected void bindComponent(Object component) {
> System.out.print("binding component");
> }
>
> protected void unbindComponent(Object component) {
> System.out.print("unbinding component");
> }
>
> protected void activate(ComponentContext context) {
> System.out.print("starting");
> try {
> Thread.sleep(30 * 1000);
> } catch (InterruptedException ex) {
> ex.printStackTrace();
> }
> System.out.print("finished starting");
> }
> }
>
> when I activate the above component and an injectable component
> immediately after, the output "binding component" will only appear after
> "finished starting".
>
> Is this a felix specific behaviour that is not guaranteed by the
> specifications?
>
> Regards,
> Reto
>
>
>
> Neil Bartlett said the following on 03/05/2009 11:09 AM:
>> It depends. If your component uses the "static policy" then the
>> binds/unbinds will not happen concurrently with the
>> activate/deactivate methods.
>>
>> However, if you use "dynamic" policy then they certainly can happen
>> concurrently and you need to write your component to be thread-safe.
>>
>> Regards
>> Neil
>>
>> On Thu, Mar 5, 2009 at 9:52 AM, Reto Bachmann-Gmür
>> <re...@trialox.org> wrote:
>>
>>> I guess a simple question: is it guaranteed that the
>>> activate,deactivate, the bind- and unbind methods of a component are not
>>> called synchronously?
>>>
>>> Cheers,
>>> Reto
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>>> For additional commands, e-mail: users-help@felix.apache.org
>>>
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>> For additional commands, e-mail: users-help@felix.apache.org
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re: declarative services and concurrency
Posted by Reto Bachmann-Gmür <re...@trialox.org>.
experimenting with felix I found that dynamic dependencies are not
injected while the acivate method is being executed.
The experiment code
/**
* @scr.component
* @scr.reference name="component" cardinality="0..n" policy="dynamic"
* interface="java.lang.Object" target="(testing=true)"
*/
public class Main {
protected void bindComponent(Object component) {
System.out.print("binding component");
}
protected void unbindComponent(Object component) {
System.out.print("unbinding component");
}
protected void activate(ComponentContext context) {
System.out.print("starting");
try {
Thread.sleep(30 * 1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.print("finished starting");
}
}
when I activate the above component and an injectable component
immediately after, the output "binding component" will only appear after
"finished starting".
Is this a felix specific behaviour that is not guaranteed by the
specifications?
Regards,
Reto
Neil Bartlett said the following on 03/05/2009 11:09 AM:
> It depends. If your component uses the "static policy" then the
> binds/unbinds will not happen concurrently with the
> activate/deactivate methods.
>
> However, if you use "dynamic" policy then they certainly can happen
> concurrently and you need to write your component to be thread-safe.
>
> Regards
> Neil
>
> On Thu, Mar 5, 2009 at 9:52 AM, Reto Bachmann-Gmür
> <re...@trialox.org> wrote:
>
>> I guess a simple question: is it guaranteed that the
>> activate,deactivate, the bind- and unbind methods of a component are not
>> called synchronously?
>>
>> Cheers,
>> Reto
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
>> For additional commands, e-mail: users-help@felix.apache.org
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org
Re: declarative services and concurrency
Posted by Neil Bartlett <nj...@gmail.com>.
It depends. If your component uses the "static policy" then the
binds/unbinds will not happen concurrently with the
activate/deactivate methods.
However, if you use "dynamic" policy then they certainly can happen
concurrently and you need to write your component to be thread-safe.
Regards
Neil
On Thu, Mar 5, 2009 at 9:52 AM, Reto Bachmann-Gmür
<re...@trialox.org> wrote:
> I guess a simple question: is it guaranteed that the
> activate,deactivate, the bind- and unbind methods of a component are not
> called synchronously?
>
> Cheers,
> Reto
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
> For additional commands, e-mail: users-help@felix.apache.org
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@felix.apache.org
For additional commands, e-mail: users-help@felix.apache.org