| 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 |
"time" |
| 24 |
|
| 25 |
"k8s.io/utils/clock" |
| 26 |
) |
| 27 |
|
| 28 |
type status int |
| 29 |
|
| 30 |
const ( |
| 31 |
Pending status = iota |
| 32 |
Ready |
| 33 |
Stopped |
| 34 |
) |
| 35 |
|
| 36 |
// ready is a three state condition variable that blocks until is Ready if is not Stopped. |
| 37 |
// Its initial state is Pending and its state machine diagram is as follow. |
| 38 |
// |
| 39 |
// Pending <------> Ready -----> Stopped |
| 40 |
// |
| 41 |
// | ^ |
| 42 |
// └---------------------------┘ |
| 43 |
type ready struct { |
| 44 |
state status // represent the state of the variable |
| 45 |
lastErr error |
| 46 |
generation int // represent the number of times we have transtioned to ready |
| 47 |
lock sync.RWMutex // protect the state and generation variables |
| 48 |
restartLock sync.Mutex // protect the transition from ready to pending where the channel is recreated |
| 49 |
waitCh chan struct{} // blocks until is ready or stopped |
| 50 |
|
| 51 |
clock clock.Clock |
| 52 |
lastStateChangeTime time.Time |
| 53 |
} |
| 54 |
|
| 55 |
func newReady(c clock.Clock) *ready { |
| 56 |
r := &ready{ |
| 57 |
waitCh: make(chan struct{}), |
| 58 |
state: Pending, |
| 59 |
clock: c, |
| 60 |
} |
| 61 |
r.updateLastStateChangeTimeLocked() |
| 62 |
|
| 63 |
return r |
| 64 |
} |
| 65 |
|
| 66 |
// done close the channel once the state is Ready or Stopped |
| 67 |
func (r *ready) done() chan struct{} { |
| 68 |
r.restartLock.Lock() |
| 69 |
defer r.restartLock.Unlock() |
| 70 |
return r.waitCh |
| 71 |
} |
| 72 |
|
| 73 |
// wait blocks until it is Ready or Stopped, it returns an error if is Stopped. |
| 74 |
func (r *ready) wait(ctx context.Context) error { |
| 75 |
_, err := r.waitAndReadGeneration(ctx) |
| 76 |
return err |
| 77 |
} |
| 78 |
|
| 79 |
// waitAndReadGenration blocks until it is Ready or Stopped and returns number |
| 80 |
// of times we entered ready state if Ready and error otherwise. |
| 81 |
func (r *ready) waitAndReadGeneration(ctx context.Context) (int, error) { |
| 82 |
for { |
| 83 |
// r.done() only blocks if state is Pending |
| 84 |
select { |
| 85 |
case <-ctx.Done(): |
| 86 |
return 0, ctx.Err() |
| 87 |
case <-r.done(): |
| 88 |
} |
| 89 |
|
| 90 |
r.lock.RLock() |
| 91 |
if r.state == Pending { |
| 92 |
// since we allow to switch between the states Pending and Ready |
| 93 |
// if there is a quick transition from Pending -> Ready -> Pending |
| 94 |
// a process that was waiting can get unblocked and see a Pending |
| 95 |
// state again. If the state is Pending we have to wait again to |
| 96 |
// avoid an inconsistent state on the system, with some processes not |
| 97 |
// waiting despite the state moved back to Pending. |
| 98 |
r.lock.RUnlock() |
| 99 |
continue |
| 100 |
} |
| 101 |
generation, err := r.readGenerationLocked() |
| 102 |
r.lock.RUnlock() |
| 103 |
return generation, err |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
// check returns the time elapsed since the state was last changed and the current value. |
| 108 |
func (r *ready) check() (time.Duration, error) { |
| 109 |
_, elapsed, err := r.checkAndReadGeneration() |
| 110 |
return elapsed, err |
| 111 |
} |
| 112 |
|
| 113 |
// checkAndReadGeneration returns the current generation, the time elapsed since the state was last changed and the current value. |
| 114 |
func (r *ready) checkAndReadGeneration() (int, time.Duration, error) { |
| 115 |
r.lock.RLock() |
| 116 |
defer r.lock.RUnlock() |
| 117 |
generation, err := r.readGenerationLocked() |
| 118 |
return generation, r.clock.Since(r.lastStateChangeTime), err |
| 119 |
} |
| 120 |
|
| 121 |
func (r *ready) readGenerationLocked() (int, error) { |
| 122 |
switch r.state { |
| 123 |
case Pending: |
| 124 |
if r.lastErr == nil { |
| 125 |
return 0, fmt.Errorf("storage is (re)initializing") |
| 126 |
} else { |
| 127 |
return 0, fmt.Errorf("storage is (re)initializing: %w", r.lastErr) |
| 128 |
} |
| 129 |
case Ready: |
| 130 |
return r.generation, nil |
| 131 |
case Stopped: |
| 132 |
return 0, fmt.Errorf("apiserver cacher is stopped") |
| 133 |
default: |
| 134 |
return 0, fmt.Errorf("unexpected apiserver cache state: %v", r.state) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
func (r *ready) setReady() { |
| 139 |
r.set(true, nil) |
| 140 |
} |
| 141 |
|
| 142 |
func (r *ready) setError(err error) { |
| 143 |
r.set(false, err) |
| 144 |
} |
| 145 |
|
| 146 |
// set the state to Pending (false) or Ready (true), it does not have effect if the state is Stopped. |
| 147 |
func (r *ready) set(ok bool, err error) { |
| 148 |
r.lock.Lock() |
| 149 |
defer r.lock.Unlock() |
| 150 |
if r.state == Stopped { |
| 151 |
return |
| 152 |
} |
| 153 |
r.lastErr = err |
| 154 |
if ok && r.state == Pending { |
| 155 |
r.state = Ready |
| 156 |
r.generation++ |
| 157 |
r.updateLastStateChangeTimeLocked() |
| 158 |
select { |
| 159 |
case <-r.waitCh: |
| 160 |
default: |
| 161 |
close(r.waitCh) |
| 162 |
} |
| 163 |
} else if !ok && r.state == Ready { |
| 164 |
// creating the waitCh can be racy if |
| 165 |
// something enter the wait() method |
| 166 |
select { |
| 167 |
case <-r.waitCh: |
| 168 |
r.restartLock.Lock() |
| 169 |
r.waitCh = make(chan struct{}) |
| 170 |
r.restartLock.Unlock() |
| 171 |
default: |
| 172 |
} |
| 173 |
r.state = Pending |
| 174 |
r.updateLastStateChangeTimeLocked() |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
// stop the condition variable and set it as Stopped. This state is irreversible. |
| 179 |
func (r *ready) stop() { |
| 180 |
r.lock.Lock() |
| 181 |
defer r.lock.Unlock() |
| 182 |
if r.state != Stopped { |
| 183 |
r.state = Stopped |
| 184 |
r.updateLastStateChangeTimeLocked() |
| 185 |
} |
| 186 |
select { |
| 187 |
case <-r.waitCh: |
| 188 |
default: |
| 189 |
close(r.waitCh) |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
func (r *ready) updateLastStateChangeTimeLocked() { |
| 194 |
r.lastStateChangeTime = r.clock.Now() |
| 195 |
} |