-
Notifications
You must be signed in to change notification settings - Fork 348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed #334: Sort attributes before computing the hashcode #338
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have two concerns with this solution:
- in
getAttributesFiltered
there's no real need for sorted attributes, there the motivation is not given / clear. I'm also not sure wheregetAttributesFiltered
is used (I'm on my phone). I'd find it more intuitive to do the sorting where sorted attributes are really needed. - the
ConcurrentHashMap
does not guarantee any order, therefore it could have been just luck that it worked for your cases (there's also no test that shows that it works). Using aTreeMap
for sorting attributes instead would be safe.
You are right, in |
Without further checking I'd think that instead of |
I meant i don't know how to get hashcode by sorted attributes. |
Can't you just pass the sorted attributes to |
ConcurrentMap is be required in serializeAttributes method, if the attributes change to Map , serializeAttributes method is need to modify? |
You're right, we'd have to change I stepped a bit through the code, and AFAICS the only msm external modification is needed for kryo-serializer's The So the change seems to be a bit more involved... ;-) |
This is my worry. |
I've not yet clearly understood the original observation: why the same session data was serialized differently. If we know what exactly caused this there might be another solution or workaround. |
Another solution could be to use a |
There is a test case: public class MapSerializerTest {
private Kryo _kryo;
@BeforeTest
public void setUp() throws Exception {
_kryo = new Kryo();
MapSerializer.registerSerializers(_kryo);
}
@Test
public void testDeserialize() throws Exception {
ConcurrentMap<String ,Object> obj = new ConcurrentHashMap<String ,Object>();
obj.put("iccid","12345");
obj.put("cityCode","heibei");
final byte[] serializedByObj = serialize(_kryo, obj);
final ConcurrentMap deserialized = deserialize(_kryo, serializedByObj, ConcurrentHashMap.class);
// that is ok
assertEquals(obj,deserialized);
final byte[] serializedByDeserialized = serialize(_kryo, deserialized);
// serializedByObj not equals serializedByDeserialized
assertNotEquals(serializedByObj,serializedByDeserialized);
// copy deserialized obj to a new obj
ConcurrentMap<String ,Object> obj2 = new ConcurrentHashMap<String ,Object>();
for (Object key : deserialized.keySet()) {
obj2.put((String) key,deserialized.get(key));
}
assertEquals(obj2,deserialized);
// serialize obj2
final byte[] serializedByObj2 = serialize(_kryo, obj2);
// compare serializedByObj with serializedByObj2
assertEquals(serializedByObj,serializedByObj2);
}
} I guess the problem caused by the deserialized obj, so i used a copy of deserialized attributes to compute hascode in my solution. |
Sorry, I can't follow you. Can you please rephrase what you want to say? |
The same data serialized differently. Maybe it's because of that the deserialized data is not deep equals with the original data. @Test
public void testDeserialize() throws Exception {
ConcurrentMap<String ,Object> obj = new ConcurrentHashMap<String ,Object>();
obj.put("iccid","12345");
obj.put("cityCode","heibei");
final byte[] serializedByObj = de.javakaffee.kryoserializers.KryoTest.serialize(_kryo, obj);
final ConcurrentMap deserialized = de.javakaffee.kryoserializers.KryoTest.deserialize(_kryo, serializedByObj, ConcurrentHashMap.class);
de.javakaffee.kryoserializers.KryoTest.assertEquals(obj,deserialized);
de.javakaffee.kryoserializers.KryoTest.assertDeepEquals(obj,deserialized);
} I got the error:
|
That's what we/you observed originally, and therfore is expected, isn't it? |
Yes, this not happen with |
Sort attributes by attribute key