aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/vespa/document/dispatcher_test.go
blob: 834ec8490a6e01d6c4efaded82c646713ed549c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package document

import (
	"io"
	"sync"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"
)

type mockFeeder struct {
	sendCount      int
	failCount      int
	failAfterNDocs int
	documents      []Document
	mu             sync.Mutex
}

func (f *mockFeeder) failAfterN(docs int) {
	f.mu.Lock()
	defer f.mu.Unlock()
	f.failAfterNDocs = docs
}

func (f *mockFeeder) failN(times int) {
	f.mu.Lock()
	defer f.mu.Unlock()
	f.failCount = times
}

func (f *mockFeeder) Send(doc Document) Result {
	f.mu.Lock()
	defer f.mu.Unlock()
	f.sendCount++
	result := Result{Id: doc.Id, HTTPStatus: 200}
	failRequest := (f.failAfterNDocs > 0 && len(f.documents) >= f.failAfterNDocs) ||
		(f.failCount > 0 && f.sendCount <= f.failCount)
	if failRequest {
		result.HTTPStatus = 500
		result.Status = StatusVespaFailure
	} else {
		f.documents = append(f.documents, doc)
	}
	return result
}

type mockCircuitBreaker struct{ state CircuitState }

func (c *mockCircuitBreaker) Success()            {}
func (c *mockCircuitBreaker) Failure()            {}
func (c *mockCircuitBreaker) State() CircuitState { return c.state }

func TestDispatcher(t *testing.T) {
	feeder := &mockFeeder{}
	clock := &manualClock{tick: time.Second}
	throttler := newThrottler(8, clock.now)
	breaker := NewCircuitBreaker(time.Second, 0)
	dispatcher := NewDispatcher(feeder, throttler, breaker, io.Discard, false)
	docs := []Document{
		{Id: mustParseId("id:ns:type::doc1"), Operation: OperationPut, Body: []byte(`{"fields": {"foo": "123"}}`)},
		{Id: mustParseId("id:ns:type::doc2"), Operation: OperationPut, Body: []byte(`{"fields": {"bar": "456"}}`)},
	}
	for _, d := range docs {
		dispatcher.Enqueue(d)
	}
	dispatcher.Close()
	if got, want := len(feeder.documents), 2; got != want {
		t.Errorf("got %d documents, want %d", got, want)
	}
}

func TestDispatcherOrdering(t *testing.T) {
	feeder := &mockFeeder{}
	commonId := mustParseId("id:ns:type::doc1")
	docs := []Document{
		{Id: commonId, Operation: OperationPut},
		{Id: mustParseId("id:ns:type::doc2"), Operation: OperationPut},
		{Id: mustParseId("id:ns:type::doc3"), Operation: OperationPut},
		{Id: mustParseId("id:ns:type::doc4"), Operation: OperationPut},
		{Id: commonId, Operation: OperationUpdate},
		{Id: mustParseId("id:ns:type::doc5"), Operation: OperationPut},
		{Id: mustParseId("id:ns:type::doc6"), Operation: OperationPut},
		{Id: commonId, Operation: OperationRemove},
		{Id: mustParseId("id:ns:type::doc7"), Operation: OperationPut},
		{Id: mustParseId("id:ns:type::doc8"), Operation: OperationPut},
		{Id: mustParseId("id:ns:type::doc9"), Operation: OperationPut},
	}
	clock := &manualClock{tick: time.Second}
	throttler := newThrottler(8, clock.now)
	breaker := NewCircuitBreaker(time.Second, 0)
	dispatcher := NewDispatcher(feeder, throttler, breaker, io.Discard, false)
	for _, d := range docs {
		dispatcher.Enqueue(d)
	}
	dispatcher.Close()

	var wantDocs []Document
	for _, d := range docs {
		if d.Id.Equal(commonId) {
			wantDocs = append(wantDocs, d)
		}
	}
	var gotDocs []Document
	for _, d := range feeder.documents {
		if d.Id.Equal(commonId) {
			gotDocs = append(gotDocs, d)
		}
	}
	assert.Equal(t, len(docs), len(feeder.documents))
	assert.Equal(t, wantDocs, gotDocs)
	assert.Equal(t, int64(0), dispatcher.Stats().Errors)
}

func TestDispatcherOrderingWithFailures(t *testing.T) {
	feeder := &mockFeeder{}
	commonId := mustParseId("id:ns:type::doc1")
	docs := []Document{
		{Id: commonId, Operation: OperationPut},
		{Id: commonId, Operation: OperationPut},
		{Id: commonId, Operation: OperationUpdate}, // fails
		{Id: commonId, Operation: OperationRemove}, // fails
	}
	feeder.failAfterN(2)
	clock := &manualClock{tick: time.Second}
	throttler := newThrottler(8, clock.now)
	breaker := NewCircuitBreaker(time.Second, 0)
	dispatcher := NewDispatcher(feeder, throttler, breaker, io.Discard, false)
	for _, d := range docs {
		dispatcher.Enqueue(d)
	}
	dispatcher.Close()
	wantDocs := docs[:2]
	assert.Equal(t, wantDocs, feeder.documents)
	assert.Equal(t, int64(20), dispatcher.Stats().Unsuccessful())

	// Dispatching more documents for same ID succeed
	feeder.failAfterN(0)
	dispatcher.start()
	dispatcher.Enqueue(Document{Id: commonId, Operation: OperationPut})
	dispatcher.Enqueue(Document{Id: commonId, Operation: OperationRemove})
	dispatcher.Enqueue(Document{Id: mustParseId("id:ns:type::doc2"), Operation: OperationPut})
	dispatcher.Enqueue(Document{Id: mustParseId("id:ns:type::doc3"), Operation: OperationPut})
	dispatcher.Close()
	assert.Equal(t, int64(20), dispatcher.Stats().Unsuccessful())
	assert.Equal(t, 6, len(feeder.documents))
}

func TestDispatcherOrderingWithRetry(t *testing.T) {
	feeder := &mockFeeder{}
	commonId := mustParseId("id:ns:type::doc1")
	docs := []Document{
		{Id: commonId, Operation: OperationPut}, // fails
		{Id: commonId, Operation: OperationRemove},
	}
	feeder.failN(5)
	clock := &manualClock{tick: time.Second}
	throttler := newThrottler(8, clock.now)
	breaker := NewCircuitBreaker(time.Second, 0)
	dispatcher := NewDispatcher(feeder, throttler, breaker, io.Discard, false)
	for _, d := range docs {
		dispatcher.Enqueue(d)
	}
	dispatcher.Close()
	assert.Equal(t, docs, feeder.documents)
	assert.Equal(t, int64(5), dispatcher.Stats().Unsuccessful())
}

func TestDispatcherOpenCircuit(t *testing.T) {
	feeder := &mockFeeder{}
	doc := Document{Id: mustParseId("id:ns:type::doc1"), Operation: OperationPut}
	clock := &manualClock{tick: time.Second}
	throttler := newThrottler(8, clock.now)
	breaker := &mockCircuitBreaker{}
	dispatcher := NewDispatcher(feeder, throttler, breaker, io.Discard, false)
	dispatcher.Enqueue(doc)
	dispatcher.inflightWg.Wait()
	breaker.state = CircuitOpen
	if err := dispatcher.Enqueue(doc); err == nil {
		t.Fatal("expected error due to open circuit")
	}
	dispatcher.Close()
	assert.Equal(t, 1, len(feeder.documents))
}

func BenchmarkDocumentDispatching(b *testing.B) {
	feeder := &mockFeeder{}
	clock := &manualClock{tick: time.Second}
	throttler := newThrottler(8, clock.now)
	breaker := NewCircuitBreaker(time.Second, 0)
	dispatcher := NewDispatcher(feeder, throttler, breaker, io.Discard, false)
	doc := Document{Id: mustParseId("id:ns:type::doc1"), Operation: OperationPut, Body: []byte(`{"fields": {"foo": "123"}}`)}
	b.ResetTimer() // ignore setup time

	for n := 0; n < b.N; n++ {
		dispatcher.Enqueue(doc)
		dispatcher.inflightWg.Wait()
	}
}