openstack4j
openstack4j copied to clipboard
Unable to retrieve current session. Please verify thread has a current session available.
I'm getting the following error when uploading a file, but unlike #745 it's not multi thread.
org.openstack4j.api.exceptions.OS4JException: Unable to retrieve current session. Please verify thread has a current session available.
at org.openstack4j.openstack.internal.BaseOpenStackService.builder(BaseOpenStackService.java:93)
at org.openstack4j.openstack.internal.BaseOpenStackService.builder(BaseOpenStackService.java:87)
at org.openstack4j.openstack.internal.BaseOpenStackService.put(BaseOpenStackService.java:58)
at org.openstack4j.openstack.storage.object.internal.ObjectStorageObjectServiceImpl.put(ObjectStorageObjectServiceImpl.java:125)
at org.openstack4j.openstack.storage.object.internal.ObjectStorageObjectServiceImpl.put(ObjectStorageObjectServiceImpl.java:105)
Here is the code I'm using to create the client and upload the file
OsClient client = OSFactory.builderV3()
.endpoint(endpoint)
.credentials(username, password)
.scopeToProject(projectId, domainId)
.authenticate();
client.objectStorage().objects().put(privateBucket, externalFileName, Payloads.create(new File(localFile)));
Is there something wrong with how the client is created? In most cases this uploading method seems to work just fine, but in one of my case scenarios it fails with this exception. Is this problem definitely related to threads? Thanks
Hi @lsarni, Starting easy: Does the authentication and subsequent swift command, you pasted above, work?
Since you're using the username, you need to provide a domain id. Only the user id is unique across all domains. You want: .credentials("admin", "secret", Identifier.byId("user domain id"))
Hint: In scopeToProject() you only need the projectId, as it is unique.
.scopeToProject(Identifier.byId("project id"))
I guess no session was established at all.
@auhlig Yes, the authentication works. I can handle the files just fine in most situations, the upload fails only in one of my scenarios (but I can't find out why it only fails in that case).
If instead of doing an upload I do this client.objectStorage().objects().get(privateBucket, externalFileName)
it doesn't fail.
The scopeToProject
takes two parameters, I'm using 2.20, so maybe your hint is in the newer versions.
Right for 2.20.
Most of OS4js Identity V3 was released with version 3.0.0 . AFAIK the issue, you pasted above, is resolved in that version.
So I'd recommend trying latest OS4j.
Fixed by upgrading to 3.0.3, thanks
I have changed the way I authenticate to the one recommended:
OsClient client = OSFactory.builderV3()
.endpoint(endpoint)
.credentials("admin", "secret", Identifier.byId("user domain id"))
.scopeToProject(Identifier.byId("project id"))
.authenticate();
And I'm using v 3.0.3 with the okhttp connector.
The error keeps occuring, with the same stack trace:
org.openstack4j.api.exceptions.OS4JException: Unable to retrieve current session. Please verify thread has a current session available.
at org.openstack4j.openstack.internal.BaseOpenStackService.builder(BaseOpenStackService.java:110)
at org.openstack4j.openstack.internal.BaseOpenStackService.builder(BaseOpenStackService.java:103)
at org.openstack4j.openstack.internal.BaseOpenStackService.put(BaseOpenStackService.java:69)
at org.openstack4j.openstack.storage.object.internal.ObjectStorageObjectServiceImpl.put(ObjectStorageObjectServiceImpl.java:125)
at org.openstack4j.openstack.storage.object.internal.ObjectStorageObjectServiceImpl.put(ObjectStorageObjectServiceImpl.java:105)
Could you share more info? What did you do @lsarni? Just authentication and get session() ?
I was doing a couple of things between the authentication and the put (as getting the endpoint url for swift and updating the "X-Container-Meta-Temp-Url-Key"
) but I commented those out and the problem remains.
So now I'm just using openstack4j to do this:
OsClient client = OSFactory.builderV3()
.endpoint(endpoint)
.credentials("admin", "secret", Identifier.byId("user domain id"))
.scopeToProject(Identifier.byId("project id"))
.authenticate();
client.objectStorage().objects().put(privateBucket, externalFileName, Payloads.create(new File(localFile)));
Apart from that I'm using Apache Tomcat 7.0 to run the web app that calls this methods. Whether the container I'm using is public or private doesn't seem to change the outcome (not that it should, but well...).
Sorry I can't be more specific, but any recommendations on where to look are welcome, otherwise I will just keep looking.
I used "Using the Same Client Between Threads", but it still have some error.some one can help me?
Hi All,
Even i am facing the same issue when using the Glance services. Initially the code works fine, but after submitting multiple requests, I am seeing this issue "org.openstack4j.api.exceptions.OS4JException: Unable to retrieve current session. Please verify thread has a current session available."
I am using Openstack4J 3.1.0 with okhttp connector.
Please suggest if there is any resolution to it. I think the okhttp connector has this bug????
The way i solved this error is by calling authenticate before every request, which is not performant at all ...
When we call .authenticate()
on OSFactory.builderV3()
we create session only for current Thread.
Since our application uses multiple threads, each subsequent request can be proccessed by different one.
To ensure, that we already created session in current thread (the one which is handling our request) we need to check if OSClientSession.OSClientSessionV3.getCurrent()
is not null.
If OSClientSession
is null, then we have to call .authenticate()
again - with this approach, after calling several consecutive requests, session will be initialized in every thread.