Skip to content

fix: retain latest flags and segments for duplicate identifiers - #33

Merged
deleteLater merged 3 commits into
featbit:masterfrom
dmytrodanilenkov:fix/duplicate-flag-key
Nov 13, 2026
Merged

deleteLater merged 3 commits into
featbit:masterfrom
dmytrodanilenkov:fix/duplicate-flag-key

Conversation

@dmytrodanilenkov

@dmytrodanilenkov dmytrodanilenkov commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #32

Duplicate feature flag keys in a data-sync payload previously caused DataModel.Data.toStorageType() to throw, failing the sync and preventing new clients from initializing. Duplicate segment IDs could cause the same failure.

Collect feature flags by key and segments by ID, retaining the entry with the greatest timestamp for each identifier, then copy the results to immutable maps.

Validation: mvn -q -Dtest=DataModelTest test — both regression tests passed. Each directly constructs two records with the same identifier, places the older record last, and verifies that the newest record is retained.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no actionable issues were found.

Summary

This PR lets data sync accept duplicate feature keys and segment IDs instead of failing.

  • Data sync keeps the newest item when keys repeat.

Reviews (3) · Last reviewed commit: "fix: retain latest segment timestamp for..." · Reviewed by Greptile

Summary by CodeRabbit

  • Bug Fixes
    • Feature flags with duplicate keys and segments with duplicate IDs now retain the item with the latest timestamp. When timestamps match, the existing item is retained. Archived items are converted before selection.

DataModel.Data.toStorageType() built an ImmutableMap keyed by flag key,
so a payload with the same key twice (e.g. featbit-agent still holding
an archived copy of a flag that was deleted and re-created) threw
"Multiple entries with same key" and failed the whole sync, leaving new
clients in a reconnect loop.

Collect items into a LinkedHashMap so the last entry wins, as the Go,
Python, Rust, Ruby and Node SDKs already do.
@coderabbitai

coderabbitai Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

DataModel.toStorageType() stores feature flags by key and segments by ID. For duplicates, it retains the item with the later timestamp. Tests cover duplicate feature flag keys and segment IDs.

Changes: Storage conversion

Layer / File(s) Summary
Duplicate item resolution
src/main/java/co/featbit/server/DataModel.java, src/test/java/co/featbit/server/DataModelTest.java
Storage conversion resolves duplicate feature flag keys and segment IDs by timestamp. Tests verify that the newer item is retained.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: dsun0720

Merge Risk: Minimal · up to 7e6db

Data sync no longer fails when a payload contains duplicate feature-flag keys or segment IDs, and the newer item is kept. No actionable merge risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "fix: retain latest flags and segments for duplicate identifiers" matches the main change. DataModel.Data.toStorageType() now keeps the item with the greater timestamp when feature flags or …
Linked Issues check Passed [#32] requires that a payload with an archived and a live flag sharing one key no longer fails the full sync, and that the re-created flag is evaluated. At head 7e6dbe6, DataModel.Data.toStorageType()…
Out of Scope Changes check Passed The DataModel.java diff changes only toStorageType(), plus the LinkedHashMap import. The added segment merge uses the same duplicate-key path and the same failure mode as flags, so it is a closely rel…
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title "fix: retain latest flags and segments for duplicate identifiers" matches the main change. DataModel.Data.toStorageType() now keeps the item with the greater timestamp when feature flags or segments share a key or ID. The added DataModelTest cases check this behavior. The title is specific and describes the fix, not the implementation details.

Full details: Linked Issues check

Explanation

[#32] requires that a payload with an archived and a live flag sharing one key no longer fails the full sync, and that the re-created flag is evaluated. At head 7e6dbe6, DataModel.Data.toStorageType() replaces ImmutableMap.Builder with a LinkedHashMap for flags, keyed by flag.getKey(), using merge(). The merge keeps the item with the greater timestamp. The re-created flag is newer than the archived copy, so it is retained. DataModelTest.toStorageTypeKeepsFlagWithLatestTimestampForDuplicateKey asserts this. The issue's Proposed fix describes last-entry-wins. The implementation uses timestamp-based selection instead. On equal timestamps, the lambda returns the incoming item, so the later entry wins. This differs from the proposal but meets the stated Expected outcome. The current change also applies the same rule to segments, keyed by ID. This goes beyond the issue, but it does not block the issue's requirement.

Full details: Out of Scope Changes check

Explanation

The DataModel.java diff changes only toStorageType(), plus the LinkedHashMap import. The added segment merge uses the same duplicate-key path and the same failure mode as flags, so it is a closely related hardening within the same method. It is not an unrelated change. DataModelTest.java contains two unit tests for flag and segment duplicate handling. These tests support the changed behavior. The raw summary states that equal timestamps keep the existing item, but the code returns the incoming item on ties. The code is the authoritative behavior. No unrelated change was found.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 1deecaf7-5206-4baa-b6b1-cc0142f71eff
Commits

Reviewing files that changed from the base of the PR and between cbf5b3f and 7e6dbe6.

Files selected for processing: 2

  • src/main/java/co/featbit/server/DataModel.java
  • src/test/java/co/featbit/server/DataModelTest.java

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@deleteLater deleteLater changed the title fix: tolerate duplicate flag keys in data sync payload fix: retain latest flags and segments for duplicate identifiers Nov 13, 2026
@deleteLater
deleteLater requested a balanced review from Copilot November 13, 2026 10:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The tests cover only live records, leaving archived/live collision handling and preservation of the retained item's key, timestamp, and archived state unverified.

1 open issue
What changed in this PR

This PR prevents duplicate identifiers in data-sync payloads from failing SDK initialization, addressing the failure reported in #32.

Changes:

  • Selects the feature flag or segment with the greatest timestamp for each duplicate identifier.
  • Preserves immutable storage maps after deduplication.
  • Adds regression tests confirming that newer records survive when older duplicates appear last.
File Description
src/​test/​java/​co/​featbit/​server/​DataModelTest.java Adds duplicate-key and duplicate-ID regression tests.
src/​main/​java/​co/​featbit/​server/​DataModel.java Merges duplicate flags and segments by timestamp before creating immutable maps.

Review effort: Balanced

@deleteLater

deleteLater commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Thanks for the fix❤️. I’ve adjusted the PR to keep the record with the latest timestamp for duplicate flag keys and segment IDs

@deleteLater deleteLater 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.

LGTM

@deleteLater
deleteLater merged commit 76a71f0 into featbit:master Nov 13, 2026
3 checks passed
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.

Full sync fails when payload contains an archived and live flag with same key

3 participants