ArafatKhan2198 commented on code in PR #10524: URL: https://github.com/apache/ozone/pull/10524#discussion_r3453415587
########## hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/chatbot/recon/ReconEndpointRouter.java: ########## @@ -0,0 +1,235 @@ +/* + * 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.hadoop.ozone.recon.chatbot.recon; + +import com.google.inject.Inject; +import com.google.inject.Singleton; +import java.io.IOException; +import java.util.Map; +import javax.ws.rs.core.Response; +import org.apache.hadoop.ozone.recon.api.BucketEndpoint; +import org.apache.hadoop.ozone.recon.api.ClusterStateEndpoint; +import org.apache.hadoop.ozone.recon.api.ContainerEndpoint; +import org.apache.hadoop.ozone.recon.api.NSSummaryEndpoint; +import org.apache.hadoop.ozone.recon.api.NodeEndpoint; +import org.apache.hadoop.ozone.recon.api.OMDBInsightEndpoint; +import org.apache.hadoop.ozone.recon.api.PipelineEndpoint; +import org.apache.hadoop.ozone.recon.api.TaskStatusService; +import org.apache.hadoop.ozone.recon.api.UtilizationEndpoint; +import org.apache.hadoop.ozone.recon.api.VolumeEndpoint; + +/** + * Dispatches chatbot tool names to in-process Recon JAX-RS endpoint beans (no HTTP loopback). + * + * <p>Which tools may run is enforced upstream by {@code ChatbotAgent.validateToolCall} against + * {@link ReconApiAllowlist}; {@link #hasRoute(String)} mirrors that allowlist. An unknown + * {@code toolName} here still throws {@link IllegalArgumentException} as a defensive fallback. + * + * <p>All routes call injected endpoint beans directly in the same JVM. + */ +@Singleton +public class ReconEndpointRouter { + + private final ClusterStateEndpoint clusterStateEndpoint; + private final NodeEndpoint nodeEndpoint; + private final PipelineEndpoint pipelineEndpoint; + private final ContainerEndpoint containerEndpoint; + private final OMDBInsightEndpoint omdbInsightEndpoint; + private final VolumeEndpoint volumeEndpoint; + private final BucketEndpoint bucketEndpoint; + private final TaskStatusService taskStatusService; + private final UtilizationEndpoint utilizationEndpoint; + private final NSSummaryEndpoint nsSummaryEndpoint; + private final ReconApiAllowlist reconApiAllowlist; + + @Inject + public ReconEndpointRouter( + ClusterStateEndpoint clusterStateEndpoint, + NodeEndpoint nodeEndpoint, + PipelineEndpoint pipelineEndpoint, + ContainerEndpoint containerEndpoint, + OMDBInsightEndpoint omdbInsightEndpoint, + VolumeEndpoint volumeEndpoint, + BucketEndpoint bucketEndpoint, + TaskStatusService taskStatusService, + UtilizationEndpoint utilizationEndpoint, + NSSummaryEndpoint nsSummaryEndpoint, + ReconApiAllowlist reconApiAllowlist) { + this.clusterStateEndpoint = clusterStateEndpoint; + this.nodeEndpoint = nodeEndpoint; + this.pipelineEndpoint = pipelineEndpoint; + this.containerEndpoint = containerEndpoint; + this.omdbInsightEndpoint = omdbInsightEndpoint; + this.volumeEndpoint = volumeEndpoint; + this.bucketEndpoint = bucketEndpoint; + this.taskStatusService = taskStatusService; + this.utilizationEndpoint = utilizationEndpoint; + this.nsSummaryEndpoint = nsSummaryEndpoint; + this.reconApiAllowlist = reconApiAllowlist; + } + + public boolean hasRoute(String toolName) { + return reconApiAllowlist.isRegistered(toolName); + } + + public Response route(String toolName, Map<String, String> params) throws IOException { + // limit is pre-clamped by ReconQueryExecutor; MAX_RECORDS_PER_CALL is a defensive fallback only. + int limit = parseInt(params.get("limit"), ReconQueryExecutor.MAX_RECORDS_PER_CALL); + String startPrefix = params.get("startPrefix") == null ? "" : params.get("startPrefix"); + + switch (toolName) { + case "api_v1_clusterState": + return clusterStateEndpoint.getClusterState(); + case "api_v1_datanodes": + return nodeEndpoint.getDatanodes(); + case "api_v1_pipelines": + return pipelineEndpoint.getPipelines(); + case "api_v1_containers": + return containerEndpoint.getContainers(limit, 0L); + case "api_v1_containers_missing": + return containerEndpoint.getMissingContainers(limit); + case "api_v1_containers_unhealthy": + return routeUnhealthyContainers(params, limit); + case "api_v1_containers_unhealthy_state": + return routeUnhealthyContainersByState(params, limit); + case "api_v1_containers_deleted": + return containerEndpoint.getSCMDeletedContainers(limit, 0L); + case "api_v1_containers_mismatch": + return routeContainersMismatch(params, limit); + case "api_v1_containers_mismatch_deleted": + return containerEndpoint.getOmContainersDeletedInSCM(limit, 0L); + case "api_v1_containers_quasiClosed": + return containerEndpoint.getQuasiClosedContainers( + limit, parseLong(params.get("minContainerId"), 0L)); + case "api_v1_containers_unhealthy_export": + return containerEndpoint.listExportJobs(); + case "api_v1_keys_open": + return routeOpenKeys(params, limit, startPrefix); + case "api_v1_keys_open_summary": + return omdbInsightEndpoint.getOpenKeySummary(); + case "api_v1_keys_open_mpu_summary": + return omdbInsightEndpoint.getOpenMPUKeySummary(); + case "api_v1_keys_deletePending_summary": + return omdbInsightEndpoint.getDeletedKeySummary(); + case "api_v1_keys_deletePending": + return omdbInsightEndpoint.getDeletedKeyInfo(limit, "", startPrefix); + case "api_v1_keys_deletePending_dirs": + return omdbInsightEndpoint.getDeletedDirInfo(limit, ""); + case "api_v1_keys_deletePending_dirs_summary": + return omdbInsightEndpoint.getDeletedDirectorySummary(); + case "api_v1_keys_listKeys": + return routeListKeys(params, limit, startPrefix); + case "api_v1_volumes": + return volumeEndpoint.getVolumes(limit, ""); + case "api_v1_buckets": + return bucketEndpoint.getBuckets(params.get("volume"), limit, ""); + case "api_v1_task_status": + return taskStatusService.getTaskStats(); + case "api_v1_utilization_fileCount": + return routeFileCount(params); + case "api_v1_utilization_containerCount": + return utilizationEndpoint.getContainerCounts(parseLong(params.get("containerSize"), 0L)); + case "api_v1_namespace_summary": + return nsSummaryEndpoint.getBasicInfo(params.get("path")); + case "api_v1_namespace_usage": + return routeNamespaceUsage(params); + case "api_v1_namespace_quota": + return nsSummaryEndpoint.getQuotaUsage(params.get("path")); + case "api_v1_namespace_dist": + return nsSummaryEndpoint.getFileSizeDistribution(params.get("path")); + default: + throw new IllegalArgumentException("No in-process route for " + toolName); + } + } + + private Response routeUnhealthyContainers(Map<String, String> params, int limit) { + long maxContainerId = parseLong(params.get("maxContainerId"), 0L); + long minContainerId = parseLong(params.get("minContainerId"), 0L); + return containerEndpoint.getUnhealthyContainers(limit, maxContainerId, minContainerId); + } + + private Response routeUnhealthyContainersByState(Map<String, String> params, int limit) { + String state = params.get("state"); + long maxContainerId = parseLong(params.get("maxContainerId"), 0L); + long minContainerId = parseLong(params.get("minContainerId"), 0L); + return containerEndpoint.getUnhealthyContainers(state, limit, maxContainerId, minContainerId); + } + + private Response routeContainersMismatch(Map<String, String> params, int limit) { + String missingIn = params.get("missingIn") == null ? "" : params.get("missingIn"); + return containerEndpoint.getContainerMisMatchInsights(limit, 0L, missingIn); + } + + private Response routeOpenKeys(Map<String, String> params, int limit, String startPrefix) { + boolean includeFso = parseBoolean(params.get("includeFso"), false); Review Comment: Added the missing keys to ReconConstants (e.g. RECON_QUERY_CONTAINER_STATE, RECON_QUERY_REPLICATION_TYPE, RECON_NAMESPACE_USAGE_*, etc.) and switched both LlmToolSpecFactory and ReconEndpointRouter to use those constants. ReconQueryExecutor now uses RECON_QUERY_LIMIT / RECON_QUERY_PREVKEY as well. A rename is now a single compile-time change in one place. -- 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]
