Implementation Notes

Storage Manager Modules

The storage manager code contains the following modules (with related C++ classes):

I/O Manager

The I/O manager was, in the early days of SHORE, expected to have more responsibility than it now has; now it is little more than a wrapper for the Volume Manager.

Volume Manager

The volume manager handles formatting of volumes, allocation and deallocation of pages and extents in stores. Within a page, allocation of space is up to the manager of the storage structure (btree, rtree, or file).

Pages are reserved for a store in units of ss_m::ext_sz, a compile-time constant that indicates the size of an extent. An extent is a set of contiguous pages. Extents are represented by persistent data structures, extnode_t, which are linked together to form the entire structure of a store. A stnode_t holds metadata for a store and sits at the head of the chain of extents that forms the store, and the extents in the store list are marked as having an owner, which is the store id of the store to which they belong. A store id is a number of type snum_t, and an extent id is a number of type extnum_t. Scanning the pages in a store can be accomplished by scanning the list of extnode_t. Each extent has a number, and the pages in the extent are arithmetically derived from the extent number; likewise, from any page, its extent can be computed. Free extents are not linked together; they simply have no owner (signified by an extnode_t::owner == 0).

Page Types

Pages in a volume come in a variety of page types, all the same size. The size of a page is a compile-time constant.
Bug:
GNATS 127: Larger page sizes are broken. We temporarily support only 8KB pages.
Each page type is a C++ class that derives from the base class page_p. Page_p implements functionality that is common to all (or most) page types. The types are as follows:

Issues specific to the page types will be dealt with in the descriptions of the modules.

Space Reservation on a Page

All pages are slotted (those that don't need the slot structure may use only one slot) and have the following layout:

Different storage structures offer different opportunities for fine-grained locking and need different means of allocation space within a page. Special care is taken to reserve space on a page when slots are freed (records are deleted) so that rollback can restore the space on the page. Page types that use this space reservation have

 page_p::rsvd_mode() == true 
.

In the case of B-trees, this is not used because undo and redo are handled logically -- entries can be re-inserted in a different page. But in the case of files, records are identified by physical ID, which includes page and slot number, so records must be reinserted just where they first appeared.

Holes in a page are coalesced (moved to the end of the page) as needed, when the total free space on the page satisfies a need but the contiguous free space does not. Hence, a record truncation followed by an append to the same record does not necessarily cause the shifting of other records on the same page.

A count of free bytes is maintained for all pages. More space-allocation metadata is maintained for rsvd_mode() pages.

When a transaction release a slot on a page with rsvd_mode(), the slot remains "reserved" for use by the same transaction. That slot is not free to be allocated by another transaction until the releasing transaction commits. This is because if the transaction aborts, the slot must be restored with the same slot number. Not only must the slot number be preserved, but the number of bytes consumed by that slot must remain available lest the transaction abort. The storage manager keeps track of the youngest active transaction that is freeing space (i.e., "reserving" it) on the page and the number of bytes freed ("reserved") by the youngest transaction. When the youngest transaction to reserve space on the page becomes older than the oldest active transaction in the system, the reserved space becomes free. This check for freeing up the reserved space happens whenever a transaction tries to allocate space on the page.

During rollback, a transaction can use any amount of reserved space, but during forward processing, it can only use space it reserved, and that is known only if the transaction in question is the youngest transaction described in the above paragraph.

The changes to space-reservation metadata (space_t) are not logged. The actions that result in updates to this metadata are logged (as page mark and page reclaim).

Bug:
GNATS 138 (performance) The allocation of space for records is known to have performance problems and this portion of the storage manager needs redesign. This is a problem inherited from the original SHORE storage manager.

Allocation of Extents and Pages

Extents are linked through extlink_t to form a linked list, which composes the allocated extents of a store. The volume manager handles allocating extents to stores. Deallocation of extents is shared by the volume manager and the the lock manager (see the discussion in lockid_t::set_ext_has_page_alloc).

The volume manager keeps a cache of {store, extent} pairs, to find extents already allocated to a store that contain free pages. This cache is consulted before new extents are allocated to a store. Since after restart the cache is necessarily empty, it is primed when first needed for the purpose of allocating anything for the store.

Priming the store is an expensive operation. It is not done on each volume mount, because volumes are mounted and dismounted several times during recovery, and priming on each mount would be prohibitive.

File Manager

A file is a group of variable-sized records (or objects). A record is the smallest persistent datum that has identity A record may also have a "user header", whose contents are for use by the server. As records vary in size, so their storage representation varies. The storage manager changes the storage representation as needed. A file comprises two stores. One store is allocated for slotted (small-record) pages, called file_p pages. One store is allocated for large records, and contains lgdata_p and lgindex_p pages. Small records are those whose size is less than or equal to sm_config_info_t.max_small_rec. A record larger than this has a slot on a small-record page, which slot contains metadata refering to pages in the large-record store. The scan order of a file is the physical order of the records in the small-record store.

Every record, large or small, has the following metadata in the record's slot on the file_p page; these data are held in a rectag_t structure:

struct rectag_t {
    uint2_t   hdr_len;  // length of user header, may be zero
    uint2_t   flags;    // enum recflags_t: indicates internal implementation
    smsize_t  body_len; // true length of the record 
};
The flags have have these values:

Internally (inside the storage manager), the class record_t is a handle on the record's tag and is the class through which the rectag_t is manipulated.

A record is exposed to the server through a set of ss_m methods (create_rec, append_rec, etc), and through the pin_i class.

All updates to records are accomplished by copying out part or all of the record from the buffer pool to the server's address space, performing updates there, and handing the new data to the storage manager. User (server) data are not updated directly in the buffer pool.

The server may cause the file_p and at most one large data page to be pinned for a given record through the pin_i class; the server must take care not to create latch-latch deadlocks by holding a record pinned while attempting to pin another. An ordering protocol among the pages pinned must be observed to avoid such deadlocks.

Note:
The system only detects lock-lock deadlocks. Deadlocks involving mutexes or latches or other blocking mechanisms will cause the server to hang.

Finding a Page with Space

When a record is created, the file manager tries to use an already-allocated page that has space for the record (based on the length hint and the data given in the create_rec call). The file manager caches information about page utilization for pages in each store. The page utilization data take the form of a histoid_t, which contains a heap and a histogram. The heap keeps track of the amount of free space in (recently-used) pages in the heap, and it is searchable so that it can return the page with the smallest free space that is larger than a given value. The histogram has a small number of buckets, each of which counts the number of pages in the file containing free space between the bucket min and the bucket max.

Three policies used can be used in combination to search for space.

Using append_file_t to create records means only t_append is used, ensuring that the record will always be appended to the file. ss_m::create_rec uses t_cache | t_compact | t_append.

Bug:
GNATS 33 (performance) t_compact is turned off. It is expensive and has not been tested lately, thus there is presently no policy that will keep files compact. If files become too sparse, the server must reorganize the database.
Once the file manager has located a page with sufficient space to create the record, the I/O and volume managers worry about Space Reservation on a Page.

Finding a Page with Space

If the file manager cannot find an already-allocated page with sufficient space, or if it is appending a record to a file and needs to allocate a new page, it first descends to the I/O manager to find an extent with a free page (or the last extent in the file) ( Allocation of Extents and Pages).

Once it has found an extent with a free page, it allocates a page in that extent.

IX-locks are acquired on file pages for the purpose of fine-grained locking. There is no file structure superimposed on the store, with which to link the file pages together, so as soon as an extent is allocated to a store, it is visible to any transaction; in particular, it is visible between the time the page is allocated by the I/O manager and the time the file manager formats that page for use. For this reason, the allocation of a file page must be a top-level action so that undo of the allocation is logical: it must check for the file page still being empty before the page can be freed. If another transaction slipped in an allocated a record on the same page, the page can't be freed on undo of the page allocation.

Protocol for allocating a file page:

Bug:
GNATS 129 There is a gap between the end of the top-level action and the logging of the file-page allocation during which a crash could cause a page to be allocated but unformatted. This needs to be fixed; the fix is to support undoable-compensation log records so that the log insert and the compensation can be done in one operation. The issue is in handling these records on restart.
To free a page, the transaction must acquire an EX lock on the page; this prevents the page from being used by any other transaction until the freeing transaction commits. If the EX lock cannot be acquired, the page is in use and will not be freed (e.g., the other transaction could abort and re-insert something on the page).

B+-Tree Manager

The values associated with the keys are opaque to the storage manager, except when IM (Index Management locking protocol) is used, in which case the value is treated as a record ID, but no integrity checks are done. It is the responsibility of the server to see that the value is legitimate in this case.

B-trees can be bulk-loaded from files of sorted key-value pairs, as long as the keys are in lexicographic form.

Bug:
GNATS 116 Btree doesn't sort elements for duplicate keys in bulk-load. This is a problem inherited from the original SHORE storage manager.
The implementation of B-trees is straight from the Mohan ARIES/IM and ARIES/KVL papers. See [MOH1], which covers both topics.

Those two papers give a thorough explanation of the arcane algorithms, including logging considerations. Anyone considering changing the B-tree code is strongly encouraged to read these papers carefully. Some of the performance tricks described in these papers are not implemented here. For example, the ARIES/IM paper describes performance of logical undo of insert operations if and only if physical undo is not possible. The storage manager always undoes inserts logically.

Bug:
GNATS 137 Latches can now be downgraded; btree code should use this.

R*-Tree Manager

The spatial indexes in the storage manager are R*-trees, a variant of R-trees that perform frequent restructuring to yield higher performance than normal R-trees. The entire index is locked. See [BKSS].

Directory Manager

All storage structures created by a server have entries in a B+-Tree index called the store directory or just directory. This index is not exposed to the server.

The storage manager maintains some transient and some persistent data for each store. The directory's key is the store ID, and the value it returns from a lookup is a sdesc_t ("store descriptor") structure, which contains both the persistent and transient information.

The persistent information is in a sinfo_s structure; the transient information is resident only in the cache of sdesc_t structures that the directory manager maintains.

The metadata include:

Lock Manager

The lock manager understands the folling kind of locks

Lock requests are issued with a lock ID (lockid_t), which encodes the identity of the entity being locked, the kind of lock, and, by inference, a lock hierarchy for a subset of the kinds of locks above. The lock manager does not insist that lock identifiers refer to any existing object.

The lock manager enforces two lock hierarchies:

Note that the following lock kinds are not in any hierarchy: -extent -user1, user2, user3, user4

Other than the way the lock identifiers are inspected for the purpose of enforcing the hierarchy, lock identifiers are considered opaque data by the lock manager.

The lockid_t structure can be constructed from the IDs of the various entities in (and out of ) the hierarchy; see lockid_t and the example lockid_test.cpp.

The lock manager escalates up the hierarchy by default. The escalation thresholds are based on run-time options. They can be controlled (set, disabled) on a per-object level. For example, escalation to the store level can be disabled when increased concurrency is desired. Escalation can also be controlled on a per-transaction or per-server basis.

Locks are acquired by storage manager operations as appropriate to the use of the data (read/write). (Update locks are not acquired by the storage manager.)

The storage manager's API allows explicit acquisition of locks by a server. Freeing locks is automatic at transaction commit and rollback. There is limited support for freeing locks in the middle of a transaction:

A quark is a marker in the list of locks held by a transaction. When the quark is destroyed, all locks acquired since the creation of the quark are freed. Quarks cannot be used while more than one thread is attached to the transaction, although the storage manager does not strictly enforce this (due to the cost). When a quark is in use for a transaction, the locks acquired will be of short duration, the assumption being that the quark will be closed before commit-time.

Extent locks are an exception; they must be held long-term for page allocation and deallocation to work, so even in the context of an open quark, extent locks will be held until end-of-transaction.

The lock manager uses a hash table whose size is determined by a configuration option. The hash function used by the lock manager is known not to distribute locks evenly among buckets. This is partly due to the nature of lock IDs.

To avoid expensive lock manager queries, each transaction keeps a cache of the last <N> locks acquired (the number <N> is a compile-time constant). This close association between the transaction manager and the lock manager is encapsulated in several classes in the file lock_x.

Deadlock Detection

The lock manager uses a statistical deadlock-detection scheme known as "Dreadlocks" [KH1]. Each storage manager thread (smthread_t) has a unique fingerprint, which is a set of bits; the deadlock detector ORs together the bits of the elements in a waits-for-dependency-list; each thread, when blocking, holds a digest (the ORed bitmap). It is therefore cheap for a thread to detect a cycle when it needs to block awaiting a lock: look at the holders of the lock and if it finds itself in any of their digests, a cycle will result. This works well when the total number of threads relative to the bitmap size is such that it is possible to assign a unique bitmap to each thread. If you cannot do so, you will have false-positive deadlocks "detected". The storage manager counts, in its statistics, the number of times it could not assign a unique fingerprint to a thread. If you notice excessive transaction-aborts due to false-positive deadlocks, you can compile the storage manager to use a larger number bits in the
 sm_thread_map_t 
found in
 smthread.h 
.

Transaction Manager

When a transaction commits, the stores that are marked for deletion are deleted, and the stores that were given sm_store_property_t t_load_file or t_insert_file are turned into t_regular stores. Because these are logged actions, and they occur if and only if the transaction commits, the storage manager guarantees that the ending of the transaction and re-marking and deletion of stores is atomic. This is accomplished by putting the transaction into a state xct_freeing_space, and writing a log record to that effect. The space is freed, the stores are converted, and a final log record is written before the transaction is truly ended. In the vent of a carash while a transaction is freeing space, recovery searches all the store metadata for stores marked for deleteion and deletes those that would otherwise have been missed in redo.

Log Manager

How the Server Uses the Log Manager

Log records for redoable-undoable operations contain both the redo- and undo- data, hence an operation never causes two different log records to be written for redo and for undo. This, too, controls logging overhead.

The protocol for applying an operation to an object is as follows:

The protocol for writing log records is as follows:

Between the time the xct log buffer is grabbed and the time it is released, the buffer is held exclusively by the one thread that grabbed it, and updates to the xct log buffer can be made freely. (Note that this per-transaction log buffer is unrelated to the log buffer internal to the log manager.)

The above protocol is enforced by the storage manager in helper functions that create log records; these functions are generated by Perl scripts from the source file logdef.dat. (See LOGDEF.)

Log Record Types

The input to the above-mentioned Perl script is the source of all log record types. Each log record type is listed in the file
 logdef.dat 
which is fairly self-explanatory, reproduced here:
# <std-header style='data' orig-src='shore'>
#
#  $Id: logdef.dat,v 1.63 2010/07/01 00:08:21 nhall Exp $
#
# SHORE -- Scalable Heterogeneous Object REpository
#
# Copyright (c) 1994-99 Computer Sciences Department, University of
#                       Wisconsin -- Madison
# All Rights Reserved.
#
# Permission to use, copy, modify and distribute this software and its
# documentation is hereby granted, provided that both the copyright
# notice and this permission notice appear in all copies of the
# software, derivative works or modified versions, and any portions
# thereof, and that both notices appear in supporting documentation.
#
# THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY
# OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS
# "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND
# FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
#
# This software was developed with support by the Advanced Research
# Project Agency, ARPA order number 018 (formerly 8230), monitored by
# the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518.
# Further funding for this work was provided by DARPA through
# Rome Research Laboratory Contract No. F30602-97-2-0247.
#
#   -- do not edit anything above this line --   </std-header>

#########################################################################
#                                    #
#    WARNING: if you add, delete or change any of the log records,    #
#    or their data members, or their semantics you also need to    #
#    update log_base::version_major and/or log_base::version_minor    #
#    in log_base.cpp.                        #
#                                                                       #
#       For every log record type, the perl script generates a class    #
#       class <type>_log {                                              #
#           void fill(const lpid_t*p, uint2_t tag, int len);            #
#       public:                                                         #
#           <type>_log(<arg>);                                          #
#        // and...                                                      #
#        // iff R bit set:                                              #
#        void redo(page_p *page);                                       #
#        // iff U bit set:                                              #
#        void undo(page_p *page);                                       #
#       }                                                               #
#                                                                       #
#    The format of the file is as follows:                              #
#        type = log record type                                         #
#        X    = transaction log (generated by transactions)             #
#                      If set, logstub_gen.cpp contains a function      #
#                      rc_t log_<type> (<arg>) to generate the log recs #
#                      according to convention.  If not, the code else- #
#                      where in the SM has to be written by hand to gen #
#                      the log record.                                  #
#        S    = sync (not used at all anymore)                          #
#        R    = redoable    (-->t_redo bit set in log record)           #
#                      Includes redo method in class                    #
#        U    = undoable    (-->t_undo)                                 #
#                      Includes undo method in class                    #
#        F    = format    NOT USED                                      #
#        A    = space-allocation:                                       #
#                      If NOT set, generated code decides if logging    #
#                      should be done, based on :                       #
#                      1) smlevel_1::log, smlevel_0::logging_enabled,   #
#                      2) (if page argument present) page.store_flags   #
#                                            == st_tmp                  #
#                      3) xct() attached and xct()->is_log_on()         #
#                                                                       #
#                      If A bit IS SET, checks #2, #3 are left out      #
#                                                                       #
#        L    = logical undo log record -- don't fix the page           #
#                                        for undo.  Irrelevant if not   #
#                      an undoable log record.                          #
#                      --> t_logical                                    # 
#                                                                       #
#                                                                       #
#        arg  = arguments to constructor                                #
#                      SPECIAL CASE: first argument is "page":          #
#                      1) store flags checked to turn off logging for   #
#                      st_tmp files.                                    #
#                      2) give_logbuf() call passes page for 2nd arg    #
#                      3) page.set_dirty() if logging is skipped        #
#                                    #
#########################################################################
# type             XSRUFAL     arg                                      #
#########################################################################
comment            1011001 (const char* msg);
compensate         1000001 (lsn_t  rec_lsn);
skip               0000000 ();
chkpt_begin        0000000 (const lsn_t &lastMountLSN);
chkpt_bf_tab       0000000 (int cnt, const lpid_t* pid,
                            const lsn_t* rec_lsn);
chkpt_xct_tab      0000000 (const tid_t& youngest, 
                            int cnt, const tid_t* tid, 
                            const smlevel_1::xct_state_t* state,
                            const lsn_t* last_lsn, const lsn_t* undo_nxt);
chkpt_dev_tab      0000000 (int cnt, const char **dev_name, const vid_t* vid);
chkpt_end          0000000 (const lsn_t& master, const lsn_t& min_rec_lsn);
mount_vol          0010010 (const char *dev_name, const vid_t &vid);
dismount_vol       0010010 (const char *dev_name, const vid_t &vid);
#########################################################################
# type             XSRUFAL     arg                                      #
#########################################################################
xct_abort          1000000 ();
xct_freeing_space  1000000 ();
xct_end            1000000 ();
xct_prepare_st     1010000 (const gtid_t* g, const server_handle_t& h);
xct_prepare_lk     1010000 (int num, lock_mode_t mode, lockid_t* lks);
xct_prepare_alk    1010000 (int num, lockid_t* lks, lock_mode_t* modes);
xct_prepare_stores 1010000 (int num, const stid_t* stids);
xct_prepare_fi     1010000 (int numex, int numix, int numsix, int numextent, const lsn_t& first, int rsvd, int ready, int used);
#########################################################################
# type             XSRUFAL     arg                                      #
#########################################################################
# page allocation log records - testable(physical) for redo
# alloc_file_page is marked "logical" because there's no need to fix the
# page in the automagic-handling code.
alloc_file_page    1001011 (const lpid_t& pid);
alloc_pages_in_ext 1011011 (const page_p& page, snum_t snum, 
                            extnum_t idx, const Pmap& pmap);
free_pages_in_ext  1011011 (const page_p& page, snum_t snum, 
                            extnum_t idx, const Pmap& pmap);

# page allocation log records - testable(physical) for redo
# create_ext_list for creation of an extent list all on same page
# free_ext_list is reverse of create_ext_list; all on same page
create_ext_list    1010011 (const page_p& page, const stid_t& stid, 
                           extnum_t prev, extnum_t next, 
                           extnum_t count, const extnum_t* list);
free_ext_list      1010011 (const page_p& page, const stid_t& stid, 
                            extnum_t head, extnum_t count);
# set_ext_next: when extent lists cross page boundaries
set_ext_next       1010011 (const page_p& page, extnum_t ext, 
                            extnum_t new_next);
store_operation    1011011 (const page_p& page, 
                            const store_operation_param& op);
#########################################################################
# type             XSRUFAL     arg                                      #
#########################################################################
#page_link used by btree pages only, for now
page_link          1011000 (const page_p& page, shpid_t new_prev, 
                            shpid_t new_next);

# page_insert used by page_p::insert_expand (inserting into a slot): generic
# page_remove used by page_p::remove_compress (removing a slot): semi-generic
page_insert        1011000 (const page_p& page, int idx, int cnt, 
                            const cvec_t* vec);
page_remove        1011000 (const page_p& page, int idx, int cnt);

# A page format reflects two operations: the page init/format part and
# the insertion of something into the first slot.
# generic page format: the page init part isn't undoable but the
# insert_expand/reclaim part is undoable
page_format        1011000 (const page_p& page, int idx, int cnt, 
                            const cvec_t* vec);
# page_mark: marks a slot as deleted
# page_reclaim: opposite of page_mark: makes a slot in-use
page_mark          1011000 (const page_p& page, int idx);
page_reclaim       1011000 (const page_p& page, int idx, const cvec_t& vec);

# shift: move data from slot to slot: very generic
# used by btree & rtree pages
page_shift         1011000 (const page_p& page, int idx2, 
                            page_s::slot_length_t off2, 
                            page_s::slot_length_t len2,
                            int idx1,  page_s::slot_length_t off1);
# splice and splicez: very generic.
# used by btree, rtree, file, large obj pages,
# for cut/paste/overwrite/merge_slots, etc.
page_splice        1011000 (const page_p& page, int idx, int start, int len, 
                            const cvec_t& vec);
page_splicez       1011000 (const page_p& page, int idx, int start, 
                            int len, int osave, int nsave, const cvec_t& vec);

# page_set_byte: for now used only by extlink pages
page_set_byte      1011000 (const page_p& page, int idx, u_char old, 
                            u_char bits, int op);
#
# page_image: for now used only by rtree pages & btree pages 
page_image         1010000 (const page_p& page);
#########################################################################
# type             XSRUFAL     arg                                      #
#########################################################################
btree_purge        1011001 (const page_p& page);
btree_insert       1011001 (const page_p& page, int idx, 
                            const cvec_t& key, const cvec_t& el,
                            bool unique);
btree_remove       1011001 (const page_p& page, int idx, 
                            const cvec_t& key, const cvec_t& el,
                            bool unique);
rtree_insert       1011001 (const page_p& page, int idx,
                            const nbox_t& key, const cvec_t& el);
rtree_remove       1011001 (const page_p& page, int idx, 
                            const nbox_t& key, const cvec_t& el);
#########################################################################
# type             XSRUFAL     arg                                      #
#########################################################################

The bodies of the methods of the class <log-rec-name>_log are hand-written and reside in

 logrec.cpp 
.

Adding a new log record type consists in adding a line to

 logdef.dat, 
adding method definitions to
 logrec.cpp, 
and adding the calls to the free function log_<log-rec-name>(args) in the storage manager. The base class for every log record is logrec_t, which is worth study but is not documented here.

Some logging records are compensated, meaning that the log records are skipped during rollback. Compensations may be needed because some operation simply cannot be undone. The protocol for compensating actions is as follows:

Note:
Grabbing an anchor prevents all other threads in a multi-threaded transaction from gaining access to the transaction manager. Be careful with this, as it can cause mutex-latch deadlocks where multi-threaded transactions are concerned. In other words, two threads cannot concurrently update in the same transaction.
In some cases, the following protocol is used to avoid excessive logging by general update functions that, if logging were turned on, would generate log records of their own.

The mechanism for turning off logging for a transaction is to construct an instance of xct_log_switch_t.

When the instance is destroyed, the original logging state is restored. The switch applies only to the transaction that is attached to the thread at the time the switch instance is constructed, and it prevents other threads of the transaction from using the log (or doing much else in the transaction manager) while the switch exists.

Log Manager Internals

The log is a collection of files, all in the same directory, whose path is determined by a run-time option. Each file in the directory is called a "log file" and represents a "partition" of the log. The log is partitioned into files to make it possible to archive portions of the log to free up disk space. A log file has the name log.<n> where <n> is a positive integer. The log file name indicates the set of logical sequence numbers (lsn_t) of log records (logrec_t) that are contained in the file. An lsn_t has a high part and a low part, and the high part (a.k.a., file part) is the <n> in the log file name.

The user-settable run-time option sm_logsize indicates the maximum number of KB that may be opened at once; this, in turn, determines the size of a partition file, since the number of partition files is a compile-time constant. The storage manager computes partition sizes based on the user-provided log size, such that partitions sizes are a convenient multiple of blocks (more about which, below).

A new partition is opened when the tail of the log approaches the end of a partition, that is, when the next insertion into the log is at an offset larger than the maximum partition size. (There is a fudge factor of BLOCK_SIZE in here for convenience in implementation.)

The low part of an lsn_t represents the byte-offset into the log file at which the log record with that lsn_t sits.

Thus, the total file size of a log file log.<n> is the size of all log records in the file, and the lsn_t of each log record in the file is lsn_t(<n>, <byte-offset>) of the log record within the file.

The log is, conceptually, a forever-expanding set of files. The log manager will open at most PARTITION_COUNT log files at any one time.

The log is considered to have run out of space if logging requires that more than smlevel_0::max_openlog partitions are needed. Partitions are needed only as long as they contain log records needed for recovery, which means:

Afer a checkpoint is taken and its log records are durable, the storage manager tries to scavenge all partitions that do not contain necessary log records. The buffer manager provides the min recovery lsn; the transaction manager provides the min xct lsn, and the log manager keeps track of the location of the last completed checkpoint in its master_lsn. Thus the minimum of the

file part of the minmum of these lsns indicates the lowest partition that cannot be scavenged; all the rest are removed.

When the log is in danger of runing out of space (because there are long-running transactions, for example) the server may be called via the LOG_WARN_CALLBACK_FUNC argument to ss_m::ss_m. This callback may abort a transaction to free up log space, but the act of aborting consumes log space. It may also archive a log file and remove it. If the server provided a LOG_ARCHIVED_CALLBACK_FUNC argument to ss_m::ss_m, this callback can be used to retrieve archived log files when needed for rollback.

Warning:
This functionality is not complete and has not been well-tested.
Log files (partitions) are written in fixed-sized blocks. The log manager pads writes, if necessary, to make them BLOCK_SIZE.

A skip_log record indicates the logical end of a partition. The log manager ensures that the last log record in a file is always a skip_log record.

Log files (partitions) are composed of segments. A segment is an integral number of blocks.

The smallest partition is one segment plus one block, but may be many segments plus one block. The last block enables the log manager to write the skip_log record to indicate the end of the file.

The partition size is determined by the storage manager run-time option, sm_logsize, which determines how much log can be open at any time, i.e., the combined sizes of the PARTITION_COUNT partitions.

The maximum size of a log record (logrec_t) is 3 storage manager pages. A page happens to match the block size but the two compile-time constants are not inter-dependent. A segment is substantially larger than a block, so it can hold at least several maximum-sized log records, preferably many.

Inserting a log record consists of copying it into the log manager's log buffer (1 segment in size). The buffer wraps so long as there is room in the partition. Meanwhile, a log-flush daemon thread writes out unflushed portions of the log buffer. The log daemon can lag behind insertions, so each insertion checks for space in the log buffer before it performs the insert. If there isn't enough space, it waits until the log flush daemon has made room.

When insertion of a log record would wrap around the buffer and the partition has no room for more segments, a new partition is opened, and the entire newly-inserted log record will go into that new partition. Meanwhile, the log-flush daemon will see that the rest of the log buffer is written to the old partition, and the next time the log flush daemon performs a flush, it will be flushing to the new partition.

The bookkeeping of the log buffer's free and used space is handled by the notion of epochs. An epoch keeps track of the start and end of the unflushed portion of the segment (log buffer). Thus, an epoch refers to only one segment (logically, log buffer copy within a partition). When an insertion fills the log buffer and causes it to wrap, a new epoch is created for the portion of the log buffer representing the new segment, and the old epoch keeps track of the portion of the log buffer representing the old segment. The inserted log record usually spans the two segements, as the segments are written contiguously to the same log file (partition).

When an insertion causes a wrap and there is no more room in the partition to hold the new segment, a new epoch is created for the portion of the log buffer representing the new segment, and the old epoch keeps track of the portion of the log buffer representing the old segment, as before. Now, however, the inserted log record is inserted, in its entirety, in the new segment. Thus, no log record spans partitions.

Meanwhile, the log-flush buffer knows about the possible existence of two epochs. When an old epoch is valid, it flushes that epoch. When a new epoch is also valid, it flushes that new one as well. If the two epochs have the same target partition, the two flushes are done with a single write.

The act of flushing an epoch to a partition consists in a single write of a size that is an even multiple of BLOCK_SIZE. The flush appends a skip_log record, and zeroes as needed, to round out the size of the write. Writes re-write portions of the log already written, in order to overwrite the skip_log record at the tail of the log (and put a new one at the new tail).

Recovery

The storage manager performs ARIES-style logging and recovery. This means the logging and recovery system has these characteristics:

Each time a storage manager (ss_m class) is constructed, the logs are inspected, the last checkpoint is located, and its lsn is remembered as the master_lsn, then recovery is performed. Recovery consists of three phases: analysis, redo and undo.

Analysis

This pass analyzes the log starting at the master_lsn, and reading log records written thereafter. Reading the log records for the last completed checkpoint, it reconstructs the transaction table, the buffer-pool's dirty page table, and mounts the devices and volumes that were mounted at the time of the checkpoint. From the dirty page table, it determines the redo_lsn, the lowest recovery lsn of the dirty pages, which is where the next phase of recovery must begin.

Redo

This pass starts reading the log at the redo_lsn, and, for each log record thereafter, decides whether that log record's work needs to be redone. The general protocol is:

Undo

After redo, the state of the database matches that at the time of the crash. Now the storage manager rolls back the transactions that remain active. Care is taken to undo the log records in reverse chronological order, rather than allowing several transactions to roll back at their own paces. This is necessary because some operations use page-fixing for concurrency-control (pages are protected only with latches if there is no page lock in the lock hierarchy -- this occurs when logical logging and high-concurrency locking are used, in the B-trees, for example. A crash in the middle of a compensated action such as a page split must result in the split being undone before any other operations on the tree are undone.).
Bug:
GNATS 49 (performance) There is no concurrent undo.
After the storage manager has recovered, control returns from its constructor method to the caller (the server). There might be transactions left in prepared state. The server is now free to resolve these transactions by communicating with its coordinator.

Log Sequence Numbers

Write-ahead logging requires a close interaction between the log manager and the buffer manager: before a page can be flushed from the buffer pool, the log might have to be flushed.

This also requires a close interaction between the transaction manager and the log manager.

All three managers understand a log sequence number (lsn_t). Log sequence numbers serve to identify and locate log records in the log, to timestamp pages, identify timestamp the last update performed by a transaction, and the last log record written by a transaction. Since every update is logged, every update can be identified by a log sequence number. Each page bears the log sequence number of the last update that affected that page.

A page cannot be written to disk until the log record with that page's lsn has been written to the log (and is on stable storage). A log sequence number is a 64-bit structure, with part identifying a log partition (file) number and the rest identifying an offset within the file.

Log Partitions

The log is partitioned to simplify archiving to tape (not implemented) The log comprises 8 partitions, where each partition's size is limited to approximately 1/8 the maximum log size given in the run-time configuration option sm_logsize. A partition resides in a file named log.<n>, where n is the partition number. The configuration option sm_logdir names a directory (which must exist before the storage manager is started) in which the storage manager may create and destroy log files.

The storage manger may have at most 8 active partitions at any one time. An active partition is one that is needed because it contains log records for running transactions. Such partitions could (if it were supported) be streamed to tape and their disk space reclaimed. Space is reclaimed when the oldest transaction ends and the new oldest transaction's first log record is in a newer partition than that in which the old oldest transaction's first log record resided. Until tape archiving is implemented, the storage manager issues an error (eOUTOFLOGSPACE) if it consumes sufficient log space to be unable to abort running transactions and perform all resulting necessary logging within the 8 partitions available.

Note:
Determining the point at which there is insufficient space to abort all running transactions is a heuristic matter and it is not reliable}.
Log records are buffered by the log manager until forced to stable storage to reduce I/O costs. The log manager keeps a buffer of a size that is determined by a run-time configuration option. The buffer is flushed to stable storage when necessary. The last log in the buffer is always a skip log record, which indicates the end of the log partition.

Ultimately, archiving to tape is necessary. The storage manager does not perform write-aside or any other work in support of long-running transactions.

The checkpoint manager chkpt_m sleeps until kicked into action by the log manager, and when it is kicked, it takes a checkpoint, then sleeps again. Taking a checkpoint amounts to these steps:

These checkpoint log records may interleave with other log records, making the checkpoint "fuzzy"; this way the world doesn't have to grind to a halt while a checkpoint is taken, but there are a few operations that must be serialized with all or portions of a checkpoint. Those operations use mutex locks to synchronize. Synchronization of operations is as follows:

Buffer Manager

The buffer manager is the means by which all other modules (except the log manager) read and write pages. A page is read by calling bf_m::fix. If the page requested cannot be found in the buffer pool, the requesting thread reads the page and blocks waiting for the read to complete.

All frames in the buffer pool are the same size, and they cannot be coalesced, so the buffer manager manages a set of pages of fixed size.

Hash Table

The buffer manager maintains a hash table mapping page IDs to buffer control blocks. A control block points to its frame, and from a frame one can arithmetically locate its control block (in bf_m::get_cb(const page_s *)). The hash table for the buffer pool uses cuckoo hashing (see [P1]) with multiple hash functions and multiple slots per bucket. These are compile-time constants and can be modified (bf_htab.h).

Cuckoo hashing is subject to cycles, in which making room on one table bucket A would require moving something else into A. Using at least two slots per bucket reduces the chance of a cycle.

The implementation contains a limit on the number of times it looks for an empty slot or moves that it has to perform to make room. It does If cycles are present, the limit will be hit, but hitting the limit does not necessarily indicate a cycle. If the limit is hit, the insert will fail. The "normal" solution in this case is to rebuild the table with different hash functions. The storage manager does not handle this case.

Bug:
bf_core_m::htab GNATS 47 : In event of insertion failure, the hash table will have to be rebuilt with different hash functions, or will have to be modified in some way.
Bug:
GNATS 35 The buffer manager hash table implementation contains a race. While a thread performs a hash-table lookup, an item could move from one bucket to another (but not from one slot to another within a bucket). The implementation contains a temporary work-around for this, until the problem is more gracefully fixed: if lookup fails to find the target of the lookup, it performs an expensive lookup and the statistics record these as bf_harsh_lookups. This is expensive.

Page Replacement

When a page is fixed, the buffer manager looks for a free buffer-pool frame, and if one is not available, it has to choose a victim to replace. It uses a clock-based algorithm to determine where in the buffer pool to start looking for an unlatched frame: On the first pass of the buffer pool it considers only clean frames. On the second pass it will consider dirty pages, and on the third or subsequent pass it will consider any frame.

The buffer manager forks background threads to flush dirty pages. The buffer manager makes an attempt to avoid hot pages and to minimize the cost of I/O by sorting and coalescing requests for contiguous pages. Statistics kept by the buffer manager tell the number of resulting write requests of each size.

There is one bf_cleaner_t thread for each volume, and it flushes pages for that volume; this is done so that it can combine contiguous pages into single write requests to minimize I/O. Each bf_cleaner_t is a master thread with multiple page-writer slave threads. The number of slave threads per master thread is controlled by a run-time option. The master thread can be disabled (thereby disabling all background flushing of dirty pages) with a run-time option.

The buffer manager writes dirty pages even if the transaction that dirtied the page is still active (steal policy). Pages stay in the buffer pool as long as they are needed, except when chosen as a victim for replacement (no force policy).

The replacement algorithm is clock-based (it sweeps the buffer pool, noting and clearing reference counts). This is a cheap way to achieve something close to LRU; it avoids much of the overhead and mutex bottlenecks associated with LRU.

The buffer manager maintains a hash table that maps page IDs to buffer frame control blocks (bfcb_t), which in turn point to frames in the buffer pool. The bfcb_t keeps track of the page in the frame, the page ID of the previously-held page, and whether it is in transit, the dirty/clean state of the page, the number of page fixes (pins) held on the page (i.e., reference counts), the recovery lsn of the page, etc. The control block also contains a latch. A page, when fixed, is always fixed in a latch mode, either LATCH_SH or LATCH_EX.

Bug:
GNATS 40 bf_m::upgrade_latch() drops the latch and re-acquires in the new mode, if it cannot perform the upgrade without blocking. This is an issue inherited from the original SHORE storage manager. To block in this case would enable a deadlock in which two threads hold the latch in SH mode and both want to upgrade to EX mode. When this happens, the statistics counter bf_upgrade_latch_race is incremented.
Page fixes are expensive (in CPU time, even if the page is resident).

Each page type defines a set of fix methods that are virtual in the base class for all pages: The rest of the storage manager interacts with the buffer manager primarily through these methods of the page classes. The macros MAKEPAGECODE are used for each page subtype; they define all the fix methods on the page in such a way that bf_m::fix() is properly called in each case.

A page frame may be latched for a page without the page being read from disk; this is done when a page is about to be formatted.

The buffer manager is responsible for maintaining WAL; this means it may not flush to disk dirty pages whose log records have not reached stable storage yet. Temporary pages (see sm_store_property_t) do not get logged, so they do not have page lsns to assist in determining their clean/dirty status, and since pages may change from temporary (unlogged) to logged, they require special handling, described below.

When a page is unfixed, sometimes it has been updated and must be marked dirty. The protocol used in the storage manager is as follows:

It is possible that a page is fixed in EX mode, marked dirty but never updated after all, then unfixed. The buffer manager attempts to recognize this situation and clean the control block "dirty" bit and recovery lsn.

Things get a little complicated where the buffer-manager's page-writer threads are concerned. The page-writer threads acquire a share latches and copy dirty pages; this being faster than holding the latch for the duration of the write to disk When the write is finished, the page-writer re-latches the page with the intention of marking it clean if no intervening updates have occurred. This means changing the dirty bit and updating the recovery lsn in the buffer control block. The difficulty lies in determining if the page is indeed clean, that is, matches the latest durable copy. In the absence of unlogged (t_temporary) pages, this would not be terribly difficult but would still have to cope with the case that the page was (updated and) written by another thread between the copy and the re-fix. It might have been cleaned, or that other thread might be operating in lock-step with this thread. The conservative handling would be not to change the recovery lsn in the control block if the page's lsn is changed, however this has serious consequences for hot pages: their recovery lsns might never be moved toward the tail of the log (the recovery lsns remain artificially low) and thus the hot pages can prevent scavenging of log partitions. If log partitions cannot be scavenged, the server runs out of log space. For this reason, the buffer manager goes to some lengths to update the recovery lsn if at all possible. To further complicate matters, the page could have changed stores, and thus its page type or store (logging) property could differ. The details of this problem are handled in a function called determine_rec_lsn().

Page Writer Mutexes

The buffer manager keeps a set of N mutexes to sychronizing the various threads that can write pages to disk. Each of these mutexes covers a run of pages of size smlevel_0::max_many_pages. N is substantially smaller than the number of "runs" in the buffer pool (size of the buffer pool/max_many_pages), so each of the N mutexes actually covers several runs:
 page-writer-mutex = page / max_many_pages % N

Foreground Page Writes and Discarding Pages

Pages can be written to disk by "foreground" threads under several circumstances. All foreground page-writing goes through the method bf_m::_scan. This is called for:
Generated on Wed Jul 7 17:22:33 2010 for Shore Storage Manager by  doxygen 1.4.7