gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/xds/internal/balancer/priority/balancer_child.go (about)

     1  /*
     2   *
     3   * Copyright 2021 gRPC authors.
     4   *
     5   * Licensed under the Apache License, Version 2.0 (the "License");
     6   * you may not use this file except in compliance with the License.
     7   * You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   *
    17   */
    18  
    19  package priority
    20  
    21  import (
    22  	"gitee.com/ks-custle/core-gm/grpc/balancer"
    23  	"gitee.com/ks-custle/core-gm/grpc/balancer/base"
    24  	"gitee.com/ks-custle/core-gm/grpc/connectivity"
    25  	"gitee.com/ks-custle/core-gm/grpc/resolver"
    26  	"gitee.com/ks-custle/core-gm/grpc/serviceconfig"
    27  )
    28  
    29  type childBalancer struct {
    30  	name   string
    31  	parent *priorityBalancer
    32  	bb     *ignoreResolveNowBalancerBuilder
    33  
    34  	ignoreReresolutionRequests bool
    35  	config                     serviceconfig.LoadBalancingConfig
    36  	rState                     resolver.State
    37  
    38  	started bool
    39  	state   balancer.State
    40  }
    41  
    42  // newChildBalancer creates a child balancer place holder, but doesn't
    43  // build/start the child balancer.
    44  func newChildBalancer(name string, parent *priorityBalancer, bb balancer.Builder) *childBalancer {
    45  	return &childBalancer{
    46  		name:    name,
    47  		parent:  parent,
    48  		bb:      newIgnoreResolveNowBalancerBuilder(bb, false),
    49  		started: false,
    50  		// Start with the connecting state and picker with re-pick error, so
    51  		// that when a priority switch causes this child picked before it's
    52  		// balancing policy is created, a re-pick will happen.
    53  		state: balancer.State{
    54  			ConnectivityState: connectivity.Connecting,
    55  			Picker:            base.NewErrPicker(balancer.ErrNoSubConnAvailable),
    56  		},
    57  	}
    58  }
    59  
    60  // updateBuilder updates builder for the child, but doesn't build.
    61  func (cb *childBalancer) updateBuilder(bb balancer.Builder) {
    62  	cb.bb = newIgnoreResolveNowBalancerBuilder(bb, cb.ignoreReresolutionRequests)
    63  }
    64  
    65  // updateConfig sets childBalancer's config and state, but doesn't send update to
    66  // the child balancer.
    67  func (cb *childBalancer) updateConfig(child *Child, rState resolver.State) {
    68  	cb.ignoreReresolutionRequests = child.IgnoreReresolutionRequests
    69  	cb.config = child.Config.Config
    70  	cb.rState = rState
    71  }
    72  
    73  // start builds the child balancer if it's not already started.
    74  //
    75  // It doesn't do it directly. It asks the balancer group to build it.
    76  func (cb *childBalancer) start() {
    77  	if cb.started {
    78  		return
    79  	}
    80  	cb.started = true
    81  	cb.parent.bg.Add(cb.name, cb.bb)
    82  }
    83  
    84  // sendUpdate sends the addresses and config to the child balancer.
    85  func (cb *childBalancer) sendUpdate() {
    86  	cb.bb.updateIgnoreResolveNow(cb.ignoreReresolutionRequests)
    87  	// TODO: return and aggregate the returned error in the parent.
    88  	err := cb.parent.bg.UpdateClientConnState(cb.name, balancer.ClientConnState{
    89  		ResolverState:  cb.rState,
    90  		BalancerConfig: cb.config,
    91  	})
    92  	if err != nil {
    93  		cb.parent.logger.Warningf("failed to update ClientConn state for child %v: %v", cb.name, err)
    94  	}
    95  }
    96  
    97  // stop stops the child balancer and resets the state.
    98  //
    99  // It doesn't do it directly. It asks the balancer group to remove it.
   100  //
   101  // Note that the underlying balancer group could keep the child in a cache.
   102  func (cb *childBalancer) stop() {
   103  	if !cb.started {
   104  		return
   105  	}
   106  	cb.parent.bg.Remove(cb.name)
   107  	cb.started = false
   108  	cb.state = balancer.State{
   109  		ConnectivityState: connectivity.Connecting,
   110  		Picker:            base.NewErrPicker(balancer.ErrNoSubConnAvailable),
   111  	}
   112  }