JBoss Cache SVN: r4678 - in core/trunk/src: main/java/org/jboss/cache/util and 1 other directories.
by jbosscache-commits@lists.jboss.org
Author: genman
Date: 2007-10-23 16:53:27 -0400 (Tue, 23 Oct 2007)
New Revision: 4678
Added:
core/trunk/src/main/java/org/jboss/cache/util/MinMapUtil.java
core/trunk/src/test/java/org/jboss/cache/util/MinMapUtilTest.java
Modified:
core/trunk/src/main/java/org/jboss/cache/AbstractNode.java
core/trunk/src/main/java/org/jboss/cache/Fqn.java
core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java
core/trunk/src/main/java/org/jboss/cache/util/DeltaMap.java
core/trunk/src/test/java/org/jboss/cache/util/DeltaMapTest.java
Log:
Save some Node memory by using "MinMapUtil"
Add assertValid() to AbstractNode
Fix size() compute for DeltaMap
Modified: core/trunk/src/main/java/org/jboss/cache/AbstractNode.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/AbstractNode.java 2007-10-23 17:47:12 UTC (rev 4677)
+++ core/trunk/src/main/java/org/jboss/cache/AbstractNode.java 2007-10-23 20:53:27 UTC (rev 4678)
@@ -14,7 +14,7 @@
{
protected boolean deleted;
protected Map<Object, Node<K, V>> children;
- protected Fqn fqn;
+ protected Fqn<?> fqn;
protected boolean resident;
public boolean isDeleted()
@@ -34,7 +34,7 @@
{
synchronized (this)
{
- for (Node child : children.values())
+ for (Node<?,?> child : children.values())
{
((AbstractNode) child).markAsDeleted(marker, true);
}
@@ -59,7 +59,7 @@
{
if (another instanceof AbstractNode)
{
- AbstractNode anotherNode = (AbstractNode) another;
+ AbstractNode<?,?> anotherNode = (AbstractNode) another;
return fqn == null && anotherNode.fqn == null || !(fqn == null || anotherNode.fqn == null) && fqn.equals(anotherNode.fqn);
}
return false;
Modified: core/trunk/src/main/java/org/jboss/cache/Fqn.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/Fqn.java 2007-10-23 17:47:12 UTC (rev 4677)
+++ core/trunk/src/main/java/org/jboss/cache/Fqn.java 2007-10-23 20:53:27 UTC (rev 4678)
@@ -61,23 +61,30 @@
* @version $Revision$
*/
@Immutable
-public class Fqn<E> implements Cloneable, Externalizable, Comparable<Fqn>
+public class Fqn<E> implements Cloneable, Externalizable, Comparable<Fqn<?>>
{
/**
* Separator between FQN elements.
*/
public static final String SEPARATOR = "/";
+
private static final long serialVersionUID = -5351930616956603651L;
+
+ private static final Log log = LogFactory.getLog(Fqn.class);
+
private List<E> elements;
private transient int hash_code = 0;
/**
* Immutable root FQN.
*/
+ @SuppressWarnings("unchecked")
public static final Fqn ROOT = new Fqn();
- private final static Log log = LogFactory.getLog(Fqn.class);
- // a cached string representation of this Fqn, used by toString to it isn't calculated again every time.
+
+ /**
+ * A cached string representation of this Fqn, used by toString to it isn't calculated again every time.
+ */
private String cachedStringRep;
/**
@@ -306,7 +313,7 @@
{
return false;
}
- Fqn other = (Fqn) obj;
+ Fqn<?> other = (Fqn<?>) obj;
return elements.equals(other.elements);
}
@@ -342,7 +349,7 @@
}
else
{
- StringBuffer sb = new StringBuffer();
+ StringBuilder sb = new StringBuilder();
for (E element : elements)
{
sb.append(SEPARATOR).append(element);
@@ -519,7 +526,7 @@
/**
* Compares this Fqn to another using {@link FqnComparator}.
*/
- public int compareTo(Fqn Fqn)
+ public int compareTo(Fqn<?> Fqn)
{
return FqnComparator.INSTANCE.compare(this, Fqn);
}
Modified: core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java 2007-10-23 17:47:12 UTC (rev 4677)
+++ core/trunk/src/main/java/org/jboss/cache/UnversionedNode.java 2007-10-23 20:53:27 UTC (rev 4678)
@@ -14,6 +14,7 @@
import org.jboss.cache.marshall.MethodDeclarations;
import org.jboss.cache.optimistic.DataVersion;
import org.jboss.cache.transaction.GlobalTransaction;
+import org.jboss.cache.util.MinMapUtil;
import java.util.Collections;
import java.util.HashMap;
@@ -66,9 +67,13 @@
/**
* Map of general data keys to values.
*/
- private final Map<K, V> data = new HashMap<K, V>();
+ private Map<K, V> data = Collections.emptyMap();
private boolean lockForChildInsertRemove;
+
+ /**
+ * Node moved or removed.
+ */
private boolean valid = true;
/**
@@ -91,7 +96,7 @@
init(child_name, fqn, cache);
setInternalState(data);
}
-
+
/**
* Initializes with a name and FQN and cache.
*/
@@ -162,15 +167,20 @@
childrenLoaded = flag;
}
+ private void assertValid() {
+ if (!valid)
+ throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ }
+
public V get(K key)
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ assertValid();
return cache.get(getFqn(), key);
}
public V getDirect(K key)
{
- return data == null ? null : data.get(key);
+ return data.get(key);
}
@@ -192,35 +202,28 @@
public Map<K, V> getData()
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
- if (cache == null) return Collections.emptyMap();
+ assertValid();
+ if (cache == null)
+ return Collections.emptyMap();
return cache.getData(getFqn());
-
-/*
- Map<K, V> dMap = new HashMap<K, V>();
- for (K k : cache.getKeys(getFqn()))
- {
- dMap.put(k, cache.get(fqn, k));
- }
- return Collections.unmodifiableMap(dMap);
-*/
}
public Map<K, V> getDataDirect()
{
- if (data == null) return Collections.emptyMap();
return Collections.unmodifiableMap(data);
}
public V put(K key, V value)
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ assertValid();
return cache.put(getFqn(), key, value);
}
public V putDirect(K key, V value)
{
- return data.put(key, value);
+ V v = data.get(key);
+ data = MinMapUtil.put(data, key, value);
+ return v;
}
public NodeSPI<K, V> getOrCreateChild(Object child_name, GlobalTransaction gtx)
@@ -287,14 +290,15 @@
public V remove(K key)
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ assertValid();
return cache.remove(getFqn(), key);
}
public V removeDirect(K key)
{
- if (data == null) return null;
- return data.remove(key);
+ V v = data.get(key);
+ data = MinMapUtil.remove(data, key);
+ return v;
}
public void printDetails(StringBuffer sb, int indent)
@@ -320,12 +324,9 @@
{
sb.append("[ ").append(fqn);
}
- if (data != null)
+ synchronized (data)
{
- synchronized (data)
- {
- sb.append(" data=").append(data.keySet());
- }
+ sb.append(" data=").append(data.keySet());
}
if (children != null && !children.isEmpty())
{
@@ -348,7 +349,7 @@
public Node<K, V> addChild(Fqn f)
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ assertValid();
Fqn nf = new Fqn(getFqn(), f);
cache.put(nf, null);
return getChild(f);
@@ -383,18 +384,18 @@
public void clearData()
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ assertValid();
cache.removeData(getFqn());
}
public void clearDataDirect()
{
- if (data != null) data.clear();
+ data = Collections.emptyMap();
}
public Node<K, V> getChild(Fqn fqn)
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ assertValid();
return cache.get(new Fqn(getFqn(), fqn));
}
@@ -419,7 +420,7 @@
public Set<Object> getChildrenNames()
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ assertValid();
return cache.getChildrenNames(getFqn());
}
@@ -430,14 +431,14 @@
public Set<K> getKeys()
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ assertValid();
Set<K> keys = cache.getKeys(getFqn());
return keys == null ? Collections.<K>emptySet() : Collections.<K>unmodifiableSet(keys);
}
public Set<K> getKeysDirect()
{
- if (data == null)
+ if (data.isEmpty())
{
return Collections.emptySet();
}
@@ -446,19 +447,19 @@
public boolean hasChild(Fqn f)
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ assertValid();
return getChild(f) != null;
}
public boolean hasChild(Object o)
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ assertValid();
return getChild(o) != null;
}
public V putIfAbsent(K k, V v)
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ assertValid();
// make sure this is atomic. Not hugely performant at the moment (should use the locking interceptors) but for now ...
synchronized (this)
{
@@ -471,7 +472,7 @@
public V replace(K key, V value)
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ assertValid();
// make sure this is atomic. Not hugely performant at the moment (should use the locking interceptors) but for now ...
synchronized (this)
{
@@ -486,7 +487,7 @@
public boolean replace(K key, V oldValue, V newValue)
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ assertValid();
// make sure this is atomic. Not hugely performant at the moment (should use the locking interceptors) but for now ...
synchronized (this)
{
@@ -502,19 +503,19 @@
public boolean removeChild(Fqn fqn)
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ assertValid();
return cache.removeNode(new Fqn(getFqn(), fqn));
}
public int dataSize()
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ assertValid();
return cache.getKeys(getFqn()).size();
}
public boolean removeChild(Object childName)
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ assertValid();
return cache.removeNode(new Fqn(getFqn(), childName));
}
@@ -549,20 +550,19 @@
public void putAll(Map<K, V> data)
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ assertValid();
cache.put(fqn, data);
}
public void replaceAll(Map<K, V> data)
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
+ assertValid();
cache.put(fqn, data, true);
}
public void putAllDirect(Map<K, V> data)
{
- if (data == null) return;
- this.data.putAll(data);
+ MinMapUtil.putAll(this.data, data);
}
public void removeChildrenDirect()
@@ -670,8 +670,9 @@
public Set<Node<K, V>> getChildren()
{
- if (!valid) throw new NodeNotValidException("Node " + getFqn() + " is not valid. Perhaps it has been moved or removed.");
- if (cache == null) return Collections.emptySet();
+ assertValid();
+ if (cache == null)
+ return Collections.emptySet();
Set<Node<K, V>> children = new HashSet<Node<K, V>>();
for (Object c : cache.getChildrenNames(getFqn()))
{
@@ -797,9 +798,9 @@
@SuppressWarnings("unchecked")
public Map getInternalState(boolean onlyInternalState)
{
- if (onlyInternalState) return new HashMap();
+ if (onlyInternalState)
+ return new HashMap(0);
// don't bother doing anything here
- if (data == null) return new HashMap();
return new HashMap(data);
}
}
Modified: core/trunk/src/main/java/org/jboss/cache/util/DeltaMap.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/DeltaMap.java 2007-10-23 17:47:12 UTC (rev 4677)
+++ core/trunk/src/main/java/org/jboss/cache/util/DeltaMap.java 2007-10-23 20:53:27 UTC (rev 4678)
@@ -77,6 +77,15 @@
}
/**
+ * Creates and returns a DeltaMap for an empty map.
+ * @return a new instance
+ */
+ public static <K, V> DeltaMap<K, V> create()
+ {
+ return create(new HashMap<K,V>(0));
+ }
+
+ /**
* Creates and returns a DeltaMap for an original map, excluding some key mappings.
*
* @param original will not be modified, except by {@link #commit()}
@@ -111,18 +120,30 @@
@Override
public int size()
{
- int size = original.size() - exclude.size();
- for (Object o : changed.keySet())
- {
- if (!original.containsKey(o))
- size++;
- }
- return size;
+ return DeltaMap.this.size();
}
+
};
}
@Override
+ public int size()
+ {
+ int size = original.size();
+ for (Object o : changed.keySet())
+ {
+ if (!original.containsKey(o))
+ size++;
+ }
+ for (Object o : exclude)
+ {
+ if (original.containsKey(o))
+ size--;
+ }
+ return size;
+ }
+
+ @Override
public boolean containsKey(Object key)
{
if (exclude.contains(key))
@@ -281,6 +302,24 @@
}
/**
+ * Returns the original wrapped Map.
+ */
+ public Map<K, V> getOriginal()
+ {
+ return original;
+ }
+
+ /**
+ * Sets the original values of this delta map.
+ */
+ public void setOriginal(Map<K, V> original)
+ {
+ if (original == null)
+ throw new NullPointerException("original");
+ this.original = original;
+ }
+
+ /**
* Returns a Map of the entries changed, not including those removed.
*/
public Map<K, V> getChanged()
Added: core/trunk/src/main/java/org/jboss/cache/util/MinMapUtil.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/util/MinMapUtil.java (rev 0)
+++ core/trunk/src/main/java/org/jboss/cache/util/MinMapUtil.java 2007-10-23 20:53:27 UTC (rev 4678)
@@ -0,0 +1,96 @@
+package org.jboss.cache.util;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+/**
+ * Minimizes Map memory usage by changing the map
+ * instance based on the number of stored entries.
+ *
+ * @author Elias Ross
+ */
+public class MinMapUtil
+{
+
+ private static Class<?> singleton = Collections.singletonMap(null, null).getClass();
+
+ private static Class<?> empty = Collections.emptyMap().getClass();
+
+ private MinMapUtil()
+ {
+ }
+
+ /**
+ * Puts a mapping into a map, returns a map with the mapping.
+ * @param map destination
+ */
+ public static <K, V> Map<K, V> put(Map<K, V> map, K key, V value)
+ {
+ int size = map.size();
+ if (size == 0)
+ return Collections.singletonMap(key, value);
+ if (size == 1)
+ {
+ HashMap<K, V> map2 = new HashMap<K, V>(map);
+ map2.put(key, value);
+ return map2;
+ }
+ else
+ {
+ map.put(key, value);
+ return map;
+ }
+ }
+
+ /**
+ * Puts a number of entries into a map, returns a map.
+ * @param dest destination map
+ * @param src source map
+ */
+ public static <K, V> Map<K, V> putAll(Map<K, V> dest, Map<K, V> src)
+ {
+ if (src == null)
+ {
+ return dest;
+ }
+ int srcSize = src.size();
+ if (srcSize == 0)
+ {
+ return dest;
+ }
+ if (srcSize == 1)
+ {
+ Entry<K, V> next = src.entrySet().iterator().next();
+ return Collections.singletonMap(next.getKey(), next.getValue());
+ }
+ Class<?> c = dest.getClass();
+ if (c == empty || c == singleton)
+ return new HashMap<K, V>(src);
+ dest.putAll(src);
+ return dest;
+ }
+
+ /**
+ * Removes a mapping by key from a map, returns the map.
+ */
+ public static <K, V> Map<K, V> remove(Map<K, V> map, K key)
+ {
+ int size = map.size();
+ if (size == 0)
+ {
+ return map;
+ }
+ if (size == 1)
+ {
+ if (map.containsKey(key))
+ return Collections.emptyMap();
+ else
+ return map;
+ }
+ map.remove(key);
+ return map;
+ }
+
+}
Modified: core/trunk/src/test/java/org/jboss/cache/util/DeltaMapTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/util/DeltaMapTest.java 2007-10-23 17:47:12 UTC (rev 4677)
+++ core/trunk/src/test/java/org/jboss/cache/util/DeltaMapTest.java 2007-10-23 20:53:27 UTC (rev 4678)
@@ -41,6 +41,20 @@
assertEquals(hm, dm);
}
+ public void testSize()
+ {
+ assertEquals(3, dm.size());
+ dm.put(Y, "HI");
+ assertEquals(3, dm.size());
+ dm.remove(Y);
+ assertEquals(2, dm.size());
+ hm.clear();
+ assertEquals(0, dm.size());
+ dm.put(Z, Z);
+ dm.getRemoved().add("NOT HERE");
+ assertEquals(1, dm.size());
+ }
+
public void testConcurrent() throws Exception
{
ConcurrentHashMap<Object, String> m = new ConcurrentHashMap<Object, String>();
Added: core/trunk/src/test/java/org/jboss/cache/util/MinMapUtilTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/util/MinMapUtilTest.java (rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/util/MinMapUtilTest.java 2007-10-23 20:53:27 UTC (rev 4678)
@@ -0,0 +1,63 @@
+package org.jboss.cache.util;
+
+import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertSame;
+
+import java.util.Collections;
+import java.util.Map;
+import java.util.HashMap;
+
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+@Test(groups =
+{"functional", "transaction"})
+public class MinMapUtilTest
+{
+
+ private static final Map<String, String> empty = Collections.emptyMap();
+ private Map<String, String> map;
+ private String key = "a";
+ private String key2 = "b";
+ private String value = "y";
+
+ @BeforeMethod
+ public void before() {
+ map = empty;
+ }
+
+ public void testRemove()
+ {
+ assertSame(map, MinMapUtil.remove(map, key));
+ map = MinMapUtil.put(map, key, value);
+ map = MinMapUtil.put(map, key2, value);
+ assertEquals(value, map.get(key));
+ assertEquals(value, map.get(key2));
+ MinMapUtil.remove(map, key2);
+ assertSame(map, MinMapUtil.remove(map, "not here"));
+ assertSame(empty, MinMapUtil.remove(map, key));
+ }
+
+ public void testPut()
+ {
+ assertSame(map, MinMapUtil.remove(map, key));
+ map = MinMapUtil.put(map, key, value);
+ assertEquals(1, map.size());
+ assertEquals(true, map.containsKey(key));
+ map = MinMapUtil.put(map, key, value);
+ assertEquals(1, map.size());
+ assertEquals(true, map.containsKey(key));
+ map = MinMapUtil.put(map, key2, value);
+ assertEquals(2, map.size());
+ }
+
+ public void testPutAll()
+ {
+ HashMap<String, String> hm = new HashMap<String, String>();
+ assertSame(empty, MinMapUtil.putAll(map, hm));
+ hm.put(key, value);
+ assertEquals(1, MinMapUtil.putAll(map, hm).size());
+ hm.put(key2, value);
+ assertEquals(2, MinMapUtil.putAll(map, hm).size());
+ }
+}
16 years, 6 months
JBoss Cache SVN: r4677 - core/trunk/src/test/java/org/jboss/cache/buddyreplication.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2007-10-23 13:47:12 -0400 (Tue, 23 Oct 2007)
New Revision: 4677
Modified:
core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyReplicationTestsBase.java
Log:
modified helper methods
Modified: core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyReplicationTestsBase.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyReplicationTestsBase.java 2007-10-23 17:31:08 UTC (rev 4676)
+++ core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyReplicationTestsBase.java 2007-10-23 17:47:12 UTC (rev 4677)
@@ -6,16 +6,6 @@
*/
package org.jboss.cache.buddyreplication;
-import static org.testng.AssertJUnit.assertEquals;
-import static org.testng.AssertJUnit.assertFalse;
-import static org.testng.AssertJUnit.assertTrue;
-
-import java.io.File;
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.transaction.TransactionManager;
-
import org.jboss.cache.Cache;
import org.jboss.cache.CacheImpl;
import org.jboss.cache.CacheSPI;
@@ -33,10 +23,15 @@
import org.jboss.cache.misc.TestingUtil;
import org.jboss.cache.xml.XmlHelper;
import org.jgroups.Address;
+import static org.testng.AssertJUnit.*;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.w3c.dom.Element;
+import javax.transaction.TransactionManager;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* Base class for BR tests
*
@@ -275,11 +270,12 @@
return caches;
}
- protected void printBuddyGroup(CacheSPI cache)
+ protected void printBuddyGroup(Cache cache)
{
- BuddyGroup bg = cache.getBuddyManager().buddyGroup;
+ BuddyManager bm = ((CacheSPI) cache).getBuddyManager();
+ BuddyGroup bg = bm.buddyGroup;
System.out.println("*** " + bg);
- System.out.println(" Groups I participate in: " + cache.getBuddyManager().buddyGroupsIParticipateIn.keySet());
+ System.out.println(" Groups I participate in: " + bm.buddyGroupsIParticipateIn.keySet());
}
/**
@@ -290,7 +286,7 @@
return 1000;
}
- protected void assertIsBuddy(CacheSPI dataOwner, CacheSPI buddy, boolean onlyBuddy)
+ protected void assertIsBuddy(Cache dataOwner, Cache buddy, boolean onlyBuddy)
{
Address dataOwnerLocalAddress = dataOwner.getLocalAddress();
Address buddyLocalAddress = buddy.getLocalAddress();
@@ -298,8 +294,8 @@
System.out.println("*** assert with groups. Testing that " + buddyLocalAddress + " is a buddy for owner " + dataOwnerLocalAddress + " only buddy? " + onlyBuddy);
printBuddyGroup(dataOwner);
- BuddyManager dataOwnerBuddyManager = dataOwner.getBuddyManager();
- BuddyManager buddyBuddyManager = buddy.getBuddyManager();
+ BuddyManager dataOwnerBuddyManager = ((CacheSPI) dataOwner).getBuddyManager();
+ BuddyManager buddyBuddyManager = ((CacheSPI) buddy).getBuddyManager();
// lets test things on the data owner's side of things
if (onlyBuddy) assertEquals("Should only have one buddy", 1, dataOwnerBuddyManager.getBuddyAddresses().size());
16 years, 6 months
JBoss Cache SVN: r4676 - core/trunk/src/test/java/org/jboss/cache/buddyreplication.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2007-10-23 13:31:08 -0400 (Tue, 23 Oct 2007)
New Revision: 4676
Modified:
core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyReplicationContentTest.java
Log:
Commented out repetition of test
Modified: core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyReplicationContentTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyReplicationContentTest.java 2007-10-23 17:28:44 UTC (rev 4675)
+++ core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyReplicationContentTest.java 2007-10-23 17:31:08 UTC (rev 4676)
@@ -237,7 +237,7 @@
}
}
- @Test (invocationCount = 25)
+ //@Test (invocationCount = 25)
public void testCompleteStateSurvival() throws Exception
{
log.debug("Running testCompleteStateSurvival");
16 years, 6 months
JBoss Cache SVN: r4675 - core/trunk/src/main/java/org/jboss/cache.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2007-10-23 13:28:44 -0400 (Tue, 23 Oct 2007)
New Revision: 4675
Modified:
core/trunk/src/main/java/org/jboss/cache/CacheImpl.java
Log:
Added more logging + missing break statement!!
Modified: core/trunk/src/main/java/org/jboss/cache/CacheImpl.java
===================================================================
--- core/trunk/src/main/java/org/jboss/cache/CacheImpl.java 2007-10-23 17:28:23 UTC (rev 4674)
+++ core/trunk/src/main/java/org/jboss/cache/CacheImpl.java 2007-10-23 17:28:44 UTC (rev 4675)
@@ -2950,6 +2950,7 @@
// for now, perform a very simple series of getData calls.
InvocationContext ctx = getInvocationContext();
+ if (log.isTraceEnabled()) log.trace("Caller is asking for " + fqn);
try
{
@@ -2961,10 +2962,12 @@
Node<K, V> actualNode = get(fqn);
ctx.getOptionOverrides().setSkipDataGravitation(false);
+ if (log.isTraceEnabled()) log.trace("In local tree, this is " + actualNode);
+
Fqn backupNodeFqn = null;
if (actualNode == null && searchSubtrees)
{
-
+ log.trace("Looking at backup trees.");
NodeSPI backupSubtree = findNode(BuddyManager.BUDDY_BACKUP_SUBTREE_FQN);
if (backupSubtree != null)
{
@@ -2982,6 +2985,8 @@
ctx.getOptionOverrides().setSkipDataGravitation(true);
actualNode = get(backupNodeFqn);
ctx.getOptionOverrides().setSkipDataGravitation(false);
+ if (log.isTraceEnabled()) log.trace("Looking for " + backupNodeFqn + ". Search result: " + actualNode);
+ if (actualNode != null) break;
}
}
}
16 years, 6 months
JBoss Cache SVN: r4674 - core/trunk/src/test/java/org/jboss/cache/buddyreplication.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2007-10-23 13:28:23 -0400 (Tue, 23 Oct 2007)
New Revision: 4674
Modified:
core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyReplicationContentTest.java
Log:
Improved test
Modified: core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyReplicationContentTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyReplicationContentTest.java 2007-10-23 17:02:47 UTC (rev 4673)
+++ core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyReplicationContentTest.java 2007-10-23 17:28:23 UTC (rev 4674)
@@ -15,6 +15,7 @@
import org.jboss.cache.notifications.annotation.CacheListener;
import org.jboss.cache.notifications.annotation.CacheUnblocked;
import org.jboss.cache.notifications.event.Event;
+import org.jboss.cache.util.CachePrinter;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.Test;
@@ -236,6 +237,7 @@
}
}
+ @Test (invocationCount = 25)
public void testCompleteStateSurvival() throws Exception
{
log.debug("Running testCompleteStateSurvival");
@@ -254,7 +256,7 @@
// TestingUtil.sleepThread(getSleepTimeout());
//TestingUtil.sleepThread(caches.get(0).getConfiguration().getStateRetrievalTimeout() * 3);
- blockListener.blockUntilAllCachesAreUnblocked(60000);
+ blockListener.blockUntilAllCachesAreUnblocked(caches.get(0).getConfiguration().getStateRetrievalTimeout() * 3);
log.info("stopping 2");
caches.get(2).stop();
@@ -265,7 +267,7 @@
// TestingUtil.sleepThread(getSleepTimeout());
// TestingUtil.sleepThread(caches.get(0).getConfiguration().getStateRetrievalTimeout() * 3);
- blockListener.blockUntilAllCachesAreUnblocked(60000);
+ blockListener.blockUntilAllCachesAreUnblocked(caches.get(0).getConfiguration().getStateRetrievalTimeout() * 3);
assertEquals("value", caches.get(0).get("/2", "key"));
@@ -275,7 +277,7 @@
// TestingUtil.sleepThread(getSleepTimeout());
// TestingUtil.sleepThread(caches.get(0).getConfiguration().getStateRetrievalTimeout() * 3);
- blockListener.blockUntilAllCachesAreUnblocked(60000);
+ blockListener.blockUntilAllCachesAreUnblocked(caches.get(0).getConfiguration().getStateRetrievalTimeout() * 3);
caches.get(1).stop();
log.info("0 ** " + caches.get(0).printLockInfo());
@@ -297,6 +299,9 @@
}
// now try the assertion again since the local gravitation would have worked.
+
+ System.out.println("Cache contents " + CachePrinter.printCacheDetails(caches.get(0)));
+
assertEquals("value", caches.get(0).get("/1", "key"));
assertEquals("value", caches.get(0).get("/2", "key"));
}
@@ -337,12 +342,13 @@
{
synchronized (this)
{
- if (blocks != 0)
+ if (blocks > 1)
{
wait(maxWait);
}
- if (blocks != 0)
+ if (blocks > 1)
throw new RuntimeException("Timed out waiting for unblocks. Number of blocks = " + blocks);
+ if (blocks == 1) blocks = 0;
}
}
}
16 years, 6 months
JBoss Cache SVN: r4673 - core/trunk/src/test/java/org/jboss/cache/buddyreplication.
by jbosscache-commits@lists.jboss.org
Author: manik.surtani(a)jboss.com
Date: 2007-10-23 13:02:47 -0400 (Tue, 23 Oct 2007)
New Revision: 4673
Modified:
core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyReplicationContentTest.java
Log:
Added some more info on blocks
Modified: core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyReplicationContentTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyReplicationContentTest.java 2007-10-23 11:54:46 UTC (rev 4672)
+++ core/trunk/src/test/java/org/jboss/cache/buddyreplication/BuddyReplicationContentTest.java 2007-10-23 17:02:47 UTC (rev 4673)
@@ -6,24 +6,27 @@
*/
package org.jboss.cache.buddyreplication;
-import static org.testng.AssertJUnit.assertEquals;
-import static org.testng.AssertJUnit.assertFalse;
-import static org.testng.AssertJUnit.assertNull;
-
-import java.util.ArrayList;
-import java.util.List;
-
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.cache.CacheImpl;
import org.jboss.cache.Fqn;
import org.jboss.cache.misc.TestingUtil;
+import org.jboss.cache.notifications.annotation.CacheBlocked;
+import org.jboss.cache.notifications.annotation.CacheListener;
+import org.jboss.cache.notifications.annotation.CacheUnblocked;
+import org.jboss.cache.notifications.event.Event;
+import static org.testng.AssertJUnit.*;
+import org.testng.annotations.Test;
+import java.util.ArrayList;
+import java.util.List;
+
/**
* Teststhe transfer of content under *normal* operation
*
* @author <a href="mailto:manik@jboss.org">Manik Surtani (manik(a)jboss.org)</a>
*/
+@Test (groups = {"functional"})
public class BuddyReplicationContentTest extends BuddyReplicationTestsBase
{
private String key = "key";
@@ -239,11 +242,19 @@
caches = null;
caches = createCaches(3, false, true);
+ CacheBlockListener blockListener = new CacheBlockListener();
+
+ caches.get(0).addCacheListener(blockListener);
+ caches.get(1).addCacheListener(blockListener);
+ caches.get(2).addCacheListener(blockListener);
+
caches.get(0).put("/0", "key", "value");
caches.get(1).put("/1", "key", "value");
caches.get(2).put("/2", "key", "value");
- TestingUtil.sleepThread(getSleepTimeout());
+// TestingUtil.sleepThread(getSleepTimeout());
+ //TestingUtil.sleepThread(caches.get(0).getConfiguration().getStateRetrievalTimeout() * 3);
+ blockListener.blockUntilAllCachesAreUnblocked(60000);
log.info("stopping 2");
caches.get(2).stop();
@@ -252,7 +263,9 @@
log.info("1 ** " + caches.get(1).printLockInfo());
log.info("2 ** " + caches.get(2).printLockInfo());
- TestingUtil.sleepThread(getSleepTimeout());
+// TestingUtil.sleepThread(getSleepTimeout());
+// TestingUtil.sleepThread(caches.get(0).getConfiguration().getStateRetrievalTimeout() * 3);
+ blockListener.blockUntilAllCachesAreUnblocked(60000);
assertEquals("value", caches.get(0).get("/2", "key"));
@@ -260,7 +273,9 @@
log.info("1 ** " + caches.get(1).printLockInfo());
log.info("2 ** " + caches.get(2).printLockInfo());
- TestingUtil.sleepThread(getSleepTimeout());
+// TestingUtil.sleepThread(getSleepTimeout());
+// TestingUtil.sleepThread(caches.get(0).getConfiguration().getStateRetrievalTimeout() * 3);
+ blockListener.blockUntilAllCachesAreUnblocked(60000);
caches.get(1).stop();
log.info("0 ** " + caches.get(0).printLockInfo());
@@ -285,4 +300,50 @@
assertEquals("value", caches.get(0).get("/1", "key"));
assertEquals("value", caches.get(0).get("/2", "key"));
}
+
+ @CacheListener
+ public static class CacheBlockListener
+ {
+ private int blocks = 0;
+ @CacheBlocked
+ public void processBlock(Event e)
+ {
+ if (e.isPre())
+ {
+ System.out.println(">>>>>>>> Got BLOCK on cache " + e.getCache().getLocalAddress());
+ synchronized (this)
+ {
+ blocks ++;
+ notifyAll();
+ }
+ }
+ }
+
+ @CacheUnblocked
+ public void processUnblock(Event e)
+ {
+ if (e.isPre())
+ {
+ System.out.println(">>>>>>>> Got UNBLOCK on cache " + e.getCache().getLocalAddress());
+ synchronized (this)
+ {
+ blocks --;
+ notifyAll();
+ }
+ }
+ }
+
+ public void blockUntilAllCachesAreUnblocked(long maxWait) throws InterruptedException
+ {
+ synchronized (this)
+ {
+ if (blocks != 0)
+ {
+ wait(maxWait);
+ }
+ if (blocks != 0)
+ throw new RuntimeException("Timed out waiting for unblocks. Number of blocks = " + blocks);
+ }
+ }
+ }
}
16 years, 6 months