13

Why UserGroupInformation class is designed this way? Why setConfiguration(Configuration conf) method of UserGroupInformation class is static ?

My understanding is that this will restrict client to be able to connect to only 1 cluster per JVM.

How can we connect to multiple clusters at the same time from single JVM? I think this is a very basic scenario currently not being supported by Hadoop API.

Any help would be appreciated.

user1522820
  • 1,584
  • 5
  • 18
  • 31

2 Answers2

2

What is static method?

In general, main motivation for making a method static is convenience. You can call a static method without creating any object, just by using it's class name.So if you need a method, which you want to call directly by class name, make that method static. Utility classes e.g. java.lang.Math or StringUtils, are good examples of classes, which uses static methods.

What static method does?

  1. Static method doesn't modify state of object. Since state of object is maintained as instance variables
  2. Static method mostly operates on arguments, almost all static method accepts arguments, perform some calculation and return value.

What setConfiguration does?

setConfiguration(Configuration conf) -

Set the static configuration for UGI. In particular, set the security authentication mechanism and the group look up service.

How can we connect to multiple clusters at the same time from single JVM? I think this is a very basic scenario currently not being supported by Hadoop API.

Ans: Hadoop API is using like singleton design pattern here. You cannot do that because if you can imagine ever requiring to use object inheritance or needing to use polymorphism for your method, you should definitely skip the static and make it an instance method.

A good scenerio is described here: Static methods are a code smell

Resource Link:

  1. When to make a method static in Java
  2. Java Singleton Design Pattern Best Practices with Examples
SkyWalker
  • 28,384
  • 14
  • 74
  • 132
  • 2
    Your answer doesn't answer the main question. Why conf has been made static? It disallows connections to multiple Kerberos clusters within same JVM. Isn't it? – user1522820 May 04 '16 at 16:09
  • yes. That I have told you. As I want to give an example, then America has only one president. Or In banking system, at the same time there will be not double session for single user/client. Multiple will not be allowed here. If you want to do that then you have to skip static and use instance method. – SkyWalker May 04 '16 at 16:20
1

I am not sure why they have made the method static. But if you want to access multiple cluster from single JVM then you can use multiple classloaders to get around the problem of setting multiple configuration from single JVM.

Here are some links:

Community
  • 1
  • 1
kamalkishor1991
  • 876
  • 9
  • 14
  • Can you provide a working example of using class loaders to overcome this problem? – Or Bar Yaacov Apr 15 '21 at 12:13
  • I tried creating custom class loaders such as child first class loader or just an empty custom class loader. I created an instance of the class loader and loaded the UserGroupInformation class. Then I want to execute the setConfig method but I can't because I need to specify the Configuration class but that class is loaded from the default class loader of the thread. – Or Bar Yaacov Apr 15 '21 at 12:20