Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1LP][RFR] added retry functionality in order to sort out Unathorized error in sprout #253

Merged
merged 2 commits into from
May 17, 2018

Conversation

izapolsk
Copy link
Contributor

@izapolsk izapolsk commented May 7, 2018

No description provided.

@dajoRH dajoRH changed the title [RFR] added retry functionality in order to sort out Unathorized error in sprout [WIP] added retry functionality in order to sort out Unathorized error in sprout May 7, 2018
@izapolsk izapolsk changed the title [WIP] added retry functionality in order to sort out Unathorized error in sprout [RFR] added retry functionality in order to sort out Unathorized error in sprout May 8, 2018
Sometimes connection to openshift api endpoint gets expired and openshift returns 401.
As a result tasks in some applications like sprout fail.
"""
def wrap(*args, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

required a wrapper should use the functools.wraps decorator if possible

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

return wrap


@reconnect(unauthenticated_error_handler)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

form maintenance it might be better to actually apply the decorator on each method

Copy link
Contributor Author

@izapolsk izapolsk May 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't difficult to me to do that. however, there are very many methods and their amount will grow. Do you think it is necessary ?

@izapolsk izapolsk force-pushed the expired_conn branch 2 times, most recently from 637ec81 to cc77fa9 Compare May 11, 2018 14:51
@bsquizz bsquizz self-assigned this May 11, 2018
Copy link
Contributor

@bsquizz bsquizz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If self.new_client is False, your reconnect logic won't do anything.

https://github.com/izapolsk/wrapanapi/blob/d21c897f029abf6ca33526ff73029798df636775/wrapanapi/containers/providers/rhopenshift.py#L159

Probably best to have a 'force_new_client' kwarg on the _connect method, which your decorator will set to True when it does a 401 retry.

But do we even need this new_client kwarg? When do we ever want new_client to be False?

try:
return method(*args, **kwargs)
except ApiException as e:
if e.reason == 'Unauthorized' and attempt <= attempts:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can check for e.status == 401 here instead of looking at the string. May be more reliable.

https://github.com/kubernetes-client/python/blob/369366a813964e3d72e91d402c5f471275e7a3e5/kubernetes/client/rest.py#L289-L301

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to use reason instead of code because it gives clear understanding about the reason. Whilst using code will require additional comment.

@wraps(method)
def wrap(*args, **kwargs):
attempts = 3
for attempt in range(1, attempts + 1):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking for "attempt <= attempts" is redundant since it's impossible for attempt to ever be > attempts inside your for loop. I think this could be written as:

def wrap(*args, **kwargs):
    reconnect_attempts = 3
    for attempt in range(reconnect_attempts):
        try:
            return method(*args, **kwargs)
        except ApiException as e:
            if e.status == 401:
                args[0]._connect()
    # Final attempt, 401 exception won't be caught here if we still hit it.
    return method(*args, **kwargs)

That would give 4 total method attempts, with 3 reconnect attempts.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same code in the beginning but changed it to what you saw in order to make exact number of reconnect attempts. But it seems it isn't necessary :)

@izapolsk
Copy link
Contributor Author

@bsquizz, regarding new_client, I started working on replacement of old methods and removal old client.
#255
It will happen very soon. So, I think we don't need to worry about its absence in connect method.

@izapolsk izapolsk requested a review from bsquizz May 16, 2018 20:47
@bsquizz bsquizz changed the title [RFR] added retry functionality in order to sort out Unathorized error in sprout [1LP][RFR] added retry functionality in order to sort out Unathorized error in sprout May 16, 2018
@izapolsk izapolsk merged commit 2e674b3 into RedHatQE:master May 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants