CSE190D: Topics in Database System Implementation Project 1: BadgerDB Buffer Manager Implementation Discussion: Friday, April 14, 2017 Presenter: Digvijay Karamchandani Slide Contributors: Apul Jain, Arun Kumar
Goal Goal: Allows students to learn about the internals of data processing. First-hand experience building the internals of a simple RDBMS Two parts: Buffer manager B+-tree
BadgerDB: IO Layer Allows upper level layer to Create/destroy files Allocate/deallocate pages Read/write pages All code provided!
BadgerDB: IO Layer File Class(File.h) create open remove isOpen exists filename = Page Class(Page.h) Page allocatePage() Page readPage(pageNo) void writePage(newPage) void deletePage(pageNo)
The Structure of the Buffer Manager The BadgerDB buffer manager uses three C++ classes: BufMgr BufDesc BufHashTbl
BufHashTbl Class Used to map file and page numbers to buffer pool frames. Implemented using chained bucket hashing.
BufDesc Class Used to keep track of the state of each frame Buffer is described by three attributes : Dirty Bit, Pin Count, Reference Bit, Valid Bit Dirty bit: if true indicates that the page is dirty (i.e. has been updated) and thus must be written to disk before the frame is used to hold another page. RefBit: used by the clock algorithm. PinCount : indicates how many times the page has been pinned. Valid bit: is used to indicate whether the frame contains a valid page.
BufMgr Class The BufMgr class is the heart of the buffer manager. This is where you write your code for this assignment.
Buffer Pools (Frames) In-memory buffer holds database pages that are read from disk P1 from File1 <empty> P4 from File2 Buffer pool (frames) P1 from File1 <empty> P4 from file2 unit of transfer is a “page” Files with pages on disk File1 P1 P2 P3 P4 P5 File2 P1 P2 P3 P4 P5
Buffer Manager: Purpose Control which pages reside in memory Request Page N From File M Buffer Manager - look up in buffer pool P1 from File1 <empty> P4 from File2 If it is in the buffer pool: return! If not: frees a frame , reads pages from disk into the frame
Buffer Replacement: Clock Algorithm Requested page in Buffer Pool: pinCnt++/ Return handle to frame Else read page in from disk find space in the buffer pool! Frame b/k: pinCnt dirty refbit … Page in use and dirty (2, 1, 0) Page in use but not dirty (3, 0, 0) Page unpinned and now not referenced Page unpinned but ref (0, 0, 0) (0, 0, 1) Page dirty but not ref/pinned (0, 1, 0) Use this frame!
Call “Set()” on the Frame Clock Algorithm Advance Clock Pointer No Valid set? Yes refbit set? Yes Clear refbit No Yes Page pinned? No Dirty bit set? Yes Flush page to disk No Call “Set()” on the Frame Use Frame
Buffer Manager: Structure BufHashTbl Class Keeps information of mapping (file, pageNo) to frameNo (file, page number) <-> (buffer pool frame) // insert entry into hash table mapping (file, pageNo) to frameNo void insert(const File* file, const int pageNo, const int frameNo); // Check if (file,pageNo) is currently in the buffer pool (ie. in // the hash table. If so, return the corresponding frame number in frameNo. void lookup(const File* file, const int pageNo, int& frameNo); // remove entry obtained by hashing (file,pageNo) from hash table. void remove(const File* file, const int pageNo);
Buffer Manager: Structure BufDesc Class Keep track of the state of each frame in the buffer pool class BufDesc { private: File* file; // pointer to file object PageId pageNo; // page within file FrameId frameNo; // buffer pool frame number int pinCnt; // number of times this page has been pinned bool dirty; // true if dirty; false otherwise bool valid; // true if page is valid bool refbit; // true if this buffer frame been referenced recently void Clear(); // initialize buffer frame void Set(File* filePtr, PageId pageNum); //set BufDesc member variable values void Print() //Print values of member variables BufDesc(); //Constructor
Buffer Manager: Structure BufMgr Class (buffer.cpp) This is where you write your code class BufMgr { private: FrameId clockHand; // clock hand for clock algorithm BufHashTbl *hashTable; // hash table mapping (File, page) to frame number BufDesc *bufDescTable; // BufDesc objects, one per frame std::uint32_t numBufs; // Number of frames in the buffer pool BufStats bufStats; // Statistics about buffer pool usage
Tasks Complete member functions of BufMgr class ~BufMgr(); void advanceClock(); void allocBuf(FrameId& frame); void readPage(File *file, const PageId PageNo, Page*& page); void unPinPage(File *file, const PageID PageNo, const bool dirty) void allocPage(File * file, PageID & PageNo, Page *& Page) void disposePage(File * file, const PageId PageNo) void flushFile(File *file)
Overview of Implementation Methods
Overview of Implementation Methods
Overview of Implementation Methods
Overview of Implementation Methods
Thanks!