openstack4j / openstack4j

Fluent OpenStack SDK for Java
https://openstack4j.github.io/
Apache License 2.0
109 stars 91 forks source link

Wrong service API URL used when having concurrent requests towards different Openstack modules #181

Open Grofight opened 2 years ago

Grofight commented 2 years ago

Current Behavior

Thread A: Makes a request to generate a token. POST <keystone_endpoint>/tokens

Thread B: Makes a request to get server details. GET <keystone_endpoint>/servers/<uuid>

Request A is sucessfully finished, token is generated, service catalog contains correct API endpoints. Request B is initiated 1 ms later, but with wrong API endpoint. It uses Identity API GET <keystone_endpoint>/servers/<uuid> instead of Compute API GET <nova_endpoint>/servers/<uuid>.

Expected Behavior

Openstack client should be thread-safe. Requests to different Openstack modules in multiple threads should not affect each other (particulary in terms of API endpoint resolution).

Environment

openstack4j-core - 3.10 openstack4j-httpclient - 3.10 Openstack Pike (via OSClientV2)

Impact

Requests may fail with 404 Not Found because of wrong API endpoint.

Steps to Reproduce

  1. A single openstack client in two threads
  2. Thread A performs a request to some Openstack module
  3. Thread B performs a request to another Openstack module right after the first request finishes. Note: the issue was observed in case of token generation + server details retrieval, other cases were not tested.
neeraj-singhjeena commented 1 week ago

This issue highlights a potential thread-safety problem in the OpenStack client. The fact that Request B is using the wrong API endpoint, even after Request A has successfully generated a token with the correct service catalog, suggests that the client is not properly synchronizing access to its internal state.

To fix this issue, I would recommend introducing thread-safe mechanisms to ensure that the client's internal state is properly updated and synchronized across threads. This could involve using thread-safe data structures, such as ConcurrentHashMap or CopyOnWriteArrayList, to store the service catalog and API endpoints.

Additionally, it would be helpful to add some logging or debugging statements to track the API endpoints being used by each request, to better understand the issue and verify the fix.