why the "loadBalancingPolicy“ must be used when "healthCheckConfig" in grpc

Viewed 146

The code file is: client and server

Doubtful code:

var serviceConfig = `{
    "loadBalancingPolicy": "round_robin",
    "healthCheckConfig": {
        "serviceName": ""
    }
}`

Test steps:

1.Run only one server and one client

2.When using "loadBalancingPolicy": "round_robin", the client can detect the "status=NOT_SERVING" of the server

3.When "loadBalancingPolicy": "round_robin" is deleted, or "pick_first" is used, the "status=NOT_SERVING" of the server cannot be detected on the client side

2 Answers

The health check meaningful when has multiple server addresses. If only has one address, there is no need to check health status. So the load balance policy round_robin is work together with health check.

The round_robin will check health status, so it will send request to READY address one after another.

The pick_first policy not support health check, so it will use first success connectted server. So there will only use specify address for any request.

You can read the document of health check and load balance policy in LB Policies Can Disable Health Checking When Needed.

For debug the client and server, you can add environment variable GRPC_GO_LOG_SEVERITY_LEVEL=info and GRPC_GO_LOG_VERBOSITY_LEVEL=99 for more detail of transport and connection event.

When I read the source code carefully, I understood the internal implementation.

  1. pick_first
  • It implements "balancer.Builder" and "balancer.Balancer" by itself.
  • "ResolverState.Addresses" will only create a SubConn, there is an addrConn in SubConn, create ClientTransport with the first addr.
  • Returns a fixed "balancer.PickResult" each time Pick() is called.
  1. round_robin
  • Pass in the parameter "HealthCheck: true" and return baseBuilder as Builder through "base.NewBalancerBuilder()".
  • Each addr of "ResolverState.Addresses" will create a corresponding SubConn.
  • Each time Pick() is called, change the internal next value, get it from "[]balancer.SubConn", and return a new "balancer.PickResult".
Related