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

Remove retry circular dependency #1969

Merged
merged 9 commits into from
Apr 22, 2024

Conversation

gustavogaldinoo
Copy link
Contributor

Removing usage of @retry.wrap in logs methods.

This removal is necessary because retries also use logs inside of their implementation so we want to avoid the following cyclic scenario:

  1. Log method is called
  2. A log fails (e.g. permission issue)
  3. The logging method uses a retry wrapper, so it tries again
  4. The code inside the retry wrapper also calls a log method, so we go back to step 1

Before this PR, this scenario was avoided by using a log_retries parameter, that would disable loggings on retries functions when the retry was called from a log function.

This PR remove this parameter and get rid of the circular dependency by making a custom retry logic inside of the logs methods.

@gustavogaldinoo
Copy link
Contributor Author

Not sure what's the best way to test this change though

import logging
import os
import sys
import traceback
import time
Copy link
Contributor

Choose a reason for hiding this comment

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

Annoying google nit, can you move this line up so the imports are alphabetical order

common/logs.py Outdated
from common import utils
from common.retry import get_delay
Copy link
Contributor

Choose a reason for hiding this comment

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

Annother annoying Google nit:we don't import functions, only modules:
(see https://google.github.io/styleguide/pyguide.html)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, got it!

I did this way to avoid logs importing retry and retry importing logs, not sure if it is a problem since log is not using retry.wrap decorator though, but I wanted to reuse the get_delay function

Copy link
Contributor

Choose a reason for hiding this comment

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

THis is a good point. I think we should just reimplement get_delay here instead of having circular imports.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or actually, can you move it to a third module that both modules can import?

common/logs.py Outdated
logger.log_struct(struct_message, severity=severity)
# Custom retry logic to avoid circular dependency
# as retry from retry.py uses log
for num_try in range(1, NUM_RETRIES + 1):
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you do this from 0? Or change NUM_RETRIES to NUM_ATTEMPTS?
As far as I can tell this will run the loop a max of 3 times. but num_retries=3 technically implies a max of 4.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice, I decided to change the nomenclature to NUM_ATTEMPTS, as the get_retry function already was 1-index based

common/logs.py Outdated
from common import utils
from common.retry import get_delay
Copy link
Contributor

Choose a reason for hiding this comment

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

THis is a good point. I think we should just reimplement get_delay here instead of having circular imports.

from common import utils


def test_get_retry_delay():
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you mock sleep so that the test doesn't take more time than it needs to.

@jonathanmetzman jonathanmetzman merged commit 162ca0c into master Apr 22, 2024
5 checks passed
@jonathanmetzman jonathanmetzman deleted the remove-retry-circular-dependency branch April 22, 2024 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants