-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathfollower_reads.go
325 lines (310 loc) · 10.1 KB
/
follower_reads.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
// Copyright 2018 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. See the AUTHORS file
// for names of contributors.
package main
import (
"bufio"
"context"
gosql "database/sql"
"math/rand"
"net/http"
"regexp"
"strconv"
"time"
"github.com/cockroachdb/cockroach/pkg/ts/tspb"
"github.com/cockroachdb/cockroach/pkg/util/httputil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
func registerFollowerReads(r *registry) {
r.Add(testSpec{
Name: "follower-reads/nodes=3",
Cluster: clusterSpec{
NodeCount: 3,
CPUs: 2,
Geo: true,
},
Run: runFollowerReadsTest,
})
}
// runFollowerReadsTest is a basic litmus test that follower reads work.
// The test does the following:
//
// * Creates a database and table.
// * Installs a number of rows into that table.
// * Queries the data initially with a recent timestamp and expecting an
// error because the table does not exist in the past immediately following
// creation.
// * Waits until the required duration has elapsed such that the installed
// data can be read with a follower read issued using `follower_timestamp()`.
// * Performs a single `follower_timestamp()` select query against a single
// row on all of the nodes and then observes the counter metric for
// store-level follower reads ensuring that they occurred on at least
// two of the nodes.
// * Performs reads against the written data on all of the nodes at a steady
// rate for 20 seconds, ensure that the 90-%ile SQL latencies during that
// time are under 10ms which implies that no WAN RPCs occurred.
//
func runFollowerReadsTest(ctx context.Context, t *test, c *cluster) {
crdbNodes := c.Range(1, c.nodes)
c.Put(ctx, cockroach, "./cockroach", crdbNodes)
c.Wipe(ctx, crdbNodes)
c.Start(ctx, t, crdbNodes)
var conns []*gosql.DB
for i := 0; i < c.nodes; i++ {
conns = append(conns, c.Conn(ctx, i+1))
defer conns[i].Close()
}
db := conns[0]
if _, err := db.ExecContext(ctx, "SET CLUSTER SETTING kv.closed_timestamp.follower_reads_enabled = 'true'"); err != nil {
t.Fatalf("failed to enable follower reads: %v", err)
}
if r, err := db.ExecContext(ctx, "CREATE DATABASE test;"); err != nil {
t.Fatalf("failed to create database: %v %v", err, r)
}
if r, err := db.ExecContext(ctx, "CREATE TABLE test.test ( k INT8, v INT8, PRIMARY KEY (k) )"); err != nil {
t.Fatalf("failed to create table: %v %v", err, r)
}
const rows = 100
const concurrency = 32
sem := make(chan struct{}, concurrency)
data := make(map[int]int64)
insert := func(ctx context.Context, k int) func() error {
v := rand.Int63()
data[k] = v
return func() error {
sem <- struct{}{}
defer func() { <-sem }()
_, err := db.ExecContext(ctx, "INSERT INTO test.test VALUES ( $1, $2 )", k, v)
return err
}
}
chooseKV := func() (k int, v int64) {
for k, v = range data {
return
}
return
}
verifySelect := func(ctx context.Context, node, k int, expectError bool, expectedVal int64) func() error {
return func() error {
db := conns[node-1]
r := db.QueryRowContext(ctx, "SELECT v FROM test.test AS OF SYSTEM TIME follower_read_timestamp() WHERE k = $1", k)
var got int64
if err := r.Scan(&got); err != nil {
// Ignore errors due to cancellation.
if ctx.Err() != nil {
return nil
}
if expectError {
return nil
}
return err
}
if expectError {
return errors.Errorf("failed to get expected error on node %d", node)
}
if got != expectedVal {
return errors.Errorf("Didn't get expected val on node %d: %v != %v",
node, got, expectedVal)
}
return nil
}
}
doSelects := func(ctx context.Context, node int) func() error {
return func() error {
for ctx.Err() == nil {
k, v := chooseKV()
err := verifySelect(ctx, node, k, false, v)()
if err != nil && ctx.Err() == nil {
return err
}
}
return nil
}
}
// Insert the data.
g, gCtx := errgroup.WithContext(ctx)
for i := 0; i < rows; i++ {
g.Go(insert(gCtx, i))
}
if err := g.Wait(); err != nil {
t.Fatalf("failed to insert data: %v", err)
}
// Verify error on immediate read.
g, gCtx = errgroup.WithContext(ctx)
for i := 1; i <= c.nodes; i++ {
// Expect an error performing a historical read at first because the table
// won't have been created yet.
g.Go(verifySelect(gCtx, i, 0, true, 0))
}
if err := g.Wait(); err != nil {
t.Fatalf("unexpected error performing historical reads: %v", err)
}
// Wait for follower_timestamp() historical reads to have data.
// TODO(ajwerner): introspect the database to determine how long to wait.
const followerReadsDur = 48 * time.Second
select {
case <-time.After(followerReadsDur):
case <-ctx.Done():
t.Fatalf("context canceled: %v", ctx.Err())
}
// Read the follower read counts before issuing the follower reads to observe
// the delta and protect from follower reads which might have happened due to
// system queries.
followerReadsBefore, err := getFollowerReadCounts(ctx, c)
if err != nil {
t.Fatalf("failed to get follower read counts: %v", err)
}
// Perform reads at follower_timestamp() and ensure we get the expected value.
g, gCtx = errgroup.WithContext(ctx)
k, v := chooseKV()
for i := 1; i <= c.nodes; i++ {
g.Go(verifySelect(gCtx, i, k, false, v))
}
if err := g.Wait(); err != nil {
t.Fatalf("error verifying node values: %v", err)
}
// Verify that the follower read count increments on at least two nodes.
followerReadsAfter, err := getFollowerReadCounts(ctx, c)
if err != nil {
t.Fatalf("failed to get follower read counts: %v", err)
}
nodesWhichSawFollowerReads := 0
for i := 0; i < len(followerReadsAfter); i++ {
if followerReadsAfter[i] > followerReadsBefore[i] {
nodesWhichSawFollowerReads++
}
}
if nodesWhichSawFollowerReads < 2 {
t.Fatalf("fewer than 2 follower reads occurred: saw %v before and %v after",
followerReadsBefore, followerReadsAfter)
}
// Run reads for 60s which given the metrics window of 10s should guarantee
// that the most recent SQL latency time series data should relate to at least
// some of these reads.
timeoutCtx, cancel := context.WithTimeout(ctx, time.Minute)
defer cancel()
g, gCtx = errgroup.WithContext(timeoutCtx)
for i := 0; i < concurrency; i++ {
g.Go(doSelects(gCtx, rand.Intn(c.nodes)+1))
}
start := timeutil.Now()
if err := g.Wait(); err != nil && timeoutCtx.Err() == nil {
t.Fatalf("error reading data: %v", err)
}
// Perform a ts query to verify that the SQL latencies were well below the
// WAN latencies which should be at least 50ms.
verifySQLLatency(ctx, c, t, c.Node(1), start, timeutil.Now(), 20*time.Millisecond)
}
// verifySQLLatency verifies that the client-facing SQL latencies in the 99th
// percentile remain below target latency between start and end ignoring the
// first 20s.
func verifySQLLatency(
ctx context.Context,
c *cluster,
t *test,
adminNode nodeListOption,
start, end time.Time,
targetLatency time.Duration,
) {
// Query needed information over the timespan of the query.
adminURLs := c.ExternalAdminUIAddr(ctx, adminNode)
url := "http://" + adminURLs[0] + "/ts/query"
request := tspb.TimeSeriesQueryRequest{
StartNanos: start.UnixNano(),
EndNanos: end.UnixNano(),
// Ask for 10s intervals.
SampleNanos: (10 * time.Second).Nanoseconds(),
Queries: []tspb.Query{
{
Name: "cr.node.sql.service.latency-p99",
},
},
}
var response tspb.TimeSeriesQueryResponse
if err := httputil.PostJSON(http.Client{}, url, &request, &response); err != nil {
t.Fatal(err)
}
perTenSeconds := response.Results[0].Datapoints[2:]
// Drop the first 20 seconds of datapoints as a "ramp-up" period.
if len(perTenSeconds) < 2 {
t.Fatalf("not enough ts data to verify latency")
}
for _, dp := range perTenSeconds {
if time.Duration(dp.Value) > targetLatency {
t.Fatalf("latency value %v for ts data point %v is above target latency %v",
time.Duration(dp.Value), dp, targetLatency)
}
}
}
const followerReadsMetric = "follower_reads_success_count"
// getFollowerReadCounts returns a slice from node to
func getFollowerReadCounts(ctx context.Context, c *cluster) ([]int, error) {
followerReadCounts := make([]int, c.nodes)
getFollowerReadCount := func(ctx context.Context, node int) func() error {
return func() error {
url := "http://" + c.ExternalAdminUIAddr(ctx, c.Node(node))[0] + "/_status/vars"
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return errors.Errorf("invalid non-200 status code %v", resp.StatusCode)
}
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
m, ok := parsePrometheusMetric(scanner.Text())
if ok {
if m.metric == followerReadsMetric {
v, err := strconv.ParseFloat(m.value, 64)
if err != nil {
return err
}
followerReadCounts[node-1] = int(v)
}
}
}
return nil
}
}
g, gCtx := errgroup.WithContext(ctx)
for i := 1; i <= c.nodes; i++ {
g.Go(getFollowerReadCount(gCtx, i))
}
if err := g.Wait(); err != nil {
return nil, err
}
return followerReadCounts, nil
}
var prometheusMetricStringPattern = "(?P<metric>\\w+)\\{" +
"(?P<labelvalues>(\\w+=\".*\",)*(\\w+=\".*\")?)\\}\\s+(?P<value>.*)"
var promethusMetricStringRE = regexp.MustCompile(prometheusMetricStringPattern)
type prometheusMetric struct {
metric string
labelValues string
value string
}
func parsePrometheusMetric(s string) (*prometheusMetric, bool) {
matches := promethusMetricStringRE.FindStringSubmatch(s)
if matches == nil {
return nil, false
}
return &prometheusMetric{
metric: matches[1],
labelValues: matches[2],
value: matches[5],
}, true
}