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

fix(auth): propagate MDC across the thread boundary for pipeline execution #560

Merged
merged 4 commits into from
May 24, 2019

Conversation

marchello2000
Copy link
Contributor

previous change (#555) moved orca invocation
to an executor thread without propagating MDC context.

…ution

previous change (spinnaker#555) moved orca invocation
to an executor thread without propagating MDC context.
@@ -166,7 +167,20 @@ public void startPipeline(Pipeline pipeline, TriggerSource triggerSource) {

private void triggerPipeline(Pipeline pipeline, TriggerSource triggerSource)
throws RejectedExecutionException {
executorService.submit(() -> triggerPipelineImpl(pipeline, triggerSource));
final Map<String, String> mdc = MDC.getCopyOfContextMap();
Copy link
Contributor

Choose a reason for hiding this comment

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

Curious on your thought process behind this vs propagate()?

Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any sort of test we could add around this ...

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 added a test, but then it actually didn't work. The problem is that the test is using a directExecutor and then it actually calls from the same thread... I will add a test but i will just need more time to figure out how to block on that thread in spock...

propagate() doesn't actually carry the MDC across thread boundary, annoyingly - maybe that was the intent but i don't see how it would actually do that short of what I am doing

Copy link
Contributor

Choose a reason for hiding this comment

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

propagate() should be able to propagate across threads, it's not going to blindly propagate all MDC but that shouldn't matter?

But you'd need to call propagate() outside the submit() and pass the resulting Callable<> into it.

My go to way of testing something like this would to do an assert within the mock ... roughly something like:

1 * orca.blah() { 
  assert MDC.get("blah") == "what I expect"
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for tip - added a test and used propagate - much better this way!

MDC.remove(AuthenticatedRequest.Header.ACCOUNTS.header)
MDC.remove(AuthenticatedRequest.Header.USER.header)

// Wait for orce to actually be called (happens on separate thread)
Copy link
Contributor

Choose a reason for hiding this comment

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

s/orce/Wait for trigger

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

try {
triggerWithCapturedContext.call();
} catch (Exception e) {
// This shouldn't happen since all exceptions are handled in triggerPipelineImpl...
Copy link
Contributor

Choose a reason for hiding this comment

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

uhh ... can we just have a test that asserts exceptions are caught and not sprinkle this all over the place?

Copy link
Contributor

Choose a reason for hiding this comment

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

assuming the above ... then can we just pass triggerWithCapturedContext into executorService.submit()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, that's much nicer, thanks!

@marchello2000 marchello2000 merged commit d2a4076 into spinnaker:master May 24, 2019
@marchello2000 marchello2000 deleted the mark/propagate_mdc branch May 24, 2019 22:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants