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

Pass arguments through promise chain instead of attaching to generator #186

Merged
merged 7 commits into from Jun 6, 2019

Conversation

jgraff2
Copy link
Contributor

@jgraff2 jgraff2 commented May 23, 2019

Pass arguments through the promise chain instead of attaching to the generator, so we don't have to clone generators for by-subject collection and we can track the OAuth token on the generator object.

This replaces the global auth tracking fix from 183.

Updated tests here: salesforce/refocus#1182

@jgraff2 jgraff2 requested review from iamigo, pallavi2209 and kriscfoster and removed request for iamigo and kriscfoster May 23, 2019 01:06
@jgraff2 jgraff2 changed the title Generator oauth fix Pass arguments through promise chain instead May 23, 2019
@jgraff2 jgraff2 changed the title Pass arguments through promise chain instead Pass arguments through promise chain instead of attaching to generator May 23, 2019
@iamigo
Copy link
Contributor

iamigo commented May 23, 2019

fyi bunch of style changes but I'll start reviewing

Copy link
Contributor

@iamigo iamigo left a comment

Choose a reason for hiding this comment

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

Nice! OK to merge once the jscs issues are cleaned up.

@coveralls
Copy link

coveralls commented May 23, 2019

Coverage Status

Coverage decreased (-0.3%) to 93.617% when pulling 53c8844 on generator-oauth-fix into 3fdd9e5 on master.

@iamigo
Copy link
Contributor

iamigo commented May 23, 2019

@jgraff2 have you tested this anywhere with an Argus generator?

@jgraff2
Copy link
Contributor Author

jgraff2 commented May 23, 2019

I ran it with the integration tests which include an OAuth generator and mocked auth endpoints.

return doCollect(_g);
}));
return getSubjectsForGenerator(generator)
.mapSeries((subject) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this mean the collections will happen sequentially instead of parallel (like before)? If yes, could this have any side effect? What if a repeater process is not finished until next repeater starts? Just throwing my thoughts out there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, that's a good point, that would definitely be a plausible scenario when run sequentially if we had lots of subjects. We don't have any way of detecting or handling that case. Thinking this through, the next repeat cycle would start while the previous one is still finishing up, so the last few requests would be sent in parallel with the first few for the next cycle. The sample upserts aren't sent until all requests have completed, and the requests are tracked in-memory separately for each collection cycle, so the overlapping cycles wouldn't interfere with each other, and they would end up finishing and sending upserts a minute apart, as normal. So it would still work normally, just with some requests being sent in parallel, which is how it was before anyway. The only impact would be the offset from the repeat cycle would be greater. And probably greater memory usage because we would be tracking two collection cycles at once.

So I think if the repeater cycles overlapped it would be fine. However, just the fact that a collection could take a lot longer this way is maybe reason to reconsider.

I did it this way to make sure we only do the token request once. If we did it in parallel, when the token expires, the next collection cycle would send a separate auth request for each subject; this way it only sends it the first time. But maybe this is another case where it would be better to rethink the current approach than try and force the solution to fit it.

The reason this is necessary in the first place is because the OAuth logic is implemented as part of sending the request. We could instead pull that out into a separate function, and run that once before sending any of the requests. I'm not sure exactly how that would work though since we currently rely on the result of the requests to tell us whether the token needs to be regenerated. Maybe when the token expires we just miss that cycle and request a new one next time? Or if any of the requests fail, we re-do all of them?

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you both for thinking that through. Good question and good answer.

The sample upserts aren't sent until all requests have completed

Does this mean that all the samples could be sitting and waiting because one of the requests might be slow and eventually times out at 30s, but with retries, everything else still waiting... ? So the rest of the samples don't get sent until that slow one finally times out again after all the retries and generates its error samples?

Maybe when the token expires we just miss that cycle and request
a new one next time?

IMO, missing a cycle is not an acceptable solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, they won't get sent until all the promises complete.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated.

I moved the token creation and request retries outside the request-specific code, and went back to making the requests in parallel.

So now the by-subject collection flow looks like this:

  • make a request to refocus to get subjects from subject query
  • If necessary, make a request to the OAuth server to get a new token
  • for each subject, in parallel, make a request to the data source
    • if any of the requests failed because of an expired token, retry the entire collection cycle, which will generate a new token
    • if any of the requests failed for other reasons, do nothing
  • generate transform samples from the successful responses, or error samples from the failed ones

This way, all the asynchronous stuff happens outside the subject loop. (subject query, token creation, retries)

@pallavi2209
Copy link
Contributor

pallavi2209 commented May 23, 2019

I would suggest testing this manually as well since the tests are not very thorough.

@pallavi2209
Copy link
Contributor

@jgraff2 Were you able to test this manually?

@jgraff2
Copy link
Contributor Author

jgraff2 commented May 28, 2019

@pallavi2209 the tests here are not very thorough, but the integration tests include an OAuth section that tests the whole sequence. I did run it against those.

However, looking at them again I realized they only cover bulk requests. I will add some by-subject tests. I will also see if I can add tests to make sure it's only requesting a new token when necessary.

@jgraff2
Copy link
Contributor Author

jgraff2 commented Jun 4, 2019

Updated: further restructuring of the collection flow to ensure correct error handling for by-subject collection.

Previously a token login error would return a single error object, but the collection handler would be expecting an array, which would cause an error instead of generating error samples.
The problem was that the previous changes had moved the auxiliary requests outside of the subjects loop, but were still using the error handling flow from before, which relied on the errors being created within the loop.

Now, we only call the request handler if the requests were actually made; otherwise we handle the error and generate error samples in the repeater-level catch.

@jgraff2 jgraff2 requested a review from iamigo June 4, 2019 23:06
@iamigo
Copy link
Contributor

iamigo commented Jun 6, 2019

@jgraff2 don't forget to npm publish!

@jgraff2 jgraff2 merged commit 32ce4cd into master Jun 6, 2019
@jgraff2 jgraff2 deleted the generator-oauth-fix branch June 6, 2019 21:02
@jgraff2
Copy link
Contributor Author

jgraff2 commented Jun 6, 2019

Published to npm.

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

4 participants