This is an automated email from the ASF dual-hosted git repository.

JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new 99cf3acebe [flink] Support task off-heap memory for writer slot 
sharing groups. (#11053)
99cf3acebe is described below

commit 99cf3acebed3f6ef00d1d1dedc3d029838e00b15
Author: Master.TJ <[email protected]>
AuthorDate: Tue Nov 10 16:04:35 2026 +0800

    [flink] Support task off-heap memory for writer slot sharing groups. 
(#11053)
---
 docs/docs/flink/configuration.md                   |  28 +++
 docs/generated/flink_connector_configuration.html  |   6 +
 .../flink/sink/cdc/FlinkCdcMultiTableSink.java     |  32 +++-
 .../sink/cdc/FlinkCdcSyncDatabaseSinkBuilder.java  |   3 +
 .../flink/sink/cdc/FlinkCdcMultiTableSinkTest.java |  69 ++++++-
 .../apache/paimon/flink/FlinkConnectorOptions.java |   8 +
 .../org/apache/paimon/flink/sink/FlinkSink.java    |  29 ++-
 .../paimon/flink/sink/WriterOffHeapMemoryTest.java | 200 +++++++++++++++++++++
 8 files changed, 370 insertions(+), 5 deletions(-)

diff --git a/docs/docs/flink/configuration.md b/docs/docs/flink/configuration.md
index 4ece3c5256..ee79fa18ad 100644
--- a/docs/docs/flink/configuration.md
+++ b/docs/docs/flink/configuration.md
@@ -91,6 +91,34 @@ buffers still use heap memory. The existing downstream slot 
sharing and explicit
 resource settings are unchanged. Leaving the new option unset preserves 
existing resource
 configuration, including default-slot managed allocation.
 
+### Task Off-Heap Memory for an Explicit Writer Resource Group
+
+Use `sink.writer-offheap-memory` to request task off-heap memory for the 
writer slot sharing
+group. This optional budget must be positive and requires 
`sink.writer-memory`; `sink.writer-cpu`
+retains its default of one core unless explicitly configured. Memory units 
such as `kb`, `mb`,
+and `gb` are supported, and values retain their byte precision.
+
+```sql
+INSERT INTO paimon_table /*+ OPTIONS(
+    'sink.writer-memory' = '512 mb',
+    'sink.writer-offheap-memory' = '128 mb',
+    'sink.writer-managed-memory' = '64 mb'
+) */
+SELECT * FROM source_table;
+```
+
+Task off-heap memory and managed memory are separate resource budgets. The 
off-heap budget is
+a request for each shared slot of the writer group, not a total across writer 
subtasks or a
+guaranteed allocation to the writer alone. It does not switch writer buffers 
to off-heap memory
+or enable the managed allocator. Allocation follows Flink's
+[fine-grained resource 
management](https://nightlies.apache.org/flink/flink-docs-release-1.20/docs/deployment/finegrained_resource/)
+requirements and available resources.
+
+This option applies to ordinary sink writers and combined CDC multi-table 
writers. Existing
+downstream slot sharing is preserved; a committer with its own explicit 
resource group does not
+receive the writer's off-heap budget. Leaving the option unset preserves 
existing resource
+configuration.
+
 ## Setting dynamic options
 
 Dynamic options tune a query or a subsequently submitted job without changing 
the options
diff --git a/docs/generated/flink_connector_configuration.html 
b/docs/generated/flink_connector_configuration.html
index b713195233..915de83b78 100644
--- a/docs/generated/flink_connector_configuration.html
+++ b/docs/generated/flink_connector_configuration.html
@@ -380,6 +380,12 @@ under the License.
             <td>MemorySize</td>
             <td>Sink writer memory to control heap memory of writer.</td>
         </tr>
+        <tr>
+            <td><h5>sink.writer-offheap-memory</h5></td>
+            <td style="word-wrap: break-word;">(none)</td>
+            <td>MemorySize</td>
+            <td>Task off-heap memory resource budget for the writer slot 
sharing group. Must be positive and requires sink.writer-memory.</td>
+        </tr>
         <tr>
             <td><h5>sink.writer-refresh-detectors</h5></td>
             <td style="word-wrap: break-word;">(none)</td>
diff --git 
a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/sink/cdc/FlinkCdcMultiTableSink.java
 
b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/sink/cdc/FlinkCdcMultiTableSink.java
index fcf9f2745a..235b4ffc8c 100644
--- 
a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/sink/cdc/FlinkCdcMultiTableSink.java
+++ 
b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/sink/cdc/FlinkCdcMultiTableSink.java
@@ -73,6 +73,7 @@ public class FlinkCdcMultiTableSink implements Serializable {
     private final double writeCpuCores;
     private final MemorySize writeHeapMemory;
     @Nullable private final MemorySize writeManagedMemory;
+    @Nullable private final MemorySize writeOffHeapMemory;
     private final double commitCpuCores;
     @Nullable private final MemorySize commitHeapMemory;
     private final String commitUser;
@@ -101,6 +102,7 @@ public class FlinkCdcMultiTableSink implements Serializable 
{
         this.writeCpuCores = writeCpuCores;
         this.writeHeapMemory = writeHeapMemory;
         this.writeManagedMemory = null;
+        this.writeOffHeapMemory = null;
         this.commitCpuCores = commitCpuCores;
         this.commitHeapMemory = commitHeapMemory;
         this.commitUser = commitUser;
@@ -142,11 +144,38 @@ public class FlinkCdcMultiTableSink implements 
Serializable {
             String commitUser,
             boolean eagerInit,
             TableFilter tableFilter) {
+        this(
+                catalogLoader,
+                databaseName,
+                writeCpuCores,
+                writeHeapMemory,
+                writeManagedMemory,
+                null,
+                commitCpuCores,
+                commitHeapMemory,
+                commitUser,
+                eagerInit,
+                tableFilter);
+    }
+
+    public FlinkCdcMultiTableSink(
+            CatalogLoader catalogLoader,
+            String databaseName,
+            double writeCpuCores,
+            @Nullable MemorySize writeHeapMemory,
+            @Nullable MemorySize writeManagedMemory,
+            @Nullable MemorySize writeOffHeapMemory,
+            double commitCpuCores,
+            @Nullable MemorySize commitHeapMemory,
+            String commitUser,
+            boolean eagerInit,
+            TableFilter tableFilter) {
         this.catalogLoader = catalogLoader;
         this.databaseName = Preconditions.checkNotNull(databaseName);
         this.writeCpuCores = writeCpuCores;
         this.writeHeapMemory = writeHeapMemory;
         this.writeManagedMemory = writeManagedMemory;
+        this.writeOffHeapMemory = writeOffHeapMemory;
         this.commitCpuCores = commitCpuCores;
         this.commitHeapMemory = commitHeapMemory;
         this.commitUser = commitUser;
@@ -222,7 +251,8 @@ public class FlinkCdcMultiTableSink implements Serializable 
{
                 shuffled.transform(
                         WRITER_NAME, typeInfo, 
createWriteOperator(sinkProvider, commitUser));
         forwardParallelism(written, shuffled);
-        configureSlotSharingGroup(written, writeCpuCores, writeHeapMemory, 
writeManagedMemory);
+        configureSlotSharingGroup(
+                written, writeCpuCores, writeHeapMemory, writeManagedMemory, 
writeOffHeapMemory);
 
         // shuffle committables by table
         DataStream<MultiTableCommittable> partitioned =
diff --git 
a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/sink/cdc/FlinkCdcSyncDatabaseSinkBuilder.java
 
b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/sink/cdc/FlinkCdcSyncDatabaseSinkBuilder.java
index 2e44c88bbe..098879e9bc 100644
--- 
a/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/sink/cdc/FlinkCdcSyncDatabaseSinkBuilder.java
+++ 
b/paimon-flink/paimon-flink-cdc/src/main/java/org/apache/paimon/flink/sink/cdc/FlinkCdcSyncDatabaseSinkBuilder.java
@@ -70,6 +70,7 @@ public class FlinkCdcSyncDatabaseSinkBuilder<T> {
     private double writerCpu;
     @Nullable private MemorySize writerMemory;
     @Nullable private MemorySize writerManagedMemory;
+    @Nullable private MemorySize writerOffHeapMemory;
     private double committerCpu;
     @Nullable private MemorySize committerMemory;
 
@@ -113,6 +114,7 @@ public class FlinkCdcSyncDatabaseSinkBuilder<T> {
         this.writerCpu = options.get(FlinkConnectorOptions.SINK_WRITER_CPU);
         this.writerMemory = 
options.get(FlinkConnectorOptions.SINK_WRITER_MEMORY);
         this.writerManagedMemory = 
options.get(FlinkConnectorOptions.SINK_WRITER_MANAGED_MEMORY);
+        this.writerOffHeapMemory = 
options.get(FlinkConnectorOptions.SINK_WRITER_OFFHEAP_MEMORY);
         this.committerCpu = 
options.get(FlinkConnectorOptions.SINK_COMMITTER_CPU);
         this.committerMemory = 
options.get(FlinkConnectorOptions.SINK_COMMITTER_MEMORY);
         this.commitUser = createCommitUser(options);
@@ -194,6 +196,7 @@ public class FlinkCdcSyncDatabaseSinkBuilder<T> {
                         writerCpu,
                         writerMemory,
                         writerManagedMemory,
+                        writerOffHeapMemory,
                         committerCpu,
                         committerMemory,
                         commitUser,
diff --git 
a/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/sink/cdc/FlinkCdcMultiTableSinkTest.java
 
b/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/sink/cdc/FlinkCdcMultiTableSinkTest.java
index cee9df8666..7f61b83812 100644
--- 
a/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/sink/cdc/FlinkCdcMultiTableSinkTest.java
+++ 
b/paimon-flink/paimon-flink-cdc/src/test/java/org/apache/paimon/flink/sink/cdc/FlinkCdcMultiTableSinkTest.java
@@ -36,6 +36,9 @@ import 
org.apache.flink.streaming.api.transformations.OneInputTransformation;
 import org.apache.flink.streaming.api.transformations.PartitionTransformation;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.io.TempDir;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.NullSource;
+import org.junit.jupiter.params.provider.ValueSource;
 
 import java.util.Collections;
 import java.util.UUID;
@@ -130,14 +133,19 @@ public class FlinkCdcMultiTableSinkTest {
                 .hasMessageContaining("database name");
     }
 
-    @Test
-    public void testDatabaseBuilderPropagatesWriterManagedMemory(
-            @TempDir java.nio.file.Path tempDir) {
+    @ParameterizedTest
+    @NullSource
+    @ValueSource(strings = {"1536 kb"})
+    public void testDatabaseBuilderPropagatesWriterResources(
+            String offHeapMemory, @TempDir java.nio.file.Path tempDir) {
         Options catalogOptions = new Options();
         catalogOptions.setString("warehouse", tempDir.toString());
         Options options = new Options();
         options.setString("sink.writer-memory", "512 mb");
         options.setString("sink.writer-managed-memory", "128 mb");
+        if (offHeapMemory != null) {
+            options.setString("sink.writer-offheap-memory", offHeapMemory);
+        }
         options.setString("sink.committer-memory", "64 mb");
         StreamExecutionEnvironment env = 
StreamExecutionEnvironment.getExecutionEnvironment();
         new FlinkCdcSyncDatabaseSinkBuilder<TestCdcEvent>()
@@ -157,10 +165,65 @@ public class FlinkCdcMultiTableSinkTest {
                 graph.getSlotSharingGroupResource("CDC MultiplexWriter").get();
         
assertThat(writerResources.getManagedMemory().getMebiBytes()).isEqualTo(128);
         
assertThat(writerResources.getTaskHeapMemory().getMebiBytes()).isEqualTo(512);
+        assertThat(writerResources.getTaskOffHeapMemory().getBytes())
+                .isEqualTo(offHeapMemory == null ? 0 : 1572864);
         ResourceProfile committerResources =
                 graph.getSlotSharingGroupResource("Multiplex Global 
Committer").get();
         assertThat(committerResources.getManagedMemory().getBytes()).isZero();
         
assertThat(committerResources.getTaskHeapMemory().getMebiBytes()).isEqualTo(64);
+        
assertThat(committerResources.getTaskOffHeapMemory().getBytes()).isZero();
+    }
+
+    @Test
+    public void testLegacyManagedMemoryConstructorPreservesResources() {
+        StreamExecutionEnvironment env = 
StreamExecutionEnvironment.getExecutionEnvironment();
+        new FlinkCdcMultiTableSink(
+                        () -> FlinkCatalogFactory.createPaimonCatalog(new 
Options()),
+                        "test_db",
+                        2.5,
+                        MemorySize.ofMebiBytes(512),
+                        MemorySize.ofMebiBytes(128),
+                        1.0,
+                        null,
+                        "test-user",
+                        false,
+                        null)
+                .sinkFrom(
+                        env.fromData(
+                                CdcMultiplexRecord.class, new 
CdcMultiplexRecord("", "", null)));
+        ResourceProfile resources =
+                env.getStreamGraph().getSlotSharingGroupResource("CDC 
MultiplexWriter").get();
+        assertThat(resources.getTaskOffHeapMemory().getBytes()).isZero();
+        assertThat(resources.getManagedMemory().getMebiBytes()).isEqualTo(128);
+        
assertThat(resources.getTaskHeapMemory().getMebiBytes()).isEqualTo(512);
+        
assertThat(resources.getCpuCores().getValue().doubleValue()).isEqualTo(2.5);
+    }
+
+    @Test
+    public void testOffHeapMemoryWithoutHeapIsRejected() {
+        StreamExecutionEnvironment env = 
StreamExecutionEnvironment.getExecutionEnvironment();
+        FlinkCdcMultiTableSink sink =
+                new FlinkCdcMultiTableSink(
+                        () -> FlinkCatalogFactory.createPaimonCatalog(new 
Options()),
+                        "test_db",
+                        1.0,
+                        null,
+                        null,
+                        MemorySize.ofMebiBytes(128),
+                        1.0,
+                        null,
+                        "test-user",
+                        false,
+                        null);
+        assertThatThrownBy(
+                        () ->
+                                sink.sinkFrom(
+                                        env.fromData(
+                                                CdcMultiplexRecord.class,
+                                                new CdcMultiplexRecord("", "", 
null))))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("sink.writer-offheap-memory")
+                .hasMessageContaining("sink.writer-memory");
     }
 
     @Test
diff --git 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/FlinkConnectorOptions.java
 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/FlinkConnectorOptions.java
index 0deded6d31..6d2fa392f7 100644
--- 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/FlinkConnectorOptions.java
+++ 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/FlinkConnectorOptions.java
@@ -375,6 +375,14 @@ public class FlinkConnectorOptions {
                                     + "weight 
sink.managed.writer-buffer-memory. The actual writer buffer "
                                     + "size depends on the slot's allocated 
resources and managed memory consumers.");
 
+    public static final ConfigOption<MemorySize> SINK_WRITER_OFFHEAP_MEMORY =
+            ConfigOptions.key("sink.writer-offheap-memory")
+                    .memoryType()
+                    .noDefaultValue()
+                    .withDescription(
+                            "Task off-heap memory resource budget for the 
writer slot sharing group. "
+                                    + "Must be positive and requires 
sink.writer-memory.");
+
     public static final ConfigOption<Double> SINK_COMMITTER_CPU =
             ConfigOptions.key("sink.committer-cpu")
                     .doubleType()
diff --git 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/FlinkSink.java
 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/FlinkSink.java
index a1fd0cd7aa..a3e8621ec1 100644
--- 
a/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/FlinkSink.java
+++ 
b/paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/sink/FlinkSink.java
@@ -67,6 +67,7 @@ import static 
org.apache.paimon.flink.FlinkConnectorOptions.SINK_USE_MANAGED_MEM
 import static org.apache.paimon.flink.FlinkConnectorOptions.SINK_WRITER_CPU;
 import static 
org.apache.paimon.flink.FlinkConnectorOptions.SINK_WRITER_MANAGED_MEMORY;
 import static org.apache.paimon.flink.FlinkConnectorOptions.SINK_WRITER_MEMORY;
+import static 
org.apache.paimon.flink.FlinkConnectorOptions.SINK_WRITER_OFFHEAP_MEMORY;
 import static org.apache.paimon.flink.FlinkConnectorOptions.generateCustomUid;
 import static 
org.apache.paimon.flink.utils.ManagedMemoryUtils.declareManagedMemory;
 import static 
org.apache.paimon.flink.utils.ParallelismUtils.forwardParallelism;
@@ -171,7 +172,8 @@ public abstract class FlinkSink<T> implements Serializable {
                 written,
                 options.get(SINK_WRITER_CPU),
                 options.get(SINK_WRITER_MEMORY),
-                options.get(SINK_WRITER_MANAGED_MEMORY));
+                options.get(SINK_WRITER_MANAGED_MEMORY),
+                options.get(SINK_WRITER_OFFHEAP_MEMORY));
 
         if (!table.primaryKeys().isEmpty() && options.get(PRECOMMIT_COMPACT)) {
             SingleOutputStreamOperator<Committable> beforeSort =
@@ -306,6 +308,15 @@ public abstract class FlinkSink<T> implements Serializable 
{
             double cpuCores,
             @Nullable MemorySize heapMemory,
             @Nullable MemorySize managedMemory) {
+        configureSlotSharingGroup(operator, cpuCores, heapMemory, 
managedMemory, null);
+    }
+
+    public static void configureSlotSharingGroup(
+            SingleOutputStreamOperator<?> operator,
+            double cpuCores,
+            @Nullable MemorySize heapMemory,
+            @Nullable MemorySize managedMemory,
+            @Nullable MemorySize offHeapMemory) {
         if (managedMemory != null) {
             checkArgument(
                     heapMemory != null,
@@ -317,6 +328,17 @@ public abstract class FlinkSink<T> implements Serializable 
{
                     "'%s' must be positive.",
                     SINK_WRITER_MANAGED_MEMORY.key());
         }
+        if (offHeapMemory != null) {
+            checkArgument(
+                    heapMemory != null,
+                    "'%s' requires '%s' to configure the writer slot sharing 
group.",
+                    SINK_WRITER_OFFHEAP_MEMORY.key(),
+                    SINK_WRITER_MEMORY.key());
+            checkArgument(
+                    offHeapMemory.getBytes() > 0,
+                    "'%s' must be positive.",
+                    SINK_WRITER_OFFHEAP_MEMORY.key());
+        }
         if (heapMemory == null) {
             return;
         }
@@ -331,6 +353,11 @@ public abstract class FlinkSink<T> implements Serializable 
{
             builder.setManagedMemory(
                     new 
org.apache.flink.configuration.MemorySize(managedMemory.getBytes()));
         }
+        if (offHeapMemory != null) {
+            builder.setTaskOffHeapMemory(
+                    new 
org.apache.flink.configuration.MemorySize(offHeapMemory.getBytes()));
+        }
+
         operator.slotSharingGroup(builder.build());
     }
 
diff --git 
a/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/sink/WriterOffHeapMemoryTest.java
 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/sink/WriterOffHeapMemoryTest.java
new file mode 100644
index 0000000000..5c7da97f72
--- /dev/null
+++ 
b/paimon-flink/paimon-flink-common/src/test/java/org/apache/paimon/flink/sink/WriterOffHeapMemoryTest.java
@@ -0,0 +1,200 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.paimon.flink.sink;
+
+import org.apache.paimon.data.GenericRow;
+import org.apache.paimon.data.InternalRow;
+import org.apache.paimon.table.FileStoreTable;
+
+import org.apache.flink.configuration.MemorySize;
+import org.apache.flink.core.memory.ManagedMemoryUseCase;
+import org.apache.flink.runtime.clusterframework.types.ResourceProfile;
+import org.apache.flink.runtime.jobgraph.JobVertex;
+import org.apache.flink.streaming.api.datastream.DataStream;
+import org.apache.flink.streaming.api.datastream.DataStreamSource;
+import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
+import org.apache.flink.streaming.api.graph.StreamGraph;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.CsvSource;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import java.util.Collections;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+
+/** Tests for the writer slot sharing group's task off-heap memory budget. */
+class WriterOffHeapMemoryTest extends CommitterTestBase {
+
+    @ParameterizedTest
+    @CsvSource({"128 mb, 134217728", "1 gb, 1073741824", "1536 kb, 1572864", 
"512 kb, 524288"})
+    void testOffHeapResourcesPreserveBytes(String offHeapMemory, long 
expectedBytes)
+            throws Exception {
+        FileStoreTable table =
+                createFileStoreTable(
+                        options -> {
+                            options.setString("sink.writer-memory", "512 mb");
+                            options.setString("sink.writer-offheap-memory", 
offHeapMemory);
+                        });
+        StreamExecutionEnvironment env = 
StreamExecutionEnvironment.getExecutionEnvironment();
+        DataStream<Committable> written = write(env, table);
+        StreamGraph graph = env.getStreamGraph();
+        String writerGroup = written.getTransformation().getName();
+        ResourceProfile resources = 
graph.getSlotSharingGroupResource(writerGroup).get();
+        
assertThat(resources.getTaskOffHeapMemory().getBytes()).isEqualTo(expectedBytes);
+        
assertThat(resources.getTaskHeapMemory()).isEqualTo(MemorySize.ofMebiBytes(512));
+        assertThat(resources.getManagedMemory()).isEqualTo(MemorySize.ZERO);
+        
assertThat(resources.getCpuCores().getValue().doubleValue()).isEqualTo(1.0);
+        graph.getStreamNodes().stream()
+                .filter(n -> !n.getOperatorName().startsWith("Source:"))
+                .forEach(n -> 
assertThat(n.getSlotSharingGroup()).isEqualTo(writerGroup));
+        
assertThat(writerVertex(graph).getSlotSharingGroup().getResourceProfile())
+                .isEqualTo(resources);
+    }
+
+    @ParameterizedTest
+    @ValueSource(booleans = {false, true})
+    void testOffHeapAndManagedResourcesAreIndependent(boolean 
managedAllocator) throws Exception {
+        FileStoreTable table =
+                createFileStoreTable(
+                        options -> {
+                            options.setString("sink.writer-cpu", "2.5");
+                            options.setString("sink.writer-memory", "512 mb");
+                            options.setString("sink.writer-offheap-memory", 
"128 mb");
+                            options.setString("sink.writer-managed-memory", 
"64 mb");
+                            options.setString(
+                                    "sink.use-managed-memory-allocator",
+                                    String.valueOf(managedAllocator));
+                            
options.setString("sink.managed.writer-buffer-memory", "256 mb");
+                            options.setString("sink.committer-memory", "32 
mb");
+                            options.setString("sink.committer-cpu", "0.25");
+                        });
+        StreamExecutionEnvironment env = 
StreamExecutionEnvironment.getExecutionEnvironment();
+        DataStream<Committable> written = write(env, table);
+        StreamGraph graph = env.getStreamGraph();
+        ResourceProfile writerResources =
+                
graph.getSlotSharingGroupResource(written.getTransformation().getName()).get();
+        
assertThat(writerResources.getTaskOffHeapMemory()).isEqualTo(MemorySize.ofMebiBytes(128));
+        
assertThat(writerResources.getManagedMemory()).isEqualTo(MemorySize.ofMebiBytes(64));
+        
assertThat(writerResources.getTaskHeapMemory()).isEqualTo(MemorySize.ofMebiBytes(512));
+        
assertThat(writerResources.getCpuCores().getValue().doubleValue()).isEqualTo(2.5);
+        
assertThat(written.getTransformation().getManagedMemoryOperatorScopeUseCaseWeights())
+                .isEqualTo(
+                        managedAllocator
+                                ? 
Collections.singletonMap(ManagedMemoryUseCase.OPERATOR, 256)
+                                : Collections.emptyMap());
+        String committerGroup =
+                graph.getStreamNodes().stream()
+                        .filter(n -> n.getOperatorName().startsWith("Global 
Committer"))
+                        .findFirst()
+                        .get()
+                        .getSlotSharingGroup();
+        ResourceProfile committerResources =
+                graph.getSlotSharingGroupResource(committerGroup).get();
+        
assertThat(committerResources.getTaskOffHeapMemory()).isEqualTo(MemorySize.ZERO);
+        
assertThat(committerResources.getManagedMemory()).isEqualTo(MemorySize.ZERO);
+        
assertThat(committerResources.getTaskHeapMemory()).isEqualTo(MemorySize.ofMebiBytes(32));
+        
assertThat(committerResources.getCpuCores().getValue().doubleValue()).isEqualTo(0.25);
+    }
+
+    @ParameterizedTest
+    @ValueSource(booleans = {false, true})
+    void testAbsentOptionPreservesExistingResources(boolean explicitHeap) 
throws Exception {
+        FileStoreTable table =
+                createFileStoreTable(
+                        options -> {
+                            if (explicitHeap) {
+                                options.setString("sink.writer-memory", "512 
mb");
+                            }
+                        });
+        StreamExecutionEnvironment env = 
StreamExecutionEnvironment.getExecutionEnvironment();
+        write(env, table);
+        ResourceProfile resources =
+                
writerVertex(env.getStreamGraph()).getSlotSharingGroup().getResourceProfile();
+        if (explicitHeap) {
+            
assertThat(resources.getTaskOffHeapMemory()).isEqualTo(MemorySize.ZERO);
+        } else {
+            assertThat(resources).isEqualTo(ResourceProfile.UNKNOWN);
+        }
+    }
+
+    @Test
+    void testLegacyResourceOverloadPreservesManagedMemory() {
+        StreamExecutionEnvironment env = 
StreamExecutionEnvironment.getExecutionEnvironment();
+        DataStreamSource<Integer> operator = env.fromData(1);
+        FlinkSink.configureSlotSharingGroup(
+                operator,
+                2.5,
+                org.apache.paimon.options.MemorySize.ofMebiBytes(512),
+                org.apache.paimon.options.MemorySize.ofMebiBytes(64));
+        operator.print();
+        ResourceProfile resources =
+                
env.getStreamGraph().getSlotSharingGroupResource(operator.getName()).get();
+        
assertThat(resources.getTaskOffHeapMemory()).isEqualTo(MemorySize.ZERO);
+        
assertThat(resources.getManagedMemory()).isEqualTo(MemorySize.ofMebiBytes(64));
+        
assertThat(resources.getTaskHeapMemory()).isEqualTo(MemorySize.ofMebiBytes(512));
+        
assertThat(resources.getCpuCores().getValue().doubleValue()).isEqualTo(2.5);
+    }
+
+    @Test
+    void testOffHeapMemoryRequiresHeap() throws Exception {
+        FileStoreTable table =
+                createFileStoreTable(
+                        options -> 
options.setString("sink.writer-offheap-memory", "128 mb"));
+        assertThatThrownBy(() -> 
write(StreamExecutionEnvironment.getExecutionEnvironment(), table))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("sink.writer-offheap-memory")
+                .hasMessageContaining("sink.writer-memory");
+    }
+
+    @ParameterizedTest
+    @ValueSource(strings = {"0 b", "-1 mb", "invalid"})
+    void testInvalidOffHeapMemoryIsRejected(String offHeapMemory) throws 
Exception {
+        FileStoreTable table =
+                createFileStoreTable(
+                        options -> {
+                            options.setString("sink.writer-memory", "512 mb");
+                            options.setString("sink.writer-offheap-memory", 
offHeapMemory);
+                        });
+        assertThatThrownBy(() -> 
write(StreamExecutionEnvironment.getExecutionEnvironment(), table))
+                .isInstanceOf(IllegalArgumentException.class)
+                .hasMessageContaining("sink.writer-offheap-memory");
+    }
+
+    private DataStream<Committable> write(StreamExecutionEnvironment env, 
FileStoreTable table) {
+        DataStream<InternalRow> source =
+                env.fromData(InternalRow.class, GenericRow.of(1, 1L))
+                        .name("upstream")
+                        .slotSharingGroup("main");
+        FlinkSink<InternalRow> sink = new FixedBucketSink(table, null);
+        DataStream<Committable> written = sink.doWrite(source, "test-user", 2);
+        sink.doCommit(written, "test-user");
+        return written;
+    }
+
+    private JobVertex writerVertex(StreamGraph graph) {
+        for (JobVertex vertex : graph.getJobGraph().getVertices()) {
+            if (vertex.getName().startsWith("Writer")) {
+                return vertex;
+            }
+        }
+        throw new AssertionError("Writer vertex not found");
+    }
+}

Reply via email to