forked from FeatureBaseDB/featurebase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidalloc_test.go
202 lines (192 loc) · 6 KB
/
idalloc_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
// Copyright 2022 Molecula Corp. (DBA FeatureBase).
// SPDX-License-Identifier: Apache-2.0
package pilosa
import (
"crypto/rand"
"io"
"reflect"
"testing"
"time"
"github.com/featurebasedb/featurebase/v3/testhook"
bolt "go.etcd.io/bbolt"
)
func TestIDAlloc(t *testing.T) {
// Acquire a temporary file.
f, err := testhook.TempFile(t, "idalloc")
if err != nil {
t.Errorf("acquiring temporary file: %v", err)
return
}
defer func() {
cerr := f.Close()
if cerr != nil {
t.Errorf("closing temporary file: %v", cerr)
}
}()
// Open bolt.
db, err := bolt.Open(f.Name(), 0600, &bolt.Options{Timeout: 1 * time.Second, NoSync: true})
if err != nil {
t.Errorf("opening bolt: %v", err)
return
}
defer func() {
cerr := db.Close()
if cerr != nil {
t.Errorf("closing bolt: %v", cerr)
}
}()
alloc := idAllocator{db: db}
a := IDAllocKey{
Index: "h",
Key: "a",
}
b := IDAllocKey{
Index: "h",
Key: "b",
}
var sessions [11][32]byte
for i := range sessions {
_, err = io.ReadFull(rand.Reader, sessions[i][:])
if err != nil {
t.Errorf("failed to obtain entropy: %v", err)
return
}
}
// Reserve 2 batches of IDs and then commit both.
if ranges, err := alloc.reserve(a, sessions[0], ^uint64(0), 3); err != nil {
t.Errorf("failed to reserve 3 IDs: %v", err)
} else {
expect := []IDRange{{1, 3}}
if !reflect.DeepEqual(expect, ranges) {
t.Errorf("expeced %v but got %v", expect, ranges)
}
}
if ranges, err := alloc.reserve(b, sessions[1], ^uint64(0), 3); err != nil {
t.Errorf("failed to reserve 3 IDs: %v", err)
} else {
expect := []IDRange{{4, 6}}
if !reflect.DeepEqual(expect, ranges) {
t.Errorf("expeced %v but got %v", expect, ranges)
}
}
if err := alloc.commit(a, sessions[0], 3); err != nil {
t.Errorf("failed to commit reservation of 3 IDs: %v", err)
}
if err := alloc.commit(b, sessions[1], 3); err != nil {
t.Errorf("failed to commit reservation of 3 IDs: %v", err)
}
// Test collision handling.
if ranges, err := alloc.reserve(a, sessions[2], ^uint64(0), 4); err != nil {
t.Errorf("failed to reserve 4 IDs: %v", err)
} else {
expect := []IDRange{{7, 10}}
if !reflect.DeepEqual(expect, ranges) {
t.Errorf("expeced %v but got %v", expect, ranges)
}
}
if ranges, err := alloc.reserve(a, sessions[3], ^uint64(0), 4); err != nil {
t.Errorf("failed to reserve 4 IDs: %v", err)
} else {
// Instead of reserving new ranges, we have taken over the previous reservation.
expect := []IDRange{{7, 10}}
if !reflect.DeepEqual(expect, ranges) {
t.Errorf("expeced %v but got %v", expect, ranges)
}
}
if err := alloc.commit(a, sessions[2], 4); err == nil {
// This was taken over by reservation 3, so this should have warned us that something went wrong.
t.Error("unexpected commit success for clobbered reservation")
}
if err := alloc.commit(a, sessions[3], 4); err != nil {
t.Errorf("failed to commit reservation of 4 IDs: %v", err)
}
// Test key return.
if ranges, err := alloc.reserve(a, sessions[4], ^uint64(0), 100); err != nil {
t.Errorf("failed to reserve 100 IDs: %v", err)
} else {
// Instead of reserving new ranges, we have taken over the previous reservation.
expect := []IDRange{{11, 110}}
if !reflect.DeepEqual(expect, ranges) {
t.Errorf("expeced %v but got %v", expect, ranges)
}
}
if err := alloc.commit(a, sessions[4], 1); err != nil {
t.Errorf("failed to commit reservation of 1 ID: %v", err)
}
if ranges, err := alloc.reserve(a, sessions[5], ^uint64(0), 100); err != nil {
t.Errorf("failed to reserve 100 IDs: %v", err)
} else {
// Instead of reserving new ranges, we have taken over the previous reservation.
expect := []IDRange{{12, 111}}
if !reflect.DeepEqual(expect, ranges) {
t.Errorf("expeced %v but got %v", expect, ranges)
}
}
if err := alloc.commit(a, sessions[5], 100); err != nil {
t.Errorf("failed to commit reservation of 100 IDs: %v", err)
}
// Test offset-based allocation.
p1 := IDAllocKey{
Index: "xyzzy",
Key: "kafka-partition-1",
}
p2 := IDAllocKey{
Index: "xyzzy",
Key: "kafka-partition-2",
}
// Start processsing on 2 partitions.
if ranges, err := alloc.reserve(p1, sessions[6], 0, 3); err != nil {
t.Errorf("failed to reserve 3 IDs: %v", err)
} else {
expect := []IDRange{{1, 3}}
if !reflect.DeepEqual(expect, ranges) {
t.Errorf("expeced %v but got %v", expect, ranges)
}
}
if ranges, err := alloc.reserve(p2, sessions[7], 0, 3); err != nil {
t.Errorf("failed to reserve 3 IDs: %v", err)
} else {
expect := []IDRange{{4, 6}}
if !reflect.DeepEqual(expect, ranges) {
t.Errorf("expeced %v but got %v", expect, ranges)
}
}
// Partition 2 finishes a batch of 2 IDs.
if err := alloc.commit(p2, sessions[7], 2); err != nil {
t.Errorf("failed to commit reservation of 2 IDs: %v", err)
}
// Partition 2 reserves more IDs.
if ranges, err := alloc.reserve(p2, sessions[8], 2, 3); err != nil {
t.Errorf("failed to reserve 3 IDs: %v", err)
} else {
// This could be coalesced here, but that is not true in the general case.
expect := []IDRange{{6, 6}, {7, 8}}
if !reflect.DeepEqual(expect, ranges) {
t.Errorf("expeced %v but got %v", expect, ranges)
}
}
// Partition 1 ingester failed and was restarted.
if ranges, err := alloc.reserve(p1, sessions[9], 0, 3); err != nil {
t.Errorf("failed to reserve 3 IDs: %v", err)
} else {
expect := []IDRange{{1, 3}}
if !reflect.DeepEqual(expect, ranges) {
t.Errorf("expeced %v but got %v", expect, ranges)
}
}
// This time, the partition 1 ingester finishes the batch.
// It commits the offset to Kafka but fails to commit to the ID allocator.
// It is restarted at Kafka's next offset.
if ranges, err := alloc.reserve(p1, sessions[10], 3, 4); err != nil {
t.Errorf("failed to reserve 3 IDs: %v", err)
} else {
expect := []IDRange{{9, 12}}
if !reflect.DeepEqual(expect, ranges) {
t.Errorf("expeced %v but got %v", expect, ranges)
}
}
// Partition 1 finishes a batch of 4 IDs.
if err := alloc.commit(p1, sessions[10], 4); err != nil {
t.Errorf("failed to commit reservation of 4 IDs: %v", err)
}
}