Skip to content

Commit

Permalink
Reuse networks (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubno authored Oct 26, 2024
2 parents df3e2fa + c4430fd commit 29c0204
Show file tree
Hide file tree
Showing 18 changed files with 345 additions and 483 deletions.
2 changes: 1 addition & 1 deletion packages/api/internal/cache/builds/build_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"go.opentelemetry.io/otel/metric"

"github.com/e2b-dev/infra/packages/api/internal/api"
"github.com/e2b-dev/infra/packages/api/internal/meters"
"github.com/e2b-dev/infra/packages/shared/pkg/meters"
)

const (
Expand Down
2 changes: 1 addition & 1 deletion packages/api/internal/cache/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package instance
import (
"context"
"fmt"
"github.com/e2b-dev/infra/packages/api/internal/meters"
"go.opentelemetry.io/otel/metric"
"sync"
"time"
Expand All @@ -16,6 +15,7 @@ import (
analyticscollector "github.com/e2b-dev/infra/packages/api/internal/analytics_collector"
"github.com/e2b-dev/infra/packages/api/internal/api"
"github.com/e2b-dev/infra/packages/shared/pkg/logs"
"github.com/e2b-dev/infra/packages/shared/pkg/meters"
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
)

Expand Down
4 changes: 2 additions & 2 deletions packages/api/internal/handlers/sandbox_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import (
"github.com/e2b-dev/infra/packages/api/internal/auth"
authcache "github.com/e2b-dev/infra/packages/api/internal/cache/auth"
"github.com/e2b-dev/infra/packages/api/internal/cache/instance"
"github.com/e2b-dev/infra/packages/api/internal/meters"
"github.com/e2b-dev/infra/packages/api/internal/utils"
"github.com/e2b-dev/infra/packages/shared/pkg/id"
"github.com/e2b-dev/infra/packages/shared/pkg/logs"
"github.com/e2b-dev/infra/packages/shared/pkg/meters"
"github.com/e2b-dev/infra/packages/shared/pkg/telemetry"
)

Expand Down Expand Up @@ -101,7 +101,7 @@ func (a *APIStore) PostSandboxes(c *gin.Context) {
telemetry.ReportEvent(ctx, "waiting for create sandbox parallel limit semaphore slot")

_, rateSpan := a.Tracer.Start(ctx, "rate-limit")
counter, err := meters.GetUpDownCounter(meters.CreateRateLimitMeterName)
counter, err := meters.GetUpDownCounter(meters.RateLimitCounterMeterName)
if err != nil {
a.logger.Errorf("error getting counter: %s", err)
}
Expand Down
15 changes: 11 additions & 4 deletions packages/nomad/otel-collector.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,20 @@ processors:
timeout: 5s
filter:
metrics:
exclude:
include:
match_type: regexp
# Exclude metrics that start with `http`, `go`, `rpc`, or `nomad` but aren't `nomad.client`
metric_names:
- "^http.*"
- "^go.*"
- "^rpc.*"
- "nomad_client_host_cpu_idle"
- "nomad_client_host_disk_available"
- "nomad_client_host_disk_size"
- "nomad_client_allocs_memory_usage"
- "nomad_client_allocs_cpu_usage"
- "nomad_client_host_memory_available"
- "nomad_client_host_memory_total"
- "nomad_client_unallocated_memory"
- "orchestrator.*"
- "api.*"
attributes/session-proxy:
actions:
- key: service.name
Expand Down
2 changes: 1 addition & 1 deletion packages/orchestrator/internal/sandbox/cache/mmapfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func NewMmapfileCache() *MmapfileCache {
cache.OnEviction(func(ctx context.Context, reason ttlcache.EvictionReason, item *ttlcache.Item[string, *Mmapfile]) {
err := item.Value().Close()
if err != nil {
fmt.Printf("failed to close mmapfile: %v", err)
fmt.Fprintf(os.Stderr, "failed to close mmapfile: %v", err)
}
})

Expand Down
2 changes: 1 addition & 1 deletion packages/orchestrator/internal/sandbox/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s *Sandbox) Healthcheck(ctx context.Context, alwaysReport bool) {
s.Logger.Healthcheck(err == nil, alwaysReport)
}()

address := fmt.Sprintf("http://%s:%d/health", s.slot.HostSnapshotIP(), consts.DefaultEnvdServerPort)
address := fmt.Sprintf("http://%s:%d/health", s.slot.HostIP(), consts.DefaultEnvdServerPort)

request, err := http.NewRequestWithContext(ctx, "GET", address, nil)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions packages/orchestrator/internal/sandbox/fc.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ type fc struct {
func (fc *fc) wait() error {
err := fc.cmd.Wait()
if err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
// Check if the process was killed by a signal
if status, ok := exitErr.Sys().(syscall.WaitStatus); ok && status.Signaled() && status.Signal() == syscall.SIGKILL {
return nil
}
}
return fmt.Errorf("error waiting for fc process: %w", err)
}

Expand Down
30 changes: 10 additions & 20 deletions packages/orchestrator/internal/sandbox/mock_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sandbox
import (
"context"
"fmt"
"os"
"time"

"go.opentelemetry.io/otel"
Expand All @@ -23,32 +24,21 @@ func MockInstance(envID, instanceID string, dns *dns.DNS, keepAlive time.Duratio

consulClient, err := consul.New(childCtx)

networkPool := make(chan IPSlot, 1)

logger := logs.NewSandboxLogger(instanceID, envID, "test-team", 2, 512, false)

select {
case <-ctx.Done():
return
default:
ips, err := NewSlot(ctx, tracer, consulClient)
if err != nil {
fmt.Printf("failed to create network: %v\n", err)
networkPool := NewNetworkSlotPool(1, 0)

return
go func() {
poolErr := networkPool.Start(ctx, consulClient)
if poolErr != nil {
fmt.Fprintf(os.Stderr, "network pool error: %v\n", poolErr)
}

err = ips.CreateNetwork(ctx, tracer)
if err != nil {
ips.Release(ctx, tracer, consulClient)

fmt.Printf("failed to create network: %v\n", err)

return
closeErr := networkPool.Close(consulClient)
if closeErr != nil {
fmt.Fprintf(os.Stderr, "network pool close error: %v\n", closeErr)
}

networkPool <- *ips
}
}()

start := time.Now()

Expand Down
Loading

0 comments on commit 29c0204

Please sign in to comment.