Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the fix for #350 included in #1119 #1521

Merged

Conversation

ckhroulev
Copy link
Contributor

@ckhroulev ckhroulev commented Nov 13, 2019

  1. We have to use H5Tequal() to compare HDF5 type IDs.
  2. When checking if we can re-use an NC_CHAR attribute it is enough to compare data types (H5Tequal() takes care of the size comparison).
  3. This PR adds missing code (reuse_att was set but not used).

Now an attribute in a NetCDF-4 file can be modified as many times as necessary, as long its type and length remain the same.

Modifications changing either type or length of an attribute require deleting and re-creating an attribute which increments the attribute order creation index. Once this index reaches 65535 all attribute modifications (for a particular group or variable) will fail.

1) We have to use H5Tequal() to compare HDF5 type IDs.
2) When checking if we can re-use an NC_CHAR attribute it is enough to
   compare data types (H5Tequal() takes care of the size comparison).
3) This commit adds missing code (reuse_att was set but not used).

Now an attribute in a NetCDF-4 file can be modified as many times as
necessary, as long its type and length remain the same.

Modifications changing either type or length of an attribute require
deleting and re-creating an attribute which increments the attribute
order creation index. Once this index reaches 65535 all attribute
modifications (for a particular group or variable) will fail.

For reference:

Issue 350 title: NetCDF-4 limits the number of times an attribute can
be modified

Pull request 1119 title: Fix checking for HDF5 max dims, no longer
re-create atts if not needed, confirm behavior for HDF5 cyclical
files, allow user to set mpiexec
@CLAassistant
Copy link

CLAassistant commented Nov 13, 2019

CLA assistant check
All committers have signed the CLA.

@ckhroulev
Copy link
Contributor Author

Unfortunately #350 was not fixed by #1119: the issue is present in the current master branch (I tested 108e938). Test cases included in #350 still fail and I could not find the test mentioned in #350 (comment) anywhere in the repository.

libhdf5/nc4hdf.c Outdated

/* Delete the attribute. */
if (file_typeid != existing_att_typeid || npoints != att->len)
if (!H5Tequal(file_typeid, existing_att_typeid) || npoints != att->len)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should modify this condition instead of setting npoints to att->len above. This has been a long day...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, it's a new day! ;-) Make the modification if you think it should be made.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I think this version makes it easier to see what's going on.

@edhartnett
Copy link
Contributor

OK, glad you took care of this, but we also must add the test back in.

Can you insert this at the bottom of nc_test4/tst_atts.c and make sure it works:

    printf("*** testing modification of an attribute 2^16 times...");
    {
        int ncid;
        int i = 42;

        /* Create a file with a global att. */
        if (nc_create(FILE_NAME, NC_NETCDF4|NC_CLOBBER, &ncid)) ERR;
        if (nc_put_att_int(ncid, NC_GLOBAL, "attribute", NC_INT, 1, &i)) ERR;
        if (nc_close(ncid)) ERR;

        /* Modify an attribute 2^16 times. This test suggested by
         * Constantine Khrulev. */
        for (i = 0; i < 65536; ++i)
        {
            if (nc_open(FILE_NAME, NC_WRITE, &ncid)) ERR;
            if (nc_put_att_int(ncid, NC_GLOBAL, "attribute", NC_INT, 1, &i)) ERR;
            if (nc_close(ncid))
                ERR;
        }
    }
    SUMMARIZE_ERR;

@edhartnett
Copy link
Contributor

And since you have separate code for handling text atts, we need a text att test too.

Thanks for this fix!

This commit adds three new tests:

- a test documenting the limitation originally reported in Unidata#350 (in
  general modifying an attribute about 2^16 times makes it impossible
  to modify this file in ways requiring nc_redef() and nc_enddef() calls).
- a test ensuring that a scalar attribute can be modified 2^16 times
  as long as its type and size remain the same
- a test ensuring that a text attribute can be modified 2^16 times as
  long as its size remains the same

This version uses the nc_redef(), nc_put_att_...(), nc_enddef()
sequence. One could also use nc_open(), nc_put_att_...(), nc_close()
but that would make these tests significantly slower.
@@ -424,5 +424,99 @@ main(int argc, char **argv)
free(data_in);
}
SUMMARIZE_ERR;
printf("*** testing modification of an attribute 2^16 times (changing size)...");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome tests, thanks!

@edhartnett
Copy link
Contributor

Thanks @ckhroulev , a very complete and well-crafted fix! Your contribution is very helpful. ;-)

Copy link
Contributor

@edhartnett edhartnett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes look great.

@WardF WardF self-assigned this Nov 15, 2019
@WardF WardF added this to the 4.7.3 milestone Nov 15, 2019
@WardF
Copy link
Member

WardF commented Nov 15, 2019

Thanks very much!

@WardF WardF merged commit 1a6351d into Unidata:master Nov 15, 2019
@ckhroulev ckhroulev deleted the netcdf4-repeated-attribute-modification branch November 15, 2019 00:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants