-
Notifications
You must be signed in to change notification settings - Fork 32
/
node.go
274 lines (236 loc) · 6.64 KB
/
node.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package scheduler
import (
"context"
"os"
"time"
"github.com/ohsu-comp-bio/funnel/config"
"github.com/ohsu-comp-bio/funnel/logger"
"github.com/ohsu-comp-bio/funnel/util"
"github.com/ohsu-comp-bio/funnel/util/fsutil"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// NewNodeProcess returns a new Node instance
func NewNodeProcess(ctx context.Context, conf config.Config, factory Worker, log *logger.Logger) (*NodeProcess, error) {
log = log.WithFields("nodeID", conf.Node.ID)
log.Debug("NewNode", "config", conf)
cli, err := NewClient(ctx, conf.RPCClient)
if err != nil {
return nil, err
}
err = fsutil.EnsureDir(conf.Worker.WorkDir)
if err != nil {
return nil, err
}
// Detect available resources at startup
res, derr := detectResources(conf.Node, conf.Worker.WorkDir)
if derr != nil {
log.Error("error detecting resources", "error", derr)
}
timeout := util.NewIdleTimeout(time.Duration(conf.Node.Timeout))
state := NodeState_UNINITIALIZED
return &NodeProcess{
conf: conf,
client: cli,
log: log,
resources: &res,
workerRun: factory,
workers: newRunSet(),
timeout: timeout,
state: state,
drained: make(chan struct{}),
}, nil
}
// NodeProcess is a structure used for tracking available resources on a compute resource.
type NodeProcess struct {
conf config.Config
client Client
log *logger.Logger
resources *Resources
workerRun Worker
workers *runSet
timeout util.IdleTimeout
state NodeState
drained chan struct{}
}
// Run runs a node with the given config. This is responsible for communication
// with the server and starting task workers
func (n *NodeProcess) Run(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
n.log.Info("Starting node")
n.state = NodeState_ALIVE
n.checkConnection(ctx)
n.sync(ctx)
ticker := time.NewTicker(time.Duration(n.conf.Node.UpdateRate))
defer ticker.Stop()
for {
select {
case <-n.timeout.Done():
cancel()
case <-n.drained:
cancel()
case <-ctx.Done():
n.timeout.Stop()
// The node gets 10 seconds to do a final sync with the scheduler.
stopCtx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
n.state = NodeState_GONE
n.sync(stopCtx)
// close grpc client connection
n.client.Close()
// The workers get 10 seconds to finish up.
n.workers.Wait(time.Second * 10)
return nil
case <-ticker.C:
n.sync(ctx)
n.checkIdleTimer()
n.checkDrain()
}
}
}
func (n *NodeProcess) checkConnection(ctx context.Context) {
_, err := n.client.GetNode(ctx, &GetNodeRequest{Id: n.conf.Node.ID})
// If its a 404 error create a new node
s, _ := status.FromError(err)
if s.Code() != codes.NotFound {
n.log.Error("Couldn't contact server.", err)
} else {
n.log.Info("Successfully connected to server.")
}
}
// sync syncs the node's state with the server. It reports task state changes,
// handles signals from the server (new task, cancel task, etc), reports resources, etc.
//
// TODO Sync should probably use a channel to sync data access.
//
// Probably only a problem for test code, where Sync is called directly.
func (n *NodeProcess) sync(ctx context.Context) {
var r *Node
var err error
r, err = n.client.GetNode(ctx, &GetNodeRequest{Id: n.conf.Node.ID})
if err != nil {
// If its a 404 error create a new node
s, _ := status.FromError(err)
if s.Code() != codes.NotFound {
n.log.Error("Couldn't get node state during sync.", err)
return
}
n.log.Info("Starting initial node sync")
r = &Node{Id: n.conf.Node.ID}
}
// Start task workers. runSet will track task IDs
// to ensure there's only one worker per ID, so it's ok
// to call this multiple times with the same task ID.
for _, id := range r.TaskIds {
if n.workers.Add(id) {
go n.runTask(ctx, id)
}
}
// Node data has been updated. Send back to server for database update.
var derr error
*n.resources, derr = detectResources(n.conf.Node, n.conf.Worker.WorkDir)
if derr != nil {
n.log.Error("error detecting resources", "error", derr)
}
// Merge metadata
meta := map[string]string{}
for k, v := range n.conf.Node.Metadata {
meta[k] = v
}
for k, v := range r.GetMetadata() {
meta[k] = v
}
_, err = n.client.PutNode(context.Background(), &Node{
Id: n.conf.Node.ID,
Resources: n.resources,
State: n.state,
Version: r.GetVersion(),
Metadata: meta,
Hostname: hostname(),
TaskIds: r.TaskIds,
})
if err != nil {
n.log.Error("Couldn't save node update. Recovering.", err)
}
}
func (n *NodeProcess) runTask(ctx context.Context, id string) {
log := n.log.WithFields("ns", "worker", "taskID", id)
log.Info("Running task")
defer n.workers.Remove(id)
defer func() {
// task cannot fully complete until it has successfully removed the
// assigned ID from the node database. this helps prevent tasks from
// running multiple times.
ticker := time.NewTicker(time.Duration(n.conf.Node.UpdateRate))
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
r, err := n.client.GetNode(ctx, &GetNodeRequest{Id: n.conf.Node.ID})
if err != nil {
log.Error("couldn't get node state during task sync.", err)
// break out of "select", but not "for".
// i.e. try again later
break
}
// Find the finished task ID in the node's assigned IDs and remove it.
var ids []string
for _, tid := range r.TaskIds {
if tid != id {
ids = append(ids, tid)
}
}
r.TaskIds = ids
_, err = n.client.PutNode(ctx, r)
if err != nil {
log.Error("couldn't save node update during task sync.", err)
// break out of "select", but not "for".
// i.e. try again later
break
}
// Update was successful, return.
return
}
}
}()
defer func() {
if r := recover(); r != nil {
log.Error("caught panic task worker", r)
}
}()
err := n.workerRun(ctx, id)
if err != nil {
log.Error("error running task", err)
return
}
log.Info("Task complete")
}
// Check if the node is idle. If so, start the timeout timer.
func (n *NodeProcess) checkIdleTimer() {
// The pool is idle if there are no task workers.
// The pool should not time out if it's not alive (e.g. if it's initializing)
idle := n.workers.Count() == 0 && n.state == NodeState_ALIVE
if idle {
n.timeout.Start()
} else {
n.timeout.Stop()
}
}
func (n *NodeProcess) checkDrain() {
if n.workers.Count() == 0 && n.state == NodeState_DRAIN {
close(n.drained)
}
}
// Drain sets the node state to DRAIN.
func (n *NodeProcess) Drain() {
n.state = NodeState_DRAIN
}
func hostname() string {
if name, err := os.Hostname(); err == nil {
return name
}
return ""
}