Skip to content

fix: Skip sample data init on restart if data already exists (#479) - #498

Closed
Ritvik-Jayaswal wants to merge 4 commits into
documentdb:mainfrom
Ritvik-Jayaswal:fix/479-duplicate-init-data
Closed

Ritvik-Jayaswal wants to merge 4 commits into
documentdb:mainfrom
Ritvik-Jayaswal:fix/479-duplicate-init-data

Conversation

@Ritvik-Jayaswal

@Ritvik-Jayaswal Ritvik-Jayaswal commented Mar 11, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #479

Problem

When the DocumentDB-local emulator container is restarted with a persisted --data-path, the entrypoint always re-runs sample data initialization. Since the sample JS files use insertMany with hardcoded _id values, this fails with a DuplicateID error, crashing the container on restart.

Fix

  • Added a check_database_has_data helper that uses mongosh to check if a database already contains data.
  • Before loading built-in sample data, check if sampledb already has data — if so, skip initialization with a log message.
  • For custom init data (--init-data-path), changed the failure from a hard exit 1 to a warning + continue, since custom scripts may also fail on restart if data already exists.

The text: "Sample data already exists..." shows in the output to confirm this fix:
image

@Ritvik-Jayaswal
Ritvik-Jayaswal marked this pull request as ready for review March 11, 2026 22:32
@Ritvik-Jayaswal
Ritvik-Jayaswal requested a review from a team as a code owner March 11, 2026 22:32
Comment thread scripts/emulator_entrypoint.sh Resolved

@guanzhousongmicrosoft guanzhousongmicrosoft left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would prefer a marker file indicating initialization status

Address PR review feedback:
- Replace mongosh-based database check with a simple marker file
  (.sample_data_initialized / .custom_data_initialized) written to
  DATA_PATH after successful init. On restart the marker file is
  detected and initialization is skipped. (visridha, guanzhousongmicrosoft)
- Flip sample data from opt-out (--skip-init-data) to opt-in
  (--load-sample-data / LOAD_SAMPLE_DATA env var). Sample data is
  now disabled by default. (guanzhousongmicrosoft, xgerman)
- Also add marker file for custom data (--init-data-path) to prevent
  re-initialization on restart.
@guanzhousongmicrosoft

guanzhousongmicrosoft commented Mar 19, 2026 •

Copy link
Copy Markdown
Member

In the custom init failure path, we now warn and continue, but the marker file is only written on success. With a persisted data dir, that means the same failing custom init will be retried on every restart and keep warning forever. Should this either still fail fast, or persist some state so restart behavior converges?

Comment thread scripts/emulator_entrypoint.sh Resolved
Comment thread scripts/emulator_entrypoint.sh Resolved
@Ritvik-Jayaswal

Copy link
Copy Markdown
Collaborator Author

In the custom init failure path, we now warn and continue, but the marker file is only written on success. With a persisted data dir, that means the same failing custom init will be retried on every restart and keep warning forever. Should this either still fail fast, or persist some state so restart behavior converges?

Fixed: Now writes marker file even on failure, so restart converges.

The previous nc-based TCP connection caused the TLS-enabled gateway to
crash with an SSLError when it received a raw (non-TLS) connection.
Instead, inspect /proc/net/tcp6 for a LISTEN socket (state 0A) on the
gateway port, which is zero-impact and avoids connecting to the port.
@documentdb-triage-tool

Copy link
Copy Markdown

🤖 Auto-triaged by documentdb-triage-tool.

Applied: infrastructure, bug
Project fields suggested: Component build · Priority P2 · Effort M · Status Needs Review
Confidence: 0.95 (mixed)

Reasoning

component from path globs (build); effort from diff stats (81+61 LOC, 1 files); LLM: Fixes a container crash on restart due to duplicate _id errors when re-running sample data init against a persisted data path.

If a label is wrong, remove it manually and ping @patty-chow so the rules can be tuned. The bot will not re-label items that already have component labels.

@alaye-ms

Copy link
Copy Markdown
Contributor

Stale PR.

@alaye-ms alaye-ms closed this Jun 16, 2026
@github-project-automation github-project-automation Bot moved this from Todo to Done in Issue triage Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

DocumentDB-local Container restartfials due to duplicate initialziation of demo data

7 participants