summaryrefslogtreecommitdiffstats
path: root/test/del_many_dense_attrs.c
blob: 806c25f3728d083d529e33141e01acf005a0f5ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * Purpose:	Test to verify that the infinite loop closing library/abort failure
 *          is fixed when the application creates and removes dense attributes
 *          (See HDFFV-10659).
 */

#include "h5test.h"

/* The test file name */
const char *FILENAME[] = {"del_many_dense_attrs", NULL};

#define ATTR_COUNT 64 /* The number of attributes */

/*-------------------------------------------------------------------------
 * Function:    catch_signal
 *
 * Purpose:     The signal handler to catch the SIGABRT signal.
 *
 * Return:      No return
 *
 * Programmer:  Vailin Choi
 *
 *-------------------------------------------------------------------------
 */
static void
catch_signal(int H5_ATTR_UNUSED signo)
{
    HDexit(EXIT_FAILURE);
} /* catch_signal() */

/*-------------------------------------------------------------------------
 * Function:	main
 *
 * Purpose:	Test to verify that the infinite loop closing library/abort failure
 *          is fixed when the application creates and removes dense attributes
 *          (See HDFFV-10659).
 *
 * Return:	Success:	exit(EXIT_SUCCESS)
 *		    Failure:	exit(EXIT_FAILURE)
 *
 * Programmer:  Vailin Choi; Dec 2018
 *
 *-------------------------------------------------------------------------
 */
int
main(void)
{
    hid_t       fid  = -1;         /* HDF5 File ID                 */
    hid_t       gid  = -1;         /* Group ID                     */
    hid_t       sid  = -1;         /* Dataspace ID                 */
    hid_t       aid  = -1;         /* Attribute ID                 */
    hid_t       tid  = -1;         /* Datatype ID                  */
    hid_t       fapl = -1;         /* File access property lists   */
    hid_t       gcpl = -1;         /* Group creation property list */
    char        aname[50];         /* Name of attribute            */
    const char *basename = "attr"; /* Name prefix for attribute    */
    char        filename[100];     /* File name                    */
    int         i;                 /* Local index variable         */

    /* Testing setup */
    h5_reset();

    /* To exit from the file for SIGABRT signal */
    if (HDsignal(SIGABRT, catch_signal) == SIG_ERR)
        TEST_ERROR;

    fapl = h5_fileaccess();
    h5_fixname(FILENAME[0], fapl, filename, sizeof(filename));

    /* Set to latest format */
    if (H5Pset_libver_bounds(fapl, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0)
        TEST_ERROR;

    /* Create the file  */
    if ((fid = H5Fcreate(filename, H5F_ACC_TRUNC, H5P_DEFAULT, fapl)) < 0)
        TEST_ERROR;

    /* Close the file */
    if (H5Fclose(fid) < 0)
        TEST_ERROR;

    /* Re-open the file */
    if ((fid = H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0)
        TEST_ERROR;

    /* Create the group creation property list */
    if ((gcpl = H5Pcreate(H5P_GROUP_CREATE)) < 0)
        TEST_ERROR;

    /* Set to use dense storage for all attributes on the group */
    if (H5Pset_attr_phase_change(gcpl, 0, 0) < 0)
        TEST_ERROR;

    /* Create the group in the file */
    if ((gid = H5Gcreate2(fid, "group", H5P_DEFAULT, gcpl, H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Create dataspace */
    if ((sid = H5Screate(H5S_SCALAR)) < 0)
        TEST_ERROR;

    /* Get a copy of the datatype */
    if ((tid = H5Tcopy(H5T_NATIVE_FLOAT)) < 0)
        TEST_ERROR;

    /* Create attributes in the group */
    for (i = ATTR_COUNT; i >= 0; i--) {
        /* Set up the attribute name */
        HDsnprintf(aname, sizeof(aname), "%s%d", basename, i);

        /* Create the attribute */
        if ((aid = H5Acreate2(gid, aname, tid, sid, H5P_DEFAULT, H5P_DEFAULT)) < 0)
            TEST_ERROR;

        /* Write to the attribute */
        if (H5Awrite(aid, tid, &i) < 0)
            TEST_ERROR;

        /* Close the attribute */
        if (H5Aclose(aid) < 0)
            TEST_ERROR;
    }

    /* Close the datatype */
    if (H5Tclose(tid) < 0)
        TEST_ERROR;

    /* Close the dataspace */
    if (H5Sclose(sid) < 0)
        TEST_ERROR;

    /* Close the group */
    if (H5Gclose(gid) < 0)
        TEST_ERROR;

    /* Close the group creation property list */
    if (H5Pclose(gcpl) < 0)
        TEST_ERROR;

    /* Close the file */
    if (H5Fclose(fid) < 0)
        TEST_ERROR;

    /* Re-open the file */
    if ((fid = H5Fopen(filename, H5F_ACC_RDWR, fapl)) < 0)
        TEST_ERROR;

    /* Open the group */
    if ((gid = H5Gopen2(fid, "group", H5P_DEFAULT)) < 0)
        TEST_ERROR;

    /* Delete the attributes */
    for (i = 0; i <= ATTR_COUNT; i++) {
        /* Set up the attribute name */
        HDsnprintf(aname, sizeof(aname), "%s%d", basename, i);

        /* Delete the attribute */
        if (H5Adelete(gid, aname) < 0)
            TEST_ERROR;
    } /* end for */

    /* Close the group */
    if (H5Gclose(gid) < 0)
        TEST_ERROR;

    /* Close the file */
    if (H5Fclose(fid) < 0)
        TEST_ERROR;

    h5_cleanup(FILENAME, fapl);

    return (EXIT_SUCCESS);

error:
    H5E_BEGIN_TRY
    {
        H5Gclose(gid);
        H5Sclose(sid);
        H5Tclose(tid);
        H5Aclose(aid);
        H5Fclose(fid);
        H5Pclose(gcpl);
        H5Pclose(fapl);
    }
    H5E_END_TRY

    return EXIT_FAILURE;
}
l kwb">uintmax_t)start[1]); /* Read record from dataset */ record->rec_id = UINT64_MAX; if (H5Dread(dsid, symbol_tid, rec_sid, file_sid, H5P_DEFAULT, record) < 0) return -1; /* Verify record value */ if (record->rec_id != start[1]) { fprintf(stderr, "*** ERROR ***\n"); fprintf(stderr, "Incorrect record value!\n"); fprintf(stderr, "Symbol = '%s', location = %" PRIuMAX ",%" PRIuMAX ", record->rec_id = %" PRIu64 "\n", symbol->name, (uintmax_t)start[0], (uintmax_t)start[1], record->rec_id); return -1; } /* end if */ /* Close the dataset's dataspace */ if (H5Sclose(file_sid) < 0) return -1; /* Close dataset for symbol */ if (H5Dclose(dsid) < 0) return -1; return 0; } /* end check_dataset() */ /*------------------------------------------------------------------------- * Function: read_records * * Purpose: For a given dataset, checks to make sure that the stated * and actual sizes are the same. If they are not, then * we have an inconsistent dataset due to a SWMR error. * * Parameters: const char *filename * The SWMR test file's name. * * unsigned verbose * Whether verbose console output is desired. * * unsigned long nrecords * The total number of records to read. * * unsigned poll_time * The amount of time to sleep (s). * * unsigned reopen_count * * * Return: Success: 0 * Failure: -1 * *------------------------------------------------------------------------- */ static int read_records(const char *filename, unsigned verbose, unsigned long nrecords, unsigned poll_time, unsigned reopen_count) { hid_t fid; /* File ID */ hid_t aid; /* Attribute ID */ time_t start_time; /* Starting time */ hid_t mem_sid; /* Memory dataspace ID */ symbol_t record; /* The record to add to the dataset */ unsigned seed; /* Seed for random number generator */ unsigned iter_to_reopen = reopen_count; /* # of iterations until reopen */ unsigned long u; /* Local index variable */ hid_t fapl; assert(filename); assert(poll_time != 0); /* Create file access property list */ if ((fapl = h5_fileaccess()) < 0) return -1; H5Pset_fclose_degree(fapl, H5F_CLOSE_SEMI); /* Emit informational message */ if (verbose) fprintf(stderr, "Opening file: %s\n", filename); /* Open the file */ if ((fid = H5Fopen(filename, H5F_ACC_RDONLY | H5F_ACC_SWMR_READ, fapl)) < 0) return -1; /* Seed the random number generator with the attribute in the file */ if ((aid = H5Aopen(fid, "seed", H5P_DEFAULT)) < 0) return -1; if (H5Aread(aid, H5T_NATIVE_UINT, &seed) < 0) return -1; if (H5Aclose(aid) < 0) return -1; HDsrandom(seed); /* Reset the record */ /* (record's 'info' field might need to change for each record written, also) */ memset(&record, 0, sizeof(record)); /* Create a dataspace for the record to read */ if ((mem_sid = H5Screate(H5S_SCALAR)) < 0) return -1; /* Emit informational message */ if (verbose) fprintf(stderr, "Reading records\n"); /* Get the starting time */ start_time = HDtime(NULL); /* Read records */ for (u = 0; u < nrecords; u++) { symbol_info_t *symbol = NULL; /* Symbol (dataset) */ htri_t attr_exists; /* Whether the sequence number attribute exists */ unsigned long file_u; /* Attribute sequence number (writer's "u") */ /* Get a random dataset, according to the symbol distribution */ symbol = choose_dataset(); /* Fill in "nrecords" field. Note that this depends on the writer * using the same algorithm and "nrecords" */ symbol->nrecords = nrecords / 5; /* Wait until we can read the dataset */ do { /* Check if sequence attribute exists */ if ((attr_exists = H5Aexists_by_name(fid, symbol->name, "seq", H5P_DEFAULT)) < 0) return -1; if (attr_exists) { /* Read sequence number attribute */ if ((aid = H5Aopen_by_name(fid, symbol->name, "seq", H5P_DEFAULT, H5P_DEFAULT)) < 0) return -1; if (H5Aread(aid, H5T_NATIVE_ULONG, &file_u) < 0) return -1; if (H5Aclose(aid) < 0) return -1; /* Check if sequence number is at least u - if so, this should * guarantee that this record has been written */ if (file_u >= u) break; } /* end if */ /* Check for timeout */ if (HDtime(NULL) >= (time_t)(start_time + (time_t)TIMEOUT)) { fprintf(stderr, "Reader timed out\n"); return -1; } /* end if */ /* Pause */ HDsleep(poll_time); /* Retrieve and print the collection of metadata read retries */ if (print_metadata_retries_info(fid) < 0) fprintf(stderr, "Warning: could not obtain metadata retries info\n"); /* Reopen the file */ if (H5Fclose(fid) < 0) return -1; if ((fid = H5Fopen(filename, H5F_ACC_RDONLY | H5F_ACC_SWMR_READ, fapl)) < 0) return -1; iter_to_reopen = reopen_count; } while (1); /* Emit informational message */ if (verbose) fprintf(stderr, "Checking dataset %lu\n", u); /* Check dataset */ if (check_dataset(fid, verbose, symbol, &record, mem_sid) < 0) return -1; memset(&record, 0, sizeof(record)); /* Check for reopen */ iter_to_reopen--; if (iter_to_reopen == 0) { /* Emit informational message */ if (verbose) fprintf(stderr, "Reopening file: %s\n", filename); /* Retrieve and print the collection of metadata read retries */ if (print_metadata_retries_info(fid) < 0) fprintf(stderr, "Warning: could not obtain metadata retries info\n"); /* Reopen the file */ if (H5Fclose(fid) < 0) return -1; if ((fid = H5Fopen(filename, H5F_ACC_RDONLY | H5F_ACC_SWMR_READ, fapl)) < 0) return -1; iter_to_reopen = reopen_count; } /* end if */ } /* end while */ /* Retrieve and print the collection of metadata read retries */ if (print_metadata_retries_info(fid) < 0) fprintf(stderr, "Warning: could not obtain metadata retries info\n"); /* Close file */ if (H5Fclose(fid) < 0) return -1; /* Close the memory dataspace */ if (H5Sclose(mem_sid) < 0) return -1; return 0; } /* end read_records() */ static void usage(void) { printf("\n"); printf("Usage error!\n"); printf("\n"); printf("Usage: swmr_sparse_reader [-q] [-s <# of seconds to wait for writer>]\n"); printf(" [-n <# of reads between reopens>] <# of records>\n"); printf("\n"); printf("Defaults to verbose (no '-q' given), 1 second wait ('-s 1') and 1 read\n"); printf("between reopens ('-r 1')\n"); printf("\n"); printf("Note that the # of records *must* be the same as that supplied to\n"); printf("swmr_sparse_writer\n"); printf("\n"); exit(EXIT_FAILURE); } /* end usage() */ int main(int argc, char *argv[]) { long nrecords = 0; /* # of records to read */ int poll_time = 1; /* # of seconds to sleep when waiting for writer */ int reopen_count = 1; /* # of reads between reopens */ unsigned verbose = 1; /* Whether to emit some informational messages */ unsigned u; /* Local index variables */ /* Parse command line options */ if (argc < 2) usage(); if (argc > 1) { u = 1; while (u < (unsigned)argc) { if (argv[u][0] == '-') { switch (argv[u][1]) { /* # of reads between reopens */ case 'n': reopen_count = atoi(argv[u + 1]); if (reopen_count < 0) usage(); u += 2; break; /* Be quiet */ case 'q': verbose = 0; u++; break; /* # of seconds between polling */ case 's': poll_time = atoi(argv[u + 1]); if (poll_time < 0) usage(); u += 2; break; default: usage(); break; } /* end switch */ } /* end if */ else { /* Get the number of records to read */ nrecords = atol(argv[u]); if (nrecords <= 0) usage(); u++; } /* end else */ } /* end while */ } /* end if */ /* Emit informational message */ if (verbose) { fprintf(stderr, "Parameters:\n"); fprintf(stderr, "\t# of seconds between polling = %d\n", poll_time); fprintf(stderr, "\t# of reads between reopens = %d\n", reopen_count); fprintf(stderr, "\t# of records to read = %ld\n", nrecords); } /* end if */ /* Emit informational message */ if (verbose) fprintf(stderr, "Generating symbol names\n"); /* Generate dataset names */ if (generate_symbols() < 0) { fprintf(stderr, "Error generating symbol names!\n"); exit(EXIT_FAILURE); } /* end if */ /* Create datatype for creating datasets */ if ((symbol_tid = create_symbol_datatype()) < 0) return -1; /* Reading records from datasets */ if (read_records(FILENAME, verbose, (unsigned long)nrecords, (unsigned)poll_time, (unsigned)reopen_count) < 0) { fprintf(stderr, "Error reading records from datasets!\n"); exit(EXIT_FAILURE); } /* end if */ /* Emit informational message */ if (verbose) fprintf(stderr, "Releasing symbols\n"); /* Clean up the symbols */ if (shutdown_symbols() < 0) { fprintf(stderr, "Error releasing symbols!\n"); exit(EXIT_FAILURE); } /* end if */ /* Emit informational message */ if (verbose) fprintf(stderr, "Closing objects\n"); /* Close objects created */ if (H5Tclose(symbol_tid) < 0) { fprintf(stderr, "Error closing symbol datatype!\n"); exit(EXIT_FAILURE); } /* end if */ return 0; }