You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@directory.apache.org by Stefan Seelmann <ma...@stefan-seelmann.de> on 2015/08/13 20:40:43 UTC

Usage of static variables in BaseInterceptor

Hi dev,

while doing some checkstyle cleanup in ApacheDS I found some strange
usage of static varables, e.g. in BaseInterceptor, there are lot of
definitions like

    protected static AttributeType OBJECT_CLASS_AT;

However those variables are not initialized in a static block, but in
the BaseInterceptor.init() method which is called for each interceptor
added. This means during server startup this static varables are set
multiple times.

Is there are reason that those are static? Is it to save some space
because I suspect that each AT is the same?

Otherwise I'd remove the static keyword and rename the variables,
because in fact they member variables.

Kind Regards,
Stefan

Re: Usage of static variables in BaseInterceptor

Posted by Emmanuel Lecharny <el...@apache.org>.
I totally agreewith your last paragraph. Actually, we designed the schema
years ago with OSGI in mind, but we weren't ready. It's probably time to
take this road now that the api is OSGI complient...

Le samedi 15 août 2015, Stefan Seelmann <ma...@stefan-seelmann.de> a écrit :

> On 08/13/2015 11:53 PM, Emmanuel Lécharny wrote:
> > So, here is what I think : we should have a diect access to those AT
> > through the SchemaManager without having to do a lookup in the
> > registries. Some ways to do it is to have a static class that is
> > initialized after the SM, and which provides a direct access to the AT,
> > like :
>
> Thanks Emmanuel for detailed explanation, I agree it makes sense to save
> CPU (and memory).
>
> I agree to have a provider for direct access to often used AT and OC.
> However I think we should avoid static class, instead initialize the
> provider object once and inject it where needed. Avoiding static code
> normally increases maintainability and makes testing easier.
>
> Kind Regards,
> Stefan
>
>
>

-- 
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com

Re: Usage of static variables in BaseInterceptor

Posted by Stefan Seelmann <ma...@stefan-seelmann.de>.
On 08/13/2015 11:53 PM, Emmanuel Lécharny wrote:
> So, here is what I think : we should have a diect access to those AT
> through the SchemaManager without having to do a lookup in the
> registries. Some ways to do it is to have a static class that is
> initialized after the SM, and which provides a direct access to the AT,
> like :

Thanks Emmanuel for detailed explanation, I agree it makes sense to save
CPU (and memory).

I agree to have a provider for direct access to often used AT and OC.
However I think we should avoid static class, instead initialize the
provider object once and inject it where needed. Avoiding static code
normally increases maintainability and makes testing easier.

Kind Regards,
Stefan



Re: Usage of static variables in BaseInterceptor

Posted by Emmanuel Lécharny <el...@gmail.com>.
Le 13/08/15 20:40, Stefan Seelmann a écrit :
> Hi dev,
>
> while doing some checkstyle cleanup in ApacheDS I found some strange
> usage of static varables, e.g. in BaseInterceptor, there are lot of
> definitions like
>
>     protected static AttributeType OBJECT_CLASS_AT;
>
> However those variables are not initialized in a static block, but in
> the BaseInterceptor.init() method which is called for each interceptor
> added. This means during server startup this static varables are set
> multiple times.
>
> Is there are reason that those are static? Is it to save some space
> because I suspect that each AT is the same?
>
> Otherwise I'd remove the static keyword and rename the variables,
> because in fact they member variables.

Those variable are meant to be initialized once and only once. I think
the current code is only implementing this partially. The rational is
that we are using AT all over the interceptors, and they never change
(because the schema can't be updated dynamically without a server
restart). Having those variables allows the interceptor instance to
avoid doing a hell lots of lookups.

Can it be done in a static code ? Short answer : only if we have a way
to get access to the schemaManager instance, and I don't think we do.

So this is a partial implementation that saves a bit of CPU when a
session uses many times eachinterceptors. It does not save as much CPU
as expected, if those variable were to be initialized once and only once.

So to speak, it's not perfect. It's not even good if we consider that we
could imagine a future situation where the schemaManager is to be
updated without restarting the server, as we may have to reload those AT
(not likely, but who knows).

So, here is what I think : we should have a diect access to those AT
through the SchemaManager without having to do a lookup in the
registries. Some ways to do it is to have a static class that is
initialized after the SM, and which provides a direct access to the AT,
like :

...
AttributeType objectClassAt =
directoryService.getAtProvider.getObjectClass();
...

where AtProvider class is like :

class AtProvider
{
    private static AttributeType OBJECT_CLASS_AT;
    ...

    AtProvider( SchemaManager sm)
    {
        OBJECT_CLASS_AT = sm.lookupAttributeType( OBJECT_CLASS_OID );
        ...
    }

    public AttributeType getObjectClass()
    {
        return OBJECT_CLASS_AT;
    }
    ...
}

Ideally, the VM would inline the getObjectClass() method to have a
direct access to the OBJECT_CLASS_AT instance.

This is just a suggestion, there may be better ways.

I hope I explain the reason we have such variables, even if it's not
perfect...