Conversation
`addIoTDatabaseIndex` drops every collection in `iotagentjson` and then
re-creates `devices` and `groups`. The IoT Agent container is already up and
connected by then - it is started by the same `docker compose up` - so it can
create `devices` itself in the window between the drop loop and the
`createCollection` call.
On mongo 6.0, which is what `MONGO_DB_VERSION` pins, `db.createCollection` on
an existing collection throws `NamespaceExists` and `mongosh` exits 1. The
script runs under `set -e`, so the whole `services` invocation dies - after
the stack is up, before the tutorial has done anything:
MongoServerError: Collection iotagentjson.devices already exists.
The two `createCollection` calls are redundant: `createIndex` creates the
collection implicitly if it is missing. Removing them makes the block
idempotent and closes the window, with no other change - verified on mongo 6.0
that the resulting index sets are identical:
devices: {_id}, {_id.service,_id.id,_id.type}, {_id.type}, {_id.id}
groups: {_id}, {_id.resource,_id.apikey,_id.service}, {_id.type}
and that the amended block exits 0 when run twice in a row and when the
collection has been created concurrently in between - the two cases that make
the current version exit 1.
Caught in tutorials.Big-Data-Flink's own CI; the same function is carried
verbatim by nine tutorials, this one among them.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
addIoTDatabaseIndexdrops every collection iniotagentjsonand then re-createsdevicesandgroups:By the time it runs, the IoT Agent container is already up and connected to the same database — it is started by the same
docker compose up. If the agent createsdevicesin the window between the drop loop and thecreateCollectioncall, the call fails:On mongo 6.0, which is what
MONGO_DB_VERSIONpins,db.createCollectionon an existing collection throwsNamespaceExists;mongoshthen exits 1; andservicesruns underset -e, so the whole invocation dies — after the stack is up, before the tutorial has run a single request.Because it depends on who wins the race, it shows up as an intermittent mystery red rather than as a bug. It was caught in
tutorials.Big-Data-Flink's own CI, where the identical commit then passed on re-run.(Worth knowing for later: mongo 8 made
createCollectionreturn{ok:1}in this case, so raisingMONGO_DB_VERSIONwould hide this rather than fix it.)The fix
The two
createCollectioncalls are redundant —createIndexcreates the collection implicitly when it is missing. Dropping them closes the window with no other change.Verified on mongo 6.0:
and the resulting indexes are identical either way:
Scope
Nine tutorials carry this function verbatim. This is one of them; the same one-hunk change is going to each, with FIWARE/tutorials.Big-Data-Flink#17 as the original.
🤖 Generated with Claude Code