[jboss-dev-forums] [Design of Messaging on JBoss (Messaging/JBoss)] - SimpleString.hashCode

clebert.suconic@jboss.com do-not-reply at jboss.com
Mon Mar 16 23:18:11 EDT 2009


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 in 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.




View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4218468#4218468

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4218468



More information about the jboss-dev-forums mailing list