Question on Implementation of createConnection

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Question on Implementation of createConnection

Sachin Jain
Hi,

I was going through the code of ConnectionFactory.createConnection in
branch-1.3. Here is the method which is called upon eventually

static Connection createConnection(final Configuration conf, final
boolean managed,
    final ExecutorService pool, final User user)
throws IOException {
  String className = conf.get(HConnection.HBASE_CLIENT_CONNECTION_IMPL,
    ConnectionManager.HConnectionImplementation.class.getName());
  Class<?> clazz = null;
  try {
    clazz = Class.forName(className);
  } catch (ClassNotFoundException e) {
    throw new IOException(e);
  }
  try {
    // Default HCM#HCI is not accessible; make it so before invoking.
    Constructor<?> constructor =
      clazz.getDeclaredConstructor(Configuration.class,
        boolean.class, ExecutorService.class, User.class);
    constructor.setAccessible(true);
    return (Connection) constructor.newInstance(conf, managed, pool, user);
  } catch (Exception e) {
    throw new IOException(e);
  }
}


It looks like we are instantiating HConnectionImplementaion via Reflection.
I am a bit curious to understand why we used Reflection here and not
instantiated it via just new operator.

Thanks
-Sachin
Reply | Threaded
Open this post in threaded view
|

Re: Question on Implementation of createConnection

stack-3
On Mon, Jun 12, 2017 at 8:18 PM, Sachin Jain <[hidden email]>
wrote:

> Hi,
>
> I was going through the code of ConnectionFactory.createConnection in
> branch-1.3. Here is the method which is called upon eventually
>
> static Connection createConnection(final Configuration conf, final
> boolean managed,
>     final ExecutorService pool, final User user)
> throws IOException {
>   String className = conf.get(HConnection.HBASE_CLIENT_CONNECTION_IMPL,
>     ConnectionManager.HConnectionImplementation.class.getName());
>   Class<?> clazz = null;
>   try {
>     clazz = Class.forName(className);
>   } catch (ClassNotFoundException e) {
>     throw new IOException(e);
>   }
>   try {
>     // Default HCM#HCI is not accessible; make it so before invoking.
>     Constructor<?> constructor =
>       clazz.getDeclaredConstructor(Configuration.class,
>         boolean.class, ExecutorService.class, User.class);
>     constructor.setAccessible(true);
>     return (Connection) constructor.newInstance(conf, managed, pool, user);
>   } catch (Exception e) {
>     throw new IOException(e);
>   }
> }
>
>
> It looks like we are instantiating HConnectionImplementaion via Reflection.
> I am a bit curious to understand why we used Reflection here and not
> instantiated it via just new operator.
>
>
This pattern is usual in place to allow plugging in alternative
implementations.
S


> Thanks
> -Sachin
>
Reply | Threaded
Open this post in threaded view
|

Re: Question on Implementation of createConnection

Sachin Jain
Thanks Stack!

This makes sense I just did not happen to observe that we are getting
className from configuration which makes it a pluggable implementation.

Thanks again!

On Wed, Jun 14, 2017 at 1:30 AM, Stack <[hidden email]> wrote:

> On Mon, Jun 12, 2017 at 8:18 PM, Sachin Jain <[hidden email]>
> wrote:
>
> > Hi,
> >
> > I was going through the code of ConnectionFactory.createConnection in
> > branch-1.3. Here is the method which is called upon eventually
> >
> > static Connection createConnection(final Configuration conf, final
> > boolean managed,
> >     final ExecutorService pool, final User user)
> > throws IOException {
> >   String className = conf.get(HConnection.HBASE_CLIENT_CONNECTION_IMPL,
> >     ConnectionManager.HConnectionImplementation.class.getName());
> >   Class<?> clazz = null;
> >   try {
> >     clazz = Class.forName(className);
> >   } catch (ClassNotFoundException e) {
> >     throw new IOException(e);
> >   }
> >   try {
> >     // Default HCM#HCI is not accessible; make it so before invoking.
> >     Constructor<?> constructor =
> >       clazz.getDeclaredConstructor(Configuration.class,
> >         boolean.class, ExecutorService.class, User.class);
> >     constructor.setAccessible(true);
> >     return (Connection) constructor.newInstance(conf, managed, pool,
> user);
> >   } catch (Exception e) {
> >     throw new IOException(e);
> >   }
> > }
> >
> >
> > It looks like we are instantiating HConnectionImplementaion via
> Reflection.
> > I am a bit curious to understand why we used Reflection here and not
> > instantiated it via just new operator.
> >
> >
> This pattern is usual in place to allow plugging in alternative
> implementations.
> S
>
>
> > Thanks
> > -Sachin
> >
>
Reply | Threaded
Open this post in threaded view
|

Re: Question on Implementation of createConnection

stack-3
On Tue, Jun 13, 2017 at 8:31 PM, Sachin Jain <[hidden email]>
wrote:

> Thanks Stack!
>
> This makes sense I just did not happen to observe that we are getting
> className from configuration which makes it a pluggable implementation.
>
>
No worries.
At your service,
St.Ack


> Thanks again!
>
> On Wed, Jun 14, 2017 at 1:30 AM, Stack <[hidden email]> wrote:
>
> > On Mon, Jun 12, 2017 at 8:18 PM, Sachin Jain <[hidden email]>
> > wrote:
> >
> > > Hi,
> > >
> > > I was going through the code of ConnectionFactory.createConnection in
> > > branch-1.3. Here is the method which is called upon eventually
> > >
> > > static Connection createConnection(final Configuration conf, final
> > > boolean managed,
> > >     final ExecutorService pool, final User user)
> > > throws IOException {
> > >   String className = conf.get(HConnection.HBASE_CLI
> ENT_CONNECTION_IMPL,
> > >     ConnectionManager.HConnectionImplementation.class.getName());
> > >   Class<?> clazz = null;
> > >   try {
> > >     clazz = Class.forName(className);
> > >   } catch (ClassNotFoundException e) {
> > >     throw new IOException(e);
> > >   }
> > >   try {
> > >     // Default HCM#HCI is not accessible; make it so before invoking.
> > >     Constructor<?> constructor =
> > >       clazz.getDeclaredConstructor(Configuration.class,
> > >         boolean.class, ExecutorService.class, User.class);
> > >     constructor.setAccessible(true);
> > >     return (Connection) constructor.newInstance(conf, managed, pool,
> > user);
> > >   } catch (Exception e) {
> > >     throw new IOException(e);
> > >   }
> > > }
> > >
> > >
> > > It looks like we are instantiating HConnectionImplementaion via
> > Reflection.
> > > I am a bit curious to understand why we used Reflection here and not
> > > instantiated it via just new operator.
> > >
> > >
> > This pattern is usual in place to allow plugging in alternative
> > implementations.
> > S
> >
> >
> > > Thanks
> > > -Sachin
> > >
> >
>