Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Detaching request id from SqlRequest #81

Merged

Conversation

arsen-es
Copy link
Contributor

Issue #, if available: #34

Description of changes: Removed id field from SqlRequest class. Using logging library's ThreadContext to store the request id as soon as we get it, and passing it around to make it available in case new threads are started from the main one. Provided a utility class to make the access to the ThreadContext simpler.

The current layout pattern of elasticsearch log messages is [%d{ISO8601}][%-5p][%-25c{1.}] [%node_name]%marker %m%n, if we pre-pend [%X{request_id}] all the messages will automatically be pre-pended with the request id. However, the plugin code does not manage the log configuration, and I didn't want to modify it at run-time.

How tested: Made some requests on local cluster, made sure request id is getting properly populated. Tested for tasks running on worker threads as well.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@arsen-es arsen-es marked this pull request as ready for review June 12, 2019 23:30
/**
* The key of the request id in the context map
*/
public static final String REQUEST_ID_KEY = "request_id";
Copy link
Member

Choose a reason for hiding this comment

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

private may be better? Since this is more like your internal detail rather than a public constant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agree, no reason to keep it public. I was trying to do something fancier to basically check if the elastic's thread context has the id and get it from there, that's why made this public, but that would be a different change anyway (if we ever make it), changing this to private.

@After
public void cleanUpContext() {

ThreadContext.clearMap();
Copy link
Member

Choose a reason for hiding this comment

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

When this or cleanup of context happen in our plugin? Or we just generate an ID for each thread and keep using it forever?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this never happens, it's just a way to make the test cases independent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

also, we should generate only once for the main (accepting the request thread), and pass it over to other threads created from within that one. As long as the threads are working on the same request, they should share the same id.

Copy link
Member

Choose a reason for hiding this comment

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

this never happens, it's just a way to make the test cases independent.

Does this mean for all the following request served by the thread, we keep using same ID?

Copy link
Member

Choose a reason for hiding this comment

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

also, we should generate only once for the main (accepting the request thread), and pass it over to other threads created from within that one. As long as the threads are working on the same request, they should share the same id.

Need more context how this works. So you're saying we generate an ID for each transport thread only once. Then I saw you "preserve" the context when you pass it to worker thread. So for all the logging on the path (for a transport thread), same ID would be in use always between requests?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I see your point now. You're right, I need to clean up the request id after the processing is done or overwrite it when the processing starts. It should be per request, not per thread, and with current code it won't work for re-used threads.

@arsen-es arsen-es requested review from dai-chen and abbashus June 13, 2019 18:31
Copy link
Contributor

@abbashus abbashus left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thanks!

Copy link
Member

@dai-chen dai-chen left a comment

Choose a reason for hiding this comment

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

Thanks for the changes!

@arsen-es arsen-es merged commit d0b51bd into opendistro-for-elasticsearch:master Jun 13, 2019
zhongnansu pushed a commit to zhongnansu/sql that referenced this pull request Jun 14, 2019
*Issue:* opendistro-for-elasticsearch#34 

*Description of changes:* Removed id field from SqlRequest class. Using logging library's ThreadContext to store the request id as soon as we get it, and passing it around to make it available in case new threads are started from the main one. Provided a utility class to make the access to the ThreadContext simpler.

The current layout pattern of elasticsearch log messages is [%d{ISO8601}][%-5p][%-25c{1.}] [%node_name]%marker %m%n, if we pre-pend [%X{request_id}] all the messages will automatically be pre-pended with the request id. However, the plugin code does not manage the log configuration, and I didn't want to modify it at run-time.

*How tested:* Made some requests on local cluster, made sure request id is getting properly populated. Tested for tasks running on worker threads as well.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants