- forward updated labels/annotations for downward API compat

- refactor queue.Pod construction to take functional options, privatize Pod fields
- refactor DelayFIFO and HistoricalFIFO to offer consistent, more useful Pop() funcs
- refactor pod update processing changes; long term we should somehow combine with the special pod config source that we are using for mirror pods
- task launch timer cleanup
This commit is contained in:
James DeFelice
2015-11-29 19:34:27 +00:00
parent 5885728318
commit af95e3fe0e
14 changed files with 454 additions and 169 deletions

View File

@@ -302,12 +302,18 @@ func (f *DelayFIFO) Get(id string) (UniqueID, bool) {
// Variant of DelayQueue.Pop() for UniqueDelayed items
func (q *DelayFIFO) Await(timeout time.Duration) UniqueID {
cancel := make(chan struct{})
ch := make(chan interface{}, 1)
var (
cancel = make(chan struct{})
ch = make(chan interface{}, 1)
t = time.NewTimer(timeout)
)
defer t.Stop()
go func() { ch <- q.pop(cancel) }()
var x interface{}
select {
case <-time.After(timeout):
case <-t.C:
close(cancel)
x = <-ch
case x = <-ch:
@@ -319,13 +325,19 @@ func (q *DelayFIFO) Await(timeout time.Duration) UniqueID {
return nil
}
// Variant of DelayQueue.Pop() for UniqueDelayed items
func (q *DelayFIFO) Pop() UniqueID {
return q.pop(nil).(UniqueID)
// Pop blocks until either there is an item available to dequeue or else the specified
// cancel chan is closed. Callers that have no interest in providing a cancel chan
// should specify nil, or else WithoutCancel() (for readability).
func (q *DelayFIFO) Pop(cancel <-chan struct{}) UniqueID {
x := q.pop(cancel)
if x == nil {
return nil
}
return x.(UniqueID)
}
// variant of DelayQueue.Pop that implements optional cancellation
func (q *DelayFIFO) pop(cancel chan struct{}) interface{} {
func (q *DelayFIFO) pop(cancel <-chan struct{}) interface{} {
next := func() *qitem {
q.lock()
defer q.unlock()

View File

@@ -358,7 +358,7 @@ func TestDFIFO_sanity_check(t *testing.T) {
// pop last
before := time.Now()
x := df.Pop()
x := df.Pop(nil)
assert.Equal(a.(*testjob).instance, 2)
now := time.Now()
@@ -395,7 +395,7 @@ func TestDFIFO_Offer(t *testing.T) {
}
before := time.Now()
x := dq.Pop()
x := dq.Pop(nil)
now := time.Now()
waitPeriod := now.Sub(before)

View File

@@ -220,22 +220,28 @@ func (f *HistoricalFIFO) Poll(id string, t EventType) bool {
// Variant of DelayQueue.Pop() for UniqueDelayed items
func (q *HistoricalFIFO) Await(timeout time.Duration) interface{} {
cancel := make(chan struct{})
ch := make(chan interface{}, 1)
go func() { ch <- q.CancelablePop(cancel) }()
var (
cancel = make(chan struct{})
ch = make(chan interface{}, 1)
t = time.NewTimer(timeout)
)
defer t.Stop()
go func() { ch <- q.Pop(cancel) }()
select {
case <-time.After(timeout):
case <-t.C:
close(cancel)
return <-ch
case x := <-ch:
return x
}
}
func (f *HistoricalFIFO) Pop() interface{} {
return f.CancelablePop(nil)
}
func (f *HistoricalFIFO) CancelablePop(cancel <-chan struct{}) interface{} {
// Pop blocks until either there is an item available to dequeue or else the specified
// cancel chan is closed. Callers that have no interest in providing a cancel chan
// should specify nil, or else WithoutCancel() (for readability).
func (f *HistoricalFIFO) Pop(cancel <-chan struct{}) interface{} {
popEvent := (Entry)(nil)
defer func() {
f.carrier(popEvent)

View File

@@ -75,7 +75,7 @@ func TestFIFO_basic(t *testing.T) {
lastInt := _int(0)
lastUint := _uint(0)
for i := 0; i < amount*2; i++ {
switch obj := f.Pop().(type) {
switch obj := f.Pop(nil).(type) {
case _int:
if obj <= lastInt {
t.Errorf("got %v (int) out of order, last was %v", obj, lastInt)
@@ -100,7 +100,7 @@ func TestFIFO_addUpdate(t *testing.T) {
got := make(chan *testObj, 2)
go func() {
for {
got <- f.Pop().(*testObj)
got <- f.Pop(nil).(*testObj)
}
}()
@@ -126,7 +126,7 @@ func TestFIFO_addReplace(t *testing.T) {
got := make(chan *testObj, 2)
go func() {
for {
got <- f.Pop().(*testObj)
got <- f.Pop(nil).(*testObj)
}
}()
@@ -158,24 +158,24 @@ func TestFIFO_detectLineJumpers(t *testing.T) {
done := make(chan struct{})
go func() {
defer close(done)
if e, a := 13, f.Pop().(*testObj).value; a != e {
if e, a := 13, f.Pop(nil).(*testObj).value; a != e {
err = fmt.Errorf("expected %d, got %d", e, a)
return
}
f.Add(&testObj{"foo", 14}) // ensure foo doesn't jump back in line
if e, a := 1, f.Pop().(*testObj).value; a != e {
if e, a := 1, f.Pop(nil).(*testObj).value; a != e {
err = fmt.Errorf("expected %d, got %d", e, a)
return
}
if e, a := 30, f.Pop().(*testObj).value; a != e {
if e, a := 30, f.Pop(nil).(*testObj).value; a != e {
err = fmt.Errorf("expected %d, got %d", e, a)
return
}
if e, a := 14, f.Pop().(*testObj).value; a != e {
if e, a := 14, f.Pop(nil).(*testObj).value; a != e {
err = fmt.Errorf("expected %d, got %d", e, a)
return
}

View File

@@ -59,7 +59,7 @@ type FIFO interface {
// ready, they are returned in the order in which they were added/updated.
// The item is removed from the queue (and the store) before it is returned,
// so if you don't successfully process it, you need to add it back with Add().
Pop() interface{}
Pop(cancel <-chan struct{}) interface{}
// Await attempts to Pop within the given interval; upon success the non-nil
// item is returned, otherwise nil
@@ -101,3 +101,5 @@ type UniqueDeadlined interface {
UniqueID
Deadlined
}
func WithoutCancel() <-chan struct{} { return nil }