aboutsummaryrefslogtreecommitdiffstats
path: root/client/go/internal/vespa/document/dispatcher.go
blob: 153631a8f5ed4f385804a1b717569d2976499e34 (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package document

import (
	"fmt"
	"io"
	"strconv"
	"strings"
	"sync"
	"sync/atomic"
	"time"
)

// maxAttempts controls the maximum number of times a document operation is attempted before giving up.
const maxAttempts = 10

// Feeder is the interface for a consumer of documents.
type Feeder interface{ Send(Document) Result }

// Dispatcher dispatches documents from a queue to a Feeder.
type Dispatcher struct {
	feeder         Feeder
	throttler      Throttler
	circuitBreaker CircuitBreaker
	stats          Stats

	started bool
	results chan documentOp
	msgs    chan string

	inflight      map[string]*Queue[documentOp]
	inflightCount atomic.Int64
	output        io.Writer
	verbose       bool

	mu         sync.Mutex
	statsMu    sync.Mutex
	wg         sync.WaitGroup
	inflightWg sync.WaitGroup
}

// documentOp represents a document operation and the number of times it has been attempted.
type documentOp struct {
	document Document
	result   Result
	attempts int
}

func (op documentOp) resetResult() documentOp {
	op.result = Result{}
	return op
}

func NewDispatcher(feeder Feeder, throttler Throttler, breaker CircuitBreaker, output io.Writer, verbose bool) *Dispatcher {
	d := &Dispatcher{
		feeder:         feeder,
		throttler:      throttler,
		circuitBreaker: breaker,
		inflight:       make(map[string]*Queue[documentOp]),
		output:         output,
		verbose:        verbose,
	}
	d.start()
	return d
}

func (d *Dispatcher) logResult(op documentOp, retry bool) {
	doc := op.document
	result := op.result
	if result.Trace != "" {
		d.msgs <- fmt.Sprintf("feed: trace for %s %s:\n%s", doc.Operation, doc.Id, result.Trace)
	}
	if !d.verbose && (retry || result.Success()) {
		return
	}
	var msg strings.Builder
	msg.WriteString("feed: got ")
	if result.Err != nil {
		msg.WriteString("error \"")
		msg.WriteString(result.Err.Error())
		msg.WriteString("\"")
	} else {
		msg.WriteString("status ")
		msg.WriteString(strconv.Itoa(result.HTTPStatus))
	}
	msg.WriteString(" (")
	if result.Body != nil {
		msg.Write(result.Body)
	} else {
		msg.WriteString("no body")
	}
	msg.WriteString(")")
	msg.WriteString(" for ")
	msg.WriteString(doc.Operation.String())
	msg.WriteString(" ")
	msg.WriteString(doc.Id.String())
	if !result.Success() {
		if retry {
			msg.WriteString(": retrying")
		} else if op.attempts > 1 {
			msg.WriteString(": giving up after ")
			msg.WriteString(strconv.Itoa(maxAttempts))
			msg.WriteString(" attempts")
		} else {
			msg.WriteString(": not retryable")
		}
	}
	d.msgs <- msg.String()
}

func (d *Dispatcher) shouldRetry(op documentOp, result Result) bool {
	if result.Success() {
		d.throttler.Success()
		d.circuitBreaker.Success()
		return false
	} else if result.HTTPStatus == 429 {
		d.throttler.Throttled(d.inflightCount.Load())
		return true
	} else if result.Err != nil || result.HTTPStatus == 500 || result.HTTPStatus == 502 || result.HTTPStatus == 503 || result.HTTPStatus == 504 {
		d.circuitBreaker.Failure()
		if op.attempts < maxAttempts {
			return true
		}
	}
	return false
}

func (d *Dispatcher) start() {
	d.mu.Lock()
	defer d.mu.Unlock()
	if d.started {
		return
	}
	d.results = make(chan documentOp, 4096)
	d.msgs = make(chan string, 4096)
	d.started = true
	d.wg.Add(2)
	go d.processResults()
	go d.printMessages()
}

func (d *Dispatcher) dispatch(op documentOp) {
	if !d.acceptDocument() {
		d.msgs <- fmt.Sprintf("refusing to dispatch document %s: too many errors", op.document.Id.String())
		d.results <- op.resetResult()
		return
	}
	go func() {
		op.attempts++
		op.result = d.feeder.Send(op.document)
		d.results <- op
	}()
}

func (d *Dispatcher) processResults() {
	defer d.wg.Done()
	for op := range d.results {
		d.statsMu.Lock()
		d.stats.Add(op.result)
		d.statsMu.Unlock()
		retry := d.shouldRetry(op, op.result)
		d.logResult(op, retry)
		if retry {
			d.enqueue(op.resetResult(), true)
		} else {
			op.document.Reset()
			d.inflightWg.Done()
		}
		d.dispatchNext(op.document.Id)
	}
}

func (d *Dispatcher) dispatchNext(id Id) {
	d.mu.Lock()
	defer d.mu.Unlock()
	k := id.String()
	q, ok := d.inflight[k]
	if !ok {
		panic("no queue exists for " + id.String() + ": this should not happen")
	}
	hasNext := q != nil
	if hasNext {
		if next, ok := q.Poll(); ok {
			// we have more operations with this ID: dispatch the next one
			d.dispatch(next)
		} else {
			hasNext = false
		}
	}
	if !hasNext {
		// no more operations with this ID: release slot
		delete(d.inflight, k)
		d.releaseSlot()
	}
}

func (d *Dispatcher) printMessages() {
	defer d.wg.Done()
	for msg := range d.msgs {
		fmt.Fprintln(d.output, msg)
	}
}

func (d *Dispatcher) enqueue(op documentOp, isRetry bool) error {
	d.mu.Lock()
	if !d.started {
		d.mu.Unlock()
		return fmt.Errorf("dispatcher is closed")
	}
	if !d.acceptDocument() {
		d.mu.Unlock()
		return fmt.Errorf("refusing to enqueue document %s: too many errors", op.document.Id.String())
	}
	k := op.document.Id.String()
	q, ok := d.inflight[k]
	if !ok {
		d.inflight[k] = nil // track operation, but defer allocating queue until needed
	} else {
		if q == nil {
			q = NewQueue[documentOp]()
			d.inflight[k] = q
		}
		q.Add(op, isRetry)
	}
	if !isRetry {
		d.inflightWg.Add(1)
	}
	d.mu.Unlock()
	if !ok && !isRetry {
		// first operation with this ID: acquire slot and dispatch
		d.acquireSlot()
		d.dispatch(op)
		d.throttler.Sent()
	}
	return nil
}

func (d *Dispatcher) acceptDocument() bool {
	switch d.circuitBreaker.State() {
	case CircuitClosed:
		return true
	case CircuitHalfOpen:
		time.Sleep(time.Second)
		return true
	case CircuitOpen:
		return false
	}
	panic("invalid circuit state")
}

func (d *Dispatcher) acquireSlot() {
	for d.inflightCount.Load() >= d.throttler.TargetInflight() {
		time.Sleep(time.Millisecond)
	}
	d.inflightCount.Add(1)
}

func (d *Dispatcher) releaseSlot() { d.inflightCount.Add(-1) }

func (d *Dispatcher) Enqueue(doc Document) error { return d.enqueue(documentOp{document: doc}, false) }

func (d *Dispatcher) Stats() Stats {
	d.statsMu.Lock()
	defer d.statsMu.Unlock()
	statsCopy := d.stats.Clone()
	statsCopy.Inflight = d.inflightCount.Load()
	return statsCopy
}

// Close waits for all inflight operations to complete and closes the dispatcher.
func (d *Dispatcher) Close() error {
	d.inflightWg.Wait() // Wait for all inflight operations to complete
	d.mu.Lock()
	if d.started {
		close(d.results)
		close(d.msgs)
		d.started = false
	}
	d.mu.Unlock()
	d.wg.Wait() // Wait for all channel readers to return
	return nil
}