wangzhigang1999 commented on code in PR #7400: URL: https://github.com/apache/kyuubi/pull/7400#discussion_r3109401310
########## externals/kyuubi-data-agent-engine/src/main/java/org/apache/kyuubi/engine/dataagent/tool/ToolRegistry.java: ########## @@ -0,0 +1,183 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.kyuubi.engine.dataagent.tool; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.openai.core.JsonValue; +import com.openai.models.FunctionDefinition; +import com.openai.models.FunctionParameters; +import com.openai.models.chat.completions.ChatCompletionCreateParams; +import com.openai.models.chat.completions.ChatCompletionFunctionTool; +import com.openai.models.chat.completions.ChatCompletionTool; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Registry for agent tools. Provides tool lookup, schema generation for LLM requests, and + * deserialization + execution of tool calls. + * + * <p>Tool metadata (name, description) comes from {@link AgentTool}. Parameter schemas are + * generated from the args class via {@link ToolSchemaGenerator}. The SDK's {@code + * function.arguments(Class)} is used for deserializing LLM responses. + * + * <p><b>Concurrency model: single-writer at startup.</b> {@link #register} is expected to be called + * only during engine initialization, before any LLM request is dispatched. After that the registry + * is read-only. {@link #addToolsTo} therefore does not synchronize on the {@code tools} map, which + * is safe under this single-writer-at-startup assumption — do NOT "fix" this by adding a lock + * around the read path. If we ever need true runtime registration, the right move is to swap {@code + * tools} for an immutable snapshot per refresh, not to grab a mutex on the hot path. + */ +public class ToolRegistry { + + private static final Logger LOG = LoggerFactory.getLogger(ToolRegistry.class); + private static final ObjectMapper JSON = new ObjectMapper(); + + private final Map<String, AgentTool<?>> tools = new LinkedHashMap<>(); + private volatile Map<String, ChatCompletionTool> cachedSpecs; + private final long toolCallTimeoutSeconds; + private final ExecutorService executor; + + /** + * @param toolCallTimeoutSeconds wall-clock cap on every {@link #executeTool} call, sourced from + * {@code kyuubi.engine.data.agent.tool.call.timeout}. When the timeout fires, the thread is + * interrupted and a descriptive error is returned to the LLM. + */ + public ToolRegistry(long toolCallTimeoutSeconds) { + this.toolCallTimeoutSeconds = toolCallTimeoutSeconds; + this.executor = + Executors.newCachedThreadPool( + r -> { + Thread t = new Thread(r, "tool-call-worker"); + t.setDaemon(true); + return t; + }); + } Review Comment: accept - Switched to a bounded `ThreadPoolExecutor(0, 8, 60s, SynchronousQueue)`. Cap is hardcoded at 8, no config knob — an LLM only fires a few concurrent tool calls per turn, and the engine doesn't serve that many sessions at once either. 8 is plenty. Adding a knob just pushes the problem to the user. - Pool full → reject right away with a clear busy error so the LLM can retry. Better than silently sitting in a queue. - Implements `AutoCloseable`, `close()` calls `shutdownNow()`, called once on engine shutdown. Already wired up in `WithMySQLContainer.stopContainer` for tests. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
