-
Notifications
You must be signed in to change notification settings - Fork 665
/
runtime.go
152 lines (127 loc) · 3.76 KB
/
runtime.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package subprocess
import (
"context"
"fmt"
"io"
"net"
"os"
"os/exec"
"strings"
"time"
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/grpcutils"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/gruntime"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/runtime"
pb "github.com/ava-labs/avalanchego/proto/pb/vm/runtime"
)
type Config struct {
// Stderr of the VM process written to this writer.
Stderr io.Writer
// Stdout of the VM process written to this writer.
Stdout io.Writer
// Duration engine server will wait for handshake success.
HandshakeTimeout time.Duration
Log logging.Logger
}
type Status struct {
// Id of the process.
Pid int
// Address of the VM gRPC service.
Addr string
}
// Bootstrap starts a VM as a subprocess after initialization completes and
// pipes the IO to the appropriate writers.
//
// The subprocess is expected to be stopped by the caller if a non-nil error is
// returned. If piping the IO fails then the subprocess will be stopped.
//
// TODO: create the listener inside this method once we refactor the tests
func Bootstrap(
ctx context.Context,
listener net.Listener,
cmd *exec.Cmd,
config *Config,
) (*Status, runtime.Stopper, error) {
defer listener.Close()
switch {
case cmd == nil:
return nil, nil, fmt.Errorf("%w: cmd required", runtime.ErrInvalidConfig)
case config.Log == nil:
return nil, nil, fmt.Errorf("%w: logger required", runtime.ErrInvalidConfig)
case config.Stderr == nil, config.Stdout == nil:
return nil, nil, fmt.Errorf("%w: stderr and stdout required", runtime.ErrInvalidConfig)
}
intitializer := newInitializer()
server := grpcutils.NewServer()
defer server.GracefulStop()
pb.RegisterRuntimeServer(server, gruntime.NewServer(intitializer))
go grpcutils.Serve(listener, server)
serverAddr := listener.Addr()
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", runtime.EngineAddressKey, serverAddr.String()))
// pass golang debug env to subprocess
for _, env := range os.Environ() {
if strings.HasPrefix(env, "GRPC_") || strings.HasPrefix(env, "GODEBUG") {
cmd.Env = append(cmd.Env, env)
}
}
stdoutPipe, err := cmd.StdoutPipe()
if err != nil {
return nil, nil, fmt.Errorf("failed to create stdout pipe: %w", err)
}
stderrPipe, err := cmd.StderrPipe()
if err != nil {
return nil, nil, fmt.Errorf("failed to create stderr pipe: %w", err)
}
// start subproccess
if err := cmd.Start(); err != nil {
return nil, nil, fmt.Errorf("failed to start process: %w", err)
}
log := config.Log
stopper := NewStopper(log, cmd)
// start stdout collector
go func() {
_, err := io.Copy(config.Stdout, stdoutPipe)
if err != nil {
log.Error("stdout collector failed",
zap.Error(err),
)
}
stopper.Stop(context.TODO())
log.Info("stdout collector shutdown")
}()
// start stderr collector
go func() {
_, err := io.Copy(config.Stderr, stderrPipe)
if err != nil {
log.Error("stderr collector failed",
zap.Error(err),
)
}
stopper.Stop(context.TODO())
log.Info("stderr collector shutdown")
}()
// wait for handshake success
timeout := time.NewTimer(config.HandshakeTimeout)
defer timeout.Stop()
select {
case <-intitializer.initialized:
case <-timeout.C:
stopper.Stop(ctx)
return nil, nil, fmt.Errorf("%w: %v", runtime.ErrHandshakeFailed, runtime.ErrProcessNotFound)
}
if intitializer.err != nil {
stopper.Stop(ctx)
return nil, nil, fmt.Errorf("%w: %v", runtime.ErrHandshakeFailed, err)
}
log.Info("plugin handshake succeeded",
zap.String("addr", intitializer.vmAddr),
)
status := &Status{
Pid: cmd.Process.Pid,
Addr: intitializer.vmAddr,
}
return status, stopper, nil
}