You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:26:31 UTC

[sling-org-apache-sling-discovery-commons] 28/38: SLING-5173 : re-added complete consistency-history after introducing splitting them and using the ConsistencyServiceChain. Probably should be refactored into something slightly nicer though

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.discovery.commons-1.0.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-discovery-commons.git

commit 3edba1a24d6bce459cfb50cd9743e2d2de985c11
Author: Stefan Egli <st...@apache.org>
AuthorDate: Thu Oct 22 16:05:09 2015 +0000

    SLING-5173 : re-added complete consistency-history after introducing splitting them and using the ConsistencyServiceChain. Probably should be refactored into something slightly nicer though
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/extensions/discovery/commons@1710047 13f79535-47bb-0310-9956-ffa450edef68
---
 .../base/AbstractServiceWithBackgroundCheck.java   | 63 ----------------
 .../providers/spi/base/ConsistencyHistory.java     | 83 ++++++++++++++++++++++
 .../spi/base/OakBacklogConsistencyService.java     | 23 ++++--
 .../spi/base/SyncTokenConsistencyService.java      | 16 ++++-
 4 files changed, 115 insertions(+), 70 deletions(-)

diff --git a/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/AbstractServiceWithBackgroundCheck.java b/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/AbstractServiceWithBackgroundCheck.java
index 3e8b93b..29088b1 100644
--- a/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/AbstractServiceWithBackgroundCheck.java
+++ b/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/AbstractServiceWithBackgroundCheck.java
@@ -18,17 +18,6 @@
  */
 package org.apache.sling.discovery.commons.providers.spi.base;
 
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Calendar;
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.List;
-
-import org.apache.sling.api.resource.LoginException;
-import org.apache.sling.api.resource.ResourceResolver;
-import org.apache.sling.api.resource.ResourceResolverFactory;
-import org.apache.sling.discovery.commons.providers.BaseTopologyView;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -39,21 +28,10 @@ import org.slf4j.LoggerFactory;
  */
 public abstract class AbstractServiceWithBackgroundCheck {
 
-    class HistoryEntry {
-        BaseTopologyView view;
-        String msg;
-        String fullLine;
-    }
-    
-    /** the date format used in the truncated log of topology events **/
-    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
-
     protected final Logger logger = LoggerFactory.getLogger(getClass());
 
     protected String slingId;
 
-    protected List<HistoryEntry> history = new LinkedList<HistoryEntry>();
-    
     /**
      * The BackgroundCheckRunnable implements the details of
      * calling BackgroundCheck.check and looping until it 
@@ -239,45 +217,4 @@ public abstract class AbstractServiceWithBackgroundCheck {
             backgroundOp.triggerCheck();
         }
     }
-    
-    public List<String> getSyncHistory() {
-        List<HistoryEntry> snapshot;
-        synchronized(history) {
-            snapshot = Collections.unmodifiableList(history);
-        }
-        List<String> result = new ArrayList<String>(snapshot.size());
-        for (HistoryEntry historyEntry : snapshot) {
-            result.add(historyEntry.fullLine);
-        }
-        return result;
-    }
-
-    protected void addHistoryEntry(BaseTopologyView view, String msg) {
-        synchronized(history) {
-            for(int i = history.size() - 1; i>=0; i--) {
-                HistoryEntry entry = history.get(i);
-                if (!entry.view.equals(view)) {
-                    // don't filter if the view starts differing,
-                    // only filter for the last few entries where
-                    // the view is equal
-                    break;
-                }
-                if (entry.msg.equals(msg)) {
-                    // if the view is equal and the msg matches
-                    // then this is a duplicate entry, so ignore
-                    return;
-                }
-            }
-            String fullLine = sdf.format(Calendar.getInstance().getTime()) + ": " + msg;
-            HistoryEntry newEntry = new HistoryEntry();
-            newEntry.view = view;
-            newEntry.fullLine = fullLine;
-            newEntry.msg = msg;
-            history.add(newEntry);
-            while (history.size() > 12) {
-                history.remove(0);
-            }
-        }
-    }
-
 }
diff --git a/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/ConsistencyHistory.java b/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/ConsistencyHistory.java
new file mode 100644
index 0000000..a6e793b
--- /dev/null
+++ b/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/ConsistencyHistory.java
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.discovery.commons.providers.spi.base;
+
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.sling.discovery.commons.providers.BaseTopologyView;
+
+public class ConsistencyHistory {
+
+    class HistoryEntry {
+        BaseTopologyView view;
+        String msg;
+        String fullLine;
+    }
+    
+    /** the date format used in the truncated log of topology events **/
+    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
+
+    protected List<HistoryEntry> history = new LinkedList<HistoryEntry>();
+    
+    public List<String> getSyncHistory() {
+        List<HistoryEntry> snapshot;
+        synchronized(history) {
+            snapshot = Collections.unmodifiableList(history);
+        }
+        List<String> result = new ArrayList<String>(snapshot.size());
+        for (HistoryEntry historyEntry : snapshot) {
+            result.add(historyEntry.fullLine);
+        }
+        return result;
+    }
+
+    protected void addHistoryEntry(BaseTopologyView view, String msg) {
+        synchronized(history) {
+            for(int i = history.size() - 1; i>=0; i--) {
+                HistoryEntry entry = history.get(i);
+                if (!entry.view.equals(view)) {
+                    // don't filter if the view starts differing,
+                    // only filter for the last few entries where
+                    // the view is equal
+                    break;
+                }
+                if (entry.msg.equals(msg)) {
+                    // if the view is equal and the msg matches
+                    // then this is a duplicate entry, so ignore
+                    return;
+                }
+            }
+            String fullLine = sdf.format(Calendar.getInstance().getTime()) + ": " + msg;
+            HistoryEntry newEntry = new HistoryEntry();
+            newEntry.view = view;
+            newEntry.fullLine = fullLine;
+            newEntry.msg = msg;
+            history.add(newEntry);
+            while (history.size() > 12) {
+                history.remove(0);
+            }
+        }
+    }
+    
+}
diff --git a/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/OakBacklogConsistencyService.java b/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/OakBacklogConsistencyService.java
index 48eb477..e972b05 100644
--- a/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/OakBacklogConsistencyService.java
+++ b/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/OakBacklogConsistencyService.java
@@ -19,6 +19,7 @@
 package org.apache.sling.discovery.commons.providers.spi.base;
 
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 
 import org.apache.felix.scr.annotations.Activate;
@@ -60,6 +61,8 @@ public class OakBacklogConsistencyService extends AbstractServiceWithBackgroundC
 
     @Reference
     protected SlingSettingsService settingsService;
+
+    private ConsistencyHistory consistencyHistory = new ConsistencyHistory();
     
     public static OakBacklogConsistencyService testConstructorAndActivate(
             final DiscoveryLiteConfig commonsConfig,
@@ -111,6 +114,14 @@ public class OakBacklogConsistencyService extends AbstractServiceWithBackgroundC
         logger.info("activate: activated with slingId="+slingId);
     }
     
+    public void setConsistencyHistory(ConsistencyHistory consistencyHistory) {
+        this.consistencyHistory = consistencyHistory;
+    }
+    
+    public ConsistencyHistory getConsistencyHistory() {
+        return consistencyHistory;
+    }
+    
     /** Get or create a ResourceResolver **/
     protected ResourceResolver getResourceResolver() throws LoginException {
         return resourceResolverFactory.getAdministrativeResourceResolver(null);
@@ -141,24 +152,24 @@ public class OakBacklogConsistencyService extends AbstractServiceWithBackgroundC
                 try {
                     if (!idMapService.isInitialized()) {
                         logger.info("waitWhileBacklog: could not initialize...");
-                        addHistoryEntry(view, "could not initialize idMapService");
+                        consistencyHistory.addHistoryEntry(view, "could not initialize idMapService");
                         return false;
                     }
                 } catch (Exception e) {
                     logger.error("waitWhileBacklog: could not initialized due to "+e, e);
-                    addHistoryEntry(view, "got Exception while initializing idMapService ("+e+")");
+                    consistencyHistory.addHistoryEntry(view, "got Exception while initializing idMapService ("+e+")");
                     return false;
                 }
                 BacklogStatus backlogStatus = getBacklogStatus(view);
                 if (backlogStatus == BacklogStatus.NO_BACKLOG) {
                     logger.info("waitWhileBacklog: no backlog (anymore), done.");
-                    addHistoryEntry(view, "no backlog (anymore)");
+                    consistencyHistory.addHistoryEntry(view, "no backlog (anymore)");
                     return true;
                 } else {
                     logger.info("waitWhileBacklog: backlogStatus still "+backlogStatus);
                     // clear the cache to make sure to get the latest version in case something changed
                     idMapService.clearCache();
-                    addHistoryEntry(view, "backlog status "+backlogStatus);
+                    consistencyHistory.addHistoryEntry(view, "backlog status "+backlogStatus);
                     return false;
                 }
             }
@@ -242,4 +253,8 @@ public class OakBacklogConsistencyService extends AbstractServiceWithBackgroundC
         return settingsService;
     }
 
+    public List<String> getSyncHistory() {
+        return consistencyHistory.getSyncHistory();
+    }
+
 }
diff --git a/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/SyncTokenConsistencyService.java b/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/SyncTokenConsistencyService.java
index 3750946..b469f4a 100644
--- a/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/SyncTokenConsistencyService.java
+++ b/src/main/java/org/apache/sling/discovery/commons/providers/spi/base/SyncTokenConsistencyService.java
@@ -62,6 +62,8 @@ public class SyncTokenConsistencyService extends AbstractServiceWithBackgroundCh
     @Reference
     protected SlingSettingsService settingsService;
 
+    protected ConsistencyHistory consistencyHistory = new ConsistencyHistory();
+
     public static SyncTokenConsistencyService testConstructorAndActivate(
             DiscoveryLiteConfig commonsConfig,
             ResourceResolverFactory resourceResolverFactory,
@@ -97,6 +99,14 @@ public class SyncTokenConsistencyService extends AbstractServiceWithBackgroundCh
         logger.info("activate: activated with slingId="+slingId);
     }
     
+    public void setConsistencyHistory(ConsistencyHistory consistencyHistory) {
+        this.consistencyHistory = consistencyHistory;
+    }
+    
+    public ConsistencyHistory getConsistencyHistory() {
+        return consistencyHistory;
+    }
+    
     /** Get or create a ResourceResolver **/
     protected ResourceResolver getResourceResolver() throws LoginException {
         return resourceResolverFactory.getAdministrativeResourceResolver(null);
@@ -130,7 +140,7 @@ public class SyncTokenConsistencyService extends AbstractServiceWithBackgroundCh
                     // that they will have to wait until the timeout hits
                     
                     // so to try to avoid this, retry storing my sync token later:
-                    addHistoryEntry(view, "storing my syncToken ("+localClusterSyncTokenId+")");
+                    consistencyHistory.addHistoryEntry(view, "storing my syncToken ("+localClusterSyncTokenId+")");
                     return false;
                 }
                 
@@ -222,10 +232,10 @@ public class SyncTokenConsistencyService extends AbstractServiceWithBackgroundCh
             }
             if (!success) {
                 logger.info("seenAllSyncTokens: not yet seen all expected syncTokens (see above for details)");
-                addHistoryEntry(view, historyEntry.toString());
+                consistencyHistory.addHistoryEntry(view, historyEntry.toString());
                 return false;
             } else {
-                addHistoryEntry(view, "seen all syncTokens");
+                consistencyHistory.addHistoryEntry(view, "seen all syncTokens");
             }
             
             resourceResolver.commit();

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.