Hello,

We have experienced thread safety issues when subclassing WritableComparator and subsequently calling WritableComparator.define. It seems that same comparator instance has its methods called from different threads. This makes the following code failing from time to time due to race condition

public class PComparator extends WritableComparator {

  ....

private DataInputBuffer buf = new DataInputBuffer(); public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {

        ... [do something that modifies buf]
   }
}
...
static {
       WritableComparator.define(PKey.class, new PComparator());
}

So, no member variables can be modified in compare without the explicit synchronization.

On the other hand, when comparator class is specified in JobConf, each thread recieives it's own instance and no race condition occurs.

I think this case should be mentioned in documentation

I can also suggest to implement another version WritableComparator.define(Class c, Class comparatorClass) which registers the comparaator class, not comparator class instance and make WritableComparator.get to instantiate comparators registered in such a way and declare old defile method deprecated

Reply via email to