Skip to content

Commit a369dc3

Browse files
committed
kvserver: make rangeID queue generic
Epic: none Release note: none
1 parent c9e7aad commit a369dc3

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

pkg/kv/kvserver/scheduler.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,36 @@ type testProcessorI interface {
2727
processTestEvent(roachpb.RangeID, *raftSchedulerShard, raftScheduleState)
2828
}
2929

30-
type rangeIDChunk struct {
30+
type rangeIDChunk[T any] struct {
3131
// Valid contents are buf[rd:wr], read at buf[rd], write at buf[wr].
32-
buf [rangeIDChunkSize]roachpb.RangeID
32+
buf [rangeIDChunkSize]T
3333
rd, wr int
3434
}
3535

36-
func (c *rangeIDChunk) PushBack(id roachpb.RangeID) bool {
36+
func (c *rangeIDChunk[T]) PushBack(item T) bool {
3737
if c.WriteCap() == 0 {
3838
return false
3939
}
40-
c.buf[c.wr] = id
40+
c.buf[c.wr] = item
4141
c.wr++
4242
return true
4343
}
4444

45-
func (c *rangeIDChunk) PopFront() (roachpb.RangeID, bool) {
45+
func (c *rangeIDChunk[T]) PopFront() (T, bool) {
4646
if c.Len() == 0 {
47-
return 0, false
47+
var empty T
48+
return empty, false
4849
}
4950
id := c.buf[c.rd]
5051
c.rd++
5152
return id, true
5253
}
5354

54-
func (c *rangeIDChunk) WriteCap() int {
55+
func (c *rangeIDChunk[T]) WriteCap() int {
5556
return len(c.buf) - c.wr
5657
}
5758

58-
func (c *rangeIDChunk) Len() int {
59+
func (c *rangeIDChunk[T]) Len() int {
5960
return c.wr - c.rd
6061
}
6162

@@ -67,30 +68,31 @@ func (c *rangeIDChunk) Len() int {
6768
//
6869
// The queue implements a FIFO queueing policy with no prioritization of some
6970
// ranges over others.
70-
type rangeIDQueue struct {
71+
type rangeIDQueue[T any] struct {
7172
len int
72-
chunks list.List
73+
chunks list.List // TODO(pav-kv): use a typed generic list
7374
}
7475

75-
func (q *rangeIDQueue) Push(id roachpb.RangeID) {
76+
func (q *rangeIDQueue[T]) Push(item T) {
7677
q.len++
7778
if q.chunks.Len() == 0 || q.back().WriteCap() == 0 {
78-
q.chunks.PushBack(&rangeIDChunk{})
79+
q.chunks.PushBack(&rangeIDChunk[T]{})
7980
}
80-
if !q.back().PushBack(id) {
81+
if !q.back().PushBack(item) {
8182
panic(fmt.Sprintf(
8283
"unable to push rangeID to chunk: len=%d, cap=%d",
8384
q.back().Len(), q.back().WriteCap()))
8485
}
8586
}
8687

87-
func (q *rangeIDQueue) PopFront() (roachpb.RangeID, bool) {
88+
func (q *rangeIDQueue[T]) PopFront() (T, bool) {
8889
if q.len == 0 {
89-
return 0, false
90+
var empty T
91+
return empty, false
9092
}
9193
q.len--
9294
frontElem := q.chunks.Front()
93-
front := frontElem.Value.(*rangeIDChunk)
95+
front := frontElem.Value.(*rangeIDChunk[T])
9496
id, ok := front.PopFront()
9597
if !ok {
9698
panic("encountered empty chunk")
@@ -101,12 +103,12 @@ func (q *rangeIDQueue) PopFront() (roachpb.RangeID, bool) {
101103
return id, true
102104
}
103105

104-
func (q *rangeIDQueue) Len() int {
106+
func (q *rangeIDQueue[T]) Len() int {
105107
return q.len
106108
}
107109

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])
110112
}
111113

112114
type raftProcessor interface {
@@ -235,7 +237,7 @@ type raftScheduler struct {
235237
type raftSchedulerShard struct {
236238
syncutil.Mutex
237239
cond *sync.Cond
238-
queue rangeIDQueue
240+
queue rangeIDQueue[roachpb.RangeID]
239241
state map[roachpb.RangeID]raftScheduleState
240242
numWorkers int
241243
maxTicks int64

pkg/kv/kvserver/scheduler_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestRangeIDChunk(t *testing.T) {
3434
defer leaktest.AfterTest(t)()
3535
defer log.Scope(t).Close(t)
3636

37-
var c rangeIDChunk
37+
var c rangeIDChunk[roachpb.RangeID]
3838
if c.Len() != 0 {
3939
t.Fatalf("expected empty chunk, but found %d", c.Len())
4040
}
@@ -90,7 +90,7 @@ func TestRangeIDQueue(t *testing.T) {
9090
defer leaktest.AfterTest(t)()
9191
defer log.Scope(t).Close(t)
9292

93-
var q rangeIDQueue
93+
var q rangeIDQueue[roachpb.RangeID]
9494
if q.Len() != 0 {
9595
t.Fatalf("expected empty queue, but found %d", q.Len())
9696
}
@@ -687,7 +687,7 @@ func runSchedulerEnqueueRaftTicks(
687687
// Flush the queue. We haven't started any workers that pull from it, so we
688688
// just clear it out.
689689
for _, shard := range s.shards {
690-
shard.queue = rangeIDQueue{}
690+
shard.queue = rangeIDQueue[roachpb.RangeID]{}
691691
}
692692
}
693693
ids.Close()

0 commit comments

Comments
 (0)