-
Notifications
You must be signed in to change notification settings - Fork 245
/
newenemy_test.go
700 lines (628 loc) · 22.6 KB
/
newenemy_test.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
package newenemy
import (
"context"
"database/sql"
"flag"
"fmt"
"log"
"math"
"math/rand"
"os"
"strings"
"testing"
"text/template"
"time"
v0 "github.com/authzed/authzed-go/proto/authzed/api/v0"
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/authzed/authzed-go/proto/authzed/api/v1alpha1"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
"github.com/stretchr/testify/require"
"github.com/authzed/spicedb/e2e"
"github.com/authzed/spicedb/e2e/cockroach"
"github.com/authzed/spicedb/e2e/generator"
"github.com/authzed/spicedb/e2e/spice"
"github.com/authzed/spicedb/pkg/zedtoken"
"github.com/authzed/spicedb/pkg/zookie"
)
type SchemaData struct {
Prefixes []string
}
const schemaText = `
{{ range .Prefixes }}
definition {{.}}/user {}
definition {{.}}/resource {
relation direct: {{.}}/user
relation excluded: {{.}}/user
permission allowed = direct - excluded
}
{{ end }}
`
const schemaAllowAllText = `
{{ range .Prefixes }}
definition {{.}}/user {}
definition {{.}}/resource {
relation direct: {{.}}/user
relation excluded: {{.}}/user
permission allowed = direct
}
{{ end }}
`
const (
objIDRegex = "[a-zA-Z0-9_][a-zA-Z0-9/_-]{0,127}"
namespacePrefixRegex = "[a-z][a-z0-9_]{1,62}[a-z0-9]"
)
var (
maxIterations = flag.Int("max-iterations", 1000, "iteration cap for statistic-based tests (0 for no limit)")
schemaTpl = template.Must(template.New("schema").Parse(schemaText))
schemaAllowTpl = template.Must(template.New("schema_allow").Parse(schemaAllowAllText))
objIdGenerator = generator.NewUniqueGenerator(objIDRegex)
prefixGenerator = generator.NewUniqueGenerator(namespacePrefixRegex)
testCtx context.Context
)
func TestMain(m *testing.M) {
var cancel context.CancelFunc
testCtx, cancel = context.WithCancel(e2e.Context())
code := m.Run()
cancel()
os.Exit(code)
}
const (
createDb = "CREATE DATABASE %s;"
setSmallRanges = "ALTER DATABASE %s CONFIGURE ZONE USING range_min_bytes = 0, range_max_bytes = 65536, num_replicas = 1, gc.ttlseconds = 5;"
dbName = "spicedbnetest"
)
func initializeTestCRDBCluster(ctx context.Context, t testing.TB) cockroach.Cluster {
require := require.New(t)
t.Log("starting cockroach...")
crdbCluster := cockroach.NewCluster(3)
for _, c := range crdbCluster {
require.NoError(c.Start(ctx))
}
t.Log("initializing crdb...")
tlog := e2e.NewTLog(t)
crdbCluster.Init(ctx, tlog, tlog)
require.NoError(crdbCluster.SQL(ctx, tlog, tlog,
fmt.Sprintf(createDb, dbName),
))
t.Log("migrating...")
require.NoError(spice.MigrateHead(ctx, tlog, "cockroachdb", crdbCluster[0].ConnectionString(dbName)))
t.Log("attempting to connect...")
require.NoError(crdbCluster[2].Connect(ctx, tlog, dbName))
require.NoError(crdbCluster[1].Connect(ctx, tlog, dbName))
require.NoError(crdbCluster[0].Connect(ctx, tlog, dbName))
return crdbCluster
}
func TestNoNewEnemy(t *testing.T) {
rand.Seed(time.Now().UnixNano())
ctx, cancel := context.WithCancel(testCtx)
defer cancel()
crdb := initializeTestCRDBCluster(ctx, t)
tlog := e2e.NewTLog(t)
t.Log("starting vulnerable spicedb...")
vulnerableSpiceDb := spice.NewClusterFromCockroachCluster(crdb, spice.WithDbName(dbName))
require.NoError(t, vulnerableSpiceDb.Start(ctx, tlog, "vulnerable",
"--datastore-tx-overlap-strategy=insecure"))
require.NoError(t, vulnerableSpiceDb.Connect(ctx, tlog))
t.Log("start protected spicedb cluster")
protectedSpiceDb := spice.NewClusterFromCockroachCluster(crdb,
spice.WithGrpcPort(50061),
spice.WithDispatchPort(50062),
spice.WithHttpPort(8444),
spice.WithMetricsPort(9100),
spice.WithDashboardPort(8100),
spice.WithDbName(dbName))
require.NoError(t, protectedSpiceDb.Start(ctx, tlog, "protected"))
require.NoError(t, protectedSpiceDb.Connect(ctx, tlog))
t.Log("configure small ranges, single replicas, short ttl")
require.NoError(t, crdb.SQL(ctx, tlog, tlog,
fmt.Sprintf(setSmallRanges, dbName),
))
t.Log("fill with schemas to span multiple ranges")
// 4000 is larger than we need to span all three nodes, but a higher number
// seems to make the test converge faster
schemaData := generateSchemaData(4000, 500)
fillSchema(t, schemaTpl, schemaData, vulnerableSpiceDb[1].Client().V1Alpha1().Schema())
slowNodeId, err := crdb[1].NodeID(testCtx)
require.NoError(t, err)
t.Log("modifying time")
require.NoError(t, crdb.TimeDelay(ctx, e2e.MustFile(ctx, t, "timeattack-1.log"), 1, -150*time.Millisecond))
tests := []struct {
name string
vulnerableProbe probeFn
protectedProbe probeFn
vulnerableMax int
sampleSize int
}{
{
name: "protected from schema newenemy",
vulnerableProbe: func(count int) (bool, int) {
return checkSchemaNoNewEnemy(ctx, t, schemaData, slowNodeId, crdb, vulnerableSpiceDb, count)
},
protectedProbe: func(count int) (bool, int) {
return checkSchemaNoNewEnemy(ctx, t, schemaData, slowNodeId, crdb, protectedSpiceDb, count)
},
vulnerableMax: 100,
sampleSize: 5,
},
{
name: "protected from data newenemy",
vulnerableProbe: func(count int) (bool, int) {
return checkDataNoNewEnemy(ctx, t, schemaData, slowNodeId, crdb, vulnerableSpiceDb, count)
},
protectedProbe: func(count int) (bool, int) {
return checkDataNoNewEnemy(ctx, t, schemaData, slowNodeId, crdb, protectedSpiceDb, count)
},
vulnerableMax: 100,
sampleSize: 5,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
vulnerableFn, protectedFn := attemptFnsForProbeFns(100, tt.vulnerableProbe, tt.protectedProbe)
statTest(t, 5, vulnerableFn, protectedFn)
})
}
}
// probeFn tests a condition a maximum of n times, returning whether the
// condition holds and how many times it was attempted. It is used as a building
// block for attemptFn and protectedAttemptFn.
type probeFn func(n int) (success bool, count int)
// attemptFnsForProbeFns takes probeFns and turns them into "testFns" that are used
// by stat tests
func attemptFnsForProbeFns(vulnerableMax int, vulnerableProbe, protectedProbe probeFn) (vulnerableFn attemptFn, protectedFn protectedAttemptFn) {
vulnerableFn = func(t *testing.T) int {
protected := true
var attempts int
for protected {
// attempt vulnerableMax times before resetting
// this helps if the test gets stuck in a state with bad initial
// conditions, like a prefix that never lands on the right nodes
protected, attempts = vulnerableProbe(vulnerableMax)
}
require.False(t, protected)
t.Logf("determined spicedb vulnerable in %d attempts", attempts)
return attempts
}
protectedFn = func(t *testing.T, count int) {
t.Logf("check spicedb is protected after %d attempts", count)
protected := true
var attempts int
for protected {
protected, attempts = protectedProbe(count)
// if the number of attempts doesn't match the count, that means
// the test has requests a reset for some reason.
if attempts < count {
continue
}
require.True(t, protected, "protection is enabled, but newenemy detected")
require.Equal(t, count, attempts)
t.Logf("spicedb is protected after %d attempts", count)
return
}
}
return
}
// attemptFn runs a check and returns how many iterations it took to fail
type attemptFn func(t *testing.T) int
// protectedAttemptFn runs a check and fails the current test if it fails within
// `count` iterations
type protectedAttemptFn func(t *testing.T, count int)
// statTest takes a testFn that is expected to fail the test, returning a sample
// and a protectedTestFn that is expected to succeed even after a given number
// of runs.
func statTest(t *testing.T, sampleSize int, vulnerableFn attemptFn, protectedFn protectedAttemptFn) {
samples := make([]int, sampleSize)
for i := 0; i < sampleSize; i++ {
t.Logf("collecting sample %d", i)
samples[i] = vulnerableFn(t)
}
protectedFn(t, iterationsForHighConfidence(samples))
}
// iterationsForHighConfidence returns how many iterations we need to get
// > 3sigma from the mean of the samples.
// Caps at maxIterations to control test runtime. Set maxIterations to 0 for no
// cap.
func iterationsForHighConfidence(samples []int) (iterations int) {
// from https://cs.opensource.google/go/x/perf/+/40a54f11:internal/stats/sample.go;l=196
// calculates mean and stddev at the same time
mean, M2 := 0.0, 0.0
for n, x := range samples {
delta := float64(x) - mean
mean += delta / float64(n+1)
M2 += delta * (float64(x) - mean)
}
variance := M2 / float64(len(samples)-1)
stddev := math.Sqrt(variance)
samplestddev := stddev / math.Sqrt(float64(len(samples)))
// how many iterations do we need to get > 3sigma from the mean?
// cap maxIterations to control test runtime.
iterations = int(math.Ceil(3*stddev*samplestddev + mean))
if *maxIterations != 0 && *maxIterations < iterations {
iterations = *maxIterations
}
return
}
// checkDataNoNewEnemy returns true if the service is protected and false if it
// is vulnerable.
//
// This subtest ensures protection from a "data-based" newenemy problem:
//
// 1. Use this schema:
// definition user {}
// definition resource {
// relation direct: user
// relation excluded: user
// permission allowed = direct - excluded
// }
// 2. Write resource:1#excluded@user:A
// 3. Write resource:2#direct:@user:A
// 4. If the timestamp from (3) is before the timestamp for (2), then:
// check(resource:1#allowed@user:A)
// may succeed, when it should fail. The timestamps can be reversed
// if the tx overlap protections are disabled, because cockroach only
// ensures overlapping transactions are linearizable.
func checkDataNoNewEnemy(ctx context.Context, t testing.TB, schemaData []SchemaData, slowNodeId int, crdb cockroach.Cluster, spicedb spice.Cluster, maxAttempts int) (bool, int) {
prefix := prefixForNode(ctx, crdb[1].Conn(), schemaData, slowNodeId)
t.Log("filling with data to span multiple ranges for prefix", prefix)
fill(t, spicedb[0].Client().V0().ACL(), prefix, 4000, 1000)
for attempts := 1; attempts <= maxAttempts; attempts++ {
direct, exclude := generateTuple(prefix, objIdGenerator)
// write to node 1
r1, err := spicedb[0].Client().V0().ACL().Write(testCtx, &v0.WriteRequest{
Updates: []*v0.RelationTupleUpdate{exclude},
})
require.NoError(t, err)
// the first write has to read the namespaces from the second node,
// which will resync the timestamps. sleeping allows the clocks to get
// back out of sync
time.Sleep(100 * time.Millisecond)
// write to node 2 (clock is behind)
r2, err := spicedb[1].Client().V0().ACL().Write(testCtx, &v0.WriteRequest{
Updates: []*v0.RelationTupleUpdate{direct},
})
require.NoError(t, err)
canHas, err := spicedb[1].Client().V0().ACL().Check(context.Background(), &v0.CheckRequest{
TestUserset: &v0.ObjectAndRelation{
Namespace: direct.Tuple.ObjectAndRelation.Namespace,
ObjectId: direct.Tuple.ObjectAndRelation.ObjectId,
Relation: "allowed",
},
User: direct.Tuple.GetUser(),
AtRevision: r2.GetRevision(),
})
require.NoError(t, err)
r1leader, r2leader := getLeaderNode(testCtx, crdb[1].Conn(), exclude.Tuple), getLeaderNode(testCtx, crdb[1].Conn(), direct.Tuple)
ns1Leader := getLeaderNodeForNamespace(testCtx, crdb[1].Conn(), exclude.Tuple.ObjectAndRelation.Namespace)
ns2Leader := getLeaderNodeForNamespace(testCtx, crdb[1].Conn(), exclude.Tuple.User.GetUserset().Namespace)
z1, _ := zookie.DecodeRevision(r1.GetRevision())
z2, _ := zookie.DecodeRevision(r2.GetRevision())
t.Log(z1, z2, z1.Sub(z2).String(), r1leader, r2leader, ns1Leader, ns2Leader)
if canHas.IsMember {
t.Log("service is subject to the new enemy problem")
return false, attempts
}
if ns1Leader != slowNodeId || ns2Leader != slowNodeId {
t.Log("namespace leader changed, pick new prefix and fill")
// returning true will re-run with a new prefix
return true, attempts
}
if z1.Sub(z2).IsPositive() {
t.Log("error in test, object id has been re-used.")
continue
}
select {
case <-ctx.Done():
return false, attempts
default:
// this sleep helps the clocks get back out of sync after an attempt
time.Sleep(100 * time.Millisecond)
continue
}
}
return true, maxAttempts
}
// checkSchemaNoNewEnemy returns true if the service is protected, false if it
// is vulnerable.
//
// This test ensures protection from a "schema and data" newenemy, i.e.
// the new enemy conditions require linearizable changes to schema and data
// 1. Start with this schema:
// definition user {}
// definition resource {
// relation direct: user
// relation excluded: user
// permission allowed = direct
// }
// 2. Write resource:1#direct:@user:A
// 3. Write resource:1#excluded@user:A
// 4. Update to this schema:
// definition user {}
// definition resource {
// relation direct: user
// relation excluded: user
// permission allowed = direct - excluded
// }
// 5. If the revision from (4) is before the timestamp for (3), then:
// check(revision from 3, resource:1#allowed@user:A)
// may fail, when it should succeed. The timestamps can be reversed
// if the tx overlap protections are disabled, because cockroach only
// ensures overlapping transactions are linearizable.
// In this case, we don't get an explicit revision back from the
// WriteSchema call, but the Schema write and the resource write are
// fully consistent.
func checkSchemaNoNewEnemy(ctx context.Context, t testing.TB, schemaData []SchemaData, slowNodeId int, crdb cockroach.Cluster, spicedb spice.Cluster, maxAttempts int) (bool, int) {
prefix := prefixForNode(ctx, crdb[1].Conn(), schemaData, slowNodeId)
var b strings.Builder
require.NoError(t, schemaAllowTpl.Execute(&b, SchemaData{Prefixes: []string{prefix}}))
allowSchema := b.String()
b.Reset()
require.NoError(t, schemaTpl.Execute(&b, SchemaData{Prefixes: []string{prefix}}))
excludeSchema := b.String()
for attempts := 1; attempts <= maxAttempts; attempts++ {
direct, exclude := generateTuple(prefix, objIdGenerator)
// write the "allow" schema
require.NoError(t, getErr(spicedb[0].Client().V1Alpha1().Schema().WriteSchema(context.Background(), &v1alpha1.WriteSchemaRequest{
Schema: allowSchema,
})))
// write the "direct" tuple
require.NoError(t, getErr(spicedb[0].Client().V0().ACL().Write(testCtx, &v0.WriteRequest{
Updates: []*v0.RelationTupleUpdate{direct},
})))
// write the "excludes" tuple
// writing to 1 primes the namespace cache on node 1 with the "allow" namespace
r2, err := spicedb[1].Client().V0().ACL().Write(testCtx, &v0.WriteRequest{
Updates: []*v0.RelationTupleUpdate{exclude},
})
require.NoError(t, err)
// write the "exclude" schema. If this write hits the slow crdb node, it
// can get a revision in between the direct and exclude tuple writes
// which will cause check to fail, when it should succeed
require.NoError(t, getErr(spicedb[1].Client().V1Alpha1().Schema().WriteSchema(testCtx, &v1alpha1.WriteSchemaRequest{
Schema: excludeSchema,
})))
rev, err := zookie.DecodeRevision(r2.GetRevision())
require.NoError(t, err)
var canHas *v1.CheckPermissionResponse
checkAccess := func() bool {
var err error
canHas, err = spicedb[0].Client().V1().Permissions().CheckPermission(testCtx, &v1.CheckPermissionRequest{
Consistency: &v1.Consistency{
Requirement: &v1.Consistency_AtExactSnapshot{AtExactSnapshot: zedtoken.NewFromRevision(rev)},
},
Resource: &v1.ObjectReference{
ObjectType: direct.Tuple.ObjectAndRelation.Namespace,
ObjectId: direct.Tuple.ObjectAndRelation.ObjectId,
},
Permission: "allowed",
Subject: &v1.SubjectReference{
Object: &v1.ObjectReference{
ObjectType: direct.Tuple.User.GetUserset().Namespace,
ObjectId: direct.Tuple.User.GetUserset().ObjectId,
},
},
})
if err != nil {
t.Log(err)
}
return err == nil
}
require.Eventually(t, checkAccess, 10*time.Second, 10*time.Millisecond)
if canHas.Permissionship == v1.CheckPermissionResponse_PERMISSIONSHIP_NO_PERMISSION {
t.Log("service is subject to the new enemy problem")
return false, attempts
}
select {
case <-ctx.Done():
return false, attempts
default:
// this is not strictly needed, but helps avoid write contention
time.Sleep(100 * time.Millisecond)
continue
}
}
return true, maxAttempts
}
func BenchmarkBatchWrites(b *testing.B) {
ctx, cancel := context.WithCancel(testCtx)
defer cancel()
crdb := initializeTestCRDBCluster(ctx, b)
spicedb := spice.NewClusterFromCockroachCluster(crdb, spice.WithDbName(dbName))
tlog := e2e.NewTLog(b)
require.NoError(b, spicedb.Start(ctx, tlog, ""))
require.NoError(b, spicedb.Connect(ctx, tlog))
directs, excludes := generateTuples("", b.N*20, objIdGenerator)
b.ResetTimer()
_, err := spicedb[0].Client().V0().ACL().Write(testCtx, &v0.WriteRequest{
Updates: excludes,
})
if err != nil {
b.Log(err)
}
_, err = spicedb[0].Client().V0().ACL().Write(testCtx, &v0.WriteRequest{
Updates: directs,
})
if err != nil {
b.Log(err)
}
}
func BenchmarkConflictingTupleWrites(b *testing.B) {
ctx, cancel := context.WithCancel(testCtx)
defer cancel()
crdb := initializeTestCRDBCluster(ctx, b)
spicedb := spice.NewClusterFromCockroachCluster(crdb, spice.WithDbName(dbName))
tlog := e2e.NewTLog(b)
require.NoError(b, spicedb.Start(ctx, tlog, ""))
require.NoError(b, spicedb.Connect(ctx, tlog))
// fill with tuples to ensure we span multiple ranges
fill(b, spicedb[0].Client().V0().ACL(), "", 2000, 100)
b.ResetTimer()
checkDataNoNewEnemy(ctx, b, generateSchemaData(1, 1), 1, crdb, spicedb, b.N)
}
func fill(t testing.TB, client v0.ACLServiceClient, prefix string, fillerCount, batchSize int) {
t.Log("filling prefix", prefix)
require := require.New(t)
directs, excludes := generateTuples(prefix, fillerCount, objIdGenerator)
for i := 0; i < fillerCount/batchSize; i++ {
t.Log("filling", i*batchSize, "to", (i+1)*batchSize)
_, err := client.Write(testCtx, &v0.WriteRequest{
Updates: excludes[i*batchSize : (i+1)*batchSize],
})
require.NoError(err)
_, err = client.Write(testCtx, &v0.WriteRequest{
Updates: directs[i*batchSize : (i+1)*batchSize],
})
require.NoError(err)
}
}
// fillSchema generates the schema text for given SchemaData and applies it
func fillSchema(t testing.TB, template *template.Template, data []SchemaData, schemaClient v1alpha1.SchemaServiceClient) {
var b strings.Builder
batchSize := len(data[0].Prefixes)
for i, d := range data {
t.Logf("filling %d to %d", i*batchSize, (i+1)*batchSize)
b.Reset()
require.NoError(t, template.Execute(&b, d))
_, err := schemaClient.WriteSchema(context.Background(), &v1alpha1.WriteSchemaRequest{
Schema: b.String(),
})
require.NoError(t, err)
}
}
// prefixForNode finds a prefix with namespace leaders on the node id
func prefixForNode(ctx context.Context, conn *pgx.Conn, data []SchemaData, node int) string {
for {
// get a random prefix
d := data[rand.Intn(len(data))]
candidate := d.Prefixes[rand.Intn(len(d.Prefixes))]
ns1 := candidate + "/user"
ns2 := candidate + "/resource"
leader1 := getLeaderNodeForNamespace(ctx, conn, ns1)
leader2 := getLeaderNodeForNamespace(ctx, conn, ns2)
if leader1 == leader2 && leader1 == node {
return candidate
}
select {
case <-ctx.Done():
return ""
default:
continue
}
}
}
func generateSchemaData(n int, batchSize int) (data []SchemaData) {
data = make([]SchemaData, 0, n/batchSize)
for i := 0; i < n/batchSize; i++ {
schema := SchemaData{Prefixes: make([]string, 0, batchSize)}
for j := i * batchSize; j < (i+1)*batchSize; j++ {
schema.Prefixes = append(schema.Prefixes, prefixGenerator.Next())
}
data = append(data, schema)
}
return
}
func generateTuple(prefix string, objIdGenerator *generator.UniqueGenerator) (direct *v0.RelationTupleUpdate, exclude *v0.RelationTupleUpdate) {
directs, excludes := generateTuples(prefix, 1, objIdGenerator)
return directs[0], excludes[0]
}
func generateTuples(prefix string, n int, objIdGenerator *generator.UniqueGenerator) (directs []*v0.RelationTupleUpdate, excludes []*v0.RelationTupleUpdate) {
directs = make([]*v0.RelationTupleUpdate, 0, n)
excludes = make([]*v0.RelationTupleUpdate, 0, n)
for i := 0; i < n; i++ {
user := &v0.User{
UserOneof: &v0.User_Userset{
Userset: &v0.ObjectAndRelation{
Namespace: prefix + "/user",
ObjectId: objIdGenerator.Next(),
Relation: "...",
},
},
}
tupleExclude := &v0.RelationTuple{
ObjectAndRelation: &v0.ObjectAndRelation{
Namespace: prefix + "/resource",
ObjectId: "thegoods",
Relation: "excluded",
},
User: user,
}
tupleDirect := &v0.RelationTuple{
ObjectAndRelation: &v0.ObjectAndRelation{
Namespace: prefix + "/resource",
ObjectId: "thegoods",
Relation: "direct",
},
User: user,
}
excludes = append(excludes, &v0.RelationTupleUpdate{
Operation: v0.RelationTupleUpdate_TOUCH,
Tuple: tupleExclude,
})
directs = append(directs, &v0.RelationTupleUpdate{
Operation: v0.RelationTupleUpdate_TOUCH,
Tuple: tupleDirect,
})
}
return
}
// getLeaderNode returns the node with the lease leader for the range containing the tuple
func getLeaderNode(ctx context.Context, conn *pgx.Conn, tuple *v0.RelationTuple) int {
t := tuple
rows, err := conn.Query(ctx, "SHOW RANGE FROM TABLE relation_tuple FOR ROW ($1::text,$2::text,$3::text,$4::text,$5::text,$6::text)",
t.ObjectAndRelation.Namespace,
t.ObjectAndRelation.ObjectId,
t.ObjectAndRelation.Relation,
t.User.GetUserset().Namespace,
t.User.GetUserset().ObjectId,
t.User.GetUserset().Relation,
)
defer rows.Close()
if err != nil {
log.Fatalf("failed to exec: %v", err)
}
return leaderFromRangeRow(rows)
}
// getLeaderNodeForNamespace returns the node with the lease leader for the range containing the namespace
func getLeaderNodeForNamespace(ctx context.Context, conn *pgx.Conn, namespace string) int {
rows, err := conn.Query(ctx, "SHOW RANGE FROM TABLE namespace_config FOR ROW ($1::text)",
namespace,
)
defer rows.Close()
if err != nil {
log.Fatalf("failed to exec: %v", err)
}
return leaderFromRangeRow(rows)
}
// leaderFromRangeRow parses the rows from a `SHOW RANGE` query and returns the
// leader node id for the range
func leaderFromRangeRow(rows pgx.Rows) int {
var (
startKey sql.NullString
endKey sql.NullString
rangeID int
leaseHolder int
leaseHoldeLocality sql.NullString
replicas pgtype.Int8Array
replicaLocalities pgtype.TextArray
)
for rows.Next() {
if err := rows.Scan(&startKey, &endKey, &rangeID, &leaseHolder, &leaseHoldeLocality, &replicas, &replicaLocalities); err != nil {
panic(err)
}
break
}
return leaseHolder
}
func getErr(vals ...interface{}) error {
if len(vals) == 0 {
return nil
}
err := vals[len(vals)-1]
if err == nil {
return nil
}
if err, ok := err.(error); ok {
return err
}
return nil
}