forked from couchbase/gocbcore
/
memdpipeline.go
163 lines (132 loc) · 4.08 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
package gocbcore
import (
"errors"
"fmt"
"sync"
)
var (
errPipelineClosed = errors.New("pipeline has been closed")
errPipelineFull = errors.New("pipeline is too full")
)
type memdGetClientFn func() (*memdClient, error)
type memdPipeline struct {
address string
getClientFn memdGetClientFn
maxItems int
queue *memdOpQueue
maxClients int
clients []*memdPipelineClient
clientsLock sync.Mutex
}
func newPipeline(address string, maxClients, maxItems int, getClientFn memdGetClientFn) *memdPipeline {
return &memdPipeline{
address: address,
getClientFn: getClientFn,
maxClients: maxClients,
maxItems: maxItems,
queue: newMemdOpQueue(),
}
}
func newDeadPipeline(maxItems int) *memdPipeline {
return newPipeline("", 0, maxItems, nil)
}
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) 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) Close() error {
// Shut down all the clients
var errs MultiError
pipeline.clientsLock.Lock()
clients := pipeline.clients
pipeline.clients = nil
pipeline.clientsLock.Unlock()
for _, pipecli := range clients {
err := pipecli.Close()
if err != nil {
errs.add(err)
}
}
// Kill the queue, forcing everyone to stop
pipeline.queue.Close()
return errs.get()
}
func (pipeline *memdPipeline) Drain(cb func(*memdQRequest)) {
pipeline.queue.Drain(cb)
}