-
Notifications
You must be signed in to change notification settings - Fork 28
/
bucket_state.go
231 lines (191 loc) · 5.56 KB
/
bucket_state.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright 2022-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package cbft
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/couchbase/cbgt"
log "github.com/couchbase/clog"
)
type streamingResponse struct {
Name string `json:"name,omitempty"`
HibernationState string `json:"hibernationState,omitempty"`
}
func pauseHandleFunc(reader *bufio.Reader) (int, error) {
resBytes, err := reader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
return 1, nil // Success
}
return 0, err
}
if len(resBytes) == 1 && resBytes[0] == '\n' {
return 0, nil
}
sg := &streamingResponse{}
err = json.Unmarshal(resBytes, &sg)
if err != nil {
return 0, err
}
// Pause failed.
if sg.HibernationState == "" {
return -1, nil
}
// Sill in the process of pausing.
if sg.HibernationState == "pausing" {
return 0, nil
}
return 0, nil
}
func resumeHandleFunc(reader *bufio.Reader) (int, error) {
resBytes, err := reader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
return -1, nil
}
return 0, err
}
if len(resBytes) == 1 && resBytes[0] == '\n' {
return 0, nil
}
sg := &streamingResponse{}
err = json.Unmarshal(resBytes, &sg)
if err != nil {
// Failure since failure to unmarshal indicates that the bucket
// is deleted. Ref: VerifySourceNotExists()
return -1, err
}
if sg.HibernationState == "" {
return 1, nil // Success
}
// Sill in the process of resuming.
if sg.HibernationState == "resuming" {
return 0, nil
}
return 0, nil
}
func TrackBucketState(mgr *cbgt.Manager, operation, bucketName string) (int, error) {
mgr.RegisterHibernationBucketTracker(bucketName)
bucketStreamingURI, err := getBucketStreamingURI(mgr, bucketName)
if err != nil {
if err == ErrBucketNotFound {
if operation == cbgt.HIBERNATE_TASK {
return 1, nil // Pause is a success
} else if operation == cbgt.UNHIBERNATE_TASK {
return -1, nil // Resume has failed
}
} else {
return -1, err
}
}
if operation == cbgt.HIBERNATE_TASK {
return setupStreamingEndpoint(mgr, bucketStreamingURI, bucketName, pauseHandleFunc)
} else if operation == cbgt.UNHIBERNATE_TASK {
return setupStreamingEndpoint(mgr, bucketStreamingURI, bucketName, resumeHandleFunc)
}
return 0, fmt.Errorf("bucket state: not implemented for operations" +
"other than hibernation and unhibernation")
}
type bucketDefaultResponse struct {
Name string `json:"name"`
StreamingURI string `json:"streamingUri"`
}
var ErrBucketNotFound = fmt.Errorf("bucket state: bucket not found")
func getBucketStreamingURI(mgr *cbgt.Manager, bucketName string) (string, error) {
poolDefaultURL := mgr.Server() + "/pools/default/buckets"
u, err := cbgt.CBAuthURL(poolDefaultURL)
if err != nil {
log.Warnf("bucket state: error adding credentials to streaming URI: %v",
err)
return "", err
}
req, err := http.NewRequest("GET", u, nil)
if err != nil {
log.Warnf("bucket state: req, err: %v", err)
return "", err
}
req.Header.Add("Content-Type", "application/json")
resp, err := cbgt.HttpClient().Do(req)
if err != nil || resp.StatusCode != 200 {
log.Warnf("bucket state: http client, response: %+v, err: %v", resp, err)
return "", err
}
var bucketsDefaultResponse []bucketDefaultResponse
err = json.NewDecoder(resp.Body).Decode(&bucketsDefaultResponse)
if err != nil {
return "", err
}
for _, bucketResponse := range bucketsDefaultResponse {
if bucketResponse.Name == bucketName {
return bucketResponse.StreamingURI, nil
}
}
return "", ErrBucketNotFound
}
func setupStreamingEndpoint(mgr *cbgt.Manager, bucketStreamingURI, bucketName string,
handleResponse func(*bufio.Reader) (int, error)) (int, error) {
var err error
bucketStreamingURL := mgr.Server() + bucketStreamingURI
backoffStartSleepMS := 500
backoffFactor := float32(1.5)
backoffMaxSleepMS := 60000
var resp *http.Response
var status int
// keep retrying with backoff logic upon connection errors.
cbgt.ExponentialBackoffLoop("listening to "+bucketStreamingURL,
func() int {
u, err := cbgt.CBAuthURL(bucketStreamingURL)
if err != nil {
log.Warnf("bucket state: error adding credentials to url: %v",
err)
return 0
}
req, err := http.NewRequest("GET", u, nil)
if err != nil {
log.Warnf("bucket state: req, err: %v", err)
return 0
}
req.Header.Add("Content-Type", "application/json")
resp, err = cbgt.HttpClient().Do(req)
if err != nil || (resp.StatusCode != 200 && resp.StatusCode != 404) {
log.Warnf("bucket state: http client, response: %+v, err: %v", resp, err)
return 0
} else if resp.StatusCode == 404 {
status = -1
return -1
}
log.Printf("bucket state: pool streaming started for bucket %s",
bucketName)
return -1 // success
},
backoffStartSleepMS, backoffFactor, backoffMaxSleepMS,
)
reader := bufio.NewReader(resp.Body)
if status == -1 {
return status, fmt.Errorf("bucket state: endpoint not found for bucket %s", bucketName)
}
status = 0
for status == 0 {
status, err = handleResponse(reader)
if status == 1 || status == -1 {
return status, nil
}
// Exit at the first error.
if err != nil {
resp.Body.Close()
return status, err
}
// continue if neither case, waiting for either conclusive
// success or failure
continue
}
return status, err
}