Skip to content

branch-4.2 [fix](constraint) Fix NPE in PrimaryKeyConstraint when loading old metadata - #68206

Open
englefly wants to merge 1 commit into
apache:branch-4.2from
englefly:pick-61342-4.2
Open

englefly wants to merge 1 commit into
apache:branch-4.2from
englefly:pick-61342-4.2

Conversation

@englefly

Copy link
Copy Markdown
Contributor

Cherry-pick of #61342 to branch-4.2

fix Fix NPE in PrimaryKeyConstraint when loading old metadata

On branch-4.2 the class only has the foreignTables collection (the newer
foreignTableNameStrs/foreignTableInfos fields come from a later refactor that is
not part of this branch), so only that field is guarded. A unit test covering
deserialization of metadata without the ft field was added.

…ding old metadata (apache#61342)

### What problem does this PR solve?

When FE starts with -r (metadata_failure_recovery) or without a
checkpoint image, it replays all journal logs from the beginning. Old
journal entries may contain PrimaryKeyConstraint objects that were
serialized before the `foreignTables` field was added to the class.

Gson uses Unsafe to instantiate objects during deserialization,
bypassing the constructor and its field initializers. If a field is
absent from the JSON, Gson leaves it as null rather than using the
declared initializer (e.g. `= new HashSet<>()`). Calling .isEmpty() or
any method on a null collection then throws a NullPointerException.

Fix: remove `final` from the `foreignTables` collection field (so it can
be reassigned in gsonPostProcess) and add a null-guard at the top of
gsonPostProcess() to initialize it to an empty collection when absent.

Note: on branch-4.2 the class only has `foreignTables`; the newer
`foreignTableNameStrs` / `foreignTableInfos` fields come from a later
refactor that is not part of this branch, so only that field is guarded.

Impact on -r mode: when FE is started with --metadata_failure_recovery
it replays every journal from journal #1, encountering the oldest
serialized PrimaryKeyConstraint objects. Without this fix, FE always
crashes during -r recovery if any PrimaryKeyConstraint was persisted
before the field existed.

### Release note

None
@englefly
englefly requested a review from yiguolei as a code owner September 18, 2026 15:35
@hello-stephen

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR.

Please clearly describe your PR:

  1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
  2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
  3. What features were added. Why was this function added?
  4. Which code was refactored and why was this part of the code refactored?
  5. Which functions were optimized and what is the difference before and after the optimization?

@englefly

Copy link
Copy Markdown
Contributor Author

run buildall

@hello-stephen

Copy link
Copy Markdown
Contributor

FE UT Coverage Report

Increment line coverage 100.00% (3/3) 🎉
Increment coverage report
Complete coverage report

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants