-
Notifications
You must be signed in to change notification settings - Fork 1
/
updates.go
326 lines (283 loc) · 10.1 KB
/
updates.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2016 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package server
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"math/rand"
"net/http"
"net/url"
"reflect"
"strconv"
"time"
"github.com/mitchellh/reflectwalk"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/build"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/server/diagnosticspb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
)
const baseUpdatesURL = `https://register.cockroachdb.com/api/clusters/updates`
const baseReportingURL = `https://register.cockroachdb.com/api/clusters/report`
var updatesURL, reportingURL *url.URL
func init() {
var err error
updatesURL, err = url.Parse(
envutil.EnvOrDefaultString("COCKROACH_UPDATE_CHECK_URL", baseUpdatesURL),
)
if err != nil {
panic(err)
}
reportingURL, err = url.Parse(
envutil.EnvOrDefaultString("COCKROACH_USAGE_REPORT_URL", baseReportingURL),
)
if err != nil {
panic(err)
}
}
const (
updateCheckFrequency = time.Hour * 24
// TODO(dt): switch to settings.
updateCheckPostStartup = time.Minute * 5
updateCheckRetryFrequency = time.Hour
updateMaxVersionsToReport = 3
updateCheckJitterSeconds = 120
)
var diagnosticReportFrequency = settings.RegisterNonNegativeDurationSetting(
"diagnostics.reporting.interval",
"interval at which diagnostics data should be reported",
time.Hour,
)
var diagnosticsMetricsEnabled = settings.RegisterBoolSetting(
"diagnostics.reporting.report_metrics",
"enable collection and reporting diagnostic metrics to cockroach labs",
true,
)
// randomly shift `d` to be up to `jitterSec` shorter or longer.
func addJitter(d time.Duration, jitterSec int) time.Duration {
j := time.Duration(rand.Intn(jitterSec*2)-jitterSec) * time.Second
return d + j
}
type versionInfo struct {
Version string `json:"version"`
Details string `json:"details"`
}
// PeriodicallyCheckForUpdates starts a background worker that periodically
// phones home to check for updates and report usage.
func (s *Server) PeriodicallyCheckForUpdates() {
s.stopper.RunWorker(context.TODO(), func(ctx context.Context) {
startup := timeutil.Now()
nextUpdateCheck := startup
nextDiagnosticReport := startup
var timer timeutil.Timer
defer timer.Stop()
for {
now := timeutil.Now()
runningTime := now.Sub(startup)
nextUpdateCheck = s.maybeCheckForUpdates(now, nextUpdateCheck, runningTime)
nextDiagnosticReport = s.maybeReportDiagnostics(ctx, now, nextDiagnosticReport, runningTime)
sooner := nextUpdateCheck
if nextDiagnosticReport.Before(sooner) {
sooner = nextDiagnosticReport
}
timer.Reset(addJitter(sooner.Sub(timeutil.Now()), updateCheckJitterSeconds))
select {
case <-s.stopper.ShouldQuiesce():
return
case <-timer.C:
timer.Read = true
}
}
})
}
// maybeCheckForUpdates determines if it is time to check for updates and does
// so if it is, before returning the time at which the next check be done.
func (s *Server) maybeCheckForUpdates(
now, scheduled time.Time, runningTime time.Duration,
) time.Time {
if scheduled.After(now) {
return scheduled
}
// checkForUpdates handles its own errors, but it returns a bool indicating if
// it succeeded, so we can schedule a re-attempt if it did not.
if succeeded := s.checkForUpdates(runningTime); !succeeded {
return now.Add(updateCheckRetryFrequency)
}
// If we've just started up, we want to check again shortly after.
// During startup is when a message is most likely to be actually seen by a
// human operator so we check as early as possible, but this makes it hard to
// differentiate real deployments vs short-lived instances for tests.
if runningTime < updateCheckPostStartup {
return now.Add(time.Hour - runningTime)
}
return now.Add(updateCheckFrequency)
}
func addInfoToURL(url *url.URL, s *Server, runningTime time.Duration) {
q := url.Query()
b := build.GetInfo()
q.Set("version", b.Tag)
q.Set("platform", b.Platform)
q.Set("uuid", s.node.ClusterID.String())
q.Set("nodeid", s.NodeID().String())
q.Set("uptime", strconv.Itoa(int(runningTime.Seconds())))
q.Set("insecure", strconv.FormatBool(s.cfg.Insecure))
url.RawQuery = q.Encode()
}
// checkForUpdates calls home to check for new versions for the current platform
// and logs messages if it finds them, as well as if it encounters any errors.
// The returned boolean indicates if the check succeeded (and thus does not need
// to be re-attempted by the scheduler after a retry-interval).
func (s *Server) checkForUpdates(runningTime time.Duration) bool {
ctx, span := s.AnnotateCtxWithSpan(context.Background(), "checkForUpdates")
defer span.Finish()
addInfoToURL(updatesURL, s, runningTime)
res, err := http.Get(updatesURL.String())
if err != nil {
// This is probably going to be relatively common in production
// environments where network access is usually curtailed.
return false
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
b, err := ioutil.ReadAll(res.Body)
log.Warningf(ctx, "failed to check for updates: status: %s, body: %s, error: %v",
res.Status, b, err)
return false
}
decoder := json.NewDecoder(res.Body)
r := struct {
Details []versionInfo `json:"details"`
}{}
err = decoder.Decode(&r)
if err != nil && err != io.EOF {
log.Warning(ctx, "Error decoding updates info: ", err)
return false
}
// Ideally the updates server only returns the most relevant updates for us,
// but if it replied with an excessive number of updates, limit log spam by
// only printing the last few.
if len(r.Details) > updateMaxVersionsToReport {
r.Details = r.Details[len(r.Details)-updateMaxVersionsToReport:]
}
for _, v := range r.Details {
log.Infof(ctx, "A new version is available: %s, details: %s", v.Version, v.Details)
}
return true
}
func (s *Server) maybeReportDiagnostics(
ctx context.Context, now, scheduled time.Time, running time.Duration,
) time.Time {
if scheduled.After(now) {
return scheduled
}
// TODO(dt): we should allow tuning the reset and report intervals separately.
// Consider something like rand.Float() > resetFreq/reportFreq here to sample
// stat reset periods for reporting.
if log.DiagnosticsReportingEnabled.Get(&s.st.SV) && diagnosticsMetricsEnabled.Get(&s.st.SV) {
s.reportDiagnostics(running)
}
s.sqlExecutor.ResetStatementStats(ctx)
s.sqlExecutor.ResetUnimplementedCounts()
return scheduled.Add(diagnosticReportFrequency.Get(&s.st.SV))
}
func (s *Server) getReportingInfo(ctx context.Context) *diagnosticspb.DiagnosticReport {
info := diagnosticspb.DiagnosticReport{}
n := s.node.recorder.GetStatusSummary(ctx)
info.Node = diagnosticspb.NodeInfo{NodeID: s.node.Descriptor.NodeID}
info.Stores = make([]diagnosticspb.StoreInfo, len(n.StoreStatuses))
for i, r := range n.StoreStatuses {
info.Stores[i].NodeID = r.Desc.Node.NodeID
info.Stores[i].StoreID = r.Desc.StoreID
info.Stores[i].KeyCount = int64(r.Metrics["keycount"])
info.Node.KeyCount += info.Stores[i].KeyCount
info.Stores[i].RangeCount = int64(r.Metrics["replicas"])
info.Node.RangeCount += info.Stores[i].RangeCount
bytes := int64(r.Metrics["sysbytes"] + r.Metrics["intentbytes"] + r.Metrics["valbytes"] + r.Metrics["keybytes"])
info.Stores[i].Bytes = bytes
info.Node.Bytes += bytes
}
schema, err := s.collectSchemaInfo(ctx)
if err != nil {
log.Warningf(ctx, "error collecting schema info for diagnostic report: %+v", err)
schema = nil
}
info.Schema = schema
info.SqlStats = s.sqlExecutor.GetScrubbedStmtStats()
info.UnimplementedErrors = make(map[string]int64)
s.sqlExecutor.FillUnimplementedErrorCounts(info.UnimplementedErrors)
return &info
}
func (s *Server) reportDiagnostics(runningTime time.Duration) {
ctx, span := s.AnnotateCtxWithSpan(context.Background(), "usageReport")
defer span.Finish()
b, err := s.getReportingInfo(ctx).Marshal()
if err != nil {
log.Warning(ctx, err)
return
}
addInfoToURL(reportingURL, s, runningTime)
res, err := http.Post(reportingURL.String(), "application/x-protobuf", bytes.NewReader(b))
if err != nil {
if log.V(2) {
// This is probably going to be relatively common in production
// environments where network access is usually curtailed.
log.Warning(ctx, "failed to report node usage metrics: ", err)
}
return
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
b, err := ioutil.ReadAll(res.Body)
log.Warningf(ctx, "failed to report node usage metrics: status: %s, body: %s, "+
"error: %v", res.Status, b, err)
}
}
func (s *Server) collectSchemaInfo(ctx context.Context) ([]sqlbase.TableDescriptor, error) {
startKey := roachpb.Key(keys.MakeTablePrefix(keys.DescriptorTableID))
endKey := startKey.PrefixEnd()
kvs, err := s.db.Scan(ctx, startKey, endKey, 0)
if err != nil {
return nil, err
}
tables := make([]sqlbase.TableDescriptor, 0, len(kvs))
redactor := stringRedactor{}
for _, kv := range kvs {
var desc sqlbase.Descriptor
if err := kv.ValueProto(&desc); err != nil {
return nil, errors.Wrapf(err, "%s: unable to unmarshal SQL descriptor", kv.Key)
}
if t := desc.GetTable(); t != nil && t.ID > keys.MaxReservedDescID {
if err := reflectwalk.Walk(t, redactor); err != nil {
panic(err) // stringRedactor never returns a non-nil err
}
tables = append(tables, *t)
}
}
return tables, nil
}
type stringRedactor struct{}
func (stringRedactor) Primitive(v reflect.Value) error {
if v.Kind() == reflect.String && v.String() != "" {
v.Set(reflect.ValueOf("_"))
}
return nil
}