child_policy.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. package rls
  19. import (
  20. "fmt"
  21. "sync/atomic"
  22. "unsafe"
  23. "google.golang.org/grpc/balancer"
  24. "google.golang.org/grpc/balancer/base"
  25. "google.golang.org/grpc/connectivity"
  26. internalgrpclog "google.golang.org/grpc/internal/grpclog"
  27. )
  28. // childPolicyWrapper is a reference counted wrapper around a child policy.
  29. //
  30. // The LB policy maintains a map of these wrappers keyed by the target returned
  31. // by RLS. When a target is seen for the first time, a child policy wrapper is
  32. // created for it and the wrapper is added to the child policy map. Each entry
  33. // in the data cache holds references to the corresponding child policy
  34. // wrappers. The LB policy also holds a reference to the child policy wrapper
  35. // for the default target specified in the LB Policy Configuration
  36. //
  37. // When a cache entry is evicted, it releases references to the child policy
  38. // wrappers that it contains. When all references have been released, the
  39. // wrapper is removed from the child policy map and is destroyed.
  40. //
  41. // The child policy wrapper also caches the connectivity state and most recent
  42. // picker from the child policy. Once the child policy wrapper reports
  43. // TRANSIENT_FAILURE, it will continue reporting that state until it goes READY;
  44. // transitions from TRANSIENT_FAILURE to CONNECTING are ignored.
  45. //
  46. // Whenever a child policy wrapper changes its connectivity state, the LB policy
  47. // returns a new picker to the channel, since the channel may need to re-process
  48. // the picks for queued RPCs.
  49. //
  50. // It is not safe for concurrent access.
  51. type childPolicyWrapper struct {
  52. logger *internalgrpclog.PrefixLogger
  53. target string // RLS target corresponding to this child policy.
  54. refCnt int // Reference count.
  55. // Balancer state reported by the child policy. The RLS LB policy maintains
  56. // these child policies in a BalancerGroup. The state reported by the child
  57. // policy is pushed to the state aggregator (which is also implemented by the
  58. // RLS LB policy) and cached here. See handleChildPolicyStateUpdate() for
  59. // details on how the state aggregation is performed.
  60. //
  61. // While this field is written to by the LB policy, it is read by the picker
  62. // at Pick time. Making this an atomic to enable the picker to read this value
  63. // without a mutex.
  64. state unsafe.Pointer // *balancer.State
  65. }
  66. // newChildPolicyWrapper creates a child policy wrapper for the given target,
  67. // and is initialized with one reference and starts off in CONNECTING state.
  68. func newChildPolicyWrapper(target string) *childPolicyWrapper {
  69. c := &childPolicyWrapper{
  70. target: target,
  71. refCnt: 1,
  72. state: unsafe.Pointer(&balancer.State{
  73. ConnectivityState: connectivity.Connecting,
  74. Picker: base.NewErrPicker(balancer.ErrNoSubConnAvailable),
  75. }),
  76. }
  77. c.logger = internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf("[rls-child-policy-wrapper %s %p] ", c.target, c))
  78. c.logger.Infof("Created")
  79. return c
  80. }
  81. // acquireRef increments the reference count on the child policy wrapper.
  82. func (c *childPolicyWrapper) acquireRef() {
  83. c.refCnt++
  84. }
  85. // releaseRef decrements the reference count on the child policy wrapper. The
  86. // return value indicates whether the released reference was the last one.
  87. func (c *childPolicyWrapper) releaseRef() bool {
  88. c.refCnt--
  89. return c.refCnt == 0
  90. }
  91. // lamify causes the child policy wrapper to return a picker which will always
  92. // fail requests. This is used when the wrapper runs into errors when trying to
  93. // build and parse the child policy configuration.
  94. func (c *childPolicyWrapper) lamify(err error) {
  95. c.logger.Warningf("Entering lame mode: %v", err)
  96. atomic.StorePointer(&c.state, unsafe.Pointer(&balancer.State{
  97. ConnectivityState: connectivity.TransientFailure,
  98. Picker: base.NewErrPicker(err),
  99. }))
  100. }