gong commented on code in PR #7059: URL: https://github.com/apache/inlong/pull/7059#discussion_r1058093963
########## inlong-sort/sort-connectors/kudu/src/main/java/org/apache/inlong/sort/kudu/sink/AbstractKuduSinkFunction.java: ########## @@ -0,0 +1,206 @@ +/* + * 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.inlong.sort.kudu.sink; + +import org.apache.flink.annotation.PublicEvolving; +import org.apache.flink.api.common.state.ListState; +import org.apache.flink.api.common.state.ListStateDescriptor; +import org.apache.flink.api.common.typeinfo.TypeHint; +import org.apache.flink.api.common.typeinfo.TypeInformation; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.runtime.state.FunctionInitializationContext; +import org.apache.flink.runtime.state.FunctionSnapshotContext; +import org.apache.flink.streaming.api.checkpoint.CheckpointedFunction; +import org.apache.flink.streaming.api.functions.sink.RichSinkFunction; +import org.apache.flink.table.api.TableSchema; +import org.apache.flink.table.data.RowData; +import org.apache.inlong.sort.base.metric.MetricOption; +import org.apache.inlong.sort.base.metric.MetricState; +import org.apache.inlong.sort.base.metric.SinkMetricData; +import org.apache.inlong.sort.base.util.MetricStateUtils; +import org.apache.kudu.client.SessionConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; + +import static org.apache.inlong.sort.base.Constants.*; +import static org.apache.inlong.sort.kudu.common.KuduOptions.*; + +/** + * The base for all kudu sinks. + */ +@PublicEvolving +public abstract class AbstractKuduSinkFunction + extends + RichSinkFunction<RowData> + implements + CheckpointedFunction { + + private static final long serialVersionUID = 1L; + + private static final Logger LOG = LoggerFactory.getLogger(AbstractKuduSinkFunction.class); + + /** + * The masters of kudu server. + */ + protected final String masters; + /** + * The name of kudu table. + */ + protected final String tableName; + /** + * The flink TableSchema. + */ + protected final TableSchema flinkTableSchema; + + protected String connectorMetricIdentify; + + /** + * The configuration of kudu sinkFunction. + */ + protected final Configuration configuration; + + /** + * The maximum number of buffered records. + */ + protected final int maxBufferSize; + + /** + * The maximum number of retries. + */ + protected final int maxRetries; + + /** + * True if the sink is running. + */ + protected volatile boolean running; + + /** + * The exception thrown in asynchronous tasks. + */ + private transient Throwable flushThrowable; + + protected final SessionConfiguration.FlushMode flushMode; + /** + * whether load metadata in `open` function + */ + protected boolean lazyLoadSchema; + + private SinkMetricData sinkMetricData; + + private transient ListState<MetricState> metricStateListState; + private transient MetricState metricState; + + private final String auditHostAndPorts; + + private final String inlongMetric; + + public AbstractKuduSinkFunction( + TableSchema flinkTableSchema, + String masters, + String tableName, + SessionConfiguration.FlushMode flushMode, + Configuration configuration, + String inlongMetric, + String auditHostAndPorts) { + this.masters = masters; + this.flushMode = flushMode; + this.tableName = tableName; + this.flinkTableSchema = flinkTableSchema; + this.configuration = configuration; + this.maxRetries = configuration.getInteger(MAX_RETRIES); + this.maxBufferSize = configuration.getInteger(MAX_BUFFER_SIZE); + this.lazyLoadSchema = configuration.getBoolean(LAZY_LOAD_SCHEMA); + this.inlongMetric = inlongMetric; + this.auditHostAndPorts = auditHostAndPorts; + MetricOption metricOption = MetricOption.builder() + .withInlongLabels(inlongMetric) + .withInlongAudit(auditHostAndPorts) + .withInitRecords(metricState != null ? metricState.getMetricValue(NUM_RECORDS_OUT) : 0L) + .withInitBytes(metricState != null ? metricState.getMetricValue(NUM_BYTES_OUT) : 0L) + .withInitDirtyRecords(metricState != null ? metricState.getMetricValue(DIRTY_RECORDS_OUT) : 0L) + .withInitDirtyBytes(metricState != null ? metricState.getMetricValue(DIRTY_BYTES_OUT) : 0L) + .withRegisterMetric(MetricOption.RegisteredMetric.ALL) + .build(); + if (metricOption != null) { + sinkMetricData = new SinkMetricData(metricOption, getRuntimeContext().getMetricGroup()); + } + } + + @Override + public void open(Configuration parameters) throws Exception { + this.running = true; + } + + @Override + public void invoke( + RowData row, + Context context) throws Exception { + addBatch(row); + sendMetrics(row.toString().getBytes()); + } + + /** + * Adds a record in the buffer. + */ + protected abstract void addBatch(RowData in) throws Exception; + + @Override + public void close() throws Exception { + this.running = false; + } + + @Override + public void snapshotState(FunctionSnapshotContext functionSnapshotContext) { + checkError(); + // We must store the exception caught in the flushing so that the + // task thread can be aware of the failure. + try { + flush(); + } catch (IOException e) { + this.flushThrowable = e; + } + checkError(); + } Review Comment: lost metric snapshot call -- 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: commits-unsubscr...@inlong.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org