FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
prometheus/tsdb/chunks/queue_test.go at main · dgonzdev/prometheus · GitHub
dgonzdev
/
prometheus
Public
forked from
prometheus/prometheus
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
prometheus
/
tsdb
/
chunks
/
queue_test.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
323 lines (247 loc) · 7.52 KB
Breadcrumbs
prometheus
/
tsdb
/
chunks
/
queue_test.go
Copy path
File metadata and controls
323 lines (247 loc) · 7.52 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// Copyright 2022 The Prometheus 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
chunks
import
(
"math/rand"
"sync"
"testing"
"time"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
)
func
(
q
*
writeJobQueue
)
assertInvariants
(
t
*
testing.
T
) {
q
.
mtx
.
Lock
()
defer
q
.
mtx
.
Unlock
()
totalSize
:=
0
for
s
:=
q
.
first
;
s
!=
nil
;
s
=
s
.
nextSegment
{
require
.
NotNil
(
t
,
s
.
segment
)
// Next read index is lower or equal than next write index (we cannot past written jobs)
require
.
LessOrEqual
(
t
,
s
.
nextRead
,
s
.
nextWrite
)
// Number of unread elements in this segment.
totalSize
+=
s
.
nextWrite
-
s
.
nextRead
// First segment can be partially read, other segments were not read yet.
if
s
==
q
.
first
{
require
.
GreaterOrEqual
(
t
,
s
.
nextRead
,
0
)
}
else
{
require
.
Equal
(
t
,
0
,
s
.
nextRead
)
}
// If first shard is empty (everything was read from it already), it must have extra capacity for
// additional elements, otherwise it would have been removed.
if
s
==
q
.
first
&&
s
.
nextRead
==
s
.
nextWrite
{
require
.
Less
(
t
,
s
.
nextWrite
,
len
(
s
.
segment
))
}
// Segments in the middle are full.
if
s
!=
q
.
first
&&
s
!=
q
.
last
{
require
.
Len
(
t
,
s
.
segment
,
s
.
nextWrite
)
}
// Last segment must have at least one element, or we wouldn't have created it.
require
.
Positive
(
t
,
s
.
nextWrite
)
}
require
.
Equal
(
t
,
q
.
size
,
totalSize
)
}
func
TestQueuePushPopSingleGoroutine
(
t
*
testing.
T
) {
seed
:=
time
.
Now
().
UnixNano
()
t
.
Log
(
"seed:"
,
seed
)
r
:=
rand
.
New
(
rand
.
NewSource
(
seed
))
const
maxSize
=
500
const
maxIters
=
50
for
maxCount
:=
1
;
maxCount
<
maxSize
;
maxCount
++
{
queue
:=
newWriteJobQueue
(
maxCount
,
1
+
(
r
.
Int
()
%
maxCount
))
elements
:=
0
// total elements in the queue
lastWriteID
:=
0
lastReadID
:=
0
for
iter
:=
0
;
iter
<
maxIters
;
iter
++
{
if
elements
<
maxCount
{
toWrite
:=
r
.
Int
()
%
(
maxCount
-
elements
)
if
toWrite
==
0
{
toWrite
=
1
}
for
i
:=
0
;
i
<
toWrite
;
i
++
{
lastWriteID
++
require
.
True
(
t
,
queue
.
push
(
chunkWriteJob
{
seriesRef
:
HeadSeriesRef
(
lastWriteID
)}))
elements
++
}
}
if
elements
>
0
{
toRead
:=
r
.
Int
()
%
elements
if
toRead
==
0
{
toRead
=
1
}
for
i
:=
0
;
i
<
toRead
;
i
++
{
lastReadID
++
j
,
b
:=
queue
.
pop
()
require
.
True
(
t
,
b
)
require
.
Equal
(
t
,
HeadSeriesRef
(
lastReadID
),
j
.
seriesRef
)
elements
--
}
}
require
.
Equal
(
t
,
elements
,
queue
.
length
())
queue
.
assertInvariants
(
t
)
}
}
}
func
TestQueuePushBlocksOnFullQueue
(
t
*
testing.
T
) {
queue
:=
newWriteJobQueue
(
5
,
5
)
pushTime
:=
make
(
chan
time.
Time
)
go
func
() {
require
.
True
(
t
,
queue
.
push
(
chunkWriteJob
{
seriesRef
:
1
}))
require
.
True
(
t
,
queue
.
push
(
chunkWriteJob
{
seriesRef
:
2
}))
require
.
True
(
t
,
queue
.
push
(
chunkWriteJob
{
seriesRef
:
3
}))
require
.
True
(
t
,
queue
.
push
(
chunkWriteJob
{
seriesRef
:
4
}))
require
.
True
(
t
,
queue
.
push
(
chunkWriteJob
{
seriesRef
:
5
}))
pushTime
<-
time
.
Now
()
// This will block
require
.
True
(
t
,
queue
.
push
(
chunkWriteJob
{
seriesRef
:
6
}))
pushTime
<-
time
.
Now
()
}()
timeBeforePush
:=
<-
pushTime
delay
:=
100
*
time
.
Millisecond
select
{
case
<-
time
.
After
(
delay
):
// ok
case
<-
pushTime
:
require
.
Fail
(
t
,
"didn't expect another push to proceed"
)
}
popTime
:=
time
.
Now
()
j
,
b
:=
queue
.
pop
()
require
.
True
(
t
,
b
)
require
.
Equal
(
t
,
HeadSeriesRef
(
1
),
j
.
seriesRef
)
timeAfterPush
:=
<-
pushTime
require
.
GreaterOrEqual
(
t
,
timeAfterPush
.
Sub
(
popTime
),
time
.
Duration
(
0
))
require
.
GreaterOrEqual
(
t
,
timeAfterPush
.
Sub
(
timeBeforePush
),
delay
)
}
func
TestQueuePopBlocksOnEmptyQueue
(
t
*
testing.
T
) {
queue
:=
newWriteJobQueue
(
5
,
5
)
popTime
:=
make
(
chan
time.
Time
)
go
func
() {
j
,
b
:=
queue
.
pop
()
require
.
True
(
t
,
b
)
require
.
Equal
(
t
,
HeadSeriesRef
(
1
),
j
.
seriesRef
)
popTime
<-
time
.
Now
()
// This will block
j
,
b
=
queue
.
pop
()
require
.
True
(
t
,
b
)
require
.
Equal
(
t
,
HeadSeriesRef
(
2
),
j
.
seriesRef
)
popTime
<-
time
.
Now
()
}()
queue
.
push
(
chunkWriteJob
{
seriesRef
:
1
})
timeBeforePop
:=
<-
popTime
delay
:=
100
*
time
.
Millisecond
select
{
case
<-
time
.
After
(
delay
):
// ok
case
<-
popTime
:
require
.
Fail
(
t
,
"didn't expect another pop to proceed"
)
}
pushTime
:=
time
.
Now
()
require
.
True
(
t
,
queue
.
push
(
chunkWriteJob
{
seriesRef
:
2
}))
timeAfterPop
:=
<-
popTime
require
.
GreaterOrEqual
(
t
,
timeAfterPop
.
Sub
(
pushTime
),
time
.
Duration
(
0
))
require
.
Greater
(
t
,
timeAfterPop
.
Sub
(
timeBeforePop
),
delay
)
}
func
TestQueuePopUnblocksOnClose
(
t
*
testing.
T
) {
queue
:=
newWriteJobQueue
(
5
,
5
)
popTime
:=
make
(
chan
time.
Time
)
go
func
() {
j
,
b
:=
queue
.
pop
()
require
.
True
(
t
,
b
)
require
.
Equal
(
t
,
HeadSeriesRef
(
1
),
j
.
seriesRef
)
popTime
<-
time
.
Now
()
// This will block until queue is closed.
j
,
b
=
queue
.
pop
()
require
.
False
(
t
,
b
)
popTime
<-
time
.
Now
()
}()
queue
.
push
(
chunkWriteJob
{
seriesRef
:
1
})
timeBeforePop
:=
<-
popTime
delay
:=
100
*
time
.
Millisecond
select
{
case
<-
time
.
After
(
delay
):
// ok
case
<-
popTime
:
require
.
Fail
(
t
,
"didn't expect another pop to proceed"
)
}
closeTime
:=
time
.
Now
()
queue
.
close
()
timeAfterPop
:=
<-
popTime
require
.
GreaterOrEqual
(
t
,
timeAfterPop
.
Sub
(
closeTime
),
time
.
Duration
(
0
))
require
.
GreaterOrEqual
(
t
,
timeAfterPop
.
Sub
(
timeBeforePop
),
delay
)
}
func
TestQueuePopAfterCloseReturnsAllElements
(
t
*
testing.
T
) {
const
count
=
10
queue
:=
newWriteJobQueue
(
count
,
count
)
for
i
:=
0
;
i
<
count
;
i
++
{
require
.
True
(
t
,
queue
.
push
(
chunkWriteJob
{
seriesRef
:
HeadSeriesRef
(
i
)}))
}
// close the queue before popping all elements.
queue
.
close
()
// No more pushing allowed after close.
require
.
False
(
t
,
queue
.
push
(
chunkWriteJob
{
seriesRef
:
HeadSeriesRef
(
11111
)}))
// Verify that we can still read all pushed elements.
for
i
:=
0
;
i
<
count
;
i
++
{
j
,
b
:=
queue
.
pop
()
require
.
True
(
t
,
b
)
require
.
Equal
(
t
,
HeadSeriesRef
(
i
),
j
.
seriesRef
)
}
_
,
b
:=
queue
.
pop
()
require
.
False
(
t
,
b
)
}
func
TestQueuePushPopManyGoroutines
(
t
*
testing.
T
) {
const
readGoroutines
=
5
const
writeGoroutines
=
10
const
writes
=
500
queue
:=
newWriteJobQueue
(
1024
,
64
)
// Reading goroutine
refsMx
:=
sync.
Mutex
{}
refs
:=
map
[
HeadSeriesRef
]
bool
{}
readersWG
:=
sync.
WaitGroup
{}
for
i
:=
0
;
i
<
readGoroutines
;
i
++
{
readersWG
.
Add
(
1
)
go
func
() {
defer
readersWG
.
Done
()
for
j
,
ok
:=
queue
.
pop
();
ok
;
j
,
ok
=
queue
.
pop
() {
refsMx
.
Lock
()
refs
[
j
.
seriesRef
]
=
true
refsMx
.
Unlock
()
}
}()
}
id
:=
atomic.
Uint64
{}
writersWG
:=
sync.
WaitGroup
{}
for
i
:=
0
;
i
<
writeGoroutines
;
i
++
{
writersWG
.
Add
(
1
)
go
func
() {
defer
writersWG
.
Done
()
for
i
:=
0
;
i
<
writes
;
i
++
{
ref
:=
id
.
Inc
()
require
.
True
(
t
,
queue
.
push
(
chunkWriteJob
{
seriesRef
:
HeadSeriesRef
(
ref
)}))
}
}()
}
// Wait until all writes are done.
writersWG
.
Wait
()
// Close the queue and wait for reading to be done.
queue
.
close
()
readersWG
.
Wait
()
// Check if we have all expected values
require
.
Len
(
t
,
refs
,
writeGoroutines
*
writes
)
}
func
TestQueueSegmentIsKeptEvenIfEmpty
(
t
*
testing.
T
) {
queue
:=
newWriteJobQueue
(
1024
,
64
)
require
.
True
(
t
,
queue
.
push
(
chunkWriteJob
{
seriesRef
:
1
}))
_
,
b
:=
queue
.
pop
()
require
.
True
(
t
,
b
)
require
.
NotNil
(
t
,
queue
.
first
)
require
.
Equal
(
t
,
1
,
queue
.
first
.
nextRead
)
require
.
Equal
(
t
,
1
,
queue
.
first
.
nextWrite
)
}
Back
|
FazBrowse Home
|
New Git URL