Modifying patient takes a long time - KeepSource: False

Hey Everyone,
Been using Orthanc alot over the last few months and wrote a python script that updates patients “PatientID”. I am using the patients "modify’ endpoint for this. when I include ‘KeepSource’: False in my command json it takes an extensive amount of time.

I am on Orthanc 1.12.2, using MYSQL DB. 32gb of memory both the DB and image storage are running on an SSD drive.

See the part of the script below that is taking a long time…

def update_patient_metadata(patient_id, new_patient_id):
    print(patient_id, new_patient_id)
    # Define headers for the request (e.g., content type)
    headers = {'Content-Type': 'application/json'}
    modify_endpoint = f'{orthanc_url}/patients/{patient_id}/modify'
    command = {
        'Replace': {
            'PatientID': str(new_patient_id),
        }, 'Force': True,
        'KeepSource': False
    }
    # Send POST request to modify the instance
    response = requests.post(modify_endpoint, data=json.dumps(command), headers=headers)
    if response.status_code == 200:
        return True
    else:
        return False

Relevant logs:

T0430 12:41:44.805049    JOBS-WORKER-3 ServerIndex.cpp:128] Remaining ancestor "4187a2f1-6692ac6f-344ffae7-b00a174e-f94ee966" (3)
T0430 12:41:44.806049    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:41:44.806049    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:41:44.806049    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:41:44.806049    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:41:44.807071    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:41:44.819657    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
I0430 12:41:44.820654    JOBS-WORKER-3 FilesystemStorage.cpp:266] Deleting attachment "1fcb1ce7-00c6-4941-af21-b7756e433069" of type 1
T0430 12:41:44.820654    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:41:44.820654             201c OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:41:44.821653             201c OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:41:44.822648             201c OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:41:44.823644             201c OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:41:44.823644             201c OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
I0430 12:41:44.823644             201c FilesystemStorage.cpp:182] Reading attachment "009a538d-84e5-4db5-bff2-aeea02fbc9f1" of "DICOM" content type (range from 0 to 10130)
T0430 12:41:44.823644             32e8 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
I0430 12:41:44.823644             201c FilesystemStorage.cpp:189] Read range of attachment "009a538d-84e5-4db5-bff2-aeea02fbc9f1" (9.89KB in 0ns = infGbps)
T0430 12:41:44.824642             32e8 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:41:44.825638             32e8 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:41:44.825638             32e8 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:41:44.825638             32e8 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:41:44.825638             32e8 OrthancPlugins.cpp:5882] (plugins) Calling service 4018 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancDicomWeb.dll
T0430 12:41:44.825638    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:41:44.826637    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:41:44.827634             201c OrthancPlugins.cpp:5882] (plugins) Calling service 3004 from plugin C:\Program Files\Orthanc Server\Plugins\libOrthancOHIF-Windows64.dll
I0430 12:41:44.827634             201c OrthancPlugins.cpp:3278] (plugins) Plugin making REST PUT call on URI /instances/059e3cb6-4c1df0e5-2bc8aa64-4c3d65ef-a5b49576/metadata/4202 (built-in API)
T0430 12:41:44.828665             32e8 OrthancPlugins.cpp:5882] (plugins) Calling service 4011 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancDicomWeb.dll
T0430 12:42:01.563920    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:01.563920    JOBS-WORKER-3 ServerIndex.cpp:161] Change related to resource 17926b25-336664d8-19258398-9ad945ad-d5cbab78 of type Instance: Deleted
T0430 12:42:01.563920    JOBS-WORKER-3 ServerIndex.cpp:128] Remaining ancestor "b39e7049-e6a57ab0-13e231a2-0c80e8a0-4f7d2d96" (3)
T0430 12:42:01.565190    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:01.565190    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:01.565190    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:01.565190    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:01.566169    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:01.580050    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
I0430 12:42:01.580050    JOBS-WORKER-3 FilesystemStorage.cpp:266] Deleting attachment "65ca2323-334f-4ddf-9df7-99cc98cf1fe9" of type 1
T0430 12:42:01.580050    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:01.581047        SAVE-JOBS OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:01.598999        SAVE-JOBS OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:01.629224        SAVE-JOBS OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:01.629224        SAVE-JOBS OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:01.629224    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:01.630237    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:04.052911    WEBDAV-UPLOAD OrthancWebDav.cpp:1246] Cleaning up the empty WebDAV upload folders
T0430 12:42:11.706680        SAVE-JOBS ServerContext.cpp:327] Serializing the content of the jobs engine
T0430 12:42:18.332146    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:18.332146    JOBS-WORKER-3 ServerIndex.cpp:161] Change related to resource 18daa2ec-e2a8f668-631a0b45-af9bafcb-d78dd34d of type Instance: Deleted
T0430 12:42:18.332146    JOBS-WORKER-3 ServerIndex.cpp:128] Remaining ancestor "8ed2e8d1-d7a5e14b-8eee2b9c-5324fb75-c4c32619" (3)
T0430 12:42:18.332972    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:18.332972    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:18.332972    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:18.333972    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:18.334805    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:18.347267    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
I0430 12:42:18.347267    JOBS-WORKER-3 FilesystemStorage.cpp:266] Deleting attachment "72b015d7-49f3-4cd3-a62e-c85d767fcbd6" of type 1
T0430 12:42:18.348256    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:18.348256             201c OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:18.349254             201c OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:18.350252             201c OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:18.351248             201c OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:18.352246             201c OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:18.352246             201c ServerIndex.cpp:161] Change related to resource 059e3cb6-4c1df0e5-2bc8aa64-4c3d65ef-a5b49576 of type Instance: UpdatedMetadata
T0430 12:42:18.355240             201c OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:18.355240             201c OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:18.355240             201c OrthancPlugins.cpp:5882] (plugins) Calling service 3001 from plugin C:\Program Files\Orthanc Server\Plugins\libOrthancOHIF-Windows64.dll
I0430 12:42:18.355240             201c OrthancPlugins.cpp:3221] (plugins) Plugin making REST GET call on URI /instances/153063d1-0d2713d5-641fa00a-305ddb8c-f4759aa1/metadata/4202 (built-in API)
T0430 12:42:18.355240    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0430 12:42:18.356244    JOBS-WORKER-3 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll

Figured I should add that when I say a long time I mean about an hour for 1 patient with 2 studies.

Hi,

All I can say from the logs is the job that each deletion takes 17 seconds:

I0430 12:41:44.820654    JOBS-WORKER-3 FilesystemStorage.cpp:266] Deleting attachment "1fcb1ce7-00c6-4941-af21-b7756e433069" of type 1
I0430 12:42:01.580050    JOBS-WORKER-3 FilesystemStorage.cpp:266] Deleting attachment "65ca2323-334f-4ddf-9df7-99cc98cf1fe9" of type 1
I0430 12:42:18.347267    JOBS-WORKER-3 FilesystemStorage.cpp:266] Deleting attachment "72b015d7-49f3-4cd3-a62e-c85d767fcbd6" of type 1

It might be related to this topic but I actually never got the final feedback from @csimon3 so the topic is on hold right now.

Best regards,

Alain.

Hi Alain,

Sorry for the delay.

We will put this item back in front of the queue since it is almost done and unit tests passed. We just want to do a new full testing and will let you know the outcome.

Best regards,

Cedric.

The instance of orthanc is only 70k studies just using it as an intermediary for a migration. Would I be better off converting away from mysql and using the sqllite db?

Well 70k studies is not that small and we usually advise to use postgresql from 5K-10K DB but that’s just a rule of thumb. SQlite might actually work very well.

But you’ll have to re-ingest everything since there are no conversion script from one DB to the other :frowning:

Ahh and I might have a storage constraint on the server to have both live at the same time. Any other ideas to make this go faster? Trying to wrap up this migration process and only being able to do 1 patient ever hour will take a very long time.

Well, if you are using Docker, I have built an image with the patch suggested by @csimon3. If not, what platform are you using ?

I am on a windows server

You can then:

  • download the “mainline” version from here
  • copy the file in C:\Program Files\Orthanc Server\Plugins,
  • stop your Orthanc service,
  • rename the current OrthancMySQLIndex.dll into OrthancMySQLIndex.dll_old (it must not have a .dll extension or it will be loaded by Orthanc)
  • rename OrthancMySQLIndex-mainline.dll into OrthancMySQLIndex.dll
  • restart the Orthanc service

Please tell us if this improves things on your side …

I0503 12:04:45.307618          HTTP-20 JobsRegistry.cpp:794] New job submitted with priority 0: 911bc0cb-bf34-4e70-96a5-a0fae044b6f7
I0503 12:04:45.307618    JOBS-WORKER-2 JobsEngine.cpp:134] (jobs) Executing job with priority 0 in worker thread 2: 911bc0cb-bf34-4e70-96a5-a0fae044b6f7
I0503 12:04:45.307618  JOB-INS-WORK-56 ResourceModificationJob.cpp:205] Modifying instance in a job: 0010f8ab-cc9c8d35-72e85255-88e4a216-66175042
T0503 12:04:45.307618  JOB-INS-WORK-56 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
I0503 12:04:45.307618  JOB-INS-WORK-57 ResourceModificationJob.cpp:205] Modifying instance in a job: 02f75cc1-8ce55bd9-3fffc5bf-f93ff86c-2f4840b3
I0503 12:04:45.307618  JOB-INS-WORK-58 ResourceModificationJob.cpp:205] Modifying instance in a job: 060dbaa3-6243030c-388bbb39-3f442e9d-e0f6150b
I0503 12:04:45.307618  JOB-INS-WORK-59 ResourceModificationJob.cpp:205] Modifying instance in a job: 079f5946-121a450a-88e9c6a2-544db162-85f844f7
I0503 12:04:45.308587  JOB-INS-WORK-60 ResourceModificationJob.cpp:205] Modifying instance in a job: 0c1db79c-c2a9eb0b-01a7b6e6-600cdc94-a559c5b8
I0503 12:04:45.308587  JOB-INS-WORK-61 ResourceModificationJob.cpp:205] Modifying instance in a job: 1499523a-081a77fc-2cf90a52-1b83a6d0-c5263c2e
I0503 12:04:45.308587  JOB-INS-WORK-62 ResourceModificationJob.cpp:205] Modifying instance in a job: 15a57702-5e548ca9-f7b9b14e-da86b77f-39084c21
I0503 12:04:45.308587  JOB-INS-WORK-63 ResourceModificationJob.cpp:205] Modifying instance in a job: 1ac7c022-5e8bdef3-53a67020-f0179a3b-182ae752
T0503 12:04:45.314169  JOB-INS-WORK-56 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0503 12:04:45.314722  JOB-INS-WORK-56 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0503 12:04:45.314929  JOB-INS-WORK-56 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
T0503 12:04:45.314929  JOB-INS-WORK-56 OrthancPlugins.cpp:5882] (plugins) Calling service 40 from plugin C:\Program Files\Orthanc Server\Plugins\OrthancMySQLIndex.dll
W0503 12:04:45.315436  JOB-INS-WORK-56 ResourceModificationJob.cpp:224] An error occurred while executing a Modification job on instance 0010f8ab-cc9c8d35-72e85255-88e4a216-66175042: 
E0503 12:04:45.315436  JOB-INS-WORK-56 ThreadedSetOfInstancesJob.cpp:273] Error in a non-permissive job: Unknown resource

here are the logs, I get an error now when I run the script saying “ThreadedSetOfInstancesJob.cpp:273] Error in a non-permissive job: Unknown resource”

Is this reproducible ? Does it happen with all studies ?
Can you access the instance in error ?
Can you provide a way to reproduce it on a separate system ?

If I call the modify patient endpoint on any patient with the following query it happens.

    print(patient_id, new_patient_id)
    # Define headers for the request (e.g., content type)
    headers = {'Content-Type': 'application/json'}
    modify_endpoint = f'{orthanc_url}/patients/{patient_id}/modify'
    command = {
        'Replace': {
            'PatientID': str(new_patient_id),
        }, 'Force': True,
        'KeepSource': False
    }
    # Send POST request to modify the instance
    response = requests.post(modify_endpoint, data=json.dumps(command), headers=headers)
    if response.status_code == 200:
        return True
    else:
        return False

where patient ID = the internal orthanc id
and new patient ID = new patient ID id like to change the dicom patient id to.

With the below docker-compose setup which uses the same version as you, I was able to modify a patient.

So, please, if you think this is an Orthanc bug, provide us with a full reproducible setup for us to investigate.

version: "2"
services:
  orthanc:
    image: osimis/orthanc:improve-mysql-delete-unstable
    depends_on: [index]
    ports: ["104:4242", "80:8042"]
    volumes: ["storage:/var/lib/orthanc/db:Z"]
    environment:
      ORTHANC__MYSQL__HOST: index
      ORTHANC__MYSQL__USERNAME: test-user
      ORTHANC__MYSQL__DATABASE: orthanc
      ORTHANC__MYSQL__PASSWORD: foo
      ORTHANC__REGISTERED_USERS: |
        {"demo": "demo"}
      ORTHANC__OVERWRITE_INSTANCES: true

    restart: unless-stopped
  index:
    image: mysql:8.0
    ports: ["3306:3306"]
    command:
      [
        mysqld,
        --default-authentication-plugin=mysql_native_password,
        --log-bin-trust-function-creators=1,
      ]
    volumes: ["index:/var/lib/mysql:Z"]
    environment:
      MYSQL_PASSWORD: foo
      MYSQL_USER: test-user
      MYSQL_DATABASE: orthanc
      MYSQL_ROOT_PASSWORD: foo-root
      
    restart: unless-stopped
volumes:
  storage:
  index: