You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hbase.apache.org by Sachin Jain <sa...@gmail.com> on 2017/06/13 03:18:16 UTC

Question on Implementation of createConnection

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

Re: Question on Implementation of createConnection

Posted by Stack <st...@duboce.net>.
On Tue, Jun 13, 2017 at 8:31 PM, Sachin Jain <sa...@gmail.com>
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 <st...@duboce.net> wrote:
>
> > On Mon, Jun 12, 2017 at 8:18 PM, Sachin Jain <sa...@gmail.com>
> > 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
> > >
> >
>

Re: Question on Implementation of createConnection

Posted by Sachin Jain <sa...@gmail.com>.
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 <st...@duboce.net> wrote:

> On Mon, Jun 12, 2017 at 8:18 PM, Sachin Jain <sa...@gmail.com>
> 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
> >
>

Re: Question on Implementation of createConnection

Posted by Stack <st...@duboce.net>.
On Mon, Jun 12, 2017 at 8:18 PM, Sachin Jain <sa...@gmail.com>
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
>