-
Notifications
You must be signed in to change notification settings - Fork 28
/
version.go
251 lines (197 loc) · 5.99 KB
/
version.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright 2015-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 (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"sync"
"time"
"github.com/couchbase/cbgt"
"github.com/couchbase/cbgt/rest"
log "github.com/couchbase/clog"
)
// cbft product version
const VERSION = "v0.5.5"
// cbft api versions, helps in controlling the
// service expectations during upgrades.
// See: MB-17990 on cbft API versioning.
const API_MAX_VERSION = "2.0.0"
const API_MIN_VERSION = "0.0.0"
var API_MAX_VERSION_JSON = WithJSONVersion(API_MAX_VERSION)
const VersionTag = "version="
// FeatureGeoSpatialVersion contains the spatial
// indexing/query capabilities based on s2.
const FeatureGeoSpatialVersion = "7.1.1"
const FeatureCollectionVersion = "7.0.0"
const FeatureGrpcVersion = "6.5.0"
const FeatureScopedIndexNamesVersion = "7.5.0"
const FeatureVectorSearchSupportVersion = "7.6.0"
func HandleAPIVersion(h string) (string, error) {
if len(h) <= 0 || h == "*/*" {
return API_MAX_VERSION, nil
}
foundRequestVersion := false
for _, val := range strings.Split(h, ",") {
versionIndex := strings.Index(val, VersionTag)
if versionIndex < 0 {
continue
}
foundRequestVersion = true
requestVersion := val[versionIndex+len(VersionTag):]
if cbgt.VersionGTE(API_MAX_VERSION, requestVersion) &&
cbgt.VersionGTE(requestVersion, API_MIN_VERSION) {
return requestVersion, nil
}
}
if !foundRequestVersion {
return API_MAX_VERSION, nil
}
return "", fmt.Errorf("version: unsupported version")
}
func WithJSONVersion(v string) string {
return "application/json;" + VersionTag + v
}
func CheckAPIVersion(w http.ResponseWriter, req *http.Request) (err error) {
if req.Header != nil {
accept := req.Header["Accept"]
if len(accept) > 0 {
version, err := HandleAPIVersion(accept[0])
if err != nil {
w.WriteHeader(406)
versionList := []string{
WithJSONVersion(API_MAX_VERSION),
WithJSONVersion(API_MIN_VERSION),
}
rest.MustEncode(w, versionList)
return err
}
w.Header().Set("Content-type", WithJSONVersion(version))
return nil
}
}
w.Header().Set("Content-type", API_MAX_VERSION_JSON)
return nil
}
// versionTracker checks the cluster compatibilty version
// with the ns-server and tracks the cluster version until
// the effective cluster version matches the given application version.
var versionTracker *clusterVersionTracker
type clusterVersionTracker struct {
server string
version uint64 // current cbft version.
clusterVersion uint64 // current compatibility version found on cluster.
m sync.RWMutex // Protects the fields that follow.
compatibleFeatures map[string]struct{}
}
func StartClusterVersionTracker(version string, server string) {
versionTracker = &clusterVersionTracker{server: server + "/pools/default"}
versionTracker.version, _ = cbgt.CompatibilityVersion(version)
versionTracker.compatibleFeatures = make(map[string]struct{})
go versionTracker.run()
}
func (vt *clusterVersionTracker) run() {
ev, err := getEffectiveClusterVersion(vt.server)
if err != nil {
log.Printf("version: getEffectiveClusterVersion, err: %v", err)
}
vt.clusterVersion = ev
if vt.version == ev {
log.Printf("version: matching clusterCompatibility"+
" version: %d found", ev)
return
}
// monitor the effective cluster version until it matches the app version.
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for {
select {
case <-ticker.C:
ev, err := getEffectiveClusterVersion(vt.server)
if err != nil {
log.Printf("version: getEffectiveClusterVersion, err: %v", err)
continue
}
vt.clusterVersion = ev
if vt.version != ev {
continue
}
log.Printf("version: matching clusterCompatibility"+
" version: %d found", ev)
return
}
}
}
// checks whether the current cluster compatibility
// version is greater than or equal the given app version.
func (vt *clusterVersionTracker) clusterCompatibleForVersion(
version string) bool {
givenVersion, err := cbgt.CompatibilityVersion(version)
if err == nil && vt.clusterVersion != 0 &&
vt.clusterVersion >= givenVersion {
return true
}
return false
}
func getEffectiveClusterVersion(server string) (uint64, error) {
if len(server) < 2 {
return 1, fmt.Errorf("version: no server URL configured")
}
u, err := cbgt.CBAuthURL(server)
if err != nil {
return 0, fmt.Errorf("version: auth for ns_server,"+
" server: %s, authType: %s, err: %v",
server, "cbauth", err)
}
req, err := http.NewRequest("GET", u, nil)
if err != nil {
return 0, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := cbgt.HttpClient().Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
respBuf, err := io.ReadAll(resp.Body)
if err != nil {
return 0, fmt.Errorf("version: error reading resp.Body,"+
" nsServerURL: %s, resp: %#v, err: %v", server, resp, err)
}
rv := &cbgt.NsServerResponse{}
err = json.Unmarshal(respBuf, rv)
if err != nil {
return 0, fmt.Errorf("version: error parsing respBuf: %s,"+
" server: %s, err: %v", respBuf, server, err)
}
return uint64(rv.Nodes[0].ClusterCompatibility), nil
}
// memoize the computation done by versionTracker.clusterCompatibleForVersion
func (vt *clusterVersionTracker) isCompatible(feature string) bool {
vt.m.RLock()
if _, ok := vt.compatibleFeatures[feature]; ok {
vt.m.RUnlock()
return true
}
vt.m.RUnlock()
compatible := vt.clusterCompatibleForVersion(feature)
if compatible {
vt.m.Lock()
vt.compatibleFeatures[feature] = struct{}{}
vt.m.Unlock()
}
return compatible
}
func isClusterCompatibleFor(feature string) bool {
if versionTracker == nil {
return false
}
return versionTracker.isCompatible(feature)
}