delphix / virtualization-sdk

Delphix vSDK.
Apache License 2.0
7 stars 25 forks source link

DLPX-79760 SnapshotDefinitionDataPaths object is not hashable since py2-3 transition #430

Closed vimleshmishra closed 2 years ago

vimleshmishra commented 2 years ago

Problem:

Since python 2-3 transition, in DB2 plugin, generated.definition subclasses are no longer hashable. Since in one of the workflows in DB2, we add a Definition subclass (SnapshotDefinitionDataPaths) to a set to make sure we don’t record duplicates, we started getting this error,

Traceback (most recent call last): File "plugin/platform_server.py", line 66, in _run_helper response = internal_func(request) File "./_linked.py", line 300, in _internal_staged_pre_snapshot File "./plugin_runner.py", line 63, in linked_pre_snapshot File "./plugin_operations/staging.py", line 53, in pre_snapshot File "./plugin_libs/linked_source.py", line 2449, in staging_push_pre_snapshot_validation File "./plugin_libs/db2_operations.py", line 734, in get_data_paths File "./plugin_libs/db2_operations.py", line 865, in get_all_paths_from_db2dart TypeError: unhashable type: 'SnapshotDefinitionDataPaths'

The same codeflow works just fine in older plugin that used python2.

Solution:

We have not implemented hash method in our Model class while we have implemented equals as below

def __eq__(self, other):
        """Returns true if both objects are equal"""
        return self.to_dict() == other.to_dict()

In Python 3

A class that overrides eq() and does not define hash() will have its hash() implicitly set to None. When the hash() method of a class is None, instances of the class will raise an appropriate TypeError

In Python 2

User-defined classes have cmp() and hash() methods by default; with them, all objects compare unequal (except with themselves) and x.hash() returns a result derived from id(x).

Due to this change in behavior, we will not be able to store the generated class in any hash-supported data structure like dictionary OR set and we will get unhashable type errors.

To solve this we have tried to override the hash() similar to equals() method.

Please note that we have changed the behavior of hash() with this diff where earlier the default hash() method was using id(x) and if two objects are equals as per equal() contract they might not have the same hashcode but with this diff, we are using the properties of the object and if two objects are equals as per equal() contract they will have the same hashcode.

Testing

Manually reproduced this issue and verified that fix is working.

git blackbox -s appdata_python_samples --extra-params="-p virt-sdk-repo=https://github.com/vimleshmishra/virtualization-sdk.git -p virt-sdk-branch=develop"

git blackbox -s appdata_basic -c APPDATA_PYTHON_DIRECT_CENTOS73 --extra-params="-p virt-sdk-repo=https://github.com/vimleshmishra/virtualization-sdk.git -p virt-sdk-branch=develop"

git blackbox -s appdata_basic -c APPDATA_PYTHON_STAGED_CENTOS73 --extra-params="-p virt-sdk-repo=https://github.com/vimleshmishra/virtualization-sdk.git -p virt-sdk-branch=develop"

SumoSourabh commented 2 years ago
vimleshmishra commented 2 years ago
  • For test cases to pass, you will have to update test_get_version () test case of test_package_util.py file.
  • Also, you will have to update the PR with JIRA ID and Description for General GitHub PrePush Checks to pass.

Thanks for highlighting.