Skip to content

fix(rest): release fetched FileScanTask state to prevent unbounded memory growth - #17691

Open
waterWang wants to merge 3 commits into
apache:mainfrom
waterWang:fix/rest-release-fetched-filescantask-state
Open

fix(rest): release fetched FileScanTask state to prevent unbounded memory growth#17691
waterWang wants to merge 3 commits into
apache:mainfrom
waterWang:fix/rest-release-fetched-filescantask-state

Conversation

@waterWang

Copy link
Copy Markdown

Problem

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 state is released only when cancelPlanning is called, but according to the REST Catalog OpenAPI specification, cancellation is not required after scan tasks have been fetched for every plan task.

Fix

  1. InMemoryPlanningState.releasePlanTask(planTaskKey) — removes the fetched plan task's file scan tasks and next-task link from the maps. Called after every successful fetch in fetchScanTasks.

  2. InMemoryPlanningState.releaseAsyncPlanForTask(planTaskKey) — when the last plan task in a chain is fetched (nextPlanTasks is empty), also removes the async planning state for the owning plan.

This mirrors the design intent of the REST Catalog API: successful fetch lifecycles must eventually release fetched task state without requiring an explicit cancellation request.

Testing

Unit tests cover:

  • releasePlanTask removes both file scan tasks and next-task links
  • releaseAsyncPlanForTask removes async planning state
  • Malformed keys (single hyphen, no hyphen) are safely ignored
  • Releasing a non-existent key is a no-op (idempotent)

Closes #17427

@github-actions github-actions Bot added the core label Aug 17, 2026

@singhpk234 singhpk234 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you using this in production if yes in what catalog ?

Comment on lines +132 to +133
* unbounded memory growth — the {@code FileScanTask} list is the dominant memory consumer and
* need not be retained once the client has received it (#17427).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to add this in java doc ?

Comment on lines +144 to +145
* the last plan task in a chain is fetched, so completed plans don't accumulate indefinitely
* (#17427).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to add this in the java doc ?

Comment on lines +153 to +161
int lastHyphen = planTaskKey.lastIndexOf('-');
if (lastHyphen < 0) {
return;
}
int secondLastHyphen = planTaskKey.lastIndexOf('-', lastHyphen - 1);
if (secondLastHyphen < 0) {
return;
}
String planId = planTaskKey.substring(0, secondLastHyphen);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not have a regex ? and extract using regex ?

i also think we should add get planId from the planTaskKey in a seperate util method

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public class TestInMemoryPlanningState {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to add this in existing test for this ?

List<String> nextPlanTasks = IN_MEMORY_PLANNING_STATE.nextPlanTask(planTask);

// Release the fetched plan task's state to prevent unbounded memory growth. The FileScanTask
// list is the dominant memory consumer and need not be retained once served (#17427). When

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this needs to be in this comments ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants