Zero  0.1.0
sm.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='SM_H'>
29 
30  $Id: sm.h,v 1.322 2010/10/27 17:04:23 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 __SM_H
58 #define __SM_H
59 
60 #include "w_defines.h"
61 
62 /* -- do not edit anything above this line -- </std-header>*/
63 
64 /*
65  * Stuff needed by value-added servers. NOT meant to be included by
66  * internal SM .c files, except to the extent that they need these
67  * definitions used in the API.
68  */
69 
70 #ifndef __SM_BASE_H
71 
72 #include "sm_base.h"
73 
74 #endif // __SM_BASE_H
75 
76 #include "smstats.h" // declares sm_stats_t and sm_config_info_t
77 #include "lsn.h"
78 #include <string>
79 #include "sm_options.h"
80 
81 #include <boost/filesystem.hpp>
82 
83 /* DOXYGEN Documentation : */
84 
332 /********************************************************************/
333 
334 class fixable_page_h;
335 class btree_page_h;
336 class xct_t;
337 class vec_t;
338 class log_m;
339 class lock_m;
340 class btree_m;
341 class pool_m;
342 class dir_m;
343 class chkpt_m;
344 class lid_m;
345 class sm_stats_cache_t;
346 class prologue_rc_t;
347 class w_keystr_t;
349 class lil_global_table;
350 struct okvl_mode;
351 class key_ranges_map;
352 
370 class sm_save_point_t : public lsn_t {
371 public:
373 
374  friend ostream& operator<<(ostream& o, const sm_save_point_t& p) {
375  return o << p._tid << ':' << (const lsn_t&)p;
376  }
377 
378  friend istream& operator>>(istream& i, sm_save_point_t& p) {
379  char ch;
380  return i >> p._tid >> ch >> (lsn_t&)p;
381  }
382 
383  tid_t tid() const {
384  return _tid;
385  }
386 
387 private:
388  friend class ss_m;
389 
391 };
392 
393 class sm_store_info_t;
394 class log_entry;
395 class coordinator;
396 class tape_t;
397 
405 class ss_m : public smlevel_top {
406  friend class prologue_rc_t;
407  friend class log_entry;
408  friend class coordinator;
409  friend class tape_t;
410 
411 public:
412 
414 
415  typedef smlevel_0::xct_state_t xct_state_t;
416 
417  typedef sm_store_property_t store_property_t;
418 
419 #ifdef COMMENT
420  //
421  // Below is most of the interface for the SHORE Storage Manager.
422  // The rest is located in smthread.h
423  //
424 
425  //
426  // TEMPORARY FILES/INDEXES
427  //
428  // When a file or index is created there is a tmp_flag parameter
429  // that when true indicates that the file is temporary.
430  // Operations on a temporary file are not logged and the
431  // file will be gone the next time the volume is mounted.
432  //
433  // TODO: IMPLEMENTATION NOTE on Temporary Files/Indexes:
434  // Temp files cannot be trusted after transaction abort.
435  // They should be marked for removal.
436  //
437  // CODE STRUCTURE:
438  // Almost all ss_m functions begin by creating a prologue object
439  // whose constructor and descructor check for many common errors.
440  // In addition most ss_m::OP() functions now call an ss_m::_OP()
441  // function to do the real work. The ss_m::OP functions should
442  // not be called by other ss_m functions, instead the corresponding
443  // ss_m::_OP function should be used.
444  //
445 
446 #endif /* COMMENT */
447 
448 public:
493  ss_m(const sm_options& options);
494 
502  ~ss_m();
503 
504  // Manually start the store if the store is not running already.
505  // Only one instance of store can be running at any time.
506  // The function starts the store using the input parameters from sm constructor,
507  // no change allowed after the ss_m constructor
508  // Internal settings will be reset, i.e. shutting_down, shutdown_clean flags
509  // Return: true if store started successfully
510  // false if store was running already
511  bool startup();
512 
513  // Manually stop a running store. If the store was not running, no-op.
514  // Only one instance of store can be running at any time.
515  // The shutdown process obeys the internal settings,
516  // i.e. shutting_down, shutdown_clean flags
517  // Return: if store shutdown successfully or was not running
518  bool shutdown();
519 
539  static void set_shutdown_flag(bool clean);
540 
549  static void set_shutdown_filthy(bool filthy);
550 
556  static rc_t log_file_was_archived(const char* logfile);
557 
558  rc_t _truncate_log();
559 
560 private:
561 // void _construct_once(LOG_WARN_CALLBACK_FUNC x=nullptr,
562 // LOG_ARCHIVED_CALLBACK_FUNC y=nullptr);
563  void _construct_once();
564 
565  void _destruct_once();
566 
567  void _do_restart();
568 
569  void _finish_recovery();
570 
571  // Used for cosntructing xct object depending on chosen implementation
572  static xct_t* _new_xct(
573  sm_stats_t* stats,
574  int timeout,
575  bool sys_xct,
576  bool single_log_sys_xct = false);
577 
578 public:
603  static rc_t begin_xct(
605 
628  static rc_t begin_xct(
629  sm_stats_t* stats, // allocated by caller
631 
646  static rc_t begin_xct(
647  tid_t& tid,
649 
662  static rc_t begin_sys_xct(
663  bool single_log_sys_xct = false,
664  sm_stats_t* stats = nullptr,
666 
680  static rc_t commit_xct(
681  bool lazy = false,
682  lsn_t* plastlsn = nullptr);
683 
698  static rc_t commit_xct(
699  sm_stats_t*& stats,
700  bool lazy = false,
701  lsn_t* plastlsn = nullptr);
702 
709  static rc_t commit_sys_xct();
710 
726  static rc_t chain_xct(
727  sm_stats_t*& stats, /* in w/new, out w/old */
728  bool lazy = false);
729 
744  static rc_t chain_xct(bool lazy = false);
745 
785  static rc_t commit_xct_group(
786  xct_t* list[],
787  int listlen);
788 
796  static rc_t abort_xct(sm_stats_t*& stats);
797 
804  static rc_t abort_xct();
805 
816  static rc_t save_work(sm_save_point_t& sp);
817 
830  static rc_t rollback_work(const sm_save_point_t& sp);
831 
839  static uint32_t num_active_xcts();
840 
846  static void attach_xct(xct_t* x) {
847  smthread_t::attach_xct(x);
848  }
849 
857  static void detach_xct() {
858  xct_t* x = smthread_t::xct();
859  if (x) {
861  }
862  }
863 
871  static xct_t* tid_to_xct(const tid_t& tid);
872 
879  static tid_t xct_to_tid(const xct_t* x);
880 
887  static rc_t dump_xcts(ostream& o);
888 
897  static xct_state_t state_xct(const xct_t* x);
898 
907  static off_t xct_log_space_needed();
908 
921  static rc_t xct_reserve_log_space(off_t amt);
922 
936  static rc_t checkpoint();
937 
942  static rc_t force_volume();
943 
947  static rc_t dump_buffers(ostream& o);
948 
949  static rc_t dump_locks(ostream& o);
950 
951  static rc_t dump_locks(); // defaults to std::cout
952 
961  static rc_t gather_xct_stats(
962  sm_stats_t& stats,
963  bool reset = false);
964 
970  static rc_t gather_stats(
971  sm_stats_t& stats
972  );
973 
979  static rc_t config_info(sm_config_info_t& info);
980 
989  static rc_t set_disk_delay(u_int milli_sec);
990 
995  static rc_t sync_log(bool block = true);
996 
1001  static rc_t flush_until(lsn_t& anlsn, bool block = true);
1002 
1007  static rc_t get_curr_lsn(lsn_t& anlsn);
1008 
1013  static rc_t get_durable_lsn(lsn_t& anlsn);
1014 
1026  static void dump_page_lsn_chain(std::ostream& o, const PageID& pid,
1027  const lsn_t& max_lsn);
1028 
1038  static void dump_page_lsn_chain(std::ostream& o, const PageID& pid);
1039 
1048  static void dump_page_lsn_chain(std::ostream& o);
1049 
1056  static rc_t verify_volume(
1057  int hash_bits, verify_volume_result& result);
1058 
1059 
1060 
1093  //
1094  // Functions for B+tree Indexes
1095  //
1118  static rc_t create_index(
1119  StoreID& stid
1120  );
1121 
1127  static rc_t destroy_index(const StoreID& iid);
1128 
1130  static rc_t print_index(StoreID stid);
1142  static rc_t touch_index(StoreID stid, uint64_t& page_count);
1143 
1157  static rc_t create_assoc(
1158  StoreID stid,
1159  const w_keystr_t& key,
1160  const vec_t& el
1161  );
1162 
1163  static rc_t create_assoc(
1164  StoreID stid,
1165  const vec_t& key,
1166  const vec_t& el
1167  );
1168 
1176  static rc_t update_assoc(
1177  StoreID stid,
1178  const w_keystr_t& key,
1179  const vec_t& el
1180  );
1181 
1189  static rc_t put_assoc(
1190  StoreID stid,
1191  const w_keystr_t& key,
1192  const vec_t& el
1193  );
1194 
1204  static rc_t overwrite_assoc(
1205  StoreID stid,
1206  const w_keystr_t& key,
1207  const char* el, smsize_t offset, smsize_t elen);
1208 
1214  static rc_t destroy_assoc(
1215  StoreID stid,
1216  const w_keystr_t& key
1217  );
1218 
1234  static rc_t find_assoc(
1235  StoreID stid,
1236  const w_keystr_t& key,
1237  void* el,
1238  smsize_t& elen,
1239  bool& found
1240  );
1241 
1247  static rc_t defrag_index_page(btree_page_h& page);
1248 
1264  static rc_t verify_index(StoreID stid, int hash_bits, bool& consistent);
1265 
1272  static rc_t open_store(StoreID stid, PageID& root_pid,
1273  bool for_update = false);
1274 
1276  static rc_t open_store_nolock(StoreID stid, PageID& root_pid);
1277 
1278  /*****************************************************************
1279  * Locking related functions
1280  *
1281  * NOTE: there are standard conversions from PageID, and
1282  * StoreID to lockid_t, so wherever a lockid_t parameter is
1283  * specified a PageID or StoreID can be used.
1284  *
1285  *****************************************************************/
1286 
1288  static lil_global_table* get_lil_global_table();
1289 
1300  static rc_t lock(
1301  const lockid_t& n,
1302  const okvl_mode& m,
1303  bool check_only = false,
1304  int timeout = timeout_t::WAIT_SPECIFIED_BY_XCT
1305  );
1306 
1307  static rc_t activate_archiver();
1308 
1311 
1312  static const sm_options& get_options() {
1313  return _options;
1314  }
1315 
1316 private:
1317 
1318  static int _instance_cnt;
1319 
1320  void _set_option_logsize();
1321 
1322  static rc_t _set_store_property(
1323  StoreID stid,
1324  store_property_t property);
1325 
1326  static rc_t _get_store_property(
1327  StoreID stid,
1328  store_property_t& property);
1329 
1330  static rc_t _begin_xct(
1331  sm_stats_t* stats, // allocated by caller
1332  tid_t& tid,
1333  int timeout,
1334  bool sys_xct = false,
1335  bool single_log_sys_xct = false);
1336 
1337  static rc_t _commit_xct(
1338  sm_stats_t*& stats,
1339  bool lazy,
1340  lsn_t* plastlsn);
1341 
1342  static rc_t _commit_xct_group(
1343  xct_t* list[],
1344  int listlen);
1345 
1346  static rc_t _chain_xct(
1347  sm_stats_t*& stats,
1348  bool lazy);
1349 
1350  static rc_t _abort_xct(
1351  sm_stats_t*& stats);
1352 
1353  static rc_t _save_work(sm_save_point_t& sp);
1354 
1355  static rc_t _rollback_work(const sm_save_point_t& sp);
1356 
1357  static rc_t _get_store_info(
1358  const StoreID& stid,
1359  sm_store_info_t& info);
1360 
1361  //
1362  // The following functions deal with files of records.
1363  //
1364 
1365  static store_flag_t _make_store_flag(store_property_t property);
1366  // reverse function:
1367  // static store_property_t _make_store_property(uint32_t flag);
1368  // is in dir_vol_m
1369 
1370 };
1371 
1378 public:
1380  store(0),
1381  root(0) {}
1382 
1384 
1387 
1390 };
1391 
1392 ostream& operator<<(ostream& o, const sm_stats_t& s);
1393 
1394 template<class ostream>
1395 ostream& operator<<(ostream& o, const sm_config_info_t& s) {
1396  o << " page_size " << s.page_size
1397  << " lg_rec_page_space " << s.lg_rec_page_space
1398  << " buffer_pool_size " << s.buffer_pool_size
1399  << " max_btree_entry_size " << s.max_btree_entry_size
1400  << " logging " << s.logging;
1401  return o;
1402 }
1403 
1404 #ifndef __VEC_T_H
1405 #include "vec_t.h"
1406 #endif // __VEC_T_H
1407 
1408 /*<std-footer incl-file-exclusion='SM_H'> -- do not edit anything below this line -- */
1409 
1410 #endif // __SM_H /*</std-footer>*/
Lock Manager API.See Orthogonal Key Value Locking and Light-weight Intent Lock.
Definition: lock.h:27
u_long page_size
compile-time constant. Settable in
Definition: smstats.h:226
static xct_t * xct()
return xct this thread is running
Definition: smthread.h:364
friend ostream & operator<<(ostream &o, const sm_save_point_t &p)
Definition: sm.h:374
uint32_t smsize_t
Definition: basics.h:41
Definition: lock_lil.h:147
static int _instance_cnt
Definition: sm.h:1318
u_long max_btree_entry_size
Largest permissible size in bytes of an index entry (key,value pair)
Definition: smstats.h:242
smlevel_0::concurrency_t concurrency_t
Definition: sm.h:413
A transaction. Internal to the storage manager.This class may be used in a limited way for the handli...
Definition: xct.h:185
Page handle for B-Tree data page.
Definition: btree_page_h.h:185
Definition: btree_verify.h:84
uint32_t StoreID
Definition: basics.h:47
Key string class which can represent a few special values.
Definition: w_key.h:47
Start-up parameters for the storage engine. See OPTIONS.
Definition: sm_options.h:24
Information about a store that can be queried by the client.
Definition: sm.h:1377
uint32_t PageID
Definition: basics.h:45
Definition: chkpt.h:256
This is the SHORE Storage Manager API.
Definition: sm.h:405
Configuration Information.
Definition: smstats.h:221
Log Sequence Number. See Log Sequence Numbers (LSN).
Definition: lsn.h:243
sm_store_property_t store_property_t
Definition: sm.h:417
Handle class for pages that may be fixed (i.e., paged in by the main buffer manager, zero::buffer_pool::BufferPool)
Definition: fixable_page_h.h:26
static const sm_options & get_options()
Definition: sm.h:1312
static void detach_xct(xct_t *x)
Definition: smthread.cpp:396
std::array< long, enum_to_base(sm_stat_id::stat_max)> sm_stats_t
Definition: smstats.h:205
static constexpr int WAIT_SPECIFIED_BY_XCT
Definition: timeout.h:30
Return code for most functions and methods.
Definition: w_rc.h:87
static sm_options _options
Definition: sm.h:1310
PageID root
Root page if this is an index.
Definition: sm.h:1389
friend istream & operator>>(istream &i, sm_save_point_t &p)
Definition: sm.h:378
sm_save_point_t()
Definition: sm.h:372
Definition: btree.h:28
tid_t tid() const
Definition: sm.h:383
StoreID store
store number
Definition: sm.h:1386
Represents a lock mode of one key entry in the OKVL lock manager.
Definition: w_okvl.h:95
The means of identifying a desired or held lock.
Definition: lock_s.h:41
u_long lg_rec_page_space
Definition: smstats.h:235
bool logging
True if logging is on.
Definition: smstats.h:257
Vector: a set of {pointer,length} pairs for memory manipulation.
Definition: vec_t.h:313
static void attach_xct(xct_t *x)
Attach the given transaction to the currently-running smthread_t.
Definition: sm.h:846
smlevel_0::xct_state_t xct_state_t
Definition: sm.h:415
~sm_store_info_t()
Definition: sm.h:1383
static constexpr int WAIT_SPECIFIED_BY_THREAD
Definition: timeout.h:29
u_long buffer_pool_size
Size in KB of buffer pool.
Definition: smstats.h:238
uint64_t tid_t
Definition: tid_t.h:59
sm_store_info_t()
Definition: sm.h:1379
tid_t _tid
Definition: sm.h:390
static void detach_xct()
Detach any attached from the currently-running smthread_t.
Definition: sm.h:857
concurrency_t
Lock granularities.
Definition: sm_base.h:256
friend class ss_m
Definition: sm.h:388
A point to which a transaction can roll back.
Definition: sm.h:370