[ 
https://issues.apache.org/jira/browse/KNOX-3156?focusedWorklogId=971969&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-971969
 ]

ASF GitHub Bot logged work on KNOX-3156:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 05/Jun/25 14:31
            Start Date: 05/Jun/25 14:31
    Worklog Time Spent: 10m 
      Work Description: pzampino commented on code in PR #1053:
URL: https://github.com/apache/knox/pull/1053#discussion_r2128994148


##########
gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/shirorealm/KnoxCacheManager.java:
##########
@@ -99,16 +108,54 @@ private synchronized org.ehcache.Cache<Object, Object> 
createCache(String name)
 
   private org.ehcache.CacheManager ensureCacheManager() throws 
MalformedURLException {
     if (manager == null) {
-      manager = CacheManagerBuilder.newCacheManager(getConfiguration());
-      manager.init();
+      XmlConfiguration xmlConfiguration = getConfiguration();
+      manager = CacheManagerBuilder.newCacheManager(xmlConfiguration);
+      try {
+        manager.init();
+      } catch (StateTransitionException e) {

Review Comment:
   This exception only results from a failure to acquire a lock on the 
persistence directory? If not, is there a way to determine that the exception 
was specifically for that reason?



##########
gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/ShiroMessages.java:
##########
@@ -44,4 +44,7 @@ public interface ShiroMessages {
 
   @Message( level = MessageLevel.WARN, text = "There was an error closing the 
CacheManager, reason: {0}" )
   void errorClosingManagedCacheManager(@StackTrace(level=MessageLevel.WARN) 
Exception e);
+
+  @Message( level = MessageLevel.WARN, text = "Unable to create Ehcache 
manager, changing persistence dir, reason: {0}" )

Review Comment:
   This message sounds like a failure. With the alternative persistence 
directory, the creation of the manager should succeed. Perhaps, something like 
"The default or configured ehcache persistence directory is unavailable; 
Choosing an alternative directory. reason: {0}" ?



##########
gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/shirorealm/KnoxCacheManager.java:
##########
@@ -99,16 +108,54 @@ private synchronized org.ehcache.Cache<Object, Object> 
createCache(String name)
 
   private org.ehcache.CacheManager ensureCacheManager() throws 
MalformedURLException {
     if (manager == null) {
-      manager = CacheManagerBuilder.newCacheManager(getConfiguration());
-      manager.init();
+      XmlConfiguration xmlConfiguration = getConfiguration();
+      manager = CacheManagerBuilder.newCacheManager(xmlConfiguration);
+      try {
+        manager.init();
+      } catch (StateTransitionException e) {
+        LOG.errorCreatingCacheManager(e.getMessage());
+        this.resolveLockConflict(xmlConfiguration);
+        if(manager.getStatus() != Status.UNINITIALIZED) {
+            manager.close();
+        }
+        manager = CacheManagerBuilder.newCacheManager(xmlConfiguration);
+        manager.init();
+      }
 
       cacheManagerImplicitlyCreated = true;
     }
 
     return manager;
   }
 
-  private URL getResource() {
+  /**
+   * Resolves lock conflicts by changing the persistence directory of the 
cache manager.
+   * This is necessary when multiple instances of the cache manager are 
created with the same configuration file,
+   * which can lead to lock conflicts.
+   *
+   * @param xmlConfiguration the XML configuration of the cache manager
+   */
+  private void resolveLockConflict(XmlConfiguration xmlConfiguration) {
+    Optional<ServiceCreationConfiguration<?>> serviceConfig = 
xmlConfiguration.getServiceCreationConfigurations().stream()
+            .filter(service -> service instanceof 
CacheManagerPersistenceConfiguration).findFirst();
+
+    if (serviceConfig.isPresent()) {
+      CacheManagerPersistenceConfiguration cachePersistenceConfig = 
(CacheManagerPersistenceConfiguration) serviceConfig.get();
+      String path = cachePersistenceConfig.getRootDirectory().getPath();
+      
xmlConfiguration.getServiceCreationConfigurations().remove(cachePersistenceConfig);
+      String newFolder = DEFAULT_FOLDER_NAME + 
UUID.randomUUID().toString().substring(0, 4);
+      String newRootDirectory = 
Paths.get(path).getParent().resolve(newFolder).toAbsolutePath().toString();
+      xmlConfiguration.getServiceCreationConfigurations()

Review Comment:
   Is there anything else possibly configured in a 
CacheManagerPersistenceConfiguration in addition to the location? Could we be 
omitting other user-defined config by replacing the original with this 
simplified version?





Issue Time Tracking
-------------------

    Worklog Id:     (was: 971969)
    Time Spent: 20m  (was: 10m)

> Improve Ehcache 3.x
> -------------------
>
>                 Key: KNOX-3156
>                 URL: https://issues.apache.org/jira/browse/KNOX-3156
>             Project: Apache Knox
>          Issue Type: Improvement
>    Affects Versions: 2.1.0
>            Reporter: Tamás Hanicz
>            Assignee: Tamás Hanicz
>            Priority: Major
>          Time Spent: 20m
>  Remaining Estimate: 0h
>
> * Add new ability to load Ehcache config file from a file that's not on the 
> classpath
>  * Knox should resolve lock dir exceptions on its own in case two topologies 
> use the same configuration file.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to