-
Notifications
You must be signed in to change notification settings - Fork 208
/
post_supervisor.go
298 lines (257 loc) · 7.63 KB
/
post_supervisor.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
package activation
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"sync"
"sync/atomic"
"github.com/spacemeshos/post/initialization"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/events"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/go-spacemesh/signing"
)
// DefaultPostServiceConfig returns the default config for post service.
func DefaultPostServiceConfig() PostSupervisorConfig {
path, err := os.Executable()
if err != nil {
panic(err)
}
return PostSupervisorConfig{
PostServiceCmd: filepath.Join(filepath.Dir(path), DefaultPostServiceName),
NodeAddress: "http://127.0.0.1:9094",
MaxRetries: 10,
}
}
// DefaultTestPostServiceConfig returns the default config for post service in tests.
func DefaultTestPostServiceConfig() PostSupervisorConfig {
path, err := exec.Command("go", "env", "GOMOD").Output()
if err != nil {
panic(err)
}
return PostSupervisorConfig{
PostServiceCmd: filepath.Join(filepath.Dir(string(path)), "build", DefaultPostServiceName),
NodeAddress: "http://127.0.0.1:9094",
MaxRetries: 10,
}
}
type PostSupervisorConfig struct {
PostServiceCmd string
NodeAddress string
MaxRetries int
CACert string
Cert string
Key string
}
// PostSupervisor manages a local post service.
type PostSupervisor struct {
logger *zap.Logger
postCfg PostConfig
provingOpts PostProvingOpts
postSetupProvider postSetupProvider
atxBuilder AtxBuilder
pid atomic.Int64 // pid of the running post service, only for tests.
mtx sync.Mutex // protects fields below
eg errgroup.Group // eg manages post service goroutines.
stop context.CancelFunc // stops the running command.
}
// NewPostSupervisor returns a new post service.
func NewPostSupervisor(
logger *zap.Logger,
postCfg PostConfig,
provingOpts PostProvingOpts,
postSetupProvider postSetupProvider,
atxBuilder AtxBuilder,
) *PostSupervisor {
return &PostSupervisor{
logger: logger,
postCfg: postCfg,
provingOpts: provingOpts,
postSetupProvider: postSetupProvider,
atxBuilder: atxBuilder,
}
}
func (ps *PostSupervisor) Config() PostConfig {
return ps.postCfg
}
// Providers returns a list of available compute providers for Post setup.
func (*PostSupervisor) Providers() ([]PostSetupProvider, error) {
providers, err := initialization.OpenCLProviders()
if err != nil {
return nil, err
}
providersAlias := make([]PostSetupProvider, len(providers))
for i, p := range providers {
providersAlias[i] = PostSetupProvider(p)
}
return providersAlias, nil
}
// Benchmark runs a short benchmarking session for a given provider to evaluate its performance.
func (*PostSupervisor) Benchmark(p PostSetupProvider) (int, error) {
score, err := initialization.Benchmark(initialization.Provider(p))
if err != nil {
return score, fmt.Errorf("benchmark GPU: %w", err)
}
return score, nil
}
func (ps *PostSupervisor) Status() *PostSetupStatus {
return ps.postSetupProvider.Status()
}
func (ps *PostSupervisor) Start(cmdCfg PostSupervisorConfig, opts PostSetupOpts, sig *signing.EdSigner) error {
ps.mtx.Lock()
defer ps.mtx.Unlock()
if ps.stop != nil {
return errors.New("post service already started")
}
if _, err := os.Stat(cmdCfg.PostServiceCmd); err != nil {
return fmt.Errorf("stat post service binary %s: %w", cmdCfg.PostServiceCmd, err)
}
// TODO(mafa): verify that opts don't delete existing files
ps.eg = errgroup.Group{} // reset errgroup to allow restarts.
ctx, stop := context.WithCancel(context.Background())
ps.stop = stop
ps.eg.Go(func() error {
// If it returns any error other than context.Canceled
// (which is how we signal it to stop) then we shutdown.
err := ps.postSetupProvider.PrepareInitializer(ctx, opts, sig.NodeID())
switch {
case errors.Is(err, context.Canceled):
return nil
case err != nil:
ps.logger.Fatal("preparing POST initializer failed", zap.Error(err))
return err
}
err = ps.postSetupProvider.StartSession(ctx, sig.NodeID())
switch {
case errors.Is(err, context.Canceled):
return nil
case err != nil:
ps.logger.Fatal("initialization failed", zap.Error(err))
return err
}
ps.atxBuilder.Register(sig)
return ps.runCmd(ctx, cmdCfg, ps.postCfg, opts, ps.provingOpts, sig.NodeID())
})
return nil
}
// Stop stops the post service.
func (ps *PostSupervisor) Stop(deleteFiles bool) error {
ps.mtx.Lock()
defer ps.mtx.Unlock()
if ps.stop == nil {
return nil
}
ps.stop()
ps.stop = nil
ps.pid.Store(0)
err := ps.eg.Wait()
switch {
case err == nil || errors.Is(err, context.Canceled):
if !deleteFiles {
return nil
}
if err := ps.postSetupProvider.Reset(); err != nil {
ps.logger.Error("failed to delete post files", zap.Error(err))
return err
}
return nil
default:
return fmt.Errorf("stop post supervisor: %w", err)
}
}
// captureCmdOutput returns a function that reads from the given pipe and logs the output.
// it returns when the pipe is closed.
func (ps *PostSupervisor) captureCmdOutput(pipe io.ReadCloser, smesherId types.NodeID) func() error {
return func() error {
scanner := bufio.NewScanner(pipe)
logger := ps.logger.With(log.ZShortStringer("smesherID", smesherId))
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimRight(line, "\r\n") // remove line delimiters at end of input
logger.Info(line)
}
return nil
}
}
func (ps *PostSupervisor) runCmd(
ctx context.Context,
cmdCfg PostSupervisorConfig,
postCfg PostConfig,
postOpts PostSetupOpts,
provingOpts PostProvingOpts,
smesherId types.NodeID,
) error {
args := []string{
"--address", cmdCfg.NodeAddress,
"--min-num-units", strconv.FormatUint(uint64(postCfg.MinNumUnits), 10),
"--max-num-units", strconv.FormatUint(uint64(postCfg.MaxNumUnits), 10),
"--k1", strconv.FormatUint(uint64(postCfg.K1), 10),
"--k2", strconv.FormatUint(uint64(postCfg.K2), 10),
"--pow-difficulty", postCfg.PowDifficulty.String(),
"--dir", postOpts.DataDir,
"-n", strconv.FormatUint(uint64(postOpts.Scrypt.N), 10),
"-r", strconv.FormatUint(uint64(postOpts.Scrypt.R), 10),
"-p", strconv.FormatUint(uint64(postOpts.Scrypt.P), 10),
"--threads", strconv.FormatUint(uint64(provingOpts.Threads), 10),
"--nonces", strconv.FormatUint(uint64(provingOpts.Nonces), 10),
"--randomx-mode", provingOpts.RandomXMode.String(),
"--watch-pid", strconv.Itoa(os.Getpid()),
}
if cmdCfg.MaxRetries > 0 {
args = append(args, "--max-retries", strconv.Itoa(cmdCfg.MaxRetries))
}
if cmdCfg.CACert != "" {
args = append(args, "--ca-cert", cmdCfg.CACert)
}
if cmdCfg.Cert != "" {
args = append(args, "--cert", cmdCfg.Cert)
}
if cmdCfg.Key != "" {
args = append(args, "--key", cmdCfg.Key)
}
cmd := exec.CommandContext(
ctx,
cmdCfg.PostServiceCmd,
args...,
)
pipe, err := cmd.StderrPipe()
if err != nil {
ps.logger.Error("setup stderr pipe for post service", zap.Error(err))
return nil
}
var eg errgroup.Group
eg.Go(ps.captureCmdOutput(pipe, smesherId))
if err := cmd.Start(); err != nil {
pipe.Close()
ps.logger.Error("start post service", zap.Error(err))
return nil
}
ps.logger.Info(
"post service started",
zap.Int("pid", cmd.Process.Pid),
zap.String("cmd", cmd.String()),
log.ZShortStringer("smesherID", smesherId),
)
ps.pid.Store(int64(cmd.Process.Pid))
events.EmitPostServiceStarted()
err = cmd.Wait()
if ctx.Err() != nil {
events.EmitPostServiceStopped()
if err := eg.Wait(); err != nil {
ps.logger.Warn("output reading goroutine failed", zap.Error(err))
}
return nil
}
eg.Wait()
ps.logger.Fatal("post service exited", zap.Error(err))
return nil
}