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.

315 lines
13 KiB

  1. /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
  2. // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
  3. #ident "$Id$"
  4. /*
  5. COPYING CONDITIONS NOTICE:
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of version 2 of the GNU General Public License as
  8. published by the Free Software Foundation, and provided that the
  9. following conditions are met:
  10. * Redistributions of source code must retain this COPYING
  11. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  12. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  13. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  14. GRANT (below).
  15. * Redistributions in binary form must reproduce this COPYING
  16. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  17. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  18. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  19. GRANT (below) in the documentation and/or other materials
  20. provided with the distribution.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  24. 02110-1301, USA.
  25. COPYRIGHT NOTICE:
  26. TokuDB, Tokutek Fractal Tree Indexing Library.
  27. Copyright (C) 2007-2013 Tokutek, Inc.
  28. DISCLAIMER:
  29. This program is distributed in the hope that it will be useful, but
  30. WITHOUT ANY WARRANTY; without even the implied warranty of
  31. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  32. General Public License for more details.
  33. UNIVERSITY PATENT NOTICE:
  34. The technology is licensed by the Massachusetts Institute of
  35. Technology, Rutgers State University of New Jersey, and the Research
  36. Foundation of State University of New York at Stony Brook under
  37. United States of America Serial No. 11/760379 and to the patents
  38. and/or patent applications resulting from it.
  39. PATENT MARKING NOTICE:
  40. This software is covered by US Patent No. 8,185,551.
  41. PATENT RIGHTS GRANT:
  42. "THIS IMPLEMENTATION" means the copyrightable works distributed by
  43. Tokutek as part of the Fractal Tree project.
  44. "PATENT CLAIMS" means the claims of patents that are owned or
  45. licensable by Tokutek, both currently or in the future; and that in
  46. the absence of this license would be infringed by THIS
  47. IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
  48. "PATENT CHALLENGE" shall mean a challenge to the validity,
  49. patentability, enforceability and/or non-infringement of any of the
  50. PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
  51. Tokutek hereby grants to you, for the term and geographical scope of
  52. the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
  53. irrevocable (except as stated in this section) patent license to
  54. make, have made, use, offer to sell, sell, import, transfer, and
  55. otherwise run, modify, and propagate the contents of THIS
  56. IMPLEMENTATION, where such license applies only to the PATENT
  57. CLAIMS. This grant does not include claims that would be infringed
  58. only as a consequence of further modifications of THIS
  59. IMPLEMENTATION. If you or your agent or licensee institute or order
  60. or agree to the institution of patent litigation against any entity
  61. (including a cross-claim or counterclaim in a lawsuit) alleging that
  62. THIS IMPLEMENTATION constitutes direct or contributory patent
  63. infringement, or inducement of patent infringement, then any rights
  64. granted to you under this License shall terminate as of the date
  65. such litigation is filed. If you or your agent or exclusive
  66. licensee institute or order or agree to the institution of a PATENT
  67. CHALLENGE, then Tokutek may terminate any rights granted to you
  68. under this License.
  69. */
  70. #ident "Copyright (c) 2007-2013 Tokutek Inc. All rights reserved."
  71. #ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it."
  72. #include "fttypes.h"
  73. #include "log-internal.h"
  74. #include "rollback-apply.h"
  75. static void
  76. poll_txn_progress_function(TOKUTXN txn, uint8_t is_commit, uint8_t stall_for_checkpoint) {
  77. if (txn->progress_poll_fun) {
  78. TOKU_TXN_PROGRESS_S progress = {
  79. .entries_total = txn->roll_info.num_rollentries,
  80. .entries_processed = txn->roll_info.num_rollentries_processed,
  81. .is_commit = is_commit,
  82. .stalled_on_checkpoint = stall_for_checkpoint};
  83. txn->progress_poll_fun(&progress, txn->progress_poll_fun_extra);
  84. }
  85. }
  86. int toku_commit_rollback_item (TOKUTXN txn, struct roll_entry *item, LSN lsn) {
  87. int r=0;
  88. rolltype_dispatch_assign(item, toku_commit_, r, txn, lsn);
  89. txn->roll_info.num_rollentries_processed++;
  90. if (txn->roll_info.num_rollentries_processed % 1024 == 0) {
  91. poll_txn_progress_function(txn, true, false);
  92. }
  93. return r;
  94. }
  95. int toku_abort_rollback_item (TOKUTXN txn, struct roll_entry *item, LSN lsn) {
  96. int r=0;
  97. rolltype_dispatch_assign(item, toku_rollback_, r, txn, lsn);
  98. txn->roll_info.num_rollentries_processed++;
  99. if (txn->roll_info.num_rollentries_processed % 1024 == 0) {
  100. poll_txn_progress_function(txn, false, false);
  101. }
  102. return r;
  103. }
  104. int
  105. note_ft_used_in_txns_parent(const FT &ft, uint32_t UU(index), TOKUTXN const child);
  106. int
  107. note_ft_used_in_txns_parent(const FT &ft, uint32_t UU(index), TOKUTXN const child) {
  108. TOKUTXN parent = child->parent;
  109. toku_txn_maybe_note_ft(parent, ft);
  110. return 0;
  111. }
  112. static int
  113. apply_txn(TOKUTXN txn, LSN lsn, apply_rollback_item func) {
  114. int r = 0;
  115. // do the commit/abort calls and free everything
  116. // we do the commit/abort calls in reverse order too.
  117. struct roll_entry *item;
  118. //printf("%s:%d abort\n", __FILE__, __LINE__);
  119. BLOCKNUM next_log = ROLLBACK_NONE;
  120. uint32_t next_log_hash = 0;
  121. bool is_current = false;
  122. if (txn_has_current_rollback_log(txn)) {
  123. next_log = txn->roll_info.current_rollback;
  124. next_log_hash = txn->roll_info.current_rollback_hash;
  125. is_current = true;
  126. }
  127. else if (txn_has_spilled_rollback_logs(txn)) {
  128. next_log = txn->roll_info.spilled_rollback_tail;
  129. next_log_hash = txn->roll_info.spilled_rollback_tail_hash;
  130. }
  131. uint64_t last_sequence = txn->roll_info.num_rollback_nodes;
  132. bool found_head = false;
  133. while (next_log.b != ROLLBACK_NONE.b) {
  134. ROLLBACK_LOG_NODE log;
  135. //pin log
  136. toku_get_and_pin_rollback_log(txn, next_log, next_log_hash, &log);
  137. toku_rollback_verify_contents(log, txn->txnid, last_sequence - 1);
  138. toku_maybe_prefetch_previous_rollback_log(txn, log);
  139. last_sequence = log->sequence;
  140. if (func) {
  141. while ((item=log->newest_logentry)) {
  142. log->newest_logentry = item->prev;
  143. r = func(txn, item, lsn);
  144. if (r!=0) return r;
  145. }
  146. }
  147. if (next_log.b == txn->roll_info.spilled_rollback_head.b) {
  148. assert(!found_head);
  149. found_head = true;
  150. assert(log->sequence == 0);
  151. }
  152. next_log = log->previous;
  153. next_log_hash = log->previous_hash;
  154. {
  155. //Clean up transaction structure to prevent
  156. //toku_txn_close from double-freeing
  157. if (is_current) {
  158. txn->roll_info.current_rollback = ROLLBACK_NONE;
  159. txn->roll_info.current_rollback_hash = 0;
  160. is_current = false;
  161. }
  162. else {
  163. txn->roll_info.spilled_rollback_tail = next_log;
  164. txn->roll_info.spilled_rollback_tail_hash = next_log_hash;
  165. }
  166. if (found_head) {
  167. assert(next_log.b == ROLLBACK_NONE.b);
  168. txn->roll_info.spilled_rollback_head = next_log;
  169. txn->roll_info.spilled_rollback_head_hash = next_log_hash;
  170. }
  171. }
  172. bool give_back = false;
  173. // each txn tries to give back at most one rollback log node
  174. // to the cache.
  175. if (next_log.b == ROLLBACK_NONE.b) {
  176. give_back = txn->logger->rollback_cache.give_rollback_log_node(
  177. txn,
  178. log
  179. );
  180. }
  181. if (!give_back) {
  182. toku_rollback_log_unpin_and_remove(txn, log);
  183. }
  184. }
  185. return r;
  186. }
  187. //Commit each entry in the rollback log.
  188. //If the transaction has a parent, it just promotes its information to its parent.
  189. int toku_rollback_commit(TOKUTXN txn, LSN lsn) {
  190. int r=0;
  191. if (txn->parent!=0) {
  192. // First we must put a rollinclude entry into the parent if we spilled
  193. if (txn_has_spilled_rollback_logs(txn)) {
  194. uint64_t num_nodes = txn->roll_info.num_rollback_nodes;
  195. if (txn_has_current_rollback_log(txn)) {
  196. num_nodes--; //Don't count the in-progress rollback log.
  197. }
  198. toku_logger_save_rollback_rollinclude(txn->parent, txn->txnid, num_nodes,
  199. txn->roll_info.spilled_rollback_head, txn->roll_info.spilled_rollback_head_hash,
  200. txn->roll_info.spilled_rollback_tail, txn->roll_info.spilled_rollback_tail_hash);
  201. //Remove ownership from child.
  202. txn->roll_info.spilled_rollback_head = ROLLBACK_NONE;
  203. txn->roll_info.spilled_rollback_head_hash = 0;
  204. txn->roll_info.spilled_rollback_tail = ROLLBACK_NONE;
  205. txn->roll_info.spilled_rollback_tail_hash = 0;
  206. }
  207. // if we're commiting a child rollback, put its entries into the parent
  208. // by pinning both child and parent and then linking the child log entry
  209. // list to the end of the parent log entry list.
  210. if (txn_has_current_rollback_log(txn)) {
  211. //Pin parent log
  212. toku_txn_lock(txn->parent);
  213. ROLLBACK_LOG_NODE parent_log;
  214. toku_get_and_pin_rollback_log_for_new_entry(txn->parent, &parent_log);
  215. //Pin child log
  216. ROLLBACK_LOG_NODE child_log;
  217. toku_get_and_pin_rollback_log(txn, txn->roll_info.current_rollback,
  218. txn->roll_info.current_rollback_hash, &child_log);
  219. toku_rollback_verify_contents(child_log, txn->txnid, txn->roll_info.num_rollback_nodes - 1);
  220. // Append the list to the front of the parent.
  221. if (child_log->oldest_logentry) {
  222. // There are some entries, so link them in.
  223. child_log->oldest_logentry->prev = parent_log->newest_logentry;
  224. if (!parent_log->oldest_logentry) {
  225. parent_log->oldest_logentry = child_log->oldest_logentry;
  226. }
  227. parent_log->newest_logentry = child_log->newest_logentry;
  228. parent_log->rollentry_resident_bytecount += child_log->rollentry_resident_bytecount;
  229. txn->parent->roll_info.rollentry_raw_count += txn->roll_info.rollentry_raw_count;
  230. child_log->rollentry_resident_bytecount = 0;
  231. }
  232. if (parent_log->oldest_logentry==NULL) {
  233. parent_log->oldest_logentry = child_log->oldest_logentry;
  234. }
  235. child_log->newest_logentry = child_log->oldest_logentry = 0;
  236. // Put all the memarena data into the parent.
  237. if (memarena_total_size_in_use(child_log->rollentry_arena) > 0) {
  238. // If there are no bytes to move, then just leave things alone, and let the memory be reclaimed on txn is closed.
  239. memarena_move_buffers(parent_log->rollentry_arena, child_log->rollentry_arena);
  240. }
  241. // each txn tries to give back at most one rollback log node
  242. // to the cache. All other rollback log nodes for this child
  243. // transaction are included in the parent's rollback log,
  244. // so this is the only node we can give back to the cache
  245. bool give_back = txn->logger->rollback_cache.give_rollback_log_node(
  246. txn,
  247. child_log
  248. );
  249. if (!give_back) {
  250. toku_rollback_log_unpin_and_remove(txn, child_log);
  251. }
  252. txn->roll_info.current_rollback = ROLLBACK_NONE;
  253. txn->roll_info.current_rollback_hash = 0;
  254. toku_maybe_spill_rollbacks(txn->parent, parent_log);
  255. toku_rollback_log_unpin(txn->parent, parent_log);
  256. assert(r == 0);
  257. toku_txn_unlock(txn->parent);
  258. }
  259. // Note the open brts, the omts must be merged
  260. r = txn->open_fts.iterate<struct tokutxn, note_ft_used_in_txns_parent>(txn);
  261. assert(r==0);
  262. //If this transaction needs an fsync (if it commits)
  263. //save that in the parent. Since the commit really happens in the root txn.
  264. txn->parent->force_fsync_on_commit |= txn->force_fsync_on_commit;
  265. txn->parent->roll_info.num_rollentries += txn->roll_info.num_rollentries;
  266. } else {
  267. r = apply_txn(txn, lsn, toku_commit_rollback_item);
  268. assert(r==0);
  269. }
  270. return r;
  271. }
  272. int toku_rollback_abort(TOKUTXN txn, LSN lsn) {
  273. int r;
  274. r = apply_txn(txn, lsn, toku_abort_rollback_item);
  275. assert(r==0);
  276. return r;
  277. }