2015-07-24 00:31:08 +00:00
|
|
|
package nomad
|
2015-07-24 04:37:28 +00:00
|
|
|
|
|
|
|
import (
|
2022-11-16 21:10:11 +00:00
|
|
|
"container/heap"
|
2019-11-07 15:39:29 +00:00
|
|
|
"encoding/json"
|
2019-11-08 19:35:06 +00:00
|
|
|
"errors"
|
2017-04-12 20:39:19 +00:00
|
|
|
"fmt"
|
2015-07-24 04:37:28 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
|
2022-03-15 12:42:43 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
2015-08-11 21:27:14 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2022-11-16 21:10:11 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/state"
|
2015-07-24 04:37:28 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
2017-04-12 20:39:19 +00:00
|
|
|
"github.com/hashicorp/nomad/testutil"
|
2022-11-16 21:10:11 +00:00
|
|
|
"github.com/shoenig/test"
|
|
|
|
"github.com/shoenig/test/must"
|
2018-03-13 18:23:11 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-07-24 04:37:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultSched = []string{
|
|
|
|
structs.JobTypeService,
|
|
|
|
structs.JobTypeBatch,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-04-14 22:24:55 +00:00
|
|
|
func testBrokerConfig() *Config {
|
|
|
|
config := DefaultConfig()
|
|
|
|
|
|
|
|
// Tune the Nack timeout
|
|
|
|
config.EvalNackTimeout = 5 * time.Second
|
|
|
|
|
|
|
|
// Tune the Nack delay
|
|
|
|
config.EvalNackInitialReenqueueDelay = 5 * time.Millisecond
|
|
|
|
config.EvalNackSubsequentReenqueueDelay = 50 * time.Millisecond
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:37:28 +00:00
|
|
|
func testBroker(t *testing.T, timeout time.Duration) *EvalBroker {
|
2017-04-14 22:24:55 +00:00
|
|
|
config := testBrokerConfig()
|
|
|
|
|
|
|
|
if timeout != 0 {
|
|
|
|
config.EvalNackTimeout = timeout
|
2015-07-24 04:37:28 +00:00
|
|
|
}
|
2017-04-14 22:24:55 +00:00
|
|
|
|
|
|
|
return testBrokerFromConfig(t, config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testBrokerFromConfig(t *testing.T, c *Config) *EvalBroker {
|
|
|
|
b, err := NewEvalBroker(c.EvalNackTimeout, c.EvalNackInitialReenqueueDelay, c.EvalNackSubsequentReenqueueDelay, 3)
|
2015-07-24 04:37:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
2017-04-12 20:39:19 +00:00
|
|
|
|
2015-07-24 04:37:28 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvalBroker_Enqueue_Dequeue_Nack_Ack(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-07-24 04:37:28 +00:00
|
|
|
b := testBroker(t, 0)
|
|
|
|
|
|
|
|
// Enqueue, but broker is disabled!
|
2015-08-11 21:27:14 +00:00
|
|
|
eval := mock.Eval()
|
2016-05-18 18:35:15 +00:00
|
|
|
b.Enqueue(eval)
|
2015-07-24 04:37:28 +00:00
|
|
|
|
|
|
|
// Verify nothing was done
|
|
|
|
stats := b.Stats()
|
|
|
|
if stats.TotalReady != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
2015-08-05 23:41:39 +00:00
|
|
|
if b.Enabled() {
|
|
|
|
t.Fatalf("should not be enabled")
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:37:28 +00:00
|
|
|
// Enable the broker, and enqueue
|
|
|
|
b.SetEnabled(true)
|
2016-05-18 18:35:15 +00:00
|
|
|
b.Enqueue(eval)
|
2015-07-24 04:37:28 +00:00
|
|
|
|
2015-08-06 00:06:02 +00:00
|
|
|
// Double enqueue is a no-op
|
2016-05-18 18:35:15 +00:00
|
|
|
b.Enqueue(eval)
|
2015-08-06 00:06:02 +00:00
|
|
|
|
2015-08-05 23:41:39 +00:00
|
|
|
if !b.Enabled() {
|
|
|
|
t.Fatalf("should be enabled")
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:37:28 +00:00
|
|
|
// Verify enqueue is done
|
|
|
|
stats = b.Stats()
|
|
|
|
if stats.TotalReady != 1 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[eval.Type].Ready != 1 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dequeue should work
|
2015-08-12 22:25:31 +00:00
|
|
|
out, token, err := b.Dequeue(defaultSched, time.Second)
|
2015-07-24 04:37:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != eval {
|
|
|
|
t.Fatalf("bad : %#v", out)
|
|
|
|
}
|
|
|
|
|
2015-08-12 22:25:31 +00:00
|
|
|
tokenOut, ok := b.Outstanding(out.ID)
|
|
|
|
if !ok {
|
2015-07-24 04:58:13 +00:00
|
|
|
t.Fatalf("should be outstanding")
|
|
|
|
}
|
2015-08-12 22:25:31 +00:00
|
|
|
if tokenOut != token {
|
|
|
|
t.Fatalf("Bad: %#v %#v", token, tokenOut)
|
|
|
|
}
|
2015-07-24 04:58:13 +00:00
|
|
|
|
2015-10-23 17:14:16 +00:00
|
|
|
// OutstandingReset should verify the token
|
2015-10-23 17:22:17 +00:00
|
|
|
err = b.OutstandingReset("nope", "foo")
|
|
|
|
if err != ErrNotOutstanding {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
err = b.OutstandingReset(out.ID, "foo")
|
|
|
|
if err != ErrTokenMismatch {
|
|
|
|
t.Fatalf("err: %v", err)
|
2015-10-23 17:14:16 +00:00
|
|
|
}
|
2015-10-23 17:22:17 +00:00
|
|
|
err = b.OutstandingReset(out.ID, tokenOut)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2015-10-23 17:14:16 +00:00
|
|
|
}
|
|
|
|
|
2015-07-24 04:37:28 +00:00
|
|
|
// Check the stats
|
|
|
|
stats = b.Stats()
|
|
|
|
if stats.TotalReady != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalUnacked != 1 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[eval.Type].Ready != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[eval.Type].Unacked != 1 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
2015-08-12 22:25:31 +00:00
|
|
|
// Nack with wrong token should fail
|
|
|
|
err = b.Nack(eval.ID, "foobarbaz")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should fail to nack")
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:37:28 +00:00
|
|
|
// Nack back into the queue
|
2015-08-12 22:25:31 +00:00
|
|
|
err = b.Nack(eval.ID, token)
|
2015-07-24 04:37:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-08-12 22:25:31 +00:00
|
|
|
if _, ok := b.Outstanding(out.ID); ok {
|
2015-07-24 04:58:13 +00:00
|
|
|
t.Fatalf("should not be outstanding")
|
|
|
|
}
|
|
|
|
|
2017-04-12 20:39:19 +00:00
|
|
|
// Check the stats
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
stats = b.Stats()
|
|
|
|
if stats.TotalReady != 1 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalUnacked != 0 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalWaiting != 0 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[eval.Type].Ready != 1 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[eval.Type].Unacked != 0 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(e error) {
|
|
|
|
t.Fatal(e)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Dequeue should work again
|
|
|
|
out2, token2, err := b.Dequeue(defaultSched, time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out2 != eval {
|
|
|
|
t.Fatalf("bad : %#v", out2)
|
|
|
|
}
|
|
|
|
if token2 == token {
|
|
|
|
t.Fatalf("should get a new token")
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenOut2, ok := b.Outstanding(out.ID)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("should be outstanding")
|
|
|
|
}
|
|
|
|
if tokenOut2 != token2 {
|
|
|
|
t.Fatalf("Bad: %#v %#v", token2, tokenOut2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ack with wrong token
|
|
|
|
err = b.Ack(eval.ID, "zip")
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should fail to ack")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ack finally
|
|
|
|
err = b.Ack(eval.ID, token2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := b.Outstanding(out.ID); ok {
|
|
|
|
t.Fatalf("should not be outstanding")
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:37:28 +00:00
|
|
|
// Check the stats
|
|
|
|
stats = b.Stats()
|
2017-04-12 20:39:19 +00:00
|
|
|
if stats.TotalReady != 0 {
|
2015-07-24 04:37:28 +00:00
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalUnacked != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
2017-04-12 20:39:19 +00:00
|
|
|
if stats.ByScheduler[eval.Type].Ready != 0 {
|
2015-07-24 04:37:28 +00:00
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[eval.Type].Unacked != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
2017-04-12 20:39:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvalBroker_Nack_Delay(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-04-12 20:39:19 +00:00
|
|
|
b := testBroker(t, 0)
|
|
|
|
|
|
|
|
// Enqueue, but broker is disabled!
|
|
|
|
b.SetEnabled(true)
|
|
|
|
eval := mock.Eval()
|
|
|
|
b.Enqueue(eval)
|
|
|
|
|
|
|
|
// Dequeue should work
|
|
|
|
out, token, err := b.Dequeue(defaultSched, time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != eval {
|
|
|
|
t.Fatalf("bad : %#v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nack back into the queue
|
|
|
|
err = b.Nack(eval.ID, token)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := b.Outstanding(out.ID); ok {
|
|
|
|
t.Fatalf("should not be outstanding")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the stats to ensure that it is waiting
|
|
|
|
stats := b.Stats()
|
|
|
|
if stats.TotalReady != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalUnacked != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalWaiting != 1 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[eval.Type].Ready != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[eval.Type].Unacked != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now wait for it to be re-enqueued
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
stats = b.Stats()
|
|
|
|
if stats.TotalReady != 1 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalUnacked != 0 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalWaiting != 0 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[eval.Type].Ready != 1 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[eval.Type].Unacked != 0 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(e error) {
|
|
|
|
t.Fatal(e)
|
|
|
|
})
|
2015-07-24 04:37:28 +00:00
|
|
|
|
|
|
|
// Dequeue should work again
|
2015-08-12 22:25:31 +00:00
|
|
|
out2, token2, err := b.Dequeue(defaultSched, time.Second)
|
2015-07-24 04:37:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out2 != eval {
|
|
|
|
t.Fatalf("bad : %#v", out2)
|
|
|
|
}
|
2015-08-12 22:25:31 +00:00
|
|
|
if token2 == token {
|
|
|
|
t.Fatalf("should get a new token")
|
|
|
|
}
|
2015-07-24 04:37:28 +00:00
|
|
|
|
2017-04-12 20:39:19 +00:00
|
|
|
// Capture the time
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
// Nack back into the queue
|
|
|
|
err = b.Nack(eval.ID, token2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2015-07-24 04:58:13 +00:00
|
|
|
}
|
2017-04-12 20:39:19 +00:00
|
|
|
|
|
|
|
// Now wait for it to be re-enqueued
|
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
stats = b.Stats()
|
|
|
|
if stats.TotalReady != 1 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalUnacked != 0 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalWaiting != 0 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[eval.Type].Ready != 1 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[eval.Type].Unacked != 0 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(e error) {
|
|
|
|
t.Fatal(e)
|
|
|
|
})
|
|
|
|
|
|
|
|
delay := time.Now().Sub(start)
|
|
|
|
if delay < b.subsequentNackDelay {
|
|
|
|
t.Fatalf("bad: delay was %v; want at least %v", delay, b.subsequentNackDelay)
|
2015-08-12 22:25:31 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 20:39:19 +00:00
|
|
|
// Dequeue should work again
|
|
|
|
out3, token3, err := b.Dequeue(defaultSched, time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out3 != eval {
|
|
|
|
t.Fatalf("bad : %#v", out3)
|
|
|
|
}
|
|
|
|
if token3 == token || token3 == token2 {
|
|
|
|
t.Fatalf("should get a new token")
|
2015-08-12 22:25:31 +00:00
|
|
|
}
|
2015-07-24 04:58:13 +00:00
|
|
|
|
2015-07-24 04:37:28 +00:00
|
|
|
// Ack finally
|
2017-04-12 20:39:19 +00:00
|
|
|
err = b.Ack(eval.ID, token3)
|
2015-07-24 04:37:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-08-12 22:25:31 +00:00
|
|
|
if _, ok := b.Outstanding(out.ID); ok {
|
2015-07-24 04:58:13 +00:00
|
|
|
t.Fatalf("should not be outstanding")
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:37:28 +00:00
|
|
|
// Check the stats
|
|
|
|
stats = b.Stats()
|
|
|
|
if stats.TotalReady != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalUnacked != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[eval.Type].Ready != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[eval.Type].Unacked != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 00:55:15 +00:00
|
|
|
func TestEvalBroker_Serialize_DuplicateJobID(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-08-06 00:55:15 +00:00
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
ns1 := "namespace-one"
|
|
|
|
ns2 := "namespace-two"
|
2022-11-16 21:10:11 +00:00
|
|
|
jobID := "example"
|
2015-08-06 00:55:15 +00:00
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
newEval := func(idx uint64, ns string) *structs.Evaluation {
|
|
|
|
eval := mock.Eval()
|
|
|
|
eval.ID = fmt.Sprintf("eval:%d", idx)
|
|
|
|
eval.JobID = jobID
|
|
|
|
eval.Namespace = ns
|
|
|
|
eval.CreateIndex = idx
|
|
|
|
eval.ModifyIndex = idx
|
|
|
|
b.Enqueue(eval)
|
|
|
|
return eval
|
|
|
|
}
|
|
|
|
|
|
|
|
// first job
|
|
|
|
eval1 := newEval(1, ns1)
|
|
|
|
newEval(2, ns1)
|
|
|
|
newEval(3, ns1)
|
|
|
|
eval4 := newEval(4, ns1)
|
|
|
|
|
|
|
|
// second job
|
|
|
|
eval5 := newEval(5, ns2)
|
|
|
|
newEval(6, ns2)
|
|
|
|
eval7 := newEval(7, ns2)
|
|
|
|
|
|
|
|
// retreive the stats from the broker, less some stats that aren't
|
|
|
|
// interesting for this test and make the test much more verbose
|
|
|
|
// to include
|
|
|
|
getStats := func() BrokerStats {
|
|
|
|
t.Helper()
|
|
|
|
stats := b.Stats()
|
|
|
|
stats.DelayedEvals = nil
|
|
|
|
stats.ByScheduler = nil
|
|
|
|
return *stats
|
2015-08-06 00:55:15 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
must.Eq(t, BrokerStats{TotalReady: 2, TotalUnacked: 0,
|
2023-01-20 19:23:56 +00:00
|
|
|
TotalPending: 5, TotalCancelable: 0}, getStats())
|
2022-11-16 21:10:11 +00:00
|
|
|
|
|
|
|
// Dequeue should get 1st eval
|
2015-08-12 22:25:31 +00:00
|
|
|
out, token, err := b.Dequeue(defaultSched, time.Second)
|
2022-11-16 21:10:11 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.Eq(t, out, eval1, must.Sprint("expected 1st eval"))
|
2015-08-06 00:55:15 +00:00
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
must.Eq(t, BrokerStats{TotalReady: 1, TotalUnacked: 1,
|
2023-01-20 19:23:56 +00:00
|
|
|
TotalPending: 5, TotalCancelable: 0}, getStats())
|
2015-08-06 00:55:15 +00:00
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
// Current wait index should be 4 but Ack to exercise behavior
|
|
|
|
// when worker's Eval.getWaitIndex gets a stale index
|
|
|
|
err = b.Ack(eval1.ID, token)
|
|
|
|
must.NoError(t, err)
|
2015-08-06 00:55:15 +00:00
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
must.Eq(t, BrokerStats{TotalReady: 2, TotalUnacked: 0,
|
2023-01-20 19:23:56 +00:00
|
|
|
TotalPending: 2, TotalCancelable: 2}, getStats())
|
2017-09-07 23:56:15 +00:00
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
// eval4 and eval5 are ready
|
2023-01-20 19:23:56 +00:00
|
|
|
// eval6 and eval7 are pending
|
2022-11-16 21:10:11 +00:00
|
|
|
// Dequeue should get 4th eval
|
2017-09-07 23:56:15 +00:00
|
|
|
out, token, err = b.Dequeue(defaultSched, time.Second)
|
2022-11-16 21:10:11 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.Eq(t, out, eval4, must.Sprint("expected 4th eval"))
|
2017-09-07 23:56:15 +00:00
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
must.Eq(t, BrokerStats{TotalReady: 1, TotalUnacked: 1,
|
2023-01-20 19:23:56 +00:00
|
|
|
TotalPending: 2, TotalCancelable: 2}, getStats())
|
2017-09-07 23:56:15 +00:00
|
|
|
|
2023-01-20 19:23:56 +00:00
|
|
|
// Ack should clear the rest of namespace-one pending but leave
|
2022-11-16 21:10:11 +00:00
|
|
|
// namespace-two untouched
|
|
|
|
err = b.Ack(eval4.ID, token)
|
|
|
|
must.NoError(t, err)
|
2017-09-07 23:56:15 +00:00
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
must.Eq(t, BrokerStats{TotalReady: 1, TotalUnacked: 0,
|
2023-01-20 19:23:56 +00:00
|
|
|
TotalPending: 2, TotalCancelable: 2}, getStats())
|
2017-09-07 23:56:15 +00:00
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
// Dequeue should get 5th eval
|
2017-09-07 23:56:15 +00:00
|
|
|
out, token, err = b.Dequeue(defaultSched, time.Second)
|
2022-11-16 21:10:11 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.Eq(t, out, eval5, must.Sprint("expected 5th eval"))
|
2015-08-06 00:55:15 +00:00
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
must.Eq(t, BrokerStats{TotalReady: 0, TotalUnacked: 1,
|
2023-01-20 19:23:56 +00:00
|
|
|
TotalPending: 2, TotalCancelable: 2}, getStats())
|
2015-08-06 00:55:15 +00:00
|
|
|
|
2023-01-20 19:23:56 +00:00
|
|
|
// Ack should clear remaining namespace-two pending evals
|
2022-11-16 21:10:11 +00:00
|
|
|
err = b.Ack(eval5.ID, token)
|
|
|
|
must.NoError(t, err)
|
2015-08-06 00:55:15 +00:00
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
must.Eq(t, BrokerStats{TotalReady: 1, TotalUnacked: 0,
|
2023-01-20 19:23:56 +00:00
|
|
|
TotalPending: 0, TotalCancelable: 3}, getStats())
|
2015-08-06 00:55:15 +00:00
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
// Dequeue should get 7th eval because that's all that's left
|
2015-08-12 22:25:31 +00:00
|
|
|
out, token, err = b.Dequeue(defaultSched, time.Second)
|
2022-11-16 21:10:11 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
must.Eq(t, out, eval7, must.Sprint("expected 7th eval"))
|
2015-08-06 00:55:15 +00:00
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
must.Eq(t, BrokerStats{TotalReady: 0, TotalUnacked: 1,
|
2023-01-20 19:23:56 +00:00
|
|
|
TotalPending: 0, TotalCancelable: 3}, getStats())
|
2015-08-06 00:55:15 +00:00
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
// Last ack should leave the broker empty except for cancels
|
|
|
|
err = b.Ack(eval7.ID, token)
|
|
|
|
must.NoError(t, err)
|
2015-08-06 00:55:15 +00:00
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
must.Eq(t, BrokerStats{TotalReady: 0, TotalUnacked: 0,
|
2023-01-20 19:23:56 +00:00
|
|
|
TotalPending: 0, TotalCancelable: 3}, getStats())
|
2015-08-06 00:55:15 +00:00
|
|
|
}
|
|
|
|
|
2015-07-24 04:37:28 +00:00
|
|
|
func TestEvalBroker_Enqueue_Disable(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-07-24 04:37:28 +00:00
|
|
|
b := testBroker(t, 0)
|
|
|
|
|
|
|
|
// Enqueue
|
2015-08-11 21:27:14 +00:00
|
|
|
eval := mock.Eval()
|
2015-07-24 04:37:28 +00:00
|
|
|
b.SetEnabled(true)
|
2016-05-18 18:35:15 +00:00
|
|
|
b.Enqueue(eval)
|
2015-07-24 04:37:28 +00:00
|
|
|
|
|
|
|
// Flush via SetEnabled
|
|
|
|
b.SetEnabled(false)
|
|
|
|
|
|
|
|
// Check the stats
|
|
|
|
stats := b.Stats()
|
|
|
|
if stats.TotalReady != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalUnacked != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if _, ok := stats.ByScheduler[eval.Type]; ok {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-15 09:02:21 +00:00
|
|
|
func TestEvalBroker_Enqueue_Disable_Delay(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2019-05-15 09:02:21 +00:00
|
|
|
b := testBroker(t, 0)
|
|
|
|
baseEval := mock.Eval()
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
{
|
|
|
|
// Enqueue
|
|
|
|
b.Enqueue(baseEval.Copy())
|
|
|
|
|
|
|
|
delayedEval := baseEval.Copy()
|
|
|
|
delayedEval.Wait = 30
|
|
|
|
b.Enqueue(delayedEval)
|
|
|
|
|
|
|
|
waitEval := baseEval.Copy()
|
|
|
|
waitEval.WaitUntil = time.Now().Add(30 * time.Second)
|
|
|
|
b.Enqueue(waitEval)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush via SetEnabled
|
|
|
|
b.SetEnabled(false)
|
|
|
|
|
|
|
|
{
|
|
|
|
// Check the stats
|
|
|
|
stats := b.Stats()
|
|
|
|
require.Equal(t, 0, stats.TotalReady, "Expected ready to be flushed")
|
|
|
|
require.Equal(t, 0, stats.TotalWaiting, "Expected waiting to be flushed")
|
2023-01-20 19:23:56 +00:00
|
|
|
require.Equal(t, 0, stats.TotalPending, "Expected pending to be flushed")
|
2019-05-15 09:02:21 +00:00
|
|
|
require.Equal(t, 0, stats.TotalUnacked, "Expected unacked to be flushed")
|
|
|
|
_, ok := stats.ByScheduler[baseEval.Type]
|
|
|
|
require.False(t, ok, "Expected scheduler to have no stats")
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Enqueue again now we're disabled
|
|
|
|
b.Enqueue(baseEval.Copy())
|
|
|
|
|
|
|
|
delayedEval := baseEval.Copy()
|
|
|
|
delayedEval.Wait = 30 * time.Second
|
|
|
|
b.Enqueue(delayedEval)
|
|
|
|
|
|
|
|
waitEval := baseEval.Copy()
|
|
|
|
waitEval.WaitUntil = time.Now().Add(30 * time.Second)
|
|
|
|
b.Enqueue(waitEval)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Check the stats again
|
|
|
|
stats := b.Stats()
|
|
|
|
require.Equal(t, 0, stats.TotalReady, "Expected ready to be flushed")
|
|
|
|
require.Equal(t, 0, stats.TotalWaiting, "Expected waiting to be flushed")
|
2023-01-20 19:23:56 +00:00
|
|
|
require.Equal(t, 0, stats.TotalPending, "Expected pending to be flushed")
|
2019-05-15 09:02:21 +00:00
|
|
|
require.Equal(t, 0, stats.TotalUnacked, "Expected unacked to be flushed")
|
|
|
|
_, ok := stats.ByScheduler[baseEval.Type]
|
|
|
|
require.False(t, ok, "Expected scheduler to have no stats")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:37:28 +00:00
|
|
|
func TestEvalBroker_Dequeue_Timeout(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-07-24 04:37:28 +00:00
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
start := time.Now()
|
2015-08-12 22:25:31 +00:00
|
|
|
out, _, err := b.Dequeue(defaultSched, 5*time.Millisecond)
|
2015-07-24 04:37:28 +00:00
|
|
|
end := time.Now()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != nil {
|
|
|
|
t.Fatalf("unexpected: %#v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := end.Sub(start); diff < 5*time.Millisecond {
|
|
|
|
t.Fatalf("bad: %#v", diff)
|
2015-11-21 00:49:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvalBroker_Dequeue_Empty_Timeout(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-11-21 00:49:55 +00:00
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
2015-11-23 19:59:49 +00:00
|
|
|
|
2019-11-08 19:35:06 +00:00
|
|
|
errCh := make(chan error)
|
2015-11-23 19:59:49 +00:00
|
|
|
go func() {
|
2019-11-08 19:35:06 +00:00
|
|
|
defer close(errCh)
|
2015-11-23 19:59:49 +00:00
|
|
|
out, _, err := b.Dequeue(defaultSched, 0)
|
|
|
|
if err != nil {
|
2019-11-08 19:35:06 +00:00
|
|
|
errCh <- err
|
|
|
|
return
|
2015-11-23 19:59:49 +00:00
|
|
|
}
|
|
|
|
if out == nil {
|
2019-11-08 19:35:06 +00:00
|
|
|
errCh <- errors.New("expected a non-nil value")
|
|
|
|
return
|
2015-11-23 19:59:49 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Sleep for a little bit
|
|
|
|
select {
|
|
|
|
case <-time.After(5 * time.Millisecond):
|
2019-11-08 19:35:06 +00:00
|
|
|
case err := <-errCh:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error from dequeue goroutine: %s", err)
|
|
|
|
}
|
|
|
|
t.Fatalf("Dequeue(0) should block, not finish")
|
2015-11-23 19:59:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enqueue to unblock the dequeue.
|
|
|
|
eval := mock.Eval()
|
2016-05-18 18:35:15 +00:00
|
|
|
b.Enqueue(eval)
|
2015-11-21 00:49:55 +00:00
|
|
|
|
2015-11-23 19:59:49 +00:00
|
|
|
select {
|
2019-11-08 19:35:06 +00:00
|
|
|
case err := <-errCh:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error from dequeue goroutine: %s", err)
|
|
|
|
}
|
2015-11-23 19:59:49 +00:00
|
|
|
case <-time.After(5 * time.Millisecond):
|
|
|
|
t.Fatal("timeout: Dequeue(0) should return after enqueue")
|
2015-07-24 04:37:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure higher priority dequeued first
|
|
|
|
func TestEvalBroker_Dequeue_Priority(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-07-24 04:37:28 +00:00
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
eval1 := mock.Eval()
|
2015-07-24 04:37:28 +00:00
|
|
|
eval1.Priority = 10
|
|
|
|
b.Enqueue(eval1)
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
eval2 := mock.Eval()
|
2015-07-24 04:37:28 +00:00
|
|
|
eval2.Priority = 30
|
|
|
|
b.Enqueue(eval2)
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
eval3 := mock.Eval()
|
2015-07-24 04:37:28 +00:00
|
|
|
eval3.Priority = 20
|
|
|
|
b.Enqueue(eval3)
|
|
|
|
|
2015-08-12 22:25:31 +00:00
|
|
|
out1, _, _ := b.Dequeue(defaultSched, time.Second)
|
2015-07-24 04:37:28 +00:00
|
|
|
if out1 != eval2 {
|
|
|
|
t.Fatalf("bad: %#v", out1)
|
|
|
|
}
|
|
|
|
|
2015-08-12 22:25:31 +00:00
|
|
|
out2, _, _ := b.Dequeue(defaultSched, time.Second)
|
2015-07-24 04:37:28 +00:00
|
|
|
if out2 != eval3 {
|
|
|
|
t.Fatalf("bad: %#v", out2)
|
|
|
|
}
|
|
|
|
|
2015-08-12 22:25:31 +00:00
|
|
|
out3, _, _ := b.Dequeue(defaultSched, time.Second)
|
2015-07-24 04:37:28 +00:00
|
|
|
if out3 != eval1 {
|
|
|
|
t.Fatalf("bad: %#v", out3)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 05:58:12 +00:00
|
|
|
// Ensure FIFO at fixed priority
|
|
|
|
func TestEvalBroker_Dequeue_FIFO(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-07-24 05:58:12 +00:00
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
NUM := 100
|
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
for i := NUM; i > 0; i-- {
|
2015-08-11 21:27:14 +00:00
|
|
|
eval1 := mock.Eval()
|
2015-07-24 05:58:12 +00:00
|
|
|
eval1.CreateIndex = uint64(i)
|
|
|
|
eval1.ModifyIndex = uint64(i)
|
|
|
|
b.Enqueue(eval1)
|
|
|
|
}
|
|
|
|
|
2022-11-16 21:10:11 +00:00
|
|
|
for i := 1; i < NUM; i++ {
|
2015-08-12 22:25:31 +00:00
|
|
|
out1, _, _ := b.Dequeue(defaultSched, time.Second)
|
2022-11-16 21:10:11 +00:00
|
|
|
must.Eq(t, uint64(i), out1.CreateIndex,
|
|
|
|
must.Sprintf("eval was not FIFO by CreateIndex"),
|
|
|
|
)
|
2015-07-24 05:58:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:37:28 +00:00
|
|
|
// Ensure fairness between schedulers
|
|
|
|
func TestEvalBroker_Dequeue_Fairness(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-07-24 04:37:28 +00:00
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
2017-04-12 20:39:19 +00:00
|
|
|
NUM := 1000
|
2015-07-24 04:37:28 +00:00
|
|
|
|
|
|
|
for i := 0; i < NUM; i++ {
|
2015-08-11 21:27:14 +00:00
|
|
|
eval1 := mock.Eval()
|
2015-07-24 04:37:28 +00:00
|
|
|
if i < (NUM / 2) {
|
|
|
|
eval1.Type = structs.JobTypeService
|
|
|
|
} else {
|
|
|
|
eval1.Type = structs.JobTypeBatch
|
|
|
|
}
|
|
|
|
b.Enqueue(eval1)
|
|
|
|
}
|
|
|
|
|
|
|
|
counter := 0
|
|
|
|
for i := 0; i < NUM; i++ {
|
2015-08-12 22:25:31 +00:00
|
|
|
out1, _, _ := b.Dequeue(defaultSched, time.Second)
|
2015-07-24 04:37:28 +00:00
|
|
|
|
|
|
|
switch out1.Type {
|
|
|
|
case structs.JobTypeService:
|
|
|
|
if counter < 0 {
|
|
|
|
counter = 0
|
|
|
|
}
|
|
|
|
counter += 1
|
|
|
|
case structs.JobTypeBatch:
|
|
|
|
if counter > 0 {
|
|
|
|
counter = 0
|
|
|
|
}
|
|
|
|
counter -= 1
|
|
|
|
}
|
|
|
|
|
2015-08-05 00:11:20 +00:00
|
|
|
// This will fail randomly at times. It is very hard to
|
|
|
|
// test deterministically that its acting randomly.
|
2017-04-12 20:39:19 +00:00
|
|
|
if counter >= 250 || counter <= -250 {
|
2015-07-24 04:37:28 +00:00
|
|
|
t.Fatalf("unlikely sequence: %d", counter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we get unblocked
|
|
|
|
func TestEvalBroker_Dequeue_Blocked(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-07-24 04:37:28 +00:00
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
// Start with a blocked dequeue
|
2019-11-08 13:52:23 +00:00
|
|
|
outCh := make(chan *structs.Evaluation)
|
|
|
|
errCh := make(chan error)
|
2015-07-24 04:37:28 +00:00
|
|
|
go func() {
|
2019-11-08 13:52:23 +00:00
|
|
|
defer close(errCh)
|
|
|
|
defer close(outCh)
|
2015-07-24 04:37:28 +00:00
|
|
|
start := time.Now()
|
2015-08-12 22:25:31 +00:00
|
|
|
out, _, err := b.Dequeue(defaultSched, time.Second)
|
2015-07-24 04:37:28 +00:00
|
|
|
if err != nil {
|
2019-11-08 13:52:23 +00:00
|
|
|
errCh <- err
|
|
|
|
return
|
2015-07-24 04:37:28 +00:00
|
|
|
}
|
2019-11-08 13:52:23 +00:00
|
|
|
end := time.Now()
|
2015-07-24 04:37:28 +00:00
|
|
|
if d := end.Sub(start); d < 5*time.Millisecond {
|
2019-11-08 13:52:23 +00:00
|
|
|
errCh <- fmt.Errorf("test broker dequeue duration too fast: %v", d)
|
|
|
|
return
|
2015-07-24 04:37:28 +00:00
|
|
|
}
|
2019-11-08 13:52:23 +00:00
|
|
|
outCh <- out
|
2015-07-24 04:37:28 +00:00
|
|
|
}()
|
|
|
|
|
2019-11-08 13:52:23 +00:00
|
|
|
// Wait for a bit, or t.Fatal if an error has already happened in
|
|
|
|
// the goroutine
|
|
|
|
select {
|
|
|
|
case <-time.After(5 * time.Millisecond):
|
|
|
|
// no errors yet, soldier on
|
|
|
|
case err := <-errCh:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error from anonymous goroutine before enqueue: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2015-07-24 04:37:28 +00:00
|
|
|
|
|
|
|
// Enqueue
|
2015-08-11 21:27:14 +00:00
|
|
|
eval := mock.Eval()
|
2016-05-18 18:35:15 +00:00
|
|
|
b.Enqueue(eval)
|
2015-07-24 04:37:28 +00:00
|
|
|
|
|
|
|
// Ensure dequeue
|
|
|
|
select {
|
|
|
|
case out := <-outCh:
|
|
|
|
if out != eval {
|
2019-11-08 13:52:23 +00:00
|
|
|
prettyExp, _ := json.MarshalIndent(eval, "", "\t")
|
|
|
|
prettyGot, _ := json.MarshalIndent(out, "", "\t")
|
|
|
|
t.Fatalf("dequeue result expected:\n%s\ngot:\n%s",
|
|
|
|
string(prettyExp), string(prettyGot))
|
2015-07-24 04:37:28 +00:00
|
|
|
}
|
2019-11-08 13:52:23 +00:00
|
|
|
case err := <-errCh:
|
|
|
|
t.Fatalf("error from anonymous goroutine after enqueue: %v", err)
|
2015-07-24 04:37:28 +00:00
|
|
|
case <-time.After(time.Second):
|
2019-11-08 13:52:23 +00:00
|
|
|
t.Fatalf("timeout waiting for dequeue result")
|
2015-07-24 04:37:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we nack in a timely manner
|
|
|
|
func TestEvalBroker_Nack_Timeout(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-07-24 04:37:28 +00:00
|
|
|
b := testBroker(t, 5*time.Millisecond)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
// Enqueue
|
2015-08-11 21:27:14 +00:00
|
|
|
eval := mock.Eval()
|
2016-05-18 18:35:15 +00:00
|
|
|
b.Enqueue(eval)
|
2015-07-24 04:37:28 +00:00
|
|
|
|
|
|
|
// Dequeue
|
2015-08-12 22:25:31 +00:00
|
|
|
out, _, err := b.Dequeue(defaultSched, time.Second)
|
2015-07-24 04:37:28 +00:00
|
|
|
start := time.Now()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != eval {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dequeue, should block on Nack timer
|
2015-08-12 22:25:31 +00:00
|
|
|
out, _, err = b.Dequeue(defaultSched, time.Second)
|
2015-07-24 04:37:28 +00:00
|
|
|
end := time.Now()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != eval {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the nack timer
|
|
|
|
if diff := end.Sub(start); diff < 5*time.Millisecond {
|
|
|
|
t.Fatalf("bad: %#v", diff)
|
|
|
|
}
|
|
|
|
}
|
2015-08-16 17:55:55 +00:00
|
|
|
|
2015-10-23 17:14:16 +00:00
|
|
|
// Ensure we nack in a timely manner
|
|
|
|
func TestEvalBroker_Nack_TimeoutReset(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-04-12 20:39:19 +00:00
|
|
|
b := testBroker(t, 50*time.Millisecond)
|
2015-10-23 17:14:16 +00:00
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
// Enqueue
|
|
|
|
eval := mock.Eval()
|
2016-05-18 18:35:15 +00:00
|
|
|
b.Enqueue(eval)
|
2015-10-23 17:14:16 +00:00
|
|
|
|
|
|
|
// Dequeue
|
|
|
|
out, token, err := b.Dequeue(defaultSched, time.Second)
|
|
|
|
start := time.Now()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != eval {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
2017-04-12 20:39:19 +00:00
|
|
|
// Reset in 20 milliseconds
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
2015-10-23 17:22:17 +00:00
|
|
|
if err := b.OutstandingReset(out.ID, token); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
2015-10-23 17:14:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Dequeue, should block on Nack timer
|
|
|
|
out, _, err = b.Dequeue(defaultSched, time.Second)
|
|
|
|
end := time.Now()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != eval {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the nack timer
|
2017-04-12 20:39:19 +00:00
|
|
|
if diff := end.Sub(start); diff < 75*time.Millisecond {
|
2015-10-23 17:14:16 +00:00
|
|
|
t.Fatalf("bad: %#v", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-04 20:50:44 +00:00
|
|
|
func TestEvalBroker_PauseResumeNackTimeout(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2017-04-12 20:39:19 +00:00
|
|
|
b := testBroker(t, 50*time.Millisecond)
|
2016-03-04 20:50:44 +00:00
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
// Enqueue
|
|
|
|
eval := mock.Eval()
|
2016-05-18 18:35:15 +00:00
|
|
|
b.Enqueue(eval)
|
2016-03-04 20:50:44 +00:00
|
|
|
|
|
|
|
// Dequeue
|
|
|
|
out, token, err := b.Dequeue(defaultSched, time.Second)
|
|
|
|
start := time.Now()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != eval {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
|
2017-04-12 20:39:19 +00:00
|
|
|
// Pause in 20 milliseconds
|
|
|
|
time.Sleep(20 * time.Millisecond)
|
2016-03-04 20:50:44 +00:00
|
|
|
if err := b.PauseNackTimeout(out.ID, token); err != nil {
|
2019-11-07 21:04:59 +00:00
|
|
|
t.Fatalf("pause nack timeout error: %v", err)
|
2016-03-04 20:50:44 +00:00
|
|
|
}
|
|
|
|
|
2019-11-07 21:04:59 +00:00
|
|
|
errCh := make(chan error)
|
2016-03-04 20:50:44 +00:00
|
|
|
go func() {
|
2019-11-07 21:04:59 +00:00
|
|
|
defer close(errCh)
|
2017-04-12 20:39:19 +00:00
|
|
|
time.Sleep(20 * time.Millisecond)
|
2016-03-04 20:50:44 +00:00
|
|
|
if err := b.ResumeNackTimeout(out.ID, token); err != nil {
|
2019-11-07 21:04:59 +00:00
|
|
|
errCh <- err
|
|
|
|
return
|
2016-03-04 20:50:44 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Dequeue, should block until the timer is resumed
|
|
|
|
out, _, err = b.Dequeue(defaultSched, time.Second)
|
|
|
|
end := time.Now()
|
|
|
|
if err != nil {
|
2019-11-07 21:04:59 +00:00
|
|
|
t.Fatalf("dequeue error: %v", err)
|
2016-03-04 20:50:44 +00:00
|
|
|
}
|
|
|
|
if out != eval {
|
2019-11-07 21:04:59 +00:00
|
|
|
prettyExp, _ := json.MarshalIndent(eval, "", "\t")
|
|
|
|
prettyGot, _ := json.MarshalIndent(out, "", "\t")
|
|
|
|
t.Fatalf("dequeue result expected:\n%s\ngot:\n%s",
|
|
|
|
string(prettyExp), string(prettyGot))
|
2016-03-04 20:50:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the nack timer
|
2017-04-12 20:39:19 +00:00
|
|
|
if diff := end.Sub(start); diff < 95*time.Millisecond {
|
2019-11-07 21:04:59 +00:00
|
|
|
t.Fatalf("deqeue happened too fast: %#v", diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the result of ResumeNackTimeout
|
|
|
|
err = <-errCh
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("resume nack timeout error:%s", err)
|
2016-03-04 20:50:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 17:55:55 +00:00
|
|
|
func TestEvalBroker_DeliveryLimit(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-08-16 17:55:55 +00:00
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
eval := mock.Eval()
|
2016-05-18 18:35:15 +00:00
|
|
|
b.Enqueue(eval)
|
2015-08-16 17:55:55 +00:00
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
// Dequeue should work
|
|
|
|
out, token, err := b.Dequeue(defaultSched, time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != eval {
|
|
|
|
t.Fatalf("bad : %#v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nack with wrong token should fail
|
|
|
|
err = b.Nack(eval.ID, token)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the stats
|
|
|
|
stats := b.Stats()
|
|
|
|
if stats.TotalReady != 1 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalUnacked != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[failedQueue].Ready != 1 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[failedQueue].Unacked != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dequeue from failed queue
|
|
|
|
out, token, err := b.Dequeue([]string{failedQueue}, time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != eval {
|
|
|
|
t.Fatalf("bad : %#v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the stats
|
|
|
|
stats = b.Stats()
|
|
|
|
if stats.TotalReady != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalUnacked != 1 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[failedQueue].Ready != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[failedQueue].Unacked != 1 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ack finally
|
|
|
|
err = b.Ack(out.ID, token)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := b.Outstanding(out.ID); ok {
|
|
|
|
t.Fatalf("should not be outstanding")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the stats
|
|
|
|
stats = b.Stats()
|
|
|
|
if stats.TotalReady != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalUnacked != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[failedQueue].Ready != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats.ByScheduler[failedQueue])
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[failedQueue].Unacked != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats.ByScheduler[failedQueue])
|
|
|
|
}
|
|
|
|
}
|
2015-09-07 20:00:45 +00:00
|
|
|
|
2016-02-11 19:07:18 +00:00
|
|
|
func TestEvalBroker_AckAtDeliveryLimit(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2016-02-11 19:07:18 +00:00
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
eval := mock.Eval()
|
2016-05-18 18:35:15 +00:00
|
|
|
b.Enqueue(eval)
|
2016-02-11 19:07:18 +00:00
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
// Dequeue should work
|
|
|
|
out, token, err := b.Dequeue(defaultSched, time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != eval {
|
|
|
|
t.Fatalf("bad : %#v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == 2 {
|
|
|
|
b.Ack(eval.ID, token)
|
|
|
|
} else {
|
|
|
|
// Nack with wrong token should fail
|
|
|
|
err = b.Nack(eval.ID, token)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the stats
|
|
|
|
stats := b.Stats()
|
|
|
|
if stats.TotalReady != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalUnacked != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if _, ok := stats.ByScheduler[failedQueue]; ok {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-28 19:20:27 +00:00
|
|
|
// TestEvalBroker_Wait asserts delayed evaluations cannot be dequeued until
|
|
|
|
// their wait duration has elapsed.
|
2015-09-07 20:00:45 +00:00
|
|
|
func TestEvalBroker_Wait(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2015-09-07 20:00:45 +00:00
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
// Create an eval that should wait
|
|
|
|
eval := mock.Eval()
|
2021-09-28 19:20:27 +00:00
|
|
|
eval.Wait = 100 * time.Millisecond
|
2016-05-18 18:35:15 +00:00
|
|
|
b.Enqueue(eval)
|
2015-09-07 20:00:45 +00:00
|
|
|
|
|
|
|
// Verify waiting
|
|
|
|
stats := b.Stats()
|
|
|
|
if stats.TotalReady != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalWaiting != 1 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
2021-09-28 19:20:27 +00:00
|
|
|
// Dequeue should not return the eval until wait has elapsed
|
|
|
|
out, token, err := b.Dequeue(defaultSched, 1)
|
|
|
|
require.Nil(t, out)
|
|
|
|
require.Empty(t, token)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2015-09-07 20:00:45 +00:00
|
|
|
// Let the wait elapse
|
2021-09-28 19:20:27 +00:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2015-09-07 20:00:45 +00:00
|
|
|
|
|
|
|
// Verify ready
|
|
|
|
stats = b.Stats()
|
|
|
|
if stats.TotalReady != 1 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalWaiting != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dequeue should work
|
2021-09-28 19:20:27 +00:00
|
|
|
out, _, err = b.Dequeue(defaultSched, time.Second)
|
2015-09-07 20:00:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != eval {
|
|
|
|
t.Fatalf("bad : %#v", out)
|
|
|
|
}
|
|
|
|
}
|
2016-05-18 19:13:59 +00:00
|
|
|
|
2018-03-06 03:46:54 +00:00
|
|
|
// Ensure that delayed evaluations work as expected
|
2018-03-02 00:28:02 +00:00
|
|
|
func TestEvalBroker_WaitUntil(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-03-02 00:28:02 +00:00
|
|
|
require := require.New(t)
|
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
// Create a few of evals with WaitUntil set
|
|
|
|
eval1 := mock.Eval()
|
|
|
|
eval1.WaitUntil = now.Add(1 * time.Second)
|
|
|
|
eval1.CreateIndex = 1
|
|
|
|
b.Enqueue(eval1)
|
|
|
|
|
|
|
|
eval2 := mock.Eval()
|
|
|
|
eval2.WaitUntil = now.Add(100 * time.Millisecond)
|
|
|
|
// set CreateIndex to use as a tie breaker when eval2
|
|
|
|
// and eval3 are both in the pending evals heap
|
|
|
|
eval2.CreateIndex = 2
|
|
|
|
b.Enqueue(eval2)
|
|
|
|
|
|
|
|
eval3 := mock.Eval()
|
|
|
|
eval3.WaitUntil = now.Add(20 * time.Millisecond)
|
|
|
|
eval3.CreateIndex = 1
|
|
|
|
b.Enqueue(eval3)
|
2018-03-06 03:46:54 +00:00
|
|
|
require.Equal(3, b.stats.TotalWaiting)
|
2018-03-02 00:28:02 +00:00
|
|
|
// sleep enough for two evals to be ready
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
|
|
|
// first dequeue should return eval3
|
|
|
|
out, _, err := b.Dequeue(defaultSched, time.Second)
|
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(eval3, out)
|
|
|
|
|
|
|
|
// second dequeue should return eval2
|
|
|
|
out, _, err = b.Dequeue(defaultSched, time.Second)
|
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(eval2, out)
|
|
|
|
|
|
|
|
// third dequeue should return eval1
|
|
|
|
out, _, err = b.Dequeue(defaultSched, 2*time.Second)
|
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(eval1, out)
|
2018-03-06 03:46:54 +00:00
|
|
|
require.Equal(0, b.stats.TotalWaiting)
|
2018-03-02 00:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure that priority is taken into account when enqueueing many evaluations.
|
2016-05-18 19:13:59 +00:00
|
|
|
func TestEvalBroker_EnqueueAll_Dequeue_Fair(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2016-05-18 19:13:59 +00:00
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
// Start with a blocked dequeue
|
2019-11-07 15:39:29 +00:00
|
|
|
outCh := make(chan *structs.Evaluation)
|
|
|
|
errCh := make(chan error)
|
2016-05-18 19:13:59 +00:00
|
|
|
go func() {
|
2019-11-07 15:39:29 +00:00
|
|
|
defer close(errCh)
|
|
|
|
defer close(outCh)
|
2016-05-18 19:13:59 +00:00
|
|
|
start := time.Now()
|
|
|
|
out, _, err := b.Dequeue(defaultSched, time.Second)
|
|
|
|
if err != nil {
|
2019-11-07 15:39:29 +00:00
|
|
|
errCh <- err
|
|
|
|
return
|
2016-05-18 19:13:59 +00:00
|
|
|
}
|
2019-11-07 15:39:29 +00:00
|
|
|
end := time.Now()
|
2016-05-18 19:13:59 +00:00
|
|
|
if d := end.Sub(start); d < 5*time.Millisecond {
|
2019-11-07 15:39:29 +00:00
|
|
|
errCh <- fmt.Errorf("test broker dequeue duration too fast: %v", d)
|
|
|
|
return
|
2016-05-18 19:13:59 +00:00
|
|
|
}
|
2019-11-07 15:39:29 +00:00
|
|
|
outCh <- out
|
2016-05-18 19:13:59 +00:00
|
|
|
}()
|
|
|
|
|
2019-11-07 15:39:29 +00:00
|
|
|
// Wait for a bit, or t.Fatal if an error has already happened in
|
|
|
|
// the goroutine
|
|
|
|
select {
|
|
|
|
case <-time.After(5 * time.Millisecond):
|
|
|
|
// no errors yet, soldier on
|
|
|
|
case err := <-errCh:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("error from anonymous goroutine before enqueue: %v", err)
|
|
|
|
}
|
|
|
|
}
|
2016-05-18 19:13:59 +00:00
|
|
|
|
|
|
|
// Enqueue
|
2016-06-24 17:14:34 +00:00
|
|
|
evals := make(map[*structs.Evaluation]string, 8)
|
2016-05-18 19:13:59 +00:00
|
|
|
expectedPriority := 90
|
|
|
|
for i := 10; i <= expectedPriority; i += 10 {
|
|
|
|
eval := mock.Eval()
|
|
|
|
eval.Priority = i
|
2016-06-24 17:14:34 +00:00
|
|
|
evals[eval] = ""
|
2016-05-18 19:13:59 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
b.EnqueueAll(evals)
|
|
|
|
|
|
|
|
// Ensure dequeue
|
|
|
|
select {
|
|
|
|
case out := <-outCh:
|
|
|
|
if out.Priority != expectedPriority {
|
2019-11-07 15:39:29 +00:00
|
|
|
pretty, _ := json.MarshalIndent(out, "", "\t")
|
|
|
|
t.Logf("bad priority on *structs.Evaluation: %s", string(pretty))
|
|
|
|
t.Fatalf("priority wanted:%d, priority got:%d", expectedPriority, out.Priority)
|
2016-05-18 19:13:59 +00:00
|
|
|
}
|
2019-11-07 15:39:29 +00:00
|
|
|
case err := <-errCh:
|
|
|
|
t.Fatalf("error from anonymous goroutine after enqueue: %v", err)
|
2016-05-18 19:13:59 +00:00
|
|
|
case <-time.After(time.Second):
|
2019-11-07 15:39:29 +00:00
|
|
|
t.Fatalf("timeout waiting for dequeue result")
|
2016-05-18 19:13:59 +00:00
|
|
|
}
|
|
|
|
}
|
2016-05-27 23:53:10 +00:00
|
|
|
|
2016-06-24 17:14:34 +00:00
|
|
|
func TestEvalBroker_EnqueueAll_Requeue_Ack(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2016-05-27 23:53:10 +00:00
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
// Create the evaluation, enqueue and dequeue
|
|
|
|
eval := mock.Eval()
|
|
|
|
b.Enqueue(eval)
|
|
|
|
|
|
|
|
out, token, err := b.Dequeue(defaultSched, time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != eval {
|
|
|
|
t.Fatalf("bad : %#v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Requeue the same evaluation.
|
2016-06-24 17:14:34 +00:00
|
|
|
b.EnqueueAll(map[*structs.Evaluation]string{eval: token})
|
2016-05-27 23:53:10 +00:00
|
|
|
|
|
|
|
// The stats should show one unacked
|
|
|
|
stats := b.Stats()
|
|
|
|
if stats.TotalReady != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalUnacked != 1 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ack the evaluation.
|
|
|
|
if err := b.Ack(eval.ID, token); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check stats again as this should cause the re-enqueued one to transition
|
|
|
|
// into the ready state
|
|
|
|
stats = b.Stats()
|
|
|
|
if stats.TotalReady != 1 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalUnacked != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Another dequeue should be successful
|
|
|
|
out2, token2, err := b.Dequeue(defaultSched, time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out2 != eval {
|
|
|
|
t.Fatalf("bad : %#v", out)
|
|
|
|
}
|
|
|
|
if token == token2 {
|
|
|
|
t.Fatalf("bad : %s and %s", token, token2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-24 17:14:34 +00:00
|
|
|
func TestEvalBroker_EnqueueAll_Requeue_Nack(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2016-05-27 23:53:10 +00:00
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
// Create the evaluation, enqueue and dequeue
|
|
|
|
eval := mock.Eval()
|
|
|
|
b.Enqueue(eval)
|
|
|
|
|
|
|
|
out, token, err := b.Dequeue(defaultSched, time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != eval {
|
|
|
|
t.Fatalf("bad : %#v", out)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Requeue the same evaluation.
|
2016-06-24 17:14:34 +00:00
|
|
|
b.EnqueueAll(map[*structs.Evaluation]string{eval: token})
|
2016-05-27 23:53:10 +00:00
|
|
|
|
|
|
|
// The stats should show one unacked
|
|
|
|
stats := b.Stats()
|
|
|
|
if stats.TotalReady != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalUnacked != 1 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nack the evaluation.
|
|
|
|
if err := b.Nack(eval.ID, token); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check stats again as this should cause the re-enqueued one to be dropped
|
2017-04-12 20:39:19 +00:00
|
|
|
testutil.WaitForResult(func() (bool, error) {
|
|
|
|
stats = b.Stats()
|
|
|
|
if stats.TotalReady != 1 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalUnacked != 0 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if len(b.requeue) != 0 {
|
|
|
|
return false, fmt.Errorf("bad: %#v", b.requeue)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}, func(e error) {
|
|
|
|
t.Fatal(e)
|
|
|
|
})
|
2016-05-27 23:53:10 +00:00
|
|
|
}
|
2018-03-13 18:23:11 +00:00
|
|
|
|
|
|
|
func TestEvalBroker_NamespacedJobs(t *testing.T) {
|
2022-03-15 12:42:43 +00:00
|
|
|
ci.Parallel(t)
|
2018-03-13 18:23:11 +00:00
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
// Create evals with the same jobid and different namespace
|
|
|
|
jobId := "test-jobID"
|
|
|
|
|
|
|
|
eval1 := mock.Eval()
|
|
|
|
eval1.JobID = jobId
|
|
|
|
eval1.Namespace = "n1"
|
|
|
|
b.Enqueue(eval1)
|
|
|
|
|
|
|
|
// This eval should not block
|
|
|
|
eval2 := mock.Eval()
|
|
|
|
eval2.JobID = jobId
|
|
|
|
eval2.Namespace = "default"
|
|
|
|
b.Enqueue(eval2)
|
|
|
|
|
|
|
|
// This eval should block
|
|
|
|
eval3 := mock.Eval()
|
|
|
|
eval3.JobID = jobId
|
|
|
|
eval3.Namespace = "default"
|
|
|
|
b.Enqueue(eval3)
|
|
|
|
|
|
|
|
require := require.New(t)
|
|
|
|
out1, _, err := b.Dequeue(defaultSched, 5*time.Millisecond)
|
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(eval1.ID, out1.ID)
|
|
|
|
|
|
|
|
out2, _, err := b.Dequeue(defaultSched, 5*time.Millisecond)
|
|
|
|
require.Nil(err)
|
|
|
|
require.Equal(eval2.ID, out2.ID)
|
|
|
|
|
|
|
|
out3, _, err := b.Dequeue(defaultSched, 5*time.Millisecond)
|
|
|
|
require.Nil(err)
|
|
|
|
require.Nil(out3)
|
|
|
|
|
2023-01-20 19:23:56 +00:00
|
|
|
require.Equal(1, len(b.pending))
|
2018-03-13 18:23:11 +00:00
|
|
|
|
|
|
|
}
|
2022-11-16 21:10:11 +00:00
|
|
|
|
2023-01-20 19:23:56 +00:00
|
|
|
func TestEvalBroker_ReadyEvals_Ordering(t *testing.T) {
|
2022-11-16 21:10:11 +00:00
|
|
|
|
2023-01-20 19:23:56 +00:00
|
|
|
ready := ReadyEvaluations{}
|
2022-11-16 21:10:11 +00:00
|
|
|
|
|
|
|
newEval := func(jobID, evalID string, priority int, index uint64) *structs.Evaluation {
|
|
|
|
eval := mock.Eval()
|
|
|
|
eval.JobID = jobID
|
|
|
|
eval.ID = evalID
|
|
|
|
eval.Priority = priority
|
|
|
|
eval.CreateIndex = uint64(index)
|
|
|
|
return eval
|
|
|
|
}
|
|
|
|
|
|
|
|
// note: we're intentionally pushing these out-of-order to assert we're
|
|
|
|
// getting them back out in the intended order and not just as inserted
|
|
|
|
heap.Push(&ready, newEval("example1", "eval01", 50, 1))
|
|
|
|
heap.Push(&ready, newEval("example3", "eval03", 70, 3))
|
|
|
|
heap.Push(&ready, newEval("example2", "eval02", 50, 2))
|
|
|
|
|
|
|
|
next := heap.Pop(&ready).(*structs.Evaluation)
|
|
|
|
test.Eq(t, "eval03", next.ID,
|
|
|
|
test.Sprint("expected highest Priority to be next ready"))
|
|
|
|
|
|
|
|
next = heap.Pop(&ready).(*structs.Evaluation)
|
|
|
|
test.Eq(t, "eval01", next.ID,
|
|
|
|
test.Sprint("expected oldest CreateIndex to be next ready"))
|
|
|
|
|
|
|
|
heap.Push(&ready, newEval("example4", "eval04", 50, 4))
|
|
|
|
|
|
|
|
next = heap.Pop(&ready).(*structs.Evaluation)
|
|
|
|
test.Eq(t, "eval02", next.ID,
|
|
|
|
test.Sprint("expected oldest CreateIndex to be next ready"))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-20 19:23:56 +00:00
|
|
|
func TestEvalBroker_PendingEval_Ordering(t *testing.T) {
|
|
|
|
pending := PendingEvaluations{}
|
2022-11-16 21:10:11 +00:00
|
|
|
|
|
|
|
newEval := func(evalID string, priority int, index uint64) *structs.Evaluation {
|
|
|
|
eval := mock.Eval()
|
|
|
|
eval.ID = evalID
|
|
|
|
eval.Priority = priority
|
|
|
|
eval.ModifyIndex = uint64(index)
|
|
|
|
return eval
|
|
|
|
}
|
|
|
|
|
|
|
|
// note: we're intentionally pushing these out-of-order to assert we're
|
|
|
|
// getting them back out in the intended order and not just as inserted
|
2023-01-20 19:23:56 +00:00
|
|
|
heap.Push(&pending, newEval("eval03", 50, 3))
|
|
|
|
heap.Push(&pending, newEval("eval02", 100, 2))
|
|
|
|
heap.Push(&pending, newEval("eval01", 50, 1))
|
2022-11-16 21:10:11 +00:00
|
|
|
|
2023-01-20 19:23:56 +00:00
|
|
|
next := heap.Pop(&pending).(*structs.Evaluation)
|
|
|
|
test.Eq(t, "eval02", next.ID,
|
|
|
|
test.Sprint("expected eval with highest priority to be next"))
|
2022-11-16 21:10:11 +00:00
|
|
|
|
2023-01-20 19:23:56 +00:00
|
|
|
next = heap.Pop(&pending).(*structs.Evaluation)
|
|
|
|
test.Eq(t, "eval03", next.ID,
|
|
|
|
test.Sprint("expected eval with highest modify index to be next"))
|
2022-11-16 21:10:11 +00:00
|
|
|
|
2023-01-20 19:23:56 +00:00
|
|
|
heap.Push(&pending, newEval("eval04", 30, 4))
|
|
|
|
next = heap.Pop(&pending).(*structs.Evaluation)
|
|
|
|
test.Eq(t, "eval01", next.ID,
|
|
|
|
test.Sprint("expected eval with highest priority to be nexct"))
|
2022-11-16 21:10:11 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-20 19:23:56 +00:00
|
|
|
func TestEvalBroker_PendingEvals_MarkForCancel(t *testing.T) {
|
2022-11-16 21:10:11 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
2023-01-20 19:23:56 +00:00
|
|
|
pending := PendingEvaluations{}
|
2022-11-16 21:10:11 +00:00
|
|
|
|
|
|
|
// note: we're intentionally pushing these out-of-order to assert we're
|
|
|
|
// getting them back out in the intended order and not just as inserted
|
|
|
|
for i := 100; i > 0; i -= 10 {
|
|
|
|
eval := mock.Eval()
|
|
|
|
eval.JobID = "example"
|
|
|
|
eval.CreateIndex = uint64(i)
|
|
|
|
eval.ModifyIndex = uint64(i)
|
2023-01-20 19:23:56 +00:00
|
|
|
heap.Push(&pending, eval)
|
2022-11-16 21:10:11 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 19:23:56 +00:00
|
|
|
canceled := pending.MarkForCancel()
|
2022-11-16 21:10:11 +00:00
|
|
|
must.Eq(t, 9, len(canceled))
|
2023-01-20 19:23:56 +00:00
|
|
|
must.Eq(t, 1, pending.Len())
|
2022-11-16 21:10:11 +00:00
|
|
|
|
2023-01-20 19:23:56 +00:00
|
|
|
raw := heap.Pop(&pending)
|
2022-11-16 21:10:11 +00:00
|
|
|
must.NotNil(t, raw)
|
|
|
|
eval := raw.(*structs.Evaluation)
|
|
|
|
must.Eq(t, 100, eval.ModifyIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestEvalBroker_IntegrationTest exercises the eval broker with realistic
|
|
|
|
// workflows
|
|
|
|
func TestEvalBroker_IntegrationTest(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
srv, cleanupS1 := TestServer(t, func(c *Config) {
|
|
|
|
c.NumSchedulers = 0 // Prevent dequeue
|
|
|
|
c.EvalReapCancelableInterval = time.Minute * 10 // Prevent sweep-up
|
|
|
|
})
|
|
|
|
|
|
|
|
defer cleanupS1()
|
|
|
|
testutil.WaitForLeader(t, srv.RPC)
|
|
|
|
|
|
|
|
codec := rpcClient(t, srv)
|
|
|
|
store := srv.fsm.State()
|
|
|
|
|
|
|
|
// create a system job, a node for it to run on, and a set of node up/down
|
|
|
|
// events that will result in evaluations queued.
|
|
|
|
|
|
|
|
job := mock.SystemJob()
|
|
|
|
jobReq := &structs.JobRegisterRequest{
|
|
|
|
Job: job,
|
|
|
|
EvalPriority: 50,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
var jobResp structs.JobRegisterResponse
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Job.Register", jobReq, &jobResp)
|
|
|
|
must.NoError(t, err)
|
|
|
|
|
|
|
|
node := mock.Node()
|
|
|
|
nodeReq := &structs.NodeRegisterRequest{
|
|
|
|
Node: node,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
var nodeResp structs.NodeUpdateResponse
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Node.Register", nodeReq, &nodeResp)
|
|
|
|
must.NoError(t, err)
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
status := structs.NodeStatusDown
|
|
|
|
if i%2 == 0 {
|
|
|
|
status = structs.NodeStatusReady
|
|
|
|
}
|
|
|
|
statusReq := &structs.NodeUpdateStatusRequest{
|
|
|
|
NodeID: node.ID,
|
|
|
|
Status: status,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
|
|
|
var statusResp structs.NodeUpdateResponse
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "Node.UpdateStatus", statusReq, &statusResp)
|
|
|
|
must.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure we have the expected number of evaluations and eval broker state
|
|
|
|
|
|
|
|
// retreive the stats from the broker, less some uninteresting ones
|
|
|
|
getStats := func() BrokerStats {
|
|
|
|
t.Helper()
|
|
|
|
stats := srv.evalBroker.Stats()
|
|
|
|
stats.DelayedEvals = nil
|
|
|
|
stats.ByScheduler = nil
|
|
|
|
return *stats
|
|
|
|
}
|
|
|
|
|
|
|
|
getEvalStatuses := func() map[string]int {
|
|
|
|
t.Helper()
|
|
|
|
statuses := map[string]int{}
|
|
|
|
iter, err := store.Evals(nil, state.SortDefault)
|
|
|
|
must.NoError(t, err)
|
|
|
|
for {
|
|
|
|
raw := iter.Next()
|
|
|
|
if raw == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
eval := raw.(*structs.Evaluation)
|
|
|
|
statuses[eval.Status] += 1
|
|
|
|
if eval.Status == structs.EvalStatusCancelled {
|
|
|
|
must.Eq(t, "canceled after more recent eval was processed", eval.StatusDescription)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return statuses
|
|
|
|
}
|
|
|
|
|
|
|
|
must.Eq(t, map[string]int{structs.EvalStatusPending: 11}, getEvalStatuses())
|
|
|
|
must.Eq(t, BrokerStats{TotalReady: 1, TotalUnacked: 0,
|
2023-01-20 19:23:56 +00:00
|
|
|
TotalPending: 10, TotalCancelable: 0}, getStats())
|
2022-11-16 21:10:11 +00:00
|
|
|
|
|
|
|
// start schedulers: all the evals are for a single job so there should only
|
|
|
|
// be one eval processesed at a time no matter how many schedulers we run
|
|
|
|
|
|
|
|
config := DefaultConfig()
|
|
|
|
config.NumSchedulers = 4
|
|
|
|
config.EvalReapCancelableInterval = time.Minute * 10
|
|
|
|
require.NoError(t, srv.Reload(config))
|
|
|
|
|
|
|
|
// assert that all but 2 evals were canceled and that the eval broker state
|
|
|
|
// has been cleared
|
|
|
|
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
got := getEvalStatuses()
|
|
|
|
return got[structs.EvalStatusComplete] == 2 && got[structs.EvalStatusCancelled] == 9
|
|
|
|
}, 2*time.Second, time.Millisecond*100)
|
|
|
|
|
|
|
|
must.Eq(t, BrokerStats{TotalReady: 0, TotalUnacked: 0,
|
2023-01-20 19:23:56 +00:00
|
|
|
TotalPending: 0, TotalCancelable: 0}, getStats())
|
2022-11-16 21:10:11 +00:00
|
|
|
}
|