You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by an...@apache.org on 2013/04/15 11:18:09 UTC

svn commit: r1467911 - in /karaf/cellar/trunk: cloud/src/main/java/org/apache/karaf/cellar/cloud/ core/src/main/java/org/apache/karaf/cellar/core/discovery/ core/src/main/java/org/apache/karaf/cellar/core/event/ hazelcast/src/main/java/org/apache/karaf...

Author: anierbeck
Date: Mon Apr 15 09:18:08 2013
New Revision: 1467911

URL: http://svn.apache.org/r1467911
Log:
ported changes from 2.2.x for 
[KARAF-2262] - cellar-cloud: The IP is not enough, sometimes the IP is hidden but a valid dns name is available
and 
[KARAF-2263] - Cellar-Cloud: discover member removes member information which isn't re-generated

Added:
    karaf/cellar/trunk/cloud/src/main/java/org/apache/karaf/cellar/cloud/ServiceContainer.java   (with props)
Modified:
    karaf/cellar/trunk/cloud/src/main/java/org/apache/karaf/cellar/cloud/BlobStoreDiscoveryService.java
    karaf/cellar/trunk/cloud/src/main/java/org/apache/karaf/cellar/cloud/BlobStoreDiscoveryServiceFactory.java
    karaf/cellar/trunk/core/src/main/java/org/apache/karaf/cellar/core/discovery/DiscoveryTask.java
    karaf/cellar/trunk/core/src/main/java/org/apache/karaf/cellar/core/event/EventDispatchTask.java
    karaf/cellar/trunk/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/HazelcastGroupManager.java

Modified: karaf/cellar/trunk/cloud/src/main/java/org/apache/karaf/cellar/cloud/BlobStoreDiscoveryService.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/cloud/src/main/java/org/apache/karaf/cellar/cloud/BlobStoreDiscoveryService.java?rev=1467911&r1=1467910&r2=1467911&view=diff
==============================================================================
--- karaf/cellar/trunk/cloud/src/main/java/org/apache/karaf/cellar/cloud/BlobStoreDiscoveryService.java (original)
+++ karaf/cellar/trunk/cloud/src/main/java/org/apache/karaf/cellar/cloud/BlobStoreDiscoveryService.java Mon Apr 15 09:18:08 2013
@@ -19,7 +19,9 @@ import org.jclouds.blobstore.BlobStore;
 import org.jclouds.blobstore.BlobStoreContext;
 import org.jclouds.blobstore.BlobStoreContextFactory;
 import org.jclouds.blobstore.domain.Blob;
+import org.jclouds.blobstore.domain.PageSet;
 import org.jclouds.blobstore.domain.StorageMetadata;
+import org.jclouds.blobstore.domain.StorageType;
 import org.jclouds.blobstore.options.ListContainerOptions;
 import org.joda.time.DateTime;
 import org.slf4j.Logger;
@@ -92,19 +94,40 @@ public class BlobStoreDiscoveryService i
         Set<String> members = new HashSet<String>();
         ListContainerOptions opt = new ListContainerOptions();
 
-        for (StorageMetadata md : blobStore.list(container, opt)) {
+        PageSet<? extends StorageMetadata> pageSet = blobStore.list(container, opt);
+        LOGGER.debug("CELLAR CLOUD: storage contains a pageset of size {}", pageSet.size());
+		for (StorageMetadata md : pageSet) {
+			if (md.getType() != StorageType.BLOB) {
+				//skip everything that isn't of type BLOB ...
+				continue;
+			}
             String ip = md.getName();
             Object obj = readBlob(container, ip);
             //Check if ip hasn't been updated recently.
             if (obj instanceof DateTime) {
+            	LOGGER.debug("CELLAR CLOUD: retrieved a DateTime from blog store");
                 DateTime registeredTime = (DateTime) obj;
                 if (registeredTime != null && registeredTime.plusSeconds(validityPeriod).isAfterNow()) {
+                	LOGGER.debug("CELLAR CLOUD: adding member {}", ip);
                     members.add(ip);
                 } else {
+                	LOGGER.debug("CELLAR CLOUD: remove container {}", ip);
+                    blobStore.removeBlob(container, ip);
+                }
+            } else if (obj instanceof ServiceContainer) {
+            	LOGGER.debug("CELLAR CLOUD: retrieved a ServiceContainer from blog store");
+            	ServiceContainer serviceContainer = (ServiceContainer) obj;
+            	DateTime registeredTime = serviceContainer.getRegisteredTime();
+            	if (registeredTime != null && registeredTime.plusSeconds(validityPeriod).isAfterNow()) {
+            		LOGGER.debug("CELLAR CLOUD: adding member {} for IP {}", serviceContainer.getHostName(), ip);
+                    members.add(serviceContainer.getHostIp());
+                } else {
+                	LOGGER.debug("CELLAR CLOUD: remove container {}", ip);
                     blobStore.removeBlob(container, ip);
                 }
             }
         }
+        LOGGER.debug("CELLAR CLOUD: returning members {}", members);
         return members;
     }
 
@@ -113,7 +136,7 @@ public class BlobStoreDiscoveryService i
      */
     public void signIn() {
         DateTime now = new DateTime();
-        createBlob(container, ipAddress, now);
+        createBlob(container, ipAddress, new ServiceContainer(getHostAdress(), getIpAddress(), now));
     }
 
     /**
@@ -121,7 +144,7 @@ public class BlobStoreDiscoveryService i
      */
     public void refresh() {
         DateTime now = new DateTime();
-        createBlob(container, ipAddress, now);
+        createBlob(container, ipAddress, new ServiceContainer(getHostAdress(), getIpAddress(), now));
     }
 
     /**
@@ -227,6 +250,15 @@ public class BlobStoreDiscoveryService i
             return null;
         }
     }
+    
+    protected String getHostAdress() {
+    	try {
+			return InetAddress.getLocalHost().getHostName();
+		} catch (UnknownHostException ex) {
+			LOGGER.error("CELLAR CLOUD: unable to determine host address for current node", ex);
+            return null;
+		}
+    }
 
     public String getProvider() {
         return provider;

Modified: karaf/cellar/trunk/cloud/src/main/java/org/apache/karaf/cellar/cloud/BlobStoreDiscoveryServiceFactory.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/cloud/src/main/java/org/apache/karaf/cellar/cloud/BlobStoreDiscoveryServiceFactory.java?rev=1467911&r1=1467910&r2=1467911&view=diff
==============================================================================
--- karaf/cellar/trunk/cloud/src/main/java/org/apache/karaf/cellar/cloud/BlobStoreDiscoveryServiceFactory.java (original)
+++ karaf/cellar/trunk/cloud/src/main/java/org/apache/karaf/cellar/cloud/BlobStoreDiscoveryServiceFactory.java Mon Apr 15 09:18:08 2013
@@ -68,6 +68,14 @@ public class BlobStoreDiscoveryServiceFa
                     serviceProperties.put(key, val);
                 }
 
+                Properties serviceProperties = new Properties();
+
+                for (Map.Entry entry : serviceProperties.entrySet()) {
+                    Object key = entry.getKey();
+                    Object val = entry.getValue();
+                    serviceProperties.put(key, val);
+                }
+
                 BlobStoreDiscoveryService service = new BlobStoreDiscoveryService();
 
                 String provider = (String) properties.get(PROVIDER);

Added: karaf/cellar/trunk/cloud/src/main/java/org/apache/karaf/cellar/cloud/ServiceContainer.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/cloud/src/main/java/org/apache/karaf/cellar/cloud/ServiceContainer.java?rev=1467911&view=auto
==============================================================================
--- karaf/cellar/trunk/cloud/src/main/java/org/apache/karaf/cellar/cloud/ServiceContainer.java (added)
+++ karaf/cellar/trunk/cloud/src/main/java/org/apache/karaf/cellar/cloud/ServiceContainer.java Mon Apr 15 09:18:08 2013
@@ -0,0 +1,59 @@
+/*
+ * Licensed 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.karaf.cellar.cloud;
+
+import java.io.Serializable;
+
+import org.joda.time.DateTime;
+
+public class ServiceContainer implements Serializable {
+
+	/**
+	 * 
+	 */
+	private static final long serialVersionUID = 1L;
+	
+	private DateTime registeredTime;
+	private String hostName;
+	private String hostIp;
+	private String hostPort;
+	
+	public DateTime getRegisteredTime() {
+		return registeredTime;
+	}
+
+	public String getHostName() {
+		return hostName;
+	}
+	
+	public String getHostIp() {
+		return hostIp;
+	}
+	
+	public String getHostPort() {
+		return hostPort;
+	}
+
+	public ServiceContainer(String hostName, String hostIp, DateTime registeredTime) {
+		this(hostName, hostIp, null, registeredTime);
+	}
+	
+	
+	public ServiceContainer(String hostName, String hostIp, String hostPort, DateTime registeredTime) {
+		this.registeredTime = registeredTime;
+		this.hostName = hostName;
+		this.hostIp = hostIp;
+		this.hostPort = hostPort;
+	}
+}

Propchange: karaf/cellar/trunk/cloud/src/main/java/org/apache/karaf/cellar/cloud/ServiceContainer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: karaf/cellar/trunk/core/src/main/java/org/apache/karaf/cellar/core/discovery/DiscoveryTask.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/core/src/main/java/org/apache/karaf/cellar/core/discovery/DiscoveryTask.java?rev=1467911&r1=1467910&r2=1467911&view=diff
==============================================================================
--- karaf/cellar/trunk/core/src/main/java/org/apache/karaf/cellar/core/discovery/DiscoveryTask.java (original)
+++ karaf/cellar/trunk/core/src/main/java/org/apache/karaf/cellar/core/discovery/DiscoveryTask.java Mon Apr 15 09:18:08 2013
@@ -14,7 +14,9 @@
 package org.apache.karaf.cellar.core.discovery;
 
 import java.io.IOException;
+import java.util.Collections;
 import java.util.Dictionary;
+import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.LinkedHashSet;
 import java.util.List;
@@ -38,10 +40,12 @@ public class DiscoveryTask implements Ru
     private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
 
     public void init() {
+    	LOGGER.debug("CELLAR DISCOVERY: a new Task initalized");
         scheduler.scheduleWithFixedDelay(this, 10, 10, TimeUnit.SECONDS);
     }
 
     public void destroy() {
+    	LOGGER.debug("CELLAR DISCOVERY: task is beeing destroyed");
         scheduler.shutdown();
     }
 
@@ -56,20 +60,34 @@ public class DiscoveryTask implements Ru
                     service.refresh();
                     Set<String> discovered = service.discoverMembers();
                     members.addAll(discovered);
+                    LOGGER.trace("CELLAR DISCOVERY: Service {} found members {}", service, discovered);
                 }
                 try {
+                	LOGGER.trace("CELLAR DISCOVERY: retrieving configuration for PID={}", Discovery.PID);
                     Configuration configuration = configurationAdmin.getConfiguration(Discovery.PID);
                     Dictionary properties = configuration.getProperties();
+                    if (properties == null) {
+                    	//this is a new configuration ...
+                    	LOGGER.trace("CELLAR DISCOVERY: configuration is new!");
+                    	properties = new Hashtable();
+                    }
                     String newMemberText = CellarUtils.createStringFromSet(members, true);
                     String memberText = (String) properties.get(Discovery.MEMBERS_PROPERTY_NAME);
                     if (newMemberText != null && newMemberText.length() > 0 && !newMemberText.equals(memberText)) {
                         properties.put(Discovery.DISCOVERED_MEMBERS_PROPERTY_NAME, newMemberText);
+                        LOGGER.trace("CELLAR DISCOVERY: adding a new member {} to configuration and updating it", newMemberText);
                         configuration.update(properties);
+                    } else {
+                    	LOGGER.trace("CELLAR DISCOVERY: found a valid member in the configuration will skip");
                     }
                 } catch (IOException e) {
                     LOGGER.error("Failed to update member list", e);
                 }
+            } else {
+            	LOGGER.trace("CELLAR DISCOVERY: no discovery services found ... ");
             }
+        } else {
+        	LOGGER.trace("CELLAR DISCOVERY: no config admin found");
         }
     }
 

Modified: karaf/cellar/trunk/core/src/main/java/org/apache/karaf/cellar/core/event/EventDispatchTask.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/core/src/main/java/org/apache/karaf/cellar/core/event/EventDispatchTask.java?rev=1467911&r1=1467910&r2=1467911&view=diff
==============================================================================
--- karaf/cellar/trunk/core/src/main/java/org/apache/karaf/cellar/core/event/EventDispatchTask.java (original)
+++ karaf/cellar/trunk/core/src/main/java/org/apache/karaf/cellar/core/event/EventDispatchTask.java Mon Apr 15 09:18:08 2013
@@ -69,13 +69,13 @@ public class EventDispatchTask<E extends
 
     public void run() {
         try {
-        boolean dispathced = false;
+        boolean dispatched = false;
 
-        for (long delay = 0; delay < timeout && !dispathced; delay += interval) {
+        for (long delay = 0; delay < timeout && !dispatched; delay += interval) {
             EventHandler handler = handlerRegistry.getHandler(event);
             if (handler != null) {
                 handler.handle(event);
-                dispathced = true;
+                dispatched = true;
             } else {
                 try {
                     Thread.sleep(interval);
@@ -84,7 +84,7 @@ public class EventDispatchTask<E extends
                 }
             }
         }
-        if (!dispathced) {
+        if (!dispatched) {
             LOGGER.warn("Failed to retrieve handler for event {}", event.getClass());
         }
         }catch(Exception ex) {

Modified: karaf/cellar/trunk/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/HazelcastGroupManager.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/HazelcastGroupManager.java?rev=1467911&r1=1467910&r2=1467911&view=diff
==============================================================================
--- karaf/cellar/trunk/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/HazelcastGroupManager.java (original)
+++ karaf/cellar/trunk/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/HazelcastGroupManager.java Mon Apr 15 09:18:08 2013
@@ -534,6 +534,8 @@ public class HazelcastGroupManager imple
      * @return
      */
     protected Set<String> convertStringToSet(String string) {
+    	if (string == null)
+    		return Collections.EMPTY_SET;
         Set<String> result = new HashSet<String>();
         String[] groupNames = string.split(",");