Эх сурвалжийг харах

[libsentry] Handle HA exception more properly

Bubble up errors in case of timeout
Romain Rigaux 10 жил өмнө
parent
commit
bbd9fd6

+ 13 - 9
desktop/libs/libsentry/src/libsentry/api.py

@@ -45,16 +45,20 @@ def ha_error_handler(func):
     while retries > 0:
       try:
         return func(*args, **kwargs)
-      except:
-        # Right now retries on any Thrift error and pull a fresh list of servers from ZooKeeper
-        LOG.info('Retrying fetching an available client in ZooKeeper.')
-        global _api_cache
-        _api_cache = None
-        time.sleep(1)
+      except SentryException, e:
+        raise e
+      except Exception, e:
         retries -= 1
-        args[0].client = _get_client(args[0].client.username)
-      else:
-        retries = 0
+        if not get_sentry_server_ha_enabled() or retries == 0:
+          raise e
+        else:
+          # Right now retries on any error and pull a fresh list of servers from ZooKeeper
+          LOG.info('Retrying fetching an available client in ZooKeeper.')
+          global _api_cache
+          _api_cache = None
+          time.sleep(1)
+          args[0].client = _get_client(args[0].client.username)
+          LOG.info('Picked %s' % args[0].client)
 
   return decorator
 

+ 8 - 3
desktop/libs/libsentry/src/libsentry/client.py

@@ -58,8 +58,10 @@ class SentryClient(object):
 
   def __init__(self, host, port, username):
     self.username = username
+    self.host = host
+    self.port = port
     self.security = self._get_security()
-    
+
     self.client = thrift_util.get_client(
         SentryPolicyService.Client,
         host,
@@ -73,6 +75,9 @@ class SentryClient(object):
         mechanism=self.security['mechanism']
     )
 
+  def __str__(self):
+    return ', '.join(map(str, [self.host, self.port, self.username, self.security]))
+
 
   def _get_security(self):
     principal = get_sentry_server_principal()
@@ -82,7 +87,7 @@ class SentryClient(object):
       kerberos_principal_short_name = None
     use_sasl = get_sentry_server_authentication() == 'KERBEROS'
     mechanism = SentryClient.SENTRY_MECHANISMS[get_sentry_server_authentication()]
-    
+
     return {
         'kerberos_principal_short_name': kerberos_principal_short_name,
         'use_sasl': use_sasl,
@@ -172,7 +177,7 @@ class SentryClient(object):
   def list_sentry_privileges_by_authorizable(self, authorizableSet, groups=None, roleSet=None):
     authorizableSet = [TSentryAuthorizable(**authorizable) for authorizable in authorizableSet]
     if roleSet is not None:
-      roleSet = TSentryActiveRoleSet(**roleSet)    
+      roleSet = TSentryActiveRoleSet(**roleSet)
 
     request = TListSentryPrivilegesByAuthRequest(requestorUserName=self.username, authorizableSet=authorizableSet, groups=groups, roleSet=roleSet)
     return self.client.list_sentry_privileges_by_authorizable(request)