Carl-Zhou-CN commented on code in PR #9576:
URL: https://github.com/apache/seatunnel/pull/9576#discussion_r2347173757


##########
seatunnel-translation/seatunnel-translation-flink/seatunnel-translation-flink-20/src/main/java/org/apache/seatunnel/translation/flink/sink/FlinkSinkWriterContext.java:
##########
@@ -0,0 +1,200 @@
+/*
+ *  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.sink;
+
+import org.apache.seatunnel.api.common.metrics.MetricsContext;
+import org.apache.seatunnel.api.event.DefaultEventProcessor;
+import org.apache.seatunnel.api.event.EventListener;
+import org.apache.seatunnel.api.sink.SinkWriter;
+import org.apache.seatunnel.translation.flink.metric.FlinkMetricContext;
+
+import org.apache.flink.api.common.functions.RuntimeContext;
+import org.apache.flink.api.connector.sink2.WriterInitContext;
+import org.apache.flink.metrics.MetricGroup;
+import org.apache.flink.streaming.api.operators.StreamingRuntimeContext;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+@Slf4j
+public class FlinkSinkWriterContext implements SinkWriter.Context {
+
+    private final WriterInitContext initContext;
+    private final int parallelism;
+    private final EventListener eventListener;
+
+    public FlinkSinkWriterContext(WriterInitContext initContext, int 
parallelism) {
+        this.initContext = initContext;
+        this.parallelism = parallelism;
+        this.eventListener = new 
DefaultEventProcessor(getFlinkJobId(initContext));
+    }
+
+    @Override
+    public int getIndexOfSubtask() {
+        return initContext.getTaskInfo().getIndexOfThisSubtask();
+    }
+
+    @Override
+    public int getNumberOfParallelSubtasks() {
+        return parallelism;
+    }
+
+    @Override
+    public MetricsContext getMetricsContext() {
+        try {
+            RuntimeContext runtimeContext = getRuntimeContext();
+            MetricGroup metricGroup = initContext.metricGroup();
+
+            if (runtimeContext != null && metricGroup != null) {
+                return new FlinkMetricContext(runtimeContext, metricGroup);
+            } else {
+                return new FlinkMetricContext(metricGroup);
+            }
+        } catch (Exception e) {
+            return new FlinkMetricContext((MetricGroup) null);
+        }
+    }
+
+    @Override
+    public EventListener getEventListener() {
+        return eventListener;
+    }
+
+    public RuntimeContext getRuntimeContext() {
+        try {
+            RuntimeContext runtimeContext = tryGetFromFields(initContext);
+            if (runtimeContext != null) {
+                return runtimeContext;
+            }
+
+            runtimeContext = tryGetFromInitContextBase(initContext);
+            if (runtimeContext != null) {
+                return runtimeContext;
+            }
+
+            runtimeContext = tryGetFromWrapper(initContext);
+            if (runtimeContext != null) {
+                return runtimeContext;
+            }
+
+            return null;
+
+        } catch (Exception e) {
+            return null;
+        }
+    }
+
+    private RuntimeContext tryGetFromInitContextBase(Object context) {
+        try {
+            Class<?> initContextBaseClass =
+                    Class.forName(
+                            
"org.apache.flink.streaming.runtime.operators.sink.InitContextBase");
+            if (initContextBaseClass.isInstance(context)) {
+                Method getRuntimeContextMethod =
+                        
initContextBaseClass.getDeclaredMethod("getRuntimeContext");
+                getRuntimeContextMethod.setAccessible(true);
+                RuntimeContext runtimeContext =
+                        (RuntimeContext) 
getRuntimeContextMethod.invoke(context);
+                log.info(
+                        "Successfully obtained RuntimeContext from 
InitContextBase: {}",
+                        runtimeContext.getClass().getName());
+                return runtimeContext;
+            }
+        } catch (Exception e) {
+            log.debug("Failed to get RuntimeContext from InitContextBase", e);
+        }
+        return null;
+    }
+
+    private RuntimeContext tryGetFromWrapper(Object context) {
+        try {
+            String[] possibleFieldNames = {
+                "delegate", "wrapped", "context", "initContext", 
"writerInitContext"

Review Comment:
   I'm not quite clear about this. Could you explain it to me?



-- 
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...@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to