-
Notifications
You must be signed in to change notification settings - Fork 90
/
vhive.go
292 lines (243 loc) · 8.52 KB
/
vhive.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
// MIT License
//
// Copyright (c) 2023 Georgiy Lebedev, Dmitrii Ustiugov, Plamen Petrov and vHive team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package main
import (
"context"
"flag"
"fmt"
"net"
"os"
"runtime"
ctrdlog "github.com/containerd/containerd/log"
log "github.com/sirupsen/logrus"
"github.com/vhive-serverless/vhive/cri"
fccri "github.com/vhive-serverless/vhive/cri/firecracker"
gvcri "github.com/vhive-serverless/vhive/cri/gvisor"
ctriface "github.com/vhive-serverless/vhive/ctriface"
hpb "github.com/vhive-serverless/vhive/examples/protobuf/helloworld"
pb "github.com/vhive-serverless/vhive/proto"
"google.golang.org/grpc"
)
const (
port = ":3333"
fwdPort = ":3334"
testImageName = "ghcr.io/ease-lab/helloworld:var_workload"
)
var (
flog *os.File
orch *ctriface.Orchestrator
funcPool *FuncPool
isSaveMemory *bool
isSnapshotsEnabled *bool
isUPFEnabled *bool
isLazyMode *bool
isMetricsMode *bool
servedThreshold *uint64
pinnedFuncNum *int
criSock *string
hostIface *string
netPoolSize *int
)
func main() {
var err error
runtime.GOMAXPROCS(16)
snapshotter := flag.String("ss", "devmapper", "snapshotter name")
debug := flag.Bool("dbg", false, "Enable debug logging")
isSaveMemory = flag.Bool("ms", false, "Enable memory saving")
isSnapshotsEnabled = flag.Bool("snapshots", false, "Use VM snapshots when adding function instances")
isUPFEnabled = flag.Bool("upf", false, "Enable user-level page faults guest memory management")
isMetricsMode = flag.Bool("metrics", false, "Calculate UPF metrics")
servedThreshold = flag.Uint64("st", 1000*1000, "Functions serves X RPCs before it shuts down (if saveMemory=true)")
pinnedFuncNum = flag.Int("hn", 0, "Number of functions pinned in memory (IDs from 0 to X)")
isLazyMode = flag.Bool("lazy", false, "Enable lazy serving mode when UPFs are enabled")
criSock = flag.String("criSock", "/etc/vhive-cri/vhive-cri.sock", "Socket address for CRI service")
hostIface = flag.String("hostIface", "", "Host net-interface for the VMs to bind to for internet access")
netPoolSize = flag.Int("netPoolSize", 10, "Amount of network configs to preallocate in a pool")
sandbox := flag.String("sandbox", "firecracker", "Sandbox tech to use, valid options: firecracker, gvisor")
flag.Parse()
if *sandbox != "firecracker" && *sandbox != "gvisor" {
log.Fatalln("Only \"gvisor\" or \"firecracker\" are supported as sandboxing-techniques")
return
}
if *isUPFEnabled {
log.Error("User-level page faults are temporarily disabled (gh-807)")
return
}
if *isUPFEnabled && !*isSnapshotsEnabled {
log.Error("User-level page faults are not supported without snapshots")
return
}
if !*isUPFEnabled && *isLazyMode {
log.Error("Lazy page fault serving mode is not supported without user-level page faults")
return
}
if flog, err = os.Create("/tmp/fccd.log"); err != nil {
panic(err)
}
defer flog.Close()
log.SetFormatter(&log.TextFormatter{
TimestampFormat: ctrdlog.RFC3339NanoFixed,
FullTimestamp: true,
})
//log.SetReportCaller(true) // FIXME: make sure it's false unless debugging
log.SetOutput(os.Stdout)
if *debug {
log.SetLevel(log.DebugLevel)
log.Debug("Debug logging is enabled")
} else {
log.SetLevel(log.InfoLevel)
}
if *isSaveMemory {
log.Info(fmt.Sprintf("Creating orchestrator for pinned=%d functions", *pinnedFuncNum))
}
switch *sandbox {
case "firecracker":
testModeOn := false
orch = ctriface.NewOrchestrator(
*snapshotter,
*hostIface,
ctriface.WithTestModeOn(testModeOn),
ctriface.WithSnapshots(*isSnapshotsEnabled),
ctriface.WithUPF(*isUPFEnabled),
ctriface.WithMetricsMode(*isMetricsMode),
ctriface.WithLazyMode(*isLazyMode),
ctriface.WithNetPoolSize(*netPoolSize),
)
funcPool = NewFuncPool(*isSaveMemory, *servedThreshold, *pinnedFuncNum, testModeOn)
go setupFirecrackerCRI()
go orchServe()
fwdServe()
case "gvisor":
setupGVisorCRI()
}
}
type server struct {
pb.UnimplementedOrchestratorServer
}
type fwdServer struct {
hpb.UnimplementedFwdGreeterServer
}
func setupFirecrackerCRI() {
lis, err := net.Listen("unix", *criSock)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
fcService, err := fccri.NewFirecrackerService(orch)
if err != nil {
log.Fatalf("failed to create firecracker service %v", err)
}
criService, err := cri.NewService(fcService)
if err != nil {
log.Fatalf("failed to create CRI service %v", err)
}
criService.Register(s)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
func orchServe() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
pb.RegisterOrchestratorServer(s, &server{})
log.Println("Listening on port" + port)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
func fwdServe() {
lis, err := net.Listen("tcp", fwdPort)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
hpb.RegisterFwdGreeterServer(s, &fwdServer{})
log.Println("Listening on port" + fwdPort)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
// StartVM, StopSingleVM and StopVMs are legacy functions that manage functions and VMs
// Should be used only to bootstrap an experiment (e.g., quick parallel start of many functions)
func (s *server) StartVM(ctx context.Context, in *pb.StartVMReq) (*pb.StartVMResp, error) {
fID := in.GetId()
imageName := in.GetImage()
log.WithFields(log.Fields{"fID": fID, "image": imageName}).Info("Received direct StartVM")
tProfile := "not supported anymore"
_, _, err := funcPool.Serve(ctx, fID, imageName, "record")
if err != nil {
return &pb.StartVMResp{Message: "First serve failed", Profile: tProfile}, err
}
return &pb.StartVMResp{Message: "started VM instance for a function " + fID, Profile: tProfile}, nil
}
func (s *server) StopSingleVM(ctx context.Context, in *pb.StopSingleVMReq) (*pb.Status, error) {
fID := in.GetId()
isSync := true
log.WithFields(log.Fields{"fID": fID}).Info("Received direct StopVM")
message, err := funcPool.RemoveInstance(fID, "bogus imageName", isSync)
if err != nil {
log.Warn(message, err)
}
return &pb.Status{Message: message}, err
}
// Note: this function is to be used only before tearing down the whole orchestrator
func (s *server) StopVMs(ctx context.Context, in *pb.StopVMsReq) (*pb.Status, error) {
log.Info("Received StopVMs")
err := orch.StopActiveVMs()
if err != nil {
log.Printf("Failed to stop VMs, err: %v\n", err)
return &pb.Status{Message: "Failed to stop VMs"}, err
}
os.Exit(0)
return &pb.Status{Message: "Stopped VMs"}, nil
}
func (s *fwdServer) FwdHello(ctx context.Context, in *hpb.FwdHelloReq) (*hpb.FwdHelloResp, error) {
fID := in.GetId()
imageName := in.GetImage()
payload := in.GetPayload()
logger := log.WithFields(log.Fields{"fID": fID, "image": imageName, "payload": payload})
logger.Debug("Received FwdHelloVM")
resp, _, err := funcPool.Serve(ctx, fID, imageName, payload)
return resp, err
}
func setupGVisorCRI() {
lis, err := net.Listen("unix", *criSock)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
gvService, err := gvcri.NewGVisorService()
if err != nil {
log.Fatalf("failed to create gVisor service %v", err)
}
criService, err := cri.NewService(gvService)
if err != nil {
log.Fatalf("failed to create CRI service %v", err)
}
criService.Register(s)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}