Value of StandardDurationFieldType.hashCode is non-deterministic

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Value of StandardDurationFieldType.hashCode is non-deterministic

Edward Ruggeri
Hey joda devs!,

I noticed that the hashCode of identical Periods varies across JVM
runs.  I traced this to StandardDurationFieldType, which doesn't
overload the default hashCode method.  Luckily, there is only one
instance of each distinct StandardDurationFieldType.  But if you
relaunch the JVM, the same DurationFieldType will map to different
hashCode values.

If we agree that deterministic behavior from hashCode is desirable, I
will write and submit a patch in the next day.  I'll just override
hashCode to return a hash of the "ordinal" byte specifying the
DurationFieldType.

-- Ned Ruggeri

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure
contains a definitive record of customers, application performance,
security threats, fraudulent activity, and more. Splunk takes this
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Value of StandardDurationFieldType.hashCode is non-deterministic

jodastephen
Hi,
I've made the change as you suggested, thanks!
Stephen

On 26 November 2011 01:43, Edward Ruggeri <[hidden email]> wrote:

> Hey joda devs!,
>
> I noticed that the hashCode of identical Periods varies across JVM
> runs.  I traced this to StandardDurationFieldType, which doesn't
> overload the default hashCode method.  Luckily, there is only one
> instance of each distinct StandardDurationFieldType.  But if you
> relaunch the JVM, the same DurationFieldType will map to different
> hashCode values.
>
> If we agree that deterministic behavior from hashCode is desirable, I
> will write and submit a patch in the next day.  I'll just override
> hashCode to return a hash of the "ordinal" byte specifying the
> DurationFieldType.
>
> -- Ned Ruggeri
>
> ------------------------------------------------------------------------------
> All the data continuously generated in your IT infrastructure
> contains a definitive record of customers, application performance,
> security threats, fraudulent activity, and more. Splunk takes this
> data and makes sense of it. IT sense. And common sense.
> http://p.sf.net/sfu/splunk-novd2d
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure
contains a definitive record of customers, application performance,
security threats, fraudulent activity, and more. Splunk takes this
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Value of StandardDurationFieldType.hashCode is non-deterministic

Edward Ruggeri
Thanks for the work, Stephen!

-- Ned Ruggeri

On Sun, Dec 4, 2011 at 3:46 AM, Stephen Colebourne <[hidden email]> wrote:

> Hi,
> I've made the change as you suggested, thanks!
> Stephen
>
> On 26 November 2011 01:43, Edward Ruggeri <[hidden email]> wrote:
>> Hey joda devs!,
>>
>> I noticed that the hashCode of identical Periods varies across JVM
>> runs.  I traced this to StandardDurationFieldType, which doesn't
>> overload the default hashCode method.  Luckily, there is only one
>> instance of each distinct StandardDurationFieldType.  But if you
>> relaunch the JVM, the same DurationFieldType will map to different
>> hashCode values.
>>
>> If we agree that deterministic behavior from hashCode is desirable, I
>> will write and submit a patch in the next day.  I'll just override
>> hashCode to return a hash of the "ordinal" byte specifying the
>> DurationFieldType.
>>
>> -- Ned Ruggeri
>>
>> ------------------------------------------------------------------------------
>> All the data continuously generated in your IT infrastructure
>> contains a definitive record of customers, application performance,
>> security threats, fraudulent activity, and more. Splunk takes this
>> data and makes sense of it. IT sense. And common sense.
>> http://p.sf.net/sfu/splunk-novd2d
>> _______________________________________________
>> Joda-interest mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/joda-interest
>
> ------------------------------------------------------------------------------
> All the data continuously generated in your IT infrastructure
> contains a definitive record of customers, application performance,
> security threats, fraudulent activity, and more. Splunk takes this
> data and makes sense of it. IT sense. And common sense.
> http://p.sf.net/sfu/splunk-novd2d
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest

------------------------------------------------------------------------------
All the data continuously generated in your IT infrastructure
contains a definitive record of customers, application performance,
security threats, fraudulent activity, and more. Splunk takes this
data and makes sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-novd2d
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Value of StandardDurationFieldType.hashCode is non-deterministic

Greg Inozemtsev
In reply to this post by Edward Ruggeri
Hello

I noticed that the latest change to the hashCode for standard duration
fields introduces collisions in PeriodType.  Because the hashCode of a
PeriodType is the sum of the hashCodes of its components, a PeriodType
with a Week and Month will have the same hashCode as one with Seconds,
and so on.

The following patch makes the hashCode a bitmask, eliminating the
collisions.  A test is included.



diff -urp joda-time-2.0.orig/src/main/java/org/joda/time/DurationFieldType.java
joda-time-2.0/src/main/java/org/joda/time/DurationFieldType.java
--- joda-time-2.0.orig/src/main/java/org/joda/time/DurationFieldType.java 2011-12-06
05:03:22.193579179 -0500
+++ joda-time-2.0/src/main/java/org/joda/time/DurationFieldType.java 2011-12-06
05:46:07.713317900 -0500
@@ -275,7 +275,7 @@ public abstract class DurationFieldType
         /** @inheritdoc */
         @Override
         public int hashCode() {
-            return iOrdinal;
+            return (1 << iOrdinal);
         }

         public DurationField getField(Chronology chronology) {
diff -urp joda-time-2.0.orig/src/test/java/org/joda/time/TestPeriodType.java
joda-time-2.0/src/test/java/org/joda/time/TestPeriodType.java
--- joda-time-2.0.orig/src/test/java/org/joda/time/TestPeriodType.java 2011-12-06
01:56:23.390957347 -0500
+++ joda-time-2.0/src/test/java/org/joda/time/TestPeriodType.java 2011-12-06
05:48:17.881963372 -0500
@@ -593,6 +593,21 @@ public class TestPeriodType extends Test
         }
     }

+    public void testForFields7() throws Exception {
+        DurationFieldType[] types = new DurationFieldType[] {
+            DurationFieldType.weeks(),
+            DurationFieldType.months(),
+        };
+        DurationFieldType[] types2 = new DurationFieldType[] {
+            DurationFieldType.seconds(),
+        };
+        PeriodType type = PeriodType.forFields(types);
+        PeriodType type2 = PeriodType.forFields(types2);
+        assertEquals(false, type == type2);
+        assertEquals(false, type.equals(type2);
+        assertEquals(false, type.hashCode() == type2.hashCode());
+    }
+
     //-----------------------------------------------------------------------
     public void testMaskYears() throws Exception {
         PeriodType type = PeriodType.standard().withYearsRemoved();

------------------------------------------------------------------------------
Cloud Services Checklist: Pricing and Packaging Optimization
This white paper is intended to serve as a reference, checklist and point of
discussion for anyone considering optimizing the pricing and packaging model
of a cloud services business. Read Now!
http://www.accelacomm.com/jaw/sfnl/114/51491232/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Value of StandardDurationFieldType.hashCode is non-deterministic

jodastephen
Agreed, the shift is a better hashcode. Pushed, thanks
Stephen

On 6 December 2011 10:52, Greg Inozemtsev <[hidden email]> wrote:

> Hello
>
> I noticed that the latest change to the hashCode for standard duration
> fields introduces collisions in PeriodType.  Because the hashCode of a
> PeriodType is the sum of the hashCodes of its components, a PeriodType
> with a Week and Month will have the same hashCode as one with Seconds,
> and so on.
>
> The following patch makes the hashCode a bitmask, eliminating the
> collisions.  A test is included.
>
>
>
> diff -urp joda-time-2.0.orig/src/main/java/org/joda/time/DurationFieldType.java
> joda-time-2.0/src/main/java/org/joda/time/DurationFieldType.java
> --- joda-time-2.0.orig/src/main/java/org/joda/time/DurationFieldType.java       2011-12-06
> 05:03:22.193579179 -0500
> +++ joda-time-2.0/src/main/java/org/joda/time/DurationFieldType.java    2011-12-06
> 05:46:07.713317900 -0500
> @@ -275,7 +275,7 @@ public abstract class DurationFieldType
>         /** @inheritdoc */
>         @Override
>         public int hashCode() {
> -            return iOrdinal;
> +            return (1 << iOrdinal);
>         }
>
>         public DurationField getField(Chronology chronology) {
> diff -urp joda-time-2.0.orig/src/test/java/org/joda/time/TestPeriodType.java
> joda-time-2.0/src/test/java/org/joda/time/TestPeriodType.java
> --- joda-time-2.0.orig/src/test/java/org/joda/time/TestPeriodType.java  2011-12-06
> 01:56:23.390957347 -0500
> +++ joda-time-2.0/src/test/java/org/joda/time/TestPeriodType.java       2011-12-06
> 05:48:17.881963372 -0500
> @@ -593,6 +593,21 @@ public class TestPeriodType extends Test
>         }
>     }
>
> +    public void testForFields7() throws Exception {
> +        DurationFieldType[] types = new DurationFieldType[] {
> +            DurationFieldType.weeks(),
> +            DurationFieldType.months(),
> +        };
> +        DurationFieldType[] types2 = new DurationFieldType[] {
> +            DurationFieldType.seconds(),
> +        };
> +        PeriodType type = PeriodType.forFields(types);
> +        PeriodType type2 = PeriodType.forFields(types2);
> +        assertEquals(false, type == type2);
> +        assertEquals(false, type.equals(type2);
> +        assertEquals(false, type.hashCode() == type2.hashCode());
> +    }
> +
>     //-----------------------------------------------------------------------
>     public void testMaskYears() throws Exception {
>         PeriodType type = PeriodType.standard().withYearsRemoved();
>
> ------------------------------------------------------------------------------
> Cloud Services Checklist: Pricing and Packaging Optimization
> This white paper is intended to serve as a reference, checklist and point of
> discussion for anyone considering optimizing the pricing and packaging model
> of a cloud services business. Read Now!
> http://www.accelacomm.com/jaw/sfnl/114/51491232/
> _______________________________________________
> Joda-interest mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/joda-interest

------------------------------------------------------------------------------
Cloud Services Checklist: Pricing and Packaging Optimization
This white paper is intended to serve as a reference, checklist and point of
discussion for anyone considering optimizing the pricing and packaging model
of a cloud services business. Read Now!
http://www.accelacomm.com/jaw/sfnl/114/51491232/
_______________________________________________
Joda-interest mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/joda-interest
Loading...