Commit 850f2bf1 authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #35382 from jbeda/nits

Automatic merge from submit-queue Expand documentation and TODOs in a few packages I was reading through unfamiliar code and mostly added TODOs and expanded and clarified documentations. There are a couple of things that are real code changes: - Removed some unused constants - Changed `workqueue.Parallize` to clamp the number of worker goroutines to the number of items to be processed. - Added another unit test to `workqueue.queue`. I thought I found a bug (I was wrong) and wrote a unit test to isolate. I figure the extra test is worth keeping.
parents 6ec02394 48d1505b
...@@ -29,7 +29,7 @@ import ( ...@@ -29,7 +29,7 @@ import (
type Config struct { type Config struct {
// The queue for your objects; either a FIFO or // The queue for your objects; either a FIFO or
// a DeltaFIFO. Your Process() function should accept // a DeltaFIFO. Your Process() function should accept
// the output of this Oueue's Pop() method. // the output of this Queue's Pop() method.
Queue Queue
// Something that can list and watch your objects. // Something that can list and watch your objects.
...@@ -121,6 +121,11 @@ func (c *Controller) Requeue(obj interface{}) error { ...@@ -121,6 +121,11 @@ func (c *Controller) Requeue(obj interface{}) error {
// TODO: Consider doing the processing in parallel. This will require a little thought // TODO: Consider doing the processing in parallel. This will require a little thought
// to make sure that we don't end up processing the same object multiple times // to make sure that we don't end up processing the same object multiple times
// concurrently. // concurrently.
//
// TODO: Plumb through the stopCh here (and down to the queue) so that this can
// actually exit when the controller is stopped. Or just give up on this stuff
// ever being stoppable. Converting this whole package to use Context would
// also be helpful.
func (c *Controller) processLoop() { func (c *Controller) processLoop() {
for { for {
obj, err := c.config.Queue.Pop(PopProcessFunc(c.config.Process)) obj, err := c.config.Queue.Pop(PopProcessFunc(c.config.Process))
...@@ -134,7 +139,7 @@ func (c *Controller) processLoop() { ...@@ -134,7 +139,7 @@ func (c *Controller) processLoop() {
} }
// ResourceEventHandler can handle notifications for events that happen to a // ResourceEventHandler can handle notifications for events that happen to a
// resource. The events are informational only, so you can't return an // resource. The events are informational only, so you can't return an
// error. // error.
// * OnAdd is called when an object is added. // * OnAdd is called when an object is added.
// * OnUpdate is called when an object is modified. Note that oldObj is the // * OnUpdate is called when an object is modified. Note that oldObj is the
......
...@@ -283,6 +283,9 @@ func TestHammerController(t *testing.T) { ...@@ -283,6 +283,9 @@ func TestHammerController(t *testing.T) {
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
close(stop) close(stop)
// TODO: Verify that no goroutines were leaked here and that everything shut
// down cleanly.
outputSetLock.Lock() outputSetLock.Lock()
t.Logf("got: %#v", outputSet) t.Logf("got: %#v", outputSet)
} }
......
...@@ -45,7 +45,7 @@ import ( ...@@ -45,7 +45,7 @@ import (
// Reflector watches a specified resource and causes all changes to be reflected in the given store. // Reflector watches a specified resource and causes all changes to be reflected in the given store.
type Reflector struct { type Reflector struct {
// name identifies this reflector. By default it will be a file:line if possible. // name identifies this reflector. By default it will be a file:line if possible.
name string name string
// The type of object we expect to place in the store. // The type of object we expect to place in the store.
...@@ -74,12 +74,6 @@ var ( ...@@ -74,12 +74,6 @@ var (
// However, it can be modified to avoid periodic resync to break the // However, it can be modified to avoid periodic resync to break the
// TCP connection. // TCP connection.
minWatchTimeout = 5 * time.Minute minWatchTimeout = 5 * time.Minute
// If we are within 'forceResyncThreshold' from the next planned resync
// and are just before issuing Watch(), resync will be forced now.
forceResyncThreshold = 3 * time.Second
// We try to set timeouts for Watch() so that we will finish about
// than 'timeoutThreshold' from next planned periodic resync.
timeoutThreshold = 1 * time.Second
) )
// NewNamespaceKeyedIndexerAndReflector creates an Indexer and a Reflector // NewNamespaceKeyedIndexerAndReflector creates an Indexer and a Reflector
...@@ -114,7 +108,7 @@ func NewNamedReflector(name string, lw ListerWatcher, expectedType interface{}, ...@@ -114,7 +108,7 @@ func NewNamedReflector(name string, lw ListerWatcher, expectedType interface{},
return r return r
} }
// internalPackages are packages that ignored when creating a default reflector name. These packages are in the common // internalPackages are packages that ignored when creating a default reflector name. These packages are in the common
// call chains to NewReflector, so they'd be low entropy names for reflectors // call chains to NewReflector, so they'd be low entropy names for reflectors
var internalPackages = []string{"kubernetes/pkg/client/cache/", "/runtime/asm_"} var internalPackages = []string{"kubernetes/pkg/client/cache/", "/runtime/asm_"}
......
...@@ -24,7 +24,7 @@ import ( ...@@ -24,7 +24,7 @@ import (
utilruntime "k8s.io/kubernetes/pkg/util/runtime" utilruntime "k8s.io/kubernetes/pkg/util/runtime"
) )
// DelayingInterface is an Interface that can Add an item at a later time. This makes it easier to // DelayingInterface is an Interface that can Add an item at a later time. This makes it easier to
// requeue items after failures without ending up in a hot-loop. // requeue items after failures without ending up in a hot-loop.
type DelayingInterface interface { type DelayingInterface interface {
Interface Interface
...@@ -68,6 +68,9 @@ type delayingType struct { ...@@ -68,6 +68,9 @@ type delayingType struct {
stopCh chan struct{} stopCh chan struct{}
// heartbeat ensures we wait no more than maxWait before firing // heartbeat ensures we wait no more than maxWait before firing
//
// TODO: replace with Ticker (and add to clock) so this can be cleaned up.
// clock.Tick will leak.
heartbeat <-chan time.Time heartbeat <-chan time.Time
// waitingForAdd is an ordered slice of items to be added to the contained work queue // waitingForAdd is an ordered slice of items to be added to the contained work queue
...@@ -115,7 +118,7 @@ func (q *delayingType) AddAfter(item interface{}, duration time.Duration) { ...@@ -115,7 +118,7 @@ func (q *delayingType) AddAfter(item interface{}, duration time.Duration) {
} }
} }
// maxWait keeps a max bound on the wait time. It's just insurance against weird things happening. // maxWait keeps a max bound on the wait time. It's just insurance against weird things happening.
// Checking the queue every 10 seconds isn't expensive and we know that we'll never end up with an // Checking the queue every 10 seconds isn't expensive and we know that we'll never end up with an
// expired item sitting for more than 10 seconds. // expired item sitting for more than 10 seconds.
const maxWait = 10 * time.Second const maxWait = 10 * time.Second
...@@ -192,6 +195,9 @@ func (q *delayingType) waitingLoop() { ...@@ -192,6 +195,9 @@ func (q *delayingType) waitingLoop() {
// inserts the given entry into the sorted entries list // inserts the given entry into the sorted entries list
// same semantics as append()... the given slice may be modified, // same semantics as append()... the given slice may be modified,
// and the returned value should be used // and the returned value should be used
//
// TODO: This should probably be converted to use container/heap to improve
// running time for a large number of items.
func insert(entries []waitFor, knownEntries map[t]time.Time, entry waitFor) []waitFor { func insert(entries []waitFor, knownEntries map[t]time.Time, entry waitFor) []waitFor {
// if the entry is already in our retry list and the existing time is before the new one, just skip it // if the entry is already in our retry list and the existing time is before the new one, just skip it
existingTime, exists := knownEntries[entry.data] existingTime, exists := knownEntries[entry.data]
......
...@@ -33,6 +33,10 @@ func Parallelize(workers, pieces int, doWorkPiece DoWorkPieceFunc) { ...@@ -33,6 +33,10 @@ func Parallelize(workers, pieces int, doWorkPiece DoWorkPieceFunc) {
} }
close(toProcess) close(toProcess)
if pieces < workers {
workers = pieces
}
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(workers) wg.Add(workers)
for i := 0; i < workers; i++ { for i := 0; i < workers; i++ {
......
...@@ -154,7 +154,7 @@ func (q *Type) Done(item interface{}) { ...@@ -154,7 +154,7 @@ func (q *Type) Done(item interface{}) {
} }
} }
// Shutdown will cause q to ignore all new items added to it. As soon as the // ShutDown will cause q to ignore all new items added to it. As soon as the
// worker goroutines have drained the existing items in the queue, they will be // worker goroutines have drained the existing items in the queue, they will be
// instructed to exit. // instructed to exit.
func (q *Type) ShutDown() { func (q *Type) ShutDown() {
......
...@@ -129,3 +129,33 @@ func TestLen(t *testing.T) { ...@@ -129,3 +129,33 @@ func TestLen(t *testing.T) {
t.Errorf("Expected %v, got %v", e, a) t.Errorf("Expected %v, got %v", e, a)
} }
} }
func TestReinsert(t *testing.T) {
q := workqueue.New()
q.Add("foo")
// Start processing
i, _ := q.Get()
if i != "foo" {
t.Errorf("Expected %v, got %v", "foo", i)
}
// Add it back while processing
q.Add(i)
// Finish it up
q.Done(i)
// It should be back on the queue
i, _ = q.Get()
if i != "foo" {
t.Errorf("Expected %v, got %v", "foo", i)
}
// Finish that one up
q.Done(i)
if a := q.Len(); a != 0 {
t.Errorf("Expected queue to be empty. Has %v items", a)
}
}
...@@ -16,10 +16,10 @@ limitations under the License. ...@@ -16,10 +16,10 @@ limitations under the License.
package workqueue package workqueue
// RateLimitingInterface is an Interface that can Add an item at a later time. This makes it easier to // RateLimitingInterface is an interface that rate limits items being added to the queue.
// requeue items after failures without ending up in a hot-loop.
type RateLimitingInterface interface { type RateLimitingInterface interface {
DelayingInterface DelayingInterface
// AddRateLimited adds an item to the workqueue after the rate limiter says its ok // AddRateLimited adds an item to the workqueue after the rate limiter says its ok
AddRateLimited(item interface{}) AddRateLimited(item interface{})
...@@ -27,6 +27,7 @@ type RateLimitingInterface interface { ...@@ -27,6 +27,7 @@ type RateLimitingInterface interface {
// or for success, we'll stop the rate limiter from tracking it. This only clears the `rateLimiter`, you // or for success, we'll stop the rate limiter from tracking it. This only clears the `rateLimiter`, you
// still have to call `Done` on the queue. // still have to call `Done` on the queue.
Forget(item interface{}) Forget(item interface{})
// NumRequeues returns back how many times the item was requeued // NumRequeues returns back how many times the item was requeued
NumRequeues(item interface{}) int NumRequeues(item interface{}) int
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment