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

Split up SyncClient and fix bandwidth tracking #85

Merged
merged 15 commits into from
Jul 14, 2020

Conversation

bjester
Copy link
Member

@bjester bjester commented Jul 2, 2020

Summary

Splits up the SyncClient class into a base class and two classes for each sync direction, PullClient and PushClient. This will enable us to better handling any database locking that should occur during the sync stages. I've left a small wrapper class in place for SyncClient such that this change should be backwards compatible for now.

This also fixes bandwidth tracking by attempting to track the response body size if there isn't a Content-Length header. It also now tracks the headers to get a better estimate of the entire request/response size.

Additionally, this fixes request exception handling in the SessionWrapper. Firstly, it was casting errors to strings which the errors being captured don't have a __str__ method defined, so it now simply logs the error class name. Also, it's more defensive on accessing the response from the error since it was always assuming it existed and that it was JSON.

Lastly, this adds a condition to only dequeue from the API to delete a transfer session when the session actually had records. This avoids a lengthy, serialize and then deserialize within the dequeue function when it's unnecessary.

TODO

  • Have tests been written for the new code?
  • Has documentation been written/updated?
  • New dependencies (if any) added to requirements file

Reviewer guidance

This will be most easily tested when integrated with Kolibri

Issues addressed

Related to learningequality/kolibri#7125

@codecov-commenter
Copy link

codecov-commenter commented Jul 2, 2020

Codecov Report

Merging #85 into master will increase coverage by 1.62%.
The diff coverage is 90.30%.

@@            Coverage Diff             @@
##           master      #85      +/-   ##
==========================================
+ Coverage   82.85%   84.47%   +1.62%     
==========================================
  Files          37       37              
  Lines        2385     2442      +57     
  Branches      301      303       +2     
==========================================
+ Hits         1976     2063      +87     
+ Misses        309      280      -29     
+ Partials      100       99       -1     

@bjester bjester marked this pull request as ready for review July 6, 2020 17:19
@bjester bjester changed the title Split up SyncClient Split up SyncClient and fix bandwidth tracking Jul 6, 2020
Copy link
Member

@jamalex jamalex left a comment

Choose a reason for hiding this comment

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

Looks great! One of my main questions was addresses in the latest commit (the JSON-serialization of the client_fsic field), so it seems basically good to go from my side, just some small suggestions and then one thing around the cache key that seems important.

morango/sync/session.py Show resolved Hide resolved
except TypeError:
pass
self.bytes_sent += _length_of_headers(prepped.headers)
self.bytes_sent += _headers_content_length(prepped.headers)
Copy link
Member

Choose a reason for hiding this comment

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

When we're doing the sending, do we assume the content length header will always be set, and hence this would never be 0?

Copy link
Member Author

Choose a reason for hiding this comment

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

No we aren't assuming it's always set. If the request has no body, the requests helper will not add it. So I believe this should be okay.

Copy link
Member

Choose a reason for hiding this comment

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

Right, sorry, what I mean is: can we safely assume that if the request has a body, the Content-Length header will have already been added, so that we wouldn't miss counting that transfer amount? I know on the receiving side we handle the case of the missing header (in case it was... removed by a proxy or something?), but here I'm guessing it's OK, as we have more control (request is being initiated locally).

Copy link
Member Author

Choose a reason for hiding this comment

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

The missing header on the response side is for when the response is chunked. The request body can be chunked too, but for the sync, none of the requests are chunked because we give the whole body. So if there is a body, then it would have the content length header. I can add comments so that's clear.

morango/sync/syncsession.py Outdated Show resolved Hide resolved
morango/sync/syncsession.py Outdated Show resolved Hide resolved
morango/sync/syncsession.py Outdated Show resolved Hide resolved
@bjester bjester requested a review from jamalex July 14, 2020 17:56
@bjester bjester merged commit 0814681 into learningequality:master Jul 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants