adnanhemani opened a new pull request, #1765:
URL: https://github.com/apache/polaris/pull/1765

   I came across an interesting bug yesterday that we need to fix to ensure 
that tasks can use the BasePersistence object, as they run outside of user call 
contexts.
   
   What I was trying to do:
   1. Create and run a Task which dumps some information to the persistence. In 
order to do this, I was using the following line of code to get a 
BasePersistence object: 
`metaStoreManagerFactory.getOrCreateSessionSupplier(CallContext.getCurrentContext().getRealmContext()).get();`
   2. Get the following error message when executing the last `.get()` call: 
   ```
   jakarta.enterprise.context.ContextNotActiveException: RequestScoped context 
was not active when trying to obtain a bean instance for a client proxy...
   ```
   
   When digging deeper into why this is happening, I realized that due to the 
Supplier's lazy-loading at 
https://github.com/apache/polaris/blob/main/extension/persistence/relational-jdbc/src/main/java/org/apache/polaris/extension/persistence/relational/jdbc/JdbcMetaStoreManagerFactory.java#L100-L105,
 the `.get()` was actually using a RequestScoped realmContext bean given by the 
previously-ran `TokenBroker` initialization (which is a `RequestScoped` object 
here: 
https://github.com/apache/polaris/blob/main/quarkus/service/src/main/java/org/apache/polaris/service/quarkus/config/QuarkusProducers.java#L290-L299.
 Given this is a relatively-new addition, this may be why we haven't seen this 
bug previously.
   
   As Tasks run asynchronously, likely after the original request was already 
completed, this error actually makes sense - we should not be able to use a 
request scoped bean inside of a Task execution. But upon further looking, we do 
not actually need `realmContext` for anything other than resolving the 
`realmIdentifier` once during the BasePersistence object initialization - as a 
result, we can cache the BasePersistence object using a supplier that caches 
the original result instead of constantly making new objects. This will also 
solve our issue, as the original request scoped RealmContext bean will not be 
used again during the Task's call to get a BasePersistence object.
   
   I've added a test case that shows the difference between the OOTB supplier 
and my ideal way to solve this problem using a CachedSupplier. If there is 
significant concern that we cannot cache the BasePersistence object, we can 
materialize the RealmContext object prior to the supplier so that at a minimum 
the RequestScoped RealmContext object is not being used - but I'm not sure if 
there's an easy way to test this, given that the MetastoreFactories are Quarkus 
`ApplicationScoped` objects.
   
   Please note, this is an issue in both EclipseLink and JDBC, as they have 
almost identical code paths here.
   
   Many thanks to @singhpk234 for being my debugging rubber ducky :)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@polaris.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to