summaryrefslogtreecommitdiffstats
path: root/tools/h5recover/enable_journaling.c
blob: 1993024a934eb6ec936213baad94bf2fc5c0f69e (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * 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.
 */

#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 TIMESTEPNAME "TimeStep"
#define NX     10                      /* dataset initial dimensions */
#define NY     10
#define CHUNKX 2			/* chunk dimensions */
#define CHUNKY 10
#define RANK   2
#define THRESHOLD       256
#define ALIGNMENT       512

/* Global variables */

/* protocols */
int writedata(hid_t file, hid_t dataset, int begin, int end, hid_t timestep);
void helppage(void);
int set_journal(hid_t faccpl, const char * journalname);

/* Display the online help page */
void
helppage(void)
{
    printf(
	"Usage:\n"
	"%s -[c|w]\n"
	"\t-c\tCreate a new file (%s)\n"
	"\t-w\tWrite more rows to the file with Journaling (%s) for crash test\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 -w\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 %s (This should succeed)\n", H5dumptoolname, H5FILE_NAME);
}


int
set_journal(hid_t faccpl, const char * journalname) 
{
    H5AC2_jnl_config_t jnl_config;

    /* get current journaling configuration */
    jnl_config.version = H5AC2__CURR_JNL_CONFIG_VER;

    if ( H5Pget_jnl_config(faccpl, &jnl_config) < 0 ) {
	fprintf(stderr, "H5Pget_jnl_config on faccpl failed\n");
	return(-1);
    }

    jnl_config.enable_journaling = 1;   /* turn on journaling */
    jnl_config.journal_recovered = 0;
    jnl_config.jbrb_buf_size = 8*1024;  /* multiples of sys buffer size*/
    jnl_config.jbrb_num_bufs = 2;
    jnl_config.jbrb_use_aio = 0;        /* only sync IO is supported */
    jnl_config.jbrb_human_readable = 1; /* only readable form is supported */
    strcpy(jnl_config.journal_file_path, journalname);

    if ( H5Pset_jnl_config(faccpl, &jnl_config) < 0 ) {
	fprintf(stderr, "H5Pset_jnl_config on faccpl failed\n");
	return(-1);
    }
    return(0);
}


int
main (int ac, char **av)
{
    hid_t       file, dataset;         /* file and dataset handles */
    hid_t       dataspace;   			/* data array handle */
    hid_t       timestep;   			/* timestep handle */
    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 */
    int		cmode=0;		/* Create mode, overrides the others. */
    int		wmode=0;		/* write mod, default no. */


    /* Parse different options:
     * Default: Create a new file and new dataset.
     * wmode: write more rows to an existing file with Journaling.
     *
     * How to use this:
     * ./enable_journaling	# create JournalEG.h5 file
     * ./enable_journaling -w	# reopen JournalEG.h5 with Journaling on and
     *			        # add more rows, then crash. JournalEG.h5
     *				# is not readable as an HDF5 file.
     * ./h5recover -j JournalEG.h5.jnl JournalEG.h5	# Recover the file.
     * 
     * 
     */
    if (ac<=1){
	helppage();
	return 1;
    }
    while (ac > 1){
	ac--;
	av++;
        if (strcmp("-c", *av) == 0){
	    cmode++;
	    printf("Create mode\n");
	}else if (strcmp("-w", *av) == 0){
	    wmode++;
	    printf("write mode\n");
	}else{
	    fprintf(stderr, "Unknown option (%s)\n", *av);
	    helppage();
	    return 1 ;
	}
    }

    /* Create a file access property list to be used by both create and 
     * reopen modes.
     * Use latest lib version needed by the Journaling feature.
     */
    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);
    }
    /* Use alignment so that it is easier to octo dump the data file for */
    /* debugging purpose. */
    if(H5Pset_alignment(faccpl, (hsize_t)THRESHOLD, (hsize_t)ALIGNMENT) < 0){
	fprintf(stderr, "H5Pset_alignment on data file failed\n");
	H5Pclose(faccpl);
	return(1);
    }

    if (cmode){
	/*===================================================
	 * Default:
	 * Create a new file, create a new dataset
	 * of unlimited dimension, initialize size to NX*NY, write data,
         * close file.
	 *===================================================*/
	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);
	H5Sclose(dataspace);
	H5Pclose(dsetpl);

	/* Also create a simple 1d file attribute to store the timestep counter. */
	dimsf[0] = 1;
	dataspace = H5Screate_simple(1, dimsf, NULL);
	timestep = H5Acreate2(file, TIMESTEPNAME, H5T_NATIVE_INT, dataspace,
			    H5P_DEFAULT, H5P_DEFAULT);

	/* write the initial NX rows of data and timestep counter */
	writedata(file, dataset, 0, NX-1, timestep);

	H5Sclose(dataspace);
	H5Dclose(dataset);
	H5Aclose(timestep);
	H5Fclose(file);
	return(0);
    }
    if (wmode){
	/*===================================================
	 * wmode:
	 *    Reopen a previous file with Journaling on, extend the dataset
	 *    to 4NX rows, write data, crash.
	 *===================================================*/

	/* reopen the file with Journaling on. */
	/* Setup file access property list with journaling */

	/* change the following 1 to 0 to see the effect of no journaling. */
#if 1
	if (set_journal(faccpl, H5JournalFILE_NAME) < 0){
            fprintf(stderr, "set_journal on data file failed\n");
            H5Pclose(faccpl);
	    return(1);
	}
#endif
	/* 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);

	/* Open dataset and timestep for modifications. */
	dataset=H5Dopen2(file, DATASETNAME, H5P_DEFAULT);
	timestep=H5Aopen(file, TIMESTEPNAME, H5P_DEFAULT);

	/* write data to new rows and crash */
	/* Do 3 writes to generate at least 3 transactions. */
	writedata(file, dataset, NX, 2*NX-1, timestep);
	writedata(file, dataset, 2*NX, 3*NX-1, timestep);
	writedata(file, dataset, 3*NX, 4*NX-1, timestep);

	/* close all remaining handles. */
	H5Dclose(dataset);
	H5Aclose(timestep);
	H5Fclose(file);
	return(0);
    }
    return(0);
}


/* writedata:
 * write rows of data to dataset starting from begin to end rows inclusive.
 */
int
writedata(hid_t file, hid_t dataset, int begin, int end, hid_t timestep)
{
    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;
    int		timecounter;


    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);
    }
    /* extend the dataset to end rows. */
    dims[0] = end+1;
    dims[1] = NY;
    H5Dset_extent(dataset, dims);

    /* setup memory space. */
    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.
     * Flush all data.
     * Finally, write the time step counter.
     */
    retcode = H5Dwrite(dataset, H5T_NATIVE_INT, memspace, dataspace, H5P_DEFAULT, data);

    /* Pause 2 seconds to allow manual abort. */
    timecounter=end;
    fprintf(stderr,
	"After data write for time step %d. Simulate calculation...", timecounter);
    sleep(2);
    fprintf(stderr, "\n");

    H5Sclose(memspace);
    H5Sclose(dataspace);
    H5Fflush(file, H5F_SCOPE_GLOBAL);
    fprintf(stderr, "File data flushed.\n");
    retcode = H5Awrite(timestep, H5T_NATIVE_INT, &timecounter);
    fprintf(stderr, "Time step counter updated.\n");
    
    
    return(retcode);
}