forked from couchbase/gocbcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memdpipeline.go
209 lines (170 loc) · 5.25 KB
/
memdpipeline.go
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
package gocbcore
import (
"errors"
"fmt"
"sync"
)
var (
errPipelineClosed = errors.New("pipeline has been closed")
errPipelineFull = errors.New("pipeline is too full")
)
type memdGetClientFn func(cancelSig <-chan struct{}) (*memdClient, error)
type memdPipeline struct {
address string
getClientFn memdGetClientFn
maxItems int
queue *memdOpQueue
maxClients int
clients []*memdPipelineClient
clientsLock sync.Mutex
isSeedNode bool
}
func newPipeline(endpoint routeEndpoint, maxClients, maxItems int, getClientFn memdGetClientFn) *memdPipeline {
return &memdPipeline{
address: endpoint.Address,
getClientFn: getClientFn,
maxClients: maxClients,
maxItems: maxItems,
queue: newMemdOpQueue(),
isSeedNode: endpoint.IsSeedNode,
}
}
func newDeadPipeline(maxItems int) *memdPipeline {
return newPipeline(routeEndpoint{}, 0, maxItems, nil)
}
// nolint: unused
func (pipeline *memdPipeline) debugString() string {
var outStr string
if pipeline.address != "" {
outStr += fmt.Sprintf("Address: %s\n", pipeline.address)
outStr += fmt.Sprintf("Max Clients: %d\n", pipeline.maxClients)
outStr += fmt.Sprintf("Num Clients: %d\n", len(pipeline.clients))
outStr += fmt.Sprintf("Max Items: %d\n", pipeline.maxItems)
} else {
outStr += "Dead-Server Queue\n"
}
outStr += "Op Queue:\n"
outStr += reindentLog(" ", pipeline.queue.debugString())
return outStr
}
func (pipeline *memdPipeline) IsSeedNode() bool {
return pipeline.isSeedNode
}
func (pipeline *memdPipeline) Clients() []*memdPipelineClient {
pipeline.clientsLock.Lock()
defer pipeline.clientsLock.Unlock()
return pipeline.clients
}
func (pipeline *memdPipeline) Address() string {
return pipeline.address
}
func (pipeline *memdPipeline) StartClients() {
pipeline.clientsLock.Lock()
defer pipeline.clientsLock.Unlock()
for len(pipeline.clients) < pipeline.maxClients {
client := newMemdPipelineClient(pipeline)
pipeline.clients = append(pipeline.clients, client)
go client.Run()
}
}
func (pipeline *memdPipeline) sendRequest(req *memdQRequest, maxItems int) error {
err := pipeline.queue.Push(req, maxItems)
if err == errOpQueueClosed {
return errPipelineClosed
} else if err == errOpQueueFull {
return errPipelineFull
} else if err != nil {
return err
}
return nil
}
func (pipeline *memdPipeline) RequeueRequest(req *memdQRequest) error {
return pipeline.sendRequest(req, 0)
}
func (pipeline *memdPipeline) SendRequest(req *memdQRequest) error {
return pipeline.sendRequest(req, pipeline.maxItems)
}
// Performs a takeover of another pipeline. Note that this does not
// take over the requests queued in the old pipeline, and those must
// be drained and processed separately.
func (pipeline *memdPipeline) Takeover(oldPipeline *memdPipeline) {
if oldPipeline.address != pipeline.address {
logErrorf("Attempted pipeline takeover for differing address")
// We try to 'gracefully' error here by resolving all the requests as
// errors, but allowing the application to continue.
err := oldPipeline.Close()
if err != nil {
// Log and continue with this non-fatal error.
logDebugf("Failed to shutdown old pipeline (%s)", err)
}
// Drain all the requests as an internal error so they are not lost
oldPipeline.Drain(func(req *memdQRequest) {
req.tryCallback(nil, errCliInternalError)
})
return
}
// Migrate all the clients to the new pipeline
oldPipeline.clientsLock.Lock()
clients := oldPipeline.clients
oldPipeline.clients = nil
oldPipeline.clientsLock.Unlock()
pipeline.clientsLock.Lock()
pipeline.clients = clients
for _, client := range pipeline.clients {
client.ReassignTo(pipeline)
}
pipeline.clientsLock.Unlock()
// Shut down the old pipelines queue, this will force all the
// clients to 'refresh' their consumer, and pick up the new
// pipeline queue from the new pipeline. This will also block
// any writers from sending new requests here if they have an
// out of date route config.
oldPipeline.queue.Close()
}
func (pipeline *memdPipeline) GracefulClose() []*memdClient {
// Shut down all the clients
pipeline.clientsLock.Lock()
clients := pipeline.clients
pipeline.clients = nil
pipeline.clientsLock.Unlock()
var memdClients []*memdClient
for _, pipecli := range clients {
client := pipecli.CloseAndTakeClient()
logDebugf("Pipeline %s/%p taking memdclient %p from client %p", pipeline.address, pipeline, client, pipecli)
if client != nil {
memdClients = append(memdClients, client)
}
}
// Kill the queue, forcing everyone to stop
pipeline.queue.Close()
return memdClients
}
func (pipeline *memdPipeline) Close() error {
// Shut down all the clients
pipeline.clientsLock.Lock()
clients := pipeline.clients
pipeline.clients = nil
pipeline.clientsLock.Unlock()
hadErrors := false
for _, pipecli := range clients {
client := pipecli.CloseAndTakeClient()
if client != nil {
err := client.Close()
if err != nil {
logErrorf("failed to shutdown memdclient: %s", err)
hadErrors = true
}
// Wait for the client to finish closing.
<-client.CloseNotify()
}
}
// Kill the queue, forcing everyone to stop
pipeline.queue.Close()
if hadErrors {
return errCliInternalError
}
return nil
}
func (pipeline *memdPipeline) Drain(cb func(*memdQRequest)) {
pipeline.queue.Drain(cb)
}