Skip to content

Commit 87a0e2d

Browse files
authored
feat(orchestrator): allow customizing number of pre-warmed NBDs (#2266)
* feat(orchestrator): allow customizing number of pre-warmed NBDs * chore: add validation * chore: use config instead of hardcoded value
1 parent f88976c commit 87a0e2d

File tree

9 files changed

+25
-21
lines changed

9 files changed

+25
-21
lines changed

packages/orchestrator/.env.local

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ PERSISTENT_VOLUME_MOUNTS=test-volume-type:./.data/test-volume
2020
REDIS_URL=localhost:6379
2121
SANDBOX_CACHE_DIR=./tmp/sandbox-cache-dir
2222
SNAPSHOT_CACHE_DIR=./tmp/snapshot-cache
23+
NBD_POOL_SIZE=16
2324
STORAGE_PROVIDER=Local

packages/orchestrator/benchmark_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ func BenchmarkBaseImageLaunch(b *testing.B) {
145145
assert.NoError(b, err)
146146
})
147147

148-
devicePool, err := nbd.NewDevicePool()
148+
devicePool, err := nbd.NewDevicePool(config.NBDPoolSize)
149149
require.NoError(b, err, "do you have the nbd kernel module installed?")
150150
go func() {
151151
devicePool.Populate(b.Context())

packages/orchestrator/cmd/create-build/main.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,18 @@ func doBuild(
254254
return fmt.Errorf("build storage: %w", err)
255255
}
256256

257-
devicePool, err := nbd.NewDevicePool()
257+
blockMetrics, _ := blockmetrics.NewMetrics(noop.NewMeterProvider())
258+
259+
if os.Getenv("NODE_IP") == "" {
260+
os.Setenv("NODE_IP", "127.0.0.1")
261+
}
262+
263+
c, err := cfg.Parse()
264+
if err != nil {
265+
return fmt.Errorf("config: %w", err)
266+
}
267+
268+
devicePool, err := nbd.NewDevicePool(c.NBDPoolSize)
258269
if err != nil {
259270
return fmt.Errorf("nbd pool: %w", err)
260271
}
@@ -280,17 +291,6 @@ func doBuild(
280291
}
281292
defer dockerhubRepo.Close()
282293

283-
blockMetrics, _ := blockmetrics.NewMetrics(noop.NewMeterProvider())
284-
285-
if os.Getenv("NODE_IP") == "" {
286-
os.Setenv("NODE_IP", "127.0.0.1")
287-
}
288-
289-
c, err := cfg.Parse()
290-
if err != nil {
291-
return fmt.Errorf("config: %w", err)
292-
}
293-
294294
templateCache, err := sbxtemplate.NewCache(c, featureFlags, persistenceTemplate, blockMetrics, peerclient.NopResolver())
295295
if err != nil {
296296
return fmt.Errorf("template cache: %w", err)

packages/orchestrator/cmd/resume-build/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1013,7 +1013,7 @@ func run(ctx context.Context, buildID string, iterations int, coldStart, noPrefe
10131013
if verbose {
10141014
fmt.Println("🔧 Creating NBD device pool...")
10151015
}
1016-
devicePool, err := nbd.NewDevicePool()
1016+
devicePool, err := nbd.NewDevicePool(config.NBDPoolSize)
10171017
if err != nil {
10181018
return fmt.Errorf("nbd pool: %w", err)
10191019
}

packages/orchestrator/cmd/smoketest/smoke_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func newTestInfra(t *testing.T, ctx context.Context) *testInfra {
187187
require.NoError(t, err)
188188

189189
// NBD
190-
devicePool, err := nbd.NewDevicePool()
190+
devicePool, err := nbd.NewDevicePool(orcConfig.NBDPoolSize)
191191
require.NoError(t, err)
192192
go devicePool.Populate(ctx)
193193
ti.closers = append(ti.closers, func(ctx context.Context) { devicePool.Close(ctx) })

packages/orchestrator/pkg/cfg/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ type Config struct {
9090
RedisTLSCABase64 string `env:"REDIS_TLS_CA_BASE64"`
9191
RedisURL string `env:"REDIS_URL"`
9292
RedisPoolSize int `env:"REDIS_POOL_SIZE" envDefault:"10"`
93+
NBDPoolSize int `env:"NBD_POOL_SIZE" envDefault:"64"`
9394
Services []string `env:"ORCHESTRATOR_SERVICES" envDefault:"orchestrator"`
9495
PersistentVolumeMounts map[string]string `env:"PERSISTENT_VOLUME_MOUNTS"`
9596
}

packages/orchestrator/pkg/factories/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ func run(config cfg.Config, opts Options) (success bool) {
507507
}
508508

509509
// device pool
510-
devicePool, err := nbd.NewDevicePool()
510+
devicePool, err := nbd.NewDevicePool(config.NBDPoolSize)
511511
if err != nil {
512512
logger.L().Fatal(ctx, "failed to create device pool", zap.Error(err))
513513
}

packages/orchestrator/pkg/sandbox/nbd/pool.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ import (
2020
"github.com/e2b-dev/infra/packages/shared/pkg/utils"
2121
)
2222

23-
// maxSlotsReady is the number of slots that are ready to be used.
2423
const (
25-
maxSlotsReady = 64
2624
waitOnNBDError = 50 * time.Millisecond
2725
devicePoolCloseReleaseTimeout = 10 * time.Minute
2826
)
@@ -82,7 +80,11 @@ type DevicePool struct {
8280
slots chan DeviceSlot
8381
}
8482

85-
func NewDevicePool() (*DevicePool, error) {
83+
func NewDevicePool(maxSlotsReady int) (*DevicePool, error) {
84+
if maxSlotsReady <= 0 {
85+
return nil, fmt.Errorf("maxSlotsReady must be > 0, got %d", maxSlotsReady)
86+
}
87+
8688
maxDevices, err := getMaxDevices()
8789
if err != nil {
8890
return nil, fmt.Errorf("failed to get max devices: %w", err)
@@ -95,7 +97,7 @@ func NewDevicePool() (*DevicePool, error) {
9597
pool := &DevicePool{
9698
done: make(chan struct{}),
9799
usedSlots: bitset.New(maxDevices),
98-
slots: make(chan DeviceSlot, int(math.Min(maxSlotsReady, float64(maxDevices)))),
100+
slots: make(chan DeviceSlot, int(math.Min(float64(maxSlotsReady), float64(maxDevices)))),
99101
}
100102

101103
return pool, nil

packages/orchestrator/pkg/sandbox/nbd/testutils/nbd_device.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
func GetNBDDevice(ctx context.Context, backend block.Device, featureFlags *featureflags.Client) (nbd.DevicePath, *Cleaner, error) {
1414
var cleaner Cleaner
1515

16-
devicePool, err := nbd.NewDevicePool()
16+
devicePool, err := nbd.NewDevicePool(64)
1717
if err != nil {
1818
return "", &cleaner, fmt.Errorf("failed to create device pool: %w", err)
1919
}

0 commit comments

Comments
 (0)