Skip to content

fix(core,model): backport ModelSchedulers isolation and configurable caps to 2.0.x (#4436) - #4438

Open
Acerola-1 wants to merge 1 commit into
agentscope-ai:2.0.xfrom
Acerola-1:backport/2.0.x-model-schedulers
Open

Acerola-1 wants to merge 1 commit into
agentscope-ai:2.0.xfrom
Acerola-1:backport/2.0.x-model-schedulers

Conversation

@Acerola-1

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.0.6-SNAPSHOT (targeting 2.0.x)

Description

Fixes #4436

Minimal, forward-compatible backport of ModelSchedulers and caller-owned transport streamScheduler from main (PR #4154, commit a4306d63d) to 2.0.x. Companion main parity for AnthropicChatModel is tracked in #4437.

On 2.0.x, model calls, HTTP transport SSE reads, and AgentBase.createEventStream() hardcoded Reactor's global Schedulers.boundedElastic(), which defaults to 10 * availableProcessors (20 threads on 2 vCPU containers) and shares threads with application, tool, MCP, RAG, and state-store workloads. Under high concurrency (e.g., 60–240 concurrent non-streaming model calls), requests queue behind the shared 20-thread pool before outbound dispatch and exhaust timeout budgets.

Changes

  • agentscope-core:
    • Backported io.agentscope.core.model.ModelSchedulers verbatim from main (requests(), streams(), responses()), raising the default thread cap to max(64, 10 * availableProcessors) with per-thread queue capacity 256 and exposing positive JVM properties: agentscope.model.requestThreadCap, agentscope.model.streamThreadCap, agentscope.model.responseThreadCap, and agentscope.model.taskQueueCapacity.
    • Added caller-owned Builder.streamScheduler(Scheduler) overrides (defaulting to ModelSchedulers.streams(), never disposed by transport.close()) to JdkHttpTransport and OkHttpTransport, replacing all Schedulers.boundedElastic() usages while keeping 2.0.x streaming state machines (sendInputStreamAsync, Flux.create) and existing constructors unchanged.
    • Migrated AgentBase.createEventStream() .publishOn(Schedulers.boundedElastic()) to .publishOn(ModelSchedulers.responses()).
  • agentscope-extensions-model-* (all 6 model providers):
    • OpenAIChatModel, DashScopeChatModel, OllamaChatModel: migrated blocking execution/request prep to ModelSchedulers.requests().
    • OpenAIResponsesChatModel: non-streaming → ModelSchedulers.requests(), streaming → ModelSchedulers.streams().
    • GeminiChatModel: removed redundant inner .subscribeOn(...) and migrated outer .subscribeOn(...) to streamEnabled ? ModelSchedulers.streams() : ModelSchedulers.requests().
    • AnthropicChatModel: removed redundant inner .subscribeOn(...) and migrated outer responseFlux to streamEnabled ? ModelSchedulers.streams() : ModelSchedulers.requests() before ModelUtils.applyTimeoutAndRetry(...) so both createStreaming() connection setup and stream iteration run off caller threads.
  • Documentation:
    • Updated docs/v2/en/docs/building-blocks/model.md and docs/v2/zh/docs/building-blocks/model.md with ModelSchedulers JVM properties and JdkHttpTransport/OkHttpTransport Builder.streamScheduler(...) usage accurate to 2.0.x.
  • Tests:
    • Added ModelSchedulersTest (13 cases): role prefixes, -XX:ActiveProcessorCount=2 default 64 cap across all 3 pools, child-JVM isolation with saturated boundedElasticSize=1, 0/-1 validation across all 4 properties, taskQueueCapacity=1 rejection, and AgentBase.stream(...) cancellation + temporary StreamingHook cleanup.
    • Added HttpTransportStreamSchedulerTest (5 cases): null rejection, custom stream thread verification, HTTP 500 error propagation, stalled/slow SSE cancellation, and non-disposal checks for both JdkHttpTransport and OkHttpTransport.
    • Added OpenAIChatModelSchedulingTest (1 case) and AnthropicChatModelSchedulingTest (2 cases).

Verification

  • git grep -n "boundedElastic" across agentscope-core/src/main/java/io/agentscope/core/model, AgentBase.java, and agentscope-extensions/agentscope-extensions-model: empty (0 remaining calls).
  • Ran mvn -T1C -pl agentscope-core,agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-openai,agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-dashscope,agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-ollama,agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-gemini,agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-anthropic,agentscope-extensions/agentscope-extensions-model/agentscope-extensions-model-openai-official -am spotless:apply spotless:check test (BUILD SUCCESS):
    • agentscope-core: 3,260 run, 0 failed, 0 errors, 11 pre-existing skipped (ModelSchedulersTest: 13/13, HttpTransportStreamSchedulerTest: 5/5)
    • agentscope-extensions-model-openai: 573 run, 0 failed, 0 errors, 11 pre-existing skipped (OpenAIChatModelSchedulingTest: 1/1)
    • agentscope-extensions-model-dashscope: 482 run, 0 failed, 0 errors, 2 pre-existing skipped
    • agentscope-extensions-model-openai-official: 291 run, 0 failed, 0 errors, 2 pre-existing skipped
    • agentscope-extensions-model-anthropic: 206 run, 0 failed, 0 errors, 0 skipped (AnthropicChatModelSchedulingTest: 2/2)
    • agentscope-extensions-model-ollama: 193 run, 0 failed, 0 errors, 1 pre-existing skipped
    • agentscope-extensions-model-gemini: 156 run, 0 failed, 0 errors, 0 skipped
  • Docs syntax/frontmatter/link inspection via docs/scripts/check-docs.mjs (inspectPage & checkSite) passed on both edited docs/v2/{en,zh}/docs/building-blocks/model.md; full docs npm suite (website.yml) and full reactor mvn -B -T1C clean verify are deferred to PR CI (the isolated worktree has no docs/node_modules).

Checklist

Please check the following items before code is ready to be reviewed.

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant