Skip to content

Commit

Permalink
restore dupe detection
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Vaillancourt <[email protected]>
  • Loading branch information
timvaillancourt committed Feb 19, 2025
1 parent a443706 commit faff7e6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
14 changes: 13 additions & 1 deletion go/vt/vtorc/discovery/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ push() operation never blocks while pop() blocks on an empty queue.
package discovery

import (
"sync"
"time"

"vitess.io/vitess/go/vt/log"
Expand All @@ -40,7 +41,8 @@ type queueItem struct {

// Queue is an implementation of discovery.Queue.
type Queue struct {
queue chan queueItem
enqueued sync.Map
queue chan queueItem
}

// NewQueue creates a new queue.
Expand All @@ -58,10 +60,14 @@ func (q *Queue) QueueLen() int {
// Push enqueues a key if it is not on a queue and is not being
// processed; silently returns otherwise.
func (q *Queue) Push(key string) {
if _, found := q.enqueued.Load(key); found {
return
}
q.queue <- queueItem{
PushedAt: time.Now(),
Key: key,
}
q.enqueued.Store(key, struct{}{})
}

// Consume fetches a key to process; blocks if queue is empty.
Expand All @@ -76,3 +82,9 @@ func (q *Queue) Consume() string {

return item.Key
}

// Release removes a key from a list of being processed keys
// which allows that key to be pushed into the queue again.
func (q *Queue) Release(key string) {
q.enqueued.Delete(key)
}
14 changes: 14 additions & 0 deletions go/vt/vtorc/discovery/queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,22 @@ func TestQueue(t *testing.T) {
// Push
q.Push(t.Name())
require.Equal(t, 1, q.QueueLen())
_, found := q.enqueued.Load(t.Name())
require.True(t, found)

// Push duplicate
q.Push(t.Name())
require.Equal(t, 1, q.QueueLen())

// Consume
require.Equal(t, t.Name(), q.Consume())
require.Zero(t, q.QueueLen())
_, found = q.enqueued.Load(t.Name())
require.True(t, found)

// Release
q.Release(t.Name())
require.Zero(t, q.QueueLen())
_, found = q.enqueued.Load(t.Name())
require.False(t, found)
}
1 change: 1 addition & 0 deletions go/vt/vtorc/logic/vtorc.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func handleDiscoveryRequests() {
for {
tabletAlias := discoveryQueue.Consume()
DiscoverInstance(tabletAlias, false /* forceDiscovery */)
discoveryQueue.Release(tabletAlias)
}
}()
}
Expand Down

0 comments on commit faff7e6

Please sign in to comment.