FIWARE / context.Orion-LD

Context Broker and CEF building block for context data management which supports both the NGSI-LD and the NGSI-v2 APIs
https://www.etsi.org/deliver/etsi_gs/CIM/001_099/009/01.06.01_60/gs_CIM009v010601p.pdf
GNU Affero General Public License v3.0
51 stars 43 forks source link

Orion container crashes when user tries to add data to an entity that doesnt exist. #1714

Open AizenvoltPrime opened 2 hours ago

AizenvoltPrime commented 2 hours ago
services:
    orion:
        labels:
            org.fiware: "tutorial"
        image: fiware/orion-ld:${ORION_LD_VERSION}
        hostname: orion
        container_name: fiware-orion
        depends_on:
            - mongo-db
        networks:
            - default
        ports:
            - "${ORION_LD_PORT}:${ORION_LD_PORT}"
        command: -dbhost mongo-db -logLevel DEBUG -forwarding -mongocOnly -wip entityMaps
        healthcheck:
            test: curl --fail -s http://orion:${ORION_LD_PORT}/version || exit 1
            interval: 5s

    # Quantum Leap is persisting Short Term History to Crate-DB
    quantumleap:
        image: orchestracities/quantumleap:${QUANTUMLEAP_VERSION}
        labels:
            org.fiware: "tutorial"
        hostname: quantumleap
        container_name: fiware-quantumleap
        ports:
            - "${QUANTUMLEAP_PORT}:${QUANTUMLEAP_PORT}"
        depends_on:
            - crate-db
            - redis-db
        environment:
            - CRATE_HOST=crate-db
            - REDIS_HOST=redis-db
            - REDIS_PORT=${REDIS_PORT}
            - REDIS_USERNAME=${REDIS_USERNAME}
            - REDIS_PASSWORD=${REDIS_PASSWORD}
            - LOGLEVEL=DEBUG
        healthcheck:
            test: curl --fail -s http://quantumleap:${QUANTUMLEAP_PORT}/version || exit 1

    # Databases
    mongo-db:
        labels:
            org.fiware: "tutorial"
        image: mongo:${MONGO_DB_VERSION}
        hostname: mongo-db
        container_name: db-mongo
        expose:
            - "${MONGO_DB_PORT}"
        ports:
            - "${MONGO_DB_PORT}:${MONGO_DB_PORT}" # localhost:27017
        networks:
            - default
        volumes:
            - mongo-db:/data
        healthcheck:
            test:
                [
                    "CMD",
                    "mongo",
                    "--quiet",
                    "127.0.0.1/test",
                    "--eval",
                    "'quit(db.runCommand({ ping: 1 }).ok ? 0 : 2)'",
                ]
            interval: 5s

    crate-db:
        labels:
            org.fiware: "tutorial"
        image: crate:${CRATE_VERSION}
        hostname: crate-db
        container_name: db-crate
        networks:
            - default
        ports:
            # Admin UI
            - "4200:4200"
            # Transport protocol
            - "4300:4300"
        command: crate -Cauth.host_based.enabled=false  -Ccluster.name=democluster -Chttp.cors.enabled=true -Chttp.cors.allow-origin="*" -Cdiscovery.type=single-node
        environment:
            - CRATE_HEAP_SIZE=2g # see https://crate.io/docs/crate/howtos/en/latest/deployment/containers/docker.html#troubleshooting
        volumes:
            - crate-db:/data

    redis-db:
        labels:
            org.fiware: "tutorial"
        image: redis:${REDIS_VERSION}
        hostname: redis-db
        container_name: db-redis
        networks:
            - default
        ports:
            - "${REDIS_PORT}:${REDIS_PORT}"
        volumes:
            - redis-db:/data
            - ./redis.conf:/usr/local/etc/redis/redis.conf
        command: redis-server /usr/local/etc/redis/redis.conf
        environment:
            - REDIS_USERNAME=${REDIS_USERNAME}
            - REDIS_PASSWORD=${REDIS_PASSWORD}
        healthcheck:
            test: >
                sh -c 'export REDISCLI_AUTH="${REDIS_PASSWORD}" &&
                redis-cli -h redis-db -p ${REDIS_PORT} --user ${REDIS_USERNAME} ping'
            interval: 10s

networks:
    default:
        labels:
            org.fiware: "tutorial"
        ipam:
            config:
                - subnet: 172.18.1.0/24

volumes:
    mongo-db: ~
    crate-db: ~
    redis-db: ~

Also for the variables in docker compose: ORION_LD_PORT=1026 ORION_LD_VERSION=1.7.0 QUANTUMLEAP_VERSION=1.0.0 QUANTUMLEAP_PORT=8668 CRATE_VERSION=latest MONGO_DB_PORT=27017 MONGO_DB_VERSION=latest REDIS_PORT=6380 REDIS_VERSION=6 for REDIS_USERNAME and REDIS_PASSWORD put whatever

If you just start all the containers and then try to add data to an entity that doesnt exist the orion container crashes. You could say I could run a cron job to check if the orion container stopped working for some reason and work around this issue but I dont think the intended functionality is for the container to crash every time someone does a wrong request. This was how I discovered the other issue with subscriptions I posted last week.

curl --location 'http://SERVER_IP:1026/ngsi-ld/v1/entities/urn:ngsi-ld:test_entity_id/attrs' \
--header 'Content-Type: application/ld+json' \
--header 'NGSILD-Tenant: openiot' \
--data-raw '{
    "temperature": {
        "type": "Property",
        "value": 2.5
    },
    "@context": {
        "temperature": "https://iemis-demo.indigital.gr/api/ngsi-ld-attributes/temperature"
    }
}
'
kzangeli commented 2 hours ago

That sounds like an old issue Infixed some time ago. Is it an entity that is registered "elsewhere" but doesn't exist locally?

What is the tag of the broker? (not clear ...)

AizenvoltPrime commented 2 hours ago

No, just think of the example I gave you on the subscriptions issue, where you skip the entity creation and subscription requests and just do the add data when the entity and subscriptions dont exist yet. In the app I am developing I have made a frontend form for users to create orion entities for iot devices dynamically and I discovered that issue in the process of developing the app. I don't think that it was intended for the container to crash if somehow, because of a bug in code a wrong post request was made.

kzangeli commented 2 hours ago

Ok. There is a known bug in the "/attrs" service. I haven't spent any time on it as that API endpoint is to be deprecated soon (the sooner the better).

Try PATCH /entities/[entityId]. Definitely preferable. Also, if you can, please stop using application/ld+json. It only gives the broker unnecessary work and extra code to execute

AizenvoltPrime commented 2 hours ago

Ok I will try to add data using PATCH then. In regards to the application/ld+json, I tried doing something like

curl --location 'http://SERVER_IP:1026/ngsi-ld/v1/entities/' \
--header 'Content-Type: application/json' \
--header 'NGSILD-Tenant: openiot' \
--header 'Link: <https://mysite.com/api/ngsi-ld-attributes>; rel="http://www.w3.org/ns/json-ld#context"; type="application/ld+json"' \
--data '{
  "id": "urn:ngsi-ld:test_entity_id2",
  "type": "test_entity_type",
  "temperature": {
    "type": "Property",
    "value": 23.4
  }
}'

the link has content like:

{
  "@context": {
    "attributes": "https://mysite.com/api/ngsi-ld-attributes/",
    "temperature": { "@id": "attributes:temperature", "@type": "xsd:float", "unit": "°C", "en": "Temperature", "el": "Θερμοκρασία" },
    "humidity": { "@id": "attributes:humidity", "@type": "xsd:integer", "unit": "%", "en": "Humidity", "el": "Υγρασία" }
  }
}

but the entities dont get saved correctly in mongo. They look like:

{
  "_id": {
    "id": "urn:ngsi-ld:test_entity_id",
    "type": "https://uri.etsi.org/ngsi-ld/default-context/test_entity_type",
    "servicePath": "/"
  },
  "attrNames": [
    "https://uri.etsi.org/ngsi-ld/attributestemperature"
  ],
  "attrs": {
    "https://uri=etsi=org/ngsi-ld/attributestemperature": {
      "type": "Property",
      "creDate": 1732901606.1755981,
      "modDate": 1732901606.1755981,
      "value": 23.4,
      "mdNames": []
    }
  },
  "creDate": 1732901606.1755981,
  "modDate": 1732901606.1755981,
  "lastCorrelator": ""
}

when they should be like:

{
  "_id": {
    "id": "urn:ngsi-ld:test_entity_id",
    "type": "https://uri.etsi.org/ngsi-ld/default-context/test_entity_type",
    "servicePath": "/"
  },
  "attrNames": [
    "https://mysite.com/api/ngsi-ld-attributes/temperature"
  ],
  "attrs": {
    "https://mysite=com/api/ngsi-ld-attributes/temperature": {
      "type": "Property",
      "creDate": 1732901686.2456894,
      "modDate": 1732901686.2456894,
      "value": 23.4,
      "mdNames": []
    }
  },
  "creDate": 1732901686.2456894,
  "modDate": 1732901686.2456894,
  "lastCorrelator": ""
}

which is done with the initial way I create entities that I showed in the example above.