netcdf-c/nczarr_test/test_zchunks.c
Dennis Heimbigner df3636b959 Mitigate S3 test interference + Unlimited Dimensions in NCZarr
This PR started as an attempt to add unlimited dimensions to NCZarr.
It did that, but this exposed significant problems with test interference.
So this PR is mostly about fixing -- well mitigating anyway -- test
interference.

The problem of test interference is now documented in the document docs/internal.md.
The solutions implemented here are also describe in that document.
The solution is somewhat fragile but multiple cleanup mechanisms
are provided. Note that this feature requires that the
AWS command line utility must be installed.

## Unlimited Dimensions.
The existing NCZarr extensions to Zarr are modified to support unlimited dimensions.
NCzarr extends the Zarr meta-data for the ".zgroup" object to include netcdf-4 model extensions. This information is stored in ".zgroup" as dictionary named "_nczarr_group".
Inside "_nczarr_group", there is a key named "dims" that stores information about netcdf-4 named dimensions. The value of "dims" is a dictionary whose keys are the named dimensions. The value associated with each dimension name has one of two forms
Form 1 is a special case of form 2, and is kept for backward compatibility. Whenever a new file is written, it uses format 1 if possible, otherwise format 2.
* Form 1: An integer representing the size of the dimension, which is used for simple named dimensions.
* Form 2: A dictionary with the following keys and values"
   - "size" with an integer value representing the (current) size of the dimension.
   - "unlimited" with a value of either "1" or "0" to indicate if this dimension is an unlimited dimension.

For Unlimited dimensions, the size is initially zero, and as variables extend the length of that dimension, the size value for the dimension increases.
That dimension size is shared by all arrays referencing that dimension, so if one array extends an unlimited dimension, it is implicitly extended for all other arrays that reference that dimension.
This is the standard semantics for unlimited dimensions.

Adding unlimited dimensions required a number of other changes to the NCZarr code-base. These included the following.
* Did a partial refactor of the slice handling code in zwalk.c to clean it up.
* Added a number of tests for unlimited dimensions derived from the same test in nc_test4.
* Added several NCZarr specific unlimited tests; more are needed.
* Add test of endianness.

## Misc. Other Changes
* Modify libdispatch/ncs3sdk_aws.cpp to optionally support use of the
   AWS Transfer Utility mechanism. This is controlled by the
   ```#define TRANSFER```` command in that file. It defaults to being disabled.
* Parameterize both the standard Unidata S3 bucket (S3TESTBUCKET) and the netcdf-c test data prefix (S3TESTSUBTREE).
* Fixed an obscure memory leak in ncdump.
* Removed some obsolete unit testing code and test cases.
* Uncovered a bug in the netcdf-c handling of big-endian floats and doubles. Have not fixed yet. See tst_h5_endians.c.
* Renamed some nczarr_tests testcases to avoid name conflicts with nc_test4.
* Modify the semantics of zmap\#ncsmap_write to only allow total rewrite of objects.
* Modify the semantics of zodom to properly handle stride > 1.
* Add a truncate operation to the libnczarr zmap code.
2023-09-26 16:56:48 -06:00

392 lines
14 KiB
C

/* This is part of the netCDF package.
Copyright 2018 University Corporation for Atmospheric Research/Unidata
See COPYRIGHT file for conditions of use.
Test netcdf-4 variables.
Ed Hartnett, Dennis Heimbigner
modified version of nc_test4/tst_chunks.c
*/
#include "ut_includes.h"
#include "test_nczarr_utils.h"
#define DEBUGNOFILL
#undef PRINT_DEFAULT_CHUNKSIZE_TABLE
#undef FILTERS
#define FILE_NAME "tst_chunks"
#define NDIMS1 1
#define D_SMALL "small_dim"
#define D_SMALL_LEN 16
#define D_MEDIUM "medium_dim"
#define D_MEDIUM_LEN 65546
#define D_LARGE "large_dim"
#define D_LARGE_LEN 1048586
#define V_SMALL "small_var"
#define V_MEDIUM "medium_var"
#define V_LARGE "large_var"
int
main(int argc, char **argv)
{
processoptions(argc,argv,FILE_NAME);
printf("\n*** Testing netcdf-4 variable chunking.\n");
printf("**** testing that fixed vars with forced chunking end up being chunked, with good sizes...");
{
int ncid;
int nvars, ndims, ngatts, unlimdimid;
int storage;
int ndims_in, natts_in, dimids_in;
int small_dimid, medium_dimid, large_dimid;
int small_varid, medium_varid, large_varid;
char var_name_in[NC_MAX_NAME + 1];
size_t chunksize_in[NDIMS1];
nc_type xtype_in;
/* Create a netcdf-4 file with three dimensions. */
if (nc_create(itoptions.path, NC_NETCDF4, &ncid)) ERR;
if (nc_def_dim(ncid, D_SMALL, D_SMALL_LEN, &small_dimid)) ERR;
if (nc_def_dim(ncid, D_MEDIUM, D_MEDIUM_LEN, &medium_dimid)) ERR;
if (nc_def_dim(ncid, D_LARGE, D_LARGE_LEN, &large_dimid)) ERR;
/* Add three vars, with forced chunking. */
if (nc_def_var(ncid, V_SMALL, NC_INT64, NDIMS1, &small_dimid, &small_varid)) ERR;
if (nc_def_var(ncid, V_MEDIUM, NC_INT64, NDIMS1, &medium_dimid, &medium_varid)) ERR;
if (nc_def_var(ncid, V_LARGE, NC_INT64, NDIMS1, &large_dimid, &large_varid)) ERR;
#ifdef FILTERS
if (nc_def_var_deflate(ncid, small_varid, 0, 1, 4)) ERR;
if (nc_def_var_deflate(ncid, medium_varid, 1, 0, 0)) ERR;
if (nc_def_var_fletcher32(ncid, large_varid, 1)) ERR;
#else
if(nc_def_var_chunking(ncid,small_varid,NC_CHUNKED,NULL)) ERR;
if(nc_def_var_chunking(ncid,medium_varid,NC_CHUNKED,NULL)) ERR;
if(nc_def_var_chunking(ncid,large_varid,NC_CHUNKED,NULL)) ERR;
#endif
if (nc_close(ncid)) ERR;
/* Open the file and check. */
if (nc_open(itoptions.path, NC_WRITE, &ncid)) ERR;
if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR;
if (nvars != 3 || ndims != 3 || ngatts != 0 || unlimdimid != -1) ERR;
if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, &dimids_in, &natts_in)) ERR;
if (strcmp(var_name_in, V_SMALL) || xtype_in != NC_INT64 || ndims_in != 1 ||
natts_in != 0) ERR;
/* Make sure chunking sizes are what we expect. */
if (nc_inq_var_chunking(ncid, small_varid, &storage, chunksize_in)) ERR;
if (storage || chunksize_in[0] != D_SMALL_LEN) ERR;
if (nc_inq_var_chunking(ncid, medium_varid, &storage, chunksize_in)) ERR;
if (storage || chunksize_in[0] * sizeof(long long) > DEFAULT_CHUNK_SIZE) ERR;
if (nc_inq_var_chunking(ncid, large_varid, &storage, chunksize_in)) ERR;
if (storage || chunksize_in[0] * sizeof(long long) > DEFAULT_CHUNK_SIZE) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("**** testing default chunksizes...");
{
int nvars, ndims, ngatts, unlimdimid;
int storage;
#define NUM_DIM 4
#define NUM_TYPE 2
int ncid;
int dim_len[NUM_DIM] = {1, 100, 1000, 2000};
// int dim_len[NUM_DIM] = {1, 50, 100, 200};
size_t chunksize_in[NUM_DIM];
int type_id[NUM_TYPE] = {NC_BYTE, NC_INT};
int dimid[NUM_DIM], varid[NUM_TYPE];
char dim_name[NC_MAX_NAME + 1], var_name[NC_MAX_NAME + 1];
int d, t;
/* Create a netcdf-4 file with NUM_DIM dimensions. */
if (nc_create(itoptions.path, NC_NETCDF4, &ncid)) ERR;
#ifdef DEBUGNOFILL
if(nc_set_fill(ncid,NC_NOFILL,&d)) ERR;
#endif
for (d = 0; d < NUM_DIM; d++)
{
sprintf(dim_name, "dim_%d", dim_len[d]);
#ifdef PRINT_DEFAULT_CHUNKSIZE_TABLE
printf("creating dim[%d] %s = %d\n", d, dim_name, dim_len[d]);
#endif
if (nc_def_dim(ncid, dim_name, dim_len[d], &dimid[d])) ERR;
}
for (t = 0; t < NUM_TYPE; t++)
{
sprintf(var_name, "var_%d", type_id[t]);
if (nc_def_var(ncid, var_name, type_id[t], NUM_DIM, dimid, &varid[t])) ERR;
if (nc_inq_var_chunking(ncid, varid[t], &storage, chunksize_in)) ERR;
#ifdef PRINT_DEFAULT_CHUNKSIZE_TABLE
printf("chunksizes for %d x %d x %d x %d var %s: %d x %d x %d x %d (=%d)\n",
dim_len[0], dim_len[1], dim_len[2], dim_len[3],
var_name,
(int)chunksize_in[0], (int)chunksize_in[1], (int)chunksize_in[2],
(int)chunksize_in[3],
(int)(chunksize_in[0] * chunksize_in[1] * chunksize_in[2] * chunksize_in[3]));
#endif
}
if (nc_close(ncid)) ERR;
/* Open the file and check. */
if (nc_open(itoptions.path, NC_WRITE, &ncid)) ERR;
if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR;
if (nvars != NUM_TYPE || ndims != NUM_DIM || ngatts != 0 || unlimdimid == 0) ERR;
for (t = 0; t < NUM_TYPE; t++)
{
sprintf(var_name, "var_%d", type_id[t]);
if (nc_inq_var_chunking(ncid, varid[t], &storage, chunksize_in)) ERR;
if (storage) ERR;
#ifdef PRINT_DEFAULT_CHUNKSIZE_TABLE
printf("chunksizes for %d x %d x %d x %d var: %d x %d x %d x %d (=%d)\n",
dim_len[0], dim_len[1], dim_len[2], dim_len[3],
(int)chunksize_in[0], (int)chunksize_in[1], (int)chunksize_in[2],
(int)chunksize_in[3],
(int)(chunksize_in[0] * chunksize_in[1] * chunksize_in[2] * chunksize_in[3]));
#endif
}
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("**** testing that chunking works on classic mode files...");
{
#define D_SMALL_LEN2 66
int ncid;
int nvars, ndims, ngatts, unlimdimid;
int storage;
int ndims_in, natts_in, dimids_in;
int small_dimid, medium_dimid, large_dimid;
int small_varid, medium_varid, large_varid;
char var_name_in[NC_MAX_NAME + 1];
size_t chunks[1], chunksize_in;
nc_type xtype_in;
/* Create a netcdf-4 file with three dimensions. */
if (nc_create(itoptions.path, NC_NETCDF4, &ncid)) ERR;
if (nc_def_dim(ncid, D_SMALL, D_SMALL_LEN2, &small_dimid)) ERR;
if (nc_def_dim(ncid, D_MEDIUM, D_MEDIUM_LEN, &medium_dimid)) ERR;
if (nc_def_dim(ncid, D_LARGE, D_LARGE_LEN, &large_dimid)) ERR;
/* Add three vars. */
if (nc_def_var(ncid, V_SMALL, NC_INT64, NDIMS1, &small_dimid, &small_varid)) ERR;
if (nc_def_var_chunking(ncid, small_varid, 1, NULL)) ERR;
if (nc_def_var(ncid, V_MEDIUM, NC_INT64, NDIMS1, &medium_dimid, &medium_varid)) ERR;
chunks[0] = D_MEDIUM_LEN / 100;
if (nc_def_var_chunking(ncid, medium_varid, 0, chunks)) ERR;
#ifdef FILTERS
if (nc_def_var_deflate(ncid, medium_varid, 1, 0, 0)) ERR;
#endif
if (nc_def_var(ncid, V_LARGE, NC_INT64, NDIMS1, &large_dimid, &large_varid)) ERR;
chunks[0] = D_LARGE_LEN / 1000;
if (nc_def_var_chunking(ncid, large_varid, 0, chunks)) ERR;
#ifdef FILTERS
if (nc_def_var_fletcher32(ncid, large_varid, 1)) ERR;
#endif
if (nc_close(ncid)) ERR;
/* Open the file and check. */
if (nc_open(itoptions.path, NC_WRITE, &ncid)) ERR;
if (nc_inq(ncid, &ndims, &nvars, &ngatts, &unlimdimid)) ERR;
if (nvars != 3 || ndims != 3 || ngatts != 0 || unlimdimid != -1) ERR;
if (nc_inq_var(ncid, 0, var_name_in, &xtype_in, &ndims_in, &dimids_in, &natts_in)) ERR;
if (strcmp(var_name_in, V_SMALL) || xtype_in != NC_INT64 || ndims_in != 1 ||
natts_in != 0) ERR;
/* Make sure chunking settings are what we expect. */
if (nc_inq_var_chunking(ncid, small_varid, &storage, &chunksize_in)) ERR;
if (storage != NC_CHUNKED) ERR;
if (nc_inq_var_chunking(ncid, medium_varid, &storage, &chunksize_in)) ERR;
if (storage || chunksize_in != D_MEDIUM_LEN / 100) ERR;
if (nc_inq_var_chunking(ncid, large_varid, &storage, &chunksize_in)) ERR;
if (storage || chunksize_in != D_LARGE_LEN / 1000) ERR;
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("**** testing many chunking variables...");
{
#define NDIMS_3 3
#define NUM_PLANS 30
#define D_SNEAKINESS "sneakiness"
#define D_SNEAKINESS_LEN 5
#define D_CLEVERNESS "clevernesss"
#define D_CLEVERNESS_LEN 3
#define D_EFFECTIVENESS "effectiveness"
#define D_EFFECTIVENESS_LEN 2
int ncid, dimids[NDIMS_3], varid[NUM_PLANS];
size_t chunksize[NDIMS_3] = {D_SNEAKINESS_LEN, D_CLEVERNESS_LEN,
D_EFFECTIVENESS_LEN};
char plan_name[NC_MAX_NAME + 1];
int storage;
size_t chunksize_in[NDIMS_3];
int i, j;
/* Create a netcdf-4 file with three dimensions. */
if (nc_create(itoptions.path, NC_NETCDF4, &ncid)) ERR;
if (nc_def_dim(ncid, D_SNEAKINESS, D_SNEAKINESS_LEN, &dimids[0])) ERR;
if (nc_def_dim(ncid, D_CLEVERNESS, D_CLEVERNESS_LEN, &dimids[1])) ERR;
if (nc_def_dim(ncid, D_EFFECTIVENESS, D_EFFECTIVENESS_LEN, &dimids[2])) ERR;
/* Oh that tricky Cardinal Richelieu, he had many plans! */
for (i = 0; i < NUM_PLANS; i++)
{
sprintf(plan_name, "Richelieu_sneaky_plan_%d", i);
if (nc_def_var(ncid, plan_name, i % (NC_STRING - 1) + 1, NDIMS_3,
dimids, &varid[i])) ERR;
if (nc_def_var_chunking(ncid, varid[i], 0, chunksize)) ERR;
}
/* Check the chunking. */
for (i = 0; i < NUM_PLANS; i++)
{
if (nc_inq_var_chunking(ncid, varid[i], &storage, chunksize_in)) ERR;
{
for (j = 0; j < NDIMS_3; j++)
if (chunksize_in[j] != chunksize[j]) ERR;
}
}
if (nc_close(ncid)) ERR;
/* Open the file and check. */
if (nc_open(itoptions.path, NC_WRITE, &ncid)) ERR;
/* Check the chunking. */
for (i = 0; i < NUM_PLANS; i++)
{
if (nc_inq_var_chunking(ncid, varid[i], &storage, chunksize_in)) ERR;
{
for (j = 0; j < NDIMS_3; j++)
if (chunksize_in[j] != chunksize[j]) ERR;
}
}
if (nc_close(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("**** testing that too large chunksizes fail...");
{
#define D_SMALL_LEN2 66
int stat = NC_NOERR;
int ncid;
int small_dimid;
int small_varid;
size_t chunks[1];
/* Create a netcdf-4 file with three dimensions. */
if (nc_create(itoptions.path, NC_NETCDF4, &ncid)) ERR;
if (nc_def_dim(ncid, D_SMALL, D_SMALL_LEN2, &small_dimid)) ERR;
/* Add one var. */
if (nc_def_var(ncid, V_SMALL, NC_INT64, NDIMS1, &small_dimid, &small_varid)) ERR;
/* Attempt to set too large chunksizes */
chunks[0] = D_SMALL_LEN2 + 1;
stat = nc_def_var_chunking(ncid, small_varid, NC_CHUNKED, chunks);
if(stat != NC_EBADCHUNK) {
printf("Return code is '%s', expected NC_BADCHUNK",nc_strerror(stat));
ERR;
}
/* try again with proper chunksize */
chunks[0] = D_SMALL_LEN2;
stat = nc_def_var_chunking(ncid, small_varid, NC_CHUNKED, chunks);
if(stat != NC_NOERR) {
printf("Return code is '%s', expected NC_NOERR",nc_strerror(stat));
ERR;
}
if (nc_abort(ncid)) ERR;
}
SUMMARIZE_ERR;
printf("**** testing cache size smaller than chunk size...");
{
#define NDIM2 2
#define DIM_X_LEN 10000
#define DIM_Y_LEN 10000
#define DIM_NAME_X_CACHE_CHUNK "Height"
#define DIM_NAME_Y_CACHE_CHUNK "Width"
#define VAR_NAME_CACHE_CHUNK "House_Size"
#define VAR_NAME_CACHE_CHUNK_2 "Boat_Size"
#define VAR_NAME_CACHE_CHUNK_3 "Deck_Size"
int ncid;
int dimid[NDIM2];
int varid, varid2, varid3;
size_t chunks[NDIM2] = {100, 100};
size_t chunks_big[NDIM2] = {DIM_X_LEN, DIM_Y_LEN};
size_t chunks_in[NDIM2];
int storage;
size_t cache_size = 16;
size_t cache_nelems = 1;
float cache_preemption = 0.5;
size_t cache_size_in;
size_t cache_nelems_in;
float cache_preemption_in;
/* Create a netcdf-4 file with two dimensions. */
if (nc_create(itoptions.path, NC_NETCDF4, &ncid)) ERR;
if (nc_def_dim(ncid, DIM_NAME_X_CACHE_CHUNK, DIM_X_LEN, &dimid[0])) ERR;
if (nc_def_dim(ncid, DIM_NAME_Y_CACHE_CHUNK, DIM_Y_LEN, &dimid[1])) ERR;
/* Add vars. */
if (nc_def_var(ncid, VAR_NAME_CACHE_CHUNK, NC_INT64, NDIM2, dimid, &varid)) ERR;
if (nc_def_var(ncid, VAR_NAME_CACHE_CHUNK_2, NC_INT64, NDIM2, dimid, &varid2)) ERR;
if (nc_def_var(ncid, VAR_NAME_CACHE_CHUNK_3, NC_INT64, NDIM2, dimid, &varid3)) ERR;
/* Set the var cache to something arbitrary but small */
if (nc_set_var_chunk_cache(ncid, varid, cache_size, cache_nelems,
cache_preemption)) ERR;
/* Set the chunking. */
if (nc_def_var_chunking(ncid, varid, NC_CHUNKED, chunks)) ERR;
if (nc_inq_var_chunking(ncid, varid, &storage, chunks_in)) ERR;
if (storage || chunks_in[0] != chunks[0] || chunks_in[1] != chunks[1]) ERR;
if (nc_def_var_chunking(ncid, varid2, NC_CHUNKED, chunks)) ERR;
if (nc_inq_var_chunking(ncid, varid2, &storage, chunks_in)) ERR;
if (storage || chunks_in[0] != chunks[0] || chunks_in[1] != chunks[1]) ERR;
if (nc_def_var_chunking(ncid, varid3, NC_CHUNKED, chunks_big)) ERR;
if (nc_inq_var_chunking(ncid, varid3, &storage, chunks_in)) ERR;
if (storage || chunks_in[0] != chunks_big[0] || chunks_in[1] != chunks_big[1]) ERR;
/* Get the var cache values. */
if (nc_get_var_chunk_cache(ncid, varid, &cache_size_in, &cache_nelems_in,
&cache_preemption_in)) ERR;
if (cache_size_in != cache_size || cache_nelems_in != cache_nelems ||
cache_preemption_in != cache_preemption) ERR;
if (nc_get_var_chunk_cache(ncid, varid2, &cache_size_in, &cache_nelems_in,
&cache_preemption_in)) ERR;
if (cache_size_in != DEFAULT_CHUNK_CACHE_SIZE) ERR;
#if 0
/* Inapplicable to zarr */
/* The cache_size has been increased due to larger chunksizes
* for varid3. */
if (nc_get_var_chunk_cache(ncid, varid3, &cache_size_in, &cache_nelems_in,
&cache_preemption_in)) ERR;
if (cache_nelems_in != CHUNK_CACHE_NELEMS ||
cache_preemption_in != CHUNK_CACHE_PREEMPTION) ERR;
/* printf("cache_size_in %ld\n", cache_size_in); */
#endif
/* Close the file. */
if (nc_close(ncid)) ERR;
/* Reopen the file. */
if (nc_open(itoptions.path, NC_NOWRITE, &ncid)) ERR;
/* Close the file. */
if (nc_close(ncid)) ERR;
}
clearoptions();
SUMMARIZE_ERR;
FINAL_RESULTS;
}