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.

211 lines
6.5 KiB

  1. /* -*- mode: C; c-basic-offset: 4 -*- */
  2. #ident "$Id$"
  3. #ident "Copyright (c) 2011 Tokutek Inc. All rights reserved."
  4. // generate fractal trees with a given height, fanout, and number of leaf elements per leaf.
  5. // jam the child buffers with inserts.
  6. // this code can be used as a template to build broken trees
  7. //
  8. // This program (copied from make-tree.c) creates a tree with bad msns by commenting out
  9. // the setting of the msn:
  10. //
  11. // To correctly set msn per node:
  12. // - set in each non-leaf when message is injected into node (see insert_into_child_buffer())
  13. // - set in each leaf node (see append_leaf())
  14. // - set in root node (set test_make_tree())
  15. #include "includes.h"
  16. #include <brt-cachetable-wrappers.h>
  17. #include "test.h"
  18. static BRTNODE
  19. make_node(BRT brt, int height) {
  20. BRTNODE node = NULL;
  21. int n_children = (height == 0) ? 1 : 0;
  22. toku_create_new_brtnode(brt, &node, height, n_children);
  23. if (n_children) BP_STATE(node,0) = PT_AVAIL;
  24. return node;
  25. }
  26. static void
  27. append_leaf(BRTNODE leafnode, void *key, size_t keylen, void *val, size_t vallen) {
  28. assert(leafnode->height == 0);
  29. DBT thekey; toku_fill_dbt(&thekey, key, keylen);
  30. DBT theval; toku_fill_dbt(&theval, val, vallen);
  31. // get an index that we can use to create a new leaf entry
  32. uint32_t idx = toku_omt_size(BLB_BUFFER(leafnode, 0));
  33. MSN msn = next_dummymsn();
  34. // apply an insert to the leaf node
  35. BRT_MSG_S cmd = { BRT_INSERT, msn, xids_get_root_xids(), .u.id = { &thekey, &theval } };
  36. brt_leaf_apply_cmd_once(leafnode, BLB(leafnode, 0), &cmd, idx, NULL, NULL, NULL, NULL);
  37. // Create bad tree (don't do following):
  38. // leafnode->max_msn_applied_to_node = msn;
  39. // dont forget to dirty the node
  40. leafnode->dirty = 1;
  41. }
  42. static void
  43. populate_leaf(BRTNODE leafnode, int seq, int n, int *minkey, int *maxkey) {
  44. for (int i = 0; i < n; i++) {
  45. int k = htonl(seq + i);
  46. int v = seq + i;
  47. append_leaf(leafnode, &k, sizeof k, &v, sizeof v);
  48. }
  49. *minkey = htonl(seq);
  50. *maxkey = htonl(seq + n - 1);
  51. }
  52. static void
  53. insert_into_child_buffer(BRT brt, BRTNODE node, int childnum, int minkey, int maxkey) {
  54. for (unsigned int val = htonl(minkey); val <= htonl(maxkey); val++) {
  55. MSN msn = next_dummymsn();
  56. unsigned int key = htonl(val);
  57. DBT thekey; toku_fill_dbt(&thekey, &key, sizeof key);
  58. DBT theval; toku_fill_dbt(&theval, &val, sizeof val);
  59. toku_brt_append_to_child_buffer(brt->compare_fun, NULL, node, childnum, BRT_INSERT, msn, xids_get_root_xids(), true, &thekey, &theval);
  60. // Create bad tree (don't do following):
  61. // node->max_msn_applied_to_node = msn;
  62. }
  63. }
  64. static BRTNODE
  65. make_tree(BRT brt, int height, int fanout, int nperleaf, int *seq, int *minkey, int *maxkey) {
  66. BRTNODE node;
  67. if (height == 0) {
  68. node = make_node(brt, 0);
  69. populate_leaf(node, *seq, nperleaf, minkey, maxkey);
  70. *seq += nperleaf;
  71. } else {
  72. node = make_node(brt, height);
  73. int minkeys[fanout], maxkeys[fanout];
  74. for (int childnum = 0; childnum < fanout; childnum++) {
  75. BRTNODE child = make_tree(brt, height-1, fanout, nperleaf, seq, &minkeys[childnum], &maxkeys[childnum]);
  76. if (childnum == 0)
  77. toku_brt_nonleaf_append_child(node, child, NULL, 0);
  78. else {
  79. int k = maxkeys[childnum-1]; // use the max of the left tree
  80. struct kv_pair *pivotkey = kv_pair_malloc(&k, sizeof k, NULL, 0);
  81. toku_brt_nonleaf_append_child(node, child, pivotkey, sizeof k);
  82. }
  83. toku_unpin_brtnode(brt, child);
  84. insert_into_child_buffer(brt, node, childnum, minkeys[childnum], maxkeys[childnum]);
  85. }
  86. *minkey = minkeys[0];
  87. *maxkey = maxkeys[0];
  88. for (int i = 1; i < fanout; i++) {
  89. if (memcmp(minkey, &minkeys[i], sizeof minkeys[i]) > 0)
  90. *minkey = minkeys[i];
  91. if (memcmp(maxkey, &maxkeys[i], sizeof maxkeys[i]) < 0)
  92. *maxkey = maxkeys[i];
  93. }
  94. }
  95. return node;
  96. }
  97. static UU() void
  98. deleted_row(UU() DB *db, UU() DBT *key, UU() DBT *val) {
  99. }
  100. static void
  101. test_make_tree(int height, int fanout, int nperleaf, int do_verify) {
  102. int r;
  103. // cleanup
  104. char fname[]= __FILE__ ".brt";
  105. r = unlink(fname);
  106. assert(r == 0 || (r == -1 && errno == ENOENT));
  107. // create a cachetable
  108. CACHETABLE ct = NULL;
  109. r = toku_brt_create_cachetable(&ct, 0, ZERO_LSN, NULL_LOGGER);
  110. assert(r == 0);
  111. // create the brt
  112. TOKUTXN null_txn = NULL;
  113. DB *null_db = NULL;
  114. BRT brt = NULL;
  115. r = toku_open_brt(fname, 1, &brt, 1024, 256, ct, null_txn, toku_builtin_compare_fun, null_db);
  116. assert(r == 0);
  117. // make a tree
  118. int seq = 0, minkey, maxkey;
  119. BRTNODE newroot = make_tree(brt, height, fanout, nperleaf, &seq, &minkey, &maxkey);
  120. // discard the old root block
  121. u_int32_t fullhash = 0;
  122. CACHEKEY *rootp;
  123. rootp = toku_calculate_root_offset_pointer(brt->h, &fullhash);
  124. // set the new root to point to the new tree
  125. *rootp = newroot->thisnodename;
  126. // Create bad tree (don't do following):
  127. // newroot->max_msn_applied_to_node = last_dummymsn(); // capture msn of last message injected into tree
  128. // unpin the new root
  129. toku_unpin_brtnode(brt, newroot);
  130. if (do_verify) {
  131. r = toku_verify_brt(brt);
  132. assert(r != 0);
  133. }
  134. // flush to the file system
  135. r = toku_close_brt_nolsn(brt, 0);
  136. assert(r == 0);
  137. // shutdown the cachetable
  138. r = toku_cachetable_close(&ct);
  139. assert(r == 0);
  140. }
  141. static int
  142. usage(void) {
  143. return 1;
  144. }
  145. int
  146. test_main (int argc , const char *argv[]) {
  147. int height = 1;
  148. int fanout = 2;
  149. int nperleaf = 8;
  150. int do_verify = 1;
  151. for (int i = 1; i < argc; i++) {
  152. const char *arg = argv[i];
  153. if (strcmp(arg, "-v") == 0) {
  154. verbose++;
  155. continue;
  156. }
  157. if (strcmp(arg, "-q") == 0) {
  158. verbose = 0;
  159. continue;
  160. }
  161. if (strcmp(arg, "--height") == 0 && i+1 < argc) {
  162. height = atoi(argv[++i]);
  163. continue;
  164. }
  165. if (strcmp(arg, "--fanout") == 0 && i+1 < argc) {
  166. fanout = atoi(argv[++i]);
  167. continue;
  168. }
  169. if (strcmp(arg, "--nperleaf") == 0 && i+1 < argc) {
  170. nperleaf = atoi(argv[++i]);
  171. continue;
  172. }
  173. if (strcmp(arg, "--verify") == 0 && i+1 < argc) {
  174. do_verify = atoi(argv[++i]);
  175. continue;
  176. }
  177. return usage();
  178. }
  179. test_make_tree(height, fanout, nperleaf, do_verify);
  180. return 0;
  181. }