-
Notifications
You must be signed in to change notification settings - Fork 291
/
instance_exec.go
240 lines (203 loc) · 6.48 KB
/
instance_exec.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
//
// (C) Copyright 2020-2023 Intel Corporation.
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
package server
import (
"context"
"fmt"
"os"
"strings"
"github.com/pkg/errors"
"github.com/daos-stack/daos/src/control/common"
srvpb "github.com/daos-stack/daos/src/control/common/proto/srv"
"github.com/daos-stack/daos/src/control/events"
"github.com/daos-stack/daos/src/control/lib/ranklist"
"github.com/daos-stack/daos/src/control/server/engine"
)
// EngineRunner defines an interface for starting and stopping the
// daos_engine.
type EngineRunner interface {
Start(context.Context) (engine.RunnerExitChan, error)
IsRunning() bool
Signal(os.Signal)
GetConfig() *engine.Config
}
func (ei *EngineInstance) format(ctx context.Context) error {
idx := ei.Index()
ei.log.Debugf("instance %d: checking if storage is formatted", idx)
if err := ei.awaitStorageReady(ctx); err != nil {
return err
}
if err := ei.createSuperblock(); err != nil {
return err
}
if !ei.hasSuperblock() {
return errors.Errorf("instance %d: no superblock after format", idx)
}
// After we know that the instance storage is ready, fire off
// any callbacks that were waiting for this state.
for _, readyFn := range ei.onStorageReady {
if err := readyFn(ctx); err != nil {
return err
}
}
return nil
}
// start checks to make sure that the instance has a valid superblock before
// performing any required NVMe preparation steps and launching a managed
// daos_engine instance.
func (ei *EngineInstance) start(ctx context.Context) (chan *engine.RunnerExitInfo, error) {
if err := ei.logScmStorage(); err != nil {
ei.log.Errorf("instance %d: unable to log SCM storage stats: %s", ei.Index(), err)
}
return ei.runner.Start(ctx)
}
// waitReady awaits ready signal from I/O Engine before starting
// management service on MS replicas immediately so other instances can join.
// I/O Engine modules are then loaded.
func (ei *EngineInstance) waitReady(ctx context.Context) error {
select {
case <-ctx.Done(): // propagated harness exit
return ctx.Err()
case ready := <-ei.awaitDrpcReady():
if err := ei.finishStartup(ctx, ready); err != nil {
return err
}
return nil
}
}
// finishStartup sets up instance once dRPC comms are ready, this includes setting the instance
// rank, starting management service and loading I/O Engine modules.
//
// Instance ready state is set to indicate that all setup is complete.
func (ei *EngineInstance) finishStartup(ctx context.Context, ready *srvpb.NotifyReadyReq) error {
if err := ei.handleReady(ctx, ready); err != nil {
return err
}
ei.ready.SetTrue()
for _, fn := range ei.onReady {
if err := fn(ctx); err != nil {
return err
}
}
return nil
}
// createPublishInstanceExitFunc returns onInstanceExitFn which will publish an exit
// event using the provided publish function.
func createPublishInstanceExitFunc(publish func(*events.RASEvent), hostname string) onInstanceExitFn {
return func(_ context.Context, engineIdx uint32, rank ranklist.Rank, exitErr error, exPid int) error {
if exitErr == nil {
return errors.New("expected non-nil exit error")
}
evt := events.NewEngineDiedEvent(hostname, engineIdx, rank.Uint32(),
common.ExitStatus(exitErr.Error()), exPid)
// set forwardable if there is a rank for the MS to operate on
publish(evt.WithForwardable(!rank.Equals(ranklist.NilRank)))
return nil
}
}
func (ei *EngineInstance) handleExit(ctx context.Context, exitPid int, exitErr error) {
engineIdx := ei.Index()
rank, err := ei.GetRank()
if err != nil {
ei.log.Debugf("instance %d: no rank (%s)", engineIdx, err)
}
ei._lastErr = exitErr
details := []string{fmt.Sprintf("instance %d", engineIdx)}
if exitPid != 0 {
details = append(details, fmt.Sprintf("pid %d", exitPid))
}
if !rank.Equals(ranklist.NilRank) {
details = append(details, fmt.Sprintf("rank %d", rank))
}
strDetails := strings.Join(details, ", ")
ei.log.Infof("%s exited with status: %s", strDetails, common.GetExitStatus(exitErr))
// After we know that the instance has exited, fire off
// any callbacks that were waiting for this state.
for _, exitFn := range ei.onInstanceExit {
err := exitFn(ctx, engineIdx, rank, exitErr, exitPid)
if err != nil {
ei.log.Errorf("onExit: %s", err)
}
}
if err := ei.removeSocket(); err != nil && !os.IsNotExist(errors.Cause(err)) {
ei.log.Errorf("removing socket file: %s", err)
}
}
// startRunner performs setup of and starts process runner for I/O Engine instance and
// will only return (if no errors are returned during setup) on I/O Engine
// process exit (triggered by harness shutdown through context cancellation
// or abnormal I/O Engine process termination).
func (ei *EngineInstance) startRunner(parent context.Context) (_ chan *engine.RunnerExitInfo, err error) {
ctx, cancel := context.WithCancel(parent)
defer func() {
if err != nil {
// If there was an error, cancel the context to signal the
// cleanup handlers.
cancel()
}
}()
if err = ei.format(ctx); err != nil {
return
}
runnerExitChan, err := ei.start(ctx)
if err != nil {
return
}
ei.waitDrpc.SetTrue()
return runnerExitChan, ei.waitReady(ctx)
}
// requestStart makes a request to (re-)start the engine, and blocks
// until the request is received.
func (ei *EngineInstance) requestStart(ctx context.Context) {
select {
case <-ctx.Done():
case ei.startRequested <- true:
}
}
// Run starts the control loop for an EngineInstance. Engine starts are triggered by
// calling requestStart() on the instance.
func (ei *EngineInstance) Run(ctx context.Context) {
// Start the instance control loop.
go func() {
var runnerExitCh engine.RunnerExitChan
var err error
var restartRequested bool
for {
select {
case <-ctx.Done():
return
case relaunch := <-ei.startRequested:
if !relaunch {
return
}
if runnerExitCh != nil {
restartRequested = true
continue
}
runnerExitCh, err = ei.startRunner(ctx)
if err != nil {
ei.log.Errorf("runner exited without starting process: %s", err)
ei.handleExit(ctx, 0, err)
continue
}
case runnerExit := <-runnerExitCh:
ei.handleExit(ctx, runnerExit.PID, runnerExit.Error)
runnerExitCh = nil // next runner will reset this
if restartRequested {
go ei.requestStart(ctx)
restartRequested = false
}
}
}
}()
// Start the instance runner.
ei.requestStart(ctx)
}
// Stop sends signal to stop EngineInstance runner (nonblocking).
func (ei *EngineInstance) Stop(signal os.Signal) error {
ei.runner.Signal(signal)
return nil
}