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.

377 lines
15 KiB

  1. /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
  2. // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
  3. #ifndef LOG_INTERNAL_H
  4. #define LOG_INTERNAL_H
  5. #ident "$Id$"
  6. /*
  7. COPYING CONDITIONS NOTICE:
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of version 2 of the GNU General Public License as
  10. published by the Free Software Foundation, and provided that the
  11. following conditions are met:
  12. * Redistributions of source code must retain this COPYING
  13. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  14. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  15. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  16. GRANT (below).
  17. * Redistributions in binary form must reproduce this COPYING
  18. CONDITIONS NOTICE, the COPYRIGHT NOTICE (below), the
  19. DISCLAIMER (below), the UNIVERSITY PATENT NOTICE (below), the
  20. PATENT MARKING NOTICE (below), and the PATENT RIGHTS
  21. GRANT (below) in the documentation and/or other materials
  22. provided with the distribution.
  23. You should have received a copy of the GNU General Public License
  24. along with this program; if not, write to the Free Software
  25. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  26. 02110-1301, USA.
  27. COPYRIGHT NOTICE:
  28. TokuDB, Tokutek Fractal Tree Indexing Library.
  29. Copyright (C) 2007-2013 Tokutek, Inc.
  30. DISCLAIMER:
  31. This program is distributed in the hope that it will be useful, but
  32. WITHOUT ANY WARRANTY; without even the implied warranty of
  33. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  34. General Public License for more details.
  35. UNIVERSITY PATENT NOTICE:
  36. The technology is licensed by the Massachusetts Institute of
  37. Technology, Rutgers State University of New Jersey, and the Research
  38. Foundation of State University of New York at Stony Brook under
  39. United States of America Serial No. 11/760379 and to the patents
  40. and/or patent applications resulting from it.
  41. PATENT MARKING NOTICE:
  42. This software is covered by US Patent No. 8,185,551.
  43. PATENT RIGHTS GRANT:
  44. "THIS IMPLEMENTATION" means the copyrightable works distributed by
  45. Tokutek as part of the Fractal Tree project.
  46. "PATENT CLAIMS" means the claims of patents that are owned or
  47. licensable by Tokutek, both currently or in the future; and that in
  48. the absence of this license would be infringed by THIS
  49. IMPLEMENTATION or by using or running THIS IMPLEMENTATION.
  50. "PATENT CHALLENGE" shall mean a challenge to the validity,
  51. patentability, enforceability and/or non-infringement of any of the
  52. PATENT CLAIMS or otherwise opposing any of the PATENT CLAIMS.
  53. Tokutek hereby grants to you, for the term and geographical scope of
  54. the PATENT CLAIMS, a non-exclusive, no-charge, royalty-free,
  55. irrevocable (except as stated in this section) patent license to
  56. make, have made, use, offer to sell, sell, import, transfer, and
  57. otherwise run, modify, and propagate the contents of THIS
  58. IMPLEMENTATION, where such license applies only to the PATENT
  59. CLAIMS. This grant does not include claims that would be infringed
  60. only as a consequence of further modifications of THIS
  61. IMPLEMENTATION. If you or your agent or licensee institute or order
  62. or agree to the institution of patent litigation against any entity
  63. (including a cross-claim or counterclaim in a lawsuit) alleging that
  64. THIS IMPLEMENTATION constitutes direct or contributory patent
  65. infringement, or inducement of patent infringement, then any rights
  66. granted to you under this License shall terminate as of the date
  67. such litigation is filed. If you or your agent or exclusive
  68. licensee institute or order or agree to the institution of a PATENT
  69. CHALLENGE, then Tokutek may terminate any rights granted to you
  70. under this License.
  71. */
  72. #ident "Copyright (c) 2007-2013 Tokutek Inc. All rights reserved."
  73. #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."
  74. #include <stdio.h>
  75. #include <sys/types.h>
  76. #include <string.h>
  77. #include <dirent.h>
  78. #include "ft-internal.h"
  79. #include "log.h"
  80. #include "toku_list.h"
  81. #include "memarena.h"
  82. #include "logfilemgr.h"
  83. #include "txn.h"
  84. #include "txn_manager.h"
  85. #include <portability/toku_pthread.h>
  86. #include <util/omt.h>
  87. #include "rollback_log_node_cache.h"
  88. #include "txn_child_manager.h"
  89. using namespace toku;
  90. // Locking for the logger
  91. // For most purposes we use the big ydb lock.
  92. // To log: grab the buf lock
  93. // If the buf would overflow, then grab the file lock, swap file&buf, release buf lock, write the file, write the entry, release the file lock
  94. // else append to buf & release lock
  95. #define LOGGER_MIN_BUF_SIZE (1<<24)
  96. struct mylock {
  97. toku_mutex_t lock;
  98. };
  99. static inline void ml_init(struct mylock *l) {
  100. toku_mutex_init(&l->lock, 0);
  101. }
  102. static inline void ml_lock(struct mylock *l) {
  103. toku_mutex_lock(&l->lock);
  104. }
  105. static inline void ml_unlock(struct mylock *l) {
  106. toku_mutex_unlock(&l->lock);
  107. }
  108. static inline void ml_destroy(struct mylock *l) {
  109. toku_mutex_destroy(&l->lock);
  110. }
  111. struct logbuf {
  112. int n_in_buf;
  113. int buf_size;
  114. char *buf;
  115. LSN max_lsn_in_buf;
  116. };
  117. struct tokulogger {
  118. struct mylock input_lock;
  119. toku_mutex_t output_condition_lock; // if you need both this lock and input_lock, acquire the output_lock first, then input_lock. More typical is to get the output_is_available condition to be false, and then acquire the input_lock.
  120. toku_cond_t output_condition; //
  121. bool output_is_available; // this is part of the predicate for the output condition. It's true if no thread is modifying the output (either doing an fsync or otherwise fiddling with the output).
  122. bool is_open;
  123. bool write_log_files;
  124. bool trim_log_files; // for test purposes
  125. char *directory; // file system directory
  126. DIR *dir; // descriptor for directory
  127. int fd;
  128. CACHETABLE ct;
  129. int lg_max; // The size of the single file in the log. Default is 100MB in TokuDB
  130. // To access these, you must have the input lock
  131. LSN lsn; // the next available lsn
  132. struct logbuf inbuf; // data being accumulated for the write
  133. // To access these, you must have the output condition lock.
  134. LSN written_lsn; // the last lsn written
  135. LSN fsynced_lsn; // What is the LSN of the highest fsynced log entry (accessed only while holding the output lock, and updated only when the output lock and output permission are held)
  136. LSN last_completed_checkpoint_lsn; // What is the LSN of the most recent completed checkpoint.
  137. long long next_log_file_number;
  138. struct logbuf outbuf; // data being written to the file
  139. int n_in_file; // The amount of data in the current file
  140. // To access the logfilemgr you must have the output condition lock.
  141. TOKULOGFILEMGR logfilemgr;
  142. uint32_t write_block_size; // How big should the blocks be written to various logs?
  143. uint64_t input_lock_ctr; // how many times has input_lock been taken and released
  144. uint64_t output_condition_lock_ctr; // how many times has output_condition_lock been taken and released
  145. uint64_t swap_ctr; // how many times have input/output log buffers been swapped
  146. uint64_t num_writes_to_disk; // how many times did we write to disk?
  147. uint64_t bytes_written_to_disk; // how many bytes have been written to disk?
  148. tokutime_t time_spent_writing_to_disk; // how much tokutime did we spend writing to disk?
  149. void (*remove_finalize_callback) (DICTIONARY_ID, void*); // ydb-level callback to be called when a transaction that ...
  150. void * remove_finalize_callback_extra; // ... deletes a file is committed or when one that creates a file is aborted.
  151. CACHEFILE rollback_cachefile;
  152. rollback_log_node_cache rollback_cache;
  153. TXN_MANAGER txn_manager;
  154. };
  155. int toku_logger_find_next_unused_log_file(const char *directory, long long *result);
  156. int toku_logger_find_logfiles (const char *directory, char ***resultp, int *n_logfiles);
  157. struct txn_roll_info {
  158. // these are number of rollback nodes and rollback entries for this txn.
  159. //
  160. // the current rollback node below has sequence number num_rollback_nodes - 1
  161. // (because they are numbered 0...num-1). often, the current rollback is
  162. // already set to this block num, which means it exists and is available to
  163. // log some entries. if the current rollback is NONE and the number of
  164. // rollback nodes for this transaction is non-zero, then we will use
  165. // the number of rollback nodes to know which sequence number to assign
  166. // to a new one we create
  167. uint64_t num_rollback_nodes;
  168. uint64_t num_rollentries;
  169. uint64_t num_rollentries_processed;
  170. uint64_t rollentry_raw_count; // the total count of every byte in the transaction and all its children.
  171. // spilled rollback nodes are rollback nodes that were gorged by this
  172. // transaction, retired, and saved in a list.
  173. // the spilled rollback head is the block number of the first rollback node
  174. // that makes up the rollback log chain
  175. BLOCKNUM spilled_rollback_head;
  176. uint32_t spilled_rollback_head_hash;
  177. // the spilled rollback is the block number of the last rollback node that
  178. // makes up the rollback log chain.
  179. BLOCKNUM spilled_rollback_tail;
  180. uint32_t spilled_rollback_tail_hash;
  181. // the current rollback node block number we may use. if this is ROLLBACK_NONE,
  182. // then we need to create one and set it here before using it.
  183. BLOCKNUM current_rollback;
  184. uint32_t current_rollback_hash;
  185. };
  186. struct tokutxn {
  187. // These don't change after create:
  188. TXNID_PAIR txnid;
  189. uint64_t snapshot_txnid64; // this is the lsn of the snapshot
  190. const TXN_SNAPSHOT_TYPE snapshot_type;
  191. const bool for_recovery;
  192. const TOKULOGGER logger;
  193. const TOKUTXN parent;
  194. // The child txn is protected by the child_txn_manager lock
  195. // and by the user contract. The user contract states (and is
  196. // enforced at the ydb layer) that a child txn should not be created
  197. // while another child exists. The txn_child_manager will protect
  198. // other threads from trying to read this value while another
  199. // thread commits/aborts the child
  200. TOKUTXN child;
  201. // statically allocated child manager, if this
  202. // txn is a root txn, this manager will be used and set to
  203. // child_manager for this transaction and all of its children
  204. txn_child_manager child_manager_s;
  205. // child manager for this transaction, all of its children,
  206. // and all of its ancestors
  207. txn_child_manager* child_manager;
  208. // These don't change but they're created in a way that's hard to make
  209. // strictly const.
  210. DB_TXN *container_db_txn; // reference to DB_TXN that contains this tokutxn
  211. xid_omt_t *live_root_txn_list; // the root txns live when the root ancestor (self if a root) started.
  212. XIDS xids; // Represents the xid list
  213. TXNID oldest_referenced_xid;
  214. TOKUTXN snapshot_next;
  215. TOKUTXN snapshot_prev;
  216. bool begin_was_logged;
  217. bool declared_read_only; // true if the txn was declared read only when began
  218. // These are not read until a commit, prepare, or abort starts, and
  219. // they're "monotonic" (only go false->true) during operation:
  220. bool do_fsync;
  221. bool force_fsync_on_commit; //This transaction NEEDS an fsync once (if) it commits. (commit means root txn)
  222. // Not used until commit, prepare, or abort starts:
  223. LSN do_fsync_lsn;
  224. TOKU_XA_XID xa_xid; // for prepared transactions
  225. TXN_PROGRESS_POLL_FUNCTION progress_poll_fun;
  226. void *progress_poll_fun_extra;
  227. toku_mutex_t txn_lock;
  228. // Protected by the txn lock:
  229. omt<FT> open_fts; // a collection of the fts that we touched. Indexed by filenum.
  230. struct txn_roll_info roll_info; // Info used to manage rollback entries
  231. // mutex that protects the transition of the state variable
  232. // the rest of the variables are used by the txn code and
  233. // hot indexing to ensure that when hot indexing is processing a
  234. // leafentry, a TOKUTXN cannot dissappear or change state out from
  235. // underneath it
  236. toku_mutex_t state_lock;
  237. toku_cond_t state_cond;
  238. TOKUTXN_STATE state;
  239. uint32_t num_pin; // number of threads (all hot indexes) that want this
  240. // txn to not transition to commit or abort
  241. };
  242. static inline int
  243. txn_has_current_rollback_log(TOKUTXN txn) {
  244. return txn->roll_info.current_rollback.b != ROLLBACK_NONE.b;
  245. }
  246. static inline int
  247. txn_has_spilled_rollback_logs(TOKUTXN txn) {
  248. return txn->roll_info.spilled_rollback_tail.b != ROLLBACK_NONE.b;
  249. }
  250. struct txninfo {
  251. uint64_t rollentry_raw_count; // the total count of every byte in the transaction and all its children.
  252. uint32_t num_fts;
  253. FT *open_fts;
  254. bool force_fsync_on_commit; //This transaction NEEDS an fsync once (if) it commits. (commit means root txn)
  255. uint64_t num_rollback_nodes;
  256. uint64_t num_rollentries;
  257. BLOCKNUM spilled_rollback_head;
  258. BLOCKNUM spilled_rollback_tail;
  259. BLOCKNUM current_rollback;
  260. };
  261. static inline int toku_logsizeof_uint8_t (uint32_t v __attribute__((__unused__))) {
  262. return 1;
  263. }
  264. static inline int toku_logsizeof_uint32_t (uint32_t v __attribute__((__unused__))) {
  265. return 4;
  266. }
  267. static inline int toku_logsizeof_uint64_t (uint32_t v __attribute__((__unused__))) {
  268. return 8;
  269. }
  270. static inline int toku_logsizeof_bool (uint32_t v __attribute__((__unused__))) {
  271. return 1;
  272. }
  273. static inline int toku_logsizeof_FILENUM (FILENUM v __attribute__((__unused__))) {
  274. return 4;
  275. }
  276. static inline int toku_logsizeof_DISKOFF (DISKOFF v __attribute__((__unused__))) {
  277. return 8;
  278. }
  279. static inline int toku_logsizeof_BLOCKNUM (BLOCKNUM v __attribute__((__unused__))) {
  280. return 8;
  281. }
  282. static inline int toku_logsizeof_LSN (LSN lsn __attribute__((__unused__))) {
  283. return 8;
  284. }
  285. static inline int toku_logsizeof_TXNID (TXNID txnid __attribute__((__unused__))) {
  286. return 8;
  287. }
  288. static inline int toku_logsizeof_TXNID_PAIR (TXNID_PAIR txnid __attribute__((__unused__))) {
  289. return 16;
  290. }
  291. static inline int toku_logsizeof_XIDP (XIDP xid) {
  292. assert(0<=xid->gtrid_length && xid->gtrid_length<=64);
  293. assert(0<=xid->bqual_length && xid->bqual_length<=64);
  294. return xid->gtrid_length
  295. + xid->bqual_length
  296. + 4 // formatID
  297. + 1 // gtrid_length
  298. + 1; // bqual_length
  299. }
  300. static inline int toku_logsizeof_FILENUMS (FILENUMS fs) {
  301. static const FILENUM f = {0}; //fs could have .num==0 and then we cannot dereference
  302. return 4 + fs.num * toku_logsizeof_FILENUM(f);
  303. }
  304. static inline int toku_logsizeof_BYTESTRING (BYTESTRING bs) {
  305. return 4+bs.len;
  306. }
  307. static inline char *fixup_fname(BYTESTRING *f) {
  308. assert(f->len>0);
  309. char *fname = (char*)toku_xmalloc(f->len+1);
  310. memcpy(fname, f->data, f->len);
  311. fname[f->len]=0;
  312. return fname;
  313. }
  314. #endif