531651225 commented on code in PR #3174: URL: https://github.com/apache/incubator-seatunnel/pull/3174#discussion_r1016072129
########## seatunnel-connectors-v2/connector-influxdb/src/main/java/org/apache/seatunnel/connectors/seatunnel/influxdb/sink/InfluxDBSinkWriter.java: ########## @@ -0,0 +1,186 @@ +/* + * 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.influxdb.sink; + +import org.apache.seatunnel.api.table.type.SeaTunnelRow; +import org.apache.seatunnel.api.table.type.SeaTunnelRowType; +import org.apache.seatunnel.connectors.seatunnel.common.sink.AbstractSinkWriter; +import org.apache.seatunnel.connectors.seatunnel.influxdb.client.InfluxDBClient; +import org.apache.seatunnel.connectors.seatunnel.influxdb.config.SinkConfig; +import org.apache.seatunnel.connectors.seatunnel.influxdb.serialize.DefaultSerializer; +import org.apache.seatunnel.connectors.seatunnel.influxdb.serialize.Serializer; + +import org.apache.seatunnel.shade.com.typesafe.config.Config; + +import com.google.common.util.concurrent.ThreadFactoryBuilder; +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import org.influxdb.InfluxDB; +import org.influxdb.dto.BatchPoints; +import org.influxdb.dto.Point; + +import java.io.IOException; +import java.net.ConnectException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; + +@Slf4j +public class InfluxDBSinkWriter extends AbstractSinkWriter<SeaTunnelRow, Void> { + + private final Serializer serializer; + private InfluxDB influxDB; + private SinkConfig sinkConfig; + private final List<Point> batchList; + + private ScheduledExecutorService scheduler; + private ScheduledFuture<?> scheduledFuture; + private volatile boolean initialize; + private volatile Exception flushException; + private final Integer batchIntervalMs; + + public InfluxDBSinkWriter(Config pluginConfig, + SeaTunnelRowType seaTunnelRowType) throws ConnectException { + this.sinkConfig = SinkConfig.loadConfig(pluginConfig); + this.batchIntervalMs = sinkConfig.getBatchIntervalMs(); + this.serializer = new DefaultSerializer( + seaTunnelRowType, sinkConfig.getPrecision().getTimeUnit(), sinkConfig.getKeyTags(), sinkConfig.getKeyTime(), sinkConfig.getMeasurement()); + this.batchList = new ArrayList<>(); + + connect(); + } + + @Override + public void write(SeaTunnelRow element) throws IOException { + Point record = serializer.serialize(element); + write(record); + } + + @SneakyThrows + @Override + public Optional<Void> prepareCommit() { + // Flush to storage before snapshot state is performed + flush(); + return super.prepareCommit(); + } + + @Override + public void close() throws IOException { + if (scheduledFuture != null) { + scheduledFuture.cancel(false); + scheduler.shutdown(); + } + + flush(); + + if (influxDB != null) { + influxDB.close(); + influxDB = null; + } + } + + private void tryInit() throws IOException { + if (initialize) { + return; + } + initialize = true; + connect(); + if (batchIntervalMs != null) { + scheduler = Executors.newSingleThreadScheduledExecutor( + new ThreadFactoryBuilder().setNameFormat("influxDB-sink-output-%s").build()); + scheduledFuture = scheduler.scheduleAtFixedRate( + () -> { + try { + flush(); + } catch (IOException e) { + flushException = e; + } + }, + batchIntervalMs, + batchIntervalMs, + TimeUnit.MILLISECONDS); + } + } + + public synchronized void write(Point record) throws IOException { + tryInit(); Review Comment: > Why not build the scheduler thread in the constructor of InfluxDBSinkWriter thinks,i have fixed it. PTAL @ic4y -- 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]
