confluentinc / cli

CLI for Confluent Cloud and Confluent Platform
https://docs.confluent.io/confluent-cli/current/overview.html
Other
56 stars 10 forks source link

Concurrent Confluent CLI processes can read an inconsistent config.json file #2622

Open gphilipp opened 8 months ago

gphilipp commented 8 months ago

I have this shell script which uses the Confluent CLI to delete all subjects in a concurrent fashion:

#!/usr/bin/env bash
delete_subject() {
    echo "Soft deleting subject $1"
    confluent schema-registry schema delete --force --version all --subject "$1"
    echo "Hard deleting subject $1"
    confluent schema-registry schema delete --force --permanent --version all --subject "$1"

}

subjects=$(confluent schema-registry schema list -o json | jq -r ".[].subject")

# Perform deletes in parallel
for subject in ${subjects}; do
    delete_subject "$subject" &
    sleep 0.2
done
wait

echo "All subjects deleted."

It works most of the time, but I sometimes get these error messages:

Successfully soft deleted all versions for subject "insurance_customer_activity-value".
  Version  
-----------
        1  
Successfully soft deleted all versions for subject "payment_transaction-value".
  Version  
-----------
        1  
Hard deleting subject shoestore_clickstream-value
Successfully hard deleted all versions for subject "gaming_player-value".
  Version  
-----------
        2  
Hard deleting subject shoestore_shoe-value
Hard deleting subject fleetmgmt_location-value
Error: unable to read configuration file "/Users/gphilippart/.confluent/config.json": invalid character ']' after top-level value
Hard deleting subject pizzastore_order_cancelled-value
Hard deleting subject insurance_customer_activity-value
Error: unable to read configuration file "/Users/gphilippart/.confluent/config.json": invalid character ']' after top-level value
Error: unable to read configuration file "/Users/gphilippart/.confluent/config.json": invalid character ']' after top-level value
Error: unable to read configuration file "/Users/gphilippart/.confluent/config.json": invalid character ']' after top-level value
Hard deleting subject payment_transaction-value

What is probably happening is that another process reads the config.json file in the middle of the config write operation. It would be best to write the config.json changes in a temporary file and then renaming it, which is an atomic operation at the OS level.

brianstrauch commented 3 months ago

While the team works on support for running the Confluent CLI in parallel, my recommendation would be to use a separate configuration file for each thread running stateful Confluent CLI commands. You can change the path of the configuration file by prefixing commands with HOME=<tmpdir>, so each configuration file will live at a different <tmpdir>/.confluent/config.json.