stratos-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From im...@apache.org
Subject stratos git commit: Adding removeMap(), removeList() methods to distributed object provider, adding method comments, updating debug logs and updating unit tests
Date Wed, 03 Dec 2014 20:29:37 GMT
Repository: stratos
Updated Branches:
  refs/heads/master e36f2be32 -> fd5c273d0


Adding removeMap(), removeList() methods to distributed object provider, adding method comments,
updating debug logs and updating unit tests


Project: http://git-wip-us.apache.org/repos/asf/stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/stratos/commit/fd5c273d
Tree: http://git-wip-us.apache.org/repos/asf/stratos/tree/fd5c273d
Diff: http://git-wip-us.apache.org/repos/asf/stratos/diff/fd5c273d

Branch: refs/heads/master
Commit: fd5c273d0b80195d9188a0245c04c36901eb5ca7
Parents: e36f2be
Author: Imesh Gunaratne <imesh@apache.org>
Authored: Thu Dec 4 01:59:05 2014 +0530
Committer: Imesh Gunaratne <imesh@apache.org>
Committed: Thu Dec 4 01:59:25 2014 +0530

----------------------------------------------------------------------
 .../clustering/DistributedObjectProvider.java   | 37 +++++++-
 .../HazelcastDistributedObjectProvider.java     | 99 ++++++++++++++++----
 .../test/DistributedObjectProviderTest.java     | 14 ++-
 3 files changed, 128 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/stratos/blob/fd5c273d/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/clustering/DistributedObjectProvider.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/clustering/DistributedObjectProvider.java
b/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/clustering/DistributedObjectProvider.java
index 7e7d130..6caa3a1 100644
--- a/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/clustering/DistributedObjectProvider.java
+++ b/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/clustering/DistributedObjectProvider.java
@@ -26,13 +26,48 @@ import java.util.concurrent.locks.Lock;
 
 /**
  * Distributed object provider service interface.
+ * Caution! When using distributed maps and lists, please note that changes done to an item
in a map/list
+ * after adding them to the map/list will not be replicated in the cluster. If a modification
of an item
+ * needs to be replicated, that item needs to be put() to the map or set() back in the list.
  */
 public interface DistributedObjectProvider extends Serializable {
-    Map getMap(String key);
+    /**
+     * Returns a distributed map if clustering is enabled, else returns a local hash map.
+     * @param name
+     * @return
+     */
+    Map getMap(String name);
 
+    /**
+     * Removes a map from the object provider.
+     * @param name
+     */
+    void removeMap(String name);
+
+    /**
+     * Returns a distributed list if clustering is enabled, else returns a local array list.
+     * @param name
+     * @return
+     */
     List getList(String name);
 
+    /**
+     * Remove a list from the object provider.
+     * @param name
+     */
+    void removeList(String name);
+
+    /**
+     * Acquires a distributed lock if clustering is enabled, else acquires a local reentrant
lock and
+     * returns the lock object.
+     * @param object
+     * @return
+     */
     Lock acquireLock(Object object);
 
+    /**
+     * Releases a given distributed/local lock.
+     * @param lock
+     */
     void releaseLock(Lock lock);
 }

http://git-wip-us.apache.org/repos/asf/stratos/blob/fd5c273d/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/clustering/impl/HazelcastDistributedObjectProvider.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/clustering/impl/HazelcastDistributedObjectProvider.java
b/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/clustering/impl/HazelcastDistributedObjectProvider.java
index e5dab09..f7ff50e 100644
--- a/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/clustering/impl/HazelcastDistributedObjectProvider.java
+++ b/components/org.apache.stratos.common/src/main/java/org/apache/stratos/common/clustering/impl/HazelcastDistributedObjectProvider.java
@@ -20,7 +20,9 @@
 package org.apache.stratos.common.clustering.impl;
 
 import com.hazelcast.core.HazelcastInstance;
+import com.hazelcast.core.IList;
 import com.hazelcast.core.ILock;
+import com.hazelcast.core.IMap;
 import org.apache.axis2.engine.AxisConfiguration;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -33,7 +35,6 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
@@ -45,80 +46,140 @@ public class HazelcastDistributedObjectProvider implements DistributedObjectProv
 
     private HazelcastDistributedMapProvider mapProvider;
     private HazelcastDistributedListProvider listProvider;
+    private Map<String, Map> mapsMap;
+    private Map<String, List> listsMap;
     private Map<Object, Lock> locksMap;
 
     public HazelcastDistributedObjectProvider() {
         HazelcastInstance hazelcastInstance = ServiceReferenceHolder.getInstance().getHazelcastInstance();
         mapProvider = new HazelcastDistributedMapProvider(hazelcastInstance);
         listProvider = new HazelcastDistributedListProvider(hazelcastInstance);
+        mapsMap = new HashMap<String, Map>();
+        listsMap = new HashMap<String, List>();
         locksMap = new HashMap<Object, Lock>();
     }
 
     /**
-     * If clustering is enabled returns a distributed map object, otherwise returns a
-     * concurrent local map object.
-     * @param key
+     * Returns a distributed map if clustering is enabled, else returns a local hash map.
+     * @param name
      * @return
      */
     @Override
-    public Map getMap(String key) {
+    public Map getMap(final String name) {
+        if(mapsMap.containsKey(name)) {
+            return mapsMap.get(name);
+        }
+
+        Map map = null;
         if(isClustered()) {
-            return mapProvider.getMap(key, new MapEntryListener() {
+            map = mapProvider.getMap(name, new MapEntryListener() {
                 @Override
                 public <X> void entryAdded(X key) {
                     if(log.isDebugEnabled()) {
-                        log.debug(String.format("Entry added to distributed map: [key] %s",
key));
+                        log.debug(String.format("Entry added to distributed map: [name] %s
[key] %s",
+                                name, key));
                     }
                 }
 
                 @Override
                 public <X> void entryRemoved(X key) {
                     if(log.isDebugEnabled()) {
-                        log.debug(String.format("Entry removed from distributed map: [key]
%s", key));
+                        log.debug(String.format("Entry removed from distributed map: [name]
%s [key] %s",
+                                name, key));
                     }
                 }
 
                 @Override
                 public <X> void entryUpdated(X key) {
                     if(log.isDebugEnabled()) {
-                        log.debug(String.format("Entry updated in distributed map: [key]
%s", key));
+                        log.debug(String.format("Entry updated in distributed map: [name]
%s [key] %s",
+                                name, key));
                     }
                 }
             });
         } else {
-            return new ConcurrentHashMap<Object, Object>();
+            map = new HashMap<Object, Object>();
+        }
+        if(map != null) {
+            mapsMap.put(name, map);
+        }
+        return map;
+    }
+
+    /**
+     * Remove map from provider
+     * @param name
+     */
+    public void removeMap(String name) {
+        if(mapsMap.containsKey(name)) {
+            if(isClustered()) {
+                IMap map = (IMap) mapsMap.get(name);
+                mapProvider.removeMap(name);
+                map.destroy();
+            }
+            mapsMap.remove(name);
         }
     }
 
     /**
-     * If clustering is enabled returns a distributed list, otherwise returns
-     * a local array list.
+     * Returns a distributed list if clustering is enabled, else returns a local array list.
      * @param name
      * @return
      */
     @Override
-    public List getList(String name) {
+    public List getList(final String name) {
+        if(listsMap.containsKey(name)) {
+            return listsMap.get(name);
+        }
+
+        List list = null;
         if(isClustered()) {
-            return listProvider.getList(name, new ListEntryListener() {
+            list = listProvider.getList(name, new ListEntryListener() {
                 @Override
                 public void itemAdded(Object item) {
                     if(log.isDebugEnabled()) {
-                        log.debug("Item added to distributed list: " + item);
+                        log.debug(String.format("Item added to distributed list: [list] %s
[item] %s", name, item));
                     }
                 }
 
                 @Override
                 public void itemRemoved(Object item) {
                     if(log.isDebugEnabled()) {
-                        log.debug("Item removed from distributed list: " + item);
+                        log.debug(String.format("Item removed from distributed list: [list]
%s [item] %s", name, item));
                     }
                 }
             });
         } else {
-            return new ArrayList();
+            list = new ArrayList();
+        }
+        if(list != null) {
+            listsMap.put(name, list);
         }
+        return list;
     }
 
+    /**
+     * Remove a list from the object provider.
+     * @param name
+     */
+    @Override
+    public void removeList(String name) {
+        if(listsMap.containsKey(name)) {
+            if(isClustered()) {
+                IList list = (IList)listsMap.get(name);
+                listProvider.removeList(name);
+                list.destroy();
+            }
+            listsMap.remove(name);
+        }
+    }
+
+    /**
+     * Acquires a distributed lock if clustering is enabled, else acquires a local reentrant
lock and
+     * returns the lock object.
+     * @param object
+     * @return
+     */
     @Override
     public Lock acquireLock(Object object) {
         if(isClustered()) {
@@ -138,6 +199,10 @@ public class HazelcastDistributedObjectProvider implements DistributedObjectProv
         }
     }
 
+    /**
+     * Releases a given distributed/local lock.
+     * @param lock
+     */
     @Override
     public void releaseLock(Lock lock) {
          if(isClustered()) {

http://git-wip-us.apache.org/repos/asf/stratos/blob/fd5c273d/components/org.apache.stratos.common/src/test/java/org/apache/stratos/common/test/DistributedObjectProviderTest.java
----------------------------------------------------------------------
diff --git a/components/org.apache.stratos.common/src/test/java/org/apache/stratos/common/test/DistributedObjectProviderTest.java
b/components/org.apache.stratos.common/src/test/java/org/apache/stratos/common/test/DistributedObjectProviderTest.java
index 60ebc99..846d493 100644
--- a/components/org.apache.stratos.common/src/test/java/org/apache/stratos/common/test/DistributedObjectProviderTest.java
+++ b/components/org.apache.stratos.common/src/test/java/org/apache/stratos/common/test/DistributedObjectProviderTest.java
@@ -38,6 +38,10 @@ import static org.junit.Assert.assertTrue;
  */
 public class DistributedObjectProviderTest {
 
+    public static final String MAP_1 = "MAP1";
+    public static final String MAP_1_WRITE_LOCK = "MAP1_WRITE_LOCK";
+    public static final String LIST_1 = "LIST1";
+    public static final String LIST_1_WRITE_LOCK = "LIST1_WRITE_LOCK";
     private static HazelcastInstance hazelcastInstance;
 
     @BeforeClass
@@ -62,14 +66,15 @@ public class DistributedObjectProviderTest {
     }
 
     private void testPutToMap(HazelcastDistributedObjectProvider provider) {
-        Map<String, String> map = provider.getMap("MAP1");
+        Map<String, String> map = provider.getMap(MAP_1);
         Lock lock = null;
         try {
-            lock = provider.acquireLock("MAP1_WRITE_LOCK");
+            lock = provider.acquireLock(MAP_1_WRITE_LOCK);
             map.put("key1", "value1");
             assertEquals(map.get("key1"), "value1");
         } finally {
             provider.releaseLock(lock);
+            provider.removeMap(MAP_1);
         }
     }
 
@@ -90,15 +95,16 @@ public class DistributedObjectProviderTest {
     }
 
     private void testAddToList(HazelcastDistributedObjectProvider provider) {
-        List list = provider.getList("LIST1");
+        List list = provider.getList(LIST_1);
         Lock lock = null;
         try {
-            lock = provider.acquireLock("LIST1_WRITE_LOCK");
+            lock = provider.acquireLock(LIST_1_WRITE_LOCK);
             String value1 = "value1";
             list.add(value1);
             assertTrue(list.contains(value1));
         } finally {
             provider.releaseLock(lock);
+            provider.removeList(LIST_1);
         }
     }
 }


Mime
View raw message