Author: stefanegli
Date: Wed Oct 21 11:02:22 2015
New Revision: 1709793

URL: http://svn.apache.org/viewvc?rev=1709793&view=rev
Log:
SLING-4603 related : some fixes in class structure / syncToken handling

Modified:
    
sling/trunk/bundles/extensions/discovery/commons/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/BaseSyncTokenConsistencyService.java
    
sling/trunk/bundles/extensions/discovery/commons/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/OakSyncTokenConsistencyService.java
    
sling/trunk/bundles/extensions/discovery/commons/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/SyncTokenOnlyConsistencyService.java

Modified: 
sling/trunk/bundles/extensions/discovery/commons/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/BaseSyncTokenConsistencyService.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/commons/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/BaseSyncTokenConsistencyService.java?rev=1709793&r1=1709792&r2=1709793&view=diff
==============================================================================
--- 
sling/trunk/bundles/extensions/discovery/commons/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/BaseSyncTokenConsistencyService.java
 (original)
+++ 
sling/trunk/bundles/extensions/discovery/commons/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/BaseSyncTokenConsistencyService.java
 Wed Oct 21 11:02:22 2015
@@ -18,7 +18,6 @@
  */
 package org.apache.sling.discovery.commons.providers.spi.base;
 
-import org.apache.felix.scr.annotations.Activate;
 import org.apache.sling.api.resource.LoginException;
 import org.apache.sling.api.resource.ModifiableValueMap;
 import org.apache.sling.api.resource.PersistenceException;
@@ -31,8 +30,6 @@ import org.apache.sling.discovery.common
 import org.apache.sling.discovery.commons.providers.spi.ConsistencyService;
 import org.apache.sling.discovery.commons.providers.util.ResourceHelper;
 import org.apache.sling.settings.SlingSettingsService;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * Implements the 'sync-token' part of the ConsistencyService,
@@ -41,25 +38,14 @@ import org.slf4j.LoggerFactory;
  */
 public abstract class BaseSyncTokenConsistencyService extends 
AbstractServiceWithBackgroundCheck implements ConsistencyService {
 
-    protected final Logger logger = LoggerFactory.getLogger(getClass());
-
     protected String slingId;
 
-    protected long syncTokenTimeoutMillis;
-    
-    protected long syncTokenIntervalMillis;
-
     protected abstract DiscoveryLiteConfig getCommonsConfig();
 
     protected abstract ResourceResolverFactory getResourceResolverFactory();
 
     protected abstract SlingSettingsService getSettingsService();
     
-    @Activate
-    protected void activate() {
-        this.slingId = getSettingsService().getSlingId();
-    }
-    
     /** Get or create a ResourceResolver **/
     protected ResourceResolver getResourceResolver() throws LoginException {
         return 
getResourceResolverFactory().getAdministrativeResourceResolver(null);
@@ -76,42 +62,61 @@ public abstract class BaseSyncTokenConsi
     }
 
     protected void syncToken(final BaseTopologyView view, final Runnable 
callback) {
-        // 1) first storing my syncToken
-        try {
-            storeMySyncToken(view.getLocalClusterSyncTokenId());
-        } catch (LoginException e) {
-            logger.error("syncToken: will run into timeout: could not login 
for storing my syncToken: "+e, e);
-        } catch (PersistenceException e) {
-            logger.error("syncToken: will run into timeout: got 
PersistenceException while storing my syncToken: "+e, e);
-        }
-        // if anything goes wrong above, then this will mean for the others
-        // that they will have to wait until the timeout hits
-        // which means we should do the same..
-        // hence no further action possible on error above
         
-        // 2) then check if all others have done the same already
         startBackgroundCheck("SyncTokenConsistencyService", new 
BackgroundCheck() {
             
             @Override
             public boolean check() {
+                // 1) first storing my syncToken
+                if (!storeMySyncToken(view.getLocalClusterSyncTokenId())) {
+                    // if anything goes wrong above, then this will mean for 
the others
+                    // that they will have to wait until the timeout hits
+                    
+                    // so to try to avoid this, retry storing my sync token 
later:
+                    return false;
+                }
+                
+                
+                // 2) then check if all others have done the same already
                 return seenAllSyncTokens(view);
             }
-        }, callback, syncTokenTimeoutMillis, syncTokenIntervalMillis);
+        }, callback, getCommonsConfig().getBgTimeoutMillis(), 
getCommonsConfig().getBgIntervalMillis());
     }
 
-    private void storeMySyncToken(String syncTokenId) throws LoginException, 
PersistenceException {
+    private boolean storeMySyncToken(String syncTokenId) {
         logger.trace("storeMySyncToken: start");
+        if (slingId == null) {
+            logger.info("storeMySyncToken: not yet activated (slingId is 
null)");
+            return false;
+        }
         ResourceResolver resourceResolver = null;
         try{
             resourceResolver = getResourceResolver();
             final Resource resource = 
ResourceHelper.getOrCreateResource(resourceResolver, getSyncTokenPath());
             ModifiableValueMap syncTokens = 
resource.adaptTo(ModifiableValueMap.class);
-            Object currentValue = syncTokens.get(slingId);
-            if (currentValue == null || !syncTokenId.equals(currentValue)) {
+            boolean updateToken = false;
+            if (!syncTokens.containsKey(slingId)) {
+                updateToken = true;
+            } else {
+                Object existingToken = syncTokens.get(slingId);
+                if (existingToken==null || !existingToken.equals(syncTokenId)) 
{
+                    updateToken = true;
+                }
+            }
+            if (updateToken) {
                 syncTokens.put(slingId, syncTokenId);
+                resourceResolver.commit();
+                logger.info("storeMySyncToken: stored syncToken of 
slingId="+slingId+" as="+syncTokenId);
+            } else {
+                logger.info("storeMySyncToken: syncToken was left unchanged 
for slingId="+slingId+" at="+syncTokenId);
             }
-            resourceResolver.commit();
-            logger.info("syncToken: stored syncToken of slingId="+slingId+" 
as="+syncTokenId);
+            return true;
+        } catch (LoginException e) {
+            logger.error("storeMySyncToken: could not login for storing my 
syncToken: "+e, e);
+            return false;
+        } catch (PersistenceException e) {
+            logger.error("storeMySyncToken: got PersistenceException while 
storing my syncToken: "+e, e);
+            return false;
         } finally {
             logger.trace("storeMySyncToken: end");
             if (resourceResolver!=null) {
@@ -133,18 +138,22 @@ public abstract class BaseSyncTokenConsi
             ValueMap syncTokens = resource.adaptTo(ValueMap.class);
             String syncToken = view.getLocalClusterSyncTokenId();
             
+            boolean success = true;
             for (InstanceDescription instance : 
view.getLocalInstance().getClusterView().getInstances()) {
                 Object currentValue = syncTokens.get(instance.getSlingId());
                 if (currentValue == null) {
-                    logger.info("seenAllSyncTokens: no syncToken of 
"+instance);
-                    return false;
-                }
-                if (!syncToken.equals(currentValue)) {
-                    logger.info("seenAllSyncTokens: old syncToken of " + 
instance
+                    logger.info("seenAllSyncTokens: no syncToken of 
"+instance.getSlingId());
+                    success = false;
+                } else if (!syncToken.equals(currentValue)) {
+                    logger.info("seenAllSyncTokens: old syncToken of " + 
instance.getSlingId()
                             + " : expected=" + syncToken + " 
got="+currentValue);
-                    return false;
+                    success = false;
                 }
             }
+            if (!success) {
+                logger.info("seenAllSyncTokens: not yet seen all expected 
syncTokens (see above for details)");
+                return false;
+            }
             
             resourceResolver.commit();
             logger.info("seenAllSyncTokens: seen all syncTokens!");

Modified: 
sling/trunk/bundles/extensions/discovery/commons/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/OakSyncTokenConsistencyService.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/commons/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/OakSyncTokenConsistencyService.java?rev=1709793&r1=1709792&r2=1709793&view=diff
==============================================================================
--- 
sling/trunk/bundles/extensions/discovery/commons/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/OakSyncTokenConsistencyService.java
 (original)
+++ 
sling/trunk/bundles/extensions/discovery/commons/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/OakSyncTokenConsistencyService.java
 Wed Oct 21 11:02:22 2015
@@ -21,6 +21,7 @@ package org.apache.sling.discovery.commo
 import java.util.HashSet;
 import java.util.Set;
 
+import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.Service;
@@ -40,7 +41,7 @@ import org.apache.sling.settings.SlingSe
  * the Oak discovery-lite descriptor.
  */
 @Component(immediate = false)
-@Service(value = { ConsistencyService.class })
+@Service(value = { ConsistencyService.class, 
OakSyncTokenConsistencyService.class })
 public class OakSyncTokenConsistencyService extends 
BaseSyncTokenConsistencyService {
 
     static enum BacklogStatus {
@@ -49,10 +50,6 @@ public class OakSyncTokenConsistencyServ
         NO_BACKLOG /* when oak's discovery lite descriptor declared we're 
backlog-free now */
     }
     
-    private long backlogWaitTimeoutMillis;
-
-    private long backlogWaitIntervalMillis;
-
     @Reference
     private IdMapService idMapService;
     
@@ -104,28 +101,30 @@ public class OakSyncTokenConsistencyServ
         }
         service.commonsConfig = commonsConfig;
         service.resourceResolverFactory = resourceResolverFactory;
-        service.syncTokenTimeoutMillis = commonsConfig.getBgTimeoutMillis();
-        service.syncTokenIntervalMillis = commonsConfig.getBgIntervalMillis();
         service.idMapService = idMapService;
         service.settingsService = settingsService;
-        service.backlogWaitIntervalMillis = 
commonsConfig.getBgIntervalMillis();
-        service.backlogWaitTimeoutMillis = commonsConfig.getBgTimeoutMillis();
         return service;
     }
     
+    @Activate
+    protected void activate() {
+        this.slingId = getSettingsService().getSlingId();
+        logger.info("activate: activated with slingId="+slingId);
+    }
+    
     @Override
     public void sync(final BaseTopologyView view, final Runnable callback) {
         // cancel the previous backgroundCheck if it's still running
         cancelPreviousBackgroundCheck();
 
         // first do the wait-for-backlog part
-        logger.info("sync: doing wait-for-backlog part for view="+view);
+        logger.info("sync: doing wait-for-backlog part for 
view="+view.toShortString());
         waitWhileBacklog(view, new Runnable() {
 
             @Override
             public void run() {
                 // when done, then do the sync-token part
-                logger.info("sync: doing sync-token part for view="+view);
+                logger.info("sync: doing sync-token part for 
view="+view.toShortString());
                 syncToken(view, callback);
             }
             
@@ -157,7 +156,7 @@ public class OakSyncTokenConsistencyServ
                     return false;
                 }
             }
-        }, runnable, backlogWaitTimeoutMillis, backlogWaitIntervalMillis);
+        }, runnable, getCommonsConfig().getBgTimeoutMillis(), 
getCommonsConfig().getBgIntervalMillis());
     }
     
     private BacklogStatus getBacklogStatus(BaseTopologyView view) {

Modified: 
sling/trunk/bundles/extensions/discovery/commons/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/SyncTokenOnlyConsistencyService.java
URL: 
http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/commons/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/SyncTokenOnlyConsistencyService.java?rev=1709793&r1=1709792&r2=1709793&view=diff
==============================================================================
--- 
sling/trunk/bundles/extensions/discovery/commons/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/SyncTokenOnlyConsistencyService.java
 (original)
+++ 
sling/trunk/bundles/extensions/discovery/commons/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/SyncTokenOnlyConsistencyService.java
 Wed Oct 21 11:02:22 2015
@@ -18,6 +18,7 @@
  */
 package org.apache.sling.discovery.commons.providers.spi.base;
 
+import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.Service;
@@ -47,22 +48,16 @@ public class SyncTokenOnlyConsistencySer
     @Reference
     protected SlingSettingsService settingsService;
 
-    protected String slingId;
-
-    protected long syncTokenTimeoutMillis;
-    
-    protected long syncTokenIntervalMillis;
-
     public static BaseSyncTokenConsistencyService testConstructorAndActivate(
             DiscoveryLiteConfig commonsConfig,
             ResourceResolverFactory resourceResolverFactory,
             SlingSettingsService settingsService) {
-        BaseSyncTokenConsistencyService service = 
testConstructor(commonsConfig, resourceResolverFactory, settingsService);
+        SyncTokenOnlyConsistencyService service = 
testConstructor(commonsConfig, resourceResolverFactory, settingsService);
         service.activate();
         return service;
     }
     
-    public static BaseSyncTokenConsistencyService testConstructor(
+    public static SyncTokenOnlyConsistencyService testConstructor(
             DiscoveryLiteConfig commonsConfig,
             ResourceResolverFactory resourceResolverFactory,
             SlingSettingsService settingsService) {
@@ -78,12 +73,16 @@ public class SyncTokenOnlyConsistencySer
         }
         service.commonsConfig = commonsConfig;
         service.resourceResolverFactory = resourceResolverFactory;
-        service.syncTokenTimeoutMillis = commonsConfig.getBgTimeoutMillis();
-        service.syncTokenIntervalMillis = commonsConfig.getBgIntervalMillis();
         service.settingsService = settingsService;
         return service;
     }
 
+    @Activate
+    protected void activate() {
+        this.slingId = getSettingsService().getSlingId();
+        logger.info("activate: activated with slingId="+slingId);
+    }
+    
     @Override
     protected DiscoveryLiteConfig getCommonsConfig() {
         return commonsConfig;


Reply via email to