Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//go:build linux

package server

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// TestWaitForAcquire_ResourceExhaustedMessageFormat verifies the resuming-gate
// ResourceExhausted error carries the differentiating waited= and the gate's
// max= cap. The single slot is pre-taken and the context is already cancelled,
// so Acquire returns immediately with the saturated-gate error.
func TestWaitForAcquire_ResourceExhaustedMessageFormat(t *testing.T) {
t.Parallel()

s, _ := startGateTestServer(t) // startingSandboxes limit = 1
require.True(t, s.startingSandboxes.TryAcquire(1), "pre-take the only slot")

ctx, cancel := context.WithCancel(t.Context())
cancel() // make Acquire give up at once rather than wait acquireTimeout

err := s.waitForAcquire(ctx, startGateResume)
require.Error(t, err)

st, ok := status.FromError(err)
require.True(t, ok, "must be a gRPC status error")
assert.Equal(t, codes.ResourceExhausted, st.Code())

msg := st.Message()
assert.Contains(t, msg, "too many sandboxes resuming on this node")
assert.Contains(t, msg, "waited=", "message must carry the differentiating wait duration")
assert.Contains(t, msg, "max=1", "message must carry the gate's cap via Limit()")
assert.Contains(t, msg, "please retry")
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ func TestSandboxLimitMetric(t *testing.T) {
})
require.Equal(t, codes.ResourceExhausted, status.Code(err))
if want > 0 {
require.Equal(t, "too many sandboxes starting on this node, please retry", status.Convert(err).Message())
require.Equal(t, "too many sandboxes starting on this node: max=1, please retry", status.Convert(err).Message())
} else {
require.Equal(t, fmt.Sprintf("max number of running sandboxes on node reached (%d), please retry", want), status.Convert(err).Message())
require.Equal(t, fmt.Sprintf("max number of running sandboxes on node reached: max=%d, please retry", want), status.Convert(err).Message())
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/orchestrator/pkg/server/sandboxes.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (s *Server) Create(ctx context.Context, req *orchestrator.SandboxCreateRequ
if errors.Is(err, sandbox.ErrNodeAtCapacity) {
telemetry.ReportEvent(ctx, "max number of running sandboxes reached")

return nil, status.Errorf(codes.ResourceExhausted, "max number of running sandboxes on node reached (%d), please retry", maxRunningSandboxesPerNode)
return nil, status.Errorf(codes.ResourceExhausted, "max number of running sandboxes on node reached: max=%d, please retry", maxRunningSandboxesPerNode)
}
if err != nil {
return nil, s.sandboxAlreadyRunning(ctx, req.GetSandbox().GetSandboxId(), req.GetSandbox().GetExecutionId(), err)
Expand All @@ -313,7 +313,7 @@ func (s *Server) Create(ctx context.Context, req *orchestrator.SandboxCreateRequ
if !acquired {
telemetry.ReportEvent(ctx, "too many starting sandboxes on node")

return nil, status.Errorf(codes.ResourceExhausted, "too many sandboxes starting on this node, please retry")
return nil, status.Errorf(codes.ResourceExhausted, "too many sandboxes starting on this node: max=%d, please retry", s.startingSandboxes.Limit())
}
}
defer s.startingSandboxes.Release(1)
Expand Down
6 changes: 5 additions & 1 deletion packages/orchestrator/pkg/server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ func (s *Server) waitForAcquire(ctx context.Context, kind string) error {
s.recordStartGateWait(ctx, kind, startGateCancelled, waited)
telemetry.ReportEvent(ctx, "too many resuming sandboxes on node")

return status.Errorf(codes.ResourceExhausted, "too many sandboxes resuming on this node, please retry")
// waited is the differentiating value here: unlike the create gate's
// instant TryAcquire, this path blocks up to acquireTimeout, so the
// time spent waiting separates a hard timeout (gate saturated the whole
// window) from a fast context cancellation. max is the gate's cap.
return status.Errorf(codes.ResourceExhausted, "too many sandboxes resuming on this node: waited=%s, max=%d, please retry", waited.Round(time.Millisecond), s.startingSandboxes.Limit())
}
s.recordStartGateWait(ctx, kind, startGateAcquired, waited)

Expand Down
10 changes: 10 additions & 0 deletions packages/shared/pkg/utils/resizable_semaphore.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ func (s *AdjustableSemaphore) SetLimit(limit int64) error {
return nil
}

// Limit returns the semaphore's current capacity. Used to annotate
// ResourceExhausted errors with the cap the caller hit; at the moment of
// refusal the used count equals this, so the limit is the informative value.
func (s *AdjustableSemaphore) Limit() int64 {
s.mu.Lock()
defer s.mu.Unlock()

return s.limit
}

func (s *AdjustableSemaphore) Release(n int64) {
if n <= 0 {
panic("Release: n must be > 0")
Expand Down
25 changes: 25 additions & 0 deletions packages/shared/pkg/utils/resizable_semaphore_limit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package utils

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// TestAdjustableSemaphore_Limit verifies Limit() reflects the initial capacity
// and any later SetLimit, independent of how many slots are currently held.
func TestAdjustableSemaphore_Limit(t *testing.T) {
t.Parallel()

s, err := NewAdjustableSemaphore(3)
require.NoError(t, err)
assert.Equal(t, int64(3), s.Limit())

// Taking slots does not change the limit.
require.True(t, s.TryAcquire(2))
assert.Equal(t, int64(3), s.Limit())

require.NoError(t, s.SetLimit(5))
assert.Equal(t, int64(5), s.Limit())
}