Simplified distributed block storage with strong consistency, like in Ceph
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

131 lines
3.8 KiB

  1. // Copyright (c) Vitaliy Filippov, 2019+
  2. // License: VNPL-1.0 (see README.md for details)
  3. #include <malloc.h>
  4. #include "timerfd_interval.h"
  5. #include "blockstore.h"
  6. int main(int narg, char *args[])
  7. {
  8. blockstore_config_t config;
  9. config["meta_device"] = "./test_meta.bin";
  10. config["journal_device"] = "./test_journal.bin";
  11. config["data_device"] = "./test_data.bin";
  12. ring_loop_t *ringloop = new ring_loop_t(512);
  13. blockstore_t *bs = new blockstore_t(config, ringloop);
  14. timerfd_interval tick_tfd(ringloop, 1, []()
  15. {
  16. printf("tick 1s\n");
  17. });
  18. blockstore_op_t op;
  19. int main_state = 0;
  20. uint64_t version = 0;
  21. ring_consumer_t main_cons;
  22. op.callback = [&](blockstore_op_t *op)
  23. {
  24. printf("op completed %d\n", op->retval);
  25. if (main_state == 1)
  26. main_state = 2;
  27. else if (main_state == 3)
  28. main_state = 4;
  29. else if (main_state == 5)
  30. main_state = 6;
  31. else if (main_state == 7)
  32. main_state = 8;
  33. else if (main_state == 9)
  34. main_state = 10;
  35. };
  36. main_cons.loop = [&]()
  37. {
  38. if (main_state == 0)
  39. {
  40. if (bs->is_started())
  41. {
  42. printf("init completed\n");
  43. op.opcode = BS_OP_WRITE;
  44. op.oid = { .inode = 1, .stripe = 0 };
  45. op.version = 0;
  46. op.offset = 16384;
  47. op.len = 4096;
  48. op.buf = (uint8_t*)memalign(512, 128*1024);
  49. memset(op.buf, 0xaa, 4096);
  50. bs->enqueue_op(&op);
  51. main_state = 1;
  52. }
  53. }
  54. else if (main_state == 2)
  55. {
  56. printf("version %lu written, syncing\n", op.version);
  57. version = op.version;
  58. op.opcode = BS_OP_SYNC;
  59. bs->enqueue_op(&op);
  60. main_state = 3;
  61. }
  62. else if (main_state == 4)
  63. {
  64. printf("stabilizing version %lu\n", version);
  65. op.opcode = BS_OP_STABLE;
  66. op.len = 1;
  67. *((obj_ver_id*)op.buf) = {
  68. .oid = { .inode = 1, .stripe = 0 },
  69. .version = version,
  70. };
  71. bs->enqueue_op(&op);
  72. main_state = 5;
  73. }
  74. else if (main_state == 6)
  75. {
  76. printf("stabilizing version %lu\n", version);
  77. op.opcode = BS_OP_STABLE;
  78. op.len = 1;
  79. *((obj_ver_id*)op.buf) = {
  80. .oid = { .inode = 1, .stripe = 0 },
  81. .version = version,
  82. };
  83. bs->enqueue_op(&op);
  84. main_state = 7;
  85. }
  86. else if (main_state == 8)
  87. {
  88. printf("reading 0-128K\n");
  89. op.opcode = BS_OP_READ;
  90. op.oid = { .inode = 1, .stripe = 0 };
  91. op.version = UINT64_MAX;
  92. op.offset = 0;
  93. op.len = 128*1024;
  94. bs->enqueue_op(&op);
  95. main_state = 9;
  96. }
  97. else if (main_state == 10)
  98. {
  99. void *cmp = memalign(512, 128*1024);
  100. memset(cmp, 0, 128*1024);
  101. memset(cmp+16384, 0xaa, 4096);
  102. int ok = 1;
  103. for (int i = 0; i < 128*1024; i += 4096)
  104. {
  105. if (memcmp(cmp+i, op.buf+i, 4096) != 0)
  106. {
  107. printf("bitmap works incorrectly, bytes %d - %d differ (%02x, should be %02x)\n", i, i+4096, ((uint8_t*)op.buf)[i], ((uint8_t*)cmp)[i]);
  108. ok = 0;
  109. }
  110. }
  111. if (ok)
  112. printf("bitmap works correctly\n");
  113. free(cmp);
  114. main_state = 11;
  115. }
  116. };
  117. ringloop->register_consumer(&main_cons);
  118. while (1)
  119. {
  120. ringloop->loop();
  121. ringloop->wait();
  122. }
  123. delete bs;
  124. delete ringloop;
  125. return 0;
  126. }