google.golang.org/grpc@v1.74.2/internal/balancer/gracefulswitch/gracefulswitch.go (about)

     1  /*
     2   *
     3   * Copyright 2022 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 gracefulswitch implements a graceful switch load balancer.
    20  package gracefulswitch
    21  
    22  import (
    23  	"errors"
    24  	"fmt"
    25  	"sync"
    26  
    27  	"google.golang.org/grpc/balancer"
    28  	"google.golang.org/grpc/balancer/base"
    29  	"google.golang.org/grpc/connectivity"
    30  	"google.golang.org/grpc/resolver"
    31  )
    32  
    33  var errBalancerClosed = errors.New("gracefulSwitchBalancer is closed")
    34  var _ balancer.Balancer = (*Balancer)(nil)
    35  
    36  // NewBalancer returns a graceful switch Balancer.
    37  func NewBalancer(cc balancer.ClientConn, opts balancer.BuildOptions) *Balancer {
    38  	return &Balancer{
    39  		cc:    cc,
    40  		bOpts: opts,
    41  	}
    42  }
    43  
    44  // Balancer is a utility to gracefully switch from one balancer to
    45  // a new balancer. It implements the balancer.Balancer interface.
    46  type Balancer struct {
    47  	bOpts balancer.BuildOptions
    48  	cc    balancer.ClientConn
    49  
    50  	// mu protects the following fields and all fields within balancerCurrent
    51  	// and balancerPending. mu does not need to be held when calling into the
    52  	// child balancers, as all calls into these children happen only as a direct
    53  	// result of a call into the gracefulSwitchBalancer, which are also
    54  	// guaranteed to be synchronous. There is one exception: an UpdateState call
    55  	// from a child balancer when current and pending are populated can lead to
    56  	// calling Close() on the current. To prevent that racing with an
    57  	// UpdateSubConnState from the channel, we hold currentMu during Close and
    58  	// UpdateSubConnState calls.
    59  	mu              sync.Mutex
    60  	balancerCurrent *balancerWrapper
    61  	balancerPending *balancerWrapper
    62  	closed          bool // set to true when this balancer is closed
    63  
    64  	// currentMu must be locked before mu. This mutex guards against this
    65  	// sequence of events: UpdateSubConnState() called, finds the
    66  	// balancerCurrent, gives up lock, updateState comes in, causes Close() on
    67  	// balancerCurrent before the UpdateSubConnState is called on the
    68  	// balancerCurrent.
    69  	currentMu sync.Mutex
    70  }
    71  
    72  // swap swaps out the current lb with the pending lb and updates the ClientConn.
    73  // The caller must hold gsb.mu.
    74  func (gsb *Balancer) swap() {
    75  	gsb.cc.UpdateState(gsb.balancerPending.lastState)
    76  	cur := gsb.balancerCurrent
    77  	gsb.balancerCurrent = gsb.balancerPending
    78  	gsb.balancerPending = nil
    79  	go func() {
    80  		gsb.currentMu.Lock()
    81  		defer gsb.currentMu.Unlock()
    82  		cur.Close()
    83  	}()
    84  }
    85  
    86  // Helper function that checks if the balancer passed in is current or pending.
    87  // The caller must hold gsb.mu.
    88  func (gsb *Balancer) balancerCurrentOrPending(bw *balancerWrapper) bool {
    89  	return bw == gsb.balancerCurrent || bw == gsb.balancerPending
    90  }
    91  
    92  // SwitchTo initializes the graceful switch process, which completes based on
    93  // connectivity state changes on the current/pending balancer. Thus, the switch
    94  // process is not complete when this method returns. This method must be called
    95  // synchronously alongside the rest of the balancer.Balancer methods this
    96  // Graceful Switch Balancer implements.
    97  //
    98  // Deprecated: use ParseConfig and pass a parsed config to UpdateClientConnState
    99  // to cause the Balancer to automatically change to the new child when necessary.
   100  func (gsb *Balancer) SwitchTo(builder balancer.Builder) error {
   101  	_, err := gsb.switchTo(builder)
   102  	return err
   103  }
   104  
   105  func (gsb *Balancer) switchTo(builder balancer.Builder) (*balancerWrapper, error) {
   106  	gsb.mu.Lock()
   107  	if gsb.closed {
   108  		gsb.mu.Unlock()
   109  		return nil, errBalancerClosed
   110  	}
   111  	bw := &balancerWrapper{
   112  		ClientConn: gsb.cc,
   113  		builder:    builder,
   114  		gsb:        gsb,
   115  		lastState: balancer.State{
   116  			ConnectivityState: connectivity.Connecting,
   117  			Picker:            base.NewErrPicker(balancer.ErrNoSubConnAvailable),
   118  		},
   119  		subconns: make(map[balancer.SubConn]bool),
   120  	}
   121  	balToClose := gsb.balancerPending // nil if there is no pending balancer
   122  	if gsb.balancerCurrent == nil {
   123  		gsb.balancerCurrent = bw
   124  	} else {
   125  		gsb.balancerPending = bw
   126  	}
   127  	gsb.mu.Unlock()
   128  	balToClose.Close()
   129  	// This function takes a builder instead of a balancer because builder.Build
   130  	// can call back inline, and this utility needs to handle the callbacks.
   131  	newBalancer := builder.Build(bw, gsb.bOpts)
   132  	if newBalancer == nil {
   133  		// This is illegal and should never happen; we clear the balancerWrapper
   134  		// we were constructing if it happens to avoid a potential panic.
   135  		gsb.mu.Lock()
   136  		if gsb.balancerPending != nil {
   137  			gsb.balancerPending = nil
   138  		} else {
   139  			gsb.balancerCurrent = nil
   140  		}
   141  		gsb.mu.Unlock()
   142  		return nil, balancer.ErrBadResolverState
   143  	}
   144  
   145  	// This write doesn't need to take gsb.mu because this field never gets read
   146  	// or written to on any calls from the current or pending. Calls from grpc
   147  	// to this balancer are guaranteed to be called synchronously, so this
   148  	// bw.Balancer field will never be forwarded to until this SwitchTo()
   149  	// function returns.
   150  	bw.Balancer = newBalancer
   151  	return bw, nil
   152  }
   153  
   154  // Returns nil if the graceful switch balancer is closed.
   155  func (gsb *Balancer) latestBalancer() *balancerWrapper {
   156  	gsb.mu.Lock()
   157  	defer gsb.mu.Unlock()
   158  	if gsb.balancerPending != nil {
   159  		return gsb.balancerPending
   160  	}
   161  	return gsb.balancerCurrent
   162  }
   163  
   164  // UpdateClientConnState forwards the update to the latest balancer created.
   165  //
   166  // If the state's BalancerConfig is the config returned by a call to
   167  // gracefulswitch.ParseConfig, then this function will automatically SwitchTo
   168  // the balancer indicated by the config before forwarding its config to it, if
   169  // necessary.
   170  func (gsb *Balancer) UpdateClientConnState(state balancer.ClientConnState) error {
   171  	// The resolver data is only relevant to the most recent LB Policy.
   172  	balToUpdate := gsb.latestBalancer()
   173  	gsbCfg, ok := state.BalancerConfig.(*lbConfig)
   174  	if ok {
   175  		// Switch to the child in the config unless it is already active.
   176  		if balToUpdate == nil || gsbCfg.childBuilder.Name() != balToUpdate.builder.Name() {
   177  			var err error
   178  			balToUpdate, err = gsb.switchTo(gsbCfg.childBuilder)
   179  			if err != nil {
   180  				return fmt.Errorf("could not switch to new child balancer: %w", err)
   181  			}
   182  		}
   183  		// Unwrap the child balancer's config.
   184  		state.BalancerConfig = gsbCfg.childConfig
   185  	}
   186  
   187  	if balToUpdate == nil {
   188  		return errBalancerClosed
   189  	}
   190  
   191  	// Perform this call without gsb.mu to prevent deadlocks if the child calls
   192  	// back into the channel. The latest balancer can never be closed during a
   193  	// call from the channel, even without gsb.mu held.
   194  	return balToUpdate.UpdateClientConnState(state)
   195  }
   196  
   197  // ResolverError forwards the error to the latest balancer created.
   198  func (gsb *Balancer) ResolverError(err error) {
   199  	// The resolver data is only relevant to the most recent LB Policy.
   200  	balToUpdate := gsb.latestBalancer()
   201  	if balToUpdate == nil {
   202  		gsb.cc.UpdateState(balancer.State{
   203  			ConnectivityState: connectivity.TransientFailure,
   204  			Picker:            base.NewErrPicker(err),
   205  		})
   206  		return
   207  	}
   208  	// Perform this call without gsb.mu to prevent deadlocks if the child calls
   209  	// back into the channel. The latest balancer can never be closed during a
   210  	// call from the channel, even without gsb.mu held.
   211  	balToUpdate.ResolverError(err)
   212  }
   213  
   214  // ExitIdle forwards the call to the latest balancer created.
   215  //
   216  // If the latest balancer does not support ExitIdle, the subConns are
   217  // re-connected to manually.
   218  func (gsb *Balancer) ExitIdle() {
   219  	balToUpdate := gsb.latestBalancer()
   220  	if balToUpdate == nil {
   221  		return
   222  	}
   223  	// There is no need to protect this read with a mutex, as the write to the
   224  	// Balancer field happens in SwitchTo, which completes before this can be
   225  	// called.
   226  	balToUpdate.ExitIdle()
   227  }
   228  
   229  // updateSubConnState forwards the update to the appropriate child.
   230  func (gsb *Balancer) updateSubConnState(sc balancer.SubConn, state balancer.SubConnState, cb func(balancer.SubConnState)) {
   231  	gsb.currentMu.Lock()
   232  	defer gsb.currentMu.Unlock()
   233  	gsb.mu.Lock()
   234  	// Forward update to the appropriate child.  Even if there is a pending
   235  	// balancer, the current balancer should continue to get SubConn updates to
   236  	// maintain the proper state while the pending is still connecting.
   237  	var balToUpdate *balancerWrapper
   238  	if gsb.balancerCurrent != nil && gsb.balancerCurrent.subconns[sc] {
   239  		balToUpdate = gsb.balancerCurrent
   240  	} else if gsb.balancerPending != nil && gsb.balancerPending.subconns[sc] {
   241  		balToUpdate = gsb.balancerPending
   242  	}
   243  	if balToUpdate == nil {
   244  		// SubConn belonged to a stale lb policy that has not yet fully closed,
   245  		// or the balancer was already closed.
   246  		gsb.mu.Unlock()
   247  		return
   248  	}
   249  	if state.ConnectivityState == connectivity.Shutdown {
   250  		delete(balToUpdate.subconns, sc)
   251  	}
   252  	gsb.mu.Unlock()
   253  	if cb != nil {
   254  		cb(state)
   255  	} else {
   256  		balToUpdate.UpdateSubConnState(sc, state)
   257  	}
   258  }
   259  
   260  // UpdateSubConnState forwards the update to the appropriate child.
   261  func (gsb *Balancer) UpdateSubConnState(sc balancer.SubConn, state balancer.SubConnState) {
   262  	gsb.updateSubConnState(sc, state, nil)
   263  }
   264  
   265  // Close closes any active child balancers.
   266  func (gsb *Balancer) Close() {
   267  	gsb.mu.Lock()
   268  	gsb.closed = true
   269  	currentBalancerToClose := gsb.balancerCurrent
   270  	gsb.balancerCurrent = nil
   271  	pendingBalancerToClose := gsb.balancerPending
   272  	gsb.balancerPending = nil
   273  	gsb.mu.Unlock()
   274  
   275  	currentBalancerToClose.Close()
   276  	pendingBalancerToClose.Close()
   277  }
   278  
   279  // balancerWrapper wraps a balancer.Balancer, and overrides some Balancer
   280  // methods to help cleanup SubConns created by the wrapped balancer.
   281  //
   282  // It implements the balancer.ClientConn interface and is passed down in that
   283  // capacity to the wrapped balancer. It maintains a set of subConns created by
   284  // the wrapped balancer and calls from the latter to create/update/shutdown
   285  // SubConns update this set before being forwarded to the parent ClientConn.
   286  // State updates from the wrapped balancer can result in invocation of the
   287  // graceful switch logic.
   288  type balancerWrapper struct {
   289  	balancer.ClientConn
   290  	balancer.Balancer
   291  	gsb     *Balancer
   292  	builder balancer.Builder
   293  
   294  	lastState balancer.State
   295  	subconns  map[balancer.SubConn]bool // subconns created by this balancer
   296  }
   297  
   298  // Close closes the underlying LB policy and shuts down the subconns it
   299  // created. bw must not be referenced via balancerCurrent or balancerPending in
   300  // gsb when called. gsb.mu must not be held.  Does not panic with a nil
   301  // receiver.
   302  func (bw *balancerWrapper) Close() {
   303  	// before Close is called.
   304  	if bw == nil {
   305  		return
   306  	}
   307  	// There is no need to protect this read with a mutex, as Close() is
   308  	// impossible to be called concurrently with the write in SwitchTo(). The
   309  	// callsites of Close() for this balancer in Graceful Switch Balancer will
   310  	// never be called until SwitchTo() returns.
   311  	bw.Balancer.Close()
   312  	bw.gsb.mu.Lock()
   313  	for sc := range bw.subconns {
   314  		sc.Shutdown()
   315  	}
   316  	bw.gsb.mu.Unlock()
   317  }
   318  
   319  func (bw *balancerWrapper) UpdateState(state balancer.State) {
   320  	// Hold the mutex for this entire call to ensure it cannot occur
   321  	// concurrently with other updateState() calls. This causes updates to
   322  	// lastState and calls to cc.UpdateState to happen atomically.
   323  	bw.gsb.mu.Lock()
   324  	defer bw.gsb.mu.Unlock()
   325  	bw.lastState = state
   326  
   327  	if !bw.gsb.balancerCurrentOrPending(bw) {
   328  		return
   329  	}
   330  
   331  	if bw == bw.gsb.balancerCurrent {
   332  		// In the case that the current balancer exits READY, and there is a pending
   333  		// balancer, you can forward the pending balancer's cached State up to
   334  		// ClientConn and swap the pending into the current. This is because there
   335  		// is no reason to gracefully switch from and keep using the old policy as
   336  		// the ClientConn is not connected to any backends.
   337  		if state.ConnectivityState != connectivity.Ready && bw.gsb.balancerPending != nil {
   338  			bw.gsb.swap()
   339  			return
   340  		}
   341  		// Even if there is a pending balancer waiting to be gracefully switched to,
   342  		// continue to forward current balancer updates to the Client Conn. Ignoring
   343  		// state + picker from the current would cause undefined behavior/cause the
   344  		// system to behave incorrectly from the current LB policies perspective.
   345  		// Also, the current LB is still being used by grpc to choose SubConns per
   346  		// RPC, and thus should use the most updated form of the current balancer.
   347  		bw.gsb.cc.UpdateState(state)
   348  		return
   349  	}
   350  	// This method is now dealing with a state update from the pending balancer.
   351  	// If the current balancer is currently in a state other than READY, the new
   352  	// policy can be swapped into place immediately. This is because there is no
   353  	// reason to gracefully switch from and keep using the old policy as the
   354  	// ClientConn is not connected to any backends.
   355  	if state.ConnectivityState != connectivity.Connecting || bw.gsb.balancerCurrent.lastState.ConnectivityState != connectivity.Ready {
   356  		bw.gsb.swap()
   357  	}
   358  }
   359  
   360  func (bw *balancerWrapper) NewSubConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (balancer.SubConn, error) {
   361  	bw.gsb.mu.Lock()
   362  	if !bw.gsb.balancerCurrentOrPending(bw) {
   363  		bw.gsb.mu.Unlock()
   364  		return nil, fmt.Errorf("%T at address %p that called NewSubConn is deleted", bw, bw)
   365  	}
   366  	bw.gsb.mu.Unlock()
   367  
   368  	var sc balancer.SubConn
   369  	oldListener := opts.StateListener
   370  	opts.StateListener = func(state balancer.SubConnState) { bw.gsb.updateSubConnState(sc, state, oldListener) }
   371  	sc, err := bw.gsb.cc.NewSubConn(addrs, opts)
   372  	if err != nil {
   373  		return nil, err
   374  	}
   375  	bw.gsb.mu.Lock()
   376  	if !bw.gsb.balancerCurrentOrPending(bw) { // balancer was closed during this call
   377  		sc.Shutdown()
   378  		bw.gsb.mu.Unlock()
   379  		return nil, fmt.Errorf("%T at address %p that called NewSubConn is deleted", bw, bw)
   380  	}
   381  	bw.subconns[sc] = true
   382  	bw.gsb.mu.Unlock()
   383  	return sc, nil
   384  }
   385  
   386  func (bw *balancerWrapper) ResolveNow(opts resolver.ResolveNowOptions) {
   387  	// Ignore ResolveNow requests from anything other than the most recent
   388  	// balancer, because older balancers were already removed from the config.
   389  	if bw != bw.gsb.latestBalancer() {
   390  		return
   391  	}
   392  	bw.gsb.cc.ResolveNow(opts)
   393  }
   394  
   395  func (bw *balancerWrapper) RemoveSubConn(sc balancer.SubConn) {
   396  	// Note: existing third party balancers may call this, so it must remain
   397  	// until RemoveSubConn is fully removed.
   398  	sc.Shutdown()
   399  }
   400  
   401  func (bw *balancerWrapper) UpdateAddresses(sc balancer.SubConn, addrs []resolver.Address) {
   402  	bw.gsb.mu.Lock()
   403  	if !bw.gsb.balancerCurrentOrPending(bw) {
   404  		bw.gsb.mu.Unlock()
   405  		return
   406  	}
   407  	bw.gsb.mu.Unlock()
   408  	bw.gsb.cc.UpdateAddresses(sc, addrs)
   409  }