From 395969ec168b6af4709600c4abe04cd7d1530ef1 Mon Sep 17 00:00:00 2001 From: AdaAibaby Date: Tue, 10 Nov 2026 20:14:14 +0800 Subject: [PATCH] fix(orchestrator): annotate ResourceExhausted refusals with the metric that varies MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #3583 When a node refuses a sandbox with ResourceExhausted and the API logs "Node exhausted, trying another node", the error string carried no numbers for two of the three sites, and the running-sandboxes one carried only the cap. Operators could not tell a saturated node from a throttled start gate. Add context, choosing per site the value that actually varies at refusal time rather than a tautological "current" (which always equals the cap the moment the gate rejects): - running sandboxes: include max= (the reserve-within cap). current is definitionally == max here, so only the cap is informative. - starting sandboxes (instant TryAcquire): include max= via a new AdjustableSemaphore.Limit() accessor. Same reasoning — a refusal means the gate is full, so the cap is the new information. - resuming sandboxes (blocking Acquire up to acquireTimeout): include waited= and max=. waited is the differentiating value: it separates a hard timeout (gate saturated the whole window) from a fast context cancellation, which current/max cannot show. Only Limit() is added to AdjustableSemaphore; a Current() accessor was dropped because it carries no information a refusal does not already imply. Tests: - resizable_semaphore_limit_test.go: Limit() tracks the initial cap and SetLimit, independent of held slots. - resource_exhausted_message_test.go: the resuming refusal message carries waited= and max= via the real waitForAcquire path. - sandbox_limit_metric_test.go: updated to the new message formats. --- .../server/resource_exhausted_message_test.go | 40 +++++++++++++++++++ .../pkg/server/sandbox_limit_metric_test.go | 4 +- packages/orchestrator/pkg/server/sandboxes.go | 4 +- packages/orchestrator/pkg/server/utils.go | 6 ++- .../shared/pkg/utils/resizable_semaphore.go | 10 +++++ .../utils/resizable_semaphore_limit_test.go | 25 ++++++++++++ 6 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 packages/orchestrator/pkg/server/resource_exhausted_message_test.go create mode 100644 packages/shared/pkg/utils/resizable_semaphore_limit_test.go diff --git a/packages/orchestrator/pkg/server/resource_exhausted_message_test.go b/packages/orchestrator/pkg/server/resource_exhausted_message_test.go new file mode 100644 index 00000000000..73b80ecfc54 --- /dev/null +++ b/packages/orchestrator/pkg/server/resource_exhausted_message_test.go @@ -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") +} diff --git a/packages/orchestrator/pkg/server/sandbox_limit_metric_test.go b/packages/orchestrator/pkg/server/sandbox_limit_metric_test.go index 6c583156a88..9c9075af05b 100644 --- a/packages/orchestrator/pkg/server/sandbox_limit_metric_test.go +++ b/packages/orchestrator/pkg/server/sandbox_limit_metric_test.go @@ -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()) } } diff --git a/packages/orchestrator/pkg/server/sandboxes.go b/packages/orchestrator/pkg/server/sandboxes.go index 498ef746509..8a2b7f7846f 100644 --- a/packages/orchestrator/pkg/server/sandboxes.go +++ b/packages/orchestrator/pkg/server/sandboxes.go @@ -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) @@ -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) diff --git a/packages/orchestrator/pkg/server/utils.go b/packages/orchestrator/pkg/server/utils.go index 807307e8da0..6ded1a218db 100644 --- a/packages/orchestrator/pkg/server/utils.go +++ b/packages/orchestrator/pkg/server/utils.go @@ -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) diff --git a/packages/shared/pkg/utils/resizable_semaphore.go b/packages/shared/pkg/utils/resizable_semaphore.go index abb03e75990..36a8447066e 100644 --- a/packages/shared/pkg/utils/resizable_semaphore.go +++ b/packages/shared/pkg/utils/resizable_semaphore.go @@ -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") diff --git a/packages/shared/pkg/utils/resizable_semaphore_limit_test.go b/packages/shared/pkg/utils/resizable_semaphore_limit_test.go new file mode 100644 index 00000000000..7c91ca2bb47 --- /dev/null +++ b/packages/shared/pkg/utils/resizable_semaphore_limit_test.go @@ -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()) +}