Caideyipi commented on code in PR #11706: URL: https://github.com/apache/iotdb/pull/11706#discussion_r1431196167
########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/connector/protocol/thrift/SyncClientManager.java: ########## @@ -0,0 +1,232 @@ +/* + * 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.iotdb.db.pipe.connector.protocol.thrift; + +import org.apache.iotdb.common.rpc.thrift.TEndPoint; +import org.apache.iotdb.commons.client.property.ThriftClientProperty; +import org.apache.iotdb.commons.conf.CommonDescriptor; +import org.apache.iotdb.commons.pipe.config.PipeConfig; +import org.apache.iotdb.db.pipe.connector.payload.evolvable.request.PipeTransferHandshakeReq; +import org.apache.iotdb.db.pipe.connector.protocol.thrift.sync.IoTDBThriftSyncConnectorClient; +import org.apache.iotdb.db.pipe.event.common.tablet.PipeInsertNodeTabletInsertionEvent; +import org.apache.iotdb.db.pipe.event.common.tablet.PipeRawTabletInsertionEvent; +import org.apache.iotdb.db.pipe.event.common.tsfile.PipeTsFileInsertionEvent; +import org.apache.iotdb.pipe.api.exception.PipeConnectionException; +import org.apache.iotdb.rpc.TSStatusCode; +import org.apache.iotdb.service.rpc.thrift.TPipeTransferResp; +import org.apache.iotdb.tsfile.utils.Pair; + +import org.apache.thrift.TException; +import org.apache.thrift.transport.TTransportException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +public class SyncClientManager { + private static final Logger LOGGER = LoggerFactory.getLogger(SyncClientManager.class); + + private static final PipeConfig PIPE_CONFIG = PipeConfig.getInstance(); + + private final boolean useSSL; + private final String trustStore; + private final String trustStorePwd; + private final boolean useLeaderCache; + + private final List<TEndPoint> endPoints; + private final Map<TEndPoint, Pair<IoTDBThriftSyncConnectorClient, Boolean>> endPoint2client = + new ConcurrentHashMap<>(); + + private final LeaderCacheManager leaderCacheManager = new LeaderCacheManager(); + + private long currentClientIndex = 0; + + public SyncClientManager( + List<TEndPoint> endPoints, + boolean useSSL, + String trustStore, + String trustStorePwd, + boolean useLeaderCache) { + this.useSSL = useSSL; + this.trustStore = trustStore; + this.trustStorePwd = trustStorePwd; + this.useLeaderCache = useLeaderCache; + this.endPoints = endPoints; + + for (TEndPoint endPoint : endPoints) { + endPoint2client.put(endPoint, new Pair<>(null, false)); + } + } + + public void initClients() throws IOException, TTransportException { + // init clients + for (TEndPoint endPoint : endPoints) { + if (Boolean.TRUE.equals(endPoint2client.get(endPoint).getRight())) { + continue; + } + + initAndSetClient(endPoint); + } + // check whether any clients are available + for (TEndPoint nodeUrl : endPoint2client.keySet()) { + if (Boolean.TRUE.equals(endPoint2client.get(nodeUrl).getRight())) { + return; + } + } + + throw new PipeConnectionException( + String.format("All target servers %s are not available.", endPoint2client.keySet())); + } + + public Pair<IoTDBThriftSyncConnectorClient, Boolean> getOneConnectedClientAndStatus() { + final int clientSize = endPoints.size(); + // Round-robin, find the next alive client + for (int tryCount = 0; tryCount < clientSize; ++tryCount) { + final int clientIndex = (int) (currentClientIndex++ % clientSize); + Pair<IoTDBThriftSyncConnectorClient, Boolean> clientAndStatus = + endPoint2client.get(endPoints.get(clientIndex)); + if (Boolean.TRUE.equals(clientAndStatus.getRight())) { + return clientAndStatus; + } + } + throw new PipeConnectionException( + "All clients are dead, please check the connection to the receiver."); + } + + public Pair<IoTDBThriftSyncConnectorClient, Boolean> getClientAndStatusByEvent( + PipeInsertNodeTabletInsertionEvent event) { + TEndPoint endPoint = leaderCacheManager.parseEndPoint(event); + return endPoint == null + || !useLeaderCache + || !endPoint2client.containsKey(endPoint) + || Boolean.FALSE.equals(endPoint2client.get(endPoint).getRight()) + ? getOneConnectedClientAndStatus() + : endPoint2client.get(endPoint); + } + + public Pair<IoTDBThriftSyncConnectorClient, Boolean> getClientAndStatusByEvent( Review Comment: Functions alike with parameters having the same parent class can be extracted ########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/connector/protocol/thrift/sync/IoTDBThriftSyncConnector.java: ########## @@ -464,9 +429,21 @@ private void doTransfer( } // 2. Transfer file seal signal, which means the file is transferred completely - final TPipeTransferResp resp = - client.pipeTransfer( - PipeTransferFileSealReq.toTPipeTransferReq(tsFile.getName(), tsFile.length())); + final TPipeTransferResp resp; Review Comment: May these part of code be extracted to a single method? -- 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]
