ia64/xen-unstable

changeset 13178:26f3c7f5de9d

Added retry behaviour when receiving SESSION_INVALID. This means that xm shell
and XenAPI.Session will reauthenticate across a Xend restart.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author Ewan Mellor <ewan@xensource.com>
date Sat Dec 23 20:20:34 2006 +0000 (2006-12-23)
parents 967426f60378
children 367f0c6efe21
files tools/python/xen/xm/XenAPI.py
line diff
     1.1 --- a/tools/python/xen/xm/XenAPI.py	Sat Dec 23 19:06:31 2006 +0000
     1.2 +++ b/tools/python/xen/xm/XenAPI.py	Sat Dec 23 20:20:34 2006 +0000
     1.3 @@ -72,6 +72,9 @@ class Failure(Exception):
     1.4                       for i in range(len(self.details))])
     1.5  
     1.6  
     1.7 +_RECONNECT_AND_RETRY = (lambda _ : ())
     1.8 +
     1.9 +
    1.10  class Session(xen.util.xmlrpclib2.ServerProxy):
    1.11      """A server proxy and session manager for communicating with Xend using
    1.12      the Xen-API.
    1.13 @@ -102,13 +105,27 @@ class Session(xen.util.xmlrpclib2.Server
    1.14              self._login(methodname, params)
    1.15              return None
    1.16          else:
    1.17 -            full_params = (self._session,) + params
    1.18 -            return _parse_result(getattr(self, methodname)(*full_params))
    1.19 +            retry_count = 0
    1.20 +            while retry_count < 3:
    1.21 +                full_params = (self._session,) + params
    1.22 +                result = _parse_result(getattr(self, methodname)(*full_params))
    1.23 +                if result == _RECONNECT_AND_RETRY:
    1.24 +                    retry_count += 1
    1.25 +                    self._login(self.last_login_method, self.last_login_params)
    1.26 +                else:
    1.27 +                    return result
    1.28 +            raise xmlrpclib.Fault(
    1.29 +                500, 'Tried 3 times to get a valid session, but failed')
    1.30  
    1.31  
    1.32      def _login(self, method, params):
    1.33 -        self._session = _parse_result(
    1.34 -            getattr(self, 'session.%s' % method)(*params))
    1.35 +        result = _parse_result(getattr(self, 'session.%s' % method)(*params))
    1.36 +        if result == _RECONNECT_AND_RETRY:
    1.37 +            raise xmlrpclib.Fault(
    1.38 +                500, 'Received SESSION_INVALID when logging in')
    1.39 +        self._session = result
    1.40 +        self.last_login_method = method
    1.41 +        self.last_login_params = params
    1.42  
    1.43  
    1.44      def __getattr__(self, name):
    1.45 @@ -131,7 +148,10 @@ def _parse_result(result):
    1.46                                    'Missing Value in response from server')
    1.47      else:
    1.48          if 'ErrorDescription' in result:
    1.49 -            raise Failure(result['ErrorDescription'])
    1.50 +            if result['ErrorDescription'][0] == 'SESSION_INVALID':
    1.51 +                return _RECONNECT_AND_RETRY
    1.52 +            else:
    1.53 +                raise Failure(result['ErrorDescription'])
    1.54          else:
    1.55              raise xmlrpclib.Fault(
    1.56                  500, 'Missing ErrorDescription in response from server')