2015-07-24 00:31:08 +00:00
|
|
|
package nomad
|
2015-07-24 04:37:28 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
2015-07-24 04:37:28 +00:00
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultSched = []string{
|
|
|
|
structs.JobTypeService,
|
|
|
|
structs.JobTypeBatch,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func testBroker(t *testing.T, timeout time.Duration) *EvalBroker {
|
|
|
|
if timeout == 0 {
|
2015-07-24 04:44:17 +00:00
|
|
|
timeout = 5 * time.Second
|
2015-07-24 04:37:28 +00:00
|
|
|
}
|
2015-08-16 17:55:55 +00:00
|
|
|
b, err := NewEvalBroker(timeout, 3)
|
2015-07-24 04:37:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvalBroker_Enqueue_Dequeue_Nack_Ack(t *testing.T) {
|
|
|
|
b := testBroker(t, 0)
|
|
|
|
|
|
|
|
// Enqueue, but broker is disabled!
|
2015-08-11 21:27:14 +00:00
|
|
|
eval := mock.Eval()
|
2015-07-24 04:37:28 +00:00
|
|
|
err := b.Enqueue(eval)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
err = b.Enqueue(eval)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-08-06 00:06:02 +00:00
|
|
|
// Double enqueue is a no-op
|
|
|
|
err = b.Enqueue(eval)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:37:28 +00:00
|
|
|
// 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[eval.Type].Ready != 1 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.ByScheduler[eval.Type].Unacked != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2015-08-12 22:25:31 +00:00
|
|
|
tokenOut2, 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 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")
|
|
|
|
}
|
2015-07-24 04:58:13 +00:00
|
|
|
|
2015-07-24 04:37:28 +00:00
|
|
|
// Ack finally
|
2015-08-12 22:25:31 +00:00
|
|
|
err = b.Ack(eval.ID, token2)
|
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) {
|
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
eval := mock.Eval()
|
2015-08-06 00:55:15 +00:00
|
|
|
err := b.Enqueue(eval)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
eval2 := mock.Eval()
|
2015-08-06 00:55:15 +00:00
|
|
|
eval2.JobID = eval.JobID
|
|
|
|
eval2.CreateIndex = eval.CreateIndex + 1
|
|
|
|
err = b.Enqueue(eval2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-08-11 21:27:14 +00:00
|
|
|
eval3 := mock.Eval()
|
2015-08-06 00:55:15 +00:00
|
|
|
eval3.JobID = eval.JobID
|
|
|
|
eval3.CreateIndex = eval.CreateIndex + 2
|
|
|
|
err = b.Enqueue(eval3)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
stats := b.Stats()
|
|
|
|
if stats.TotalReady != 1 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalBlocked != 2 {
|
|
|
|
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-08-06 00:55:15 +00:00
|
|
|
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.TotalBlocked != 2 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ack out
|
2015-08-12 22:25:31 +00:00
|
|
|
err = b.Ack(eval.ID, token)
|
2015-08-06 00:55:15 +00:00
|
|
|
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.TotalBlocked != 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-08-06 00:55:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != eval2 {
|
|
|
|
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.TotalBlocked != 1 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ack out
|
2015-08-12 22:25:31 +00:00
|
|
|
err = b.Ack(eval2.ID, token)
|
2015-08-06 00:55:15 +00:00
|
|
|
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.TotalBlocked != 0 {
|
|
|
|
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-08-06 00:55:15 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != eval3 {
|
|
|
|
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.TotalBlocked != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ack out
|
2015-08-12 22:25:31 +00:00
|
|
|
err = b.Ack(eval3.ID, token)
|
2015-08-06 00:55:15 +00:00
|
|
|
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 stats.TotalBlocked != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:37:28 +00:00
|
|
|
func TestEvalBroker_Enqueue_Disable(t *testing.T) {
|
|
|
|
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)
|
|
|
|
err := b.Enqueue(eval)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvalBroker_Dequeue_Timeout(t *testing.T) {
|
|
|
|
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) {
|
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
2015-11-23 19:59:49 +00:00
|
|
|
doneCh := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
out, _, err := b.Dequeue(defaultSched, 0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out == nil {
|
|
|
|
t.Fatal("Expect an eval")
|
|
|
|
}
|
|
|
|
doneCh <- struct{}{}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Sleep for a little bit
|
|
|
|
select {
|
|
|
|
case <-time.After(5 * time.Millisecond):
|
|
|
|
case <-doneCh:
|
|
|
|
t.Fatalf("Dequeue(0) should block")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enqueue to unblock the dequeue.
|
|
|
|
eval := mock.Eval()
|
|
|
|
err := b.Enqueue(eval)
|
2015-11-21 00:49:55 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-11-23 19:59:49 +00:00
|
|
|
select {
|
|
|
|
case <-doneCh:
|
|
|
|
return
|
|
|
|
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) {
|
|
|
|
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) {
|
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
NUM := 100
|
|
|
|
|
|
|
|
for i := 0; i < NUM; 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < NUM; i++ {
|
2015-08-12 22:25:31 +00:00
|
|
|
out1, _, _ := b.Dequeue(defaultSched, time.Second)
|
2015-07-24 05:58:12 +00:00
|
|
|
if out1.CreateIndex != uint64(i) {
|
|
|
|
t.Fatalf("bad: %d %#v", i, out1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-24 04:37:28 +00:00
|
|
|
// Ensure fairness between schedulers
|
|
|
|
func TestEvalBroker_Dequeue_Fairness(t *testing.T) {
|
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
NUM := 100
|
|
|
|
|
|
|
|
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.
|
2015-08-06 00:55:15 +00:00
|
|
|
if counter >= 25 || counter <= -25 {
|
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) {
|
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
// Start with a blocked dequeue
|
|
|
|
outCh := make(chan *structs.Evaluation, 1)
|
|
|
|
go func() {
|
|
|
|
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
|
|
|
end := time.Now()
|
|
|
|
outCh <- out
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if d := end.Sub(start); d < 5*time.Millisecond {
|
|
|
|
t.Fatalf("bad: %v", d)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for a bit
|
|
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
|
|
|
|
// Enqueue
|
2015-08-11 21:27:14 +00:00
|
|
|
eval := mock.Eval()
|
2015-07-24 04:37:28 +00:00
|
|
|
err := b.Enqueue(eval)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure dequeue
|
|
|
|
select {
|
|
|
|
case out := <-outCh:
|
|
|
|
if out != eval {
|
|
|
|
t.Fatalf("bad: %v", out)
|
|
|
|
}
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fatalf("timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we nack in a timely manner
|
|
|
|
func TestEvalBroker_Nack_Timeout(t *testing.T) {
|
|
|
|
b := testBroker(t, 5*time.Millisecond)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
// Enqueue
|
2015-08-11 21:27:14 +00:00
|
|
|
eval := mock.Eval()
|
2015-07-24 04:37:28 +00:00
|
|
|
err := b.Enqueue(eval)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
b := testBroker(t, 5*time.Millisecond)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
// Enqueue
|
|
|
|
eval := mock.Eval()
|
|
|
|
err := b.Enqueue(eval)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset in 2 milliseconds
|
|
|
|
time.Sleep(2 * 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
|
|
|
|
if diff := end.Sub(start); diff < 7*time.Millisecond {
|
|
|
|
t.Fatalf("bad: %#v", diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-16 17:55:55 +00:00
|
|
|
func TestEvalBroker_DeliveryLimit(t *testing.T) {
|
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
eval := mock.Eval()
|
|
|
|
err := b.Enqueue(eval)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
eval := mock.Eval()
|
|
|
|
err := b.Enqueue(eval)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-07 20:00:45 +00:00
|
|
|
// Ensure fairness between schedulers
|
|
|
|
func TestEvalBroker_Wait(t *testing.T) {
|
|
|
|
b := testBroker(t, 0)
|
|
|
|
b.SetEnabled(true)
|
|
|
|
|
|
|
|
// Create an eval that should wait
|
|
|
|
eval := mock.Eval()
|
|
|
|
eval.Wait = 10 * time.Millisecond
|
|
|
|
err := b.Enqueue(eval)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify waiting
|
|
|
|
stats := b.Stats()
|
|
|
|
if stats.TotalReady != 0 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
if stats.TotalWaiting != 1 {
|
|
|
|
t.Fatalf("bad: %#v", stats)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let the wait elapse
|
|
|
|
time.Sleep(15 * time.Millisecond)
|
|
|
|
|
|
|
|
// 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
|
|
|
|
out, _, err := b.Dequeue(defaultSched, time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
if out != eval {
|
|
|
|
t.Fatalf("bad : %#v", out)
|
|
|
|
}
|
|
|
|
}
|