python-zulip-api icon indicating copy to clipboard operation
python-zulip-api copied to clipboard

api: Add random exponential backoff to do_api_query.

Open orientor opened this issue 5 years ago • 15 comments

Set delay time on failure in do_api_query according to random exponential backoff.

Fixes #537 .

orientor avatar Feb 23 '20 10:02 orientor

Made a PR for do_api_query. But base and cap need to be decided. Also if this is done, then I can change class RandomExponentialBackoff accordingly.

orientor avatar Feb 23 '20 10:02 orientor

Can you provide terminal output showing you've tested the error handling behaves correctly?

timabbott avatar Feb 25 '20 19:02 timabbott

Screenshot from 2020-02-27 07-15-53

I have tested the condition when retry is required. But in the original code retry happens only in two cases:

  1. When we receive a 50x status code.
  2. If we have connected to the server before but couldn't connect now.

Hence in ALL other cases (with no retry) my code works same as the original code.(Tested it and also an error gets raised in these cases so changing loop doesn't effect them)

For the first 2 cases the output will be like that shown in the image.

orientor avatar Feb 27 '20 01:02 orientor

I also checked if it works normally in normal conditions. Screenshot from 2020-03-03 02-19-31

Screenshot from 2020-03-03 02-19-50

orientor avatar Mar 02 '20 21:03 orientor

Some of the other errors:

Screenshot from 2020-03-03 02-20-12 Screenshot from 2020-03-03 02-22-45

orientor avatar Mar 02 '20 21:03 orientor

I have thoroughly checked most of the cases. Code working fine in all.

orientor avatar Mar 02 '20 21:03 orientor

@orientor can you rework your commits here to follow our commit style? I think what makes sense are:

  • A first commit that adjusts the behavior of RandomExponentialBackoff to add a delay_cap, with a default value of 90 (10 is too short).
  • A second commit that does the delay_base refactor (nonfunctional).
  • A third commit that changes the randomization approach in RandomExponentialBackoff, which we might choose to skip.
  • A next commit that migrates the error_retry logic to use RandomExponentialBackoff
  • Etc.

That approach will be reviewable, biseactable, and mergable.

timabbott avatar Mar 04 '20 21:03 timabbott

@timabbott I have reworked my changes into two commits. In the first commit I am improving the Random Exponential Backoff class. In the second commit I am updating the do_api_query method to use Random Exponential Backoff.

Now going to update it to 4.

orientor avatar Mar 04 '20 21:03 orientor

@timabbott removed delay_base completely as it changed only one iteration(it was set to 1/2 so it would basically reduce power of 2 by 1) and I think was not required.

Reworked changes into 3 commits.

  1. Added delay_cap as class variable to the CountingBackoff class, the superclass for RandomExponentialBackoff. Changed the default delay_cap to 90.

  2. Changed the algorithm for Random Exponential Backoff.

  3. Changed do_api_query so as to use Random Exponential backoff.

orientor avatar Mar 04 '20 23:03 orientor

The first commit adds the delay_cap field but doesn't have it do anything. That isn't a coherent commit -- it's just confusing were to merge just that.

Zulip's development model is around every commit in every PR being mergable incrementally, which is important for bisecting as well as efficient code review.

Can you fix the first commit to fully implement delay_cap?

timabbott avatar Mar 05 '20 00:03 timabbott

@timabbott Improved the commits. The first commit now basically adds delay_cap properly, with the Random Exponential Backoff now using delay_cap. The second commit improves Random Exponential Backoff algorithm. The third commit adds Random Exponential Backoff to do_api_query method.

orientor avatar Mar 05 '20 16:03 orientor

I merged the first commit and posted a couple comments. I'll also note that this doesn't replace the time.sleep(1) in call_on_each_event, nor does it add a change to detect "invalid authentication" failures and not retry those, which I think we should do.

timabbott avatar Mar 05 '20 21:03 timabbott

@timabbott Changed the code to be more readable and have minimal changes. I was not aware of call_on_each_event. Will make the necessary commits soon. Also can you shed some light on invalid authentication? According to me the only way of having invalid authentication is wrong API key. Are there any other methods? I will make the changes accordingly.

orientor avatar Mar 06 '20 19:03 orientor

Added exponential backoff to call_on_each_event. But the function was originally intended to keep retrying to connect the server until successful. So an infinite loop. @timabbott @showell Is that intended or should I add a max number of retries?

orientor avatar Mar 15 '20 11:03 orientor

Heads up @orientor, we just merged some commits that conflict with the changes your made in this pull request! You can review this repository's recent commits to see where the conflicts occur. Please rebase your feature branch against the upstream/master branch and resolve your pull request's merge conflicts accordingly.

zulipbot avatar Apr 19 '20 03:04 zulipbot