1
#include <glusterfs/api/glfs.h>
2
#include <glusterfs/api/glfs-handles.h>
11
* 1. A client may have multiple lease keys, but a lease key cannot be shared by
13
* 2. Lease key can be set before open, or in glfs_lease request. A lease key
14
* set like this is valid for the lifetime of the fd, i.e. a fd cannot have
15
* multiple lease key. But a lease key can be shared across multiple fds.
17
glfs_t *client1 = NULL, *client2 = NULL, *client3 = NULL, *client4 = NULL;
18
glfs_fd_t *fd1 = NULL, *fd2 = NULL, *fd3 = NULL, *fd4 = NULL;
20
char lid1[GLFS_LEASE_ID_SIZE] = "lid1-clnt1",
21
lid2[GLFS_LEASE_ID_SIZE] = "lid2-clnt2";
22
char lid3[GLFS_LEASE_ID_SIZE] = "lid3-clnt2", lid4[GLFS_LEASE_ID_SIZE] = {
25
char *volname = NULL, *glfs_log_file = NULL;
29
#define TEST_FILE "/test/lease"
35
recall_cbk(struct glfs_lease lease, void *data);
38
set_read_lease(glfs_fd_t *fd, char ld[])
40
struct glfs_lease lease = {
45
memset(&lease, 0, sizeof(lease));
46
lease.cmd = GLFS_SET_LEASE;
47
lease.lease_type = GLFS_RD_LEASE;
48
memcpy(&lease.lease_id, ld, GLFS_LEASE_ID_SIZE);
49
ret = glfs_lease(fd, &lease, &recall_cbk, fd);
51
fprintf(log_file, "\n RD_LEASE failed with ret: %d (%s)", ret,
55
fprintf(log_file, "\n Took RD_LEASE");
60
set_write_lease(glfs_fd_t *fd, char ld[])
62
struct glfs_lease lease = {
67
memset(&lease, 0, sizeof(lease));
68
lease.cmd = GLFS_SET_LEASE;
69
lease.lease_type = GLFS_RW_LEASE;
70
memcpy(&lease.lease_id, ld, GLFS_LEASE_ID_SIZE);
71
ret = glfs_lease(fd, &lease, &recall_cbk, NULL);
73
fprintf(log_file, "\n RW_LEASE failed with ret: %d (%s)", ret,
77
fprintf(log_file, "\n Took RW_LEASE");
82
get_lease(glfs_fd_t *fd, char ld[])
84
struct glfs_lease lease = {
89
memset(&lease, 0, sizeof(lease));
90
lease.cmd = GLFS_GET_LEASE;
91
lease.lease_type = -1;
92
memcpy(&lease.lease_id, ld, GLFS_LEASE_ID_SIZE);
93
ret = glfs_lease(fd, &lease, &recall_cbk, NULL);
95
fprintf(log_file, "\n GET_LEASE failed with ret: %d (%s)", ret,
99
if (lease.lease_type == GLFS_RD_LEASE)
100
fprintf(log_file, "\n Esisting Lease: RD_LEASE");
101
else if (lease.lease_type == GLFS_RW_LEASE)
102
fprintf(log_file, "\n Esisting Lease: RW_LEASE");
103
else if (lease.lease_type == 3)
104
fprintf(log_file, "\n Esisting Lease: RD_LEASE|RW_LEASE");
105
else if (lease.lease_type == 0)
106
fprintf(log_file, "\n Esisting Lease: NONE");
108
fprintf(log_file, "\n Existing lease type:%d", lease.lease_type);
109
return lease.lease_type;
113
unlk_write_lease(glfs_fd_t *fd, char ld[])
115
struct glfs_lease lease = {
120
memset(&lease, 0, sizeof(lease));
121
lease.cmd = GLFS_UNLK_LEASE;
122
lease.lease_type = GLFS_RW_LEASE;
123
memcpy(&lease.lease_id, ld, GLFS_LEASE_ID_SIZE);
124
ret = glfs_lease(fd, &lease, &recall_cbk, NULL);
126
fprintf(log_file, "\n Unlock RW_LESAE failed with ret: %d (%s)", ret,
130
fprintf(log_file, "\n Unlocked RW_LEASE");
135
unlk_read_lease(glfs_fd_t *fd, char ld[])
137
struct glfs_lease lease = {
142
memset(&lease, 0, sizeof(lease));
143
lease.cmd = GLFS_UNLK_LEASE;
144
lease.lease_type = GLFS_RD_LEASE;
145
memcpy(&lease.lease_id, ld, GLFS_LEASE_ID_SIZE);
147
ret = glfs_lease(fd, &lease, &recall_cbk, NULL);
149
fprintf(log_file, "\n Unlock RD_LEASE failed with ret: %d (%s)", ret,
153
fprintf(log_file, "\n Unlocked RD_LEASE");
158
setup_new_client(char *volname, char *log_fileile)
163
fs = glfs_new(volname);
165
fprintf(log_file, "\nglfs_new: returned NULL (%s)\n", strerror(errno));
169
ret = glfs_set_volfile_server(fs, "tcp", "localhost", 24007);
171
fprintf(log_file, "\nglfs_set_volfile_server failed ret:%d (%s)\n", ret,
176
ret = glfs_set_logging(fs, log_fileile, 7);
178
fprintf(log_file, "\nglfs_set_logging failed with ret: %d (%s)\n", ret,
185
fprintf(log_file, "\nglfs_init failed with ret: %d (%s)\n", ret,
194
#define OPEN(client, flags, fd, lease_id) \
197
ret_val = glfs_setfsleaseid(lease_id); \
200
"\nglfs_setfsleaseid failed with ret: %d (%s)\n", ret, \
204
fd = glfs_open(client, TEST_FILE, flags); \
206
fprintf(log_file, "\nglfs_open failed with ret: %d (%s)\n", ret, \
212
#define VERIFY_RESULT(test_case, ret, value) \
214
if (ret != value) { \
216
"\n Testcase %d failed, ret = %d, value=%d\n", \
217
test_case, ret, value); \
218
goto error; /*test unsuccessful*/ \
220
fprintf(log_file, "\n Testcase %d Succeeded\n", test_case); \
224
recall_cbk(struct glfs_lease lease, void *data)
227
char ld[GLFS_LEASE_ID_SIZE] = "";
229
fprintf(log_file, "\nRECALL received on lease_id:(%s)", lease.lease_id);
230
memcpy(ld, lease.lease_id, GLFS_LEASE_ID_SIZE);
231
ret = unlk_write_lease((glfs_fd_t *)data, ld);
232
VERIFY_RESULT(500, ret, SHUD_PASS);
240
glfs_fd_t *fd1 = NULL;
243
fprintf(log_file, "\n Basic test case for Read lease:");
244
/* Open fd on client 1 in RD mode */
245
OPEN(client1, O_RDONLY, fd1, lid1);
246
ret = set_write_lease(fd1, lid1);
247
VERIFY_RESULT(1, ret, SHUD_FAIL);
249
ret = set_read_lease(fd1, lid1);
250
VERIFY_RESULT(2, ret, SHUD_PASS);
252
ret = get_lease(fd1, lid1);
253
VERIFY_RESULT(3, ret, GLFS_RD_LEASE);
255
ret = unlk_write_lease(fd1, lid1);
256
VERIFY_RESULT(4, ret, SHUD_FAIL);
258
ret = unlk_read_lease(fd1, lid1);
259
VERIFY_RESULT(5, ret, SHUD_PASS);
261
ret = get_lease(fd1, lid1);
262
VERIFY_RESULT(6, ret, NONE);
264
ret = unlk_read_lease(fd1, lid1);
265
VERIFY_RESULT(7, ret, SHUD_PASS);
267
ret = glfs_close(fd1);
268
VERIFY_RESULT(8, ret, SHUD_PASS);
278
glfs_fd_t *fd1 = NULL;
281
fprintf(log_file, "\n Basic test case for Write lease:");
282
/* Open fd on client 1 in WRonly mode */
283
OPEN(client1, O_WRONLY, fd1, lid1);
284
ret = set_read_lease(fd1, lid1);
285
VERIFY_RESULT(1, ret, SHUD_FAIL);
287
ret = unlk_write_lease(fd1, lid1);
288
VERIFY_RESULT(2, ret, SHUD_PASS);
290
ret = set_write_lease(fd1, lid1);
291
VERIFY_RESULT(3, ret, SHUD_PASS);
293
ret = get_lease(fd1, lid1);
294
VERIFY_RESULT(4, ret, GLFS_RW_LEASE);
296
ret = unlk_write_lease(fd1, lid1);
297
VERIFY_RESULT(5, ret, SHUD_PASS);
299
ret = get_lease(fd1, lid1);
300
VERIFY_RESULT(6, ret, NONE);
302
ret = unlk_read_lease(fd1, lid1);
303
VERIFY_RESULT(7, ret, SHUD_FAIL);
305
ret = glfs_close(fd1);
306
VERIFY_RESULT(8, ret, SHUD_PASS);
314
testcase3_rd_wr_lease()
316
glfs_fd_t *fd1 = NULL;
319
fprintf(log_file, "\n Basic test case for Read Write lease:");
320
/* Open fd on client 1 in WRonly mode */
321
OPEN(client1, O_RDWR, fd1, lid1);
322
ret = set_read_lease(fd1, lid1);
323
VERIFY_RESULT(1, ret, SHUD_PASS);
325
ret = set_write_lease(fd1, lid1);
326
VERIFY_RESULT(2, ret, SHUD_PASS);
328
ret = get_lease(fd1, lid1);
329
VERIFY_RESULT(3, ret, (GLFS_RW_LEASE | GLFS_RD_LEASE));
331
ret = unlk_write_lease(fd1, lid1);
332
VERIFY_RESULT(4, ret, SHUD_PASS);
334
ret = get_lease(fd1, lid1);
335
VERIFY_RESULT(5, ret, GLFS_RD_LEASE);
337
ret = unlk_read_lease(fd1, lid1);
338
VERIFY_RESULT(6, ret, SHUD_PASS);
340
ret = get_lease(fd1, lid1);
341
VERIFY_RESULT(7, ret, NONE);
343
ret = glfs_close(fd1);
344
VERIFY_RESULT(8, ret, SHUD_PASS);
352
testcase4_rd_lease_multi_clnt()
354
glfs_fd_t *fd1 = NULL;
355
glfs_fd_t *fd2 = NULL;
358
fprintf(log_file, "\n Basic test case for multi client Read lease:");
360
/* Open fd on client 1 in RD mode */
361
OPEN(client1, O_RDONLY, fd1, lid1);
363
/* Open fd on client 2 in RW mode */
364
OPEN(client2, O_RDONLY, fd2, lid2);
366
ret = set_read_lease(fd1, lid1);
367
VERIFY_RESULT(1, ret, SHUD_PASS);
369
ret = set_read_lease(fd2, lid2);
370
VERIFY_RESULT(2, ret, SHUD_PASS);
372
ret = get_lease(fd1, lid1);
373
VERIFY_RESULT(3, ret, GLFS_RD_LEASE);
375
ret = unlk_read_lease(fd1, lid1);
376
VERIFY_RESULT(4, ret, SHUD_PASS);
378
ret = unlk_read_lease(fd2, lid2);
379
VERIFY_RESULT(5, ret, SHUD_PASS);
381
ret = get_lease(fd1, lid1);
382
VERIFY_RESULT(6, ret, NONE);
384
ret = get_lease(fd2, lid2);
385
VERIFY_RESULT(7, ret, NONE);
387
ret = glfs_close(fd1);
388
VERIFY_RESULT(8, ret, SHUD_PASS);
390
ret = glfs_close(fd2);
391
VERIFY_RESULT(9, ret, SHUD_PASS);
399
testcase5_openfd_multi_lid()
401
glfs_fd_t *fd1 = NULL;
402
glfs_fd_t *fd2 = NULL;
403
glfs_fd_t *fd3 = NULL;
406
fprintf(log_file, "\n Basic test case for multi lid openfd check:");
408
/* Open fd on client 1 in RD mode */
409
OPEN(client1, O_RDONLY, fd1, lid1);
411
/* Open fd on client 2 in RW mode */
412
OPEN(client2, O_RDWR, fd2, lid2);
413
OPEN(client2, O_RDWR, fd3, lid2);
415
ret = set_read_lease(fd1, lid1);
418
SHUD_FAIL); /*As there are other openfds in WR mode from diff lid*/
420
ret = set_write_lease(fd2, lid2);
422
2, ret, SHUD_FAIL); /*As thers is another fd in RD mode from diff lid */
424
ret = glfs_close(fd1);
425
VERIFY_RESULT(3, ret, SHUD_PASS);
427
ret = set_write_lease(fd2, lid2);
428
VERIFY_RESULT(4, ret, SHUD_PASS);
430
ret = unlk_write_lease(fd2, lid2);
431
VERIFY_RESULT(5, ret, SHUD_PASS);
433
ret = glfs_close(fd2);
434
VERIFY_RESULT(6, ret, SHUD_PASS);
436
ret = glfs_close(fd3);
437
VERIFY_RESULT(7, ret, SHUD_PASS);
445
testcase6_openfd_same_lid()
447
glfs_fd_t *fd1 = NULL;
448
glfs_fd_t *fd2 = NULL;
449
glfs_fd_t *fd3 = NULL;
452
fprintf(log_file, "\n Basic test case for same lid openfd check:");
454
/* Open fd on client 2 in RW mode */
455
OPEN(client1, O_RDWR, fd1, lid2);
456
OPEN(client1, O_RDWR, fd2, lid2);
458
ret = set_write_lease(fd1, lid2);
459
VERIFY_RESULT(4, ret, SHUD_PASS);
461
ret = set_write_lease(fd2, lid2);
462
VERIFY_RESULT(4, ret, SHUD_PASS);
464
ret = set_read_lease(fd2, lid2);
465
VERIFY_RESULT(4, ret, SHUD_PASS);
467
ret = unlk_write_lease(fd1, lid2);
468
VERIFY_RESULT(5, ret, SHUD_PASS);
470
ret = unlk_read_lease(fd2, lid2);
471
VERIFY_RESULT(5, ret, SHUD_PASS);
473
ret = unlk_write_lease(fd2, lid2);
474
VERIFY_RESULT(5, ret, SHUD_PASS);
476
ret = glfs_close(fd1);
477
VERIFY_RESULT(6, ret, SHUD_PASS);
479
ret = glfs_close(fd2);
480
VERIFY_RESULT(7, ret, SHUD_PASS);
488
testcase7_rd_multi_lid()
490
glfs_fd_t *fd1 = NULL;
491
glfs_fd_t *fd2 = NULL;
494
fprintf(log_file, "\n Basic test case for multi lease id Read lease:");
496
/* Open fd on client 1 in RD mode */
497
OPEN(client2, O_RDONLY, fd1, lid2);
499
/* Open fd on client 2 in RD mode */
500
OPEN(client2, O_RDONLY, fd2, lid3);
502
ret = set_read_lease(fd1, lid2);
503
VERIFY_RESULT(1, ret, SHUD_PASS);
505
ret = set_read_lease(fd2, lid3);
506
VERIFY_RESULT(2, ret, SHUD_PASS);
508
ret = get_lease(fd1, lid2);
509
VERIFY_RESULT(3, ret, GLFS_RD_LEASE);
511
ret = unlk_read_lease(fd1, lid2);
512
VERIFY_RESULT(4, ret, SHUD_PASS);
514
ret = unlk_read_lease(fd2, lid3);
515
VERIFY_RESULT(5, ret, SHUD_PASS);
517
ret = get_lease(fd1, lid2);
518
VERIFY_RESULT(6, ret, NONE);
520
ret = get_lease(fd2, lid3);
521
VERIFY_RESULT(7, ret, NONE);
523
ret = glfs_close(fd1);
524
VERIFY_RESULT(8, ret, SHUD_PASS);
526
ret = glfs_close(fd2);
527
VERIFY_RESULT(9, ret, SHUD_PASS);
535
testcase8_client_disconnect()
537
glfs_fd_t *fd1 = NULL;
538
glfs_fd_t *fd2 = NULL;
541
fprintf(log_file, "\n Basic test case for client disconnect cleanup");
543
/* Open fd on client 1 in RD mode */
544
OPEN(client1, O_RDWR, fd1, lid1);
546
ret = set_read_lease(fd1, lid1);
547
VERIFY_RESULT(1, ret, SHUD_PASS);
549
ret = get_lease(fd1, lid1);
550
VERIFY_RESULT(2, ret, GLFS_RD_LEASE);
552
ret = set_write_lease(fd1, lid1);
553
VERIFY_RESULT(3, ret, SHUD_PASS);
555
ret = get_lease(fd1, lid1);
556
VERIFY_RESULT(4, ret, (GLFS_RD_LEASE | GLFS_RW_LEASE));
558
ret = glfs_fini(client1);
559
VERIFY_RESULT(5, ret, SHUD_PASS);
561
/* Open fd on client 2 in RD mode */
562
OPEN(client2, O_RDONLY, fd2, lid3);
564
ret = get_lease(fd2, lid3);
565
VERIFY_RESULT(6, ret, NONE);
567
ret = glfs_close(fd2);
568
VERIFY_RESULT(7, ret, SHUD_PASS);
570
client1 = setup_new_client(volname, glfs_log_file);
578
testcase9_recall_conflict_lease()
580
struct glfs_object *obj = NULL;
581
glfs_fd_t *fd1 = NULL;
583
struct glfs_lease lease = {
588
"\n Basic test case for conflicting lease causing recall");
590
memset(&lease, 0, sizeof(lease));
591
lease.cmd = GLFS_SET_LEASE;
592
lease.lease_type = GLFS_RD_LEASE;
593
memcpy(&lease.lease_id, lid2, GLFS_LEASE_ID_SIZE);
594
/* Open fd on client 1 in RD mode */
595
OPEN(client1, O_RDWR, fd1, lid1);
596
ret = set_write_lease(fd1, lid1);
597
VERIFY_RESULT(1, ret, SHUD_PASS);
599
obj = glfs_h_lookupat(client2, NULL, TEST_FILE, NULL, 0);
600
ret = glfs_h_lease(client2, obj, &lease);
601
VERIFY_RESULT(2, ret, SHUD_FAIL);
603
ret = unlk_write_lease(fd1, lid1);
604
VERIFY_RESULT(5, ret, SHUD_PASS);
607
ret = glfs_h_close(obj);
608
VERIFY_RESULT(3, ret, SHUD_PASS);
609
ret = glfs_close(fd1);
610
VERIFY_RESULT(4, ret, SHUD_PASS);
618
testcase10_recall_open_conflict()
620
glfs_fd_t *fd1 = NULL;
621
glfs_fd_t *fd2 = NULL;
624
fprintf(log_file, "\n Basic test case for conflicting open causing recall");
626
/* Open fd on client 1 in RW mode */
627
OPEN(client1, O_RDWR, fd1, lid1);
629
ret = set_write_lease(fd1, lid1);
630
VERIFY_RESULT(1, ret, SHUD_PASS);
632
/* Open fd on client 1 in RW mode */
633
OPEN(client2, O_RDWR, fd2, lid2);
635
/* TODO: Check for recall cbk functionality */
636
ret = glfs_close(fd1);
637
VERIFY_RESULT(2, ret, SHUD_PASS);
639
ret = glfs_close(fd2);
640
VERIFY_RESULT(3, ret, SHUD_PASS);
648
main(int argc, char *argv[])
652
glfs_fd_t *fd = NULL;
653
glfs_fd_t *fd1 = NULL;
654
char *topdir = "topdir", *filename = "file1";
657
ssize_t xattr_size = -1;
661
"Expect following args %s <Vol> <glfs client log file> "
662
"<testcase log file>\n",
667
log_file = fopen(argv[3], "w");
672
glfs_log_file = argv[2];
674
/* Setup 3 clients */
675
client1 = setup_new_client(volname, glfs_log_file);
676
client2 = setup_new_client(volname, glfs_log_file);
677
client3 = setup_new_client(volname, glfs_log_file);
679
ret = testcase1_rd_lease();
680
VERIFY_RESULT(101, ret, SHUD_PASS);
682
ret = testcase2_wr_lease();
683
VERIFY_RESULT(102, ret, SHUD_PASS);
685
ret = testcase3_rd_wr_lease();
686
VERIFY_RESULT(103, ret, SHUD_PASS);
688
ret = testcase4_rd_lease_multi_clnt();
689
VERIFY_RESULT(104, ret, SHUD_PASS);
691
ret = testcase5_openfd_multi_lid();
692
VERIFY_RESULT(105, ret, SHUD_PASS);
694
ret = testcase6_openfd_same_lid();
695
VERIFY_RESULT(106, ret, SHUD_PASS);
697
ret = testcase7_rd_multi_lid();
698
VERIFY_RESULT(107, ret, SHUD_PASS);
700
ret = testcase8_client_disconnect();
701
VERIFY_RESULT(108, ret, SHUD_PASS);
703
ret = testcase9_recall_conflict_lease();
704
VERIFY_RESULT(109, ret, SHUD_PASS);
706
ret = testcase10_recall_open_conflict();
707
VERIFY_RESULT(110, ret, SHUD_PASS);