From 981a32953bc025ea083adf7723d4e1f3647f8da4 Mon Sep 17 00:00:00 2001 From: kzangeli Date: Sat, 19 Sep 2026 14:12:38 +0200 Subject: [PATCH] fix: addIoTDatabaseIndex races the IoT Agent and kills the script `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) --- services | 2 -- 1 file changed, 2 deletions(-) diff --git a/services b/services index d591892..a20b345 100755 --- a/services +++ b/services @@ -176,11 +176,9 @@ addIoTDatabaseIndex () { conn = new Mongo(); db = conn.getDB("iotagentjson"); db.getCollectionNames().forEach(c=>db[c].drop()); - db.createCollection("devices"); db.devices.createIndex({"_id.service": 1, "_id.id": 1, "_id.type": 1}); db.devices.createIndex({"_id.type": 1}); db.devices.createIndex({"_id.id": 1}); - db.createCollection("groups"); db.groups.createIndex({"_id.resource": 1, "_id.apikey": 1, "_id.service": 1}); db.groups.createIndex({"_id.type": 1});' > /dev/null echo -e " \033[1;32mdone\033[0m"