featzhang commented on code in PR #27385: URL: https://github.com/apache/flink/pull/27385#discussion_r2757930850
########## flink-models/flink-model-triton/src/main/java/org/apache/flink/model/triton/TritonInferenceModelFunction.java: ########## @@ -0,0 +1,472 @@ +/* + * 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.flink.model.triton; + +import org.apache.flink.configuration.ReadableConfig; +import org.apache.flink.model.triton.exception.TritonClientException; +import org.apache.flink.model.triton.exception.TritonNetworkException; +import org.apache.flink.model.triton.exception.TritonSchemaException; +import org.apache.flink.model.triton.exception.TritonServerException; +import org.apache.flink.table.catalog.Column; +import org.apache.flink.table.data.GenericRowData; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.data.binary.BinaryStringData; +import org.apache.flink.table.factories.ModelProviderFactory; +import org.apache.flink.table.functions.AsyncPredictFunction; +import org.apache.flink.table.types.logical.ArrayType; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.VarCharType; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import okhttp3.Call; +import okhttp3.Callback; +import okhttp3.MediaType; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.concurrent.CompletableFuture; +import java.util.zip.GZIPOutputStream; + +/** + * {@link AsyncPredictFunction} for Triton Inference Server generic inference task. + * + * <p><b>Request Model (v1):</b> This implementation processes records one-by-one. Each {@link + * #asyncPredict(RowData)} call triggers one HTTP request to Triton server. There is no Flink-side + * mini-batch aggregation in the current version. + * + * <p><b>Batch Efficiency:</b> Inference throughput benefits from: + * + * <ul> + * <li><b>Triton Dynamic Batching</b>: Configure {@code dynamic_batching} in model's {@code + * config.pbtxt} to aggregate concurrent requests server-side + * <li><b>Flink Parallelism</b>: High parallelism naturally creates concurrent requests that + * Triton can batch together + * <li><b>AsyncDataStream Capacity</b>: Buffer size controls concurrent in-flight requests, + * increasing opportunities for server-side batching + * </ul> + * + * <p><b>Future Roadmap (v2+):</b> Flink-side mini-batch aggregation will be added to reduce HTTP + * overhead (configurable via {@code batch-size} and {@code batch-timeout} options). + * + * @see <a + * href="https://github.com/triton-inference-server/server/blob/main/docs/user_guide/model_configuration.md#dynamic-batcher">Triton + * Dynamic Batching Documentation</a> + */ +public class TritonInferenceModelFunction extends AbstractTritonModelFunction { + private static final long serialVersionUID = 1L; + private static final Logger LOG = LoggerFactory.getLogger(TritonInferenceModelFunction.class); + + private static final MediaType JSON_MEDIA_TYPE = + MediaType.get("application/json; charset=utf-8"); + private static final ObjectMapper objectMapper = new ObjectMapper(); + + /** Thread-local ByteArrayOutputStream for gzip compression to avoid repeated allocations. */ + private static final ThreadLocal<ByteArrayOutputStream> BAOS_HOLDER = Review Comment: Thanks for the suggestion. Updated to use an instance variable `ByteArrayOutputStream` as suggested. -- 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]
