influxdata / influxdb-client-python

InfluxDB 2.0 python client
https://influxdb-client.readthedocs.io/en/stable/
MIT License
724 stars 187 forks source link

Handling "Unauthorized Access" Error with MultiprocessingWriter in InfluxDB Client After Service Restart #619

Closed Paulius11 closed 11 months ago

Paulius11 commented 12 months ago

Specifications

After restarting the InfluxDB service (using sudo systemctl restart influxd), my Python application loses its connection to the InfluxDB instance. Subsequently, it throws an “unauthorized access” error. This issue arises specifically in the context of using MultiprocessingWriter for handling data writes.

Code sample to reproduce problem


@classmethod
def error(cls, conf: (str, str, str), data: str, exception: InfluxDBError):
   if exception and exception.message == "unauthorized access":
      print(exception)

 callback = BatchingCallback()
    self.writer = MultiprocessingWriter(
        url=f"{self.address}:{self.port}",
        username=USERNAME,
        password=PASSWORD,
        org=ORG,
        write_options=WriteOptions(batch_size=100),
        success_callback=callback.success,
        error_callback=callback.error,
        retry_callback=callback.retry,
    )
    self.writer.start()
    self.writer.write(bucket="my-bucket", record=f"mem,tag=a value=5")

Expected behavior

After the InfluxDB service is restarted (using sudo systemctl restart influxd), the Python application using the influxdb_client package and MultiprocessingWriter should automatically re-establish the connection to the InfluxDB instance. The reconnection should occur without the need to restart the entire Python application, ensuring continuous data writes through the MultiprocessingWrite

Actual behavior

Currently, when the InfluxDB service is restarted, the Python application loses its connection to the InfluxDB instance.
The application reports an “unauthorized access” error and fails to reconnect to the database automatically.
This issue requires a manual restart of the entire Python application to re-establish the connection, disrupting the data writing process.

Additional info

No response

bednar commented 12 months ago

Hi @Paulius11,

Thank you for providing detailed information about the issue you're encountering with InfluxDB and your Python application. Based on your description, it appears that the problem stems from the way authentication is handled after the InfluxDB service restarts.

When you use username and password for authentication, it typically relies on an HTTP session to maintain the authorization state. Restarting the InfluxDB service invalidates this session, leading to the "unauthorized access" error you are encountering. This is why your application is unable to automatically re-establish the connection and requires a manual restart.

To resolve this issue, I recommend switching to token-based authentication. Token-based authentication is more robust and better suited for long-running tasks like the ones you are managing with your application. Unlike session-based authentication, tokens do not depend on the state of the server session, so they remain valid even after the server restarts, ensuring a smoother and more reliable reconnection process.

Regards