Zero  0.1.0
chkpt.h
Go to the documentation of this file.
1 /*
2  * (c) Copyright 2011-2014, Hewlett-Packard Development Company, LP
3  */
4 
5 /* -*- mode:C++; c-basic-offset:4 -*-
6  Shore-MT -- Multi-threaded port of the SHORE storage manager
7 
8  Copyright (c) 2007-2009
9  Data Intensive Applications and Systems Labaratory (DIAS)
10  Ecole Polytechnique Federale de Lausanne
11 
12  All Rights Reserved.
13 
14  Permission to use, copy, modify and distribute this software and
15  its documentation is hereby granted, provided that both the
16  copyright notice and this permission notice appear in all copies of
17  the software, derivative works or modified versions, and any
18  portions thereof, and that both notices appear in supporting
19  documentation.
20 
21  This code is distributed in the hope that it will be useful, but
22  WITHOUT ANY WARRANTY; without even the implied warranty of
23  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS
24  DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
25  RESULTING FROM THE USE OF THIS SOFTWARE.
26 */
27 
28 /*<std-header orig-src='shore' incl-file-exclusion='CHKPT_H'>
29 
30  $Id: chkpt.h,v 1.23 2010/06/08 22:28:55 nhall Exp $
31 
32 SHORE -- Scalable Heterogeneous Object REpository
33 
34 Copyright (c) 1994-99 Computer Sciences Department, University of
35  Wisconsin -- Madison
36 All Rights Reserved.
37 
38 Permission to use, copy, modify and distribute this software and its
39 documentation is hereby granted, provided that both the copyright
40 notice and this permission notice appear in all copies of the
41 software, derivative works or modified versions, and any portions
42 thereof, and that both notices appear in supporting documentation.
43 
44 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY
45 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS
46 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND
47 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 
49 This software was developed with support by the Advanced Research
50 Project Agency, ARPA order number 018 (formerly 8230), monitored by
51 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518.
52 Further funding for this work was provided by DARPA through
53 Rome Research Laboratory Contract No. F30602-97-2-0247.
54 
55 */
56 
57 #ifndef __CHKPT_H
58 #define __CHKPT_H
59 
60 #include "w_defines.h"
61 
62 #include "sm_options.h"
63 #include "sm_base.h"
64 #include "w_heap.h"
65 #include "logarchiver.h"
66 #include "w_okvl.h"
67 #include "xct.h"
68 
69 #include <vector>
70 #include <list>
71 #include <unordered_map>
72 #include <algorithm>
73 #include <limits>
74 #include <fstream>
75 
77  lsn_t rec_lsn; // initial dirty lsn
78  lsn_t page_lsn; // last write lsn
79  lsn_t clean_lsn; // last time page was cleaned
80 
82  rec_lsn(lsn_t::max),
83  page_lsn(lsn_t::null),
84  clean_lsn(lsn_t::null) {}
85 
86  bool is_dirty() const {
87  return page_lsn >= clean_lsn;
88  }
89 
90  void mark_dirty(lsn_t page, lsn_t rec) {
91  // w_assert1(!rec.is_null());
92  if (page > page_lsn) {
93  page_lsn = page;
94  }
95  if (rec >= clean_lsn && rec < rec_lsn) {
96  rec_lsn = rec;
97  }
98  }
99 
100  void mark_clean(lsn_t clean) {
101  if (clean > clean_lsn) {
102  clean_lsn = clean;
103  }
104  }
105 };
106 
107 struct lock_info_t {
109  uint32_t lock_hash;
110 
111 // lock_info_t(okvl_mode mode, uint32_t hash) :
112 // lock_mode(mode), lock_hash(hash)
113 // {}
114 };
115 
117  smlevel_0::xct_state_t state;
118 
119  lsn_t last_lsn; // most recent log record
120  lsn_t first_lsn; // first lsn of the txn
121  vector<lock_info_t> locks;
122 
124  state(xct_t::xct_active),
125  last_lsn(lsn_t::null),
126  first_lsn(lsn_t::max) {}
127 
128  bool is_active() const {
129  return state != xct_t::xct_ended;
130  }
131 
132  void add_lock(okvl_mode mode, uint32_t hash) {
133  if (is_active()) {
134  locks.push_back({mode, hash});
135  }
136  }
137 
138  void mark_ended() {
139  state = xct_t::xct_ended;
140  }
141 
142  void update_lsns(lsn_t first, lsn_t last) {
143  if (last > last_lsn) {
144  last_lsn = last;
145  }
146  if (first < first_lsn && !first.is_null()) {
147  first_lsn = first;
148  }
149  }
150 };
151 
152 typedef unordered_map<PageID, buf_tab_entry_t> buf_tab_t;
153 
154 typedef unordered_map<tid_t, xct_tab_entry_t> xct_tab_t;
155 
156 class chkpt_t {
157  friend class chkpt_m;
158 
159 private:
161 
163 
164  /*
165  * CS: looking back at my design for checkpoints and log analysis, it would
166  * be better to store information such as ongoing restore processes and
167  * backups in normal B-trees (i.e., a kind of system catalog). That would
168  * simplify things tremendously, since log analysis and chekpoints would
169  * be simpler and that kind of system information would be maintained and
170  * recovered using the same basic infrastructure of "user" data (i.e.,
171  * dirty pages and active transactions only)
172  */
173 
175 
176 public: // required for restart for now
178 
180 
181  string bkp_path;
182 
184 
185  std::vector<uint32_t> restore_tab;
186 
188 
190 
191 public:
192  void init();
193 
194  void scan_log(lsn_t scan_start = lsn_t::null, lsn_t archived_lsn = lsn_t::null);
195 
196  void mark_page_dirty(PageID pid, lsn_t page_lsn, lsn_t rec_lsn);
197 
198  void mark_page_clean(PageID pid, lsn_t lsn);
199 
200  xct_tab_entry_t& mark_xct_active(tid_t tid, lsn_t first, lsn_t last);
201 
202  void add_backup(const char* path, lsn_t backupLSN);
203 
204  void analyze_logrec(logrec_t&, xct_tab_entry_t* xct,
205  lsn_t& scan_stop, lsn_t archived_lsn);
206 
207  lsn_t get_min_rec_lsn() const;
208 
209  lsn_t get_min_xct_lsn() const;
210 
212  return last_scan_start;
213  }
214 
216  last_scan_start = l;
217  }
218 
220  return highest_tid;
221  }
222 
223  void set_highest_tid(tid_t tid) {
224  highest_tid = tid;
225  }
226 
227  void dump(ostream& out);
228 
229  void serialize_binary(ofstream& ofs);
230 
231  void deserialize_binary(ifstream& ofs, lsn_t archived_lsn = lsn_t::null);
232 
233  // Used by nodb mode
234  void set_redo_low_water_mark(lsn_t lsn);
235 
236 private:
237  void cleanup();
238 
239  void acquire_lock(xct_tab_entry_t& xct, logrec_t& r);
240 };
241 
242 class chkpt_thread_t;
243 
244 /*********************************************************************
245  *
246  * class chkpt_m
247  *
248  * Checkpoint Manager. User calls spawn_chkpt_thread() to fork
249  * a background thread to take checkpoint every now and then.
250  * User calls take() to take a checkpoint immediately.
251  *
252  * User calls wakeup_and_take() to wake up the checkpoint
253  * thread to checkpoint soon.
254  *
255  *********************************************************************/
256 class chkpt_m : public worker_thread_t {
257 public:
259  chkpt_m(const sm_options&, chkpt_t* chkpt_info = nullptr);
260 
261  virtual ~chkpt_m();
262 
263 public:
264  virtual void do_work();
265 
266  void take(chkpt_t* chkpt = nullptr);
267 
269  return _min_rec_lsn;
270  }
271 
273  return _min_xct_lsn;
274  }
275 
276  /*
277  * min_active_lsn is the LSN up to which log records can be thrown away,
278  * because they will never be needed for undoing an active transaction or
279  * redoing a dirty page. If both min_rec_lsn and min_xct_lsn are null, it
280  * indicates that the system is "clean", i.e., no active transactions or
281  * dirty pages. For this case, we return the LSN of the last checkpoint.
282  */
284  lsn_t min = _last_end_lsn;
285  if (!_min_rec_lsn.is_null() && _min_rec_lsn < min) {
286  min = _min_rec_lsn;
287  }
288  if (!_min_xct_lsn.is_null() && _min_xct_lsn < min) {
289  min = _min_xct_lsn;
290  }
291  return min;
292  }
293 
294 private:
296 
298 
299  std::mutex chkpt_mutex;
300 
301  void _acquire_lock(logrec_t& r, chkpt_t& new_chkpt);
302 
303  // Values cached from the last checkpoint
305 
307 
309 
311 
313 
315 
317 
318  std::ofstream _propstats_ofs;
319 };
320 
321 /*<std-footer incl-file-exclusion='CHKPT_H'> -- do not edit anything below this line -- */
322 
323 #endif // __CHKPT_H /*</std-footer>*/
bool ignore_restore
Definition: chkpt.h:174
bool _print_propstats
Definition: chkpt.h:314
long _chkpt_count
Definition: chkpt.h:295
void update_lsns(lsn_t first, lsn_t last)
Definition: chkpt.h:142
lsn_t get_last_scan_start() const
Definition: chkpt.h:211
Definition: worker_thread.h:12
buf_tab_entry_t()
Definition: chkpt.h:81
lsn_t clean_lsn
Definition: chkpt.h:79
bool ongoing_restore
Definition: chkpt.h:187
string bkp_path
Definition: chkpt.h:181
bool is_active() const
Definition: chkpt.h:128
Definition: chkpt.h:76
A transaction. Internal to the storage manager.This class may be used in a limited way for the handli...
Definition: xct.h:185
uint32_t lock_hash
Definition: chkpt.h:109
lsn_t last_lsn
Definition: chkpt.h:119
Definition: chkpt.h:156
Start-up parameters for the storage engine. See OPTIONS.
Definition: sm_options.h:24
chkpt_t curr_chkpt
Definition: chkpt.h:297
std::vector< uint32_t > restore_tab
Definition: chkpt.h:185
static const lsn_t null
Definition: lsn.h:371
std::mutex chkpt_mutex
Definition: chkpt.h:299
lsn_t bkp_lsn
Definition: chkpt.h:183
bool _use_log_archive
Definition: chkpt.h:310
xct_tab_entry_t()
Definition: chkpt.h:123
bool is_dirty() const
Definition: chkpt.h:86
Represents a transactional log record.
Definition: logrec.h:143
lsn_t _min_rec_lsn
Definition: chkpt.h:304
buf_tab_t buf_tab
Definition: chkpt.h:177
uint32_t PageID
Definition: basics.h:45
Definition: chkpt.h:256
void add_lock(okvl_mode mode, uint32_t hash)
Definition: chkpt.h:132
Log Sequence Number. See Log Sequence Numbers (LSN).
Definition: lsn.h:243
const T max(const T x, const T y)
Definition: w_minmax.h:45
bool _log_based
Definition: chkpt.h:312
void mark_ended()
Definition: chkpt.h:138
lsn_t page_lsn
Definition: chkpt.h:78
lsn_t last_scan_start
Definition: chkpt.h:162
lsn_t _min_xct_lsn
Definition: chkpt.h:306
lsn_t get_min_rec_lsn()
Definition: chkpt.h:268
lsn_t rec_lsn
Definition: chkpt.h:77
Definition: chkpt.h:107
Represents a lock mode of one key entry in the OKVL lock manager.
Definition: w_okvl.h:95
size_t _dirty_page_count
Definition: chkpt.h:316
xct_t * xct()
Definition: smthread.h:575
void mark_dirty(lsn_t page, lsn_t rec)
Definition: chkpt.h:90
lsn_t _last_end_lsn
Definition: chkpt.h:308
okvl_mode lock_mode
Definition: chkpt.h:108
lsn_t get_min_active_lsn()
Definition: chkpt.h:283
lsn_t get_min_xct_lsn()
Definition: chkpt.h:272
uint64_t tid_t
Definition: tid_t.h:59
PageID restore_page_cnt
Definition: chkpt.h:189
std::ofstream _propstats_ofs
Definition: chkpt.h:318
bool is_null() const
Definition: lsn.h:357
const T min(const T x, const T y)
Definition: w_minmax.h:52
unordered_map< tid_t, xct_tab_entry_t > xct_tab_t
Definition: chkpt.h:154
tid_t get_highest_tid()
Definition: chkpt.h:219
Definition: chkpt.h:116
tid_t highest_tid
Definition: chkpt.h:160
lsn_t first_lsn
Definition: chkpt.h:120
vector< lock_info_t > locks
Definition: chkpt.h:121
void set_highest_tid(tid_t tid)
Definition: chkpt.h:223
xct_tab_t xct_tab
Definition: chkpt.h:179
smlevel_0::xct_state_t state
Definition: chkpt.h:117
void mark_clean(lsn_t clean)
Definition: chkpt.h:100
void set_last_scan_start(lsn_t l)
Definition: chkpt.h:215
unordered_map< PageID, buf_tab_entry_t > buf_tab_t
Definition: chkpt.h:152