summaryrefslogtreecommitdiffstats
path: root/tools/h5recover/enable_journaling.c
blob: 3bd967bf85cb75c2c8c6a8d4ff8ce7145417b8d2 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * 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 files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
 * access to either file, you may request a copy from help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 * This example shows how to use the Journaling feature. It also simulates a
 * crash without closing the file first.
 * The patch mode reopens the file, defines the dataset again without writing
 * any data, then close the file.  This is a temporary fix till the object header
 * coding is working.
 */

#include "hdf5.h"
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>

#define H5FILE_NAME        	"JournalEG.h5"
#define H5JournalFILE_NAME 	H5FILE_NAME".jnl"
#define H5recovertoolname 	"h5recover"
#define H5dumptoolname 		"h5dump"
#define ProgName	 	"enable_journaling"	/* program name */
#define DATASETNAME "IntArray"
#define NX     20                      /* dataset initial dimensions */
#define NY     20
#define CHUNKX 2			/* chunk dimensions */
#define CHUNKY 2
#define RANK   2

/* Global variables */

/* protocols */
int writedata(hid_t dataset, int begin, int end);
void helppage(void);

/* Display the online help page */
void
helppage(void)
{
    printf(
	"Usage:\n"
	"%s -[c|r|p]\n"
	"\t-c\tCreate a new file (%s)\n"
	"\t-r\tReopen the file with Journaling (%s) for crash test\n"
	"\t-p\tPatch it with metadta of the added rows\n\n",
	ProgName, H5FILE_NAME, H5JournalFILE_NAME
    );
    printf("To try this program, run:\n");
    printf("\t%s -c\n", ProgName);
    printf("\t%s %s\n", H5dumptoolname, H5FILE_NAME);
    printf("\t%s -r\n", ProgName);
    printf("\t%s %s (This should fail)\n", H5dumptoolname, H5FILE_NAME);
    printf("\t%s -j %s %s\n", H5recovertoolname, H5JournalFILE_NAME, H5FILE_NAME);
    printf("\t%s -p\n", ProgName);
    printf("\t%s %s (This should show more data)\n", H5dumptoolname, H5FILE_NAME);
}

int
main (int ac, char **av)
{
    hid_t       file, dataset;         /* file and dataset handles */
    hid_t       dataspace;   			/* handles */
    hsize_t 	maxdims[RANK] = {H5S_UNLIMITED, NY}; /* Dataset dimensions */
						/* with unlimited rows. */
    hsize_t     dimsf[RANK]={NX, NY};           /* initial dataset dimensions */
    hsize_t     chunk[RANK]={CHUNKX, CHUNKY};	/* chunk dimensions */
    hid_t       dsetpl;			/* Dataset property list */
    hid_t       faccpl;			/* File access property list */
    pid_t	mypid;
    int		cmode=0;		/* Create mode, overrides the others. */
    int		pmode=0;		/* patch mode, default no. */
    int		rmode=0;		/* Reopen mod, default no. */


    /* Parse different options:
     * Default: Create a new file and new dataset.
     * rmode: Reopen an existing file with Journaling.
     * pmode: Patch mode (implies rmode but no data write nor crash).
     *
     * How to use this:
     * ./enable_journaling	# create JournalEG.h5 file
     * ./enable_journaling -r	# reopen JournalEG.h5 with Journaling on and
     *			        # add more rows, then crash.
     * ./h5recover -j JournalEG.h5.jnl JournalEG.h5	# to recover the file.
     * ./enable_journaling -p	# patch it with metadata of the added rows.
     * Then JournalEG.h5 should have all the expected written rows and data.
     * 
     */
    if (ac<=1){
	helppage();
	return 1;
    }
    while (ac > 1){
	ac--;
	av++;
        if (strcmp("-c", *av) == 0){
	    cmode++;
	    printf("Create mode\n");
	}else
	if (strcmp("-p", *av) == 0){
	    pmode++;
	    rmode++;
	    printf("Patch mode on\n");
	}else
        if (strcmp("-r", *av) == 0){
	    rmode++;
	    printf("Reopen mode\n");
	}else{
	    fprintf(stderr, "Unknown option (%s)\n", *av);
	    helppage();
	    return 1 ;
	}
    }

    if (cmode){
	/*===================================================
	 * Default:
	 * Create a new file with latest lib version, create a new dataset
	 * of unlimited dimension, initialize size to NX*NY, write data,
         * close file.
         * Needs the latest lib version in order to allow Journaling later.
	 *===================================================*/
	/*
	 * Create a new file using H5F_ACC_TRUNC access,
	 * default file creation properties, and latest lib version file
	 * access properties.
	 */
	faccpl = H5Pcreate(H5P_FILE_ACCESS);
	if (H5Pset_libver_bounds(faccpl, H5F_LIBVER_LATEST, H5F_LIBVER_LATEST) < 0){
	    fprintf(stderr, "H5Pset_libver_bounds on data file failed\n");
	    H5Pclose(faccpl);
	    return(-1);
	}
	file = H5Fcreate(H5FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, faccpl);
	H5Pclose(faccpl);

	/*
	 * create the data space for fixed size dataset.
	 * Initial dimension is NX*NY.
	 */
	dataspace = H5Screate_simple(RANK, dimsf, maxdims);

	/* Create dataset creation property list */
	dsetpl = H5Pcreate(H5P_DATASET_CREATE);
	
	/* Enable chunking needed for unlimited dimesion */
	H5Pset_layout(dsetpl, H5D_CHUNKED);
	H5Pset_chunk(dsetpl, 2, chunk);

	/*
	 * Create a new dataset of native int within the file using defined dataspace
	 * and chunked storage type.
	 */
	dataset = H5Dcreate2(file, DATASETNAME, H5T_NATIVE_INT, dataspace,
			    H5P_DEFAULT, dsetpl, H5P_DEFAULT);

	writedata(dataset, 0, NX-1);

	H5Sclose(dataspace);
	H5Pclose(dsetpl);
	H5Dclose(dataset);
	H5Fclose(file);
	return(0);
    }
    if (rmode || pmode){
	/*===================================================
	 * rmode:
	 *    Reopen a previous file with Journaling on, extend the dataset
	 *    to 2NX rows, write data, crash.
	 *    Need to turn off H5Pset_sieve_buf_size( hid_t fapl_id, hsize_t size  ) so that raw data will be flushed immediately.
	 * pmode:
	 *    Patch mode (similar to rmode but no data write nor crash).
	 *    Reopen a restored file, extend the dataset to 2NX rows,
	 *    do not write data, close file.
	 *===================================================*/

	if (pmode){
	    /* just reopen the file to patch it. */
	    file = H5Fopen(H5FILE_NAME, H5F_ACC_RDWR, H5P_DEFAULT);
	}else{
	    /* reopen the file with Journaling on. */
	    /* Setup file access property list with journaling */
	    faccpl = H5Pcreate(H5P_FILE_ACCESS);
	    if (H5Pset_journal(faccpl, H5JournalFILE_NAME) < 0){
		fprintf(stderr, "H5Pset_journal on data file failed\n");
		H5Pclose(faccpl);
		return(-1);
	    }
	    /* Turn off data sieving to get raw data flushed to file */
            /* immediately.                                          */
	    if (H5Pset_sieve_buf_size(faccpl, 0) < 0){
		fprintf(stderr, "H5Pset_sieve_buf_size on data file failed\n");
		H5Pclose(faccpl);
		return(-1);
	    }
	    /* Delete the journal file since journal code does not allow */
	    /* existed journal file. */
	    remove(H5JournalFILE_NAME);
	    /* open the file with journaling property list */
	    file = H5Fopen(H5FILE_NAME, H5F_ACC_RDWR, faccpl);

	    /* close handle not needed any more. */
	    H5Pclose(faccpl);
	}

	dataset=H5Dopen2(file, DATASETNAME, H5P_DEFAULT);
	/* extend the dataset to 2NX rows. */
	dimsf[0] = 2*NX;
	dimsf[1] = NY;
	H5Dset_extent(dataset, dimsf);

	if (!pmode){
	    /* write data to new rows and crash */
	    writedata(dataset, NX, 2*NX-1);
	    /* simulate a crash ending of the aiplication */
	    fprintf(stderr, "going to crash myself\n");
	    mypid = getpid();
	    kill(mypid, SIGTERM);	/* Terminate myself */
	}else{
	    H5Dclose(dataset);
	    H5Fclose(file);
	    return(0);
	}
    }
}


/* writedata():
 * write rows of data to dataset starting from begin to end rows inclusive.
 */
int
writedata(hid_t dataset, int begin, int end)
{
    int         data[NX][NY];          /* data to write */
    int         nrows, i, j;
    hid_t	memspace, dataspace;
    hsize_t	dims[RANK], start[RANK], count[RANK];
    herr_t	retcode;


    if ((end < begin) || (begin < 0)){
	fprintf(stderr, "writedata: bad arguments (begin=%d, end=%d)\n", begin, end);
	return(-1);
    }
    nrows = end-begin+1;
    if (nrows>NX){
	fprintf(stderr, "writedata: too many rows (begin=%d, end=%d, NX=%d)\n",
	    begin, end, NX);
	return(-1);
    }

    dims[0]=NX;
    dims[1]=NY;
    memspace = H5Screate_simple(RANK, dims, NULL);

    dataspace = H5Dget_space(dataset);
    start[0]=begin;
    start[1]=0;
    count[0]=nrows;
    count[1]=NY;
    if ((retcode = H5Sselect_hyperslab(dataspace, H5S_SELECT_SET, start,
		    NULL, count, NULL)) <0){
	fprintf(stderr, "H5Sselect_hyperslab failed\n");
	return(-1);
    };

    /* Initialize data buffer */
    for(i = 0; i < nrows; i++)
	for(j = 0; j < NY; j++)
	    data[i][j] = (i+begin)*NY + j;

    /*
     * Write the data to the dataset using default transfer properties.
     */
    retcode = H5Dwrite(dataset, H5T_NATIVE_INT, memspace, dataspace, H5P_DEFAULT, data);
    
    H5Sclose(memspace);
    H5Sclose(dataspace);
    return(retcode);
}