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

examples/features/loadbalancing: Add custom lb example #6649

Closed
wants to merge 1 commit into from

Conversation

zasweq
Copy link
Contributor

@zasweq zasweq commented Sep 20, 2023

This PR adds a custom lb example, with a custom lb deployed as the top level balancer of the channel through service config. This scales up a previous example

RELEASE NOTES:

  • examples/features/loadbalancing: Add custom lb example

@zasweq zasweq requested a review from dfawley September 20, 2023 00:52
@zasweq zasweq added the Type: Documentation Documentation or examples label Sep 20, 2023
@zasweq zasweq added this to the 1.59 Release milestone Sep 20, 2023

# custom_round_robin

```
Copy link
Member

Choose a reason for hiding this comment

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

Please add a similar explanation of this case, keeping a similar structure, rather than just starting with output text. "The third client is configured to use a custom ............"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -84,11 +89,24 @@ func main() {

fmt.Println("--- calling helloworld.Greeter/SayHello with round_robin ---")
makeRPCs(roundrobinConn, 10)
// You can also plug in your own custom lb policy, which needs to be
Copy link
Member

Choose a reason for hiding this comment

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

Add a blank line above this for better visual separation please.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

// configurable. This n is configurable. Try changing it and see how the
// behavior changes.
customroundrobinConn, err := grpc.Dial(
fmt.Sprintf("%s:///%s", exampleScheme, exampleServiceName),
Copy link
Member

Choose a reason for hiding this comment

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

Let's pull this out at the top of the function:

target := fmt.Sprintf("%s:///%s", exampleScheme, exampleServiceName)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Good point.

serviceconfig.LoadBalancingConfig `json:"-"`

// N represents how often pick iterations chose the second SubConn
// in the list. Defaults to 3.
Copy link
Member

Choose a reason for hiding this comment

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

Correction: 2

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah good catch. Switched the default in ParseConfig() to 3.

func (customRoundRobinBuilder) Build(cc balancer.ClientConn, bOpts balancer.BuildOptions) balancer.Balancer {
crr := &customRoundRobin{}
baseBuilder := base.NewBalancerBuilder(customRRName, crr, base.Config{HealthCheck: true})
crr.Balancer = baseBuilder.Build(cc, bOpts)
Copy link
Member

Choose a reason for hiding this comment

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

I'm really not a fan of the base balancer...are we sure we want users using it as a template for how to do their own LB policy? I think it would be better to show the actual connection management APIs here instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Switched to petiole policy wrapping pick first after offline discussion.

}

// Following is an example name resolver implementation. Read the name
// resolution example to learn more about it.

type exampleResolverBuilder struct{}
Copy link
Member

Choose a reason for hiding this comment

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

I don't believe this example intends to show off custom resolvers - can this be replaced with the manual resolver instead? We may not have had it when this was created.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Switched.

@@ -123,4 +141,87 @@ func (*exampleResolver) Close() {}

func init() {
resolver.Register(&exampleResolverBuilder{})
balancer.Register(&customRoundRobinBuilder{})
Copy link
Member

Choose a reason for hiding this comment

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

Maybe put the custom LB policy into its own file (or even its own sub-package), since that's how we expect it to be used typically?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

}

func (crr *customRoundRobin) Build(info base.PickerBuildInfo) balancer.Picker {
if len(info.ReadySCs) == 0 {
Copy link
Member

Choose a reason for hiding this comment

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

If you want to make this deterministic, then also do this until len(info.ReadySCs) == 2?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made deterministic in new PR based off child balancer connectivity states.

@dfawley dfawley removed their assignment Sep 21, 2023
@zasweq
Copy link
Contributor Author

zasweq commented Oct 5, 2023

Opened a new PR #6691 containing all the offline discussion decisions such as making Custom Load Balancer a petiole policy wrapping pick_first load balancers. Made a whole new example because this example is outdated and shows concepts like custom resolvers, and new example needs to use endpoints.

@zasweq zasweq closed this Oct 5, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Type: Documentation Documentation or examples
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants