/
waituntilreadyprovider.go
179 lines (148 loc) · 4.23 KB
/
waituntilreadyprovider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package gocb
import (
"context"
"errors"
"time"
"github.com/couchbase/gocbcoreps"
"github.com/couchbase/gocbcore/v10"
)
type waitUntilReadyProvider interface {
WaitUntilReady(ctx context.Context, deadline time.Time, opts *WaitUntilReadyOptions) error
}
type gocbcoreWaitUntilReadyProvider interface {
WaitUntilReady(deadline time.Time, opts gocbcore.WaitUntilReadyOptions,
cb gocbcore.WaitUntilReadyCallback) (gocbcore.PendingOp, error)
}
type waitUntilReadyProviderCore struct {
retryStrategyWrapper *coreRetryStrategyWrapper
provider gocbcoreWaitUntilReadyProvider
}
func (wpw *waitUntilReadyProviderCore) WaitUntilReady(ctx context.Context, deadline time.Time,
opts *WaitUntilReadyOptions) error {
desiredState := opts.DesiredState
if desiredState == 0 {
desiredState = ClusterStateOnline
}
gocbcoreServices := make([]gocbcore.ServiceType, len(opts.ServiceTypes))
for i, svc := range opts.ServiceTypes {
gocbcoreServices[i] = gocbcore.ServiceType(svc)
}
wrapper := wpw.retryStrategyWrapper
if opts.RetryStrategy != nil {
wrapper = newCoreRetryStrategyWrapper(opts.RetryStrategy)
}
coreOpts := gocbcore.WaitUntilReadyOptions{
DesiredState: gocbcore.ClusterState(desiredState),
ServiceTypes: gocbcoreServices,
RetryStrategy: wrapper,
}
var errOut error
opm := newAsyncOpManager(ctx)
err := opm.Wait(wpw.provider.WaitUntilReady(deadline, coreOpts, func(res *gocbcore.WaitUntilReadyResult, err error) {
if err != nil {
errOut = maybeEnhanceCoreErr(err)
opm.Reject()
return
}
opm.Resolve()
}))
if err != nil {
errOut = maybeEnhanceCoreErr(err)
}
return errOut
}
type waitUntilreadyRequestPs struct {
// reasons is effectively a set, so we can't just use len(reasons) for num attempts.
reasons []RetryReason
attempts uint32
strategy RetryStrategy
}
func (w *waitUntilreadyRequestPs) RetryAttempts() uint32 {
return w.attempts
}
func (w *waitUntilreadyRequestPs) Identifier() string {
return "WaitUntilReady"
}
func (w *waitUntilreadyRequestPs) Idempotent() bool {
return true
}
func (w *waitUntilreadyRequestPs) RetryReasons() []RetryReason {
return w.reasons
}
func (w *waitUntilreadyRequestPs) retryStrategy() RetryStrategy {
return w.strategy
}
func (w *waitUntilreadyRequestPs) recordRetryAttempt(reason RetryReason) {
w.attempts++
found := false
for i := 0; i < len(w.reasons); i++ {
if w.reasons[i] == reason {
found = true
break
}
}
// if idx is out of the range of retryReasons then it wasn't found.
if !found {
w.reasons = append(w.reasons, reason)
}
}
type waitUntilReadyProviderPs struct {
defaultRetryStrategy RetryStrategy
client *gocbcoreps.RoutingClient
}
func (wpw *waitUntilReadyProviderPs) WaitUntilReady(ctx context.Context, deadline time.Time,
opts *WaitUntilReadyOptions) error {
start := time.Now()
desiredState := opts.DesiredState
if desiredState == ClusterStateOffline {
return makeInvalidArgumentsError("cannot use offline as a desired state")
}
if desiredState == 0 {
desiredState = ClusterStateOnline
}
retryStrategy := wpw.defaultRetryStrategy
if opts.RetryStrategy != nil {
retryStrategy = opts.RetryStrategy
}
retryRequest := &waitUntilreadyRequestPs{
strategy: retryStrategy,
}
if ctx == nil {
ctx = context.Background()
}
ctx, cancel := context.WithDeadline(ctx, deadline)
defer cancel()
for {
state := wpw.client.ConnectionState()
var gocbState ClusterState
switch state {
case gocbcoreps.ConnStateOffline:
gocbState = ClusterStateOffline
case gocbcoreps.ConnStateOnline:
gocbState = ClusterStateOnline
case gocbcoreps.ConnStateDegraded:
gocbState = ClusterStateDegraded
}
if gocbState == desiredState {
return nil
}
shouldRetry, retryAfter := retryOrchMaybeRetry(retryRequest, NotReadyRetryReason)
if !shouldRetry {
// This should never actually happen - not ready is always retry.
return ErrRequestCanceled
}
select {
case <-ctx.Done():
err := ctx.Err()
if errors.Is(err, context.DeadlineExceeded) {
return &TimeoutError{
InnerError: ErrUnambiguousTimeout,
TimeObserved: time.Since(start),
RetryReasons: retryRequest.RetryReasons(),
RetryAttempts: retryRequest.RetryAttempts(),
}
}
case <-time.After(time.Until(retryAfter)):
}
}
}