forked from synadia-io/nex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmachine_mgr.go
463 lines (397 loc) · 16.3 KB
/
machine_mgr.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package nexnode
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/url"
"os"
"os/signal"
"path"
"strconv"
"strings"
"syscall"
"time"
"github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats.go"
"github.com/nats-io/nkeys"
agentapi "github.com/synadia-io/nex/internal/agent-api"
controlapi "github.com/synadia-io/nex/internal/control-api"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
)
const (
EventSubjectPrefix = "$NEX.events"
LogSubjectPrefix = "$NEX.logs"
WorkloadCacheBucketName = "NEXCACHE"
defaultHandshakeTimeoutMillis = 5000
defaultNatsStoreDir = "pnats"
)
// The machine manager is responsible for the pool of warm firecracker VMs. This includes starting new
// VMs, stopping VMs, and pulling VMs from the pool on demand
type MachineManager struct {
api *ApiListener
rootContext context.Context
rootCancel context.CancelFunc
config *NodeConfiguration
kp nkeys.KeyPair
nc *nats.Conn
ncInternal *nats.Conn
log *slog.Logger
allVms map[string]*runningFirecracker
warmVms chan *runningFirecracker
handshakes map[string]string
handshakeTimeout time.Duration // TODO: make configurable...
natsStoreDir string
publicKey string
}
func NewMachineManager(ctx context.Context, cancel context.CancelFunc, nc *nats.Conn, config *NodeConfiguration, log *slog.Logger) (*MachineManager, error) {
// Validate the node config
if !config.Validate() {
return nil, fmt.Errorf("failed to create new machine manager; invalid node config; %v", config.Errors)
}
// Create a new keypair
server, err := nkeys.CreateServer()
if err != nil {
return nil, fmt.Errorf("failed to create new machine manager; failed to generate keypair; %s", err)
}
pubkey, err := server.PublicKey()
if err != nil {
return nil, fmt.Errorf("failed to create new machine manager; failed to encode public key; %s", err)
}
m := &MachineManager{
rootContext: ctx,
rootCancel: cancel,
config: config,
nc: nc,
log: log,
kp: server,
publicKey: pubkey,
handshakes: make(map[string]string),
handshakeTimeout: time.Duration(defaultHandshakeTimeoutMillis * time.Millisecond),
natsStoreDir: defaultNatsStoreDir,
allVms: make(map[string]*runningFirecracker),
warmVms: make(chan *runningFirecracker, config.MachinePoolSize-1),
}
m.api = NewApiListener(log, m, config)
return m, nil
}
// Starts the machine manager. Publishes a node started event and starts the goroutine responsible for
// keeping the firecracker VM pool full
func (m *MachineManager) Start() error {
m.log.Info("Virtual machine manager starting")
natsServer, ncInternal, err := m.startInternalNats()
if err != nil {
return err
}
m.ncInternal = ncInternal
m.log.Info("Internal NATs server started", slog.String("client_url", natsServer.ClientURL()))
_, err = m.ncInternal.Subscribe("agentint.*.logs", handleAgentLog(m))
if err != nil {
return err
}
_, err = m.ncInternal.Subscribe("agentint.*.events.*", handleAgentEvent(m))
if err != nil {
return err
}
_, err = m.ncInternal.Subscribe("agentint.handshake", handleHandshake(m))
if err != nil {
return err
}
err = m.api.Start()
if err != nil {
m.log.Error("Failed to start API listener", slog.Any("err", err))
return err
}
go m.fillPool()
_ = m.PublishNodeStarted()
m.setupSignalHandlers()
return nil
}
func (m *MachineManager) DeployWorkload(vm *runningFirecracker, runRequest controlapi.RunRequest, deployRequest agentapi.DeployRequest) error {
bytes, err := json.Marshal(deployRequest)
if err != nil {
return err
}
status := m.ncInternal.Status()
m.log.Debug("NATS internal connection status",
slog.String("vmid", vm.vmmID),
slog.String("status", status.String()))
subject := fmt.Sprintf("agentint.%s.deploy", vm.vmmID)
resp, err := m.ncInternal.Request(subject, bytes, 1*time.Second)
if err != nil {
if errors.Is(err, os.ErrDeadlineExceeded) {
return errors.New("timed out waiting for acknowledgement of workload deployment")
} else {
return fmt.Errorf("failed to submit request for workload deployment: %s", err)
}
}
var deployResponse agentapi.DeployResponse
err = json.Unmarshal(resp.Data, &deployResponse)
if err != nil {
return err
}
if !deployResponse.Accepted {
return fmt.Errorf("workload rejected by agent: %s", *deployResponse.Message)
} else if runRequest.SupportsTriggerSubjects() {
for _, tsub := range runRequest.TriggerSubjects {
_, err := m.nc.Subscribe(tsub, func(msg *nats.Msg) {
intmsg := nats.NewMsg(fmt.Sprintf("agentint.%s.trigger", vm.vmmID))
intmsg.Data = msg.Data
intmsg.Header.Add("x-nex-trigger-subject", msg.Subject)
resp, err := m.ncInternal.RequestMsg(intmsg, time.Millisecond*10000) // FIXME-- make timeout configurable
if err != nil {
m.log.Error("Failed to request agent execution via internal trigger subject",
slog.Any("err", err),
slog.String("trigger_subject", tsub),
slog.String("workload_type", *runRequest.WorkloadType),
slog.String("vmid", vm.vmmID),
)
functionFailedTriggers.Add(m.rootContext, 1)
functionFailedTriggers.Add(m.rootContext, 1, metric.WithAttributes(attribute.String("namespace", vm.namespace)))
functionFailedTriggers.Add(m.rootContext, 1, metric.WithAttributes(attribute.String("workload_name", *vm.deployedWorkload.WorkloadName)))
} else if resp != nil {
runTime := resp.Header.Get("x-nex-run-nano-sec")
m.log.Debug("Received response from execution via trigger subject",
slog.String("vmid", vm.vmmID),
slog.String("trigger_subject", tsub),
slog.String("workload_type", *runRequest.WorkloadType),
slog.String("function_run_time_nanosec", runTime),
slog.Int("payload_size", len(resp.Data)),
)
runTime_int64, err := strconv.ParseInt(runTime, 10, 64)
if err != nil {
m.log.Warn("failed to log function runtime", slog.Any("err", err))
}
functionTriggers.Add(m.rootContext, 1)
functionTriggers.Add(m.rootContext, 1, metric.WithAttributes(attribute.String("namespace", vm.namespace)))
functionTriggers.Add(m.rootContext, 1, metric.WithAttributes(attribute.String("workload_name", *vm.deployedWorkload.WorkloadName)))
functionRunTimeNano.Add(m.rootContext, runTime_int64)
functionRunTimeNano.Add(m.rootContext, runTime_int64, metric.WithAttributes(attribute.String("namespace", vm.namespace)))
functionRunTimeNano.Add(m.rootContext, runTime_int64, metric.WithAttributes(attribute.String("workload_name", *vm.deployedWorkload.WorkloadName)))
err = msg.Respond(resp.Data)
if err != nil {
m.log.Error("Failed to respond to trigger subject subscription request for deployed workload",
slog.String("vmid", vm.vmmID),
slog.String("trigger_subject", tsub),
slog.String("workload_type", *runRequest.WorkloadType),
slog.Any("err", err),
)
}
}
})
if err != nil {
m.log.Error("Failed to create trigger subject subscription for deployed workload",
slog.String("vmid", vm.vmmID),
slog.String("trigger_subject", tsub),
slog.String("workload_type", *runRequest.WorkloadType),
slog.Any("err", err),
)
// TODO-- rollback the otherwise accepted deployment and return the error below...
// return err
}
m.log.Info("Created trigger subject subscription for deployed workload",
slog.String("vmid", vm.vmmID),
slog.String("trigger_subject", tsub),
slog.String("workload_type", *runRequest.WorkloadType),
)
}
}
vm.workloadStarted = time.Now().UTC()
vm.namespace = *deployRequest.Namespace
vm.workloadSpecification = runRequest
vm.deployedWorkload = deployRequest
workloadCounter.Add(m.rootContext, 1, metric.WithAttributes(attribute.String("workload_type", *vm.workloadSpecification.WorkloadType)))
workloadCounter.Add(m.rootContext, 1, metric.WithAttributes(attribute.String("namespace", vm.namespace)), metric.WithAttributes(attribute.String("workload_type", *vm.workloadSpecification.WorkloadType)))
deployedByteCounter.Add(m.rootContext, deployRequest.TotalBytes)
deployedByteCounter.Add(m.rootContext, deployRequest.TotalBytes, metric.WithAttributes(attribute.String("namespace", vm.namespace)))
allocatedVCPUCounter.Add(m.rootContext, *vm.machine.Cfg.MachineCfg.VcpuCount)
allocatedVCPUCounter.Add(m.rootContext, *vm.machine.Cfg.MachineCfg.VcpuCount, metric.WithAttributes(attribute.String("namespace", vm.namespace)))
allocatedMemoryCounter.Add(m.rootContext, *vm.machine.Cfg.MachineCfg.MemSizeMib)
allocatedMemoryCounter.Add(m.rootContext, *vm.machine.Cfg.MachineCfg.MemSizeMib, metric.WithAttributes(attribute.String("namespace", vm.namespace)))
return nil
}
// Stops the machine manager, which will in turn stop all firecracker VMs and attempt to clean
// up any applicable resources. Note that all "stopped" events emitted during a stop are best-effort
// and not guaranteed.
func (m *MachineManager) Stop() error {
m.log.Info("Virtual machine manager stopping")
m.rootCancel() // stops the pool from refilling
for _, vm := range m.allVms {
_ = m.PublishMachineStopped(vm)
vm.shutDown(m.log)
}
_ = m.PublishNodeStopped()
// Now empty the leftovers in the pool
for vm := range m.warmVms {
vm.shutDown(m.log)
// TODO: confirm this needs to be here
workloadCounter.Add(m.rootContext, -1, metric.WithAttributes(attribute.String("workload_type", *vm.workloadSpecification.WorkloadType)))
workloadCounter.Add(m.rootContext, -1, metric.WithAttributes(attribute.String("workload_type", *vm.workloadSpecification.WorkloadType)), metric.WithAttributes(attribute.String("namespace", vm.namespace)))
deployedByteCounter.Add(m.rootContext, vm.deployedWorkload.TotalBytes*-1)
deployedByteCounter.Add(m.rootContext, vm.deployedWorkload.TotalBytes*-1, metric.WithAttributes(attribute.String("namespace", vm.namespace)))
allocatedVCPUCounter.Add(m.rootContext, *vm.machine.Cfg.MachineCfg.VcpuCount*-1)
allocatedVCPUCounter.Add(m.rootContext, *vm.machine.Cfg.MachineCfg.VcpuCount*-1, metric.WithAttributes(attribute.String("namespace", vm.namespace)))
allocatedMemoryCounter.Add(m.rootContext, *vm.machine.Cfg.MachineCfg.MemSizeMib*-1)
allocatedMemoryCounter.Add(m.rootContext, *vm.machine.Cfg.MachineCfg.MemSizeMib*-1, metric.WithAttributes(attribute.String("namespace", vm.namespace)))
}
time.Sleep(100 * time.Millisecond)
_ = m.nc.Drain()
return nil
}
// Stops a single machine. Will return an error if called with a non-existent workload/vm ID
func (m *MachineManager) StopMachine(vmId string) error {
vm, exists := m.allVms[vmId]
if !exists {
return errors.New("no such workload")
}
subject := fmt.Sprintf("agentint.%s.undeploy", vm.vmmID)
// we do a request here so we don't tell firecracker to shut down until
// we get a reply
_, err := m.ncInternal.Request(subject, []byte{}, 500*time.Millisecond)
if err != nil {
return err
}
_ = m.PublishMachineStopped(vm)
vm.shutDown(m.log)
delete(m.allVms, vmId)
workloadCounter.Add(m.rootContext, -1)
workloadCounter.Add(m.rootContext, -1, metric.WithAttributes(attribute.String("namespace", vm.namespace)))
deployedByteCounter.Add(m.rootContext, vm.deployedWorkload.TotalBytes*-1)
deployedByteCounter.Add(m.rootContext, vm.deployedWorkload.TotalBytes*-1, metric.WithAttributes(attribute.String("namespace", vm.namespace)))
allocatedVCPUCounter.Add(m.rootContext, *vm.machine.Cfg.MachineCfg.VcpuCount*-1)
allocatedVCPUCounter.Add(m.rootContext, *vm.machine.Cfg.MachineCfg.VcpuCount*-1, metric.WithAttributes(attribute.String("namespace", vm.namespace)))
allocatedMemoryCounter.Add(m.rootContext, *vm.machine.Cfg.MachineCfg.MemSizeMib*-1)
allocatedMemoryCounter.Add(m.rootContext, *vm.machine.Cfg.MachineCfg.MemSizeMib*-1, metric.WithAttributes(attribute.String("namespace", vm.namespace)))
return nil
}
// Looks up a virtual machine by workload/vm ID. Returns nil if machine doesn't exist
func (m *MachineManager) LookupMachine(vmId string) *runningFirecracker {
vm, exists := m.allVms[vmId]
if !exists {
return nil
}
return vm
}
// Called by a consumer looking to submit a workload into a virtual machine. Prior to that, the machine
// must be taken out of the pool. Taking a machine out of the pool unblocks a goroutine that will automatically
// replenish
func (m *MachineManager) TakeFromPool() (*runningFirecracker, error) {
running := <-m.warmVms
//m.allVms[running.vmmID] = running
return running, nil
}
func (m *MachineManager) fillPool() {
for {
select {
case <-m.rootContext.Done():
return
default:
vm, err := createAndStartVM(m.rootContext, m.config, m.log)
if err != nil {
m.log.Error("Failed to create VMM for warming pool. Aborting.", slog.Any("err", err))
return
}
go m.awaitHandshake(vm.vmmID)
m.log.Info("Adding new VM to warm pool", slog.Any("ip", vm.ip), slog.String("vmid", vm.vmmID))
// If the pool is full, this line will block until a slot is available.
m.warmVms <- vm
// This gets executed when another goroutine pulls a vm out of the warmVms channel and unblocks
m.allVms[vm.vmmID] = vm
}
}
}
func (m *MachineManager) awaitHandshake(vmid string) {
timeoutAt := time.Now().UTC().Add(m.handshakeTimeout)
handshakeOk := false
for !handshakeOk {
if time.Now().UTC().After(timeoutAt) {
m.log.Error("Did not receive NATS handshake from agent within timeout. Exiting unstable node", slog.String("vmid", vmid))
_ = m.Stop()
os.Exit(1) // FIXME
}
_, handshakeOk = m.handshakes[vmid]
time.Sleep(time.Millisecond * agentapi.DefaultRunloopSleepTimeoutMillis)
}
}
// TODO : look into also pre-removing /var/lib/cni/networks/fcnet/ during startup sequence
// to ensure we get the full IP range
// Remove firecracker VM sockets created by this pid
func (m *MachineManager) cleanSockets() {
dir, err := os.ReadDir(os.TempDir())
if err != nil {
m.log.Error("Failed to read temp directory", slog.Any("err", err))
}
for _, d := range dir {
if strings.Contains(d.Name(), fmt.Sprintf(".firecracker.sock-%d-", os.Getpid())) {
os.Remove(path.Join([]string{"tmp", d.Name()}...))
}
}
}
func (m *MachineManager) setupSignalHandlers() {
go func() {
// both firecracker and the embedded NATS server register signal handlers... wipe those so ours are the ones being used
signal.Reset(syscall.SIGINT, syscall.SIGTERM, syscall.SIGUSR1, syscall.SIGUSR2, syscall.SIGHUP)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT)
stop := func() {
err := m.Stop()
if err != nil {
m.log.Warn("Machine manager failed to stop", slog.Any("err", err))
}
m.cleanSockets()
os.Exit(0) // FIXME
}
for {
switch s := <-c; {
case s == syscall.SIGTERM || s == os.Interrupt:
m.log.Info("Caught signal, requesting clean shutdown", slog.String("signal", s.String()))
stop()
case s == syscall.SIGQUIT:
m.log.Info("Caught quit signal, still trying graceful shutdown", slog.String("signal", s.String()))
stop()
}
}
}()
}
func (m *MachineManager) startInternalNats() (*server.Server, *nats.Conn, error) {
natsServer, err := server.NewServer(&server.Options{
Host: "0.0.0.0",
Port: -1,
JetStream: true,
NoLog: true,
StoreDir: path.Join(os.TempDir(), m.natsStoreDir),
})
if err != nil {
return nil, nil, err
}
natsServer.Start()
time.Sleep(50 * time.Millisecond) // TODO: unsure if we need to give the server time to start
clientUrl, err := url.Parse(natsServer.ClientURL())
if err != nil {
return nil, nil, fmt.Errorf("failed to parse internal NATS client URL: %s", err)
}
p, err := strconv.Atoi(clientUrl.Port())
if err != nil {
return nil, nil, fmt.Errorf("failed to parse internal NATS client URL: %s", err)
}
m.config.InternalNodePort = &p
nc, err := nats.Connect(natsServer.ClientURL())
if err != nil {
return nil, nil, fmt.Errorf("failed to connect to internal nats: %s", err)
}
jsCtx, err := nc.JetStream()
if err != nil {
return nil, nil, fmt.Errorf("failed to establish jetstream connection to internal nats: %s", err)
}
_, err = jsCtx.CreateObjectStore(&nats.ObjectStoreConfig{
Bucket: WorkloadCacheBucketName,
Description: "Object store cache for nex-node workloads",
Storage: nats.MemoryStorage,
})
if err != nil {
return nil, nil, fmt.Errorf("failed to create internal object store: %s", err)
}
return natsServer, nc, nil
}