hazelcast/hazelcast — pull request #3136 by serkan-ozal (2014-08-02 22:14:30+00:00)
Title: fix for #3090 and #3091

Cause of these issues is that there can be a synchronization problem for initializing attribute type and using attribute type for converting searched value.

Note: I could only reproduced this issue wit stabilizer with settings mentioned by @Danny-Hazelcast

Cluster profile: Medium
Stabilizer test: com.hazelcast.stabilizer.tests.map.MapIndexTest,
Hazelcast version: 3.3-RC3

Without synchronization around attribute type, Thread-1 can see attribute type as null since there is no added index and doesn't convert searched value so uses it as raw (String). Then this thread may be deactivated, another thread (Thread-2) may take a change to run for saving an index and updates attribute type. Then Thread-1 may be activated again and iterates over records on index store. In this case, Thread-1 didn't convert searched value to expected value type and iteration over indexed elements can cause class cast exception. To handle this case, synchronization is implemented with locks around attribute type.

In addition, all methods converting searched values and saving index can be defined as synchronized and this technique solves the issue, but I think this is not an efficient way to making synchronized for all cases since synchronization is required only when attribute type is null. Because in that case, there is no metadata to convert searched value.