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

grpc: add caller info to client and wrap innerClient #8704

Open
wants to merge 21 commits into
base: master
Choose a base branch
from

Conversation

okJiang
Copy link
Member

@okJiang okJiang commented Oct 15, 2024

What problem does this PR solve?

Issue Number: Ref #8593

What is changed and how does it work?

Check List

Tests

  • Unit test
  • Integration test

Release note

None.

Copy link
Contributor

ti-chi-bot bot commented Oct 15, 2024

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. dco-signoff: yes Indicates the PR's author has signed the dco. labels Oct 15, 2024
Copy link
Contributor

ti-chi-bot bot commented Oct 15, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign zhouqiang-cl for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Oct 15, 2024
@okJiang
Copy link
Member Author

okJiang commented Oct 18, 2024

/retest

Signed-off-by: okJiang <[email protected]>
Signed-off-by: okJiang <[email protected]>
Signed-off-by: okJiang <[email protected]>
@okJiang
Copy link
Member Author

okJiang commented Oct 18, 2024

/retest

Copy link

codecov bot commented Oct 18, 2024

Codecov Report

Attention: Patch coverage is 85.07463% with 50 lines in your changes missing coverage. Please review.

Project coverage is 75.73%. Comparing base (2637331) to head (550c343).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8704      +/-   ##
==========================================
- Coverage   75.80%   75.73%   -0.07%     
==========================================
  Files         461      461              
  Lines       72315    72072     -243     
==========================================
- Hits        54817    54584     -233     
- Misses      14000    14002       +2     
+ Partials     3498     3486      -12     
Flag Coverage Δ
unittests 75.73% <85.07%> (-0.07%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

@ti-chi-bot ti-chi-bot bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Oct 25, 2024
Signed-off-by: okJiang <[email protected]>
Signed-off-by: okJiang <[email protected]>
Signed-off-by: okJiang <[email protected]>
Signed-off-by: okJiang <[email protected]>
Signed-off-by: okJiang <[email protected]>
@okJiang
Copy link
Member Author

okJiang commented Nov 6, 2024

/retest

@okJiang okJiang marked this pull request as ready for review November 6, 2024 08:56
@okJiang okJiang changed the title [WIP] grpc: add caller info and wrap innerClient grpc: add caller info and wrap innerClient Nov 6, 2024
@ti-chi-bot ti-chi-bot bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 6, 2024
@okJiang
Copy link
Member Author

okJiang commented Nov 6, 2024

/cc @JmPotato

@ti-chi-bot ti-chi-bot bot requested a review from JmPotato November 6, 2024 08:57
client/caller/constant.go Outdated Show resolved Hide resolved
client/caller/constant.go Outdated Show resolved Hide resolved
client/client.go Outdated Show resolved Hide resolved
wg sync.WaitGroup
tlsCfg *tls.Config
option *option
inner *innerClient
Copy link
Member

Choose a reason for hiding this comment

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

A nested struct without introducing the field inner may reduce the code changes. But I'm not sure whether the pointer here still works, maybe we can have a try.

Copy link
Member Author

Choose a reason for hiding this comment

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

But I'm not sure whether the pointer here still works, maybe we can have a try.

I tried it before and indeed encountered some issues, so I wrapped the inner client.

Copy link
Member Author

@okJiang okJiang Nov 8, 2024

Choose a reason for hiding this comment

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

And I think wrapping innerclient also helps to reduce the amount of code in client.go, as the code lines in client.go are a bit excessive.

Signed-off-by: okJiang <[email protected]>
@okJiang okJiang changed the title grpc: add caller info and wrap innerClient grpc: add caller info to client and wrap innerClient Nov 8, 2024
@ti-chi-bot ti-chi-bot bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 8, 2024
Signed-off-by: okJiang <[email protected]>
WithCallerID(callerID caller.ID) RPCClient
// WithCallerComponent returns a new RPCClient with the specified caller component.
// Caller component refers to the components within the process.
WithCallerComponent(callerComponent caller.Component) RPCClient
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we provide an automatic method to ingest the CallerID and Component? Like:

package processutil

import "os"

// GetCallerID returns the name of the currently running process
func GetCallerID() string {
	return os.Args[0]
}


func GetTheComponent() string {
	// Get the program counter for the calling function
	pc, _, _, ok := runtime.Caller(1)
	if !ok {
		return "unknown"
	}

	// Retrieve the full function name, including the package path
	fullFuncName := runtime.FuncForPC(pc).Name()

	// Find the last slash, which separates the package path from the function name
	lastSlash := strings.LastIndex(fullFuncName, "/")
	if lastSlash == -1 {
		// No slash, use the last period instead, which separates the package and function
		lastSlash = strings.LastIndex(fullFuncName, ".")
	}
	// Extract the package name
	return fullFuncName[:lastSlash]
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Very good suggestion👍, I will consider if there are any flaws

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps the HTTP client could also adopt this method.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have added these two functions to this pr. In the default situation, CreateClient will set the callerID and callerComponent by these two functions. I removed callerID input from CreateClient and keep the callerComponent to allow caller set component.

And I expose the upperLayer to allow user to set it. For example, client-go can set it to catch the real caller in TiDB.

How do you think about these changes?

Copy link
Contributor

@nolouch nolouch left a comment

Choose a reason for hiding this comment

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

rest lgtm

svrUrls: svrAddrs,
tlsCfg: tlsCfg,
option: newOption(),
callerID: caller.GetCallerID(),
Copy link
Contributor

Choose a reason for hiding this comment

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

why not move to innerClient, and make callerComponent as ClientOption?

Copy link
Member Author

Choose a reason for hiding this comment

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

What you said is exactly what I thought at the beginning. You can see it here

The ClientOption is not actually universal; if we want to apply it across all interfaces, we need to further organize the various options. Therefore, I adopted @JmPotato 's suggestion. In my view, I can accept both implementation methods.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think @JmPotato's suggestion also doesn't require changing the function signature. However, if you intend to require the client to set the component name, I'm fine with changing it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I intend to require the client to set it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dco-signoff: yes Indicates the PR's author has signed the dco. release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants