|
17 | 17 | #include "sql_class.h" |
18 | 18 | #include "backup_innodb.h" |
19 | 19 | #include "log0log.h" |
| 20 | +#include "srw_lock.h" |
| 21 | +#include "fil0fil.h" |
| 22 | +#include <vector> |
20 | 23 |
|
21 | 24 | namespace |
22 | 25 | { |
23 | | -struct InnoDB_backup |
| 26 | +class InnoDB_backup |
24 | 27 | { |
| 28 | + /** mutex protecting the queue */ |
| 29 | + srw_mutex_impl<false> mutex; |
| 30 | + |
25 | 31 | /** the original state of innodb_log_archive */ |
26 | 32 | bool was_archived; |
27 | 33 |
|
28 | | - void init(THD *thd) noexcept |
| 34 | + /** collection of files to be copied */ |
| 35 | + std::vector<uint32_t> queue; |
| 36 | + |
| 37 | + /** name of the target directory */ |
| 38 | + const LEX_CSTRING *target; |
| 39 | + |
| 40 | + /** the checkpoint from which the backup starts */ |
| 41 | + lsn_t checkpoint; |
| 42 | + /** end_lsn of the checkpoint at the backup start */ |
| 43 | + lsn_t checkpoint_end_lsn; |
| 44 | +public: |
| 45 | + /** |
| 46 | + Start of BACKUP SERVER: collect all files to be backed up |
| 47 | + @param thd current session |
| 48 | + @param target target directory |
| 49 | + @return error code |
| 50 | + @retval 0 on success |
| 51 | + */ |
| 52 | + int init(THD *thd, const LEX_CSTRING *target) noexcept |
29 | 53 | { |
30 | 54 | mysql_mutex_lock(&LOCK_global_system_variables); |
| 55 | + mutex.init(); |
| 56 | + mutex.wr_lock(); |
31 | 57 | was_archived= log_sys.archive; |
32 | | - log_sys.set_archive(true, thd); |
| 58 | + bool fail{log_sys.set_archive(true, thd)}; |
33 | 59 | mysql_mutex_unlock(&LOCK_global_system_variables); |
| 60 | + |
| 61 | + if (!fail) |
| 62 | + { |
| 63 | + log_sys.latch.wr_lock(); |
| 64 | + fail= !log_sys.archive; |
| 65 | + checkpoint= log_sys.archived_checkpoint; |
| 66 | + checkpoint_end_lsn= log_sys.archived_lsn; |
| 67 | + log_sys.latch.wr_unlock(); |
| 68 | + } |
| 69 | + |
| 70 | + if (!fail) |
| 71 | + { |
| 72 | + this->target= target; |
| 73 | + mysql_mutex_lock(&fil_system.mutex); |
| 74 | + for (fil_space_t &space : fil_system.space_list) |
| 75 | + queue.emplace_back(space.id); |
| 76 | + mysql_mutex_unlock(&fil_system.mutex); |
| 77 | + } |
| 78 | + mutex.wr_unlock(); |
| 79 | + return fail; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + Process a file that was collected at init(). |
| 84 | + This may be invoked from multiple concurrent threads. |
| 85 | + @param thd current session |
| 86 | + @return number of files remaining, or negative on error |
| 87 | + @retval 0 on completion |
| 88 | + */ |
| 89 | + int step(THD *thd) noexcept |
| 90 | + { |
| 91 | + uint32_t id= FIL_NULL; |
| 92 | + mutex.wr_lock(); |
| 93 | + size_t size{queue.size()}; |
| 94 | + if (size) |
| 95 | + { |
| 96 | + size--; |
| 97 | + id= queue.back(); |
| 98 | + queue.pop_back(); |
| 99 | + } |
| 100 | + mutex.wr_unlock(); |
| 101 | + |
| 102 | + if (fil_space_t *space= fil_space_t::get(id)) |
| 103 | + { |
| 104 | + /* TODO: copy the file to target safely, even when there may be |
| 105 | + concurrent writes to it */ |
| 106 | + space->release(); |
| 107 | + } |
| 108 | + |
| 109 | + size= std::min(size_t{std::numeric_limits<int>::max()}, size); |
| 110 | + |
| 111 | + return int(size); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + Finish copying and determine the logical time of the backup snapshot. |
| 116 | + @param thd current sesssion |
| 117 | + @param abort whether BACKUP SERVER was aborted |
| 118 | + @return error code |
| 119 | + @retval 0 on success |
| 120 | + */ |
| 121 | + int end(THD *thd, bool abort) noexcept |
| 122 | + { |
| 123 | + mutex.wr_lock(); |
| 124 | + if (abort) |
| 125 | + queue.clear(); |
| 126 | + ut_ad(queue.empty()); |
| 127 | + mutex.wr_unlock(); |
| 128 | + return 0; |
34 | 129 | } |
35 | 130 |
|
| 131 | + /** |
| 132 | + After a successful end(), finalize the backup. |
| 133 | + @param thd current sesssion |
| 134 | + */ |
36 | 135 | void fini(THD *thd) noexcept |
37 | 136 | { |
38 | 137 | mysql_mutex_lock(&LOCK_global_system_variables); |
| 138 | + ut_d(mutex.wr_lock()); |
| 139 | + ut_ad(queue.empty()); |
| 140 | + ut_d(mutex.wr_unlock()); |
| 141 | + mutex.destroy(); |
39 | 142 | if (!was_archived) |
40 | 143 | log_sys.set_archive(false, thd); |
41 | 144 | mysql_mutex_unlock(&LOCK_global_system_variables); |
42 | 145 | } |
43 | 146 | }; |
44 | 147 |
|
| 148 | +/** The backup context */ |
45 | 149 | static InnoDB_backup backup; |
46 | 150 | } |
47 | 151 |
|
48 | 152 | int innodb_backup_start(THD *thd, const LEX_CSTRING *target) noexcept |
49 | 153 | { |
50 | | - backup.init(thd); |
51 | | - /* create a work queue of files that must be copied */ |
52 | | - return 0; |
| 154 | + return backup.init(thd, target); |
53 | 155 | } |
54 | 156 |
|
55 | 157 | int innodb_backup_step(THD *thd) noexcept |
56 | 158 | { |
57 | | - return 0; |
| 159 | + return backup.step(thd); |
58 | 160 | } |
59 | 161 |
|
60 | 162 | int innodb_backup_end(THD *thd, bool abort) noexcept |
61 | 163 | { |
62 | | - return 0; |
| 164 | + return backup.end(thd, abort); |
63 | 165 | } |
64 | 166 |
|
65 | | -/** After a successful backup_end(), finalize the backup. */ |
66 | 167 | void innodb_backup_finalize(THD *thd) noexcept |
67 | 168 | { |
68 | 169 | backup.fini(thd); |
|
0 commit comments