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.

234 lines
8.6 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. #ifndef TOKU_LOCK_REQUEST_H
  73. #define TOKU_LOCK_REQUEST_H
  74. #include <db.h>
  75. #include <toku_pthread.h>
  76. #include <ft/fttypes.h>
  77. #include <ft/comparator.h>
  78. #include "locktree.h"
  79. #include "txnid_set.h"
  80. #include "wfg.h"
  81. namespace toku {
  82. // A lock request contains the db, the key range, the lock type, and
  83. // the transaction id that describes a potential row range lock.
  84. //
  85. // the typical use case is:
  86. // - initialize a lock request
  87. // - start to try to acquire the lock
  88. // - do something else
  89. // - wait for the lock request to be resolved on a timed condition
  90. // - destroy the lock request
  91. // a lock request is resolved when its state is no longer pending, or
  92. // when it becomes granted, or timedout, or deadlocked. when resolved, the
  93. // state of the lock request is changed and any waiting threads are awakened.
  94. class lock_request {
  95. public:
  96. enum type {
  97. UNKNOWN,
  98. READ,
  99. WRITE
  100. };
  101. // effect: Initializes a lock request with a given wait time.
  102. void create(uint64_t wait_time);
  103. // effect: Destroys a lock request.
  104. void destroy(void);
  105. // effect: Resets the lock request parameters, allowing it to be reused.
  106. // requires: Lock request was already created at some point
  107. void set(locktree *lt, TXNID txnid,
  108. const DBT *left_key, const DBT *right_key, type lock_type);
  109. // effect: Tries to acquire a lock described by this lock request.
  110. // returns: The return code of locktree::acquire_[write,read]_lock()
  111. // or DB_LOCK_DEADLOCK if this request would end up deadlocked.
  112. int start(void);
  113. // effect: Sleeps until either the request is granted or the wait time expires.
  114. // returns: The return code of locktree::acquire_[write,read]_lock()
  115. // or simply DB_LOCK_NOTGRANTED if the wait time expired.
  116. int wait(void);
  117. const DBT *get_left_key(void) const;
  118. const DBT *get_right_key(void) const;
  119. // effect: Retries all of the lock requests for the given locktree.
  120. // Any lock requests successfully restarted is completed and woken up.
  121. // The rest remain pending.
  122. static void retry_all_lock_requests(locktree *lt);
  123. private:
  124. enum state {
  125. UNINITIALIZED,
  126. INITIALIZED,
  127. PENDING,
  128. COMPLETE,
  129. };
  130. // The keys for a lock request are stored "unowned" in m_left_key
  131. // and m_right_key. When the request is about to go to sleep, it
  132. // copies these keys and stores them in m_left_key_copy etc and
  133. // sets the temporary pointers to null.
  134. TXNID m_txnid;
  135. const DBT *m_left_key;
  136. const DBT *m_right_key;
  137. DBT m_left_key_copy;
  138. DBT m_right_key_copy;
  139. // The lock request type and associated locktree
  140. type m_type;
  141. locktree *m_lt;
  142. // If the lock request is in the completed state, then its
  143. // final return value is stored in m_complete_r
  144. int m_complete_r;
  145. state m_state;
  146. uint64_t m_wait_time;
  147. toku_cond_t m_wait_cond;
  148. // the lock request info state stored in the
  149. // locktree that this lock request is for.
  150. struct locktree::lt_lock_request_info *m_info;
  151. // effect: tries again to acquire the lock described by this lock request
  152. // returns: 0 if retrying the request succeeded and is now complete
  153. int retry(void);
  154. void complete(int complete_r);
  155. // effect: Finds another lock request by txnid.
  156. // requires: The lock request info mutex is held
  157. lock_request *find_lock_request(const TXNID &txnid);
  158. // effect: Insert this lock request into the locktree's set.
  159. // requires: the locktree's mutex is held
  160. void insert_into_lock_requests(void);
  161. // effect: Removes this lock request from the locktree's set.
  162. // requires: The lock request info mutex is held
  163. void remove_from_lock_requests(void);
  164. // effect: Asks this request's locktree which txnids are preventing
  165. // us from getting the lock described by this request.
  166. // returns: conflicts is populated with the txnid's that this request
  167. // is blocked on
  168. void get_conflicts(txnid_set *conflicts);
  169. // effect: Builds a wait-for-graph for this lock request and the given conflict set
  170. void build_wait_graph(wfg *wait_graph, const txnid_set &conflicts);
  171. // returns: True if this lock request is in deadlock with the given conflicts set
  172. bool deadlock_exists(const txnid_set &conflicts);
  173. void copy_keys(void);
  174. void calculate_cond_wakeup_time(struct timespec *ts);
  175. static int find_by_txnid(lock_request * const &request, const TXNID &txnid);
  176. friend class lock_request_unit_test;
  177. };
  178. ENSURE_POD(lock_request);
  179. } /* namespace toku */
  180. #endif /* TOKU_LOCK_REQUEST_H */