You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by st...@apache.org on 2015/04/16 14:35:48 UTC

svn commit: r1674062 - in /sling/trunk/bundles/extensions/discovery/impl/src/main: java/org/apache/sling/discovery/impl/Config.java java/org/apache/sling/discovery/impl/common/heartbeat/HeartbeatHandler.java resources/OSGI-INF/metatype/metatype.properties

Author: stefanegli
Date: Thu Apr 16 12:35:47 2015
New Revision: 1674062

URL: http://svn.apache.org/r1674062
Log:
SLING-4516: Allow to configure the leader to follow the crx master : new config property introduced for this: invertRepositoryDescriptor. If that is set to true (default is false) then this achieves what SLING-4516 likes to have

Modified:
    sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/Config.java
    sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/heartbeat/HeartbeatHandler.java
    sling/trunk/bundles/extensions/discovery/impl/src/main/resources/OSGI-INF/metatype/metatype.properties

Modified: sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/Config.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/Config.java?rev=1674062&r1=1674061&r2=1674062&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/Config.java (original)
+++ sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/Config.java Thu Apr 16 12:35:47 2015
@@ -91,6 +91,13 @@ public class Config {
     @Property
     public static final String LEADER_ELECTION_REPOSITORY_DESCRIPTOR_NAME_KEY = "leaderElectionRepositoryDescriptor";
     
+    /**
+     * Whether or not (default false) the leaderElectionRepositoryDescriptor should be inverted (if that one
+     * is configured at all).
+     */
+    @Property(boolValue=false)
+    public static final String INVERT_REPOSITORY_DESCRIPTOR_NAME_KEY = "invertRepositoryDescriptor";
+    
     /** URLs where to join a topology, eg http://localhost:4502/libs/sling/topology/connector */
     @Property(cardinality=1024)
     public static final String TOPOLOGY_CONNECTOR_URLS_KEY = "topologyConnectorUrls";
@@ -171,6 +178,8 @@ public class Config {
 
     private String leaderElectionRepositoryDescriptor ;
 
+    private boolean invertRepositoryDescriptor = false; /* default: false */
+    
     /** True when auto-stop of a local-loop is enabled. Default is false. **/
     private boolean autoStopLocalLoopEnabled;
     
@@ -301,6 +310,12 @@ public class Config {
                 null);
         logger.debug("configure: leaderElectionRepositoryDescriptor='{}'",
                 this.leaderElectionRepositoryDescriptor);
+        
+        this.invertRepositoryDescriptor = PropertiesUtil.toBoolean(
+                properties.get(INVERT_REPOSITORY_DESCRIPTOR_NAME_KEY),
+                false /* default: false*/);
+        logger.debug("configure: invertRepositoryDescriptor='{}'",
+                this.invertRepositoryDescriptor);
 
         delayInitEventUntilVoted = PropertiesUtil.toBoolean(properties.get(DELAY_INIT_EVENT_UNTIL_VOTED), true);
         autoStopLocalLoopEnabled = PropertiesUtil.toBoolean(properties.get(AUTO_STOP_LOCAL_LOOP_ENABLED), false);
@@ -429,6 +444,18 @@ public class Config {
     public String getLeaderElectionRepositoryDescriptor() {
         return leaderElectionRepositoryDescriptor;
     }
+    
+    /**
+     * Returns true when the value of the repository descriptor identified
+     * via the property 'leaderElectionRepositoryDescriptor' should be 
+     * inverted - only applies when 'leaderElectionRepositoryDescriptor' 
+     * is configured of course.
+     * @return true when property resulting from 'leaderElectionRepositoryDescriptor'
+     * should be inverted, false if it should remain unchanged.
+     */
+    public boolean shouldInvertRepositoryDescriptor() {
+        return invertRepositoryDescriptor;
+    }
 
     /**
      * @return true if hmac is enabled.

Modified: sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/heartbeat/HeartbeatHandler.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/heartbeat/HeartbeatHandler.java?rev=1674062&r1=1674061&r2=1674062&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/heartbeat/HeartbeatHandler.java (original)
+++ sling/trunk/bundles/extensions/discovery/impl/src/main/java/org/apache/sling/discovery/impl/common/heartbeat/HeartbeatHandler.java Thu Apr 16 12:35:47 2015
@@ -366,7 +366,8 @@ public class HeartbeatHandler implements
                 String currentTimeMillisStr = String.format("%0"
                         + maxLongLength + "d", System.currentTimeMillis());
 
-                String prefix = "0";
+                final boolean shouldInvertRepositoryDescriptor = config.shouldInvertRepositoryDescriptor();
+                String prefix = (shouldInvertRepositoryDescriptor ? "1" : "0");
 
                 String leaderElectionRepositoryDescriptor = config.getLeaderElectionRepositoryDescriptor();
                 if (leaderElectionRepositoryDescriptor!=null && leaderElectionRepositoryDescriptor.length()!=0) {
@@ -377,13 +378,22 @@ public class HeartbeatHandler implements
                     if ( session != null ) {
                         String value = session.getRepository()
                                 .getDescriptor(leaderElectionRepositoryDescriptor);
-                        if (value != null && value.equalsIgnoreCase("true")) {
-                            prefix = "1";
+                        if (value != null) {
+                            if (value.equalsIgnoreCase("true")) {
+                                if (!shouldInvertRepositoryDescriptor) {
+                                    prefix = "1";
+                                } else {
+                                    prefix = "0";
+                                }
+                            }
                         }
                     }
                 }
-                resourceMap.put("leaderElectionId", prefix + "_"
-                        + currentTimeMillisStr + "_" + slingId);
+                final String newLeaderElectionId = prefix + "_"
+                        + currentTimeMillisStr + "_" + slingId;
+                resourceMap.put("leaderElectionId", newLeaderElectionId);
+                resourceMap.put("leaderElectionIdCreatedAt", new Date());
+                logger.debug("issueClusterLocalHeartbeat: set leaderElectionId to "+newLeaderElectionId);
                 resetLeaderElectionId = false;
             }
             resourceResolver.commit();

Modified: sling/trunk/bundles/extensions/discovery/impl/src/main/resources/OSGI-INF/metatype/metatype.properties
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/discovery/impl/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=1674062&r1=1674061&r2=1674062&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/discovery/impl/src/main/resources/OSGI-INF/metatype/metatype.properties (original)
+++ sling/trunk/bundles/extensions/discovery/impl/src/main/resources/OSGI-INF/metatype/metatype.properties Thu Apr 16 12:35:47 2015
@@ -57,6 +57,11 @@ leaderElectionRepositoryDescriptor.name
 leaderElectionRepositoryDescriptor.description = Name of the repository descriptor to be taken \
  into account for leader election: those instances have preference to become leader which have \
  the corresponding descriptor value of 'false'.
+ 
+invertRepositoryDescriptor.name = Invert Repository Descriptor
+invertRepositoryDescriptor.description = Enabling this property allows to invert the \
+ repository descriptor value that is obtained via the configured 'leaderElectionRepositoryDescriptor' \
+ (thus only applies if that is configured). Default is 'false' (don't invert).
 
 autoStopLocalLoopEnabled.name = Auto-Stop Local-Loops
 autoStopLocalLoopEnabled.description = If true, and the discovery.impl detects a local-looping \