Uploaded image for project: 'ONOS'
  1. ONOS
  2. ONOS-4394

ConsistentMap does not compare key equality properly

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed (View Workflow)
    • Priority: Major
    • Resolution: Done
    • Affects Version/s: 1.6.0
    • Fix Version/s: 1.6.0
    • Component/s: None
    • Labels:
      None
    • Environment:

      commit 28d67687d0712b2cc93f81442130c690277c95a6

    • Story Points:
      3
    • Epic Link:

      Description

      First we create a Key class and override the equals() to only compare part of the variables.

      private class Key {
              int a;
              int b;
      
              Key(int a, int b) {
                  this.a = a;
                  this.b = b;
              }
      
              @Override
              public boolean equals(Object o) {
                  if (this == o) {
                      return true;
                  }
                  if (!(o instanceof Key)) {
                      return false;
                  }
                  Key that = (Key) o;
                  return (Objects.equals(this.a, that.a));
              }
      
              @Override
              public int hashCode() {
                  return Objects.hash(a);
              }
      
          }
      

      Java HashMap will use the overridden equals(). If we do the following

              HashMap<Key, Integer> map = new HashMap<>();
              map.put(new Key(1, 2), 3);
              map.put(new Key(1, 4), 5);
      

      These two keys will be recognized as the same one and thus the second statement only updates the value from 3 to 5 instead of adding a new map entry.

      However,
      ConsistentMap doesn't follow this behavior. If we do the following

      ConsistentMap<Key, Integer> cmap;
              cmap = storageService
                      .<Key, Integer>consistentMapBuilder()
                      .withName("test-map")
                      .withSerializer(Serializer.using(new KryoNamespace.Builder()
                              .register(KryoNamespaces.API)
                              .register(Key.class).build()))
                      .build();
              cmap.put(new Key(1, 2), 3);
              cmap.put(new Key(1, 4), 5);
      

      ConsistentMap will think these are two different keys. The second statement will put an extra entry instead of updating the value.

      It seems ConsistentMap doesn't use equals() properly when calculating the equality of the keys.
      I don't think this is an expected behavior.

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            charles Charles Chan
            Reporter:
            charles Charles Chan
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: