-
Notifications
You must be signed in to change notification settings - Fork 205
/
manager.go
284 lines (247 loc) · 6.96 KB
/
manager.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
/*
* Copyright (c) 2020. Ant Group. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/
package process
import (
"context"
"fmt"
"os"
"os/exec"
"sync"
"syscall"
"github.com/containerd/containerd/log"
"github.com/pkg/errors"
"github.com/dragonflyoss/image-service/contrib/nydus-snapshotter/config"
"github.com/dragonflyoss/image-service/contrib/nydus-snapshotter/pkg/daemon"
"github.com/dragonflyoss/image-service/contrib/nydus-snapshotter/pkg/errdefs"
"github.com/dragonflyoss/image-service/contrib/nydus-snapshotter/pkg/store"
"github.com/dragonflyoss/image-service/contrib/nydus-snapshotter/pkg/utils/mount"
)
type configGenerator = func(*daemon.Daemon) error
type Manager struct {
store Store
nydusdBinaryPath string
DaemonMode string
mounter mount.Interface
mu sync.Mutex
}
type Opt struct {
NydusdBinaryPath string
Database *store.Database
DaemonMode string
}
func NewManager(opt Opt) (*Manager, error) {
s, err := store.NewDaemonStore(opt.Database)
if err != nil {
return nil, err
}
return &Manager{
store: s,
mounter: &mount.Mounter{},
nydusdBinaryPath: opt.NydusdBinaryPath,
DaemonMode: opt.DaemonMode,
}, nil
}
func (m *Manager) NewDaemon(daemon *daemon.Daemon) error {
m.mu.Lock()
defer m.mu.Unlock()
d, err := m.store.GetBySnapshot(daemon.SnapshotID)
if err == nil && d != nil {
return errdefs.ErrAlreadyExists
}
return m.store.Add(daemon)
}
func (m *Manager) DeleteBySnapshotID(id string) (*daemon.Daemon, error) {
m.mu.Lock()
defer m.mu.Unlock()
s, err := m.store.GetBySnapshot(id)
if err != nil {
return nil, err
}
m.store.Delete(s)
return s, nil
}
func (m *Manager) GetBySnapshotID(id string) (*daemon.Daemon, error) {
return m.store.GetBySnapshot(id)
}
func (m *Manager) GetByID(id string) (*daemon.Daemon, error) {
return m.store.Get(id)
}
func (m *Manager) DeleteDaemon(daemon *daemon.Daemon) {
if daemon == nil {
return
}
m.store.Delete(daemon)
}
func (m *Manager) ListDaemons() []*daemon.Daemon {
return m.store.List()
}
func (m *Manager) CleanUpDaemonResource(d *daemon.Daemon) {
resource := []string{d.ConfigDir, d.LogDir}
if d.IsMultipleDaemon() {
resource = append(resource, d.SocketDir)
}
for _, dir := range resource {
if err := os.RemoveAll(dir); err != nil {
log.L.Errorf("failed to remove dir %s err %v", dir, err)
}
}
}
func (m *Manager) StartDaemon(d *daemon.Daemon) error {
// if cg != nil {
// err := cg(d)
// if err != nil {
// return err
// }
// }
cmd, err := m.buildStartCommand(d)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to create start command for daemon %s", d.ID))
}
if err := cmd.Start(); err != nil {
return err
}
d.Pid = cmd.Process.Pid
// process wait when destroy daemon and kill process
return nil
}
func (m *Manager) buildStartCommand(d *daemon.Daemon) (*exec.Cmd, error) {
args := []string{
"--apisock", d.APISock(),
"--log-level", "info",
"--log-file", d.LogFile(),
"--thread-num", "10",
}
if d.IsMultipleDaemon() {
bootstrap, err := d.BootstrapFile()
if err != nil {
return nil, err
}
args = append(args,
"--config",
d.ConfigFile(),
"--bootstrap",
bootstrap,
"--mountpoint",
d.MountPoint(),
)
} else if m.isOneDaemon() {
args = append(args,
"--mountpoint",
*d.RootMountPoint,
)
} else {
return nil, errors.Errorf("DaemonMode %s doesn't have daemon configured", d.DaemonMode)
}
return exec.Command(m.nydusdBinaryPath, args...), nil
}
func (m *Manager) DestroyBySnapshotID(id string) error {
m.mu.Lock()
defer m.mu.Unlock()
d, err := m.store.GetBySnapshot(id)
if err != nil {
return err
}
return m.DestroyDaemon(d)
}
func (m *Manager) DestroyDaemon(d *daemon.Daemon) error {
m.store.Delete(d)
m.CleanUpDaemonResource(d)
log.L.Infof("umount remote snapshot, mountpoint %s", d.MountPoint())
// if daemon is shared mount or use shared mount to do
// prefetch, we should only umount the daemon with api instead
// of umount entire mountpoint
if m.isOneDaemon() {
return d.SharedUmount()
}
// if we found pid here, we need to kill and wait process to exit, Pid=0 means somehow we lost
// the daemon pid, so that we can't kill the process, just roughly umount the mountpoint
if d.Pid > 0 {
p, err := os.FindProcess(d.Pid)
if err != nil {
return err
}
err = p.Signal(syscall.SIGTERM)
if err != nil {
return err
}
_, err = p.Wait()
if err != nil {
return err
}
}
if err := m.mounter.Umount(d.MountPoint()); err != nil && err != syscall.EINVAL {
return errors.Wrap(err, fmt.Sprintf("failed to umount mountpoint %s", d.MountPoint()))
}
return nil
}
func (m *Manager) isOneDaemon() bool {
return m.DaemonMode == config.DaemonModeShared ||
m.DaemonMode == config.DaemonModePrefetch
}
func (m *Manager) isNoneDaemon() bool {
return m.DaemonMode == config.DaemonModeNone
}
func (m *Manager) IsSharedDaemon() bool {
return m.DaemonMode == config.DaemonModeShared
}
func (m *Manager) IsPrefetchDaemon() bool {
return m.DaemonMode == config.DaemonModePrefetch
}
// Reconnect already running daemons,and rebuild daemons management structs.
func (m *Manager) Reconnect(ctx context.Context) error {
var (
daemons []*daemon.Daemon
sharedDaemon *daemon.Daemon = nil
)
if m.isNoneDaemon() {
return nil
}
if err := m.store.WalkDaemons(ctx, func(d *daemon.Daemon) error {
log.L.WithField("daemon", d.ID).
WithField("mode", d.DaemonMode).
Info("found daemon in database")
// Do not check status on virtual daemons
if m.isOneDaemon() && d.ID != daemon.SharedNydusDaemonID {
daemons = append(daemons, d)
log.L.WithField("daemon", d.ID).Infof("found virtual daemon")
return nil
}
_, err := d.CheckStatus()
if err != nil {
log.L.WithField("daemon", d.ID).Warnf("failed to check daemon status")
return nil
}
log.L.WithField("daemon", d.ID).Infof("found alive daemon")
daemons = append(daemons, d)
// Get the global shared daemon here after CheckStatus() by attention
// so that we're sure it's alive.
if d.ID == daemon.SharedNydusDaemonID {
sharedDaemon = d
}
return nil
}); err != nil {
return errors.Wrapf(err, "failed to walk daemons to reconnect")
}
if !m.isOneDaemon() && sharedDaemon != nil {
return errors.Errorf("SharedDaemon or PrefetchDaemon disabled, but shared daemon is found")
}
if m.isOneDaemon() && sharedDaemon == nil && len(daemons) > 0 {
log.L.Warnf("SharedDaemon or PrefetchDaemon enabled, but cannot find alive shared daemon")
// Clear daemon list to skip adding them into daemon store
daemons = nil
}
// cleanup database so that we'll have a clean database for this snapshotter process lifetime
log.L.Infof("found %d daemons running", len(daemons))
if err := m.store.CleanupDaemons(ctx); err != nil {
return errors.Wrapf(err, "failed to cleanup database")
}
for _, d := range daemons {
if err := m.NewDaemon(d); err != nil {
return errors.Wrapf(err, "failed to add daemon(%s) to daemon store", d.ID)
}
}
return nil
}