forked from hashicorp/serf
/
agent.go
256 lines (206 loc) · 5.56 KB
/
agent.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
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
package agent
import (
"fmt"
"github.com/hashicorp/serf/serf"
"io"
"log"
"net"
"net/rpc"
"os"
"sync"
)
// Agent starts and manages a Serf instance, adding some niceties
// on top of Serf such as storing logs that you can later retrieve,
// invoking and EventHandler when events occur, and putting an RPC
// layer in front so that you can query and control the Serf instance
// remotely.
type Agent struct {
// EventHandler is what is invoked when an event occurs. If this
// isn't set, then events aren't handled in any special way.
EventHandler EventHandler
// LogOutput is where log messages are written to for the Agent,
// Serf, and the underlying Memberlist.
LogOutput io.Writer
// RPCAddr is the address to bind the RPC listener to.
RPCAddr string
// SerfConfig is the configuration of Serf to use. Some settings
// in here may be overriden by the agent.
SerfConfig *serf.Config
logs []string
logChs map[chan<- string]struct{}
logIndex int
logLock sync.Mutex
logger *log.Logger
once sync.Once
rpcListener net.Listener
serf *serf.Serf
shutdownCh chan<- struct{}
state AgentState
lock sync.Mutex
}
type AgentState int
const (
AgentIdle AgentState = iota
AgentRunning
)
// Join asks the Serf instance to join. See the Serf.Join function.
func (a *Agent) Join(addrs []string) (n int, err error) {
a.once.Do(a.init)
a.logger.Printf("[INFO] Agent joining: %v", addrs)
n, err = a.serf.Join(addrs)
return
}
// NotifyLogs causes the agent to begin sending log events to the
// given channel. The return value is a buffer of past events up to a
// point.
//
// All NotifyLogs calls should be paired with a StopLogs call.
func (a *Agent) NotifyLogs(ch chan<- string) []string {
a.once.Do(a.init)
a.logLock.Lock()
defer a.logLock.Unlock()
if a.logChs == nil {
a.logChs = make(map[chan<- string]struct{})
}
a.logChs[ch] = struct{}{}
if a.logs == nil {
return nil
}
past := make([]string, 0, len(a.logs))
if a.logs[a.logIndex] != "" {
past = append(past, a.logs[a.logIndex:]...)
}
past = append(past, a.logs[:a.logIndex]...)
return past
}
// StopLogs causes the agent to stop sending logs to the given
// channel.
func (a *Agent) StopLogs(ch chan<- string) {
a.once.Do(a.init)
a.logLock.Lock()
defer a.logLock.Unlock()
delete(a.logChs, ch)
}
// Returns the Serf agent of the running Agent.
func (a *Agent) Serf() *serf.Serf {
return a.serf
}
// Shutdown does a graceful shutdown of this agent and all of its processes.
func (a *Agent) Shutdown() error {
a.once.Do(a.init)
a.lock.Lock()
defer a.lock.Unlock()
if a.state == AgentIdle {
return nil
}
// Stop the RPC listener which in turn will stop the RPC server.
if err := a.rpcListener.Close(); err != nil {
return err
}
// Gracefully leave the serf cluster
a.logger.Println("[INFO] agent: requesting graceful leave from Serf")
if err := a.serf.Leave(); err != nil {
return err
}
a.logger.Println("[INFO] agent: requesting serf shutdown")
if err := a.serf.Shutdown(); err != nil {
return err
}
a.logger.Println("[INFO] agent: shutdown complete")
a.state = AgentIdle
close(a.shutdownCh)
return nil
}
// Start starts the agent, kicking off any goroutines to handle various
// aspects of the agent.
func (a *Agent) Start() error {
a.once.Do(a.init)
a.lock.Lock()
defer a.lock.Unlock()
a.logger.Println("[INFO] Serf agent starting")
// Setup logging a bit
a.SerfConfig.MemberlistConfig.LogOutput = a.LogOutput
a.SerfConfig.LogOutput = a.LogOutput
eventCh := make(chan serf.Event, 64)
a.SerfConfig.EventCh = eventCh
var err error
a.serf, err = serf.Create(a.SerfConfig)
if err != nil {
return fmt.Errorf("Error creating Serf: %s", err)
}
a.rpcListener, err = net.Listen("tcp", a.RPCAddr)
if err != nil {
return fmt.Errorf("Error starting RPC listener: %s", err)
}
rpcServer := rpc.NewServer()
err = registerEndpoint(rpcServer, a)
if err != nil {
return fmt.Errorf("Error starting RPC server: %s", err)
}
go func(l net.Listener) {
for {
conn, err := l.Accept()
if err != nil {
a.logger.Printf("[ERR] RPC accept error: %s", err)
return
}
go rpcServer.ServeConn(conn)
}
}(a.rpcListener)
shutdownCh := make(chan struct{})
// Only listen for events if we care about events.
go a.eventLoop(a.EventHandler, eventCh, shutdownCh)
a.shutdownCh = shutdownCh
a.state = AgentRunning
a.logger.Println("[INFO] Serf agent started")
return nil
}
// UserEvent sends a UserEvent on Serf, see Serf.UserEvent.
func (a *Agent) UserEvent(name string, payload []byte) error {
a.logger.Printf("Requesting user event send: %s %#v",
name, string(payload))
return a.serf.UserEvent(name, payload)
}
func (a *Agent) storeLog(v string) {
a.logLock.Lock()
defer a.logLock.Unlock()
if a.logs == nil {
a.logs = make([]string, 512)
}
a.logs[a.logIndex] = v
a.logIndex++
if a.logIndex >= len(a.logs) {
a.logIndex = 0
}
for ch, _ := range a.logChs {
select {
case ch <- v:
default:
}
}
}
func (a *Agent) eventLoop(h EventHandler, eventCh <-chan serf.Event, done <-chan struct{}) {
for {
select {
case <-done:
return
case e := <-eventCh:
a.logger.Printf("[INFO] agent: Received event: %s", e.String())
if h == nil {
continue
}
err := h.HandleEvent(a.logger, e)
if err != nil {
a.logger.Printf("[ERR] agent: Error invoking event handler: %s", err)
}
}
}
}
func (a *Agent) init() {
if a.LogOutput == nil {
a.LogOutput = os.Stderr
}
eventWriter := &EventWriter{Agent: a}
a.LogOutput = io.MultiWriter(a.LogOutput, eventWriter)
a.logger = log.New(a.LogOutput, "", log.LstdFlags)
}