@@ -27,35 +27,36 @@ type testProcessorI interface {
27
27
processTestEvent (roachpb.RangeID , * raftSchedulerShard , raftScheduleState )
28
28
}
29
29
30
- type rangeIDChunk struct {
30
+ type rangeIDChunk [ T any ] struct {
31
31
// Valid contents are buf[rd:wr], read at buf[rd], write at buf[wr].
32
- buf [rangeIDChunkSize ]roachpb. RangeID
32
+ buf [rangeIDChunkSize ]T
33
33
rd , wr int
34
34
}
35
35
36
- func (c * rangeIDChunk ) PushBack (id roachpb. RangeID ) bool {
36
+ func (c * rangeIDChunk [ T ] ) PushBack (item T ) bool {
37
37
if c .WriteCap () == 0 {
38
38
return false
39
39
}
40
- c .buf [c .wr ] = id
40
+ c .buf [c .wr ] = item
41
41
c .wr ++
42
42
return true
43
43
}
44
44
45
- func (c * rangeIDChunk ) PopFront () (roachpb. RangeID , bool ) {
45
+ func (c * rangeIDChunk [ T ] ) PopFront () (T , bool ) {
46
46
if c .Len () == 0 {
47
- return 0 , false
47
+ var empty T
48
+ return empty , false
48
49
}
49
50
id := c .buf [c .rd ]
50
51
c .rd ++
51
52
return id , true
52
53
}
53
54
54
- func (c * rangeIDChunk ) WriteCap () int {
55
+ func (c * rangeIDChunk [ T ] ) WriteCap () int {
55
56
return len (c .buf ) - c .wr
56
57
}
57
58
58
- func (c * rangeIDChunk ) Len () int {
59
+ func (c * rangeIDChunk [ T ] ) Len () int {
59
60
return c .wr - c .rd
60
61
}
61
62
@@ -67,30 +68,31 @@ func (c *rangeIDChunk) Len() int {
67
68
//
68
69
// The queue implements a FIFO queueing policy with no prioritization of some
69
70
// ranges over others.
70
- type rangeIDQueue struct {
71
+ type rangeIDQueue [ T any ] struct {
71
72
len int
72
- chunks list.List
73
+ chunks list.List // TODO(pav-kv): use a typed generic list
73
74
}
74
75
75
- func (q * rangeIDQueue ) Push (id roachpb. RangeID ) {
76
+ func (q * rangeIDQueue [ T ] ) Push (item T ) {
76
77
q .len ++
77
78
if q .chunks .Len () == 0 || q .back ().WriteCap () == 0 {
78
- q .chunks .PushBack (& rangeIDChunk {})
79
+ q .chunks .PushBack (& rangeIDChunk [ T ] {})
79
80
}
80
- if ! q .back ().PushBack (id ) {
81
+ if ! q .back ().PushBack (item ) {
81
82
panic (fmt .Sprintf (
82
83
"unable to push rangeID to chunk: len=%d, cap=%d" ,
83
84
q .back ().Len (), q .back ().WriteCap ()))
84
85
}
85
86
}
86
87
87
- func (q * rangeIDQueue ) PopFront () (roachpb. RangeID , bool ) {
88
+ func (q * rangeIDQueue [ T ] ) PopFront () (T , bool ) {
88
89
if q .len == 0 {
89
- return 0 , false
90
+ var empty T
91
+ return empty , false
90
92
}
91
93
q .len --
92
94
frontElem := q .chunks .Front ()
93
- front := frontElem .Value .(* rangeIDChunk )
95
+ front := frontElem .Value .(* rangeIDChunk [ T ] )
94
96
id , ok := front .PopFront ()
95
97
if ! ok {
96
98
panic ("encountered empty chunk" )
@@ -101,12 +103,12 @@ func (q *rangeIDQueue) PopFront() (roachpb.RangeID, bool) {
101
103
return id , true
102
104
}
103
105
104
- func (q * rangeIDQueue ) Len () int {
106
+ func (q * rangeIDQueue [ T ] ) Len () int {
105
107
return q .len
106
108
}
107
109
108
- func (q * rangeIDQueue ) back () * rangeIDChunk {
109
- return q .chunks .Back ().Value .(* rangeIDChunk )
110
+ func (q * rangeIDQueue [ T ] ) back () * rangeIDChunk [ T ] {
111
+ return q .chunks .Back ().Value .(* rangeIDChunk [ T ] )
110
112
}
111
113
112
114
type raftProcessor interface {
@@ -235,7 +237,7 @@ type raftScheduler struct {
235
237
type raftSchedulerShard struct {
236
238
syncutil.Mutex
237
239
cond * sync.Cond
238
- queue rangeIDQueue
240
+ queue rangeIDQueue [roachpb. RangeID ]
239
241
state map [roachpb.RangeID ]raftScheduleState
240
242
numWorkers int
241
243
maxTicks int64
0 commit comments