You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@gluten.apache.org by lg...@apache.org on 2026/01/14 08:24:45 UTC
(incubator-gluten) branch main updated: [GLUTEN-11364][FLINK] Improve the job graph generation code (#11365)
This is an automated email from the ASF dual-hosted git repository.
lgbo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-gluten.git
The following commit(s) were added to refs/heads/main by this push:
new d9e04ef030 [GLUTEN-11364][FLINK] Improve the job graph generation code (#11365)
d9e04ef030 is described below
commit d9e04ef030d34005d7e8b99c1d356267b39ed06a
Author: lgbo <lg...@gmail.com>
AuthorDate: Wed Jan 14 16:24:40 2026 +0800
[GLUTEN-11364][FLINK] Improve the job graph generation code (#11365)
* refactor stream graph translator
* nexmark debug
* resolve the ownership of RowVector
* fix some typos
* Refactor OffloadedJobGraphGenerator to check upstream/downstream operators from JobGraph
- Modify couldOutputRowVector to check if downstream vertex's operator is gluten operator when outputs is empty
- Modify couldInputRowVector to check if upstream vertex's last operator is gluten operator when inputs is empty
* Refactor: improve naming and code structure
* Fix code according to review comments
---
.../plan/nodes/exec/common/CommonExecSink.java | 14 +-
.../plan/nodes/exec/stream/StreamExecCalc.java | 12 +-
.../nodes/exec/stream/StreamExecDeduplicate.java | 9 +-
.../plan/nodes/exec/stream/StreamExecExchange.java | 12 +-
.../stream/StreamExecGlobalWindowAggregate.java | 9 +-
.../exec/stream/StreamExecGroupAggregate.java | 9 +-
.../stream/StreamExecGroupWindowAggregate.java | 10 +-
.../plan/nodes/exec/stream/StreamExecJoin.java | 9 +-
.../stream/StreamExecLocalWindowAggregate.java | 9 +-
.../plan/nodes/exec/stream/StreamExecRank.java | 9 +-
.../exec/stream/StreamExecWatermarkAssigner.java | 9 +-
.../exec/stream/StreamExecWindowAggregate.java | 11 +-
.../nodes/exec/stream/StreamExecWindowJoin.java | 9 +-
.../apache/gluten/velox/FileSystemSinkFactory.java | 5 +-
.../gluten/velox/FromElementsSourceFactory.java | 8 +-
.../gluten/velox/FuzzerSourceSinkFactory.java | 16 +-
.../gluten/velox/KafkaSourceSinkFactory.java | 8 +-
.../apache/gluten/velox/NexmarkSourceFactory.java | 12 +-
.../org/apache/gluten/velox/PrintSinkFactory.java | 9 +-
.../apache/flink/client/StreamGraphTranslator.java | 187 +---
.../streaming/runtime/tasks/OperatorChain.java | 944 ---------------------
.../gluten/client/OffloadedJobGraphGenerator.java | 605 +++++++++++++
.../apache/gluten/client/OperatorChainSlice.java | 65 ++
.../gluten/client/OperatorChainSliceGraph.java | 82 ++
.../client/OperatorChainSliceGraphGenerator.java | 145 ++++
.../streaming/api/operators/GlutenOperator.java | 4 +
.../api/operators/GlutenStreamSource.java | 19 +-
.../runtime/operators/GlutenOneInputOperator.java | 197 +++--
.../runtime/operators/GlutenSessionResource.java | 65 ++
.../runtime/operators/GlutenSourceFunction.java | 200 ++++-
...utOperator.java => GlutenTwoInputOperator.java} | 185 ++--
.../operators/GlutenVectorOneInputOperator.java | 203 -----
.../operators/GlutenVectorSourceFunction.java | 185 ----
...er.java => GlutenStatefulRecordSerializer.java} | 33 +-
.../main/java/org/apache/gluten/util/Utils.java | 82 +-
.../org/apache/gluten/util/VectorInputBridge.java | 115 +++
.../org/apache/gluten/util/VectorOutputBridge.java | 137 +++
.../gluten/vectorized/ArrowVectorWriter.java | 20 +-
.../api/operators/GlutenStreamFilterTest.java | 4 +-
.../operators/GlutenStreamJoinOperatorTest.java | 12 +-
.../GlutenStreamJoinOperatorTestBase.java | 14 +-
.../operators/GlutenStreamOperatorTestBase.java | 4 +-
42 files changed, 1876 insertions(+), 1820 deletions(-)
diff --git a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/common/CommonExecSink.java b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/common/CommonExecSink.java
index 8dbac05783..fab420c481 100644
--- a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/common/CommonExecSink.java
+++ b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/common/CommonExecSink.java
@@ -16,9 +16,6 @@
*/
package org.apache.flink.table.planner.plan.nodes.exec.common;
-import org.apache.gluten.table.runtime.operators.GlutenOneInputOperator;
-import org.apache.gluten.util.LogicalTypeConverter;
-import org.apache.gluten.util.PlanNodeIdGenerator;
import org.apache.gluten.velox.VeloxSourceSinkFactory;
import org.apache.flink.api.common.io.OutputFormat;
@@ -73,6 +70,7 @@ import org.apache.flink.table.runtime.keyselector.RowDataKeySelector;
import org.apache.flink.table.runtime.operators.sink.ConstraintEnforcer;
import org.apache.flink.table.runtime.operators.sink.RowKindSetter;
import org.apache.flink.table.runtime.operators.sink.SinkOperator;
+import org.apache.flink.table.runtime.operators.sink.StreamRecordTimestampInserter;
import org.apache.flink.table.runtime.typeutils.InternalTypeInfo;
import org.apache.flink.table.types.logical.BinaryType;
import org.apache.flink.table.types.logical.CharType;
@@ -558,11 +556,6 @@ public abstract class CommonExecSink extends ExecNodeBase<Object>
if (rowtimeFieldIndex == -1) {
return inputTransform;
}
- // --- Begin Gluten-specific code changes ---
- io.github.zhztheplayer.velox4j.type.RowType outputType =
- (io.github.zhztheplayer.velox4j.type.RowType)
- LogicalTypeConverter.toVLType(
- ((InternalTypeInfo) inputTransform.getOutputType()).toLogicalType());
return ExecNodeUtil.createOneInputTransformation(
inputTransform,
createTransformationMeta(
@@ -570,13 +563,10 @@ public abstract class CommonExecSink extends ExecNodeBase<Object>
String.format("StreamRecordTimestampInserter(rowtime field: %s)", rowtimeFieldIndex),
"StreamRecordTimestampInserter",
config),
- // TODO: support it, Map.of() will not be used, hardcode it here.
- new GlutenOneInputOperator(
- null, PlanNodeIdGenerator.newId(), null, Map.of("1", outputType)),
+ new StreamRecordTimestampInserter(rowtimeFieldIndex),
inputTransform.getOutputType(),
sinkParallelism,
sinkParallelismConfigured);
- // --- End Gluten-specific code changes ---
}
private InternalTypeInfo<RowData> getInputTypeInfo() {
diff --git a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecCalc.java b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecCalc.java
index bc98c5154c..1062f3c08e 100644
--- a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecCalc.java
+++ b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecCalc.java
@@ -19,7 +19,7 @@ package org.apache.flink.table.planner.plan.nodes.exec.stream;
import org.apache.gluten.rexnode.RexConversionContext;
import org.apache.gluten.rexnode.RexNodeConverter;
import org.apache.gluten.rexnode.Utils;
-import org.apache.gluten.table.runtime.operators.GlutenVectorOneInputOperator;
+import org.apache.gluten.table.runtime.operators.GlutenOneInputOperator;
import org.apache.gluten.util.LogicalTypeConverter;
import org.apache.gluten.util.PlanNodeIdGenerator;
@@ -53,6 +53,8 @@ import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonCre
import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.calcite.rex.RexNode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
@@ -68,6 +70,7 @@ import java.util.Map;
minPlanVersion = FlinkVersion.v1_15,
minStateVersion = FlinkVersion.v1_15)
public class StreamExecCalc extends CommonExecCalc implements StreamExecNode<RowData> {
+ private static final Logger LOG = LoggerFactory.getLogger(StreamExecCalc.class);
public StreamExecCalc(
ReadableConfig tableConfig,
@@ -142,11 +145,14 @@ public class StreamExecCalc extends CommonExecCalc implements StreamExecNode<Row
(io.github.zhztheplayer.velox4j.type.RowType)
LogicalTypeConverter.toVLType(getOutputType());
final OneInputStreamOperator calOperator =
- new GlutenVectorOneInputOperator(
+ new GlutenOneInputOperator(
new StatefulPlanNode(project.getId(), project),
PlanNodeIdGenerator.newId(),
inputType,
- Map.of(project.getId(), outputType));
+ Map.of(project.getId(), outputType),
+ RowData.class,
+ RowData.class,
+ "StreamExecCalc");
return ExecNodeUtil.createOneInputTransformation(
inputTransform,
new TransformationMetadata("gluten-calc", "Gluten cal operator"),
diff --git a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecDeduplicate.java b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecDeduplicate.java
index ed89b71774..899a98365e 100644
--- a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecDeduplicate.java
+++ b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecDeduplicate.java
@@ -16,7 +16,7 @@
*/
package org.apache.flink.table.planner.plan.nodes.exec.stream;
-import org.apache.gluten.table.runtime.operators.GlutenVectorOneInputOperator;
+import org.apache.gluten.table.runtime.operators.GlutenOneInputOperator;
import org.apache.gluten.util.LogicalTypeConverter;
import org.apache.gluten.util.PlanNodeIdGenerator;
@@ -212,11 +212,14 @@ public class StreamExecDeduplicate extends ExecNodeBase<RowData>
deduplicateNode,
outputType);
operator =
- new GlutenVectorOneInputOperator(
+ new GlutenOneInputOperator(
new StatefulPlanNode(streamRankNode.getId(), streamRankNode),
PlanNodeIdGenerator.newId(),
inputType,
- Map.of(streamRankNode.getId(), outputType));
+ Map.of(streamRankNode.getId(), outputType),
+ RowData.class,
+ RowData.class,
+ "StreamExecDeduplicate");
} else {
throw new RuntimeException("ProcTime in deduplicate is not supported.");
}
diff --git a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecExchange.java b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecExchange.java
index e6ec9162c2..17e3966d52 100644
--- a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecExchange.java
+++ b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecExchange.java
@@ -19,7 +19,7 @@ package org.apache.flink.table.planner.plan.nodes.exec.stream;
import org.apache.gluten.streaming.api.operators.GlutenOperator;
import org.apache.gluten.streaming.runtime.partitioner.GlutenKeyGroupStreamPartitioner;
import org.apache.gluten.table.runtime.keyselector.GlutenKeySelector;
-import org.apache.gluten.table.runtime.operators.GlutenVectorOneInputOperator;
+import org.apache.gluten.table.runtime.operators.GlutenOneInputOperator;
import org.apache.gluten.util.LogicalTypeConverter;
import org.apache.gluten.util.PlanNodeIdGenerator;
@@ -164,8 +164,14 @@ public class StreamExecExchange extends CommonExecExchange implements StreamExec
partitionFunctionSpec);
PlanNode exchange = new StreamPartitionNode(id, localPartition, parallelism);
final OneInputStreamOperator exchangeKeyGenerator =
- new GlutenVectorOneInputOperator(
- new StatefulPlanNode(id, exchange), id, glutenInputType, Map.of(id, outputType));
+ new GlutenOneInputOperator(
+ new StatefulPlanNode(id, exchange),
+ id,
+ glutenInputType,
+ Map.of(id, outputType),
+ RowData.class,
+ RowData.class,
+ "StreamExecExchange");
inputTransform =
ExecNodeUtil.createOneInputTransformation(
inputTransform,
diff --git a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecGlobalWindowAggregate.java b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecGlobalWindowAggregate.java
index ee8c91a375..9cbc017232 100644
--- a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecGlobalWindowAggregate.java
+++ b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecGlobalWindowAggregate.java
@@ -19,7 +19,7 @@ package org.apache.flink.table.planner.plan.nodes.exec.stream;
import org.apache.gluten.rexnode.AggregateCallConverter;
import org.apache.gluten.rexnode.Utils;
import org.apache.gluten.rexnode.WindowUtils;
-import org.apache.gluten.table.runtime.operators.GlutenVectorOneInputOperator;
+import org.apache.gluten.table.runtime.operators.GlutenOneInputOperator;
import org.apache.gluten.util.LogicalTypeConverter;
import org.apache.gluten.util.PlanNodeIdGenerator;
@@ -254,11 +254,14 @@ public class StreamExecGlobalWindowAggregate extends StreamExecWindowAggregateBa
outputType,
rowtimeIndex);
final OneInputStreamOperator windowOperator =
- new GlutenVectorOneInputOperator(
+ new GlutenOneInputOperator(
new StatefulPlanNode(windowAgg.getId(), windowAgg),
PlanNodeIdGenerator.newId(),
inputType,
- Map.of(windowAgg.getId(), outputType));
+ Map.of(windowAgg.getId(), outputType),
+ RowData.class,
+ RowData.class,
+ "StreamExecGlobalWindowAggregate");
// --- End Gluten-specific code changes ---
final RowDataKeySelector selector =
diff --git a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecGroupAggregate.java b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecGroupAggregate.java
index 6758111254..0f9ff4a9a5 100644
--- a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecGroupAggregate.java
+++ b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecGroupAggregate.java
@@ -18,7 +18,7 @@ package org.apache.flink.table.planner.plan.nodes.exec.stream;
import org.apache.gluten.rexnode.AggregateCallConverter;
import org.apache.gluten.rexnode.Utils;
-import org.apache.gluten.table.runtime.operators.GlutenVectorOneInputOperator;
+import org.apache.gluten.table.runtime.operators.GlutenOneInputOperator;
import org.apache.gluten.util.LogicalTypeConverter;
import org.apache.gluten.util.PlanNodeIdGenerator;
@@ -206,11 +206,14 @@ public class StreamExecGroupAggregate extends StreamExecAggregateBase {
new GroupAggregationNode(
PlanNodeIdGenerator.newId(), aggsHandlerNode, keySelectorSpec, outputType);
final OneInputStreamOperator operator =
- new GlutenVectorOneInputOperator(
+ new GlutenOneInputOperator(
new StatefulPlanNode(aggregation.getId(), aggregation),
PlanNodeIdGenerator.newId(),
inputType,
- Map.of(aggregation.getId(), outputType));
+ Map.of(aggregation.getId(), outputType),
+ RowData.class,
+ RowData.class,
+ "StreamExecGroupAggregate");
// --- End Gluten-specific code changes ---
// partitioned aggregation
diff --git a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecGroupWindowAggregate.java b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecGroupWindowAggregate.java
index a2ce1425ea..ccd1165316 100644
--- a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecGroupWindowAggregate.java
+++ b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecGroupWindowAggregate.java
@@ -18,7 +18,7 @@ package org.apache.flink.table.planner.plan.nodes.exec.stream;
import org.apache.gluten.rexnode.AggregateCallConverter;
import org.apache.gluten.rexnode.Utils;
-import org.apache.gluten.table.runtime.operators.GlutenVectorOneInputOperator;
+import org.apache.gluten.table.runtime.operators.GlutenOneInputOperator;
import org.apache.gluten.util.LogicalTypeConverter;
import org.apache.gluten.util.PlanNodeIdGenerator;
@@ -240,11 +240,14 @@ public class StreamExecGroupWindowAggregate extends StreamExecAggregateBase {
1, // TODO: get from window attributes
outputType);
final OneInputStreamOperator windowOperator =
- new GlutenVectorOneInputOperator(
+ new GlutenOneInputOperator(
new StatefulPlanNode(windowAgg.getId(), windowAgg),
PlanNodeIdGenerator.newId(),
inputType,
- Map.of(windowAgg.getId(), outputType));
+ Map.of(windowAgg.getId(), outputType),
+ RowData.class,
+ RowData.class,
+ "StreamExecGroupWindowAggregate");
// --- End Gluten-specific code changes ---
final OneInputTransformation<RowData, RowData> transform =
@@ -264,6 +267,7 @@ public class StreamExecGroupWindowAggregate extends StreamExecAggregateBase {
InternalTypeInfo.of(inputRowType));
transform.setStateKeySelector(selector);
transform.setStateKeyType(selector.getProducedType());
+
return transform;
}
}
diff --git a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecJoin.java b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecJoin.java
index fe8cec7974..cf66cbbee4 100644
--- a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecJoin.java
+++ b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecJoin.java
@@ -19,7 +19,7 @@ package org.apache.flink.table.planner.plan.nodes.exec.stream;
import org.apache.gluten.rexnode.RexConversionContext;
import org.apache.gluten.rexnode.RexNodeConverter;
import org.apache.gluten.rexnode.Utils;
-import org.apache.gluten.table.runtime.operators.GlutenVectorTwoInputOperator;
+import org.apache.gluten.table.runtime.operators.GlutenTwoInputOperator;
import org.apache.gluten.util.LogicalTypeConverter;
import org.apache.gluten.util.PlanNodeIdGenerator;
@@ -308,13 +308,16 @@ public class StreamExecJoin extends ExecNodeBase<RowData>
outputType,
1024);
operator =
- new GlutenVectorTwoInputOperator(
+ new GlutenTwoInputOperator(
new StatefulPlanNode(join.getId(), join),
leftInput.getId(),
rightInput.getId(),
leftInputType,
rightInputType,
- Map.of(join.getId(), outputType));
+ Map.of(join.getId(), outputType),
+ RowData.class,
+ RowData.class,
+ "StreamExecJoin");
// --- End Gluten-specific code changes ---
}
}
diff --git a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecLocalWindowAggregate.java b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecLocalWindowAggregate.java
index 4ffcf7998c..fd0cc04a30 100644
--- a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecLocalWindowAggregate.java
+++ b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecLocalWindowAggregate.java
@@ -19,7 +19,7 @@ package org.apache.flink.table.planner.plan.nodes.exec.stream;
import org.apache.gluten.rexnode.AggregateCallConverter;
import org.apache.gluten.rexnode.Utils;
import org.apache.gluten.rexnode.WindowUtils;
-import org.apache.gluten.table.runtime.operators.GlutenVectorOneInputOperator;
+import org.apache.gluten.table.runtime.operators.GlutenOneInputOperator;
import org.apache.gluten.util.LogicalTypeConverter;
import org.apache.gluten.util.PlanNodeIdGenerator;
@@ -222,11 +222,14 @@ public class StreamExecLocalWindowAggregate extends StreamExecWindowAggregateBas
outputType,
rowtimeIndex);
final OneInputStreamOperator localAggOperator =
- new GlutenVectorOneInputOperator(
+ new GlutenOneInputOperator(
new StatefulPlanNode(windowAgg.getId(), windowAgg),
PlanNodeIdGenerator.newId(),
inputType,
- Map.of(windowAgg.getId(), outputType));
+ Map.of(windowAgg.getId(), outputType),
+ RowData.class,
+ RowData.class,
+ "StreamExecLocalWindowAggregate");
// --- End Gluten-specific code changes ---
return ExecNodeUtil.createOneInputTransformation(
diff --git a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecRank.java b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecRank.java
index c1b87e8f60..eeaa27b750 100644
--- a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecRank.java
+++ b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecRank.java
@@ -17,7 +17,7 @@
package org.apache.flink.table.planner.plan.nodes.exec.stream;
import org.apache.gluten.rexnode.Utils;
-import org.apache.gluten.table.runtime.operators.GlutenVectorOneInputOperator;
+import org.apache.gluten.table.runtime.operators.GlutenOneInputOperator;
import org.apache.gluten.util.LogicalTypeConverter;
import org.apache.gluten.util.PlanNodeIdGenerator;
@@ -268,11 +268,14 @@ public class StreamExecRank extends ExecNodeBase<RowData>
streamTopNNode,
outputType);
final OneInputStreamOperator operator =
- new GlutenVectorOneInputOperator(
+ new GlutenOneInputOperator(
new StatefulPlanNode(streamRankNode.getId(), streamRankNode),
PlanNodeIdGenerator.newId(),
inputType,
- Map.of(streamRankNode.getId(), outputType));
+ Map.of(streamRankNode.getId(), outputType),
+ RowData.class,
+ RowData.class,
+ "StreamExecRank");
// --- End Gluten-specific code changes ---
OneInputTransformation<RowData, RowData> transform =
diff --git a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecWatermarkAssigner.java b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecWatermarkAssigner.java
index e0a56ac918..3230bac8c2 100644
--- a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecWatermarkAssigner.java
+++ b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecWatermarkAssigner.java
@@ -19,7 +19,7 @@ package org.apache.flink.table.planner.plan.nodes.exec.stream;
import org.apache.gluten.rexnode.RexConversionContext;
import org.apache.gluten.rexnode.RexNodeConverter;
import org.apache.gluten.rexnode.Utils;
-import org.apache.gluten.table.runtime.operators.GlutenVectorOneInputOperator;
+import org.apache.gluten.table.runtime.operators.GlutenOneInputOperator;
import org.apache.gluten.util.LogicalTypeConverter;
import org.apache.gluten.util.PlanNodeIdGenerator;
@@ -162,11 +162,14 @@ public class StreamExecWatermarkAssigner extends ExecNodeBase<RowData>
rowtimeFieldIndex,
watermarkInterval);
final OneInputStreamOperator watermarkOperator =
- new GlutenVectorOneInputOperator(
+ new GlutenOneInputOperator(
new StatefulPlanNode(watermark.getId(), watermark),
PlanNodeIdGenerator.newId(),
inputType,
- Map.of(watermark.getId(), outputType));
+ Map.of(watermark.getId(), outputType),
+ RowData.class,
+ RowData.class,
+ "StreamExecWatermarkAssigner");
return ExecNodeUtil.createOneInputTransformation(
inputTransform,
diff --git a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecWindowAggregate.java b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecWindowAggregate.java
index afe5d01483..499a65516d 100644
--- a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecWindowAggregate.java
+++ b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecWindowAggregate.java
@@ -19,7 +19,7 @@ package org.apache.flink.table.planner.plan.nodes.exec.stream;
import org.apache.gluten.rexnode.AggregateCallConverter;
import org.apache.gluten.rexnode.Utils;
import org.apache.gluten.rexnode.WindowUtils;
-import org.apache.gluten.table.runtime.operators.GlutenVectorOneInputOperator;
+import org.apache.gluten.table.runtime.operators.GlutenOneInputOperator;
import org.apache.gluten.util.LogicalTypeConverter;
import org.apache.gluten.util.PlanNodeIdGenerator;
@@ -246,11 +246,14 @@ public class StreamExecWindowAggregate extends StreamExecWindowAggregateBase {
outputType,
rowtimeIndex);
final OneInputStreamOperator windowOperator =
- new GlutenVectorOneInputOperator(
+ new GlutenOneInputOperator(
new StatefulPlanNode(windowAgg.getId(), windowAgg),
PlanNodeIdGenerator.newId(),
inputType,
- Map.of(windowAgg.getId(), outputType));
+ Map.of(windowAgg.getId(), outputType),
+ RowData.class,
+ RowData.class,
+ "StreamExecWindowAggregate");
// --- End Gluten-specific code changes ---
final RowDataKeySelector selector =
@@ -270,6 +273,8 @@ public class StreamExecWindowAggregate extends StreamExecWindowAggregateBase {
false);
// set KeyType and Selector for state
+ // This key selector will be updated in OffloadedJobGraphGenerator to GlutenKeySelector as
+ // needed.
transform.setStateKeySelector(selector);
transform.setStateKeyType(selector.getProducedType());
return transform;
diff --git a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecWindowJoin.java b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecWindowJoin.java
index d32b22911b..400a86ccb0 100644
--- a/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecWindowJoin.java
+++ b/gluten-flink/planner/src/main/java/org/apache/flink/table/planner/plan/nodes/exec/stream/StreamExecWindowJoin.java
@@ -17,7 +17,7 @@
package org.apache.flink.table.planner.plan.nodes.exec.stream;
import org.apache.gluten.rexnode.Utils;
-import org.apache.gluten.table.runtime.operators.GlutenVectorTwoInputOperator;
+import org.apache.gluten.table.runtime.operators.GlutenTwoInputOperator;
import org.apache.gluten.util.LogicalTypeConverter;
import org.apache.gluten.util.PlanNodeIdGenerator;
@@ -227,13 +227,16 @@ public class StreamExecWindowJoin extends ExecNodeBase<RowData>
leftWindowEndIndex,
rightWindowEndIndex);
final TwoInputStreamOperator operator =
- new GlutenVectorTwoInputOperator(
+ new GlutenTwoInputOperator(
new StatefulPlanNode(join.getId(), join),
leftInput.getId(),
rightInput.getId(),
leftInputType,
rightInputType,
- Map.of(join.getId(), outputType));
+ Map.of(join.getId(), outputType),
+ RowData.class,
+ RowData.class,
+ "StreamExecWindowJoin");
// --- End Gluten-specific code changes ---
final RowType returnType = (RowType) getOutputType();
diff --git a/gluten-flink/planner/src/main/java/org/apache/gluten/velox/FileSystemSinkFactory.java b/gluten-flink/planner/src/main/java/org/apache/gluten/velox/FileSystemSinkFactory.java
index 657e321762..ff08dcec63 100644
--- a/gluten-flink/planner/src/main/java/org/apache/gluten/velox/FileSystemSinkFactory.java
+++ b/gluten-flink/planner/src/main/java/org/apache/gluten/velox/FileSystemSinkFactory.java
@@ -128,7 +128,10 @@ public class FileSystemSinkFactory implements VeloxSourceSinkFactory {
new StatefulPlanNode(fileSystemWriteNode.getId(), fileSystemWriteNode),
PlanNodeIdGenerator.newId(),
inputDataColumns,
- Map.of(fileSystemWriteNode.getId(), ignore));
+ Map.of(fileSystemWriteNode.getId(), ignore),
+ RowData.class,
+ RowData.class,
+ "FileSystemInsertTable");
GlutenOneInputOperatorFactory<?, ?> operatorFactory =
new GlutenOneInputOperatorFactory(onewInputOperator);
Transformation<RowData> veloxFileWriterTransformation =
diff --git a/gluten-flink/planner/src/main/java/org/apache/gluten/velox/FromElementsSourceFactory.java b/gluten-flink/planner/src/main/java/org/apache/gluten/velox/FromElementsSourceFactory.java
index da31edeccd..40c8b86c11 100644
--- a/gluten-flink/planner/src/main/java/org/apache/gluten/velox/FromElementsSourceFactory.java
+++ b/gluten-flink/planner/src/main/java/org/apache/gluten/velox/FromElementsSourceFactory.java
@@ -17,7 +17,7 @@
package org.apache.gluten.velox;
import org.apache.gluten.streaming.api.operators.GlutenStreamSource;
-import org.apache.gluten.table.runtime.operators.GlutenVectorSourceFunction;
+import org.apache.gluten.table.runtime.operators.GlutenSourceFunction;
import org.apache.gluten.util.LogicalTypeConverter;
import org.apache.gluten.util.PlanNodeIdGenerator;
import org.apache.gluten.util.ReflectUtils;
@@ -94,11 +94,13 @@ public class FromElementsSourceFactory implements VeloxSourceSinkFactory {
new TableScanNode(PlanNodeIdGenerator.newId(), rowType, tableHandle, List.of());
GlutenStreamSource op =
new GlutenStreamSource(
- new GlutenVectorSourceFunction(
+ new GlutenSourceFunction(
new StatefulPlanNode(scanNode.getId(), scanNode),
Map.of(scanNode.getId(), rowType),
scanNode.getId(),
- new FromElementsConnectorSplit("connector-from-elements", 0, false)));
+ new FromElementsConnectorSplit("connector-from-elements", 0, false),
+ RowData.class),
+ "FromElementsSource");
return new LegacySourceTransformation<RowData>(
sourceTransformation.getName(),
op,
diff --git a/gluten-flink/planner/src/main/java/org/apache/gluten/velox/FuzzerSourceSinkFactory.java b/gluten-flink/planner/src/main/java/org/apache/gluten/velox/FuzzerSourceSinkFactory.java
index 16ac7f0831..6e9c232f04 100644
--- a/gluten-flink/planner/src/main/java/org/apache/gluten/velox/FuzzerSourceSinkFactory.java
+++ b/gluten-flink/planner/src/main/java/org/apache/gluten/velox/FuzzerSourceSinkFactory.java
@@ -18,8 +18,8 @@ package org.apache.gluten.velox;
import org.apache.gluten.streaming.api.operators.GlutenOneInputOperatorFactory;
import org.apache.gluten.streaming.api.operators.GlutenStreamSource;
-import org.apache.gluten.table.runtime.operators.GlutenVectorOneInputOperator;
-import org.apache.gluten.table.runtime.operators.GlutenVectorSourceFunction;
+import org.apache.gluten.table.runtime.operators.GlutenOneInputOperator;
+import org.apache.gluten.table.runtime.operators.GlutenSourceFunction;
import org.apache.gluten.util.LogicalTypeConverter;
import org.apache.gluten.util.PlanNodeIdGenerator;
@@ -86,11 +86,12 @@ public class FuzzerSourceSinkFactory implements VeloxSourceSinkFactory {
PlanNode tableScan = new TableScanNode(id, outputType, tableHandle, List.of());
GlutenStreamSource sourceOp =
new GlutenStreamSource(
- new GlutenVectorSourceFunction(
+ new GlutenSourceFunction(
new StatefulPlanNode(id, tableScan),
Map.of(id, outputType),
id,
- new FuzzerConnectorSplit("connector-fuzzer", 1000)));
+ new FuzzerConnectorSplit("connector-fuzzer", 1000),
+ RowData.class));
return new LegacySourceTransformation<RowData>(
sourceTransformation.getName(),
sourceOp,
@@ -128,11 +129,14 @@ public class FuzzerSourceSinkFactory implements VeloxSourceSinkFactory {
List.of(new EmptyNode(outputType)));
GlutenOneInputOperatorFactory operatorFactory =
new GlutenOneInputOperatorFactory(
- new GlutenVectorOneInputOperator(
+ new GlutenOneInputOperator(
new StatefulPlanNode(plan.getId(), plan),
PlanNodeIdGenerator.newId(),
outputType,
- Map.of(plan.getId(), ignore)));
+ Map.of(plan.getId(), ignore),
+ RowData.class,
+ RowData.class,
+ "FuzzerSink"));
DataStream<RowData> newInputStream =
sinkTransformation
.getInputStream()
diff --git a/gluten-flink/planner/src/main/java/org/apache/gluten/velox/KafkaSourceSinkFactory.java b/gluten-flink/planner/src/main/java/org/apache/gluten/velox/KafkaSourceSinkFactory.java
index 54e2b8b436..8644dac3da 100644
--- a/gluten-flink/planner/src/main/java/org/apache/gluten/velox/KafkaSourceSinkFactory.java
+++ b/gluten-flink/planner/src/main/java/org/apache/gluten/velox/KafkaSourceSinkFactory.java
@@ -17,7 +17,7 @@
package org.apache.gluten.velox;
import org.apache.gluten.streaming.api.operators.GlutenStreamSource;
-import org.apache.gluten.table.runtime.operators.GlutenVectorSourceFunction;
+import org.apache.gluten.table.runtime.operators.GlutenSourceFunction;
import org.apache.gluten.util.LogicalTypeConverter;
import org.apache.gluten.util.PlanNodeIdGenerator;
import org.apache.gluten.util.ReflectUtils;
@@ -115,11 +115,13 @@ public class KafkaSourceSinkFactory implements VeloxSourceSinkFactory {
TableScanNode kafkaScan = new TableScanNode(planId, outputType, kafkaTableHandle, List.of());
GlutenStreamSource sourceOp =
new GlutenStreamSource(
- new GlutenVectorSourceFunction(
+ new GlutenSourceFunction(
new StatefulPlanNode(kafkaScan.getId(), kafkaScan),
Map.of(kafkaScan.getId(), outputType),
kafkaScan.getId(),
- connectorSplit));
+ connectorSplit,
+ RowData.class),
+ "KafkaSource");
SourceTransformation sourceTransformation = (SourceTransformation) transformation;
return new LegacySourceTransformation<RowData>(
sourceTransformation.getName(),
diff --git a/gluten-flink/planner/src/main/java/org/apache/gluten/velox/NexmarkSourceFactory.java b/gluten-flink/planner/src/main/java/org/apache/gluten/velox/NexmarkSourceFactory.java
index 736f3cc3c7..ae0d94fb3c 100644
--- a/gluten-flink/planner/src/main/java/org/apache/gluten/velox/NexmarkSourceFactory.java
+++ b/gluten-flink/planner/src/main/java/org/apache/gluten/velox/NexmarkSourceFactory.java
@@ -17,7 +17,7 @@
package org.apache.gluten.velox;
import org.apache.gluten.streaming.api.operators.GlutenStreamSource;
-import org.apache.gluten.table.runtime.operators.GlutenVectorSourceFunction;
+import org.apache.gluten.table.runtime.operators.GlutenSourceFunction;
import org.apache.gluten.util.LogicalTypeConverter;
import org.apache.gluten.util.PlanNodeIdGenerator;
import org.apache.gluten.util.ReflectUtils;
@@ -35,10 +35,14 @@ import org.apache.flink.streaming.api.transformations.SourceTransformation;
import org.apache.flink.table.data.RowData;
import org.apache.flink.table.runtime.typeutils.InternalTypeInfo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import java.util.List;
import java.util.Map;
public class NexmarkSourceFactory implements VeloxSourceSinkFactory {
+ private static final Logger LOG = LoggerFactory.getLogger(NexmarkSourceFactory.class);
@SuppressWarnings("rawtypes")
@Override
@@ -79,13 +83,15 @@ public class NexmarkSourceFactory implements VeloxSourceSinkFactory {
new TableScanNode(id, outputType, new NexmarkTableHandle("connector-nexmark"), List.of());
GlutenStreamSource sourceOp =
new GlutenStreamSource(
- new GlutenVectorSourceFunction(
+ new GlutenSourceFunction(
new StatefulPlanNode(tableScan.getId(), tableScan),
Map.of(id, outputType),
id,
new NexmarkConnectorSplit(
"connector-nexmark",
- maxEvents > Integer.MAX_VALUE ? Integer.MAX_VALUE : maxEvents.intValue())));
+ maxEvents > Integer.MAX_VALUE ? Integer.MAX_VALUE : maxEvents.intValue()),
+ RowData.class));
+
return new LegacySourceTransformation<RowData>(
transformation.getName(),
sourceOp,
diff --git a/gluten-flink/planner/src/main/java/org/apache/gluten/velox/PrintSinkFactory.java b/gluten-flink/planner/src/main/java/org/apache/gluten/velox/PrintSinkFactory.java
index b00a76a21f..737d6bab7e 100644
--- a/gluten-flink/planner/src/main/java/org/apache/gluten/velox/PrintSinkFactory.java
+++ b/gluten-flink/planner/src/main/java/org/apache/gluten/velox/PrintSinkFactory.java
@@ -17,7 +17,7 @@
package org.apache.gluten.velox;
import org.apache.gluten.streaming.api.operators.GlutenOneInputOperatorFactory;
-import org.apache.gluten.table.runtime.operators.GlutenVectorOneInputOperator;
+import org.apache.gluten.table.runtime.operators.GlutenOneInputOperator;
import org.apache.gluten.util.LogicalTypeConverter;
import org.apache.gluten.util.PlanNodeIdGenerator;
@@ -114,11 +114,14 @@ public class PrintSinkFactory implements VeloxSourceSinkFactory {
inputTrans,
transformation.getName(),
new GlutenOneInputOperatorFactory(
- new GlutenVectorOneInputOperator(
+ new GlutenOneInputOperator(
new StatefulPlanNode(tableWriteNode.getId(), tableWriteNode),
PlanNodeIdGenerator.newId(),
inputColumns,
- Map.of(tableWriteNode.getId(), ignore))),
+ Map.of(tableWriteNode.getId(), ignore),
+ RowData.class,
+ RowData.class,
+ "PrintSink")),
transformation.getParallelism());
}
}
diff --git a/gluten-flink/runtime/src/main/java/org/apache/flink/client/StreamGraphTranslator.java b/gluten-flink/runtime/src/main/java/org/apache/flink/client/StreamGraphTranslator.java
index db702d5605..6be550e71c 100644
--- a/gluten-flink/runtime/src/main/java/org/apache/flink/client/StreamGraphTranslator.java
+++ b/gluten-flink/runtime/src/main/java/org/apache/flink/client/StreamGraphTranslator.java
@@ -16,41 +16,16 @@
*/
package org.apache.flink.client;
-import org.apache.gluten.streaming.api.operators.GlutenOneInputOperatorFactory;
-import org.apache.gluten.streaming.api.operators.GlutenOperator;
-import org.apache.gluten.streaming.api.operators.GlutenStreamSource;
-import org.apache.gluten.table.runtime.keyselector.GlutenKeySelector;
-import org.apache.gluten.table.runtime.operators.GlutenVectorOneInputOperator;
-import org.apache.gluten.table.runtime.operators.GlutenVectorSourceFunction;
-import org.apache.gluten.table.runtime.operators.GlutenVectorTwoInputOperator;
-import org.apache.gluten.table.runtime.typeutils.GlutenRowVectorSerializer;
-import org.apache.gluten.util.Utils;
-
-import io.github.zhztheplayer.velox4j.plan.StatefulPlanNode;
-import io.github.zhztheplayer.velox4j.type.RowType;
+import org.apache.gluten.client.OffloadedJobGraphGenerator;
import org.apache.flink.api.dag.Pipeline;
import org.apache.flink.configuration.Configuration;
-import org.apache.flink.runtime.jobgraph.IntermediateDataSetID;
import org.apache.flink.runtime.jobgraph.JobGraph;
-import org.apache.flink.runtime.jobgraph.JobVertex;
-import org.apache.flink.streaming.api.graph.NonChainedOutput;
-import org.apache.flink.streaming.api.graph.StreamConfig;
-import org.apache.flink.streaming.api.graph.StreamEdge;
import org.apache.flink.streaming.api.graph.StreamGraph;
-import org.apache.flink.streaming.api.operators.SimpleOperatorFactory;
-import org.apache.flink.streaming.api.operators.StreamOperator;
-import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-
import static org.apache.flink.util.Preconditions.checkArgument;
/**
@@ -73,12 +48,17 @@ public class StreamGraphTranslator implements FlinkPipelineTranslator {
@Override
public JobGraph translateToJobGraph(
Pipeline pipeline, Configuration optimizerConfiguration, int defaultParallelism) {
+ // --- Begin Gluten-specific code changes ---
+
checkArgument(
pipeline instanceof StreamGraph, "Given pipeline is not a DataStream StreamGraph.");
-
StreamGraph streamGraph = (StreamGraph) pipeline;
JobGraph jobGraph = streamGraph.getJobGraph(userClassloader, null);
- return mergeGlutenOperators(jobGraph);
+ OffloadedJobGraphGenerator generator =
+ new OffloadedJobGraphGenerator(jobGraph, userClassloader);
+ return generator.generate();
+ // --- End Gluten-specific code changes ---
+
}
@Override
@@ -95,155 +75,4 @@ public class StreamGraphTranslator implements FlinkPipelineTranslator {
public boolean canTranslate(Pipeline pipeline) {
return pipeline instanceof StreamGraph;
}
-
- // --- Begin Gluten-specific code changes ---
- private JobGraph mergeGlutenOperators(JobGraph jobGraph) {
- for (JobVertex vertex : jobGraph.getVertices()) {
- StreamConfig streamConfig = new StreamConfig(vertex.getConfiguration());
- buildGlutenChains(streamConfig);
- LOG.debug("Vertex {} is {}.", vertex.getName(), streamConfig);
- }
- return jobGraph;
- }
-
- // A JobVertex may contain several operators chained like this: Source-->Op1-->Op2-->Sink1.
- // -->Sink2.
- // If the operators connected all support translated to gluten, we merge them into
- // a single GlutenOperator to avoid data transferred between flink and native.
- // One operator may be followed by several other operators.
- private void buildGlutenChains(StreamConfig vertexConfig) {
- Map<Integer, StreamConfig> serializedTasks =
- vertexConfig.getTransitiveChainedTaskConfigs(userClassloader);
- Map<Integer, StreamConfig> chainedTasks = new HashMap<>(serializedTasks.size());
- serializedTasks.forEach(
- (id, config) -> chainedTasks.put(id, new StreamConfig(config.getConfiguration())));
- buildGlutenChains(vertexConfig, chainedTasks);
- // TODO: may need fallback if failed.
- vertexConfig.setAndSerializeTransitiveChainedTaskConfigs(chainedTasks);
- }
-
- private void buildGlutenChains(StreamConfig taskConfig, Map<Integer, StreamConfig> chainedTasks) {
- List<StreamEdge> outEdges = taskConfig.getChainedOutputs(userClassloader);
- Optional<GlutenOperator> sourceOperatorOpt = getGlutenOperator(taskConfig);
- GlutenOperator sourceOperator = sourceOperatorOpt.orElse(null);
- boolean isSourceGluten = sourceOperatorOpt.isPresent();
- if (outEdges == null || outEdges.isEmpty()) {
- LOG.debug("{} has no chained task.", taskConfig.getOperatorName());
- // TODO: judge whether can set?
- if (isSourceGluten) {
- if (taskConfig.getOperatorName().equals("exchange-hash")) {
- taskConfig.setTypeSerializerOut(new GlutenRowVectorSerializer(null));
- }
- Map<IntermediateDataSetID, String> nodeToNonChainedOuts = new HashMap<>(outEdges.size());
- taskConfig
- .getOperatorNonChainedOutputs(userClassloader)
- .forEach(edge -> nodeToNonChainedOuts.put(edge.getDataSetId(), sourceOperator.getId()));
- Utils.setNodeToNonChainedOutputs(taskConfig, nodeToNonChainedOuts);
- taskConfig.serializeAllConfigs();
- }
- return;
- }
- Map<String, Integer> nodeToChainedOuts = new HashMap<>(outEdges.size());
- Map<IntermediateDataSetID, String> nodeToNonChainedOuts = new HashMap<>(outEdges.size());
- Map<String, RowType> nodeToOutTypes = new HashMap<>(outEdges.size());
- List<StreamEdge> chainedOutputs = new ArrayList<>(outEdges.size());
- List<NonChainedOutput> nonChainedOutputs = new ArrayList<>(outEdges.size());
- StatefulPlanNode sourceNode = isSourceGluten ? sourceOperator.getPlanNode() : null;
- boolean allGluten = true;
- LOG.debug("Edge size {}, OP {}", outEdges.size(), sourceOperator);
- for (StreamEdge outEdge : outEdges) {
- StreamConfig outTask = chainedTasks.get(outEdge.getTargetId());
- if (outTask == null) {
- LOG.error("Not find task {} in Chained tasks", outEdge.getTargetId());
- allGluten = false;
- break;
- }
- buildGlutenChains(outTask, chainedTasks);
- Optional<GlutenOperator> outOperator = getGlutenOperator(outTask);
- if (isSourceGluten && outOperator.isPresent()) {
- StatefulPlanNode outNode = outOperator.get().getPlanNode();
- if (sourceNode != null) {
- sourceNode.addTarget(outNode);
- LOG.debug("Add {} target {}", sourceNode, outNode);
- } else {
- sourceNode = outNode;
- LOG.debug("Set target node to {}", outNode);
- }
- Map<String, Integer> node2Out = Utils.getNodeToChainedOutputs(outTask, userClassloader);
- if (node2Out != null) {
- nodeToChainedOuts.putAll(node2Out);
- nodeToNonChainedOuts.putAll(Utils.getNodeToNonChainedOutputs(outTask, userClassloader));
- } else {
- outTask
- .getChainedOutputs(userClassloader)
- .forEach(edge -> nodeToChainedOuts.put(outNode.getId(), edge.getTargetId()));
- outTask
- .getOperatorNonChainedOutputs(userClassloader)
- .forEach(edge -> nodeToNonChainedOuts.put(edge.getDataSetId(), outNode.getId()));
- }
- nodeToOutTypes.putAll(outOperator.get().getOutputTypes());
- chainedOutputs.addAll(outTask.getChainedOutputs(userClassloader));
- nonChainedOutputs.addAll(outTask.getOperatorNonChainedOutputs(userClassloader));
- } else {
- allGluten = false;
- LOG.debug(
- "{} and {} can not be merged", taskConfig.getOperatorName(), outTask.getOperatorName());
- break;
- }
- }
- if (allGluten) {
- if (sourceOperator instanceof GlutenStreamSource) {
- GlutenStreamSource streamSource = (GlutenStreamSource) sourceOperator;
- taskConfig.setStreamOperator(
- new GlutenStreamSource(
- new GlutenVectorSourceFunction(
- sourceNode,
- nodeToOutTypes,
- sourceOperator.getId(),
- streamSource.getConnectorSplit())));
- } else if (sourceOperator instanceof GlutenVectorTwoInputOperator) {
- GlutenVectorTwoInputOperator twoInputOperator =
- (GlutenVectorTwoInputOperator) sourceOperator;
- taskConfig.setStreamOperator(
- new GlutenVectorTwoInputOperator(
- sourceNode,
- twoInputOperator.getLeftId(),
- twoInputOperator.getRightId(),
- twoInputOperator.getLeftInputType(),
- twoInputOperator.getRightInputType(),
- nodeToOutTypes));
- // TODO: judge whether can set?
- taskConfig.setStatePartitioner(0, new GlutenKeySelector());
- taskConfig.setStatePartitioner(1, new GlutenKeySelector());
- taskConfig.setupNetworkInputs(
- new GlutenRowVectorSerializer(null), new GlutenRowVectorSerializer(null));
- } else {
- taskConfig.setStreamOperator(
- new GlutenVectorOneInputOperator(
- sourceNode, sourceOperator.getId(), sourceOperator.getInputType(), nodeToOutTypes));
- // TODO: judge whether can set?
- taskConfig.setStatePartitioner(0, new GlutenKeySelector());
- taskConfig.setupNetworkInputs(new GlutenRowVectorSerializer(null));
- }
- Utils.setNodeToChainedOutputs(taskConfig, nodeToChainedOuts);
- Utils.setNodeToNonChainedOutputs(taskConfig, nodeToNonChainedOuts);
- taskConfig.setChainedOutputs(chainedOutputs);
- taskConfig.setOperatorNonChainedOutputs(nonChainedOutputs);
- taskConfig.serializeAllConfigs();
- }
- }
-
- private Optional<GlutenOperator> getGlutenOperator(StreamConfig taskConfig) {
- StreamOperatorFactory operatorFactory = taskConfig.getStreamOperatorFactory(userClassloader);
- if (operatorFactory instanceof SimpleOperatorFactory) {
- StreamOperator streamOperator = taskConfig.getStreamOperator(userClassloader);
- if (streamOperator instanceof GlutenOperator) {
- return Optional.of((GlutenOperator) streamOperator);
- }
- } else if (operatorFactory instanceof GlutenOneInputOperatorFactory) {
- return Optional.of(((GlutenOneInputOperatorFactory) operatorFactory).getOperator());
- }
- return Optional.empty();
- }
- // --- End Gluten-specific code changes ---
}
diff --git a/gluten-flink/runtime/src/main/java/org/apache/flink/streaming/runtime/tasks/OperatorChain.java b/gluten-flink/runtime/src/main/java/org/apache/flink/streaming/runtime/tasks/OperatorChain.java
deleted file mode 100644
index 082bd6edaf..0000000000
--- a/gluten-flink/runtime/src/main/java/org/apache/flink/streaming/runtime/tasks/OperatorChain.java
+++ /dev/null
@@ -1,944 +0,0 @@
-/*
- * 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.flink.streaming.runtime.tasks;
-
-import org.apache.gluten.streaming.runtime.tasks.GlutenOutputCollector;
-import org.apache.gluten.util.Utils;
-
-import org.apache.flink.annotation.VisibleForTesting;
-import org.apache.flink.api.common.typeutils.TypeSerializer;
-import org.apache.flink.api.java.tuple.Tuple2;
-import org.apache.flink.metrics.Counter;
-import org.apache.flink.metrics.SimpleCounter;
-import org.apache.flink.metrics.groups.OperatorMetricGroup;
-import org.apache.flink.runtime.checkpoint.CheckpointException;
-import org.apache.flink.runtime.checkpoint.CheckpointMetaData;
-import org.apache.flink.runtime.checkpoint.CheckpointOptions;
-import org.apache.flink.runtime.checkpoint.StateObjectCollection;
-import org.apache.flink.runtime.checkpoint.channel.ChannelStateWriter;
-import org.apache.flink.runtime.event.AbstractEvent;
-import org.apache.flink.runtime.execution.Environment;
-import org.apache.flink.runtime.io.network.api.StopMode;
-import org.apache.flink.runtime.io.network.api.writer.RecordWriter;
-import org.apache.flink.runtime.io.network.api.writer.RecordWriterDelegate;
-import org.apache.flink.runtime.io.network.partition.consumer.IndexedInputGate;
-import org.apache.flink.runtime.jobgraph.IntermediateDataSetID;
-import org.apache.flink.runtime.jobgraph.OperatorID;
-import org.apache.flink.runtime.metrics.MetricNames;
-import org.apache.flink.runtime.metrics.groups.InternalOperatorMetricGroup;
-import org.apache.flink.runtime.metrics.groups.TaskIOMetricGroup;
-import org.apache.flink.runtime.operators.coordination.AcknowledgeCheckpointEvent;
-import org.apache.flink.runtime.operators.coordination.OperatorEvent;
-import org.apache.flink.runtime.operators.coordination.OperatorEventDispatcher;
-import org.apache.flink.runtime.plugable.SerializationDelegate;
-import org.apache.flink.runtime.state.CheckpointStreamFactory;
-import org.apache.flink.runtime.state.SnapshotResult;
-import org.apache.flink.streaming.api.graph.NonChainedOutput;
-import org.apache.flink.streaming.api.graph.StreamConfig;
-import org.apache.flink.streaming.api.graph.StreamEdge;
-import org.apache.flink.streaming.api.operators.BoundedMultiInput;
-import org.apache.flink.streaming.api.operators.CountingOutput;
-import org.apache.flink.streaming.api.operators.Input;
-import org.apache.flink.streaming.api.operators.MultipleInputStreamOperator;
-import org.apache.flink.streaming.api.operators.OneInputStreamOperator;
-import org.apache.flink.streaming.api.operators.OperatorSnapshotFutures;
-import org.apache.flink.streaming.api.operators.Output;
-import org.apache.flink.streaming.api.operators.SourceOperator;
-import org.apache.flink.streaming.api.operators.StreamOperator;
-import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
-import org.apache.flink.streaming.api.operators.StreamOperatorFactoryUtil;
-import org.apache.flink.streaming.api.operators.StreamTaskStateInitializer;
-import org.apache.flink.streaming.runtime.io.RecordWriterOutput;
-import org.apache.flink.streaming.runtime.io.StreamTaskSourceInput;
-import org.apache.flink.streaming.runtime.operators.sink.SinkWriterOperatorFactory;
-import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
-import org.apache.flink.streaming.runtime.tasks.mailbox.MailboxExecutorFactory;
-import org.apache.flink.util.CollectionUtil;
-import org.apache.flink.util.FlinkException;
-import org.apache.flink.util.OutputTag;
-import org.apache.flink.util.SerializedValue;
-
-import org.apache.flink.shaded.guava31.com.google.common.io.Closer;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.annotation.Nullable;
-
-import java.io.Closeable;
-import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.function.Supplier;
-import java.util.stream.Collectors;
-
-import static org.apache.flink.util.Preconditions.checkArgument;
-import static org.apache.flink.util.Preconditions.checkNotNull;
-import static org.apache.flink.util.Preconditions.checkState;
-
-/**
- * The {@code OperatorChain} contains all operators that are executed as one chain within a single
- * {@link StreamTask}.
- *
- * <p>The main entry point to the chain is it's {@code mainOperator}. {@code mainOperator} is
- * driving the execution of the {@link StreamTask}, by pulling the records from network inputs
- * and/or source inputs and pushing produced records to the remaining chained operators.
- *
- * @param <OUT> The type of elements accepted by the chain, i.e., the input type of the chain's main
- * operator.
- */
-public abstract class OperatorChain<OUT, OP extends StreamOperator<OUT>>
- implements BoundedMultiInput, Closeable {
-
- private static final Logger LOG = LoggerFactory.getLogger(OperatorChain.class);
-
- protected final RecordWriterOutput<?>[] streamOutputs;
-
- protected final WatermarkGaugeExposingOutput<StreamRecord<OUT>> mainOperatorOutput;
-
- /**
- * For iteration, {@link StreamIterationHead} and {@link StreamIterationTail} used for executing
- * feedback edges do not contain any operators, in which case, {@code mainOperatorWrapper} and
- * {@code tailOperatorWrapper} are null.
- *
- * <p>Usually first operator in the chain is the same as {@link #mainOperatorWrapper}, but that's
- * not the case if there are chained source inputs. In this case, one of the source inputs will be
- * the first operator. For example the following operator chain is possible:
- *
- * <pre>
- * first
- * \
- * main (multi-input) -> ... -> tail
- * /
- * second
- * </pre>
- *
- * <p>Where "first" and "second" (there can be more) are chained source operators. When it comes
- * to things like closing, stat initialisation or state snapshotting, the operator chain is
- * traversed: first, second, main, ..., tail or in reversed order: tail, ..., main, second, first
- */
- @Nullable protected final StreamOperatorWrapper<OUT, OP> mainOperatorWrapper;
-
- @Nullable protected final StreamOperatorWrapper<?, ?> firstOperatorWrapper;
- @Nullable protected final StreamOperatorWrapper<?, ?> tailOperatorWrapper;
-
- protected final Map<StreamConfig.SourceInputConfig, ChainedSource> chainedSources;
-
- protected final int numOperators;
-
- protected final OperatorEventDispatcherImpl operatorEventDispatcher;
-
- protected final Closer closer = Closer.create();
-
- protected final @Nullable FinishedOnRestoreInput finishedOnRestoreInput;
-
- protected boolean isClosed;
-
- public OperatorChain(
- StreamTask<OUT, OP> containingTask,
- RecordWriterDelegate<SerializationDelegate<StreamRecord<OUT>>> recordWriterDelegate) {
-
- this.operatorEventDispatcher =
- new OperatorEventDispatcherImpl(
- containingTask.getEnvironment().getUserCodeClassLoader().asClassLoader(),
- containingTask.getEnvironment().getOperatorCoordinatorEventGateway());
-
- final ClassLoader userCodeClassloader = containingTask.getUserCodeClassLoader();
- final StreamConfig configuration = containingTask.getConfiguration();
-
- StreamOperatorFactory<OUT> operatorFactory =
- configuration.getStreamOperatorFactory(userCodeClassloader);
-
- // we read the chained configs, and the order of record writer registrations by output name
- Map<Integer, StreamConfig> chainedConfigs =
- configuration.getTransitiveChainedTaskConfigsWithSelf(userCodeClassloader);
-
- // create the final output stream writers
- // we iterate through all the out edges from this job vertex and create a stream output
- List<NonChainedOutput> outputsInOrder =
- configuration.getVertexNonChainedOutputs(userCodeClassloader);
- Map<IntermediateDataSetID, RecordWriterOutput<?>> recordWriterOutputs =
- CollectionUtil.newHashMapWithExpectedSize(outputsInOrder.size());
- this.streamOutputs = new RecordWriterOutput<?>[outputsInOrder.size()];
- this.finishedOnRestoreInput =
- this.isTaskDeployedAsFinished()
- ? new FinishedOnRestoreInput(
- streamOutputs, configuration.getInputs(userCodeClassloader).length)
- : null;
-
- // from here on, we need to make sure that the output writers are shut down again on failure
- boolean success = false;
- try {
- createChainOutputs(
- outputsInOrder,
- recordWriterDelegate,
- chainedConfigs,
- containingTask,
- recordWriterOutputs);
-
- // we create the chain of operators and grab the collector that leads into the chain
- List<StreamOperatorWrapper<?, ?>> allOpWrappers = new ArrayList<>(chainedConfigs.size());
- this.mainOperatorOutput =
- createOutputCollector(
- containingTask,
- configuration,
- chainedConfigs,
- userCodeClassloader,
- recordWriterOutputs,
- allOpWrappers,
- containingTask.getMailboxExecutorFactory(),
- operatorFactory != null);
-
- if (operatorFactory != null) {
- Tuple2<OP, Optional<ProcessingTimeService>> mainOperatorAndTimeService =
- StreamOperatorFactoryUtil.createOperator(
- operatorFactory,
- containingTask,
- configuration,
- mainOperatorOutput,
- operatorEventDispatcher);
-
- OP mainOperator = mainOperatorAndTimeService.f0;
- mainOperator
- .getMetricGroup()
- .gauge(MetricNames.IO_CURRENT_OUTPUT_WATERMARK, mainOperatorOutput.getWatermarkGauge());
- this.mainOperatorWrapper =
- createOperatorWrapper(
- mainOperator, containingTask, configuration, mainOperatorAndTimeService.f1, true);
-
- // add main operator to end of chain
- allOpWrappers.add(mainOperatorWrapper);
-
- this.tailOperatorWrapper = allOpWrappers.get(0);
- } else {
- checkState(allOpWrappers.size() == 0);
- this.mainOperatorWrapper = null;
- this.tailOperatorWrapper = null;
- }
-
- this.chainedSources =
- createChainedSources(
- containingTask,
- configuration.getInputs(userCodeClassloader),
- chainedConfigs,
- userCodeClassloader,
- allOpWrappers);
-
- this.numOperators = allOpWrappers.size();
-
- firstOperatorWrapper = linkOperatorWrappers(allOpWrappers);
-
- success = true;
- } finally {
- // make sure we clean up after ourselves in case of a failure after acquiring
- // the first resources
- if (!success) {
- for (int i = 0; i < streamOutputs.length; i++) {
- if (streamOutputs[i] != null) {
- streamOutputs[i].close();
- }
- streamOutputs[i] = null;
- }
- }
- }
- }
-
- @VisibleForTesting
- OperatorChain(
- List<StreamOperatorWrapper<?, ?>> allOperatorWrappers,
- RecordWriterOutput<?>[] streamOutputs,
- WatermarkGaugeExposingOutput<StreamRecord<OUT>> mainOperatorOutput,
- StreamOperatorWrapper<OUT, OP> mainOperatorWrapper) {
- this.streamOutputs = streamOutputs;
- this.finishedOnRestoreInput = null;
- this.mainOperatorOutput = checkNotNull(mainOperatorOutput);
- this.operatorEventDispatcher = null;
-
- checkState(allOperatorWrappers != null && allOperatorWrappers.size() > 0);
- this.mainOperatorWrapper = checkNotNull(mainOperatorWrapper);
- this.tailOperatorWrapper = allOperatorWrappers.get(0);
- this.numOperators = allOperatorWrappers.size();
- this.chainedSources = Collections.emptyMap();
-
- firstOperatorWrapper = linkOperatorWrappers(allOperatorWrappers);
- }
-
- public abstract boolean isTaskDeployedAsFinished();
-
- public abstract void dispatchOperatorEvent(
- OperatorID operator, SerializedValue<OperatorEvent> event) throws FlinkException;
-
- public abstract void prepareSnapshotPreBarrier(long checkpointId) throws Exception;
-
- /**
- * Ends the main operator input specified by {@code inputId}).
- *
- * @param inputId the input ID starts from 1 which indicates the first input.
- */
- public abstract void endInput(int inputId) throws Exception;
-
- /**
- * Initialize state and open all operators in the chain from <b>tail to heads</b>, contrary to
- * {@link StreamOperator#close()} which happens <b>heads to tail</b> (see {@link
- * #finishOperators(StreamTaskActionExecutor, StopMode)}).
- */
- public abstract void initializeStateAndOpenOperators(
- StreamTaskStateInitializer streamTaskStateInitializer) throws Exception;
-
- /**
- * Closes all operators in a chain effect way. Closing happens from <b>heads to tail</b> operator
- * in the chain, contrary to {@link StreamOperator#open()} which happens <b>tail to heads</b> (see
- * {@link #initializeStateAndOpenOperators(StreamTaskStateInitializer)}).
- */
- public abstract void finishOperators(StreamTaskActionExecutor actionExecutor, StopMode stopMode)
- throws Exception;
-
- public abstract void notifyCheckpointComplete(long checkpointId) throws Exception;
-
- public abstract void notifyCheckpointAborted(long checkpointId) throws Exception;
-
- public abstract void notifyCheckpointSubsumed(long checkpointId) throws Exception;
-
- public abstract void snapshotState(
- Map<OperatorID, OperatorSnapshotFutures> operatorSnapshotsInProgress,
- CheckpointMetaData checkpointMetaData,
- CheckpointOptions checkpointOptions,
- Supplier<Boolean> isRunning,
- ChannelStateWriter.ChannelStateWriteResult channelStateWriteResult,
- CheckpointStreamFactory storage)
- throws Exception;
-
- public OperatorEventDispatcher getOperatorEventDispatcher() {
- return operatorEventDispatcher;
- }
-
- public void broadcastEvent(AbstractEvent event) throws IOException {
- broadcastEvent(event, false);
- }
-
- public void broadcastEvent(AbstractEvent event, boolean isPriorityEvent) throws IOException {
- for (RecordWriterOutput<?> streamOutput : streamOutputs) {
- streamOutput.broadcastEvent(event, isPriorityEvent);
- }
- }
-
- public void alignedBarrierTimeout(long checkpointId) throws IOException {
- for (RecordWriterOutput<?> streamOutput : streamOutputs) {
- streamOutput.alignedBarrierTimeout(checkpointId);
- }
- }
-
- public void abortCheckpoint(long checkpointId, CheckpointException cause) {
- for (RecordWriterOutput<?> streamOutput : streamOutputs) {
- streamOutput.abortCheckpoint(checkpointId, cause);
- }
- }
-
- /**
- * Execute {@link StreamOperator#close()} of each operator in the chain of this {@link
- * StreamTask}. Closing happens from <b>tail to head</b> operator in the chain.
- */
- public void closeAllOperators() throws Exception {
- isClosed = true;
- }
-
- public RecordWriterOutput<?>[] getStreamOutputs() {
- return streamOutputs;
- }
-
- /** Returns an {@link Iterable} which traverses all operators in forward topological order. */
- @VisibleForTesting
- public Iterable<StreamOperatorWrapper<?, ?>> getAllOperators() {
- return getAllOperators(false);
- }
-
- /**
- * Returns an {@link Iterable} which traverses all operators in forward or reverse topological
- * order.
- */
- protected Iterable<StreamOperatorWrapper<?, ?>> getAllOperators(boolean reverse) {
- return reverse
- ? new StreamOperatorWrapper.ReadIterator(tailOperatorWrapper, true)
- : new StreamOperatorWrapper.ReadIterator(mainOperatorWrapper, false);
- }
-
- public Input getFinishedOnRestoreInputOrDefault(Input defaultInput) {
- return finishedOnRestoreInput == null ? defaultInput : finishedOnRestoreInput;
- }
-
- public int getNumberOfOperators() {
- return numOperators;
- }
-
- public WatermarkGaugeExposingOutput<StreamRecord<OUT>> getMainOperatorOutput() {
- return mainOperatorOutput;
- }
-
- public ChainedSource getChainedSource(StreamConfig.SourceInputConfig sourceInput) {
- checkArgument(
- chainedSources.containsKey(sourceInput),
- "Chained source with sourcedId = [%s] was not found",
- sourceInput);
- return chainedSources.get(sourceInput);
- }
-
- public List<Output<StreamRecord<?>>> getChainedSourceOutputs() {
- return chainedSources.values().stream()
- .map(ChainedSource::getSourceOutput)
- .collect(Collectors.toList());
- }
-
- public StreamTaskSourceInput<?> getSourceTaskInput(StreamConfig.SourceInputConfig sourceInput) {
- checkArgument(
- chainedSources.containsKey(sourceInput),
- "Chained source with sourcedId = [%s] was not found",
- sourceInput);
- return chainedSources.get(sourceInput).getSourceTaskInput();
- }
-
- public List<StreamTaskSourceInput<?>> getSourceTaskInputs() {
- return chainedSources.values().stream()
- .map(ChainedSource::getSourceTaskInput)
- .collect(Collectors.toList());
- }
-
- /**
- * This method should be called before finishing the record emission, to make sure any data that
- * is still buffered will be sent. It also ensures that all data sending related exceptions are
- * recognized.
- *
- * @throws IOException Thrown, if the buffered data cannot be pushed into the output streams.
- */
- public void flushOutputs() throws IOException {
- for (RecordWriterOutput<?> streamOutput : getStreamOutputs()) {
- streamOutput.flush();
- }
- }
-
- /**
- * This method releases all resources of the record writer output. It stops the output flushing
- * thread (if there is one) and releases all buffers currently held by the output serializers.
- *
- * <p>This method should never fail.
- */
- public void close() throws IOException {
- closer.close();
- }
-
- @Nullable
- public OP getMainOperator() {
- return (mainOperatorWrapper == null) ? null : mainOperatorWrapper.getStreamOperator();
- }
-
- @Nullable
- protected StreamOperator<?> getTailOperator() {
- return (tailOperatorWrapper == null) ? null : tailOperatorWrapper.getStreamOperator();
- }
-
- protected void snapshotChannelStates(
- StreamOperator<?> op,
- ChannelStateWriter.ChannelStateWriteResult channelStateWriteResult,
- OperatorSnapshotFutures snapshotInProgress) {
- if (op == getMainOperator()) {
- snapshotInProgress.setInputChannelStateFuture(
- channelStateWriteResult
- .getInputChannelStateHandles()
- .thenApply(StateObjectCollection::new)
- .thenApply(SnapshotResult::of));
- }
- if (op == getTailOperator()) {
- snapshotInProgress.setResultSubpartitionStateFuture(
- channelStateWriteResult
- .getResultSubpartitionStateHandles()
- .thenApply(StateObjectCollection::new)
- .thenApply(SnapshotResult::of));
- }
- }
-
- public boolean isClosed() {
- return isClosed;
- }
-
- /** Wrapper class to access the chained sources and their's outputs. */
- public static class ChainedSource {
- private final WatermarkGaugeExposingOutput<StreamRecord<?>> chainedSourceOutput;
- private final StreamTaskSourceInput<?> sourceTaskInput;
-
- public ChainedSource(
- WatermarkGaugeExposingOutput<StreamRecord<?>> chainedSourceOutput,
- StreamTaskSourceInput<?> sourceTaskInput) {
- this.chainedSourceOutput = chainedSourceOutput;
- this.sourceTaskInput = sourceTaskInput;
- }
-
- public WatermarkGaugeExposingOutput<StreamRecord<?>> getSourceOutput() {
- return chainedSourceOutput;
- }
-
- public StreamTaskSourceInput<?> getSourceTaskInput() {
- return sourceTaskInput;
- }
- }
-
- // ------------------------------------------------------------------------
- // initialization utilities
- // ------------------------------------------------------------------------
-
- private void createChainOutputs(
- List<NonChainedOutput> outputsInOrder,
- RecordWriterDelegate<SerializationDelegate<StreamRecord<OUT>>> recordWriterDelegate,
- Map<Integer, StreamConfig> chainedConfigs,
- StreamTask<OUT, OP> containingTask,
- Map<IntermediateDataSetID, RecordWriterOutput<?>> recordWriterOutputs) {
- for (int i = 0; i < outputsInOrder.size(); ++i) {
- NonChainedOutput output = outputsInOrder.get(i);
-
- RecordWriterOutput<?> recordWriterOutput =
- createStreamOutput(
- recordWriterDelegate.getRecordWriter(i),
- output,
- chainedConfigs.get(output.getSourceNodeId()),
- containingTask.getEnvironment());
-
- this.streamOutputs[i] = recordWriterOutput;
- recordWriterOutputs.put(output.getDataSetId(), recordWriterOutput);
- }
- }
-
- private RecordWriterOutput<OUT> createStreamOutput(
- RecordWriter<SerializationDelegate<StreamRecord<OUT>>> recordWriter,
- NonChainedOutput streamOutput,
- StreamConfig upStreamConfig,
- Environment taskEnvironment) {
- OutputTag sideOutputTag =
- streamOutput.getOutputTag(); // OutputTag, return null if not sideOutput
-
- TypeSerializer outSerializer;
-
- if (streamOutput.getOutputTag() != null) {
- // side output
- outSerializer =
- upStreamConfig.getTypeSerializerSideOut(
- streamOutput.getOutputTag(),
- taskEnvironment.getUserCodeClassLoader().asClassLoader());
- } else {
- // main output
- outSerializer =
- upStreamConfig.getTypeSerializerOut(
- taskEnvironment.getUserCodeClassLoader().asClassLoader());
- }
-
- return closer.register(
- new RecordWriterOutput<OUT>(
- recordWriter,
- outSerializer,
- sideOutputTag,
- streamOutput.supportsUnalignedCheckpoints()));
- }
-
- @SuppressWarnings("rawtypes")
- private Map<StreamConfig.SourceInputConfig, ChainedSource> createChainedSources(
- StreamTask<OUT, OP> containingTask,
- StreamConfig.InputConfig[] configuredInputs,
- Map<Integer, StreamConfig> chainedConfigs,
- ClassLoader userCodeClassloader,
- List<StreamOperatorWrapper<?, ?>> allOpWrappers) {
- if (Arrays.stream(configuredInputs)
- .noneMatch(input -> input instanceof StreamConfig.SourceInputConfig)) {
- return Collections.emptyMap();
- }
- checkState(
- mainOperatorWrapper.getStreamOperator() instanceof MultipleInputStreamOperator,
- "Creating chained input is only supported with MultipleInputStreamOperator and MultipleInputStreamTask");
- Map<StreamConfig.SourceInputConfig, ChainedSource> chainedSourceInputs = new HashMap<>();
- MultipleInputStreamOperator<?> multipleInputOperator =
- (MultipleInputStreamOperator<?>) mainOperatorWrapper.getStreamOperator();
- List<Input> operatorInputs = multipleInputOperator.getInputs();
-
- int sourceInputGateIndex =
- Arrays.stream(containingTask.getEnvironment().getAllInputGates())
- .mapToInt(IndexedInputGate::getInputGateIndex)
- .max()
- .orElse(-1)
- + 1;
-
- for (int inputId = 0; inputId < configuredInputs.length; inputId++) {
- if (!(configuredInputs[inputId] instanceof StreamConfig.SourceInputConfig)) {
- continue;
- }
- StreamConfig.SourceInputConfig sourceInput =
- (StreamConfig.SourceInputConfig) configuredInputs[inputId];
- int sourceEdgeId = sourceInput.getInputEdge().getSourceId();
- StreamConfig sourceInputConfig = chainedConfigs.get(sourceEdgeId);
- OutputTag outputTag = sourceInput.getInputEdge().getOutputTag();
-
- WatermarkGaugeExposingOutput chainedSourceOutput =
- createChainedSourceOutput(
- containingTask,
- sourceInputConfig,
- userCodeClassloader,
- getFinishedOnRestoreInputOrDefault(operatorInputs.get(inputId)),
- multipleInputOperator.getMetricGroup(),
- outputTag);
-
- SourceOperator<?, ?> sourceOperator =
- (SourceOperator<?, ?>)
- createOperator(
- containingTask,
- sourceInputConfig,
- userCodeClassloader,
- (WatermarkGaugeExposingOutput<StreamRecord<OUT>>) chainedSourceOutput,
- allOpWrappers,
- true);
- chainedSourceInputs.put(
- sourceInput,
- new ChainedSource(
- chainedSourceOutput,
- this.isTaskDeployedAsFinished()
- ? new StreamTaskFinishedOnRestoreSourceInput<>(
- sourceOperator, sourceInputGateIndex++, inputId)
- : new StreamTaskSourceInput<>(sourceOperator, sourceInputGateIndex++, inputId)));
- }
- return chainedSourceInputs;
- }
-
- /**
- * Get the numRecordsOut counter for the operator represented by the given config. And re-use the
- * operator-level counter for the task-level numRecordsOut counter if this operator is at the end
- * of the operator chain.
- *
- * <p>Return null if we should not use the numRecordsOut counter to track the records emitted by
- * this operator.
- */
- @Nullable
- private Counter getOperatorRecordsOutCounter(
- StreamTask<?, ?> containingTask, StreamConfig operatorConfig) {
- ClassLoader userCodeClassloader = containingTask.getUserCodeClassLoader();
- Class<StreamOperatorFactory<?>> streamOperatorFactoryClass =
- operatorConfig.getStreamOperatorFactoryClass(userCodeClassloader);
-
- // Do not use the numRecordsOut counter on output if this operator is SinkWriterOperator.
- //
- // Metric "numRecordsOut" is defined as the total number of records written to the
- // external system in FLIP-33, but this metric is occupied in AbstractStreamOperator as the
- // number of records sent to downstream operators, which is number of Committable batches
- // sent to SinkCommitter. So we skip registering this metric on output and leave this metric
- // to sink writer implementations to report.
- try {
- Class<?> sinkWriterFactoryClass =
- userCodeClassloader.loadClass(SinkWriterOperatorFactory.class.getName());
- if (sinkWriterFactoryClass.isAssignableFrom(streamOperatorFactoryClass)) {
- return null;
- }
- } catch (ClassNotFoundException e) {
- throw new StreamTaskException(
- "Could not load SinkWriterOperatorFactory class from userCodeClassloader.", e);
- }
-
- InternalOperatorMetricGroup operatorMetricGroup =
- containingTask
- .getEnvironment()
- .getMetricGroup()
- .getOrAddOperator(operatorConfig.getOperatorID(), operatorConfig.getOperatorName());
-
- return operatorMetricGroup.getIOMetricGroup().getNumRecordsOutCounter();
- }
-
- @SuppressWarnings({"rawtypes", "unchecked"})
- private WatermarkGaugeExposingOutput<StreamRecord> createChainedSourceOutput(
- StreamTask<?, OP> containingTask,
- StreamConfig sourceInputConfig,
- ClassLoader userCodeClassloader,
- Input input,
- OperatorMetricGroup metricGroup,
- OutputTag outputTag) {
-
- Counter recordsOutCounter = getOperatorRecordsOutCounter(containingTask, sourceInputConfig);
-
- WatermarkGaugeExposingOutput<StreamRecord> chainedSourceOutput;
- if (containingTask.getExecutionConfig().isObjectReuseEnabled()) {
- chainedSourceOutput = new ChainingOutput(input, recordsOutCounter, metricGroup, outputTag);
- } else {
- TypeSerializer<?> inSerializer = sourceInputConfig.getTypeSerializerOut(userCodeClassloader);
- chainedSourceOutput =
- new CopyingChainingOutput(input, inSerializer, recordsOutCounter, metricGroup, outputTag);
- }
- /**
- * Chained sources are closed when {@link
- * org.apache.flink.streaming.runtime.io.StreamTaskSourceInput} are being closed.
- */
- return closer.register(chainedSourceOutput);
- }
-
- private <T> WatermarkGaugeExposingOutput<StreamRecord<T>> createOutputCollector(
- StreamTask<?, ?> containingTask,
- StreamConfig operatorConfig,
- Map<Integer, StreamConfig> chainedConfigs,
- ClassLoader userCodeClassloader,
- Map<IntermediateDataSetID, RecordWriterOutput<?>> recordWriterOutputs,
- List<StreamOperatorWrapper<?, ?>> allOperatorWrappers,
- MailboxExecutorFactory mailboxExecutorFactory,
- boolean shouldAddMetric) {
- // --- Begin Gluten-specific code changes ---
- List<OutputWithChainingCheck<StreamRecord<T>>> allOutputs = new ArrayList<>(4);
- Map<String, OutputWithChainingCheck<StreamRecord<T>>> glutenOutputs = new HashMap<>();
-
- Map<IntermediateDataSetID, String> node2outputs =
- Utils.getNodeToNonChainedOutputs(operatorConfig, userCodeClassloader);
- // create collectors for the network outputs
- for (NonChainedOutput streamOutput :
- operatorConfig.getOperatorNonChainedOutputs(userCodeClassloader)) {
- @SuppressWarnings("unchecked")
- RecordWriterOutput<T> recordWriterOutput =
- (RecordWriterOutput<T>) recordWriterOutputs.get(streamOutput.getDataSetId());
-
- allOutputs.add(recordWriterOutput);
- glutenOutputs.put(node2outputs.get(streamOutput.getDataSetId()), recordWriterOutput);
- }
-
- // Create collectors for the chained outputs
- for (StreamEdge outputEdge : operatorConfig.getChainedOutputs(userCodeClassloader)) {
- int outputId = outputEdge.getTargetId();
- StreamConfig chainedOpConfig = chainedConfigs.get(outputId);
-
- WatermarkGaugeExposingOutput<StreamRecord<T>> output =
- createOperatorChain(
- containingTask,
- operatorConfig,
- chainedOpConfig,
- chainedConfigs,
- userCodeClassloader,
- recordWriterOutputs,
- allOperatorWrappers,
- outputEdge.getOutputTag(),
- mailboxExecutorFactory,
- shouldAddMetric);
- checkState(output instanceof OutputWithChainingCheck);
- allOutputs.add((OutputWithChainingCheck) output);
-
- // If the operator has multiple downstream chained operators, only one of them should
- // increment the recordsOutCounter for this operator. Set shouldAddMetric to false
- // so that we would skip adding the counter to other downstream operators.
- shouldAddMetric = false;
- }
-
- WatermarkGaugeExposingOutput<StreamRecord<T>> result;
-
- if (allOutputs.size() == 1) {
- result = allOutputs.get(0);
- // only if this is a single RecordWriterOutput, reuse its numRecordOut for task.
- if (result instanceof RecordWriterOutput) {
- Counter numRecordsOutCounter = createNumRecordsOutCounter(containingTask);
- ((RecordWriterOutput<T>) result).setNumRecordsOut(numRecordsOutCounter);
- }
- } else {
- if (glutenOutputs.size() > 0 && allOutputs.size() != glutenOutputs.size()) {
- throw new RuntimeException("Number of outputs and gluten outputs do not match.");
- }
- // TODO: add counter
- result = closer.register(new GlutenOutputCollector<>(glutenOutputs, null));
- }
- // --- End Gluten-specific code changes ---
-
- if (shouldAddMetric) {
- // Create a CountingOutput to increment the recordsOutCounter for this operator
- // if we have not added the counter to any downstream chained operator.
- Counter recordsOutCounter = getOperatorRecordsOutCounter(containingTask, operatorConfig);
- if (recordsOutCounter != null) {
- result = new CountingOutput<>(result, recordsOutCounter);
- }
- }
- return result;
- }
-
- private static Counter createNumRecordsOutCounter(StreamTask<?, ?> containingTask) {
- TaskIOMetricGroup taskIOMetricGroup =
- containingTask.getEnvironment().getMetricGroup().getIOMetricGroup();
- Counter counter = new SimpleCounter();
- taskIOMetricGroup.reuseRecordsOutputCounter(counter);
- return counter;
- }
-
- /**
- * Recursively create chain of operators that starts from the given {@param operatorConfig}.
- * Operators are created tail to head and wrapped into an {@link WatermarkGaugeExposingOutput}.
- */
- private <IN, OUT> WatermarkGaugeExposingOutput<StreamRecord<IN>> createOperatorChain(
- StreamTask<OUT, ?> containingTask,
- StreamConfig prevOperatorConfig,
- StreamConfig operatorConfig,
- Map<Integer, StreamConfig> chainedConfigs,
- ClassLoader userCodeClassloader,
- Map<IntermediateDataSetID, RecordWriterOutput<?>> recordWriterOutputs,
- List<StreamOperatorWrapper<?, ?>> allOperatorWrappers,
- OutputTag<IN> outputTag,
- MailboxExecutorFactory mailboxExecutorFactory,
- boolean shouldAddMetricForPrevOperator) {
- // create the output that the operator writes to first. this may recursively create more
- // operators
- WatermarkGaugeExposingOutput<StreamRecord<OUT>> chainedOperatorOutput =
- createOutputCollector(
- containingTask,
- operatorConfig,
- chainedConfigs,
- userCodeClassloader,
- recordWriterOutputs,
- allOperatorWrappers,
- mailboxExecutorFactory,
- true);
-
- OneInputStreamOperator<IN, OUT> chainedOperator =
- createOperator(
- containingTask,
- operatorConfig,
- userCodeClassloader,
- chainedOperatorOutput,
- allOperatorWrappers,
- false);
-
- return wrapOperatorIntoOutput(
- chainedOperator,
- containingTask,
- prevOperatorConfig,
- operatorConfig,
- userCodeClassloader,
- outputTag,
- shouldAddMetricForPrevOperator);
- }
-
- /**
- * Create and return a single operator from the given {@param operatorConfig} that will be
- * producing records to the {@param output}.
- */
- private <OUT, OP extends StreamOperator<OUT>> OP createOperator(
- StreamTask<OUT, ?> containingTask,
- StreamConfig operatorConfig,
- ClassLoader userCodeClassloader,
- WatermarkGaugeExposingOutput<StreamRecord<OUT>> output,
- List<StreamOperatorWrapper<?, ?>> allOperatorWrappers,
- boolean isHead) {
-
- // now create the operator and give it the output collector to write its output to
- Tuple2<OP, Optional<ProcessingTimeService>> chainedOperatorAndTimeService =
- StreamOperatorFactoryUtil.createOperator(
- operatorConfig.getStreamOperatorFactory(userCodeClassloader),
- containingTask,
- operatorConfig,
- output,
- operatorEventDispatcher);
-
- OP chainedOperator = chainedOperatorAndTimeService.f0;
- allOperatorWrappers.add(
- createOperatorWrapper(
- chainedOperator,
- containingTask,
- operatorConfig,
- chainedOperatorAndTimeService.f1,
- isHead));
-
- chainedOperator
- .getMetricGroup()
- .gauge(MetricNames.IO_CURRENT_OUTPUT_WATERMARK, output.getWatermarkGauge()::getValue);
- return chainedOperator;
- }
-
- private <IN, OUT> WatermarkGaugeExposingOutput<StreamRecord<IN>> wrapOperatorIntoOutput(
- OneInputStreamOperator<IN, OUT> operator,
- StreamTask<OUT, ?> containingTask,
- StreamConfig prevOperatorConfig,
- StreamConfig operatorConfig,
- ClassLoader userCodeClassloader,
- OutputTag<IN> outputTag,
- boolean shouldAddMetricForPrevOperator) {
-
- Counter recordsOutCounter = null;
-
- if (shouldAddMetricForPrevOperator) {
- recordsOutCounter = getOperatorRecordsOutCounter(containingTask, prevOperatorConfig);
- }
-
- WatermarkGaugeExposingOutput<StreamRecord<IN>> currentOperatorOutput;
- if (containingTask.getExecutionConfig().isObjectReuseEnabled()) {
- currentOperatorOutput =
- new ChainingOutput<>(operator, recordsOutCounter, operator.getMetricGroup(), outputTag);
- } else {
- TypeSerializer<IN> inSerializer = operatorConfig.getTypeSerializerIn1(userCodeClassloader);
- currentOperatorOutput =
- new CopyingChainingOutput<>(
- operator, inSerializer, recordsOutCounter, operator.getMetricGroup(), outputTag);
- }
-
- // wrap watermark gauges since registered metrics must be unique
- operator
- .getMetricGroup()
- .gauge(
- MetricNames.IO_CURRENT_INPUT_WATERMARK,
- currentOperatorOutput.getWatermarkGauge()::getValue);
-
- return closer.register(currentOperatorOutput);
- }
-
- /**
- * Links operator wrappers in forward topological order.
- *
- * @param allOperatorWrappers is an operator wrapper list of reverse topological order
- */
- private StreamOperatorWrapper<?, ?> linkOperatorWrappers(
- List<StreamOperatorWrapper<?, ?>> allOperatorWrappers) {
- StreamOperatorWrapper<?, ?> previous = null;
- for (StreamOperatorWrapper<?, ?> current : allOperatorWrappers) {
- if (previous != null) {
- previous.setPrevious(current);
- }
- current.setNext(previous);
- previous = current;
- }
- return previous;
- }
-
- private <T, P extends StreamOperator<T>> StreamOperatorWrapper<T, P> createOperatorWrapper(
- P operator,
- StreamTask<?, ?> containingTask,
- StreamConfig operatorConfig,
- Optional<ProcessingTimeService> processingTimeService,
- boolean isHead) {
- return new StreamOperatorWrapper<>(
- operator,
- processingTimeService,
- containingTask.getMailboxExecutorFactory().createExecutor(operatorConfig.getChainIndex()),
- isHead);
- }
-
- protected void sendAcknowledgeCheckpointEvent(long checkpointId) {
- if (operatorEventDispatcher == null) {
- return;
- }
-
- operatorEventDispatcher
- .getRegisteredOperators()
- .forEach(
- x ->
- operatorEventDispatcher
- .getOperatorEventGateway(x)
- .sendEventToCoordinator(new AcknowledgeCheckpointEvent(checkpointId)));
- }
-}
diff --git a/gluten-flink/runtime/src/main/java/org/apache/gluten/client/OffloadedJobGraphGenerator.java b/gluten-flink/runtime/src/main/java/org/apache/gluten/client/OffloadedJobGraphGenerator.java
new file mode 100644
index 0000000000..622777eebe
--- /dev/null
+++ b/gluten-flink/runtime/src/main/java/org/apache/gluten/client/OffloadedJobGraphGenerator.java
@@ -0,0 +1,605 @@
+/*
+ * 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.gluten.client;
+
+import org.apache.gluten.streaming.api.operators.GlutenOperator;
+import org.apache.gluten.streaming.api.operators.GlutenStreamSource;
+import org.apache.gluten.table.runtime.keyselector.GlutenKeySelector;
+import org.apache.gluten.table.runtime.operators.GlutenOneInputOperator;
+import org.apache.gluten.table.runtime.operators.GlutenSourceFunction;
+import org.apache.gluten.table.runtime.operators.GlutenTwoInputOperator;
+import org.apache.gluten.table.runtime.typeutils.GlutenStatefulRecordSerializer;
+import org.apache.gluten.util.Utils;
+
+import io.github.zhztheplayer.velox4j.plan.StatefulPlanNode;
+import io.github.zhztheplayer.velox4j.stateful.StatefulRecord;
+import io.github.zhztheplayer.velox4j.type.RowType;
+
+import org.apache.flink.api.java.functions.KeySelector;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.runtime.jobgraph.IntermediateDataSet;
+import org.apache.flink.runtime.jobgraph.JobEdge;
+import org.apache.flink.runtime.jobgraph.JobGraph;
+import org.apache.flink.runtime.jobgraph.JobVertex;
+import org.apache.flink.streaming.api.graph.StreamConfig;
+import org.apache.flink.streaming.api.graph.StreamEdge;
+import org.apache.flink.streaming.api.graph.StreamNode;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
+import org.apache.flink.table.data.RowData;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+/*
+ * Generates an offloaded JobGraph by transforming offloadable operators to Gluten operators.
+ * Main workflow:
+ * 1. For each JobVertex, generate an OperatorChainSliceGraph to identify offloadable slices.
+ * 2. Recursively visit chain slices: create offloaded operators for offloadable slices,
+ * keep original operators for unoffloadable ones.
+ * 3. For offloadable operators: update input/output serializers and state partitioners
+ * based on upstream/downstream operator capabilities.
+ * 4. Update stream edges and serialize all operator configurations.
+ */
+public class OffloadedJobGraphGenerator {
+ private static final Logger LOG = LoggerFactory.getLogger(OffloadedJobGraphGenerator.class);
+ private final JobGraph jobGraph;
+ private final ClassLoader userClassloader;
+ private boolean hasGenerated = false;
+
+ public OffloadedJobGraphGenerator(JobGraph jobGraph, ClassLoader userClassloader) {
+ this.jobGraph = jobGraph;
+ this.userClassloader = userClassloader;
+ }
+
+ public JobGraph generate() {
+ if (hasGenerated) {
+ throw new IllegalStateException("JobGraph has been generated.");
+ }
+
+ for (JobVertex jobVertex : jobGraph.getVertices()) {
+ offloadJobVertex(jobVertex);
+ }
+ hasGenerated = true;
+ return jobGraph;
+ }
+
+ private void offloadJobVertex(JobVertex jobVertex) {
+ OperatorChainSliceGraphGenerator graphGenerator =
+ new OperatorChainSliceGraphGenerator(jobVertex, userClassloader);
+ OperatorChainSliceGraph chainSliceGraph = graphGenerator.getGraph();
+ LOG.info("OperatorChainSliceGraph:\n{}", chainSliceGraph);
+
+ OperatorChainSlice sourceChainSlice = chainSliceGraph.getSourceSlice();
+ OperatorChainSliceGraph offloadedChainSliceGraph = new OperatorChainSliceGraph();
+ visitAndOffloadChainOperators(
+ sourceChainSlice, chainSliceGraph, offloadedChainSliceGraph, 0, jobVertex);
+ visitAndUpdateStreamEdges(sourceChainSlice, chainSliceGraph, offloadedChainSliceGraph);
+ serializeAllOperatorsConfigs(offloadedChainSliceGraph);
+
+ StreamConfig sourceConfig = sourceChainSlice.getOperatorConfigs().get(0);
+ StreamConfig offloadedSourceConfig =
+ offloadedChainSliceGraph.getSlice(sourceChainSlice.id()).getOperatorConfigs().get(0);
+
+ Map<Integer, StreamConfig> chainedConfigs =
+ collectChainedConfigs(sourceChainSlice, offloadedChainSliceGraph);
+ updateSourceConfigIfOffloadable(
+ sourceConfig, offloadedSourceConfig, sourceChainSlice, chainedConfigs);
+ sourceConfig.setAndSerializeTransitiveChainedTaskConfigs(chainedConfigs);
+ sourceConfig.serializeAllConfigs();
+ }
+
+ // Process and offload operator chain slices recursively
+ private void visitAndOffloadChainOperators(
+ OperatorChainSlice sourceChainSlice,
+ OperatorChainSliceGraph sourceChainSliceGraph,
+ OperatorChainSliceGraph offloadedChainSliceGraph,
+ Integer chainIndex,
+ JobVertex jobVertex) {
+ OperatorChainSlice processedChainSlice;
+ if (sourceChainSlice.isOffloadable()) {
+ processedChainSlice =
+ createOffloadedOperatorChainSlice(
+ sourceChainSliceGraph, sourceChainSlice, chainIndex, jobVertex);
+ chainIndex = chainIndex + 1;
+ } else {
+ processedChainSlice = createUnoffloadableOperatorChainSlice(sourceChainSlice, chainIndex);
+ chainIndex = chainIndex + sourceChainSlice.getOperatorConfigs().size();
+ }
+
+ processedChainSlice.getInputs().addAll(sourceChainSlice.getInputs());
+ processedChainSlice.getOutputs().addAll(sourceChainSlice.getOutputs());
+ offloadedChainSliceGraph.addSlice(sourceChainSlice.id(), processedChainSlice);
+
+ // Recursively process downstream chain slices
+ for (Integer downstreamSliceId : sourceChainSlice.getOutputs()) {
+ OperatorChainSlice downstreamSourceSlice = sourceChainSliceGraph.getSlice(downstreamSliceId);
+ OperatorChainSlice downstreamProcessedSlice =
+ offloadedChainSliceGraph.getSlice(downstreamSliceId);
+ if (downstreamProcessedSlice == null) {
+ visitAndOffloadChainOperators(
+ downstreamSourceSlice,
+ sourceChainSliceGraph,
+ offloadedChainSliceGraph,
+ chainIndex,
+ jobVertex);
+ }
+ }
+ }
+
+ // Keep the original operator chain slice as is.
+ private OperatorChainSlice createUnoffloadableOperatorChainSlice(
+ OperatorChainSlice sourceChainSlice, Integer chainIndex) {
+ OperatorChainSlice unoffloadableChainSlice = new OperatorChainSlice(sourceChainSlice.id());
+ List<StreamConfig> operatorConfigs = sourceChainSlice.getOperatorConfigs();
+ for (StreamConfig opConfig : operatorConfigs) {
+ StreamConfig newOpConfig = new StreamConfig(new Configuration(opConfig.getConfiguration()));
+ newOpConfig.setChainIndex(chainIndex);
+ unoffloadableChainSlice.getOperatorConfigs().add(newOpConfig);
+ }
+ unoffloadableChainSlice.setOffloadable(false);
+ return unoffloadableChainSlice;
+ }
+
+ // Create offloadable operator chain slice, and update the input/output channel serializers
+ private OperatorChainSlice createOffloadedOperatorChainSlice(
+ OperatorChainSliceGraph chainSliceGraph,
+ OperatorChainSlice sourceChainSlice,
+ Integer chainIndex,
+ JobVertex jobVertex) {
+ OperatorChainSlice offloadedChainSlice = new OperatorChainSlice(sourceChainSlice.id());
+ List<StreamConfig> operatorConfigs = sourceChainSlice.getOperatorConfigs();
+
+ // May coalesce multiple operators into one in the future.
+ if (operatorConfigs.size() != 1) {
+ throw new UnsupportedOperationException(
+ "Only one operator is supported for offloaded operator chain slice.");
+ }
+
+ StreamConfig sourceOpConfig = operatorConfigs.get(0);
+ GlutenOperator sourceOperator = Utils.getGlutenOperator(sourceOpConfig, userClassloader).get();
+ StatefulPlanNode planNode = sourceOperator.getPlanNode();
+ // Create a new operator config for the offloaded operator.
+ StreamConfig offloadedOpConfig =
+ new StreamConfig(new Configuration(sourceOpConfig.getConfiguration()));
+ if (sourceOperator instanceof GlutenStreamSource) {
+ boolean supportsVectorOutput =
+ supportsVectorOutput(sourceChainSlice, chainSliceGraph, jobVertex);
+ Class<?> outClass = supportsVectorOutput ? StatefulRecord.class : RowData.class;
+ GlutenStreamSource newSourceOp =
+ new GlutenStreamSource(
+ new GlutenSourceFunction<>(
+ planNode,
+ sourceOperator.getOutputTypes(),
+ sourceOperator.getId(),
+ ((GlutenStreamSource) sourceOperator).getConnectorSplit(),
+ outClass));
+ offloadedOpConfig.setStreamOperator(newSourceOp);
+ if (supportsVectorOutput) {
+ setOffloadedOutputSerializer(offloadedOpConfig, sourceOperator);
+ }
+ } else if (sourceOperator instanceof GlutenOneInputOperator) {
+ createOffloadedOneInputOperator(
+ sourceChainSlice,
+ chainSliceGraph,
+ jobVertex,
+ planNode,
+ (GlutenOneInputOperator<?, ?>) sourceOperator,
+ sourceOpConfig,
+ offloadedOpConfig);
+ } else if (sourceOperator instanceof GlutenTwoInputOperator) {
+ createOffloadedTwoInputOperator(
+ sourceChainSlice,
+ chainSliceGraph,
+ jobVertex,
+ planNode,
+ (GlutenTwoInputOperator<?, ?>) sourceOperator,
+ sourceOpConfig,
+ offloadedOpConfig);
+ } else {
+ throw new UnsupportedOperationException(
+ "Unsupported operator type for offloading: " + sourceOperator.getClass().getName());
+ }
+
+ offloadedOpConfig.setChainIndex(chainIndex);
+ offloadedChainSlice.getOperatorConfigs().add(offloadedOpConfig);
+ offloadedChainSlice.setOffloadable(true);
+ return offloadedChainSlice;
+ }
+
+ private void createOffloadedOneInputOperator(
+ OperatorChainSlice sourceChainSlice,
+ OperatorChainSliceGraph chainSliceGraph,
+ JobVertex jobVertex,
+ StatefulPlanNode planNode,
+ GlutenOneInputOperator<?, ?> sourceOperator,
+ StreamConfig sourceOpConfig,
+ StreamConfig offloadedOpConfig) {
+ boolean supportsVectorOutput =
+ supportsVectorOutput(sourceChainSlice, chainSliceGraph, jobVertex);
+ boolean supportsVectorInput = supportsVectorInput(sourceChainSlice, chainSliceGraph, jobVertex);
+ Class<?> inClass = supportsVectorInput ? StatefulRecord.class : RowData.class;
+ Class<?> outClass = supportsVectorOutput ? StatefulRecord.class : RowData.class;
+ GlutenOneInputOperator<?, ?> newOneInputOp =
+ new GlutenOneInputOperator<>(
+ planNode,
+ sourceOperator.getId(),
+ sourceOperator.getInputType(),
+ sourceOperator.getOutputTypes(),
+ inClass,
+ outClass,
+ sourceOperator.getDescription());
+ offloadedOpConfig.setStreamOperator(newOneInputOp);
+ if (supportsVectorOutput) {
+ setOffloadedOutputSerializer(offloadedOpConfig, sourceOperator);
+ }
+ if (supportsVectorInput) {
+ setOffloadedInputSerializer(offloadedOpConfig, sourceOperator);
+ setOffloadedStatePartitioner(
+ sourceOpConfig, offloadedOpConfig, 0, sourceOperator.getDescription());
+ }
+ }
+
+ private void createOffloadedTwoInputOperator(
+ OperatorChainSlice sourceChainSlice,
+ OperatorChainSliceGraph chainSliceGraph,
+ JobVertex jobVertex,
+ StatefulPlanNode planNode,
+ GlutenTwoInputOperator<?, ?> sourceOperator,
+ StreamConfig sourceOpConfig,
+ StreamConfig offloadedOpConfig) {
+ boolean supportsVectorOutput =
+ supportsVectorOutput(sourceChainSlice, chainSliceGraph, jobVertex);
+ boolean supportsVectorInput = supportsVectorInput(sourceChainSlice, chainSliceGraph, jobVertex);
+ setOffloadedStatePartitioner(
+ sourceOpConfig, offloadedOpConfig, 0, sourceOperator.getDescription());
+ setOffloadedStatePartitioner(
+ sourceOpConfig, offloadedOpConfig, 1, sourceOperator.getDescription());
+ Class<?> inClass = supportsVectorInput ? StatefulRecord.class : RowData.class;
+ Class<?> outClass = supportsVectorOutput ? StatefulRecord.class : RowData.class;
+ GlutenTwoInputOperator<?, ?> newTwoInputOp =
+ new GlutenTwoInputOperator<>(
+ planNode,
+ sourceOperator.getLeftId(),
+ sourceOperator.getRightId(),
+ sourceOperator.getLeftInputType(),
+ sourceOperator.getRightInputType(),
+ sourceOperator.getOutputTypes(),
+ inClass,
+ outClass);
+ offloadedOpConfig.setStreamOperator(newTwoInputOp);
+ offloadedOpConfig.setStatePartitioner(0, new GlutenKeySelector());
+ offloadedOpConfig.setStatePartitioner(1, new GlutenKeySelector());
+ if (supportsVectorOutput) {
+ setOffloadedOutputSerializer(offloadedOpConfig, sourceOperator);
+ }
+ if (supportsVectorInput) {
+ setOffloadedInputSerializersForTwoInputOperator(offloadedOpConfig, sourceOperator);
+ }
+ }
+
+ private void setOffloadedOutputSerializer(StreamConfig opConfig, GlutenOperator operator) {
+ RowType rowType = operator.getOutputTypes().entrySet().iterator().next().getValue();
+ opConfig.setTypeSerializerOut(new GlutenStatefulRecordSerializer(rowType, operator.getId()));
+ }
+
+ private void setOffloadedInputSerializer(StreamConfig opConfig, GlutenOperator operator) {
+ opConfig.setupNetworkInputs(
+ new GlutenStatefulRecordSerializer(operator.getInputType(), operator.getId()));
+ }
+
+ private void setOffloadedInputSerializersForTwoInputOperator(
+ StreamConfig opConfig, GlutenTwoInputOperator<?, ?> operator) {
+ opConfig.setupNetworkInputs(
+ new GlutenStatefulRecordSerializer(operator.getLeftInputType(), operator.getId()),
+ new GlutenStatefulRecordSerializer(operator.getRightInputType(), operator.getId()));
+ }
+
+ private void setOffloadedStatePartitioner(
+ StreamConfig sourceOpConfig,
+ StreamConfig offloadedOpConfig,
+ int inputIndex,
+ String operatorDescription) {
+ KeySelector<?, ?> keySelector = sourceOpConfig.getStatePartitioner(inputIndex, userClassloader);
+ if (keySelector != null) {
+ LOG.info(
+ "State partitioner ({}) found in input {} of operator {}, change it to GlutenKeySelector.",
+ keySelector.getClass().getName(),
+ inputIndex,
+ operatorDescription);
+ offloadedOpConfig.setStatePartitioner(inputIndex, new GlutenKeySelector());
+ }
+ }
+
+ private StreamConfig findLastOperatorInChain(JobVertex vertex) {
+ StreamConfig rootStreamConfig = new StreamConfig(vertex.getConfiguration());
+ Map<Integer, StreamConfig> chainedConfigs =
+ rootStreamConfig.getTransitiveChainedTaskConfigs(userClassloader);
+ chainedConfigs.put(rootStreamConfig.getVertexID(), rootStreamConfig);
+
+ // Find the last operator (the one with no chained outputs)
+ for (StreamConfig config : chainedConfigs.values()) {
+ List<StreamEdge> chainedOutputs = config.getChainedOutputs(userClassloader);
+ if (chainedOutputs == null || chainedOutputs.isEmpty()) {
+ return config;
+ }
+ }
+
+ // If no last operator found, use the root config
+ return rootStreamConfig;
+ }
+
+ private StreamNode mockStreamNode(StreamConfig streamConfig) {
+ return new StreamNode(
+ streamConfig.getVertexID(),
+ null,
+ null,
+ (StreamOperatorFactory<?>) streamConfig.getStreamOperatorFactory(userClassloader),
+ streamConfig.getOperatorName(),
+ null);
+ }
+
+ // Update stream edges when vertices have been changed due to offloading
+ private void visitAndUpdateStreamEdges(
+ OperatorChainSlice sourceChainSlice,
+ OperatorChainSliceGraph sourceChainSliceGraph,
+ OperatorChainSliceGraph offloadedChainSliceGraph) {
+ OperatorChainSlice offloadedChainSlice =
+ offloadedChainSliceGraph.getSlice(sourceChainSlice.id());
+ if (offloadedChainSlice.isOffloadable()) {
+ updateStreamEdgesForOffloadedSlice(
+ sourceChainSlice, sourceChainSliceGraph, offloadedChainSliceGraph, offloadedChainSlice);
+ }
+
+ // Recursively update downstream chain slices
+ for (Integer downstreamSliceId : sourceChainSlice.getOutputs()) {
+ visitAndUpdateStreamEdges(
+ sourceChainSliceGraph.getSlice(downstreamSliceId),
+ sourceChainSliceGraph,
+ offloadedChainSliceGraph);
+ }
+ }
+
+ private void updateStreamEdgesForOffloadedSlice(
+ OperatorChainSlice sourceChainSlice,
+ OperatorChainSliceGraph sourceChainSliceGraph,
+ OperatorChainSliceGraph offloadedChainSliceGraph,
+ OperatorChainSlice offloadedChainSlice) {
+ List<Integer> downstreamSliceIds = sourceChainSlice.getOutputs();
+ if (downstreamSliceIds.isEmpty()) {
+ StreamConfig offloadedOpConfig = offloadedChainSlice.getOperatorConfigs().get(0);
+ offloadedOpConfig.setChainedOutputs(new ArrayList<>());
+ return;
+ }
+
+ List<StreamEdge> newOutputEdges = new ArrayList<>();
+ List<StreamConfig> sourceOperatorConfigs = sourceChainSlice.getOperatorConfigs();
+ StreamConfig lastSourceOpConfig = sourceOperatorConfigs.get(sourceOperatorConfigs.size() - 1);
+ List<StreamEdge> originalOutputEdges = lastSourceOpConfig.getChainedOutputs(userClassloader);
+
+ for (int i = 0; i < downstreamSliceIds.size(); i++) {
+ Integer downstreamSliceId = downstreamSliceIds.get(i);
+ OperatorChainSlice downstreamOffloadedSlice =
+ offloadedChainSliceGraph.getSlice(downstreamSliceId);
+ StreamConfig downstreamOpConfig = downstreamOffloadedSlice.getOperatorConfigs().get(0);
+ StreamEdge originalEdge = originalOutputEdges.get(i);
+ StreamEdge newEdge = createStreamEdge(originalEdge, downstreamOpConfig, offloadedChainSlice);
+ newOutputEdges.add(newEdge);
+ }
+
+ StreamConfig offloadedOpConfig = offloadedChainSlice.getOperatorConfigs().get(0);
+ offloadedOpConfig.setChainedOutputs(newOutputEdges);
+ }
+
+ private StreamEdge createStreamEdge(
+ StreamEdge originalEdge,
+ StreamConfig downstreamOpConfig,
+ OperatorChainSlice offloadedChainSlice) {
+ StreamConfig sourceOpConfig = offloadedChainSlice.getOperatorConfigs().get(0);
+ return new StreamEdge(
+ mockStreamNode(sourceOpConfig),
+ mockStreamNode(downstreamOpConfig),
+ originalEdge.getTypeNumber(),
+ originalEdge.getBufferTimeout(),
+ originalEdge.getPartitioner(),
+ originalEdge.getOutputTag(),
+ originalEdge.getExchangeMode(),
+ 0, // default value
+ originalEdge.getIntermediateDatasetIdToProduce());
+ }
+
+ private void serializeAllOperatorsConfigs(OperatorChainSliceGraph chainSliceGraph) {
+ for (OperatorChainSlice chainSlice : chainSliceGraph.getSlices().values()) {
+ for (StreamConfig opConfig : chainSlice.getOperatorConfigs()) {
+ opConfig.serializeAllConfigs();
+ }
+ }
+ }
+
+ private Map<Integer, StreamConfig> collectChainedConfigs(
+ OperatorChainSlice sourceChainSlice, OperatorChainSliceGraph offloadedChainSliceGraph) {
+ Map<Integer, StreamConfig> chainedConfigs = new HashMap<>();
+ if (!sourceChainSlice.isOffloadable()) {
+ List<StreamConfig> operatorConfigs = sourceChainSlice.getOperatorConfigs();
+ for (int i = 1; i < operatorConfigs.size(); i++) {
+ StreamConfig opConfig = operatorConfigs.get(i);
+ chainedConfigs.put(opConfig.getVertexID(), opConfig);
+ }
+ }
+ for (OperatorChainSlice chainSlice : offloadedChainSliceGraph.getSlices().values()) {
+ if (chainSlice.id().equals(sourceChainSlice.id())) {
+ continue;
+ }
+ for (StreamConfig opConfig : chainSlice.getOperatorConfigs()) {
+ chainedConfigs.put(opConfig.getVertexID(), opConfig);
+ }
+ }
+ return chainedConfigs;
+ }
+
+ private void updateSourceConfigIfOffloadable(
+ StreamConfig sourceConfig,
+ StreamConfig offloadedSourceConfig,
+ OperatorChainSlice sourceChainSlice,
+ Map<Integer, StreamConfig> chainedConfigs) {
+ if (sourceChainSlice.isOffloadable()) {
+ // Update the first operator config
+ sourceConfig.setStreamOperatorFactory(
+ offloadedSourceConfig.getStreamOperatorFactory(userClassloader));
+ sourceConfig.setChainedOutputs(offloadedSourceConfig.getChainedOutputs(userClassloader));
+ sourceConfig.setTypeSerializerOut(
+ offloadedSourceConfig.getTypeSerializerOut(userClassloader));
+ sourceConfig.setInputs(offloadedSourceConfig.getInputs(userClassloader));
+ updateStatePartitioners(sourceConfig, offloadedSourceConfig);
+ }
+ }
+
+ private void updateStatePartitioners(StreamConfig sourceConfig, StreamConfig offloadedConfig) {
+ KeySelector<?, ?> keySelector0 = offloadedConfig.getStatePartitioner(0, userClassloader);
+ if (keySelector0 != null) {
+ sourceConfig.setStatePartitioner(0, keySelector0);
+ }
+ KeySelector<?, ?> keySelector1 = offloadedConfig.getStatePartitioner(1, userClassloader);
+ if (keySelector1 != null) {
+ sourceConfig.setStatePartitioner(1, keySelector1);
+ }
+ }
+
+ private boolean supportsVectorOutput(
+ OperatorChainSlice chainSlice, OperatorChainSliceGraph chainSliceGraph, JobVertex jobVertex) {
+ List<Integer> downstreamSliceIds = chainSlice.getOutputs();
+
+ // If chainSlice has downstream in the operator chain, check these downstream
+ if (!downstreamSliceIds.isEmpty()) {
+ return checkDownstreamSlicesInChain(chainSliceGraph, downstreamSliceIds);
+ } else {
+ // If chainSlice has no downstream in the operator chain, check downstream JobVertex from
+ // JobGraph level
+ return checkDownstreamVerticesFromJobGraph(jobVertex);
+ }
+ }
+
+ private boolean checkDownstreamSlicesInChain(
+ OperatorChainSliceGraph chainSliceGraph, List<Integer> downstreamSliceIds) {
+ for (Integer downstreamSliceId : downstreamSliceIds) {
+ OperatorChainSlice downstreamSlice = chainSliceGraph.getSlice(downstreamSliceId);
+ if (!downstreamSlice.isOffloadable()) {
+ return false;
+ }
+ if (!downstreamSlice.getInputs().stream()
+ .map(chainSliceGraph::getSlice)
+ .allMatch(OperatorChainSlice::isOffloadable)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private boolean checkDownstreamVerticesFromJobGraph(JobVertex jobVertex) {
+ List<JobVertex> downstreamVertices = getDownstreamJobVertices(jobVertex);
+ if (downstreamVertices.isEmpty()) {
+ // If there is no downstream JobVertex, can output RowVector (no downstream needs
+ // conversion)
+ return true;
+ }
+ // Check if all downstream vertices' operators are gluten operators
+ for (JobVertex downstreamVertex : downstreamVertices) {
+ StreamConfig downstreamStreamConfig = new StreamConfig(downstreamVertex.getConfiguration());
+ Optional<GlutenOperator> glutenOperator =
+ Utils.getGlutenOperator(downstreamStreamConfig, userClassloader);
+ if (!glutenOperator.isPresent()) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private boolean supportsVectorInput(
+ OperatorChainSlice chainSlice, OperatorChainSliceGraph chainSliceGraph, JobVertex jobVertex) {
+ List<Integer> upstreamSliceIds = chainSlice.getInputs();
+
+ // If chainSlice has upstream in the operator chain, check these upstream
+ if (!upstreamSliceIds.isEmpty()) {
+ return checkUpstreamSlicesInChain(chainSliceGraph, upstreamSliceIds, jobVertex);
+ } else {
+ // If chainSlice has no upstream in the operator chain, check upstream JobVertex from
+ // JobGraph level
+ return checkUpstreamVerticesFromJobGraph(jobVertex);
+ }
+ }
+
+ private boolean checkUpstreamSlicesInChain(
+ OperatorChainSliceGraph chainSliceGraph,
+ List<Integer> upstreamSliceIds,
+ JobVertex jobVertex) {
+ for (Integer upstreamSliceId : upstreamSliceIds) {
+ OperatorChainSlice upstreamSlice = chainSliceGraph.getSlice(upstreamSliceId);
+ if (!upstreamSlice.isOffloadable()) {
+ return false;
+ }
+ if (!supportsVectorOutput(upstreamSlice, chainSliceGraph, jobVertex)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ private boolean checkUpstreamVerticesFromJobGraph(JobVertex jobVertex) {
+ List<JobVertex> upstreamVertices = getUpstreamJobVertices(jobVertex);
+ if (upstreamVertices.isEmpty()) {
+ // If there is no upstream JobVertex, can input RowVector (no upstream needs conversion)
+ return true;
+ }
+ // Check if the last operator of all upstream vertices are gluten operators
+ for (JobVertex upstreamVertex : upstreamVertices) {
+ StreamConfig lastOperatorConfig = findLastOperatorInChain(upstreamVertex);
+ Optional<GlutenOperator> glutenOperator =
+ Utils.getGlutenOperator(lastOperatorConfig, userClassloader);
+ if (!glutenOperator.isPresent()) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /** Get all downstream JobVertices of a JobVertex. */
+ private List<JobVertex> getDownstreamJobVertices(JobVertex vertex) {
+ List<JobVertex> downstreamVertices = new ArrayList<>();
+ for (IntermediateDataSet dataSet : vertex.getProducedDataSets()) {
+ for (JobEdge edge : dataSet.getConsumers()) {
+ JobVertex downstreamVertex = edge.getTarget();
+ downstreamVertices.add(downstreamVertex);
+ }
+ }
+ return downstreamVertices;
+ }
+
+ /** Get all upstream JobVertices of a JobVertex. */
+ private List<JobVertex> getUpstreamJobVertices(JobVertex vertex) {
+ List<JobVertex> upstreamVertices = new ArrayList<>();
+ for (JobEdge edge : vertex.getInputs()) {
+ JobVertex upstreamVertex = edge.getSource().getProducer();
+ upstreamVertices.add(upstreamVertex);
+ }
+ return upstreamVertices;
+ }
+}
diff --git a/gluten-flink/runtime/src/main/java/org/apache/gluten/client/OperatorChainSlice.java b/gluten-flink/runtime/src/main/java/org/apache/gluten/client/OperatorChainSlice.java
new file mode 100644
index 0000000000..91b96d811d
--- /dev/null
+++ b/gluten-flink/runtime/src/main/java/org/apache/gluten/client/OperatorChainSlice.java
@@ -0,0 +1,65 @@
+/*
+ * 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.gluten.client;
+
+import org.apache.flink.streaming.api.graph.StreamConfig;
+
+import java.util.ArrayList;
+import java.util.List;
+
+// Split operator chain into slices for offloading
+// In the same slice, operators are all could offload or not.
+public class OperatorChainSlice {
+ // upstream slice indices
+ private List<Integer> inputs;
+ // downstream slice indices
+ private List<Integer> outputs;
+ private List<StreamConfig> operatorConfigs;
+ private Integer id;
+ private Boolean offloadable = false;
+
+ public OperatorChainSlice(Integer id) {
+ inputs = new ArrayList<>();
+ outputs = new ArrayList<>();
+ operatorConfigs = new ArrayList<>();
+ this.id = id;
+ }
+
+ public Integer id() {
+ return id;
+ }
+
+ public List<Integer> getInputs() {
+ return inputs;
+ }
+
+ public List<Integer> getOutputs() {
+ return outputs;
+ }
+
+ public List<StreamConfig> getOperatorConfigs() {
+ return operatorConfigs;
+ }
+
+ public Boolean isOffloadable() {
+ return offloadable;
+ }
+
+ public void setOffloadable(Boolean offloadable) {
+ this.offloadable = offloadable;
+ }
+}
diff --git a/gluten-flink/runtime/src/main/java/org/apache/gluten/client/OperatorChainSliceGraph.java b/gluten-flink/runtime/src/main/java/org/apache/gluten/client/OperatorChainSliceGraph.java
new file mode 100644
index 0000000000..c2861313e1
--- /dev/null
+++ b/gluten-flink/runtime/src/main/java/org/apache/gluten/client/OperatorChainSliceGraph.java
@@ -0,0 +1,82 @@
+/*
+ * 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.gluten.client;
+
+import org.apache.flink.util.Preconditions;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+public class OperatorChainSliceGraph {
+ private Map<Integer, OperatorChainSlice> slices;
+
+ public OperatorChainSliceGraph() {
+ slices = new HashMap<>();
+ }
+
+ public void addSlice(Integer id, OperatorChainSlice chainSlice) {
+ slices.put(id, chainSlice);
+ }
+
+ public OperatorChainSlice getSlice(Integer id) {
+ return slices.get(id);
+ }
+
+ public OperatorChainSlice getSourceSlice() {
+ List<OperatorChainSlice> sourceCandidates = new ArrayList<>();
+
+ for (OperatorChainSlice chainSlice : slices.values()) {
+ if (chainSlice.getInputs().isEmpty()) {
+ sourceCandidates.add(chainSlice);
+ }
+ }
+
+ Preconditions.checkState(
+ sourceCandidates.size() == 1,
+ "Expected exactly one source operator chain slice with empty inputs, but found %s",
+ sourceCandidates.size());
+
+ return sourceCandidates.get(0);
+ }
+
+ public Map<Integer, OperatorChainSlice> getSlices() {
+ return slices;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder sb = new StringBuilder();
+ for (OperatorChainSlice chainSlice : slices.values()) {
+ sb.append("Slice ID: ")
+ .append(chainSlice.id())
+ .append(", offloadable: ")
+ .append(chainSlice.isOffloadable())
+ .append("\n");
+ sb.append(" Inputs: ").append(chainSlice.getInputs()).append("\n");
+ sb.append(" Outputs: ").append(chainSlice.getOutputs()).append("\n");
+ String operatorConfigs =
+ chainSlice.getOperatorConfigs().stream()
+ .map(config -> config.getOperatorName() + "(" + config.getVertexID() + ")")
+ .collect(Collectors.joining(", "));
+ sb.append(" Operator Configs: ").append(operatorConfigs).append("\n");
+ }
+ return sb.toString();
+ }
+}
diff --git a/gluten-flink/runtime/src/main/java/org/apache/gluten/client/OperatorChainSliceGraphGenerator.java b/gluten-flink/runtime/src/main/java/org/apache/gluten/client/OperatorChainSliceGraphGenerator.java
new file mode 100644
index 0000000000..ec7d995146
--- /dev/null
+++ b/gluten-flink/runtime/src/main/java/org/apache/gluten/client/OperatorChainSliceGraphGenerator.java
@@ -0,0 +1,145 @@
+/*
+ * 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.gluten.client;
+
+import org.apache.gluten.util.Utils;
+
+import org.apache.flink.runtime.jobgraph.JobVertex;
+import org.apache.flink.streaming.api.graph.StreamConfig;
+import org.apache.flink.streaming.api.graph.StreamEdge;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Generates an OperatorChainSliceGraph from a JobVertex by analyzing its operator chain structure.
+ * The graph represents operator slices and their relationships, identifying which slices are
+ * offloadable to Gluten.
+ */
+class OperatorChainSliceGraphGenerator {
+ private OperatorChainSliceGraph chainSliceGraph;
+ private final JobVertex jobVertex;
+ private Map<Integer, StreamConfig> chainedConfigs;
+ private final ClassLoader userClassLoader;
+
+ public OperatorChainSliceGraphGenerator(JobVertex jobVertex, ClassLoader userClassLoader) {
+ this.jobVertex = jobVertex;
+ this.userClassLoader = userClassLoader;
+ }
+
+ /**
+ * Generates and returns the OperatorChainSliceGraph for the JobVertex. The graph is generated
+ * lazily on first call and cached.
+ *
+ * @return the generated OperatorChainSliceGraph
+ */
+ public OperatorChainSliceGraph getGraph() {
+ if (chainSliceGraph == null) {
+ generateGraph();
+ }
+ return chainSliceGraph;
+ }
+
+ /** Generates the OperatorChainSliceGraph by traversing the operator chain. */
+ private void generateGraph() {
+ chainSliceGraph = new OperatorChainSliceGraph();
+ StreamConfig rootConfig = new StreamConfig(jobVertex.getConfiguration());
+
+ chainedConfigs = collectChainedConfigs(rootConfig);
+ OperatorChainSlice rootSlice = createRootSlice(rootConfig);
+ chainSliceGraph.addSlice(rootSlice.id(), rootSlice);
+
+ traverseOperatorChain(rootSlice, rootConfig);
+ }
+
+ /** Collects all chained operator configurations from the root configuration. */
+ private Map<Integer, StreamConfig> collectChainedConfigs(StreamConfig rootConfig) {
+ Map<Integer, StreamConfig> configs = new HashMap<>();
+ rootConfig
+ .getTransitiveChainedTaskConfigs(userClassLoader)
+ .forEach((id, config) -> configs.put(id, new StreamConfig(config.getConfiguration())));
+ configs.put(rootConfig.getVertexID(), rootConfig);
+ return configs;
+ }
+
+ /** Creates the root operator chain slice. */
+ private OperatorChainSlice createRootSlice(StreamConfig rootConfig) {
+ OperatorChainSlice rootSlice = new OperatorChainSlice(rootConfig.getVertexID());
+ rootSlice.setOffloadable(isOffloadableOperator(rootConfig));
+ rootSlice.getOperatorConfigs().add(rootConfig);
+ return rootSlice;
+ }
+
+ /** Traverses the operator chain recursively, creating slices for each operator. */
+ private void traverseOperatorChain(OperatorChainSlice currentSlice, StreamConfig currentConfig) {
+ List<StreamEdge> outputEdges = currentConfig.getChainedOutputs(userClassLoader);
+ if (outputEdges == null || outputEdges.isEmpty()) {
+ return;
+ }
+
+ for (StreamEdge edge : outputEdges) {
+ Integer targetId = edge.getTargetId();
+ StreamConfig childConfig = chainedConfigs.get(targetId);
+ processChildOperator(currentSlice, childConfig);
+ }
+ }
+
+ /**
+ * Processes a child operator, creating a new slice if not already visited. Note: We don't
+ * coalesce operators into the same velox plan at present. Each operator is a separate velox plan.
+ */
+ private void processChildOperator(OperatorChainSlice parentSlice, StreamConfig childConfig) {
+ Integer childId = childConfig.getVertexID();
+ OperatorChainSlice childSlice = chainSliceGraph.getSlice(childId);
+
+ if (childSlice == null) {
+ // First visit: create new slice and continue traversal
+ childSlice = createChildSlice(childConfig);
+ chainSliceGraph.addSlice(childId, childSlice);
+ connectSlices(parentSlice, childSlice);
+ traverseOperatorChain(childSlice, childConfig);
+ } else {
+ // Already visited: just connect the slices
+ connectSlices(parentSlice, childSlice);
+ }
+ }
+
+ /** Creates a new operator chain slice for a child operator. */
+ private OperatorChainSlice createChildSlice(StreamConfig childConfig) {
+ OperatorChainSlice childSlice = new OperatorChainSlice(childConfig.getVertexID());
+ childSlice.setOffloadable(isOffloadableOperator(childConfig));
+ childSlice.getOperatorConfigs().add(childConfig);
+ return childSlice;
+ }
+
+ /** Connects two operator chain slices by adding their relationship. */
+ private void connectSlices(OperatorChainSlice parentSlice, OperatorChainSlice childSlice) {
+ parentSlice.getOutputs().add(childSlice.id());
+ childSlice.getInputs().add(parentSlice.id());
+ }
+
+ /**
+ * Checks if an operator is offloadable to Gluten.
+ *
+ * @param opConfig the operator configuration to check
+ * @return true if the operator is offloadable, false otherwise
+ */
+ private boolean isOffloadableOperator(StreamConfig opConfig) {
+ return Utils.getGlutenOperator(opConfig, userClassLoader).isPresent();
+ }
+}
diff --git a/gluten-flink/runtime/src/main/java/org/apache/gluten/streaming/api/operators/GlutenOperator.java b/gluten-flink/runtime/src/main/java/org/apache/gluten/streaming/api/operators/GlutenOperator.java
index 6ed7d0497b..9720551967 100644
--- a/gluten-flink/runtime/src/main/java/org/apache/gluten/streaming/api/operators/GlutenOperator.java
+++ b/gluten-flink/runtime/src/main/java/org/apache/gluten/streaming/api/operators/GlutenOperator.java
@@ -30,4 +30,8 @@ public interface GlutenOperator {
public Map<String, RowType> getOutputTypes();
public String getId();
+
+ public default String getDescription() {
+ return "";
+ }
}
diff --git a/gluten-flink/runtime/src/main/java/org/apache/gluten/streaming/api/operators/GlutenStreamSource.java b/gluten-flink/runtime/src/main/java/org/apache/gluten/streaming/api/operators/GlutenStreamSource.java
index 0281def229..349261ff4d 100644
--- a/gluten-flink/runtime/src/main/java/org/apache/gluten/streaming/api/operators/GlutenStreamSource.java
+++ b/gluten-flink/runtime/src/main/java/org/apache/gluten/streaming/api/operators/GlutenStreamSource.java
@@ -16,7 +16,7 @@
*/
package org.apache.gluten.streaming.api.operators;
-import org.apache.gluten.table.runtime.operators.GlutenVectorSourceFunction;
+import org.apache.gluten.table.runtime.operators.GlutenSourceFunction;
import org.apache.gluten.util.ReflectUtils;
import org.apache.gluten.util.Utils;
@@ -32,11 +32,24 @@ import java.util.Map;
/** Legacy stream source operator in gluten, which will call Velox to run. */
public class GlutenStreamSource extends StreamSource implements GlutenOperator {
- private final GlutenVectorSourceFunction sourceFunction;
+ private final GlutenSourceFunction sourceFunction;
+ private final String description;
- public GlutenStreamSource(GlutenVectorSourceFunction function) {
+ public GlutenStreamSource(GlutenSourceFunction function) {
super(function);
sourceFunction = function;
+ this.description = "";
+ }
+
+ public GlutenStreamSource(GlutenSourceFunction function, String description) {
+ super(function);
+ sourceFunction = function;
+ this.description = description;
+ }
+
+ @Override
+ public String getDescription() {
+ return description;
}
@Override
diff --git a/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenOneInputOperator.java b/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenOneInputOperator.java
index 09af2f4576..ccc3cde3c0 100644
--- a/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenOneInputOperator.java
+++ b/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenOneInputOperator.java
@@ -19,32 +19,30 @@ package org.apache.gluten.table.runtime.operators;
import org.apache.gluten.streaming.api.operators.GlutenOperator;
import org.apache.gluten.table.runtime.config.VeloxConnectorConfig;
import org.apache.gluten.table.runtime.config.VeloxQueryConfig;
-import org.apache.gluten.vectorized.FlinkRowToVLVectorConvertor;
+import org.apache.gluten.util.VectorInputBridge;
+import org.apache.gluten.util.VectorOutputBridge;
-import io.github.zhztheplayer.velox4j.Velox4j;
import io.github.zhztheplayer.velox4j.connector.ExternalStreamConnectorSplit;
import io.github.zhztheplayer.velox4j.connector.ExternalStreamTableHandle;
import io.github.zhztheplayer.velox4j.connector.ExternalStreams;
-import io.github.zhztheplayer.velox4j.data.RowVector;
import io.github.zhztheplayer.velox4j.iterator.UpIterator;
-import io.github.zhztheplayer.velox4j.memory.AllocationListener;
-import io.github.zhztheplayer.velox4j.memory.MemoryManager;
import io.github.zhztheplayer.velox4j.plan.StatefulPlanNode;
import io.github.zhztheplayer.velox4j.plan.TableScanNode;
import io.github.zhztheplayer.velox4j.query.Query;
import io.github.zhztheplayer.velox4j.query.SerialTask;
import io.github.zhztheplayer.velox4j.serde.Serde;
-import io.github.zhztheplayer.velox4j.session.Session;
import io.github.zhztheplayer.velox4j.stateful.StatefulElement;
+import io.github.zhztheplayer.velox4j.stateful.StatefulRecord;
+import io.github.zhztheplayer.velox4j.stateful.StatefulWatermark;
import io.github.zhztheplayer.velox4j.type.RowType;
+import org.apache.flink.runtime.state.StateInitializationContext;
+import org.apache.flink.runtime.state.StateSnapshotContext;
import org.apache.flink.streaming.api.operators.OneInputStreamOperator;
+import org.apache.flink.streaming.api.watermark.Watermark;
import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
-import org.apache.flink.table.data.RowData;
import org.apache.flink.table.runtime.operators.TableStreamOperator;
-import org.apache.arrow.memory.BufferAllocator;
-import org.apache.arrow.memory.RootAllocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -52,8 +50,8 @@ import java.util.List;
import java.util.Map;
/** Calculate operator in gluten, which will call Velox to run. */
-public class GlutenOneInputOperator extends TableStreamOperator<RowData>
- implements OneInputStreamOperator<RowData, RowData>, GlutenOperator {
+public class GlutenOneInputOperator<IN, OUT> extends TableStreamOperator<OUT>
+ implements OneInputStreamOperator<IN, OUT>, GlutenOperator {
private static final Logger LOG = LoggerFactory.getLogger(GlutenOneInputOperator.class);
@@ -61,33 +59,74 @@ public class GlutenOneInputOperator extends TableStreamOperator<RowData>
private final String id;
private final RowType inputType;
private final Map<String, RowType> outputTypes;
+ private final RowType outputType;
+ private final String description;
- private StreamRecord<RowData> outElement = null;
-
- private MemoryManager memoryManager;
- private Session session;
- private Query query;
- private ExternalStreams.BlockingQueue inputQueue;
- private BufferAllocator allocator;
- private SerialTask task;
+ private transient GlutenSessionResource sessionResource;
+ private transient Query query;
+ private transient ExternalStreams.BlockingQueue inputQueue;
+ private transient SerialTask task;
+ private final Class<IN> inClass;
+ private final Class<OUT> outClass;
+ private transient VectorInputBridge<IN> inputBridge;
+ private transient VectorOutputBridge<OUT> outputBridge;
public GlutenOneInputOperator(
- StatefulPlanNode plan, String id, RowType inputType, Map<String, RowType> outputTypes) {
+ StatefulPlanNode plan,
+ String id,
+ RowType inputType,
+ Map<String, RowType> outputTypes,
+ Class<IN> inClass,
+ Class<OUT> outClass,
+ String description) {
+ if (plan == null) {
+ throw new IllegalArgumentException(
+ "StatefulPlanNode 'plan' cannot be null in GlutenOneInputOperator constructor");
+ }
this.glutenPlan = plan;
this.id = id;
this.inputType = inputType;
this.outputTypes = outputTypes;
+ this.inClass = inClass;
+ this.outClass = outClass;
+ this.inputBridge = VectorInputBridge.Factory.create(inClass, getId());
+ this.outputBridge = VectorOutputBridge.Factory.create(outClass);
+ this.outputType = outputTypes.values().iterator().next();
+ this.description = description;
+ }
+
+ public GlutenOneInputOperator(
+ StatefulPlanNode plan,
+ String id,
+ RowType inputType,
+ Map<String, RowType> outputTypes,
+ Class<IN> inClass,
+ Class<OUT> outClass) {
+ this(plan, id, inputType, outputTypes, inClass, outClass, "");
}
@Override
- public void open() throws Exception {
- super.open();
- outElement = new StreamRecord(null);
- memoryManager = MemoryManager.create(AllocationListener.NOOP);
- session = Velox4j.newSession(memoryManager);
+ public String getDescription() {
+ return description;
+ }
- inputQueue = session.externalStreamOps().newBlockingQueue();
+ void initSession() {
+ if (sessionResource != null) {
+ return;
+ }
+ if (inputBridge == null) {
+ inputBridge = VectorInputBridge.Factory.create(inClass, getId());
+ }
+ if (outputBridge == null) {
+ outputBridge = VectorOutputBridge.Factory.create(outClass);
+ }
+ sessionResource = new GlutenSessionResource();
+ inputQueue = sessionResource.getSession().externalStreamOps().newBlockingQueue();
// add a mock input as velox not allow the source is empty.
+ if (inputType == null) {
+ LOG.warn("inputType is null. plan is " + Serde.toJson(glutenPlan));
+ throw new IllegalArgumentException("inputType cannot be null");
+ }
StatefulPlanNode mockInput =
new StatefulPlanNode(
id,
@@ -104,43 +143,66 @@ public class GlutenOneInputOperator extends TableStreamOperator<RowData>
mockInput,
VeloxQueryConfig.getConfig(getRuntimeContext()),
VeloxConnectorConfig.getConfig(getRuntimeContext()));
- allocator = new RootAllocator(Long.MAX_VALUE);
- task = session.queryOps().execute(query);
- ExternalStreamConnectorSplit split =
- new ExternalStreamConnectorSplit("connector-external-stream", inputQueue.id());
- task.addSplit(id, split);
+ task = sessionResource.getSession().queryOps().execute(query);
+ task.addSplit(
+ id, new ExternalStreamConnectorSplit("connector-external-stream", inputQueue.id()));
task.noMoreSplits(id);
}
@Override
- public void processElement(StreamRecord<RowData> element) {
- try (RowVector inRv =
- FlinkRowToVLVectorConvertor.fromRowData(
- element.getValue(), allocator, session, inputType)) {
- inputQueue.put(inRv);
+ public void open() throws Exception {
+ super.open();
+ initSession();
+ }
+
+ @Override
+ public void processElement(StreamRecord<IN> element) {
+ if (element.getValue() == null) {
+ return;
+ }
+ StatefulRecord statefulRecord =
+ inputBridge.convertToStatefulRecord(
+ element, sessionResource.getAllocator(), sessionResource.getSession(), inputType);
+ inputQueue.put(statefulRecord.getRowVector());
+
+ // Only the rowvectors generated by this operator should be closed here.
+ if (getId().equals(statefulRecord.getNodeId())) {
+ statefulRecord.close();
+ }
+ processElementInternal();
+ }
+
+ private void processElementInternal() {
+ while (true) {
UpIterator.State state = task.advance();
if (state == UpIterator.State.AVAILABLE) {
final StatefulElement statefulElement = task.statefulGet();
-
- try (RowVector outRv = statefulElement.asRecord().getRowVector()) {
- List<RowData> rows =
- FlinkRowToVLVectorConvertor.toRowData(
- outRv, allocator, outputTypes.values().iterator().next());
- for (RowData row : rows) {
- output.collect(outElement.replace(row));
- }
+ if (statefulElement.isWatermark()) {
+ StatefulWatermark watermark = statefulElement.asWatermark();
+ output.emitWatermark(new Watermark(watermark.getTimestamp()));
+ } else {
+ outputBridge.collect(
+ output, statefulElement.asRecord(), sessionResource.getAllocator(), outputType);
}
+ statefulElement.close();
+ } else {
+ break;
}
}
}
@Override
public void close() throws Exception {
- inputQueue.close();
- task.close();
- session.close();
- memoryManager.close();
- allocator.close();
+ if (inputQueue != null) {
+ inputQueue.noMoreInput();
+ inputQueue.close();
+ }
+ if (task != null) {
+ task.close();
+ }
+ if (sessionResource != null) {
+ sessionResource.close();
+ }
}
@Override
@@ -162,4 +224,41 @@ public class GlutenOneInputOperator extends TableStreamOperator<RowData>
public String getId() {
return id;
}
+
+ @Override
+ public void prepareSnapshotPreBarrier(long checkpointId) throws Exception {
+ // TODO: notify velox
+ super.prepareSnapshotPreBarrier(checkpointId);
+ }
+
+ @Override
+ public void snapshotState(StateSnapshotContext context) throws Exception {
+ // TODO: implement it
+ task.snapshotState(0);
+ super.snapshotState(context);
+ }
+
+ @Override
+ public void initializeState(StateInitializationContext context) throws Exception {
+ if (task == null) {
+ initSession();
+ }
+ // TODO: implement it
+ task.initializeState(0);
+ super.initializeState(context);
+ }
+
+ @Override
+ public void notifyCheckpointComplete(long checkpointId) throws Exception {
+ // TODO: notify velox
+ task.notifyCheckpointComplete(checkpointId);
+ super.notifyCheckpointComplete(checkpointId);
+ }
+
+ @Override
+ public void notifyCheckpointAborted(long checkpointId) throws Exception {
+ // TODO: notify velox
+ task.notifyCheckpointAborted(checkpointId);
+ super.notifyCheckpointAborted(checkpointId);
+ }
}
diff --git a/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenSessionResource.java b/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenSessionResource.java
new file mode 100644
index 0000000000..b54102c466
--- /dev/null
+++ b/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenSessionResource.java
@@ -0,0 +1,65 @@
+/*
+ * 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.gluten.table.runtime.operators;
+
+import io.github.zhztheplayer.velox4j.Velox4j;
+import io.github.zhztheplayer.velox4j.memory.AllocationListener;
+import io.github.zhztheplayer.velox4j.memory.MemoryManager;
+import io.github.zhztheplayer.velox4j.session.Session;
+
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.memory.RootAllocator;
+
+// Manage the session and resource for Velox.
+class GlutenSessionResource {
+ private Session session;
+ private MemoryManager memoryManager;
+ private BufferAllocator allocator;
+
+ public GlutenSessionResource() {
+ this.memoryManager = MemoryManager.create(AllocationListener.NOOP);
+ this.session = Velox4j.newSession(memoryManager);
+ this.allocator = new RootAllocator(Long.MAX_VALUE);
+ }
+
+ public void close() {
+ if (session != null) {
+ session.close();
+ session = null;
+ }
+ if (memoryManager != null) {
+ memoryManager.close();
+ memoryManager = null;
+ }
+ if (allocator != null) {
+ allocator.close();
+ allocator = null;
+ }
+ }
+
+ public Session getSession() {
+ return session;
+ }
+
+ public MemoryManager getMemoryManager() {
+ return memoryManager;
+ }
+
+ public BufferAllocator getAllocator() {
+ return allocator;
+ }
+}
diff --git a/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenSourceFunction.java b/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenSourceFunction.java
index 54c33f5159..5c7b0c874d 100644
--- a/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenSourceFunction.java
+++ b/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenSourceFunction.java
@@ -18,35 +18,40 @@ package org.apache.gluten.table.runtime.operators;
import org.apache.gluten.table.runtime.config.VeloxConnectorConfig;
import org.apache.gluten.table.runtime.config.VeloxQueryConfig;
+import org.apache.gluten.table.runtime.metrics.SourceTaskMetrics;
import org.apache.gluten.vectorized.FlinkRowToVLVectorConvertor;
-import io.github.zhztheplayer.velox4j.Velox4j;
import io.github.zhztheplayer.velox4j.connector.ConnectorSplit;
-import io.github.zhztheplayer.velox4j.data.RowVector;
import io.github.zhztheplayer.velox4j.iterator.UpIterator;
-import io.github.zhztheplayer.velox4j.memory.AllocationListener;
-import io.github.zhztheplayer.velox4j.memory.MemoryManager;
import io.github.zhztheplayer.velox4j.plan.StatefulPlanNode;
import io.github.zhztheplayer.velox4j.query.Query;
import io.github.zhztheplayer.velox4j.query.SerialTask;
-import io.github.zhztheplayer.velox4j.serde.Serde;
import io.github.zhztheplayer.velox4j.session.Session;
import io.github.zhztheplayer.velox4j.stateful.StatefulElement;
+import io.github.zhztheplayer.velox4j.stateful.StatefulRecord;
+import io.github.zhztheplayer.velox4j.stateful.StatefulWatermark;
import io.github.zhztheplayer.velox4j.type.RowType;
+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.source.RichParallelSourceFunction;
+import org.apache.flink.streaming.api.watermark.Watermark;
import org.apache.flink.table.data.RowData;
-import org.apache.arrow.memory.BufferAllocator;
-import org.apache.arrow.memory.RootAllocator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.Map;
-/** Gluten legacy source function, call velox plan to execute. */
-public class GlutenSourceFunction extends RichParallelSourceFunction<RowData> {
+/**
+ * Gluten legacy source function, call velox plan to execute. It sends RowVector to downstream
+ * instead of RowData to avoid data conversion.
+ */
+public class GlutenSourceFunction<OUT> extends RichParallelSourceFunction<OUT>
+ implements CheckpointedFunction {
private static final Logger LOG = LoggerFactory.getLogger(GlutenSourceFunction.class);
private final StatefulPlanNode planNode;
@@ -55,20 +60,23 @@ public class GlutenSourceFunction extends RichParallelSourceFunction<RowData> {
private final ConnectorSplit split;
private volatile boolean isRunning = true;
- private Session session;
+ private GlutenSessionResource sessionResource;
private Query query;
- BufferAllocator allocator;
- private MemoryManager memoryManager;
+ private SerialTask task;
+ private SourceTaskMetrics taskMetrics;
+ private final Class<OUT> outClass;
public GlutenSourceFunction(
StatefulPlanNode planNode,
Map<String, RowType> outputTypes,
String id,
- ConnectorSplit split) {
+ ConnectorSplit split,
+ Class<OUT> outClass) {
this.planNode = planNode;
this.outputTypes = outputTypes;
this.id = id;
this.split = split;
+ this.outClass = outClass;
}
public StatefulPlanNode getPlanNode() {
@@ -88,48 +96,152 @@ public class GlutenSourceFunction extends RichParallelSourceFunction<RowData> {
}
@Override
- public void run(SourceContext<RowData> sourceContext) throws Exception {
- LOG.debug("Running GlutenSourceFunction: " + Serde.toJson(planNode));
- memoryManager = MemoryManager.create(AllocationListener.NOOP);
- session = Velox4j.newSession(memoryManager);
- query =
- new Query(
- planNode,
- VeloxQueryConfig.getConfig(getRuntimeContext()),
- VeloxConnectorConfig.getConfig(getRuntimeContext()));
- allocator = new RootAllocator(Long.MAX_VALUE);
+ public void open(Configuration parameters) throws Exception {
+ initSession();
+ }
- SerialTask task = session.queryOps().execute(query);
- task.addSplit(id, split);
- task.noMoreSplits(id);
+ @Override
+ public void run(SourceContext<OUT> sourceContext) throws Exception {
while (isRunning) {
UpIterator.State state = task.advance();
- if (state == UpIterator.State.AVAILABLE) {
- final StatefulElement element = task.statefulGet();
- try (final RowVector outRv = element.asRecord().getRowVector()) {
- List<RowData> rows =
- FlinkRowToVLVectorConvertor.toRowData(
- outRv, allocator, outputTypes.values().iterator().next());
- for (RowData row : rows) {
- sourceContext.collect(row);
- }
- }
- } else if (state == UpIterator.State.BLOCKED) {
- LOG.debug("Get empty row");
+ switch (state) {
+ case AVAILABLE:
+ processAvailableElement(sourceContext);
+ break;
+ case BLOCKED:
+ LOG.debug("Get empty row");
+ break;
+ default:
+ LOG.info("Velox task finished");
+ return;
+ }
+ taskMetrics.updateMetrics(task, id);
+ }
+ }
+
+ /** Processes an available element from the task, handling records and watermarks. */
+ private void processAvailableElement(SourceContext<OUT> sourceContext) {
+ StatefulElement element = task.statefulGet();
+ try {
+ if (element.isRecord()) {
+ processRecord(sourceContext, element.asRecord());
+ } else if (element.isWatermark()) {
+ processWatermark(sourceContext, element.asWatermark());
} else {
- LOG.info("Velox task finished");
- break;
+ LOG.debug("Ignoring element that is neither record nor watermark");
}
+ } finally {
+ element.close();
}
+ }
+
+ /** Strategy for collecting a StatefulRecord into the source context. */
+ private interface OutputHandler<OUT> {
+ void collect(SourceContext<OUT> sourceContext, StatefulRecord record);
+ }
+ /** Returns the appropriate OutputHandler based on the configured output class. */
+ private OutputHandler<OUT> getOutputHandler() {
+ if (isRowDataOutput()) {
+ return this::collectAsRowData;
+ }
+ if (isStatefulRecordOutput()) {
+ return this::collectAsStatefulRecord;
+ }
+ throw unsupportedOutputException();
+ }
+ /** Creates an exception for unsupported output class configurations. */
+ private UnsupportedOperationException unsupportedOutputException() {
+ return new UnsupportedOperationException("Unsupported output class: " + outClass.getName());
+ }
+ /** Processes a StatefulRecord and collects it to the source context. */
+ private void processRecord(SourceContext<OUT> sourceContext, StatefulRecord record) {
+ OutputHandler handler = getOutputHandler();
+ handler.collect(sourceContext, record);
+ }
+
+ /** Processes a watermark and emits it to the source context. */
+ private void processWatermark(SourceContext<OUT> sourceContext, StatefulWatermark watermark) {
+ sourceContext.emitWatermark(new Watermark(watermark.getTimestamp()));
+ }
+
+ /** Collects a StatefulRecord as RowData by converting the RowVector. */
+ private void collectAsRowData(SourceContext<OUT> sourceContext, StatefulRecord record) {
+ List<RowData> rows =
+ FlinkRowToVLVectorConvertor.toRowData(
+ record.getRowVector(), sessionResource.getAllocator(), outputTypes.get(id));
+ for (RowData row : rows) {
+ sourceContext.collect((OUT) row);
+ }
+ }
+
+ /** Collects a StatefulRecord directly without conversion. */
+ private void collectAsStatefulRecord(SourceContext<OUT> sourceContext, StatefulRecord record) {
+ sourceContext.collect((OUT) record);
+ }
- task.close();
- session.close();
- memoryManager.close();
- allocator.close();
+ private boolean isRowDataOutput() {
+ return outClass.isAssignableFrom(RowData.class);
+ }
+
+ private boolean isStatefulRecordOutput() {
+ return outClass.isAssignableFrom(StatefulRecord.class);
}
@Override
public void cancel() {
isRunning = false;
}
+
+ @Override
+ public void close() throws Exception {
+ isRunning = false;
+ if (task != null) {
+ task.close();
+ task = null;
+ }
+ if (sessionResource != null) {
+ sessionResource.close();
+ sessionResource = null;
+ }
+ }
+
+ @Override
+ public void snapshotState(FunctionSnapshotContext context) throws Exception {
+ // TODO: implement it
+ this.task.snapshotState(0);
+ }
+
+ @Override
+ public void initializeState(FunctionInitializationContext context) throws Exception {
+ initSession();
+ // TODO: implement it
+ this.task.initializeState(0);
+ }
+
+ public String[] notifyCheckpointComplete(long checkpointId) throws Exception {
+ // TODO: notify velox
+ return this.task.notifyCheckpointComplete(checkpointId);
+ }
+
+ public void notifyCheckpointAborted(long checkpointId) throws Exception {
+ // TODO: notify velox
+ this.task.notifyCheckpointAborted(checkpointId);
+ }
+
+ private void initSession() {
+ if (sessionResource != null) {
+ return;
+ }
+ sessionResource = new GlutenSessionResource();
+ Session session = sessionResource.getSession();
+ query =
+ new Query(
+ planNode,
+ VeloxQueryConfig.getConfig(getRuntimeContext()),
+ VeloxConnectorConfig.getConfig(getRuntimeContext()));
+ task = session.queryOps().execute(query);
+ task.addSplit(id, split);
+ task.noMoreSplits(id);
+ taskMetrics = new SourceTaskMetrics(getRuntimeContext().getMetricGroup());
+ }
}
diff --git a/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenVectorTwoInputOperator.java b/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenTwoInputOperator.java
similarity index 64%
rename from gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenVectorTwoInputOperator.java
rename to gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenTwoInputOperator.java
index 4fbfe8c7ad..923e8d8d0d 100644
--- a/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenVectorTwoInputOperator.java
+++ b/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenTwoInputOperator.java
@@ -19,19 +19,15 @@ package org.apache.gluten.table.runtime.operators;
import org.apache.gluten.streaming.api.operators.GlutenOperator;
import org.apache.gluten.table.runtime.config.VeloxConnectorConfig;
import org.apache.gluten.table.runtime.config.VeloxQueryConfig;
+import org.apache.gluten.util.VectorInputBridge;
+import org.apache.gluten.util.VectorOutputBridge;
-import io.github.zhztheplayer.velox4j.Velox4j;
import io.github.zhztheplayer.velox4j.connector.ExternalStreamConnectorSplit;
import io.github.zhztheplayer.velox4j.connector.ExternalStreams;
-import io.github.zhztheplayer.velox4j.data.RowVector;
import io.github.zhztheplayer.velox4j.iterator.UpIterator;
-import io.github.zhztheplayer.velox4j.memory.AllocationListener;
-import io.github.zhztheplayer.velox4j.memory.MemoryManager;
import io.github.zhztheplayer.velox4j.plan.StatefulPlanNode;
import io.github.zhztheplayer.velox4j.query.Query;
import io.github.zhztheplayer.velox4j.query.SerialTask;
-import io.github.zhztheplayer.velox4j.serde.Serde;
-import io.github.zhztheplayer.velox4j.session.Session;
import io.github.zhztheplayer.velox4j.stateful.StatefulElement;
import io.github.zhztheplayer.velox4j.stateful.StatefulRecord;
import io.github.zhztheplayer.velox4j.stateful.StatefulWatermark;
@@ -53,11 +49,10 @@ import java.util.Map;
* Two input operator in gluten, which will call Velox to run. It receives RowVector from upstream
* instead of flink RowData.
*/
-public class GlutenVectorTwoInputOperator extends AbstractStreamOperator<StatefulRecord>
- implements TwoInputStreamOperator<StatefulRecord, StatefulRecord, StatefulRecord>,
- GlutenOperator {
+public class GlutenTwoInputOperator<IN, OUT> extends AbstractStreamOperator<OUT>
+ implements TwoInputStreamOperator<IN, IN, OUT>, GlutenOperator {
- private static final Logger LOG = LoggerFactory.getLogger(GlutenVectorTwoInputOperator.class);
+ private static final Logger LOG = LoggerFactory.getLogger(GlutenTwoInputOperator.class);
private final StatefulPlanNode glutenPlan;
private final String leftId;
@@ -65,82 +60,99 @@ public class GlutenVectorTwoInputOperator extends AbstractStreamOperator<Statefu
private final RowType leftInputType;
private final RowType rightInputType;
private final Map<String, RowType> outputTypes;
+ private final RowType outputType;
- private StreamRecord<StatefulRecord> outElement = null;
-
- private MemoryManager memoryManager;
- private Session session;
+ private GlutenSessionResource sessionResource;
private Query query;
private ExternalStreams.BlockingQueue leftInputQueue;
private ExternalStreams.BlockingQueue rightInputQueue;
private SerialTask task;
+ private final Class<IN> inClass;
+ private final Class<OUT> outClass;
+ private VectorInputBridge<IN> inputBridge;
+ private VectorOutputBridge<OUT> outputBridge;
+ private String description;
- public GlutenVectorTwoInputOperator(
+ public GlutenTwoInputOperator(
StatefulPlanNode plan,
String leftId,
String rightId,
RowType leftInputType,
RowType rightInputType,
- Map<String, RowType> outputTypes) {
+ Map<String, RowType> outputTypes,
+ Class<IN> inClass,
+ Class<OUT> outClass,
+ String description) {
this.glutenPlan = plan;
this.leftId = leftId;
this.rightId = rightId;
this.leftInputType = leftInputType;
this.rightInputType = rightInputType;
this.outputTypes = outputTypes;
+ this.inClass = inClass;
+ this.outClass = outClass;
+ this.inputBridge = VectorInputBridge.Factory.create(inClass, getId());
+ this.outputBridge = VectorOutputBridge.Factory.create(outClass);
+ this.outputType = outputTypes.values().iterator().next();
+ this.description = description;
}
- // initializeState is called before open, so need to init gluten task first.
- private void initGlutenTask() {
- memoryManager = MemoryManager.create(AllocationListener.NOOP);
- session = Velox4j.newSession(memoryManager);
- query =
- new Query(
- glutenPlan,
- VeloxQueryConfig.getConfig(getRuntimeContext()),
- VeloxConnectorConfig.getConfig(getRuntimeContext()));
- task = session.queryOps().execute(query);
- LOG.debug("Gluten Plan: {}", Serde.toJson(glutenPlan));
- LOG.debug("OutTypes: {}", outputTypes.keySet());
- LOG.debug("RuntimeContext: {}", getRuntimeContext().getClass().getName());
+ public GlutenTwoInputOperator(
+ StatefulPlanNode plan,
+ String leftId,
+ String rightId,
+ RowType leftInputType,
+ RowType rightInputType,
+ Map<String, RowType> outputTypes,
+ Class<IN> inClass,
+ Class<OUT> outClass) {
+ this(plan, leftId, rightId, leftInputType, rightInputType, outputTypes, inClass, outClass, "");
+ }
+
+ @Override
+ public String getDescription() {
+ return description;
}
@Override
public void open() throws Exception {
super.open();
- if (task == null) {
- initGlutenTask();
- }
- outElement = new StreamRecord(null);
- leftInputQueue = session.externalStreamOps().newBlockingQueue();
- rightInputQueue = session.externalStreamOps().newBlockingQueue();
- ExternalStreamConnectorSplit leftSplit =
- new ExternalStreamConnectorSplit("connector-external-stream", leftInputQueue.id());
- ExternalStreamConnectorSplit rightSplit =
- new ExternalStreamConnectorSplit("connector-external-stream", rightInputQueue.id());
- task.addSplit(leftId, leftSplit);
- task.noMoreSplits(leftId);
- task.addSplit(rightId, rightSplit);
- task.noMoreSplits(rightId);
+ initSession();
}
@Override
- public void processElement1(StreamRecord<StatefulRecord> element) {
- final RowVector inRv = element.getValue().getRowVector();
- leftInputQueue.put(inRv);
- processElement();
- inRv.close();
+ public String getId() {
+ return glutenPlan.getId();
}
@Override
- public void processElement2(StreamRecord<StatefulRecord> element) {
- final RowVector inRv = element.getValue().getRowVector();
- rightInputQueue.put(inRv);
- processElement();
- inRv.close();
+ public void processElement1(StreamRecord<IN> element) {
+ StatefulRecord statefulRecord =
+ inputBridge.convertToStatefulRecord(
+ element, sessionResource.getAllocator(), sessionResource.getSession(), leftInputType);
+ leftInputQueue.put(statefulRecord.getRowVector());
+ // Only the rowvectors generated by this operator should be closed here.
+ if (getId().equals(statefulRecord.getNodeId())) {
+ statefulRecord.close();
+ }
+ processElementInternal();
+ }
+
+ @Override
+ public void processElement2(StreamRecord<IN> element) {
+ StatefulRecord statefulRecord =
+ inputBridge.convertToStatefulRecord(
+ element, sessionResource.getAllocator(), sessionResource.getSession(), rightInputType);
+ rightInputQueue.put(statefulRecord.getRowVector());
+ // Only the rowvectors generated by this operator should be closed here.
+
+ if (getId().equals(statefulRecord.getNodeId())) {
+ statefulRecord.close();
+ }
+ processElementInternal();
}
- private void processElement() {
+ private void processElementInternal() {
while (true) {
UpIterator.State state = task.advance();
if (state == UpIterator.State.AVAILABLE) {
@@ -149,10 +161,10 @@ public class GlutenVectorTwoInputOperator extends AbstractStreamOperator<Statefu
StatefulWatermark watermark = element.asWatermark();
output.emitWatermark(new Watermark(watermark.getTimestamp()));
} else {
- final StatefulRecord statefulRecord = element.asRecord();
- output.collect(outElement.replace(statefulRecord));
- statefulRecord.close();
+ outputBridge.collect(
+ output, element.asRecord(), sessionResource.getAllocator(), outputType);
}
+ element.close();
} else {
break;
}
@@ -163,23 +175,30 @@ public class GlutenVectorTwoInputOperator extends AbstractStreamOperator<Statefu
public void processWatermark1(Watermark mark) throws Exception {
// TODO: implement it;
task.notifyWatermark(mark.getTimestamp(), 1);
- processElement();
+ processElementInternal();
}
@Override
public void processWatermark2(Watermark mark) throws Exception {
// TODO: implement it;
task.notifyWatermark(mark.getTimestamp(), 2);
- processElement();
+ processElementInternal();
}
@Override
public void close() throws Exception {
- leftInputQueue.close();
- rightInputQueue.close();
- task.close();
- session.close();
- memoryManager.close();
+ if (leftInputQueue != null) {
+ leftInputQueue.close();
+ }
+ if (rightInputQueue != null) {
+ rightInputQueue.close();
+ }
+ if (task != null) {
+ task.close();
+ }
+ if (sessionResource != null) {
+ sessionResource.close();
+ }
}
@Override
@@ -189,7 +208,7 @@ public class GlutenVectorTwoInputOperator extends AbstractStreamOperator<Statefu
@Override
public RowType getInputType() {
- throw new RuntimeException("Should not call getInputType on GlutenVectorTwoInputOperator");
+ throw new RuntimeException("Should not call getInputType on GlutenTwoInputOperator");
}
public RowType getLeftInputType() {
@@ -205,11 +224,6 @@ public class GlutenVectorTwoInputOperator extends AbstractStreamOperator<Statefu
return outputTypes;
}
- @Override
- public String getId() {
- throw new RuntimeException("Should not call getId on GlutenVectorTwoInputOperator");
- }
-
public String getLeftId() {
return leftId;
}
@@ -233,14 +247,39 @@ public class GlutenVectorTwoInputOperator extends AbstractStreamOperator<Statefu
@Override
public void initializeState(StateInitializationContext context) throws Exception {
- if (task == null) {
- initGlutenTask();
- }
+ initSession();
// TODO: implement it
task.initializeState(0);
super.initializeState(context);
}
+ private void initSession() {
+ if (sessionResource != null) {
+ return;
+ }
+
+ sessionResource = new GlutenSessionResource();
+
+ leftInputQueue = sessionResource.getSession().externalStreamOps().newBlockingQueue();
+ rightInputQueue = sessionResource.getSession().externalStreamOps().newBlockingQueue();
+
+ query =
+ new Query(
+ glutenPlan,
+ VeloxQueryConfig.getConfig(getRuntimeContext()),
+ VeloxConnectorConfig.getConfig(getRuntimeContext()));
+ task = sessionResource.getSession().queryOps().execute(query);
+
+ ExternalStreamConnectorSplit leftSplit =
+ new ExternalStreamConnectorSplit("connector-external-stream", leftInputQueue.id());
+ ExternalStreamConnectorSplit rightSplit =
+ new ExternalStreamConnectorSplit("connector-external-stream", rightInputQueue.id());
+ task.addSplit(leftId, leftSplit);
+ task.noMoreSplits(leftId);
+ task.addSplit(rightId, rightSplit);
+ task.noMoreSplits(rightId);
+ }
+
@Override
public void notifyCheckpointComplete(long checkpointId) throws Exception {
// TODO: notify velox
diff --git a/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenVectorOneInputOperator.java b/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenVectorOneInputOperator.java
deleted file mode 100644
index b55ebb6868..0000000000
--- a/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenVectorOneInputOperator.java
+++ /dev/null
@@ -1,203 +0,0 @@
-/*
- * 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.gluten.table.runtime.operators;
-
-import org.apache.gluten.streaming.api.operators.GlutenOperator;
-import org.apache.gluten.table.runtime.config.VeloxConnectorConfig;
-import org.apache.gluten.table.runtime.config.VeloxQueryConfig;
-
-import io.github.zhztheplayer.velox4j.Velox4j;
-import io.github.zhztheplayer.velox4j.connector.ExternalStreamConnectorSplit;
-import io.github.zhztheplayer.velox4j.connector.ExternalStreamTableHandle;
-import io.github.zhztheplayer.velox4j.connector.ExternalStreams;
-import io.github.zhztheplayer.velox4j.data.RowVector;
-import io.github.zhztheplayer.velox4j.iterator.UpIterator;
-import io.github.zhztheplayer.velox4j.memory.AllocationListener;
-import io.github.zhztheplayer.velox4j.memory.MemoryManager;
-import io.github.zhztheplayer.velox4j.plan.StatefulPlanNode;
-import io.github.zhztheplayer.velox4j.plan.TableScanNode;
-import io.github.zhztheplayer.velox4j.query.Query;
-import io.github.zhztheplayer.velox4j.query.SerialTask;
-import io.github.zhztheplayer.velox4j.serde.Serde;
-import io.github.zhztheplayer.velox4j.session.Session;
-import io.github.zhztheplayer.velox4j.stateful.StatefulElement;
-import io.github.zhztheplayer.velox4j.stateful.StatefulRecord;
-import io.github.zhztheplayer.velox4j.stateful.StatefulWatermark;
-import io.github.zhztheplayer.velox4j.type.RowType;
-
-import org.apache.flink.runtime.state.StateInitializationContext;
-import org.apache.flink.runtime.state.StateSnapshotContext;
-import org.apache.flink.streaming.api.operators.OneInputStreamOperator;
-import org.apache.flink.streaming.api.watermark.Watermark;
-import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
-import org.apache.flink.table.runtime.operators.TableStreamOperator;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.util.List;
-import java.util.Map;
-
-/** Calculate operator in gluten, which will call Velox to run. */
-public class GlutenVectorOneInputOperator extends TableStreamOperator<StatefulRecord>
- implements OneInputStreamOperator<StatefulRecord, StatefulRecord>, GlutenOperator {
-
- private static final Logger LOG = LoggerFactory.getLogger(GlutenVectorOneInputOperator.class);
-
- private final StatefulPlanNode glutenPlan;
- private final String id;
- private final RowType inputType;
- private final Map<String, RowType> outputTypes;
-
- private StreamRecord<StatefulRecord> outElement = null;
-
- private MemoryManager memoryManager;
- private Session session;
- private Query query;
- private ExternalStreams.BlockingQueue inputQueue;
- protected SerialTask task;
-
- public GlutenVectorOneInputOperator(
- StatefulPlanNode plan, String id, RowType inputType, Map<String, RowType> outputTypes) {
- this.glutenPlan = plan;
- this.id = id;
- this.inputType = inputType;
- this.outputTypes = outputTypes;
- }
-
- void initGlutenTask() {
- memoryManager = MemoryManager.create(AllocationListener.NOOP);
- session = Velox4j.newSession(memoryManager);
- // add a mock input as velox not allow the source is empty.
- StatefulPlanNode mockInput =
- new StatefulPlanNode(
- id,
- new TableScanNode(
- id,
- inputType,
- new ExternalStreamTableHandle("connector-external-stream"),
- List.of()));
- mockInput.addTarget(glutenPlan);
- LOG.debug("Gluten Plan: {}", Serde.toJson(mockInput));
- LOG.debug("OutTypes: {}", outputTypes.keySet());
- query =
- new Query(
- mockInput,
- VeloxQueryConfig.getConfig(getRuntimeContext()),
- VeloxConnectorConfig.getConfig(getRuntimeContext()));
- task = session.queryOps().execute(query);
- }
-
- @Override
- public void open() throws Exception {
- super.open();
- outElement = new StreamRecord(null);
- inputQueue = session.externalStreamOps().newBlockingQueue();
- ExternalStreamConnectorSplit split =
- new ExternalStreamConnectorSplit("connector-external-stream", inputQueue.id());
- task.addSplit(id, split);
- task.noMoreSplits(id);
- }
-
- @Override
- public void processElement(StreamRecord<StatefulRecord> element) {
- RowVector inRv = element.getValue().getRowVector();
- inputQueue.put(inRv);
- while (true) {
- UpIterator.State state = task.advance();
- if (state == UpIterator.State.AVAILABLE) {
- final StatefulElement statefulElement = task.statefulGet();
- if (statefulElement.isWatermark()) {
- StatefulWatermark watermark = statefulElement.asWatermark();
- output.emitWatermark(new Watermark(watermark.getTimestamp()));
- } else {
- final StatefulRecord statefulRecord = statefulElement.asRecord();
- output.collect(outElement.replace(statefulRecord));
- statefulRecord.close();
- }
- } else {
- break;
- }
- }
- inRv.close();
- }
-
- @Override
- public void close() throws Exception {
- inputQueue.close();
- task.close();
- session.close();
- memoryManager.close();
- }
-
- @Override
- public StatefulPlanNode getPlanNode() {
- return glutenPlan;
- }
-
- @Override
- public RowType getInputType() {
- return inputType;
- }
-
- @Override
- public Map<String, RowType> getOutputTypes() {
- return outputTypes;
- }
-
- @Override
- public String getId() {
- return id;
- }
-
- @Override
- public void prepareSnapshotPreBarrier(long checkpointId) throws Exception {
- // TODO: notify velox
- super.prepareSnapshotPreBarrier(checkpointId);
- }
-
- @Override
- public void snapshotState(StateSnapshotContext context) throws Exception {
- // TODO: implement it
- task.snapshotState(0);
- super.snapshotState(context);
- }
-
- @Override
- public void initializeState(StateInitializationContext context) throws Exception {
- if (task == null) {
- initGlutenTask();
- }
- // TODO: implement it
- task.initializeState(0);
- super.initializeState(context);
- }
-
- @Override
- public void notifyCheckpointComplete(long checkpointId) throws Exception {
- // TODO: notify velox
- task.notifyCheckpointComplete(checkpointId);
- super.notifyCheckpointComplete(checkpointId);
- }
-
- @Override
- public void notifyCheckpointAborted(long checkpointId) throws Exception {
- // TODO: notify velox
- task.notifyCheckpointAborted(checkpointId);
- super.notifyCheckpointAborted(checkpointId);
- }
-}
diff --git a/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenVectorSourceFunction.java b/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenVectorSourceFunction.java
deleted file mode 100644
index 67bc802946..0000000000
--- a/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/operators/GlutenVectorSourceFunction.java
+++ /dev/null
@@ -1,185 +0,0 @@
-/*
- * 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.gluten.table.runtime.operators;
-
-import org.apache.gluten.table.runtime.config.VeloxConnectorConfig;
-import org.apache.gluten.table.runtime.config.VeloxQueryConfig;
-import org.apache.gluten.table.runtime.metrics.SourceTaskMetrics;
-
-import io.github.zhztheplayer.velox4j.Velox4j;
-import io.github.zhztheplayer.velox4j.connector.ConnectorSplit;
-import io.github.zhztheplayer.velox4j.iterator.UpIterator;
-import io.github.zhztheplayer.velox4j.memory.AllocationListener;
-import io.github.zhztheplayer.velox4j.memory.MemoryManager;
-import io.github.zhztheplayer.velox4j.plan.StatefulPlanNode;
-import io.github.zhztheplayer.velox4j.query.Query;
-import io.github.zhztheplayer.velox4j.query.SerialTask;
-import io.github.zhztheplayer.velox4j.serde.Serde;
-import io.github.zhztheplayer.velox4j.session.Session;
-import io.github.zhztheplayer.velox4j.stateful.StatefulElement;
-import io.github.zhztheplayer.velox4j.type.RowType;
-
-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.source.RichParallelSourceFunction;
-import org.apache.flink.streaming.api.watermark.Watermark;
-
-import org.apache.arrow.memory.BufferAllocator;
-import org.apache.arrow.memory.RootAllocator;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import java.util.Map;
-
-/**
- * Gluten legacy source function, call velox plan to execute. It sends RowVector to downstream
- * instead of RowData to avoid data convert.
- */
-public class GlutenVectorSourceFunction extends RichParallelSourceFunction<StatefulElement>
- implements CheckpointedFunction {
- private static final Logger LOG = LoggerFactory.getLogger(GlutenVectorSourceFunction.class);
-
- private final StatefulPlanNode planNode;
- private final Map<String, RowType> outputTypes;
- private final String id;
- private final ConnectorSplit split;
- private volatile boolean isRunning = true;
-
- private Session session;
- private Query query;
- private BufferAllocator allocator;
- private MemoryManager memoryManager;
- private SerialTask task;
- private SourceTaskMetrics taskMetrics;
-
- public GlutenVectorSourceFunction(
- StatefulPlanNode planNode,
- Map<String, RowType> outputTypes,
- String id,
- ConnectorSplit split) {
- this.planNode = planNode;
- this.outputTypes = outputTypes;
- this.id = id;
- this.split = split;
- }
-
- public StatefulPlanNode getPlanNode() {
- return planNode;
- }
-
- public Map<String, RowType> getOutputTypes() {
- return outputTypes;
- }
-
- public String getId() {
- return id;
- }
-
- public ConnectorSplit getConnectorSplit() {
- return split;
- }
-
- @Override
- public void open(Configuration parameters) throws Exception {
- if (memoryManager == null) {
- memoryManager = MemoryManager.create(AllocationListener.NOOP);
- session = Velox4j.newSession(memoryManager);
- query =
- new Query(
- planNode,
- VeloxQueryConfig.getConfig(getRuntimeContext()),
- VeloxConnectorConfig.getConfig(getRuntimeContext()));
- allocator = new RootAllocator(Long.MAX_VALUE);
-
- task = session.queryOps().execute(query);
- task.addSplit(id, split);
- task.noMoreSplits(id);
- }
- taskMetrics = new SourceTaskMetrics(getRuntimeContext().getMetricGroup());
- }
-
- @Override
- public void run(SourceContext<StatefulElement> sourceContext) throws Exception {
- while (isRunning) {
- UpIterator.State state = task.advance();
- if (state == UpIterator.State.AVAILABLE) {
- final StatefulElement element = task.statefulGet();
- if (element.isWatermark()) {
- sourceContext.emitWatermark(new Watermark(element.asWatermark().getTimestamp()));
- } else {
- sourceContext.collect(element);
- }
- element.close();
- } else if (state == UpIterator.State.BLOCKED) {
- LOG.debug("Get empty row");
- } else {
- LOG.info("Velox task finished");
- break;
- }
- taskMetrics.updateMetrics(task, id);
- }
-
- task.close();
- session.close();
- memoryManager.close();
- allocator.close();
- }
-
- @Override
- public void cancel() {
- isRunning = false;
- }
-
- @Override
- public void snapshotState(FunctionSnapshotContext context) throws Exception {
- // TODO: implement it
- this.task.snapshotState(0);
- }
-
- @Override
- public void initializeState(FunctionInitializationContext context) throws Exception {
- if (memoryManager == null) {
- LOG.debug("Running GlutenSourceFunction: " + Serde.toJson(planNode));
- memoryManager = MemoryManager.create(AllocationListener.NOOP);
- session = Velox4j.newSession(memoryManager);
- query =
- new Query(
- planNode,
- VeloxQueryConfig.getConfig(getRuntimeContext()),
- VeloxConnectorConfig.getConfig(getRuntimeContext()));
- allocator = new RootAllocator(Long.MAX_VALUE);
-
- task = session.queryOps().execute(query);
- task.addSplit(id, split);
- task.noMoreSplits(id);
- }
- // TODO: implement it
- this.task.initializeState(0);
- }
-
- public String[] notifyCheckpointComplete(long checkpointId) throws Exception {
- // TODO: notify velox
- return this.task.notifyCheckpointComplete(checkpointId);
- }
-
- public void notifyCheckpointAborted(long checkpointId) throws Exception {
- // TODO: notify velox
- this.task.notifyCheckpointAborted(checkpointId);
- }
-}
diff --git a/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/typeutils/GlutenRowVectorSerializer.java b/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/typeutils/GlutenStatefulRecordSerializer.java
similarity index 83%
rename from gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/typeutils/GlutenRowVectorSerializer.java
rename to gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/typeutils/GlutenStatefulRecordSerializer.java
index db17a47a2d..1040e776c1 100644
--- a/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/typeutils/GlutenRowVectorSerializer.java
+++ b/gluten-flink/runtime/src/main/java/org/apache/gluten/table/runtime/typeutils/GlutenStatefulRecordSerializer.java
@@ -31,24 +31,31 @@ import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
import org.apache.flink.core.memory.DataInputView;
import org.apache.flink.core.memory.DataOutputView;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
import java.io.Closeable;
import java.io.IOException;
/** Serializer for {@link RowVector}. */
@Internal
-public class GlutenRowVectorSerializer extends TypeSerializer<StatefulRecord> implements Closeable {
+public class GlutenStatefulRecordSerializer extends TypeSerializer<StatefulRecord>
+ implements Closeable {
+ private static final Logger LOG = LoggerFactory.getLogger(GlutenStatefulRecordSerializer.class);
private static final long serialVersionUID = 1L;
private final RowType rowType;
private transient MemoryManager memoryManager;
private transient Session session;
+ private final String nodeId;
- public GlutenRowVectorSerializer(RowType rowType) {
+ public GlutenStatefulRecordSerializer(RowType rowType, String nodeId) {
+ this.nodeId = nodeId;
this.rowType = rowType;
}
@Override
public TypeSerializer<StatefulRecord> duplicate() {
- return new GlutenRowVectorSerializer(rowType);
+ return new GlutenStatefulRecordSerializer(rowType, nodeId);
}
@Override
@@ -73,7 +80,7 @@ public class GlutenRowVectorSerializer extends TypeSerializer<StatefulRecord> im
byte[] str = new byte[len];
source.readFully(str);
RowVector rowVector = session.baseVectorOps().deserializeOne(new String(str)).asRowVector();
- StatefulRecord record = new StatefulRecord(null, 0, 0, false, -1);
+ StatefulRecord record = new StatefulRecord(nodeId, 0, 0, false, -1);
record.setRowVector(rowVector);
return record;
}
@@ -85,7 +92,7 @@ public class GlutenRowVectorSerializer extends TypeSerializer<StatefulRecord> im
@Override
public StatefulRecord copy(StatefulRecord from) {
- throw new RuntimeException("Not implemented for gluten");
+ return from;
}
@Override
@@ -100,9 +107,9 @@ public class GlutenRowVectorSerializer extends TypeSerializer<StatefulRecord> im
@Override
public boolean equals(Object obj) {
- if (obj instanceof GlutenRowVectorSerializer) {
+ if (obj instanceof GlutenStatefulRecordSerializer) {
if (rowType != null) {
- GlutenRowVectorSerializer other = (GlutenRowVectorSerializer) obj;
+ GlutenStatefulRecordSerializer other = (GlutenStatefulRecordSerializer) obj;
return rowType.equals(other.rowType);
}
return true;
@@ -121,7 +128,7 @@ public class GlutenRowVectorSerializer extends TypeSerializer<StatefulRecord> im
@Override
public boolean isImmutableType() {
- return false;
+ return true;
}
@Override
@@ -131,7 +138,7 @@ public class GlutenRowVectorSerializer extends TypeSerializer<StatefulRecord> im
@Override
public TypeSerializerSnapshot<StatefulRecord> snapshotConfiguration() {
- return new RowVectorSerializerSnapshot(rowType);
+ return new RowVectorSerializerSnapshot(rowType, nodeId);
}
@Override
@@ -148,13 +155,15 @@ public class GlutenRowVectorSerializer extends TypeSerializer<StatefulRecord> im
private static final int CURRENT_VERSION = 1;
private RowType rowType;
+ private String nodeId;
@SuppressWarnings("unused")
public RowVectorSerializerSnapshot() {
// this constructor is used when restoring from a checkpoint/savepoint.
}
- RowVectorSerializerSnapshot(RowType rowType) {
+ RowVectorSerializerSnapshot(RowType rowType, String nodeId) {
+ this.nodeId = nodeId;
this.rowType = rowType;
}
@@ -171,8 +180,8 @@ public class GlutenRowVectorSerializer extends TypeSerializer<StatefulRecord> im
throws IOException {}
@Override
- public GlutenRowVectorSerializer restoreSerializer() {
- return new GlutenRowVectorSerializer(rowType);
+ public GlutenStatefulRecordSerializer restoreSerializer() {
+ return new GlutenStatefulRecordSerializer(rowType, nodeId);
}
@Override
diff --git a/gluten-flink/runtime/src/main/java/org/apache/gluten/util/Utils.java b/gluten-flink/runtime/src/main/java/org/apache/gluten/util/Utils.java
index d580a520af..eaa6a7d20e 100644
--- a/gluten-flink/runtime/src/main/java/org/apache/gluten/util/Utils.java
+++ b/gluten-flink/runtime/src/main/java/org/apache/gluten/util/Utils.java
@@ -16,63 +16,55 @@
*/
package org.apache.gluten.util;
+import org.apache.gluten.streaming.api.operators.GlutenOneInputOperatorFactory;
+import org.apache.gluten.streaming.api.operators.GlutenOperator;
+
import org.apache.flink.connector.file.table.stream.PartitionCommitInfo;
-import org.apache.flink.runtime.jobgraph.IntermediateDataSetID;
import org.apache.flink.streaming.api.graph.StreamConfig;
-import org.apache.flink.streaming.runtime.tasks.StreamTaskException;
-import org.apache.flink.util.InstantiationUtil;
+import org.apache.flink.streaming.api.operators.SimpleOperatorFactory;
+import org.apache.flink.streaming.api.operators.StreamOperator;
+import org.apache.flink.streaming.api.operators.StreamOperatorFactory;
-import java.io.IOException;
import java.io.Serializable;
-import java.util.Map;
+import java.util.Optional;
/** Utils to add and get some infos to StreamConfig. */
public class Utils {
- public static void setNodeToChainedOutputs(
- StreamConfig streamConfig, Map<String, Integer> nodeToChainedOutputs) {
- setConfigToStreamConfig("_nodeToChainedOutputs", streamConfig, nodeToChainedOutputs);
- }
-
- public static Map<String, Integer> getNodeToChainedOutputs(
- StreamConfig streamConfig, ClassLoader userClassLoader) {
- return getConfigFromStreamConfig("_nodeToChainedOutputs", streamConfig, userClassLoader);
- }
-
- public static void setNodeToNonChainedOutputs(
- StreamConfig streamConfig, Map<IntermediateDataSetID, String> nodeToChainedOutputs) {
- setConfigToStreamConfig("_nodeToNonChainedOutputs", streamConfig, nodeToChainedOutputs);
- }
-
- public static Map<IntermediateDataSetID, String> getNodeToNonChainedOutputs(
- StreamConfig streamConfig, ClassLoader userClassLoader) {
- return getConfigFromStreamConfig("_nodeToNonChainedOutputs", streamConfig, userClassLoader);
- }
-
- private static <T> T getConfigFromStreamConfig(
- String key, StreamConfig streamConfig, ClassLoader userClassLoader) {
- try {
- return InstantiationUtil.readObjectFromConfig(
- streamConfig.getConfiguration(), key, userClassLoader);
- } catch (Exception e) {
- throw new StreamTaskException("Could not instantiate serializer.", e);
- }
- }
-
- private static void setConfigToStreamConfig(
- String key, StreamConfig streamConfig, Object object) {
- try {
- InstantiationUtil.writeObjectToConfig(object, streamConfig.getConfiguration(), key);
- } catch (IOException e) {
- throw new StreamTaskException(
- String.format("Could not serialize object for key %s.", key), e);
- }
- }
-
+ /**
+ * Constructs a PartitionCommitInfo object for checkpoint completion notification.
+ *
+ * @param id checkpoint ID
+ * @param subtaskIndex index of the subtask
+ * @param numberOfSubtasks total number of subtasks
+ * @param committed array of committed partition paths
+ * @return Serializable PartitionCommitInfo object
+ */
public static Serializable constructCommitInfo(
long id, int subtaskIndex, int numberOfSubtasks, String[] committed) {
PartitionCommitInfo commitInfo =
new PartitionCommitInfo(id, subtaskIndex, numberOfSubtasks, committed);
return commitInfo;
}
+
+ /**
+ * Extracts a GlutenOperator from a StreamConfig if it exists.
+ *
+ * @param streamConfig the stream configuration to check
+ * @param userClassLoader the class loader to use for deserialization
+ * @return Optional containing the GlutenOperator if found, empty otherwise
+ */
+ public static Optional<GlutenOperator> getGlutenOperator(
+ StreamConfig streamConfig, ClassLoader userClassLoader) {
+ StreamOperatorFactory operatorFactory = streamConfig.getStreamOperatorFactory(userClassLoader);
+ if (operatorFactory instanceof SimpleOperatorFactory) {
+ StreamOperator streamOperator = streamConfig.getStreamOperator(userClassLoader);
+ if (streamOperator instanceof GlutenOperator) {
+ return Optional.of((GlutenOperator) streamOperator);
+ }
+ } else if (operatorFactory instanceof GlutenOneInputOperatorFactory) {
+ return Optional.of(((GlutenOneInputOperatorFactory) operatorFactory).getOperator());
+ }
+ return Optional.empty();
+ }
}
diff --git a/gluten-flink/runtime/src/main/java/org/apache/gluten/util/VectorInputBridge.java b/gluten-flink/runtime/src/main/java/org/apache/gluten/util/VectorInputBridge.java
new file mode 100644
index 0000000000..cf8f3d4de4
--- /dev/null
+++ b/gluten-flink/runtime/src/main/java/org/apache/gluten/util/VectorInputBridge.java
@@ -0,0 +1,115 @@
+/*
+ * 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.gluten.util;
+
+import io.github.zhztheplayer.velox4j.session.Session;
+import io.github.zhztheplayer.velox4j.stateful.StatefulRecord;
+import io.github.zhztheplayer.velox4j.type.RowType;
+
+import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
+
+import org.apache.arrow.memory.BufferAllocator;
+
+import java.io.Serializable;
+
+/**
+ * Interface for converting input data from Flink StreamRecord to StatefulRecord. Different
+ * implementations handle different input types.
+ */
+public interface VectorInputBridge<IN> extends Serializable {
+
+ /**
+ * Converts a StreamRecord to StatefulRecord based on the input type.
+ *
+ * @param inputData the input StreamRecord
+ * @param allocator buffer allocator for creating RowVector
+ * @param session Velox session
+ * @param inputType the RowType schema of the input
+ * @return StatefulRecord containing the converted or original data
+ */
+ StatefulRecord convertToStatefulRecord(
+ StreamRecord<IN> inputData, BufferAllocator allocator, Session session, RowType inputType);
+
+ /** Factory for creating VectorInputBridge instances based on input type. */
+ class Factory {
+ /**
+ * Creates a VectorInputBridge instance for the given input class.
+ *
+ * @param inputClass the input class type
+ * @param nodeId the node ID for the bridge
+ * @param <IN> the input type
+ * @return a VectorInputBridge instance
+ * @throws UnsupportedOperationException if input class is not supported
+ */
+ public static <IN> VectorInputBridge<IN> create(Class<IN> inputClass, String nodeId) {
+ if (inputClass.isAssignableFrom(org.apache.flink.table.data.RowData.class)) {
+ @SuppressWarnings("unchecked")
+ VectorInputBridge<IN> bridge = (VectorInputBridge<IN>) new RowDataInputBridge(nodeId);
+ return bridge;
+ } else if (inputClass.isAssignableFrom(StatefulRecord.class)) {
+ @SuppressWarnings("unchecked")
+ VectorInputBridge<IN> bridge = (VectorInputBridge<IN>) new StatefulRecordInputBridge();
+ return bridge;
+ } else {
+ throw new UnsupportedOperationException("Unsupported input class: " + inputClass.getName());
+ }
+ }
+ }
+
+ /**
+ * Implementation for RowData input type. Converts RowData to RowVector and wraps in
+ * StatefulRecord.
+ */
+ class RowDataInputBridge implements VectorInputBridge<org.apache.flink.table.data.RowData> {
+ private static final long serialVersionUID = 1L;
+ private final String nodeId;
+
+ public RowDataInputBridge(String nodeId) {
+ this.nodeId = nodeId;
+ }
+
+ @Override
+ public StatefulRecord convertToStatefulRecord(
+ StreamRecord<org.apache.flink.table.data.RowData> inputData,
+ BufferAllocator allocator,
+ Session session,
+ RowType inputType) {
+ org.apache.flink.table.data.RowData rowData = inputData.getValue();
+ var rowVector =
+ org.apache.gluten.vectorized.FlinkRowToVLVectorConvertor.fromRowData(
+ rowData, allocator, session, inputType);
+ StatefulRecord statefulRecord = new StatefulRecord(nodeId, rowVector.id(), 0, false, -1);
+ statefulRecord.setRowVector(rowVector);
+ return statefulRecord;
+ }
+ }
+
+ /** Implementation for StatefulRecord input type. Passes through the StatefulRecord directly. */
+ class StatefulRecordInputBridge implements VectorInputBridge<StatefulRecord> {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ public StatefulRecord convertToStatefulRecord(
+ StreamRecord<StatefulRecord> inputData,
+ BufferAllocator allocator,
+ Session session,
+ RowType inputType) {
+ // Pass through the StatefulRecord directly. This bridge does not take ownership of or close.
+ return inputData.getValue();
+ }
+ }
+}
diff --git a/gluten-flink/runtime/src/main/java/org/apache/gluten/util/VectorOutputBridge.java b/gluten-flink/runtime/src/main/java/org/apache/gluten/util/VectorOutputBridge.java
new file mode 100644
index 0000000000..4849f90e94
--- /dev/null
+++ b/gluten-flink/runtime/src/main/java/org/apache/gluten/util/VectorOutputBridge.java
@@ -0,0 +1,137 @@
+/*
+ * 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.gluten.util;
+
+import io.github.zhztheplayer.velox4j.stateful.StatefulRecord;
+import io.github.zhztheplayer.velox4j.type.RowType;
+
+import org.apache.flink.streaming.api.operators.Output;
+import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
+
+import org.apache.arrow.memory.BufferAllocator;
+
+import java.io.Serializable;
+
+/**
+ * Interface for converting output data from StatefulRecord to Flink StreamRecord. Different
+ * implementations handle different output types.
+ */
+public interface VectorOutputBridge<OUT> extends Serializable {
+
+ /**
+ * Converts a StatefulRecord to the appropriate output type and collects it to the output.
+ *
+ * @param collector the Flink output collector
+ * @param record the StatefulRecord to convert and collect
+ * @param allocator buffer allocator for converting RowVector
+ * @param outputType the RowType schema of the output
+ */
+ void collect(
+ Output<StreamRecord<OUT>> collector,
+ StatefulRecord record,
+ BufferAllocator allocator,
+ RowType outputType);
+
+ /** Factory for creating VectorOutputBridge instances based on output type. */
+ class Factory {
+ /**
+ * Creates a VectorOutputBridge instance for the given output class.
+ *
+ * @param outputClass the output class type
+ * @param <OUT> the output type
+ * @return a VectorOutputBridge instance
+ * @throws UnsupportedOperationException if output class is not supported
+ */
+ public static <OUT> VectorOutputBridge<OUT> create(Class<OUT> outputClass) {
+ if (outputClass.isAssignableFrom(org.apache.flink.table.data.RowData.class)) {
+ @SuppressWarnings("unchecked")
+ VectorOutputBridge<OUT> bridge = (VectorOutputBridge<OUT>) new RowDataOutputBridge();
+ return bridge;
+ } else if (outputClass.isAssignableFrom(StatefulRecord.class)) {
+ @SuppressWarnings("unchecked")
+ VectorOutputBridge<OUT> bridge = (VectorOutputBridge<OUT>) new StatefulRecordOutputBridge();
+ return bridge;
+ } else {
+ throw new UnsupportedOperationException(
+ "Unsupported output class: " + outputClass.getName());
+ }
+ }
+ }
+
+ /**
+ * Implementation for RowData output type. Converts RowVector from StatefulRecord to RowData and
+ * collects to output.
+ */
+ class RowDataOutputBridge implements VectorOutputBridge<org.apache.flink.table.data.RowData> {
+ private static final long serialVersionUID = 1L;
+ private transient StreamRecord<org.apache.flink.table.data.RowData> outputElement;
+
+ public RowDataOutputBridge() {
+ this.outputElement = new StreamRecord<>(null);
+ }
+
+ @Override
+ public void collect(
+ Output<StreamRecord<org.apache.flink.table.data.RowData>> collector,
+ StatefulRecord record,
+ BufferAllocator allocator,
+ RowType outputType) {
+ java.util.List<org.apache.flink.table.data.RowData> rows =
+ org.apache.gluten.vectorized.FlinkRowToVLVectorConvertor.toRowData(
+ record.getRowVector(), allocator, outputType);
+ for (org.apache.flink.table.data.RowData row : rows) {
+ collector.collect(getOrCreateOutputElement().replace(row));
+ }
+ }
+
+ private StreamRecord<org.apache.flink.table.data.RowData> getOrCreateOutputElement() {
+ if (outputElement == null) {
+ outputElement = new StreamRecord<>(null);
+ }
+ return outputElement;
+ }
+ }
+
+ /**
+ * Implementation for StatefulRecord output type. Passes through the StatefulRecord directly to
+ * output.
+ */
+ class StatefulRecordOutputBridge implements VectorOutputBridge<StatefulRecord> {
+ private static final long serialVersionUID = 1L;
+ private transient StreamRecord<StatefulRecord> outputElement;
+
+ public StatefulRecordOutputBridge() {
+ this.outputElement = new StreamRecord<>(null);
+ }
+
+ @Override
+ public void collect(
+ Output<StreamRecord<StatefulRecord>> collector,
+ StatefulRecord record,
+ BufferAllocator allocator,
+ RowType outputType) {
+ collector.collect(getOrCreateOutputElement().replace(record));
+ }
+
+ private StreamRecord<StatefulRecord> getOrCreateOutputElement() {
+ if (outputElement == null) {
+ outputElement = new StreamRecord<>(null);
+ }
+ return outputElement;
+ }
+ }
+}
diff --git a/gluten-flink/runtime/src/main/java/org/apache/gluten/vectorized/ArrowVectorWriter.java b/gluten-flink/runtime/src/main/java/org/apache/gluten/vectorized/ArrowVectorWriter.java
index d96f48ecbe..e6a511a492 100644
--- a/gluten-flink/runtime/src/main/java/org/apache/gluten/vectorized/ArrowVectorWriter.java
+++ b/gluten-flink/runtime/src/main/java/org/apache/gluten/vectorized/ArrowVectorWriter.java
@@ -395,11 +395,18 @@ class VarCharVectorWriter extends BaseVectorWriter<VarCharVector, byte[]> {
}
}
-class TimestampVectorWriter extends BaseVectorWriter<TimeStampMilliVector, Long> {
+class TimestampVectorWriter extends BaseVectorWriter<FieldVector, Long> {
private final int precision = 3; // Millisecond precision
public TimestampVectorWriter(Type fieldType, BufferAllocator allocator, FieldVector vector) {
super(vector);
+ // Verify that the vector is a timestamp vector (either TimeStampMilliVector or
+ // TimeStampMilliTZVector)
+ if (!(vector instanceof TimeStampMilliVector) && !(vector instanceof TimeStampMilliTZVector)) {
+ throw new IllegalArgumentException(
+ "Expected TimeStampMilliVector or TimeStampMilliTZVector, but got: "
+ + vector.getClass().getName());
+ }
}
@Override
@@ -414,7 +421,16 @@ class TimestampVectorWriter extends BaseVectorWriter<TimeStampMilliVector, Long>
@Override
protected void setValue(int index, Long value) {
- this.typedVector.setSafe(index, value);
+
+ // Both TimeStampMilliVector and TimeStampMilliTZVector support setSafe with long value
+ if (this.typedVector instanceof TimeStampMilliVector) {
+ ((TimeStampMilliVector) this.typedVector).setSafe(index, value);
+ } else if (this.typedVector instanceof TimeStampMilliTZVector) {
+ ((TimeStampMilliTZVector) this.typedVector).setSafe(index, value);
+ } else {
+ throw new IllegalStateException(
+ "Unexpected vector type: " + this.typedVector.getClass().getName());
+ }
}
}
diff --git a/gluten-flink/ut/src/test/java/org/apache/gluten/streaming/api/operators/GlutenStreamFilterTest.java b/gluten-flink/ut/src/test/java/org/apache/gluten/streaming/api/operators/GlutenStreamFilterTest.java
index 15a3968fe9..1955ece25a 100644
--- a/gluten-flink/ut/src/test/java/org/apache/gluten/streaming/api/operators/GlutenStreamFilterTest.java
+++ b/gluten-flink/ut/src/test/java/org/apache/gluten/streaming/api/operators/GlutenStreamFilterTest.java
@@ -216,7 +216,9 @@ public class GlutenStreamFilterTest extends GlutenStreamOperatorTestBase {
new io.github.zhztheplayer.velox4j.plan.StatefulPlanNode(veloxPlan.getId(), veloxPlan),
PlanNodeIdGenerator.newId(),
veloxType,
- Map.of(veloxPlan.getId(), veloxType));
+ Map.of(veloxPlan.getId(), veloxType),
+ RowData.class,
+ RowData.class);
}
@Override
diff --git a/gluten-flink/ut/src/test/java/org/apache/gluten/streaming/api/operators/GlutenStreamJoinOperatorTest.java b/gluten-flink/ut/src/test/java/org/apache/gluten/streaming/api/operators/GlutenStreamJoinOperatorTest.java
index 8853239865..9215431a54 100644
--- a/gluten-flink/ut/src/test/java/org/apache/gluten/streaming/api/operators/GlutenStreamJoinOperatorTest.java
+++ b/gluten-flink/ut/src/test/java/org/apache/gluten/streaming/api/operators/GlutenStreamJoinOperatorTest.java
@@ -16,7 +16,7 @@
*/
package org.apache.gluten.streaming.api.operators;
-import org.apache.gluten.table.runtime.operators.GlutenVectorTwoInputOperator;
+import org.apache.gluten.table.runtime.operators.GlutenTwoInputOperator;
import org.apache.flink.table.data.GenericRowData;
import org.apache.flink.table.data.RowData;
@@ -77,7 +77,7 @@ public class GlutenStreamJoinOperatorTest extends GlutenStreamJoinOperatorTestBa
@Test
public void testInnerJoin() throws Exception {
- GlutenVectorTwoInputOperator operator = createGlutenJoinOperator(FlinkJoinType.INNER);
+ GlutenTwoInputOperator operator = createGlutenJoinOperator(FlinkJoinType.INNER);
List<RowData> expectedOutput =
Arrays.asList(
@@ -99,7 +99,7 @@ public class GlutenStreamJoinOperatorTest extends GlutenStreamJoinOperatorTestBa
@Test
@Disabled
public void testLeftJoin() throws Exception {
- GlutenVectorTwoInputOperator operator = createGlutenJoinOperator(FlinkJoinType.LEFT);
+ GlutenTwoInputOperator operator = createGlutenJoinOperator(FlinkJoinType.LEFT);
List<RowData> expectedOutput =
Arrays.asList(
@@ -127,7 +127,7 @@ public class GlutenStreamJoinOperatorTest extends GlutenStreamJoinOperatorTestBa
@Test
@Disabled
public void testRightJoin() throws Exception {
- GlutenVectorTwoInputOperator operator = createGlutenJoinOperator(FlinkJoinType.RIGHT);
+ GlutenTwoInputOperator operator = createGlutenJoinOperator(FlinkJoinType.RIGHT);
List<RowData> expectedOutput =
Arrays.asList(
@@ -151,7 +151,7 @@ public class GlutenStreamJoinOperatorTest extends GlutenStreamJoinOperatorTestBa
@Test
@Disabled
public void testFullOuterJoin() throws Exception {
- GlutenVectorTwoInputOperator operator = createGlutenJoinOperator(FlinkJoinType.FULL);
+ GlutenTwoInputOperator operator = createGlutenJoinOperator(FlinkJoinType.FULL);
List<RowData> expectedOutput =
Arrays.asList(
@@ -181,7 +181,7 @@ public class GlutenStreamJoinOperatorTest extends GlutenStreamJoinOperatorTestBa
@Test
public void testInnerJoinWithNonEquiCondition() throws Exception {
RexNode nonEquiCondition = createNonEquiCondition();
- GlutenVectorTwoInputOperator operator =
+ GlutenTwoInputOperator operator =
createGlutenJoinOperator(FlinkJoinType.INNER, nonEquiCondition);
List<RowData> expectedOutput =
diff --git a/gluten-flink/ut/src/test/java/org/apache/gluten/streaming/api/operators/GlutenStreamJoinOperatorTestBase.java b/gluten-flink/ut/src/test/java/org/apache/gluten/streaming/api/operators/GlutenStreamJoinOperatorTestBase.java
index 7aa7f57d68..8b0e040a3d 100644
--- a/gluten-flink/ut/src/test/java/org/apache/gluten/streaming/api/operators/GlutenStreamJoinOperatorTestBase.java
+++ b/gluten-flink/ut/src/test/java/org/apache/gluten/streaming/api/operators/GlutenStreamJoinOperatorTestBase.java
@@ -19,7 +19,7 @@ package org.apache.gluten.streaming.api.operators;
import org.apache.gluten.rexnode.RexConversionContext;
import org.apache.gluten.rexnode.RexNodeConverter;
import org.apache.gluten.rexnode.Utils;
-import org.apache.gluten.table.runtime.operators.GlutenVectorTwoInputOperator;
+import org.apache.gluten.table.runtime.operators.GlutenTwoInputOperator;
import org.apache.gluten.table.runtime.stream.common.Velox4jEnvironment;
import org.apache.gluten.util.LogicalTypeConverter;
import org.apache.gluten.util.PlanNodeIdGenerator;
@@ -160,11 +160,11 @@ public abstract class GlutenStreamJoinOperatorTestBase extends StreamingJoinOper
return outputRowType;
}
- protected GlutenVectorTwoInputOperator createGlutenJoinOperator(FlinkJoinType joinType) {
+ protected GlutenTwoInputOperator createGlutenJoinOperator(FlinkJoinType joinType) {
return createGlutenJoinOperator(joinType, null);
}
- protected GlutenVectorTwoInputOperator createGlutenJoinOperator(
+ protected GlutenTwoInputOperator createGlutenJoinOperator(
FlinkJoinType joinType, RexNode nonEquiCondition) {
JoinType veloxJoinType = Utils.toVLJoinType(joinType);
@@ -226,13 +226,15 @@ public abstract class GlutenStreamJoinOperatorTestBase extends StreamingJoinOper
outputVeloxType,
1024);
- return new GlutenVectorTwoInputOperator(
+ return new GlutenTwoInputOperator(
new StatefulPlanNode(join.getId(), join),
leftInput.getId(),
rightInput.getId(),
leftVeloxType,
rightVeloxType,
- Map.of(join.getId(), outputVeloxType));
+ Map.of(join.getId(), outputVeloxType),
+ RowData.class,
+ RowData.class);
}
protected void processTestData(
@@ -281,7 +283,7 @@ public abstract class GlutenStreamJoinOperatorTestBase extends StreamingJoinOper
}
protected void executeJoinTest(
- GlutenVectorTwoInputOperator operator,
+ GlutenTwoInputOperator operator,
List<RowData> leftData,
List<RowData> rightData,
List<RowData> expectedOutput)
diff --git a/gluten-flink/ut/src/test/java/org/apache/gluten/streaming/api/operators/GlutenStreamOperatorTestBase.java b/gluten-flink/ut/src/test/java/org/apache/gluten/streaming/api/operators/GlutenStreamOperatorTestBase.java
index 224dcfd191..f26bb9038d 100644
--- a/gluten-flink/ut/src/test/java/org/apache/gluten/streaming/api/operators/GlutenStreamOperatorTestBase.java
+++ b/gluten-flink/ut/src/test/java/org/apache/gluten/streaming/api/operators/GlutenStreamOperatorTestBase.java
@@ -165,6 +165,8 @@ public abstract class GlutenStreamOperatorTestBase {
new StatefulPlanNode(veloxPlan.getId(), veloxPlan),
PlanNodeIdGenerator.newId(),
inputVeloxType,
- Map.of(veloxPlan.getId(), outputVeloxType));
+ Map.of(veloxPlan.getId(), outputVeloxType),
+ RowData.class,
+ RowData.class);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@gluten.apache.org
For additional commands, e-mail: commits-help@gluten.apache.org