Skip to content

Commit

Permalink
1.支持bufferpool clock淘汰算法
Browse files Browse the repository at this point in the history
2.标记SQL解析调用栈
  • Loading branch information
kaori-seasons committed Oct 30, 2021
1 parent c611d5f commit 928234b
Show file tree
Hide file tree
Showing 21 changed files with 390 additions and 167 deletions.
9 changes: 9 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[submodule "deps/googletest"]
path = deps/googletest
url = https://github.com/google/googletest
[submodule "deps/jsoncpp"]
path = deps/jsoncpp
url = https://github.com/open-source-parsers/jsoncpp.git
[submodule "deps/libevent"]
path = deps/libevent
url = https://github.com/libevent/libevent
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 14)
#SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

include_directories(./deps)

project(minidb)

MESSAGE(STATUS "This is SOURCE dir " ${test_SOURCE_DIR})
Expand Down
4 changes: 2 additions & 2 deletions deps/common/conf/ini.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ Ini::~Ini() {}

void Ini::insert_session(const std::string &session_name) {
std::map<std::string, std::string> session_map;
std::pair < std::string, std::map < std::string, std::string >> entry =
std::pair < std::string, std::map < std::string, std::string > > entry =
std::pair < std::string, std::map < std::string, std::string
>> (session_name,
> > (session_name,
session_map);

sections_.insert(entry);
Expand Down
2 changes: 1 addition & 1 deletion deps/common/conf/ini.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class Ini {
int insert_entry(std::map<std::string, std::string> *session_map,
const std::string &line);

typedef std::map<std::string, std::map<std::string, std::string>>
typedef std::map<std::string, std::map<std::string, std::string> >
SessionsMap;

private:
Expand Down
4 changes: 2 additions & 2 deletions deps/common/lang/mutex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace common {
std::map<pthread_mutex_t *, LockTrace::LockID> LockTrace::mLocks;
std::map<pthread_mutex_t *, int> LockTrace::mWaitTimes;
std::map<long long, pthread_mutex_t *> LockTrace::mWaitLocks;
std::map<long long, std::set<pthread_mutex_t *>> LockTrace::mOwnLocks;
std::map<long long, std::set<pthread_mutex_t *> > LockTrace::mOwnLocks;
std::set<pthread_mutex_t *> LockTrace::mEnableRecurisives;

pthread_rwlock_t LockTrace::mMapMutex = PTHREAD_RWLOCK_INITIALIZER;
Expand Down Expand Up @@ -101,7 +101,7 @@ bool LockTrace::deadlockCheck(pthread_mutex_t *mutex, const long long threadId,
}
}

std::map<long long, std::set<pthread_mutex_t *>>::iterator it =
std::map<long long, std::set<pthread_mutex_t *> >::iterator it =
mOwnLocks.find(threadId);
if (it == mOwnLocks.end()) {
return false;
Expand Down
2 changes: 1 addition & 1 deletion deps/common/lang/mutex.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class LockTrace {
static std::map<pthread_mutex_t *, LockID> mLocks;
static std::map<pthread_mutex_t *, int> mWaitTimes;
static std::map<long long, pthread_mutex_t *> mWaitLocks;
static std::map<long long, std::set<pthread_mutex_t *>> mOwnLocks;
static std::map<long long, std::set<pthread_mutex_t *> > mOwnLocks;

static pthread_rwlock_t mMapMutex;
static int mMaxBlockTids;
Expand Down
2 changes: 1 addition & 1 deletion deps/common/seda/event_dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ class EventDispatcher : public Stage {
// implementation state

typedef std::pair<StageEvent *, DispatchContext *> StoredEvent;
typedef std::map<std::string, std::list<StoredEvent>> EventHash;
typedef std::map<std::string, std::list<StoredEvent> > EventHash;

EventHash event_store_; // events stored here while waiting
pthread_mutex_t event_lock_; // protects access to event_store_
Expand Down
1 change: 1 addition & 0 deletions deps/googletest
Submodule googletest added at 16f637
1 change: 1 addition & 0 deletions deps/jsoncpp
Submodule jsoncpp added at 94a622
1 change: 1 addition & 0 deletions deps/libevent
Submodule libevent added at 5df303
2 changes: 1 addition & 1 deletion docs/how_to_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@



2. build libevent
2. build cd libevent

```
Expand Down
12 changes: 6 additions & 6 deletions etc/observer.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ LOG_CONSOLE_LEVEL=1
# the module's log will output whatever level used.
#DefaultLogModules="server.cpp,client.cpp"

[SQLThreads]
# the thread number of this threadpool, 0 means cpu's cores.
# if miss the setting of count, it will use cpu's core number;
count=3
#count=0

# seda's configuration
[SEDA_BASE]
# record every event
Expand All @@ -35,12 +41,6 @@ CLIENT_ADDRESS=INADDR_ANY
MAX_CONNECTION_NUM=8192
PORT=6789

[SQLThreads]
# the thread number of this threadpool, 0 means cpu's cores.
# if miss the setting of count, it will use cpu's core number;
count=3
#count=0

[IOThreads]
# the thread number of this threadpool, 0 means cpu's cores.
# if miss the setting of count, it will use cpu's core number;
Expand Down
2 changes: 1 addition & 1 deletion src/observer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ MESSAGE("MAIN SRC: " ${MAIN_SRC})
FOREACH (F ${ALL_SRC})

IF (NOT ${F} STREQUAL ${MAIN_SRC})
SET(LIB_SRC ${LIB_SRC} ${F})
SET(LIB_SRC ${LIB_SRC} ${F} storage/default/clock_replacer.cpp storage/default/clock_replacer.h storage/default/common.h storage/default/disk_manager.cpp storage/default/disk_manager.h)
ENDIF()

MESSAGE("Use " ${F})
Expand Down
1 change: 1 addition & 0 deletions src/observer/sql/parser/yacc_sql.tab.c
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,7 @@ YYSTYPE yylval YY_INITIAL_VALUE (= yyval_default);
/*--------------------------------------------------------------------.
| yysetstate -- set current state (the top of the stack) to yystate. |
`--------------------------------------------------------------------*/
//维护当前语法解析树的语法栈
yysetstate:
YYDPRINTF ((stderr, "Entering state %d\n", yystate));
YY_ASSERT (0 <= yystate && yystate < YYNSTATES);
Expand Down
59 changes: 59 additions & 0 deletions src/observer/storage/default/clock_replacer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// Created by windwheel on 2021/10/24.
//

//这里因为参考了cmu的讲义 所以实现的不是传统的lru,而是nru
//根据时钟进行淘汰的算法
#include <cstdlib>
#include "clock_replacer.h"

bool ClockReplacer::Victim(Frame *frame) {
latch_.lock();
if (size<=0){
latch_.unlock();
return false;
}

while (true){
if (!in_replacer[clockhand]){
clockhand = (clockhand+1) % buffer_pool_size;
continue;
}

if (!ref_bits_[clockhand]){
clockhand = (clockhand+1) % buffer_pool_size;
continue;
}

frame->file_desc = clockhand;
in_replacer[clockhand] = false;
ref_bits_[clockhand] = false;
size-=1;
break;
}
latch_.unlock();
return true;
}
//对空闲列表中 正在准备替换的page进行状态标记
void ClockReplacer::Pin(Frame *frame) {
latch_.lock();
if (in_replacer[frame->file_desc]){
in_replacer[frame->file_desc] = false;
ref_bits_[frame->file_desc] = false;
size -=1;
}
latch_.unlock();
}

void ClockReplacer::Unpin(Frame *frame) {
latch_.lock();
if (!in_replacer[frame->file_desc]){
in_replacer[frame->file_desc] = true;
ref_bits_[frame->file_desc] = true;
}
}

ClockReplacer::ClockReplacer(ClockReplacer *pReplacer) {

}

49 changes: 49 additions & 0 deletions src/observer/storage/default/clock_replacer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Created by windwheel on 2021/10/24.
//

#ifndef MINIDB_CLOCK_REPLACER_H
#define MINIDB_CLOCK_REPLACER_H


#include <vector>
#include <mutex>
#include "common.h"

class ClockReplacer {

public:
ClockReplacer(PageNum page_nums){
using std::vector;
ref_bits_ = vector<bool>(page_nums, false);
in_replacer = vector<bool>(page_nums, false);
buffer_pool_size = page_nums;
size = 0;
clockhand = 0;

}

ClockReplacer(ClockReplacer *pReplacer);

~ClockReplacer() = default;

public:
std::vector<bool > ref_bits_; //待被替换的page进行标记状态
std::vector<bool> in_replacer;//正在被替换的page
size_t buffer_pool_size; //缓冲池管理器的大小
std::mutex latch_;//维持操作系统空闲列表换页的互斥锁
int size; //维护当前动态遍历的空闲列表大小
int clockhand; //记录当前遍历到的Page 利用同余摸循环遍历空闲列表
Frame *frame;//待分配的新页


public:
bool Victim(Frame *frame); //双指针遍历待替换的freelist列表
void Pin(Frame *frame); //Pin住的页 正在准备被替换
void Unpin(Frame *frame); //已经被淘汰掉的Page
};




#endif //MINIDB_CLOCK_REPLACER_H
63 changes: 63 additions & 0 deletions src/observer/storage/default/common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// Created by windwheel on 2021/10/24.
//

#ifndef MINIDB_COMMON_H
#define MINIDB_COMMON_H
#define BP_INVALID_PAGE_NUM (-1)
#define BP_PAGE_SIZE (1 << 12)
#define BP_FILE_SUB_HDR_SIZE (sizeof(BPFileSubHeader))
#define BP_BUFFER_SIZE 50
#define MAX_OPEN_FILE 1024
#define BP_PAGE_DATA_SIZE (BP_PAGE_SIZE - sizeof(PageNum))


#endif //MINIDB_COMMON_H

typedef int PageNum;



typedef struct{
PageNum page_num;
char data[BP_PAGE_DATA_SIZE];
} Page;

typedef struct{
bool dirty;
unsigned int pin_count;
unsigned long acc_time;
int file_desc;
Page page;
} Frame;


typedef struct {
PageNum page_count;
int allocated_pages;
} BPFileSubHeader;

class BPFileHandle{
public:
BPFileHandle() {
memset(this, 0, sizeof(*this));
}

public:
bool bopen;
const char *file_name;
int file_desc;
Frame *hdr_frame;
Page *hdr_page;
char *bitmap;
BPFileSubHeader *file_sub_header;
} ;

//因为在操作系统中页和页表项都会有落盘的页表ID长度,所以需要做数据类型的适配

using frame_id_t = int32_t;
using page_id = int32_t;
using old_t = uint16_t;



Loading

0 comments on commit 928234b

Please sign in to comment.