AttributeSerializer conflict on HashMap data type for migrated Titan graph
JanusGraph now has a native serializer for the HashMap class and that's clashing with the custom HashMap serializer they had added to Titan (even after the serializer is refactored to use the JanusGraph packages). So when starting up against the existing DB it fails with the following:
java.lang.IllegalArgumentException: DataType has already been registered: class java.util.HashMap
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:146)
at org.janusgraph.graphdb.database.serialize.StandardSerializer.registerClassInternal(StandardSerializer.java:147)
at org.janusgraph.graphdb.database.serialize.StandardSerializer.registerClass(StandardSerializer.java:141)
at org.janusgraph.graphdb.configuration.RegisteredAttributeClass.registerWith(RegisteredAttributeClass.java:43)
at org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.getSerializer(GraphDatabaseConfiguration.java:1869)
at org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.getSerializer(GraphDatabaseConfiguration.java:1862)
at org.janusgraph.graphdb.database.StandardJanusGraph.<init>(StandardJanusGraph.java:139)
at org.janusgraph.core.JanusGraphFactory.open(JanusGraphFactory.java:107)
at org.janusgraph.core.JanusGraphFactory.open(JanusGraphFactory.java:97)
at org.janusgraph.core.JanusGraphFactory$Builder.open(JanusGraphFactory.java:152)
...
Currently there is no way to unregister the default HashMap serializer from StandardSerializer or a way to override it with the user's preferred serializer.
Another solution would be to move HashMap serialization from StandardSerializer to test configuration. This would keep TinkerPop tests (e.g. GraphComputerTest, PropertyTest, etc.) passing. But given the presence of these tests I'm wondering if HashMap serialization might be required for some operations (e.g. OLAP) in more recent versions of TinkerPop in which case going this route, while solving this version migration issue, might cause problems for others. I can do some testing on this.
@sjudeng Interesting approach. I hadn't considered that idea -- @smcquillan thoughts?
@sjudeng, @pluradj Sounds like it could work, but I assume moving it into test configuration would mean it was no longer available by default to new users of JanusGraph.
If so then I’d be against it because we found it very useful to be able to store nested JSON within the graph. The fact that we couldn’t do this with Titan was what prompted us to add the custom HashMap serializer to Titan in the first place. Also, would we not run the risk that some Janus users are already exploiting it and we’d cause them problems if we removed it now.
I’d been thinking we could add a check at start up to see if there was an existing HashMap serializer already defined. If one was found then use that, but if not then register the standard one as we do today.
@smcquillan This update is only on master/0.2-SNAPSHOT (e.g. not in 0.1.x) so I'm not worried about any major impacts to users. I do agree generic HashMap serialization might be useful feature generally. Can you tell which serializer is loaded first (e.g. your custom one or the current native one)?
Update: Nevermind about the "not on 0.1.x", it was part of the TinkerPop 3.2.3 updates so it is in there.
@sjudeng The native one is loaded first in the StandardSerializer constructor.
@smcquillan Is your custom serializer significantly different or better than what JanusGraph is using? If not, it might be nice to enable a away to migrate from the custom Titan serializer to the default JanusGraph serializer.
@pluradj you might be better placed to say whether or not our customer one is better ;) its based upon one that you wrote titan-attribute-serializer.
Also we've no particular preference as to which gets used, only that we can continue to read the data previously written. If our new customers were to use the StandardSerializer instead I don't see that as an issue. But we many need to continue to use the old one for existing customers if the two are not compatible.
From the stack trace you can see that the method which adds in the custom serializers is StandardSerializer.registerClass(). I’m thinking that we’ll not have used the default HashMap serializer yet and so if we spot an attempt to add a custom one in this method then de-register the default one and then register the custom serializer as normal.
The code changes are quite simple, just six new lines:
public synchronized <V> void registerClass(int registrationNo, Class<V> datatype, AttributeSerializer<V> serializer) {
Preconditions.checkArgument(registrationNo >= 0 && registrationNo < MAX_REGISTRATION_NO, "Registration number" +
" out of range [0,%s]: %s", MAX_REGISTRATION_NO, registrationNo);
+
+ if (datatype == HashMap.class)
+ {
+ Integer hashMapRegNo = registrations.inverse().get(normalizeDataType(HashMap.class));
+ registrations.remove(hashMapRegNo);
+ handlers.remove(datatype);
+ }
+
registerClassInternal(CLASS_REGISTRATION_OFFSET + registrationNo, datatype, serializer);
}
And when I dropped a jar file into our build with this change in place it was able to read the data which had been written by Titan 1.0 with the custom serializer. So, it’s looking like a promising way to resolve the issue (though I can’t claim to be familiar enough with the JanusGraph code base yet to know if there will be any unwanted side effects)
@smcquillan Your approach looks good to me. I was worried it was going to be messy so glad it might be pretty simple. Only thing to do (and this is a surefire way to get more familiar with the code) is find relevant unit test class and add some cases to test this. I'll unassign myself from this issue since I agree this is better than moving the serializer to test configuration at this point.
The current native HashMap serializer was as simple as I could make it and uses Java serialization (ObjectOutputStream). For performance alone I'd be interested in yours if you guys think it's better. If I only would have done the test config approach to begin with on this we could have approached the HashMap native serializer as a separate feature and spent a little more time on it ...
Thanks @sjudeng. If someone wants to assign the issue to me then I'm happy to have a go at adding some test cases for this and try to get it into the next version. Probably need some pointers on how we use Git and branching in JanusGraph though as it s probably different to how we're using it in ITSM.
You can go ahead @smcquillan -- check out the details on JanusGraph specific Git flow in the CONTRIBUTING.md and let us know if you run into any problems. Make sure you sign your commit with the same email found in the CLA.
@sjudeng, @pluradj pushed the proposed fix up along with a unit test which checks a custom HashMap can be added. For the unit test I just extended the SerializableSerializer class to add an extra property into the HashMap when its deserialised, then check for this property which would only be present if the custom serialiser had been used.
Let me know if I've set up my fork etc correctly and shall I just go ahead and create a pull request next?
@smcquillan Yes please go ahead and create the PR and we can iterate from there. Thanks for taking this on!
Not sure why but my pull requests are coming with the "cla: no" tag. At first I thought it was because my global Git user was coming up as "Scott McQuillan" so I tried again using "smcquillan" and amended the commit as per CONTRIBUTING.md but that also came up with "cla: no"