davidzollo commented on code in PR #9872: URL: https://github.com/apache/seatunnel/pull/9872#discussion_r2361906470
########## seatunnel-connectors-v2/connector-iotdb-v2/src/main/java/org/apache/seatunnel/connectors/seatunnel/iotdb/sink/IoTDBSinkClient.java: ########## @@ -0,0 +1,209 @@ +/* + * 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.seatunnel.connectors.seatunnel.iotdb.sink; + +import org.apache.seatunnel.common.exception.CommonErrorCodeDeprecated; +import org.apache.seatunnel.connectors.seatunnel.iotdb.config.SinkConfig; +import org.apache.seatunnel.connectors.seatunnel.iotdb.exception.IotdbConnectorErrorCode; +import org.apache.seatunnel.connectors.seatunnel.iotdb.exception.IotdbConnectorException; +import org.apache.seatunnel.connectors.seatunnel.iotdb.serialize.IoTDBRecord; + +import lombok.Getter; +import lombok.extern.slf4j.Slf4j; +import shaded.org.apache.iotdb.rpc.IoTDBConnectionException; +import shaded.org.apache.iotdb.rpc.StatementExecutionException; +import shaded.org.apache.iotdb.session.Session; +import shaded.org.apache.tsfile.enums.TSDataType; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +@Slf4j +public class IoTDBSinkClient { + + private final SinkConfig sinkConfig; + private final List<IoTDBRecord> batchList; + + private Session session; + private volatile boolean initialize; + private volatile Exception flushException; + + public IoTDBSinkClient(SinkConfig sinkConfig) { + this.sinkConfig = sinkConfig; + this.batchList = new ArrayList<>(); + } + + private void tryInit() throws IOException { + if (initialize) { + return; + } + + Session.Builder sessionBuilder = + new Session.Builder() + .nodeUrls(sinkConfig.getNodeUrls()) + .username(sinkConfig.getUsername()) + .password(sinkConfig.getPassword()); + if (sinkConfig.getThriftDefaultBufferSize() != null) { + sessionBuilder.thriftDefaultBufferSize(sinkConfig.getThriftDefaultBufferSize()); + } + if (sinkConfig.getThriftMaxFrameSize() != null) { + sessionBuilder.thriftMaxFrameSize(sinkConfig.getThriftMaxFrameSize()); + } + if (sinkConfig.getZoneId() != null) { + sessionBuilder.zoneId(sinkConfig.getZoneId()); + } + + session = sessionBuilder.build(); + try { + if (sinkConfig.getConnectionTimeoutInMs() != null) { + session.open( + sinkConfig.getEnableRPCCompression(), + sinkConfig.getConnectionTimeoutInMs()); + } else if (sinkConfig.getEnableRPCCompression() != null) { + session.open(sinkConfig.getEnableRPCCompression()); + } else { + session.open(); + } + } catch (IoTDBConnectionException e) { + log.error("Initialize IoTDB client failed.", e); + throw new IotdbConnectorException( + IotdbConnectorErrorCode.INITIALIZE_CLIENT_FAILED, + "Initialize IoTDB client failed.", + e); + } + initialize = true; + } + + public synchronized void write(IoTDBRecord record) throws IOException { + tryInit(); + checkFlushException(); + + batchList.add(record); + if (sinkConfig.getBatchSize() > 0 && batchList.size() >= sinkConfig.getBatchSize()) { + flush(); + } + } + + public synchronized void close() throws IOException { + try { + flush(); + } finally { + try { + if (session != null) { + session.close(); + } + } catch (IoTDBConnectionException e) { + log.error("Close IoTDB client failed.", e); + } + } + } + + synchronized void flush() throws IOException { + checkFlushException(); + if (batchList.isEmpty()) { + return; + } + + BatchRecords batchRecords = new BatchRecords(batchList); + for (int i = 0; i <= sinkConfig.getMaxRetries(); i++) { + try { + if (batchRecords.getTypesList().isEmpty()) { + session.insertRecords( + batchRecords.getDeviceIds(), + batchRecords.getTimestamps(), + batchRecords.getMeasurementsList(), + batchRecords.getStringValuesList()); + } else { + session.insertRecords( + batchRecords.getDeviceIds(), + batchRecords.getTimestamps(), Review Comment: Should there be a break in this for clause? -- 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]
