iceberg icon indicating copy to clipboard operation
iceberg copied to clipboard

[Bug] REST server-side scan planning retains fetched FileScanTasks indefinitely

Open chenwyi2 opened this issue 4 months ago • 1 comments

Apache Iceberg version

1.10.1

Query engine

Starrocks

Please describe the bug 🐞

Query engine

REST Catalog server-side scan planning.

Please describe the bug

The reference implementation of REST server-side scan planning keeps all planned FileScanTask objects in the singleton InMemoryPlanningState, even after clients have successfully fetched every plan task.

As a result, repeated successful scans cause retained heap usage to grow approximately linearly with the number of planned files.

The retained state is stored in the following maps:

Map<String, List> planTaskToFileScanTasks; Map<String, String> planTaskToNext; Map<String, PlanStatus> asyncPlanningStates;

The normal successful fetch path only reads these maps:

`List fileScanTasks = IN_MEMORY_PLANNING_STATE.fileScanTasksForPlanTask(planTask);

return FetchScanTasksResponse.builder() .withFileScanTasks(fileScanTasks) .withPlanTasks(IN_MEMORY_PLANNING_STATE.nextPlanTask(planTask)) .withSpecsById(table.specs()) .build();`

Neither fileScanTasksForPlanTask nor nextPlanTask removes the fetched entries. The initial task returned by synchronous planTableScan or asynchronous fetchPlanningResult is also retained. Currently, the state is released only when cancelPlanning calls InMemoryPlanningState.cancelPlan. However, according to the REST Catalog OpenAPI specification, cancellation is not required after scan tasks have been fetched for every plan task. This implies that a successful fetch lifecycle must eventually release fetched task state without requiring an explicit cancellation request. Specification: https://github.com/apache/iceberg/blob/main/open-api/rest-catalog-open-api.yaml Relevant implementation: https://github.com/apache/iceberg/blob/main/core/src/main/java/org/apache/iceberg/rest/CatalogHandlers.java https://github.com/apache/iceberg/blob/main/core/src/main/java/org/apache/iceberg/rest/InMemoryPlanningState.java Impact In a long-running REST catalog deployment based on this implementation, we observed:

  • Heap usage continuously increasing after successful scans

  • Tens of millions of retained FileScanTask objects

  • More than 20 GB of heap retained by planning state

  • Full GC unable to reclaim the objects

  • No active scans corresponding to most retained tasks

  • No cancellation requests because all plan tasks had already been fetched successfully

In one observation window, approximately 25 million FileScanTask objects were retained after about 22 hours of operation. This may eventually cause excessive GC pressure or an OutOfMemoryError.

Willingness to contribute

  • [ ] I can contribute a fix for this bug independently
  • [ ] I would be willing to contribute a fix for this bug with guidance from the Iceberg community
  • [ ] I cannot contribute a fix for this bug at this time

chenwyi2 avatar Jul 30 '26 02:07 chenwyi2

Confirmed against current main — this is a real leak on the spec-conformant happy path, not just a cache without eviction.

InMemoryPlanningState.fileScanTasksForPlanTask (line 112) and nextPlanTask (line 121) are plain get calls. The only removals are in cancelPlan (lines 165-166) and the full clear(), and the class is a process-lifetime singleton, so every successful scan adds state that is never reclaimed.

What makes it a defect rather than a design choice is the spec. rest-catalog-open-api.yaml states that cancellation "is not necessary after fetchScanTasks has been used to fetch scan tasks for each plan task". A client that behaves exactly as specified therefore never calls cancelPlanning, so the only code path that frees state is the one such a client never takes.

There is already a complete fix in #17429 by @Solaris-star, which adds releasePlanTask and releaseAsyncPlanForTask and calls them from fetchScanTasks, with tests. It was closed by its own author on 2026-08-12 after two weeks with no review — not rejected on merit. I have commented there with the two points a reviewer would want covered (that fetch becomes non-idempotent, which is spec-defensible but should be stated; and that deriving the planId by counting hyphens in the key is fragile).

I have not opened a competing PR. The efficient path here is for a committer to look at #17429.

vbhanuchander-lang avatar Aug 14 '26 00:08 vbhanuchander-lang