netcdf-c/nczarr_test/s3util.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

440 lines
10 KiB
C

/*
* Copyright 2018, University Corporation for Atmospheric Research
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#if defined(_WIN32) && !defined(__MINGW32__)
#include "XGetopt.h"
#endif
#include "zincludes.h"
#include "ncs3sdk.h"
#include "ncpathmgr.h"
#include "nclog.h"
#include "ncuri.h"
#include "netcdf_aux.h"
#undef NODELETE
#undef DEBUG
#define DATANAME "data"
typedef enum S3op {
S3OP_NONE=0,
S3OP_LIST=1,
S3OP_CLEAR=2,
S3OP_PRINT=3,
S3OP_UPLOAD=4,
S3OP_DOWNLOAD=5,
} S3op;
static struct S3ops {
S3op s3op;
const char* opnames[3];
} s3ops[] = {
{S3OP_LIST,{"list","l",NULL}},
{S3OP_CLEAR,{"clear","c",NULL}},
{S3OP_PRINT,{"print","p",NULL}},
{S3OP_UPLOAD,{"upload","u",NULL}},
{S3OP_DOWNLOAD,{"download","d",NULL}},
{S3OP_NONE,{NULL,NULL,NULL}},
};
/* Command line options */
struct Dumpptions {
int debug;
int verbose;
S3op s3op;
NCURI* url;
char* key; /* via -k flag */
char* rootkey; /* from url | key */
nc_type nctype; /* for printing content */
char* filename;
char* profile;
} dumpoptions;
struct S3SDK {
NCS3INFO s3;
void* s3client;
char* errmsg;
} s3sdk;
/* Forward */
static int s3list(void);
static int s3clear(void);
static int s3print(void);
static int s3upload(void);
static int s3download(void);
static nc_type typefor(const char* t);
static void printcontent(size64_t len, const char* content, nc_type nctype);
static void
usage(void)
{
fprintf(stderr,"usage: s3util list|print|upload|download|clear -u <url> [-k <key.] [-f <filename>]\n");
exit(1);
}
static S3op
decodeop(const char* name)
{
struct S3ops* s3op = s3ops;
const char** s = NULL;
for(;s3op->opnames[0] != NULL;s3op++) {
for(s=s3op->opnames;*s;s++) {
if(strcasecmp(*s,name)==0) return s3op->s3op;
}
}
return S3OP_NONE;
}
static int
s3setup(void)
{
int stat = NC_NOERR;
s3sdk.s3client = NC_s3sdkcreateclient(&s3sdk.s3);
return stat;
}
static int
s3shutdown(int deleteit)
{
int stat = NC_NOERR;
stat = NC_s3sdkclose(s3sdk.s3client, &s3sdk.s3, deleteit, &s3sdk.errmsg);
return stat;
}
int
main(int argc, char** argv)
{
int stat = NC_NOERR;
int c;
char* tmp = NULL;
nc_initialize();
memset((void*)&dumpoptions,0,sizeof(dumpoptions));
dumpoptions.nctype = NC_UBYTE; /* default */
while ((c = getopt(argc, argv, "df:hk:p:t:T:u:v")) != EOF) {
switch(c) {
case 'd':
dumpoptions.debug = 1;
break;
case 'f':
dumpoptions.filename = strdup(optarg);
break;
case 'h':
usage();
return 0;
case 'k': {
size_t len = strlen(optarg);
dumpoptions.key = (char*)malloc(len+1+1);
if(*optarg != '/') {
fprintf(stderr,"warning: -k option does not start with '/': %s",optarg);
dumpoptions.key[0] = '/';
memcpy(dumpoptions.key+1,optarg,len);
len++;
} else
memcpy(dumpoptions.key,optarg,strlen(optarg));
dumpoptions.key[len] = '\0';
} break;
case 'p':
dumpoptions.profile = strdup(optarg);
break;
case 't':
dumpoptions.nctype = typefor(optarg);
break;
case 'u': {
char* p = NC_shellUnescape(optarg);
ncuriparse(p,&dumpoptions.url);
nullfree(p);
if(dumpoptions.url == NULL) {
fprintf(stderr,"malformed -u option: %s",optarg);
stat = NC_EINVAL;
goto done;
}
} break;
case 'v':
dumpoptions.verbose = 1;
break;
case 'T':
nctracelevel(atoi(optarg));
break;
case '?':
fprintf(stderr,"unknown option\n");
stat = NC_EINVAL;
goto done;
}
}
/* get command argument */
argc -= optind;
argv += optind;
if (argc > 1) {
int j;
fprintf(stderr, "s3util: only one command argument permitted:");
for(j=0;j<argc;j++) fprintf(stderr," %s",argv[j]);
fprintf(stderr, "\n");
stat = NC_EINVAL;
goto done;
}
if (argc == 0) {
fprintf(stderr, "s3util: no command specified\n");
stat = NC_EINVAL;
goto done;
}
dumpoptions.s3op = decodeop(argv[0]);
memset(&s3sdk,0,sizeof(s3sdk));
if((stat = NC_s3urlprocess(dumpoptions.url, &s3sdk.s3, NULL))) goto done;
if(s3sdk.s3.rootkey != NULL && dumpoptions.key != NULL) {
/* Make the root key be the concatenation of rootkey+dumpoptions.key */
if((stat = nczm_concat(s3sdk.s3.rootkey,dumpoptions.key,&tmp))) goto done;
nullfree(s3sdk.s3.rootkey);
s3sdk.s3.rootkey = tmp; tmp = NULL;
} else if(dumpoptions.key != NULL) {
s3sdk.s3.rootkey = dumpoptions.key;
dumpoptions.key = NULL;
}
if(s3sdk.s3.rootkey == NULL || strlen(s3sdk.s3.rootkey)==0)
s3sdk.s3.rootkey = strdup("/");
switch (dumpoptions.s3op) {
default:
fprintf(stderr,"Default action: list\n");
/* fall thru */
case S3OP_LIST:
if((stat = s3list())) goto done;
break;
case S3OP_CLEAR:
if((stat = s3clear())) goto done;
break;
case S3OP_PRINT:
if((stat = s3print())) goto done;
break;
case S3OP_UPLOAD:
if((stat = s3upload())) goto done;
break;
case S3OP_DOWNLOAD:
if((stat = s3download())) goto done;
break;
}
done:
/* Reclaim dumpoptions */
ncurifree(dumpoptions.url);
nullfree(dumpoptions.rootkey);
nullfree(tmp);
NC_s3clear(&s3sdk.s3);
nc_finalize();
if(stat)
fprintf(stderr,"fail: %s\n",nc_strerror(stat));
return (stat ? 1 : 0);
}
static int
s3list(void)
{
int stat = NC_NOERR;
size_t nkeys = 0;
char** keys = NULL;
if(s3setup()) goto done;
stat = NC_s3sdksearch(s3sdk.s3client, s3sdk.s3.bucket, s3sdk.s3.rootkey, &nkeys, &keys, &s3sdk.errmsg);
if(stat) goto done;
if(nkeys > 0) {
size_t i;
/* Sort the list -- shortest first */
nczm_sortenvv(nkeys,keys);
for(i=0;i<nkeys;i++) {
printf("[%u] %s\n",(unsigned)i,keys[i]);
}
} else
printf("<empty>\n");
done:
s3shutdown(0);
NCZ_freeenvv(nkeys,keys);
return stat;
}
static int
s3clear(void)
{
int stat = NC_NOERR;
size_t nkeys = 0;
char** keys = NULL;
if(s3setup()) goto done;
if((stat = NC_s3sdksearch(s3sdk.s3client, s3sdk.s3.bucket, s3sdk.s3.rootkey, &nkeys, &keys, &s3sdk.errmsg))) goto done;
if(nkeys > 0 && keys != NULL) {
size_t i;
/* Sort the list -- shortest first */
nczm_sortenvv(nkeys,keys);
if(dumpoptions.verbose) {
printf("deleted keys:\n");
for(i=0;i<nkeys;i++) {
printf("\t%s\n",keys[i]);
}
}
#ifndef NODELETE
for(i=0;i<nkeys;i++) {
if((stat = NC_s3sdkdeletekey(s3sdk.s3client, s3sdk.s3.bucket, keys[i], &s3sdk.errmsg)))
goto done;
}
#endif
}
done:
NCZ_freeenvv(nkeys,keys);
s3shutdown(0);
return stat;
}
static int
s3print(void)
{
int stat = NC_NOERR;
size_t nkeys = 0;
char** keys = NULL;
size64_t count;
char* content = NULL;
if(s3setup()) goto done;
if((stat = NC_s3sdkinfo(s3sdk.s3client, s3sdk.s3.bucket, s3sdk.s3.rootkey, &count,&s3sdk.errmsg)))
goto done;
if((content = (char*)calloc(1,count+1))==NULL)
{stat = NC_ENOMEM; goto done;}
if((stat = NC_s3sdkread(s3sdk.s3client, s3sdk.s3.bucket, s3sdk.s3.rootkey, 0, count, (void*) content, &s3sdk.errmsg)))
goto done;
printcontent(count,content,dumpoptions.nctype);
done:
if(content) free(content);
s3shutdown(0);
NCZ_freeenvv(nkeys,keys);
return stat;
}
static int
s3upload(void)
{
int stat = NC_NOERR;
size_t red = 0;
void* content = NULL;
if(s3setup()) goto done;
if((stat = ncaux_readfile(dumpoptions.filename,&red,&content)))
goto done;
if((stat = NC_s3sdkwriteobject(s3sdk.s3client, s3sdk.s3.bucket, s3sdk.s3.rootkey, red, content, &s3sdk.errmsg)))
goto done;
done:
if(content) free(content);
s3shutdown(0);
return stat;
}
static int
s3download(void)
{
int stat = NC_NOERR;
size64_t count;
char* content = NULL;
if(s3setup()) goto done;
if((stat = NC_s3sdkinfo(s3sdk.s3client, s3sdk.s3.bucket, s3sdk.s3.rootkey, &count,&s3sdk.errmsg)))
goto done;
if((content = (char*)calloc(1,count))==NULL)
{stat = NC_ENOMEM; goto done;}
if((stat = NC_s3sdkread(s3sdk.s3client, s3sdk.s3.bucket, s3sdk.s3.rootkey, 0, count, (void*) content, &s3sdk.errmsg)))
goto done;
if((stat = ncaux_writefile(dumpoptions.filename,count,content)))
goto done;
done:
if(content) free(content);
s3shutdown(0);
return stat;
}
static void
printcontent(size64_t len, const char* content, nc_type nctype)
{
size64_t i;
if(len == 0) {
printf("<empty>\n");
return;
}
if(nctype == NC_STRING) printf("|");
for(i=0;i<len;i++) {
if(nctype != NC_STRING && i > 0) printf(", ");
switch(nctype) {
case NC_BYTE: printf("%d",((char*)content)[i]); break;
case NC_SHORT: printf("%d",((short*)content)[i]); break;
case NC_INT: printf("%d",((int*)content)[i]); break;
case NC_INT64: printf("%lld",((long long*)content)[i]); break;
case NC_UBYTE: printf("%u",((unsigned char*)content)[i]); break;
case NC_USHORT: printf("%u",((unsigned short*)content)[i]); break;
case NC_UINT: printf("%u",((unsigned int*)content)[i]); break;
case NC_UINT64: printf("%llu",((unsigned long long*)content)[i]); break;
case NC_FLOAT: printf("%f",((float*)content)[i]); break;
case NC_DOUBLE: printf("%lf",((double*)content)[i]); break;
case NC_STRING: putc(content[i],stdout); break;
default: abort();
}
}
if(nctype == NC_STRING) printf("|\n");
}
static nc_type
typefor(const char* t)
{
if(strcmp(t,"NC_BYTE")==0) return NC_BYTE;
else if(strcmp(t,"NC_SHORT")==0) return NC_SHORT;
else if(strcmp(t,"NC_INT")==0) return NC_INT;
else if(strcmp(t,"NC_INT64")==0) return NC_INT64;
else if(strcmp(t,"NC_UBYTE")==0) return NC_UBYTE;
else if(strcmp(t,"NC_USHORT")==0) return NC_USHORT;
else if(strcmp(t,"NC_UINT")==0) return NC_UINT;
else if(strcmp(t,"NC_UINT64")==0) return NC_UINT64;
else if(strcmp(t,"NC_FLOAT")==0) return NC_FLOAT;
else if(strcmp(t,"NC_DOUBLE")==0) return NC_DOUBLE;
else if(strcmp(t,"NC_STRING")==0) return NC_STRING;
return NC_NAT;
}