k8s.io/apiserver@v0.29.3/pkg/storage/cacher/ready.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package cacher 18 19 import ( 20 "context" 21 "fmt" 22 "sync" 23 ) 24 25 type status int 26 27 const ( 28 Pending status = iota 29 Ready 30 Stopped 31 ) 32 33 // ready is a three state condition variable that blocks until is Ready if is not Stopped. 34 // Its initial state is Pending and its state machine diagram is as follow. 35 // 36 // Pending <------> Ready -----> Stopped 37 // 38 // | ^ 39 // └---------------------------┘ 40 type ready struct { 41 state status // represent the state of the variable 42 generation int // represent the number of times we have transtioned to ready 43 lock sync.RWMutex // protect the state and generation variables 44 restartLock sync.Mutex // protect the transition from ready to pending where the channel is recreated 45 waitCh chan struct{} // blocks until is ready or stopped 46 } 47 48 func newReady() *ready { 49 return &ready{ 50 waitCh: make(chan struct{}), 51 state: Pending, 52 } 53 } 54 55 // done close the channel once the state is Ready or Stopped 56 func (r *ready) done() chan struct{} { 57 r.restartLock.Lock() 58 defer r.restartLock.Unlock() 59 return r.waitCh 60 } 61 62 // wait blocks until it is Ready or Stopped, it returns an error if is Stopped. 63 func (r *ready) wait(ctx context.Context) error { 64 _, err := r.waitAndReadGeneration(ctx) 65 return err 66 } 67 68 // waitAndReadGenration blocks until it is Ready or Stopped and returns number 69 // of times we entered ready state if Ready and error otherwise. 70 func (r *ready) waitAndReadGeneration(ctx context.Context) (int, error) { 71 for { 72 // r.done() only blocks if state is Pending 73 select { 74 case <-ctx.Done(): 75 return 0, ctx.Err() 76 case <-r.done(): 77 } 78 79 r.lock.RLock() 80 switch r.state { 81 case Pending: 82 // since we allow to switch between the states Pending and Ready 83 // if there is a quick transition from Pending -> Ready -> Pending 84 // a process that was waiting can get unblocked and see a Pending 85 // state again. If the state is Pending we have to wait again to 86 // avoid an inconsistent state on the system, with some processes not 87 // waiting despite the state moved back to Pending. 88 r.lock.RUnlock() 89 case Ready: 90 generation := r.generation 91 r.lock.RUnlock() 92 return generation, nil 93 case Stopped: 94 r.lock.RUnlock() 95 return 0, fmt.Errorf("apiserver cacher is stopped") 96 default: 97 r.lock.RUnlock() 98 return 0, fmt.Errorf("unexpected apiserver cache state: %v", r.state) 99 } 100 } 101 } 102 103 // check returns true only if it is Ready. 104 func (r *ready) check() bool { 105 _, ok := r.checkAndReadGeneration() 106 return ok 107 } 108 109 // checkAndReadGeneration returns the current generation and whether it is Ready. 110 func (r *ready) checkAndReadGeneration() (int, bool) { 111 r.lock.RLock() 112 defer r.lock.RUnlock() 113 return r.generation, r.state == Ready 114 } 115 116 // set the state to Pending (false) or Ready (true), it does not have effect if the state is Stopped. 117 func (r *ready) set(ok bool) { 118 r.lock.Lock() 119 defer r.lock.Unlock() 120 if r.state == Stopped { 121 return 122 } 123 if ok && r.state == Pending { 124 r.state = Ready 125 r.generation++ 126 select { 127 case <-r.waitCh: 128 default: 129 close(r.waitCh) 130 } 131 } else if !ok && r.state == Ready { 132 // creating the waitCh can be racy if 133 // something enter the wait() method 134 select { 135 case <-r.waitCh: 136 r.restartLock.Lock() 137 r.waitCh = make(chan struct{}) 138 r.restartLock.Unlock() 139 default: 140 } 141 r.state = Pending 142 } 143 } 144 145 // stop the condition variable and set it as Stopped. This state is irreversible. 146 func (r *ready) stop() { 147 r.lock.Lock() 148 defer r.lock.Unlock() 149 if r.state != Stopped { 150 r.state = Stopped 151 } 152 select { 153 case <-r.waitCh: 154 default: 155 close(r.waitCh) 156 } 157 }