github-actions[bot] commented on code in PR #62880: URL: https://github.com/apache/doris/pull/62880#discussion_r3148498060
########## fe/be-java-extensions/max-compute-connector/src/main/java/org/apache/doris/maxcompute/MaxComputeFeClient.java: ########## @@ -0,0 +1,327 @@ +// 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.doris.maxcompute; + +import org.apache.doris.thrift.FrontendService; +import org.apache.doris.thrift.TMaxComputeBlockIdRequest; +import org.apache.doris.thrift.TMaxComputeBlockIdResult; +import org.apache.doris.thrift.TNetworkAddress; +import org.apache.doris.thrift.TStatus; +import org.apache.doris.thrift.TStatusCode; + +import org.apache.log4j.Logger; +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.transport.TSocket; +import org.apache.thrift.transport.TTransport; +import org.apache.thrift.transport.TTransportException; +import org.apache.thrift.transport.layered.TFramedTransport; + +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +/** + * FE thrift client used by MaxCompute writer runtime code in BE's embedded JVM. + */ +class MaxComputeFeClient implements AutoCloseable { + static final String FE_HOST = "fe_host"; + static final String FE_PORT = "fe_port"; + static final String FE_RPC_TIMEOUT_MS = "fe_rpc_timeout_ms"; + static final String FE_THRIFT_SERVER_TYPE = "fe_thrift_server_type"; + + private static final Logger LOG = Logger.getLogger(MaxComputeFeClient.class); + private static final int FETCH_BLOCK_ID_MAX_RETRY_TIMES = 3; + private static final long FETCH_BLOCK_ID_RETRY_SLEEP_MS = 10L; + private static final long FETCH_BLOCK_ID_LENGTH = 1L; + private static final int DEFAULT_FE_RPC_TIMEOUT_MS = 60000; + private static final String THREADED_SELECTOR = "THREADED_SELECTOR"; + private static final String THREAD_POOL = "THREAD_POOL"; + + private final int rpcTimeoutMs; + private final String thriftServerType; + private final RpcExecutor rpcExecutor; + private final long retrySleepMs; + private TNetworkAddress masterAddress; + + static MaxComputeFeClient create(Map<String, String> params) { + String host = requireParam(params, FE_HOST); + int port = Integer.parseInt(requireParam(params, FE_PORT)); + int timeoutMs = Integer.parseInt(params.getOrDefault(FE_RPC_TIMEOUT_MS, + String.valueOf(DEFAULT_FE_RPC_TIMEOUT_MS))); + String serverType = params.getOrDefault(FE_THRIFT_SERVER_TYPE, THREAD_POOL); + return new MaxComputeFeClient(new TNetworkAddress(host, port), timeoutMs, serverType); + } + + MaxComputeFeClient(TNetworkAddress masterAddress, int rpcTimeoutMs, String thriftServerType) { + this(masterAddress, rpcTimeoutMs, thriftServerType, new ReusableRpcExecutor(), + FETCH_BLOCK_ID_RETRY_SLEEP_MS); + } + + MaxComputeFeClient(TNetworkAddress masterAddress, int rpcTimeoutMs, String thriftServerType, + RpcExecutor rpcExecutor, long retrySleepMs) { + this.masterAddress = copyAddress(Objects.requireNonNull(masterAddress, "masterAddress")); + this.rpcTimeoutMs = rpcTimeoutMs; + this.thriftServerType = thriftServerType == null ? THREAD_POOL : thriftServerType; + this.rpcExecutor = Objects.requireNonNull(rpcExecutor, "rpcExecutor"); + this.retrySleepMs = retrySleepMs; + } + + long requestBlockId(long txnId, String writeSessionId) throws IOException { + if (txnId <= 0) { + throw new IOException("invalid MaxCompute txn_id for block_id allocation: " + txnId); + } + if (writeSessionId == null || writeSessionId.isEmpty()) { + throw new IOException("empty MaxCompute write_session_id for block_id allocation"); + } + + TMaxComputeBlockIdRequest request = buildBlockIdRequest(txnId, writeSessionId); + return callWithMasterRedirect( + "allocate MaxCompute block_id", + client -> client.getMaxComputeBlockIdRange(request), + (result, requestAddress, retryTimes) -> + handleBlockIdResult(result, requestAddress, retryTimes, txnId, writeSessionId)); + } + + @Override + public synchronized void close() { + rpcExecutor.close(); + } + + private synchronized <T, R> R callWithMasterRedirect(String operation, FeCall<T> call, + ResponseHandler<T, R> handler) + throws IOException { + validateAddress(masterAddress); + + Exception lastException = null; + for (int retryTimes = 0; retryTimes < FETCH_BLOCK_ID_MAX_RETRY_TIMES; retryTimes++) { + TNetworkAddress requestAddress = copyAddress(masterAddress); Review Comment: Before this refactor, each block-id allocation started from `ExecEnv::GetInstance()->cluster_info()->master_fe_addr`, so the next allocation could follow BE heartbeat updates after a master crash. Here the request address always comes from the client's cached `masterAddress`, which only changes after a successful `NOT_MASTER` response. If the old master dies before it can return `NOT_MASTER`, BE can learn the new leader, but this writer never re-reads it and keeps retrying the dead endpoint for the rest of the query. Can we refresh the baseline FE address between independent `requestBlockId()` calls (or on RPC failures) instead of pinning the whole writer lifetime to the first address? -- 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]
