Hisoka-X commented on code in PR #9576:
URL: https://github.com/apache/seatunnel/pull/9576#discussion_r2386043397


##########
seatunnel-core/seatunnel-flink-starter/seatunnel-flink-20-starter/src/main/java/org/apache/seatunnel/core/starter/flink/execution/FlinkExecution.java:
##########
@@ -0,0 +1,53 @@
+/*
+ * 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.core.starter.flink.execution;
+
+import org.apache.seatunnel.shade.com.typesafe.config.Config;
+
+import org.apache.seatunnel.translation.flink.metric.FlinkJobMetricsSummary;
+
+import org.apache.flink.api.common.JobExecutionResult;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Flink execution implementation for Flink 1.20. */
+public class FlinkExecution extends AbstractFlinkExecution {
+
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(FlinkExecution.class);
+
+    public FlinkExecution(Config config) {
+        super(config);
+    }
+
+    @Override
+    protected FlinkJobMetricsSummary createJobMetricsSummary(

Review Comment:
   After remove logs, I found this method same as method in common module. We 
shoud do some refactor.



##########
seatunnel-translation/seatunnel-translation-flink/seatunnel-translation-flink-20/src/main/java/org/apache/seatunnel/translation/flink/metric/FlinkAccumulatorCounter.java:
##########
@@ -0,0 +1,140 @@
+/*
+ *  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.translation.flink.metric;
+
+import org.apache.seatunnel.api.common.metrics.Counter;
+import org.apache.seatunnel.api.common.metrics.MetricNames;
+import org.apache.seatunnel.api.common.metrics.Unit;
+
+import org.apache.flink.api.common.accumulators.LongCounter;
+import org.apache.flink.api.common.functions.RuntimeContext;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class FlinkAccumulatorCounter implements Counter {
+    private final String name;
+    private final org.apache.flink.metrics.Counter flinkCounter;
+    private final LongCounter accumulator;
+    private final RuntimeContext runtimeContext;
+    private final java.util.concurrent.atomic.AtomicLong localCount =
+            new java.util.concurrent.atomic.AtomicLong(0L);
+
+    public FlinkAccumulatorCounter(
+            String name,
+            org.apache.flink.metrics.Counter flinkCounter,
+            RuntimeContext runtimeContext) {
+        this.name = name;
+        this.flinkCounter = flinkCounter;
+        this.runtimeContext = runtimeContext;
+        this.accumulator = new LongCounter();
+
+        try {
+            String accumulatorName = getStandardAccumulatorName(name);
+            runtimeContext.addAccumulator(accumulatorName, accumulator);
+        } catch (Exception e) {
+            log.warn("Failed to register accumulator: {}", name);
+        }
+    }
+
+    @Override
+    public void inc() {
+        inc(1L);
+    }
+
+    @Override
+    public void inc(long n) {
+        if (flinkCounter != null) {
+            flinkCounter.inc(n);
+        }
+        accumulator.add(n);
+        localCount.addAndGet(n);
+    }
+
+    @Override
+    public void dec() {
+        dec(1L);
+    }
+
+    @Override
+    public void dec(long n) {
+        if (flinkCounter != null) {
+            flinkCounter.inc(-n);
+        }
+        accumulator.add(-n);
+        localCount.addAndGet(-n);
+    }
+
+    @Override
+    public void set(long n) {
+        long current = localCount.get();
+        long diff = n - current;
+        if (flinkCounter != null) {
+            flinkCounter.inc(diff);
+        }
+        accumulator.add(diff);
+        localCount.set(n);
+    }
+
+    @Override
+    public long getCount() {
+        long accumulatorValue = accumulator.getLocalValue();
+        if (accumulatorValue != localCount.get()) {
+            localCount.set(accumulatorValue);
+        }
+        return accumulatorValue;
+    }
+
+    @Override
+    public String name() {
+        return name;
+    }
+
+    @Override
+    public Unit unit() {
+        return Unit.COUNT;
+    }
+
+    public LongCounter getAccumulator() {
+        return accumulator;
+    }
+
+    public void sync() {
+        long accumulatorValue = accumulator.getLocalValue();
+        if (accumulatorValue != localCount.get()) {
+            localCount.set(accumulatorValue);
+        }
+    }
+
+    private String getStandardAccumulatorName(String originalName) {
+        if (originalName.contains("SinkWriteCount")

Review Comment:
   same as https://github.com/apache/seatunnel/pull/9576#discussion_r2382410956



##########
seatunnel-translation/seatunnel-translation-flink/seatunnel-translation-flink-20/src/main/java/org/apache/seatunnel/translation/flink/metric/FlinkAccumulatorCounter.java:
##########
@@ -0,0 +1,140 @@
+/*
+ *  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.translation.flink.metric;
+
+import org.apache.seatunnel.api.common.metrics.Counter;
+import org.apache.seatunnel.api.common.metrics.MetricNames;
+import org.apache.seatunnel.api.common.metrics.Unit;
+
+import org.apache.flink.api.common.accumulators.LongCounter;
+import org.apache.flink.api.common.functions.RuntimeContext;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class FlinkAccumulatorCounter implements Counter {
+    private final String name;
+    private final org.apache.flink.metrics.Counter flinkCounter;
+    private final LongCounter accumulator;
+    private final RuntimeContext runtimeContext;
+    private final java.util.concurrent.atomic.AtomicLong localCount =
+            new java.util.concurrent.atomic.AtomicLong(0L);

Review Comment:
   why we need two fields to store value?



##########
seatunnel-translation/seatunnel-translation-flink/seatunnel-translation-flink-20/src/main/java/org/apache/seatunnel/translation/flink/metric/FlinkAccumulatorCounter.java:
##########
@@ -0,0 +1,140 @@
+/*
+ *  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.translation.flink.metric;
+
+import org.apache.seatunnel.api.common.metrics.Counter;
+import org.apache.seatunnel.api.common.metrics.MetricNames;
+import org.apache.seatunnel.api.common.metrics.Unit;
+
+import org.apache.flink.api.common.accumulators.LongCounter;
+import org.apache.flink.api.common.functions.RuntimeContext;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class FlinkAccumulatorCounter implements Counter {
+    private final String name;
+    private final org.apache.flink.metrics.Counter flinkCounter;
+    private final LongCounter accumulator;
+    private final RuntimeContext runtimeContext;
+    private final java.util.concurrent.atomic.AtomicLong localCount =
+            new java.util.concurrent.atomic.AtomicLong(0L);
+
+    public FlinkAccumulatorCounter(
+            String name,
+            org.apache.flink.metrics.Counter flinkCounter,
+            RuntimeContext runtimeContext) {
+        this.name = name;
+        this.flinkCounter = flinkCounter;
+        this.runtimeContext = runtimeContext;
+        this.accumulator = new LongCounter();
+
+        try {
+            String accumulatorName = getStandardAccumulatorName(name);
+            runtimeContext.addAccumulator(accumulatorName, accumulator);
+        } catch (Exception e) {
+            log.warn("Failed to register accumulator: {}", name);
+        }
+    }
+
+    @Override
+    public void inc() {
+        inc(1L);
+    }
+
+    @Override
+    public void inc(long n) {
+        if (flinkCounter != null) {
+            flinkCounter.inc(n);
+        }
+        accumulator.add(n);
+        localCount.addAndGet(n);
+    }
+
+    @Override
+    public void dec() {
+        dec(1L);
+    }
+
+    @Override
+    public void dec(long n) {
+        if (flinkCounter != null) {
+            flinkCounter.inc(-n);
+        }
+        accumulator.add(-n);
+        localCount.addAndGet(-n);
+    }
+
+    @Override
+    public void set(long n) {
+        long current = localCount.get();
+        long diff = n - current;
+        if (flinkCounter != null) {
+            flinkCounter.inc(diff);
+        }
+        accumulator.add(diff);
+        localCount.set(n);
+    }
+
+    @Override
+    public long getCount() {
+        long accumulatorValue = accumulator.getLocalValue();
+        if (accumulatorValue != localCount.get()) {
+            localCount.set(accumulatorValue);
+        }
+        return accumulatorValue;
+    }
+
+    @Override
+    public String name() {
+        return name;
+    }
+
+    @Override
+    public Unit unit() {
+        return Unit.COUNT;
+    }
+
+    public LongCounter getAccumulator() {
+        return accumulator;
+    }
+
+    public void sync() {

Review Comment:
   what's for?



##########
seatunnel-translation/seatunnel-translation-flink/seatunnel-translation-flink-20/src/main/java/org/apache/seatunnel/translation/flink/metric/FlinkMetricContext.java:
##########
@@ -0,0 +1,150 @@
+/*
+ *  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.translation.flink.metric;
+
+import org.apache.seatunnel.api.common.metrics.Counter;
+import org.apache.seatunnel.api.common.metrics.Meter;
+import org.apache.seatunnel.api.common.metrics.MetricNames;
+import org.apache.seatunnel.api.common.metrics.MetricsContext;
+
+import org.apache.flink.api.common.functions.RuntimeContext;
+import org.apache.flink.metrics.MetricGroup;
+import org.apache.flink.streaming.api.operators.StreamingRuntimeContext;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+@Slf4j
+public class FlinkMetricContext implements MetricsContext {
+
+    private final MetricGroup metricGroup;
+    private final StreamingRuntimeContext runtimeContext;
+    private final RuntimeContext generalRuntimeContext;
+    private final Map<String, Counter> counters = new ConcurrentHashMap<>();
+    private final Map<String, Meter> meters = new ConcurrentHashMap<>();
+
+    public FlinkMetricContext(StreamingRuntimeContext runtimeContext) {
+        this.runtimeContext = runtimeContext;
+        this.generalRuntimeContext = runtimeContext;
+        this.metricGroup = runtimeContext != null ? 
runtimeContext.getMetricGroup() : null;
+    }
+
+    public FlinkMetricContext(RuntimeContext runtimeContext, MetricGroup 
metricGroup) {
+        this.runtimeContext =
+                runtimeContext instanceof StreamingRuntimeContext
+                        ? (StreamingRuntimeContext) runtimeContext
+                        : null;
+        this.generalRuntimeContext = runtimeContext;
+        this.metricGroup = metricGroup;
+    }
+
+    public FlinkMetricContext(MetricGroup metricGroup) {
+        this.metricGroup = metricGroup;
+        this.generalRuntimeContext = null;
+        this.runtimeContext = null;
+    }
+
+    @Override
+    public Counter counter(String name) {
+        Counter existingCounter = counters.get(name);
+        if (existingCounter != null) {
+            return existingCounter;
+        }
+
+        if (metricGroup == null) {
+            Counter noOpCounter = new NoOpCounter();

Review Comment:
   It's werid. Why other flink version does not need this?



-- 
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]

Reply via email to