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

[Enhancement] Profile enhance #52779

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

before-Sunrise
Copy link
Contributor

Why I'm doing:

What I'm doing:

Fixes #issue

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
  • I have added documentation for my new feature or new function
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 3.3
    • 3.2
    • 3.1
    • 3.0
    • 2.5

Signed-off-by: before-Sunrise <[email protected]>
Signed-off-by: before-Sunrise <[email protected]>
Signed-off-by: before-Sunrise <[email protected]>
Signed-off-by: before-Sunrise <[email protected]>
return results;

}

public static long getTotalQueriesCount() {
return TOTAL_QUERIES.size();
}
Copy link

Choose a reason for hiding this comment

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

The most risky bug in this code is:
If TOTAL_QUERIES.pollFirst() returns null because the deque is empty or has concurrent modifications, a NullPointerException might occur when calling head.getQueryId().

You can modify the code like this:

private static void removeExpiredQueryDetails() {
    long deleteTime = getCurrentTimeNS() - Config.query_detail_cache_time_nanosecond;

    while (!TOTAL_QUERIES.isEmpty()) {
        QueryDetail head = TOTAL_QUERIES.peekFirst();
        if (head != null && head.getEventTime() < deleteTime) {
            TOTAL_QUERIES.pollFirst(); 
            QUERY_MAP.remove(head.getQueryId());
        } else {
            break;
        }
    }
}

private static class ProfileRequest {
List<String> profileIds;
}
} No newline at end of file
Copy link

Choose a reason for hiding this comment

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

The most risky bug in this code is:
Not handling potential NullPointerException when accessing profileRequest.profileIds.
You can modify the code like this:

public void execute(BaseRequest request, BaseResponse response) throws DdlException {
    String jsonStr = request.getContent();

    Gson gson = new Gson();
    ProfileRequest profileRequest;

    try {
        Type type = new TypeToken<ProfileRequest>() {}.getType();
        profileRequest = gson.fromJson(jsonStr, type);

        // Add a null check for profileRequest to avoid NPE
        if (profileRequest == null || profileRequest.profileIds == null || profileRequest.profileIds.isEmpty()) {
            response.getContent().append("profileIds cannot be empty");
            sendResult(request, response, HttpResponseStatus.BAD_REQUEST);
            return;
        }
    } catch (Exception e) {
        response.getContent().append("not valid parameter");
        sendResult(request, response, HttpResponseStatus.BAD_REQUEST);
        return;
    }

    List<String> profileIds = profileRequest.profileIds;
    List<String> profiles = QueryDetailQueue.getQueryProfilesByQueryIds(profileIds);

    String jsonString = gson.toJson(profiles);
    response.getContent().append(jsonString);
    sendResult(request, response);
}

This fix ensures that if gson.fromJson returns null, the method will handle it properly by attaching an error message and returning an appropriate HTTP response.

Signed-off-by: before-Sunrise <[email protected]>
Signed-off-by: before-Sunrise <[email protected]>
Copy link

sonarcloud bot commented Nov 11, 2024

Copy link

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

Copy link

[FE Incremental Coverage Report]

pass : 46 / 46 (100.00%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/qe/QueryDetail.java 6 6 100.00% []
🔵 com/starrocks/qe/QueryDetailQueue.java 13 13 100.00% []
🔵 com/starrocks/qe/StmtExecutor.java 2 2 100.00% []
🔵 com/starrocks/http/rest/QueryDetailProfileAction.java 24 24 100.00% []
🔵 com/starrocks/http/HttpServer.java 1 1 100.00% []

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.

1 participant