0 Replies Latest reply on Mar 16, 2009 11:18 PM by clebert.suconic

    SimpleString.hashCode

    clebert.suconic

      Some of the Clustering failures we were having were due to SimpleString.hashCode

      I was investigating a failure on MessageRedistributionTest::testBackAndForth, and the test would fail 4 in 5 times, if running it by the first time on Eclipse.

      I was having weird issues with properties not being filled, even thought the property.add was called.

      After some investigation, the issue was related to calculating the hashCode as the hashCode is cached and most keys are constants such as PostOfficeImpl::HDR_RESET_QUEUE_DATA.

      SimpleString::hashCode had an issue on multi-threading.

      a thread would eventually use the hash being calculated but not in its final calculated value yet.


       public int hashCode()
       {
       if (hash == 0)
       {
       for (int i = 0; i < data.length; i++)
       {
       hash = (hash << 5) - hash + data; // (hash << 5) - hash is same as hash * 31
       }
       }
      
       return hash;
       }
      
      



      I have changed the method and the problem was fixed. I've changed it to make it atomic.

       public int hashCode()
       {
       if (hash == 0)
       {
       int tmphash = 0;
       for (int i = 0; i < data.length; i++)
       {
       tmphash = (tmphash << 5) - tmphash + data; // (hash << 5) - hash is same as hash * 31
       }
       hash = tmphash;
       }
      
       return hash;
       }
      
      


      I added SimpleStringTest::testMultithreadHashCode replicating this issue.