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

DM-38578: Improve configuration of HttpResource class #48

Merged
merged 6 commits into from Apr 8, 2023

Conversation

airnandez
Copy link
Contributor

Checklist

  • ran Jenkins
  • added a release note for user-visible changes to doc/changes

@codecov
Copy link

codecov bot commented Apr 5, 2023

Codecov Report

Patch coverage: 87.80% and no project coverage change.

Comparison is base (94ae226) 85.49% compared to head (1f65ffd) 85.49%.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #48   +/-   ##
=======================================
  Coverage   85.49%   85.49%           
=======================================
  Files          27       27           
  Lines        3633     3731   +98     
  Branches      746      766   +20     
=======================================
+ Hits         3106     3190   +84     
- Misses        416      428   +12     
- Partials      111      113    +2     
Impacted Files Coverage Δ
python/lsst/resources/http.py 81.18% <81.01%> (-0.55%) ⬇️
tests/test_http.py 94.67% <100.00%> (+0.44%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Member

@timj timj left a comment

Choose a reason for hiding this comment

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

I didn't follow everything in detail but I agree this does make the code more configurable and it looks okay to me.

class HttpResourcePathConfig:
"""Configuration class to encapsulate the configurable items used by class
HttpResourcePath.
"""

# Default timeouts for all HTTP requests (seconds).
DEFAULT_TIMEOUT_CONNECT = 30
Copy link
Member

Choose a reason for hiding this comment

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

I assume the drop from 300 to 30 was deliberate?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it was deliberate. 300 seconds was too high for a default connection timeout.

)
try:
self._timeout = (
int(os.environ.get("LSST_HTTP_TIMEOUT_CONNECT", self.DEFAULT_TIMEOUT_CONNECT)),
Copy link
Member

Choose a reason for hiding this comment

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

timeout isn't a float?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are right. Timeout is a pair of floats. Corrected.

backoff_factor=30 * (1 + random.random()),
# (seconds). Compute a random jitter to prevent all the clients
# to overwhelm the server by sending requests at the same time.
backoff_factor=self._backoff_min + (self._backoff_max - self._backoff_min) * random.random(),
Copy link
Member

Choose a reason for hiding this comment

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

The http client backoff doesn't do this automatically? In the s3 implementation we use the backoff package but I assumed that requests handled that itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The urllib3 implementation of retries (used by the requests package) does not add jitter to the backoff factor passed as argument. I thought it would be more flexible to allow configuring via environment variables an interval from when the backoff is computed including jitter. In the current implementation jitter is added but the backoff factor is kind of fixed to be in the interval [30.0, 60.0) seconds.

config = HttpResourcePathConfig()
self.assertFalse(config.collect_memory_usage)

with unittest.mock.patch.dict(os.environ, {"LSST_HTTP_COLLECT_MEMORY_USAGE": "true"}, clear=True):
Copy link
Member

Choose a reason for hiding this comment

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

Another approach would be to take one of the other real tests, patch the environment and then run with assertLogs("DEBUG") and see if you get a memory usage message in the log. It depends on whether you want to test that this flag is set or if this flag has a real effect.

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 considered that the tests of time_this would test that memory usage is actually collected when required. My intention here is to test that you can actually configure that from the environment.

…ntext manager.

We only collect memory usage if the LSST_HTTP_COLLECT_MEMORY_USAGE environment
variable is set to any value. Systematically collecting memory usage for every
operation against the server is too expensive in terms of execution time
and is only required for debugging purposes.
* place all the default values inside the class.
* add the possibility to configure the collection of memory usage via the
  environment variable LSST_HTTP_COLLECT_MEMORY_USAGE to avoid expending
  time collecting this information for every time_this context.
* add the possibility to specify the interval for selecting the backoff
  factor for retrying requests via environment variables
  LSST_HTTP_BACKOFF_MIN and LSST_HTTP_BACKOFF_MAX.
@airnandez airnandez merged commit 1b3121a into main Apr 8, 2023
15 checks passed
@airnandez airnandez deleted the tickets/DM-38578 branch April 8, 2023 07:56
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.

None yet

2 participants