You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@brooklyn.apache.org by he...@apache.org on 2014/07/03 11:46:57 UTC

[5/6] git commit: Deserialize: discard dangling ref in ImmutableSet

Deserialize: discard dangling ref in ImmutableSet

- If didn’t then it would fail to deserialise, which would cause 
  entire file to fail to deserialise.
  The dangling ref will have been logged plus handled by the 
  RebindExceptionHandler, so mostly harmless ignoring it here.


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

Branch: refs/heads/master
Commit: e518690e53810fd460c47b57088a2a7ebba9f628
Parents: 258bd7d
Author: Aled Sage <al...@gmail.com>
Authored: Tue Jun 24 20:41:13 2014 +0100
Committer: Aled Sage <al...@gmail.com>
Committed: Thu Jun 26 15:48:43 2014 +0100

----------------------------------------------------------------------
 .../util/xstream/ImmutableListConverter.java    | 14 ++--
 .../util/xstream/ImmutableMapConverter.java     | 38 +++++++++++
 .../util/xstream/ImmutableSetConverter.java     | 36 ++++++++++
 .../brooklyn/util/xstream/XmlSerializer.java    |  2 +
 .../persister/XmlMementoSerializerTest.java     | 70 +++++++++++++++++---
 5 files changed, 144 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e518690e/core/src/main/java/brooklyn/util/xstream/ImmutableListConverter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/xstream/ImmutableListConverter.java b/core/src/main/java/brooklyn/util/xstream/ImmutableListConverter.java
index fdd7ee8..e0e42ae 100644
--- a/core/src/main/java/brooklyn/util/xstream/ImmutableListConverter.java
+++ b/core/src/main/java/brooklyn/util/xstream/ImmutableListConverter.java
@@ -1,9 +1,11 @@
 package brooklyn.util.xstream;
 
-import java.util.ArrayList;
 import java.util.Collection;
 
+import com.google.common.base.Predicates;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
 import com.thoughtworks.xstream.converters.UnmarshallingContext;
 import com.thoughtworks.xstream.converters.collections.CollectionConverter;
 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
@@ -23,12 +25,12 @@ public class ImmutableListConverter extends CollectionConverter {
     // marshalling is the same
     // so is unmarshalling the entries
 
-    // only difference is creating the overarching collection, which we do after the fact
-    // (optimizing format on disk as opposed to in-memory)
+    // only differences are creating the overarching collection, which we do after the fact
+    // (optimizing format on disk as opposed to in-memory), and we discard null values 
+    // to avoid failing entirely.
     public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
-        Collection collection = new ArrayList();
+        Collection<?> collection = Lists.newArrayList();
         populateCollection(reader, context, collection);
-        return ImmutableList.copyOf(collection);
+        return ImmutableList.copyOf(Iterables.filter(collection, Predicates.notNull()));
     }
-
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e518690e/core/src/main/java/brooklyn/util/xstream/ImmutableMapConverter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/xstream/ImmutableMapConverter.java b/core/src/main/java/brooklyn/util/xstream/ImmutableMapConverter.java
new file mode 100644
index 0000000..27cb049
--- /dev/null
+++ b/core/src/main/java/brooklyn/util/xstream/ImmutableMapConverter.java
@@ -0,0 +1,38 @@
+package brooklyn.util.xstream;
+
+import java.util.Map;
+import java.util.Map.Entry;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
+import com.thoughtworks.xstream.converters.UnmarshallingContext;
+import com.thoughtworks.xstream.io.HierarchicalStreamReader;
+import com.thoughtworks.xstream.mapper.Mapper;
+
+public class ImmutableMapConverter extends MapConverter {
+
+    public ImmutableMapConverter(Mapper mapper) {
+        super(mapper);
+    }
+
+    @Override
+    public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
+        return ImmutableMap.class.isAssignableFrom(type);
+    }
+
+    // marshalling is the same
+    // so is unmarshalling the entries
+
+    // only differences are creating the overarching collection, which we do after the fact
+    // (optimizing format on disk as opposed to in-memory), and we discard null key/values 
+    // to avoid failing entirely.
+    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
+        Map<?, ?> map = Maps.newLinkedHashMap();
+        populateMap(reader, context, map);
+        return ImmutableMap.copyOf(Maps.filterEntries(map, new Predicate<Map.Entry<?,?>>() {
+                @Override public boolean apply(Entry<?, ?> input) {
+                    return input != null && input.getKey() != null && input.getValue() != null;
+                }}));
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e518690e/core/src/main/java/brooklyn/util/xstream/ImmutableSetConverter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/xstream/ImmutableSetConverter.java b/core/src/main/java/brooklyn/util/xstream/ImmutableSetConverter.java
new file mode 100644
index 0000000..757c643
--- /dev/null
+++ b/core/src/main/java/brooklyn/util/xstream/ImmutableSetConverter.java
@@ -0,0 +1,36 @@
+package brooklyn.util.xstream;
+
+import java.util.Collection;
+
+import com.google.common.base.Predicates;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Lists;
+import com.thoughtworks.xstream.converters.UnmarshallingContext;
+import com.thoughtworks.xstream.converters.collections.CollectionConverter;
+import com.thoughtworks.xstream.io.HierarchicalStreamReader;
+import com.thoughtworks.xstream.mapper.Mapper;
+
+public class ImmutableSetConverter extends CollectionConverter {
+
+    public ImmutableSetConverter(Mapper mapper) {
+        super(mapper);
+    }
+
+    @Override
+    public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
+        return ImmutableSet.class.isAssignableFrom(type);
+    }
+
+    // marshalling is the same
+    // so is unmarshalling the entries
+
+    // only differences are creating the overarching collection, which we do after the fact
+    // (optimizing format on disk as opposed to in-memory), and we discard null values 
+    // to avoid failing entirely.
+    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
+        Collection<?> collection = Lists.newArrayList();
+        populateCollection(reader, context, collection);
+        return ImmutableSet.copyOf(Iterables.filter(collection, Predicates.notNull()));
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e518690e/core/src/main/java/brooklyn/util/xstream/XmlSerializer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/xstream/XmlSerializer.java b/core/src/main/java/brooklyn/util/xstream/XmlSerializer.java
index 1553ff4..334dc7e 100644
--- a/core/src/main/java/brooklyn/util/xstream/XmlSerializer.java
+++ b/core/src/main/java/brooklyn/util/xstream/XmlSerializer.java
@@ -46,6 +46,8 @@ public class XmlSerializer<T> {
         
         xstream.aliasType("ImmutableList", ImmutableList.class);
         xstream.registerConverter(new ImmutableListConverter(xstream.getMapper()));
+        xstream.registerConverter(new ImmutableSetConverter(xstream.getMapper()));
+        xstream.registerConverter(new ImmutableMapConverter(xstream.getMapper()));
 
         xstream.registerConverter(new EnumCaseForgivingConverter());
         xstream.registerConverter(new Inet4AddressConverter());

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/e518690e/core/src/test/java/brooklyn/entity/rebind/persister/XmlMementoSerializerTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/entity/rebind/persister/XmlMementoSerializerTest.java b/core/src/test/java/brooklyn/entity/rebind/persister/XmlMementoSerializerTest.java
index 22ccec4..c8fcfd6 100644
--- a/core/src/test/java/brooklyn/entity/rebind/persister/XmlMementoSerializerTest.java
+++ b/core/src/test/java/brooklyn/entity/rebind/persister/XmlMementoSerializerTest.java
@@ -18,6 +18,7 @@ import org.testng.annotations.Test;
 import brooklyn.entity.Entity;
 import brooklyn.entity.basic.ApplicationBuilder;
 import brooklyn.entity.basic.Entities;
+import brooklyn.entity.proxying.EntitySpec;
 import brooklyn.location.Location;
 import brooklyn.location.LocationSpec;
 import brooklyn.management.ManagementContext;
@@ -25,6 +26,7 @@ import brooklyn.mementos.BrooklynMementoPersister.LookupContext;
 import brooklyn.policy.Enricher;
 import brooklyn.policy.Policy;
 import brooklyn.test.entity.TestApplication;
+import brooklyn.test.entity.TestEntity;
 import brooklyn.util.collections.MutableList;
 import brooklyn.util.collections.MutableMap;
 import brooklyn.util.collections.MutableSet;
@@ -109,7 +111,7 @@ public class XmlMementoSerializerTest {
         final TestApplication app = ApplicationBuilder.newManagedApp(TestApplication.class);
         ManagementContext managementContext = app.getManagementContext();
         try {
-            serializer.setLookupContext(new LookupContextImpl(managementContext, ImmutableList.of(app), ImmutableList.<Location>of(), ImmutableList.<Policy>of(), ImmutableList.<Enricher>of()));
+            serializer.setLookupContext(new LookupContextImpl(managementContext, ImmutableList.of(app), ImmutableList.<Location>of(), ImmutableList.<Policy>of(), ImmutableList.<Enricher>of(), true));
             assertSerializeAndDeserialize(app);
         } finally {
             Entities.destroyAll(managementContext);
@@ -123,7 +125,7 @@ public class XmlMementoSerializerTest {
         try {
             @SuppressWarnings("deprecation")
             final Location loc = managementContext.getLocationManager().createLocation(LocationSpec.create(brooklyn.location.basic.SimulatedLocation.class));
-            serializer.setLookupContext(new LookupContextImpl(managementContext, ImmutableList.<Entity>of(), ImmutableList.of(loc), ImmutableList.<Policy>of(), ImmutableList.<Enricher>of()));
+            serializer.setLookupContext(new LookupContextImpl(managementContext, ImmutableList.<Entity>of(), ImmutableList.of(loc), ImmutableList.<Policy>of(), ImmutableList.<Enricher>of(), true));
             assertSerializeAndDeserialize(loc);
         } finally {
             Entities.destroyAll(managementContext);
@@ -131,12 +133,38 @@ public class XmlMementoSerializerTest {
     }
 
     @Test
+    public void testImmutableCollectionsWithDanglingEntityRef() throws Exception {
+        // If there's a dangling entity in an ImmutableList etc, then discard it entirely.
+        // If we try to insert null then it fails, breaking the deserialization of that entire file.
+        
+        final TestApplication app = ApplicationBuilder.newManagedApp(TestApplication.class);
+        final TestEntity entity = app.createAndManageChild(EntitySpec.create(TestEntity.class));
+        ManagementContext managementContext = app.getManagementContext();
+        try {
+            serializer.setLookupContext(new LookupContextImpl(managementContext, ImmutableList.of(app), ImmutableList.<Location>of(), ImmutableList.<Policy>of(), ImmutableList.<Enricher>of(), false));
+            
+            List<?> resultList = serializeAndDeserialize(ImmutableList.of(app, entity));
+            assertEquals(resultList, ImmutableList.of(app));
+            
+            Set<?> resultSet = serializeAndDeserialize(ImmutableSet.of(app, entity));
+            assertEquals(resultSet, ImmutableSet.of(app));
+            
+            Map<?, ?> resultMap = serializeAndDeserialize(ImmutableMap.of(app, "appval", "appkey", app, entity, "entityval", "entityKey", entity));
+            assertEquals(resultMap, ImmutableMap.of(app, "appval", "appkey", app));
+
+        } finally {
+            Entities.destroyAll(managementContext);
+        }
+    }
+
+
+    @Test
     public void testFieldReffingEntity() throws Exception {
         final TestApplication app = ApplicationBuilder.newManagedApp(TestApplication.class);
         ReffingEntity reffer = new ReffingEntity(app);
         ManagementContext managementContext = app.getManagementContext();
         try {
-            serializer.setLookupContext(new LookupContextImpl(managementContext, ImmutableList.of(app), ImmutableList.<Location>of(), ImmutableList.<Policy>of(), ImmutableList.<Enricher>of()));
+            serializer.setLookupContext(new LookupContextImpl(managementContext, ImmutableList.of(app), ImmutableList.<Location>of(), ImmutableList.<Policy>of(), ImmutableList.<Enricher>of(), true));
             ReffingEntity reffer2 = assertSerializeAndDeserialize(reffer);
             assertEquals(reffer2.entity, app);
         } finally {
@@ -150,7 +178,7 @@ public class XmlMementoSerializerTest {
         ReffingEntity reffer = new ReffingEntity((Object)app);
         ManagementContext managementContext = app.getManagementContext();
         try {
-            serializer.setLookupContext(new LookupContextImpl(managementContext, ImmutableList.of(app), ImmutableList.<Location>of(), ImmutableList.<Policy>of(), ImmutableList.<Enricher>of()));
+            serializer.setLookupContext(new LookupContextImpl(managementContext, ImmutableList.of(app), ImmutableList.<Location>of(), ImmutableList.<Policy>of(), ImmutableList.<Enricher>of(), true));
             ReffingEntity reffer2 = assertSerializeAndDeserialize(reffer);
             assertEquals(reffer2.obj, app);
         } finally {
@@ -186,15 +214,23 @@ public class XmlMementoSerializerTest {
         return (T) deserialized;
     }
 
+    @SuppressWarnings("unchecked")
+    private <T> T serializeAndDeserialize(T obj) throws Exception {
+        String serializedForm = serializer.toString(obj);
+        System.out.println("serializedForm="+serializedForm);
+        return (T) serializer.fromString(serializedForm);
+    }
+
     static class LookupContextImpl implements LookupContext {
         private final ManagementContext mgmt;
         private final Map<String, Entity> entities;
         private final Map<String, Location> locations;
         private final Map<String, Policy> policies;
         private final Map<String, Enricher> enrichers;
+        private final boolean failOnDangling;
 
         LookupContextImpl(ManagementContext mgmt, Iterable<? extends Entity> entities, Iterable<? extends Location> locations,
-                Iterable<? extends Policy> policies, Iterable<? extends Enricher> enrichers) {
+                Iterable<? extends Policy> policies, Iterable<? extends Enricher> enrichers, boolean failOnDangling) {
             this.mgmt = mgmt;
             this.entities = Maps.newLinkedHashMap();
             this.locations = Maps.newLinkedHashMap();
@@ -204,14 +240,16 @@ public class XmlMementoSerializerTest {
             for (Location location : locations) this.locations.put(location.getId(), location);
             for (Policy policy : policies) this.policies.put(policy.getId(), policy);
             for (Enricher enricher : enrichers) this.enrichers.put(enricher.getId(), enricher);
+            this.failOnDangling = failOnDangling;
         }
         LookupContextImpl(ManagementContext mgmt, Map<String,? extends Entity> entities, Map<String,? extends Location> locations,
-                Map<String,? extends Policy> policies, Map<String,? extends Enricher> enrichers) {
+                Map<String,? extends Policy> policies, Map<String,? extends Enricher> enrichers, boolean failOnDangling) {
             this.mgmt = mgmt;
             this.entities = ImmutableMap.copyOf(entities);
             this.locations = ImmutableMap.copyOf(locations);
             this.policies = ImmutableMap.copyOf(policies);
             this.enrichers = ImmutableMap.copyOf(enrichers);
+            this.failOnDangling = failOnDangling;
         }
         @Override public ManagementContext lookupManagementContext() {
             return mgmt;
@@ -220,25 +258,37 @@ public class XmlMementoSerializerTest {
             if (entities.containsKey(id)) {
                 return entities.get(id);
             }
-            throw new NoSuchElementException("no entity with id "+id+"; contenders are "+entities.keySet());
+            if (failOnDangling) {
+                throw new NoSuchElementException("no entity with id "+id+"; contenders are "+entities.keySet());
+            }
+            return null;
         }
         @Override public Location lookupLocation(String id) {
             if (locations.containsKey(id)) {
                 return locations.get(id);
             }
-            throw new NoSuchElementException("no location with id "+id+"; contenders are "+locations.keySet());
+            if (failOnDangling) {
+                throw new NoSuchElementException("no location with id "+id+"; contenders are "+locations.keySet());
+            }
+            return null;
         }
         @Override public Policy lookupPolicy(String id) {
             if (policies.containsKey(id)) {
                 return policies.get(id);
             }
-            throw new NoSuchElementException("no policy with id "+id+"; contenders are "+policies.keySet());
+            if (failOnDangling) {
+                throw new NoSuchElementException("no policy with id "+id+"; contenders are "+policies.keySet());
+            }
+            return null;
         }
         @Override public Enricher lookupEnricher(String id) {
             if (enrichers.containsKey(id)) {
                 return enrichers.get(id);
             }
-            throw new NoSuchElementException("no enricher with id "+id+"; contenders are "+enrichers.keySet());
+            if (failOnDangling) {
+                throw new NoSuchElementException("no enricher with id "+id+"; contenders are "+enrichers.keySet());
+            }
+            return null;
         }
     };
 }