fluss icon indicating copy to clipboard operation
fluss copied to clipboard

Fix OutOfBoundException in writeChangeType when the number of records is large

Open gyang94 opened this issue 8 months ago • 5 comments

Search before asking

  • [x] I searched in the issues and found nothing similar.

Fluss version

0.9.0 (latest release)

Please describe the bug 🐞

In function ChangeTypeVectorWriter.writeChangeType, there is chance to see OutOfBoundException when a record batch is large.

Suppressed: java.io.IOException: One or more Fluss Writer send requests have encountered exception at org.apache.fluss.flink.sink.writer.FlinkSinkWriter.checkAsyncException(FlinkSinkWriter.java:239) at org.apache.fluss.flink.sink.writer.FlinkSinkWriter.close(FlinkSinkWriter.java:198) at org.apache.flink.util.IOUtils.closeAll(IOUtils.java:255) at org.apache.flink.util.IOUtils.closeAll(IOUtils.java:234) at org.apache.flink.util.IOUtils.closeAll(IOUtils.java:223) at org.apache.flink.streaming.runtime.operators.sink.SinkWriterOperator.close(SinkWriterOperator.java:275) at org.apache.flink.streaming.runtime.tasks.StreamOperatorWrapper.close(StreamOperatorWrapper.java:163) at org.apache.flink.streaming.runtime.tasks.RegularOperatorChain.closeAllOperators(RegularOperatorChain.java:125) at org.apache.flink.streaming.runtime.tasks.StreamTask.closeAllOperators(StreamTask.java:1410) at org.apache.flink.util.IOUtils.closeAll(IOUtils.java:255) at org.apache.flink.core.fs.AutoCloseableRegistry.doClose(AutoCloseableRegistry.java:72) at org.apache.flink.util.AbstractAutoCloseableRegistry.close(AbstractAutoCloseableRegistry.java:127) at org.apache.flink.streaming.runtime.tasks.StreamTask.cleanUp(StreamTask.java:1314) at org.apache.flink.runtime.taskmanager.Task.lambda$restoreAndInvoke$0(Task.java:958) at org.apache.flink.runtime.taskmanager.Task.runWithSystemExitMonitoring(Task.java:976) at org.apache.flink.runtime.taskmanager.Task.restoreAndInvoke(Task.java:958) ... 3 more Caused by: java.util.concurrent.CompletionException: org.apache.fluss.exception.UnknownServerException: java.lang.IndexOutOfBoundsException: capacity: 131072, index: 131072, put length: 1 at org.apache.fluss.memory.MemorySegment.put(MemorySegment.java:325) at org.apache.fluss.record.ChangeTypeVectorWriter.writeChangeType(ChangeTypeVectorWriter.java:44) at org.apache.fluss.record.MemoryLogRecordsArrowBuilder.append(MemoryLogRecordsArrowBuilder.java:185) at org.apache.fluss.server.kv.wal.ArrowWalBuilder.append(ArrowWalBuilder.java:48) at org.apache.fluss.server.kv.KvTablet.applyUpdate(KvTablet.java:624) at org.apache.fluss.server.kv.KvTablet.processUpsert(KvTablet.java:582) at org.apache.fluss.server.kv.KvTablet.processKvRecords(KvTablet.java:493) at org.apache.fluss.server.kv.KvTablet.lambda$putAsLeader$0(KvTablet.java:406) at org.apache.fluss.utils.concurrent.LockUtils.inLock(LockUtils.java:42) at org.apache.fluss.utils.concurrent.LockUtils.inWriteLock(LockUtils.java:65) at org.apache.fluss.server.kv.KvTablet.putAsLeader(KvTablet.java:367) at org.apache.fluss.server.replica.Replica.lambda$putRecordsToLeader$9(Replica.java:1012) at org.apache.fluss.utils.concurrent.LockUtils.inLock(LockUtils.java:42) at org.apache.fluss.utils.concurrent.LockUtils.inReadLock(LockUtils.java:55) at org.apache.fluss.server.replica.Replica.putRecordsToLeader(Replica.java:996) at org.apache.fluss.server.replica.ReplicaManager.putToLocalKv(ReplicaManager.java:1282) at org.apache.fluss.server.replica.ReplicaManager.putRecordsToKv(ReplicaManager.java:641) at org.apache.fluss.server.tablet.TabletService.putKv(TabletService.java:238) at jdk.internal.reflect.GeneratedMethodAccessor10.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:568) at org.apache.fluss.rpc.netty.server.FlussRequestHandler.processRequest(FlussRequestHandler.java:63)

Solution

ChangeTypeVectorWriter only have one memory segment now. To solve this problem, we need to make the ChangeTypeVectorWriter support multiple memory pages.

And another minor problem is that the boundary check is not good.

if (recordsCount > capacity) { throw new IllegalStateException("The change type vector is full."); } segment.put(startPosition + recordsCount, changeType.toByteValue());

we should check records >= capacity and expect to see this IllegalStateException, rather than writing to memory and see a runtime OutOfBoundException

Are you willing to submit a PR?

  • [ ] I'm willing to submit a PR!

gyang94 avatar Mar 31 '26 12:03 gyang94

Hi, I would like to work on this issue

hemanthsavasere avatar Apr 04 '26 03:04 hemanthsavasere

@hemanthsavasere Thanks. Please go ahead. But I don't have permission to assign tickets. cc: @wuchong

gyang94 avatar Apr 04 '26 11:04 gyang94

We hit this on Fluss 1.0.0 with partial deletes on a primary-key table (Flink FlussSink with partial update columns, about 1M rows, sink parallelism 1). After about 66k rows the server rejected a write batch:

org.apache.fluss.exception.UnknownServerException: java.lang.IndexOutOfBoundsException: capacity: 131072, index: 131072, put length: 1

What happens

  • The tablet server turns one client write batch into one changelog batch (Replica → KvTablet#putAsLeader → KvWriteProcessor#putAsLeader). For the ARROW log format, MemoryLogRecordsArrowBuilder creates the change type vector (one byte per changelog row) on the first segment only, whose size is server.buffer.page-size (128kb by default).
  • ChangeTypeVectorWriter#writeChangeType checks recordsCount > capacity, so the overflow shows up as an IndexOutOfBoundsException from MemorySegment#put instead of the intended "The change type vector is full." error.
  • With the default FULL changelog image, a partial delete (or partial update) of an existing row that keeps other columns produces two changelog rows (-U/+U). Delete records carry only the key, so a default 2mb client batch can hold several times the ~65k existing-row deletes that fit into the first segment. Upserts carry the whole row and rarely get that many records into one batch, which is probably why this mostly shows up with deletes.

Reproduction (fluss-client, added to FlussTableITCase). It fails on main (8a92b4f) with the exception above:

    @Test
    void testPartialDeleteManyRowsInOneWriteBatch() throws Exception {
        Schema schema =
                Schema.newBuilder()
                        .column("a", DataTypes.INT())
                        .column("b", DataTypes.STRING())
                        .column("c", DataTypes.STRING())
                        .primaryKey("a")
                        .build();
        TableDescriptor tableDescriptor =
                TableDescriptor.builder().schema(schema).distributedBy(1, "a").build();
        createTable(DATA1_TABLE_PATH_PK, tableDescriptor, true);

        // A partial delete that keeps other columns turns each existing row into -U and +U, and
        // the server writes one client write batch as one changelog batch. Key-only delete
        // records are small, so this 2mb batch carries 70k deletes (140k changelog rows).
        int rowCount = 70_000;
        Configuration conf = new Configuration(clientConf);
        conf.set(ConfigOptions.CLIENT_WRITER_BUFFER_MEMORY_SIZE, MemorySize.parse("64mb"));
        conf.set(ConfigOptions.CLIENT_WRITER_BATCH_SIZE, MemorySize.parse("2mb"));
        conf.set(ConfigOptions.CLIENT_WRITER_DYNAMIC_BATCH_SIZE_ENABLED, false);
        conf.set(ConfigOptions.CLIENT_WRITER_BATCH_TIMEOUT, Duration.ofMinutes(1));
        try (Connection connection = ConnectionFactory.createConnection(conf);
                Table table = connection.getTable(DATA1_TABLE_PATH_PK)) {
            UpsertWriter upsertWriter = table.newUpsert().createWriter();
            for (int i = 0; i < rowCount; i++) {
                upsertWriter.upsert(row(i, "b" + i, "c" + i));
            }
            upsertWriter.flush();

            UpsertWriter partialWriter = table.newUpsert().partialUpdate("a", "c").createWriter();
            List<CompletableFuture<?>> deletes = new ArrayList<>();
            for (int i = 0; i < rowCount; i++) {
                deletes.add(partialWriter.delete(row(i, null, null)));
            }
            partialWriter.flush();
            CompletableFuture.allOf(deletes.toArray(new CompletableFuture[0])).get();

            Lookuper lookuper = table.newLookup().createLookuper();
            assertThat(lookupRow(lookuper, row(0)))
                    .isEqualTo(compactedRow(schema.getRowType(), new Object[] {0, "b0", null}));
            assertThat(lookupRow(lookuper, row(rowCount - 1)))
                    .isEqualTo(
                            compactedRow(
                                    schema.getRowType(),
                                    new Object[] {rowCount - 1, "b" + (rowCount - 1), null}));
        }
    }

#3004

#3004 still applies cleanly on main (8a92b4f). With it applied, the test above passes, and so does MemoryLogRecordsArrowBuilderTest including the new cases from #3004. It would be great to get #3004 reviewed.

Until then, a smaller client.writer.batch-size avoids the problem for writers that send many small partial deletes. We use 256kb, which keeps a batch under ~44k deletes even with 1-byte keys.

ymkim1006 avatar Oct 27 '26 10:10 ymkim1006

Hi, @ymkim1006. Thanks for sharing this and letting us know you’ve run into the issue as well. It’s helpful to hear that it’s affecting your use case, and we can look into moving the fix forward. And sorry about missing the fix PR from @hemanthsavasere

Btw, while we’re here, we’d love to learn more about how you’re using Fluss, if you’re open to sharing. What does your use case look like, and what benefits have you seen so far? Any context you can share would be greatly appreciated—it helps us understand how the community is using Fluss and where we can improve.

gyang94 avatar Oct 29 '26 08:10 gyang94

I have also hit this, would be great to merge #3004 for the fix. Perhaps worth considering adding a boundary test at the real default page size.

mcgrj avatar Nov 21 '26 14:11 mcgrj