summaryrefslogtreecommitdiffstats
path: root/client/go/internal/vespa/document/dispatcher.go
blob: 7011ae7a9b6365662f3da208e2e8e3fd1ab234c3 (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
package document

import (
	"fmt"
	"sync"
	"sync/atomic"
	"time"
)

const maxAttempts = 10

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

	closed        bool
	ready         chan Id
	results       chan Result
	inflight      map[string]*documentGroup
	inflightCount int64

	mu       sync.RWMutex
	wg       sync.WaitGroup
	resultWg sync.WaitGroup
}

// documentGroup holds document operations which share their ID, and must be dispatched in order.
type documentGroup struct {
	id         Id
	operations []documentOp
	mu         sync.Mutex
}

type documentOp struct {
	document Document
	attempts int
}

func (g *documentGroup) append(op documentOp) {
	g.mu.Lock()
	defer g.mu.Unlock()
	g.operations = append(g.operations, op)
}

func NewDispatcher(feeder Feeder, throttler Throttler, breaker CircuitBreaker) *Dispatcher {
	d := &Dispatcher{
		feeder:         feeder,
		throttler:      throttler,
		circuitBreaker: breaker,
		inflight:       make(map[string]*documentGroup),
	}
	d.start()
	return d
}

func (d *Dispatcher) dispatchAll(g *documentGroup) {
	g.mu.Lock()
	defer g.mu.Unlock()
	for i := 0; i < len(g.operations); i++ {
		op := g.operations[i]
		ok := false
		for !ok {
			op.attempts++
			result := d.feeder.Send(op.document)
			d.results <- result
			ok = result.Status.Success()
			if !d.shouldRetry(op, result) {
				break
			}
		}
		d.releaseSlot()
	}
	g.operations = nil
}

func (d *Dispatcher) shouldRetry(op documentOp, result Result) bool {
	if result.HTTPStatus/100 == 2 || result.HTTPStatus == 404 || result.HTTPStatus == 412 {
		d.throttler.Success()
		d.circuitBreaker.Success()
		return false
	}
	if result.HTTPStatus == 429 || result.HTTPStatus == 503 {
		d.throttler.Throttled(atomic.LoadInt64(&d.inflightCount))
		return true
	}
	if result.HTTPStatus == 500 || result.HTTPStatus == 502 || result.HTTPStatus == 504 {
		d.circuitBreaker.Error(fmt.Errorf("request failed with status %d", result.HTTPStatus))
		if op.attempts <= maxAttempts {
			return true
		}
	}
	return false
}

func (d *Dispatcher) start() {
	d.mu.Lock()
	defer d.mu.Unlock()
	d.ready = make(chan Id, 4096)
	d.results = make(chan Result, 4096)
	d.closed = false
	d.wg.Add(1)
	go func() {
		defer d.wg.Done()
		d.readDocuments()
	}()
	d.resultWg.Add(1)
	go func() {
		defer d.resultWg.Done()
		d.readResults()
	}()
}

func (d *Dispatcher) readDocuments() {
	for id := range d.ready {
		d.mu.RLock()
		group := d.inflight[id.String()]
		d.mu.RUnlock()
		if group != nil {
			d.wg.Add(1)
			go func() {
				defer d.wg.Done()
				d.dispatchAll(group)
			}()
		}
	}
}

func (d *Dispatcher) readResults() {
	for result := range d.results {
		d.stats.Add(result.Stats)
	}
}

func (d *Dispatcher) Enqueue(doc Document) error {
	d.mu.Lock()
	if d.closed {
		return fmt.Errorf("dispatcher is closed")
	}
	group, ok := d.inflight[doc.Id.String()]
	if ok {
		group.append(documentOp{document: doc})
	} else {
		group = &documentGroup{
			id:         doc.Id,
			operations: []documentOp{{document: doc}},
		}
		d.inflight[doc.Id.String()] = group
	}
	d.mu.Unlock()
	d.enqueueWithSlot(doc.Id)
	return nil
}

func (d *Dispatcher) Stats() Stats { return d.stats }

func (d *Dispatcher) enqueueWithSlot(id Id) {
	d.acquireSlot()
	d.ready <- id
	d.throttler.Sent()
}

func (d *Dispatcher) acquireSlot() {
	for atomic.LoadInt64(&d.inflightCount) >= d.throttler.TargetInflight() {
		time.Sleep(time.Millisecond)
	}
	atomic.AddInt64(&d.inflightCount, 1)
}

func (d *Dispatcher) releaseSlot() { atomic.AddInt64(&d.inflightCount, -1) }

func closeAndWait[T any](ch chan T, wg *sync.WaitGroup, d *Dispatcher, markClosed bool) {
	d.mu.Lock()
	if !d.closed {
		close(ch)
		if markClosed {
			d.closed = true
		}
	}
	d.mu.Unlock()
	wg.Wait()
}

// Close closes the dispatcher and waits for all inflight operations to complete.
func (d *Dispatcher) Close() error {
	closeAndWait(d.ready, &d.wg, d, false)
	closeAndWait(d.results, &d.resultWg, d, true)
	return nil
}