Whamcloud - gitweb
land b_eq on HEAD
[fs/lustre-release.git] / lustre / liblustre / tests / replay_ost_single.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light user test program
5  *
6  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define _BSD_SOURCE
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <sys/queue.h>
35 #include <signal.h>
36
37 #include <sysio.h>
38 #include <mount.h>
39
40 #include "test_common.h"
41
42
43
44 static char mds_server[1024] = {0,};
45 static char barrier_script[1024] = {0,};
46 static char failover_script[1024] = {0,};
47 static char barrier_cmd[1024] = {0,};
48 static char failover_cmd[1024] = {0,};
49
50 static void replay_barrier()
51 {
52         int rc;
53
54         if ((rc = system(barrier_cmd))) {
55                 printf("excute barrier error: %d\n", rc);
56                 exit(rc);
57         }
58 }
59
60 static void mds_failover()
61 {
62         int rc;
63
64         if ((rc = system(failover_cmd))) {
65                 printf("excute failover error: %d\n", rc);
66                 exit(rc);
67         }
68 }
69
70
71 #define ENTRY(str)                                                      \
72         do {                                                            \
73                 char buf[100];                                          \
74                 int len;                                                \
75                 sprintf(buf, "===== START: %s ", (str));                \
76                 len = strlen(buf);                                      \
77                 if (len < 79) {                                         \
78                         memset(buf+len, '=', 100-len);                  \
79                         buf[79] = '\n';                                 \
80                         buf[80] = 0;                                    \
81                 }                                                       \
82                 printf("%s", buf);                                      \
83         } while (0)
84
85 #define LEAVE()                                                         \
86         do {                                                            \
87                 printf("----- END TEST successfully ---");              \
88                 printf("-----------------------------");                \
89                 printf("-------------------\n");                        \
90         } while (0)
91
92 void t0()
93 {
94         const int bufsize = 4096;
95         char *path = "/mnt/lustre/rp_ost_t0_file";
96         char buf[bufsize];
97         int fd, i, j, rc;
98         ENTRY("open-failover-write-verification (no ping involved)");
99
100         printf("create/open file...\n");
101         t_touch(path);
102         fd = t_open(path);
103         printf("OST failover...\n");
104         replay_barrier();
105         mds_failover();
106
107         printf("write file...\n");
108         for (i = 0; i < 20; i++) {
109                 memset(buf, i, bufsize);
110                 if ((rc = write(fd, buf, bufsize)) != bufsize) {
111                         perror("write error after failover");
112                         printf("i = %d, rc = %d\n", i, rc);
113                         exit(-1);
114                 }
115         }
116
117         /* verify */
118         printf("read & verify...\n");
119         lseek(fd, 0, SEEK_SET);
120         for (i = 0; i < 20; i++) {
121                 memset(buf, -1, bufsize);
122                 if ((rc = read(fd, buf, bufsize)) != bufsize) {
123                         perror("read error rc");
124                         printf("i = %d, rc = %d\n", i, rc);
125                         exit(-1);
126                 }
127                 for (j = 0; j < bufsize; j++) {
128                         if (buf[j] != i) {
129                                 printf("verify error!\n");
130                                 exit(-1);
131                         }
132                 }
133         }
134         t_close(fd);
135         t_unlink(path);
136         LEAVE();
137 }
138
139 void t1()
140 {
141         const int bufsize = 4096;
142         char *path = "/mnt/lustre/rp_ost_t1_file";
143         char buf[bufsize];
144         int fd, i, j;
145         ENTRY("open-write-close-open-failover-read (no ping involved)");
146
147         printf("create/open file...\n");
148         t_touch(path);
149         fd = t_open(path);
150         printf("write file...\n");
151         for (i = 0; i < 20; i++) {
152                 memset(buf, i, bufsize);
153                 if (write(fd, buf, bufsize) != bufsize) {
154                         perror("write error");
155                         exit(-1);
156                 }
157         }
158         printf("close/reopen...\n");
159         t_close(fd);
160         fd = t_open(path);
161         lseek(fd, 0, SEEK_SET);
162
163         printf("OST failover...\n");
164         replay_barrier();
165         mds_failover();
166
167         printf("read & verify...\n");
168         for (i = 0; i < 20; i++) {
169                 memset(buf, -1, bufsize);
170                 if (read(fd, buf, bufsize) != bufsize) {
171                         perror("read error after failover");
172                         exit(-1);
173                 }
174                 for (j = 0; j < bufsize; j++) {
175                         if (buf[j] != i) {
176                                 printf("verify error after failover\n");
177                                 exit(-1);
178                         }
179                 }
180         }
181
182         t_close(fd);
183         t_unlink(path);
184         LEAVE();
185 }
186
187 void t2()
188 {
189         char *path = "/mnt/lustre/rp_ost_t2_file";
190         char *str = "xxxxjoiwlsdf98lsjdfsjfoajflsjfajfoaidfojaj08eorje;";
191         ENTRY("empty replay");
192
193         replay_barrier();
194         mds_failover();
195
196         t_echo_create(path, str);
197         t_grep(path, str);
198         t_unlink(path);
199 }
200
201 void t3()
202 {
203         char *path = "/mnt/lustre/rp_ost_t3_file";
204         char *str = "xxxxjoiwlsdf98lsjdfsjfoajflsjfajfoaidfojaj08eorje;";
205         ENTRY("touch");
206
207         printf("touch to create a file\n");
208         t_echo_create(path, str);
209         replay_barrier();
210         mds_failover();
211
212         printf("read & verify\n");
213         t_grep(path, str);
214         t_unlink(path);
215         /* XXX have problem without this, seems server side problem XXX */
216         sleep(5);
217 }
218
219 void t4()
220 {
221         char *path = "/mnt/lustre/rp_ost_t4_file";
222         char namebuf[1024];
223         char str[1024];
224         int count = 10, i;
225         ENTRY("|X| 10 open(CREAT)s (ping involved)");
226
227         printf("create %d files\n", count);
228         for (i = 0; i < count; i++) {
229                 sprintf(namebuf, "%s%02d", path, i);
230                 sprintf(str, "%s-%08d-%08x-AAAAA", "content", i, i);
231                 t_echo_create(namebuf, str);
232         }
233         replay_barrier();
234         mds_failover();
235
236         printf("read & verify\n");
237         for (i = 0; i < count; i++) {
238                 sprintf(namebuf, "%s%02d", path, i);
239                 sprintf(str, "%s-%08d-%08x-AAAAA", "content", i, i);
240                 t_grep(namebuf, str);
241                 t_unlink(namebuf);
242         }
243 }
244
245 extern int portal_debug;
246 extern int portal_subsystem_debug;
247
248 extern void __liblustre_setup_(void);
249 extern void __liblustre_cleanup_(void);
250
251 void usage(const char *cmd)
252 {
253         printf("Usage: \t%s --target mdsnid:/mdsname/profile -s ost_hostname "
254                 "-b \"barrier cmd\" -f \"failover cmd\"\n", cmd);
255         printf("       \t%s --dumpfile dumpfile -s ost_hostname -b \"barrier cmd\" "
256                 "-f \"failover cmd\"\n", cmd);
257         exit(-1);
258 }
259
260 void test_ssh()
261 {
262         char cmd[1024];
263
264         sprintf(cmd, "ssh %s cat /dev/null", mds_server);
265         if (system(cmd)) {
266                 printf("ssh can't access server node: %s\n", mds_server);
267                 exit(-1);
268         }
269 }
270
271 int main(int argc, char * const argv[])
272 {
273         int opt_index, c;
274         static struct option long_opts[] = {
275                 {"target", 1, 0, 0},
276                 {"dumpfile", 1, 0, 0},
277                 {0, 0, 0, 0}
278         };
279
280         if (argc < 4)
281                 usage(argv[0]);
282
283         while ((c = getopt_long(argc, argv, "s:b:f:", long_opts, &opt_index)) != -1) {
284                 switch (c) {
285                 case 0: {
286                         if (!optarg[0])
287                                 usage(argv[0]);
288
289                         if (!strcmp(long_opts[opt_index].name, "target")) {
290                                 setenv(ENV_LUSTRE_MNTTGT, optarg, 1);
291                         } else if (!strcmp(long_opts[opt_index].name, "dumpfile")) {
292                                 setenv(ENV_LUSTRE_DUMPFILE, optarg, 1);
293                         } else
294                                 usage(argv[0]);
295                         break;
296                 }
297                 case 's':
298                         strcpy(mds_server, optarg);
299                         break;
300                 case 'b':
301                         strcpy(barrier_script, optarg);
302                         break;
303                 case 'f':
304                         strcpy(failover_script, optarg);
305                         break;
306                 default:
307                         usage(argv[0]);
308                 }
309         }
310
311         if (optind != argc)
312                 usage(argv[0]);
313         if (!strlen(mds_server) || !strlen(barrier_script) ||
314             !strlen(failover_script))
315                 usage(argv[0]);
316
317         test_ssh();
318
319         /* prepare remote command */
320         sprintf(barrier_cmd, "ssh %s \"%s\"", mds_server, barrier_script);
321         sprintf(failover_cmd, "ssh %s \"%s\"", mds_server, failover_script);
322
323         setenv(ENV_LUSTRE_TIMEOUT, "5", 1);
324
325         __liblustre_setup_();
326
327         t0();
328         t1();
329         t2();
330         t3();
331         t4();
332
333         printf("liblustre is about shutdown\n");
334         __liblustre_cleanup_();
335
336         printf("complete successfully\n");
337         return 0;
338 }