├── .gitignore ├── CMakeLists.txt ├── COPYING ├── README.md ├── deps ├── CMakeLists.txt └── common │ ├── CMakeLists.txt │ ├── conf │ ├── ini.cpp │ └── ini.h │ ├── defs.h │ ├── io │ ├── io.cpp │ ├── io.h │ ├── roll_select_dir.cpp │ ├── roll_select_dir.h │ └── select_dir.h │ ├── lang │ ├── bitmap.cpp │ ├── bitmap.h │ ├── mutex.cpp │ ├── mutex.h │ ├── serializable.h │ ├── string.cpp │ └── string.h │ ├── log │ ├── log.cpp │ └── log.h │ ├── math │ ├── md5.cpp │ ├── md5.h │ ├── random_generator.cpp │ ├── random_generator.h │ ├── regex.cpp │ └── regex.h │ ├── metrics │ ├── console_reporter.cpp │ ├── console_reporter.h │ ├── histogram_snapshot.cpp │ ├── histogram_snapshot.h │ ├── log_reporter.cpp │ ├── log_reporter.h │ ├── metric.h │ ├── metrics.cpp │ ├── metrics.h │ ├── metrics_registry.cpp │ ├── metrics_registry.h │ ├── reporter.cpp │ ├── reporter.h │ ├── reservoir.cpp │ ├── reservoir.h │ ├── sampler.cpp │ ├── sampler.h │ ├── snapshot.h │ ├── timer_snapshot.cpp │ ├── timer_snapshot.h │ ├── uniform_reservoir.cpp │ └── uniform_reservoir.h │ ├── mm │ ├── debug_new.cpp.skip │ ├── debug_new.h │ ├── mem.cpp.skip │ ├── mem.h │ ├── mem_pool.cpp │ └── mem_pool.h │ ├── os │ ├── os.cpp │ ├── os.h │ ├── path.cpp │ ├── path.h │ ├── pidfile.cpp │ ├── pidfile.h │ ├── process.cpp │ ├── process.h │ ├── process_param.cpp │ ├── process_param.h │ ├── signal.cpp │ └── signal.h │ ├── seda │ ├── callback.cpp │ ├── callback.h │ ├── class_factory.h │ ├── event_dispatcher.cpp │ ├── event_dispatcher.h │ ├── example_stage.cpp │ ├── example_stage.h │ ├── init.cpp │ ├── init.h │ ├── kill_thread.cpp │ ├── kill_thread.h │ ├── metrics_report_event.cpp │ ├── metrics_report_event.h │ ├── metrics_stage.cpp │ ├── metrics_stage.h │ ├── seda_config.cpp │ ├── seda_config.h │ ├── seda_defs.h │ ├── stage.cpp │ ├── stage.h │ ├── stage_event.cpp │ ├── stage_event.h │ ├── stage_factory.h │ ├── thread_pool.cpp │ ├── thread_pool.h │ ├── timer_stage.cpp │ └── timer_stage.h │ ├── time │ ├── datetime.cpp │ ├── datetime.h │ ├── timeout_info.cpp │ └── timeout_info.h │ └── version.h ├── docs └── how_to_build.md ├── etc └── observer.ini ├── main.cpp ├── miniob └── db │ └── sys │ ├── table1.data │ ├── table1.table │ ├── table2.data │ ├── table2.table │ ├── table3.data │ ├── table3.table │ ├── table4.data │ ├── table4.table │ ├── table5.data │ └── table5.table ├── src ├── CMakeLists.txt ├── obclient │ ├── CMakeLists.txt │ └── client.cpp └── observer │ ├── CMakeLists.txt │ ├── event │ ├── execution_plan_event.cpp │ ├── execution_plan_event.h │ ├── session_event.cpp │ ├── session_event.h │ ├── sql_event.cpp │ ├── sql_event.h │ ├── storage_event.cpp │ └── storage_event.h │ ├── handler │ └── handler.h │ ├── ini_setting.h │ ├── init.cpp │ ├── init.h │ ├── main.cpp │ ├── net │ ├── connection_context.h │ ├── server.cpp │ ├── server.h │ └── server_param.h │ ├── rc.cpp │ ├── rc.h │ ├── session │ ├── session.cpp │ ├── session.h │ ├── session_stage.cpp │ └── session_stage.h │ ├── sql │ ├── executor │ │ ├── execute_stage.cpp │ │ ├── execute_stage.h │ │ ├── execution_node.cpp │ │ ├── execution_node.h │ │ ├── tuple.cpp │ │ ├── tuple.h │ │ ├── value.cpp │ │ └── value.h │ ├── optimizer │ │ ├── optimize_stage.cpp │ │ └── optimize_stage.h │ ├── parser │ │ ├── lex.yy.c │ │ ├── lex.yy.h │ │ ├── lex_sql.l │ │ ├── parse.cpp │ │ ├── parse.h │ │ ├── parse_defs.h │ │ ├── parse_stage.cpp │ │ ├── parse_stage.h │ │ ├── resolve_stage.cpp │ │ ├── resolve_stage.h │ │ ├── yacc_sql.tab.c │ │ ├── yacc_sql.tab.h │ │ └── yacc_sql.y │ ├── plan_cache │ │ ├── plan_cache_stage.cpp │ │ └── plan_cache_stage.h │ └── query_cache │ │ ├── query_cache_stage.cpp │ │ └── query_cache_stage.h │ └── storage │ ├── common │ ├── bplus_tree.cpp │ ├── bplus_tree.h │ ├── bplus_tree_index.cpp │ ├── bplus_tree_index.h │ ├── condition_filter.cpp │ ├── condition_filter.h │ ├── db.cpp │ ├── db.h │ ├── field_meta.cpp │ ├── field_meta.h │ ├── index.cpp │ ├── index.h │ ├── index_meta.cpp │ ├── index_meta.h │ ├── meta_util.cpp │ ├── meta_util.h │ ├── record_manager.cpp │ ├── record_manager.h │ ├── table.cpp │ ├── table.h │ ├── table_meta.cpp │ └── table_meta.h │ ├── default │ ├── default_handler.cpp │ ├── default_handler.h │ ├── default_storage_stage.cpp │ ├── default_storage_stage.h │ ├── disk_buffer_pool.cpp │ └── disk_buffer_pool.h │ ├── mem │ ├── mem_storage_stage.cpp │ └── mem_storage_stage.h │ └── trx │ ├── trx.cpp │ └── trx.h ├── test ├── CMakeLists.txt └── client_performance_test.cpp └── unitest ├── CMakeLists.txt ├── bitmap_test.cpp ├── bp_manager_test.cpp ├── bplus_tree_test.cpp ├── log_test.cpp ├── log_test.h ├── md5_test.cpp ├── md5_test.h ├── mem_pool_test.cpp ├── path_test.cpp ├── pidfile_test.cpp ├── rc_test.cpp └── thread_test.h /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | cmake-build-debug/ 3 | observer.log.* 4 | deps/googletest/ 5 | deps/jsoncpp/ 6 | deps/libevent/ 7 | .idea/ 8 | .vscode/ 9 | .vscode-server/ -------------------------------------------------------------------------------- /deps/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | ADD_SUBDIRECTORY(common) 4 | -------------------------------------------------------------------------------- /deps/common/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | PROJECT(common) 3 | MESSAGE("Begin to build " ${PROJECT_NAME}) 4 | MESSAGE(STATUS "This is PROJECT_BINARY_DIR dir " ${PROJECT_BINARY_DIR}) 5 | MESSAGE(STATUS "This is PROJECT_SOURCE_DIR dir " ${PROJECT_SOURCE_DIR}) 6 | 7 | INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/..) 8 | #find_file() 只会找一个文件 9 | #AUX_SOURCE_DIRECTORY(. SRC_LIST) 10 | #FOREACH(F ${SRC_LIST}) 11 | # MESSAGE(${F}) 12 | #ENDFOREACH(F) 13 | 14 | FILE(GLOB_RECURSE ALL_SRC *.cpp) 15 | FOREACH(F ${ALL_SRC}) 16 | SET(SRC_LIST ${SRC_LIST} ${F}) 17 | MESSAGE("Use " ${F}) 18 | ENDFOREACH(F) 19 | 20 | 21 | #SHARED,动态库 22 | #STATIC,静态库 23 | ADD_LIBRARY(common SHARED ${ALL_SRC} ) 24 | ADD_LIBRARY(common_static STATIC ${ALL_SRC}) 25 | 26 | # 没有SET_TARGET_PROPERTIES, 是不能同时build 静态库和动态库, 如果只build 一种库, 就不需要这个功能 27 | SET_TARGET_PROPERTIES(common_static PROPERTIES OUTPUT_NAME "common") 28 | GET_TARGET_PROPERTY(OUTPUT_VALUE common_static OUTPUT_NAME) 29 | MESSAGE(STATUS "This is the func_static OUTPUT_NAME:" ${OUTPUT_VALUE}) 30 | 31 | # 编译静态库时,自动会把同名的动态库给删除, 因此需要临时设置一下 32 | SET_TARGET_PROPERTIES(common PROPERTIES CLEAN_DIRECT_OUTPUT 1) 33 | SET_TARGET_PROPERTIES(common_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) 34 | 35 | # 设置版本号 VERSION指代动态库版本,SOVERSION指代API版本 36 | 37 | 38 | SET(MAIJOR_VER 1) 39 | SET(MINOR_VER 0) 40 | SET(PATCH_VER 0) 41 | SET(OTHER_VER 1) 42 | ADD_DEFINITIONS(-DMAIJOR_VER=${MAIJOR_VER} -DMINOR_VER=${MINOR_VER} -DPATCH_VER=${PATCH_VER} -DOTHER_VER=${OTHER_VER}) 43 | 44 | SET_TARGET_PROPERTIES(common PROPERTIES VERSION ${MAIJOR_VER}.${MINOR_VER}.${PATCH_VER} SOVERSION ${MAIJOR_VER}) 45 | 46 | SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../../lib) 47 | # Target 必须在定义 ADD_EXECUTABLE 之后, programs 不受这个限制 48 | # TARGETS和PROGRAMS 的默认权限是OWNER_EXECUTE, GROUP_EXECUTE, 和WORLD_EXECUTE,即755权限, programs 都是处理脚步类 49 | # 类型分为RUNTIME/LIBRARY/ARCHIVE, prog 50 | INSTALL(TARGETS common common_static 51 | LIBRARY DESTINATION lib 52 | ARCHIVE DESTINATION lib) 53 | 54 | # 小心 安装的时候,不要变成include/func.h, 如果这样,第二次安装的时候会变成include/func.h/func.h 55 | # INSTALL(FILES func.h DESTINATION include/) 56 | 57 | FILE(GLOB_RECURSE ALL_HEADER *.h) 58 | FOREACH(F ${ALL_HEADER}) 59 | file(RELATIVE_PATH RELAPATH_HEADER ${PROJECT_SOURCE_DIR} ${F}) # 获取相对路径 60 | get_filename_component(headDir ${RELAPATH_HEADER} DIRECTORY) 61 | MESSAGE("Install " ${RELAPATH_HEADER} " to " ${CMAKE_INSTALL_PREFIX} "/" ${PROJECT_NAME} "/include/" ${headDir}) 62 | INSTALL(FILES ${RELAPATH_HEADER} DESTINATION include/${headDir}) 63 | ENDFOREACH(F) -------------------------------------------------------------------------------- /deps/common/defs.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #ifndef __COMMON_DEFS_H__ 16 | #define __COMMON_DEFS_H__ 17 | 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include "common/version.h" 28 | namespace common { 29 | 30 | #ifndef gettid 31 | #if defined(__MACH__) 32 | #define gettid() ((long long)pthread_self()) 33 | #elif defined(LINUX) 34 | #define gettid() ((long long)pthread_self()) 35 | #endif 36 | 37 | #endif 38 | 39 | inline const std::string &theSwVersion() 40 | { 41 | static const std::string swVersion(VERSION_STR); 42 | 43 | return swVersion; 44 | } 45 | 46 | enum { 47 | // General Error Codes 48 | STATUS_SUCCESS = 0, //!< Success status should be zero, 49 | STATUS_INVALID_PARAM, //!< Invalid parameter 50 | STATUS_FAILED_INIT, //!< Failed to init program 51 | STATUS_PROPERTY_ERR, //!< Property error 52 | STATUS_INIT_LOG, //!< log error 53 | STATUS_INIT_THREAD, //!< failed to init thread 54 | STATUS_FAILED_JOB, //!< Failed to do job 55 | STATUS_FAILED_NETWORK, //!< Network failure 56 | 57 | STATUS_UNKNOW_ERROR, 58 | STATUS_LAST_ERR //!< last error code 59 | 60 | }; 61 | 62 | const unsigned int ONE_KILO = 1024; 63 | const unsigned int ONE_MILLION = ONE_KILO * ONE_KILO; 64 | const unsigned int ONE_GIGA = ONE_MILLION * ONE_KILO; 65 | const unsigned int FILENAME_LENGTH_MAX = 256; // the max filename length 66 | 67 | static const char FILE_PATH_SPLIT = '/'; 68 | static const char FILE_PATH_SPLIT_STR[] = "/"; 69 | 70 | /* 71 | * Define types 72 | * 73 | */ 74 | typedef unsigned char u8_t; 75 | typedef unsigned short u16_t; 76 | typedef unsigned int u32_t; 77 | typedef unsigned long long u64_t; 78 | 79 | typedef char s8_t; 80 | typedef short s16_t; 81 | typedef int s32_t; 82 | typedef long long s64_t; 83 | 84 | #define LOCAL_HOST "localhost" 85 | 86 | } // namespace common 87 | #endif //__COMMON_DEFS_H__ 88 | -------------------------------------------------------------------------------- /deps/common/io/io.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #ifndef __COMMON_IO_IO_H__ 16 | #define __COMMON_IO_IO_H__ 17 | 18 | #include 19 | #include 20 | 21 | #include "common/defs.h" 22 | 23 | namespace common { 24 | 25 | /** 26 | * read data from file fileName, store data to data 27 | * if success, store file continent to data 28 | * if fail, return -1 and don't change data 29 | */ 30 | int readFromFile(const std::string &fileName, char *&data, size_t &fileSize); 31 | 32 | int writeToFile(const std::string &fileName, const char *data, u32_t dataSize, const char *openMode); 33 | 34 | /** 35 | * return the line number which line.strip() isn't empty 36 | */ 37 | int getFileLines(const std::string &fileName, u64_t &lineNum); 38 | 39 | /** Get file list from the dir 40 | * don't care ".", "..", ".****" hidden files 41 | * just count regular files, don't care directory 42 | * @param[out] fileList file List 43 | * @param[in] path the search path 44 | * @param[in] pattern regex string, if not empty, the file should match 45 | * list 46 | * @param[in] resursion if this has been set, it will search subdirs 47 | * @return 0 if success, error code otherwise 48 | */ 49 | int getFileList( 50 | std::vector &fileList, const std::string &path, const std::string &pattern, bool recursive); 51 | int getFileNum(u64_t &fileNum, const std::string &path, const std::string &pattern, bool recursive); 52 | int getDirList(std::vector &dirList, const std::string &path, const std::string &pattern); 53 | 54 | int touch(const std::string &fileName); 55 | 56 | /** 57 | * get file size 58 | */ 59 | int getFileSize(const char *filePath, u64_t &fileLen); 60 | 61 | } // namespace common 62 | #endif /* __COMMON_IO_IO_H__ */ 63 | -------------------------------------------------------------------------------- /deps/common/io/roll_select_dir.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #include "common/io/roll_select_dir.h" 16 | #include "common/io/io.h" 17 | #include "common/log/log.h" 18 | namespace common { 19 | 20 | void RollSelectDir::setBaseDir(std::string baseDir) 21 | { 22 | mBaseDir = baseDir; 23 | 24 | std::vector dirList; 25 | int rc = getDirList(dirList, mBaseDir, ""); 26 | if (rc) { 27 | LOG_ERROR("Failed to all subdir entry"); 28 | } 29 | 30 | if (dirList.size() == 0) { 31 | MUTEX_LOCK(&mMutex); 32 | 33 | mSubdirs.clear(); 34 | mSubdirs.push_back(mBaseDir); 35 | mPos = 0; 36 | MUTEX_UNLOCK(&mMutex); 37 | 38 | return; 39 | } 40 | 41 | MUTEX_LOCK(&mMutex); 42 | mSubdirs = dirList; 43 | mPos = 0; 44 | MUTEX_UNLOCK(&mMutex); 45 | return; 46 | } 47 | 48 | std::string RollSelectDir::select() 49 | { 50 | std::string ret; 51 | 52 | MUTEX_LOCK(&mMutex); 53 | ret = mSubdirs[mPos % mSubdirs.size()]; 54 | mPos++; 55 | MUTEX_UNLOCK(&mMutex); 56 | 57 | return ret; 58 | } 59 | 60 | } // namespace common -------------------------------------------------------------------------------- /deps/common/io/roll_select_dir.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #ifndef __COMMON_IO_ROLL_SELECT_DIR__ 16 | #define __COMMON_IO_ROLL_SELECT_DIR__ 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #include "common/defs.h" 23 | #include "common/io/select_dir.h" 24 | #include "common/lang/mutex.h" 25 | namespace common { 26 | 27 | class RollSelectDir : public SelectDir { 28 | public: 29 | RollSelectDir() 30 | { 31 | MUTEX_INIT(&mMutex, NULL); 32 | } 33 | ~RollSelectDir() 34 | { 35 | MUTEX_DESTROY(&mMutex); 36 | } 37 | 38 | public: 39 | /** 40 | * inherit from CSelectDir 41 | */ 42 | std::string select(); 43 | void setBaseDir(std::string baseDir); 44 | 45 | public: 46 | std::string mBaseDir; 47 | std::vector mSubdirs; 48 | pthread_mutex_t mMutex; 49 | u32_t mPos; 50 | }; 51 | 52 | } // namespace common 53 | #endif /* __COMMON_IO_ROLL_SELECT_DIR__ */ 54 | -------------------------------------------------------------------------------- /deps/common/io/select_dir.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #ifndef __COMMON_IO_SELECT_DIR_H__ 16 | #define __COMMON_IO_SELECT_DIR_H__ 17 | 18 | #include 19 | namespace common { 20 | 21 | class SelectDir { 22 | public: 23 | virtual std::string select() 24 | { 25 | return std::string(""); 26 | }; 27 | virtual void setBaseDir(std::string baseDir){}; 28 | }; 29 | 30 | } // namespace common 31 | #endif /* __COMMON_IO_SELECT_DIR_H__ */ 32 | -------------------------------------------------------------------------------- /deps/common/lang/bitmap.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by wangyunlai on 2021/5/7. 13 | // 14 | 15 | #include "common/lang/bitmap.h" 16 | 17 | namespace common { 18 | 19 | int find_first_zero(char byte, int start) 20 | { 21 | for (int i = start; i < 8; i++) { 22 | if ((byte & (1 << i)) == 0) { 23 | return i; 24 | } 25 | } 26 | return -1; 27 | } 28 | 29 | int find_first_setted(char byte, int start) 30 | { 31 | for (int i = start; i < 8; i++) { 32 | if ((byte & (1 << i)) != 0) { 33 | return i; 34 | } 35 | } 36 | return -1; 37 | } 38 | 39 | Bitmap::Bitmap(char *bitmap, int size) : bitmap_(bitmap), size_(size) 40 | {} 41 | 42 | bool Bitmap::get_bit(int index) 43 | { 44 | char bits = bitmap_[index / 8]; 45 | return (bits & (1 << (index % 8))) != 0; 46 | } 47 | 48 | void Bitmap::set_bit(int index) 49 | { 50 | char &bits = bitmap_[index / 8]; 51 | bits |= (1 << (index % 8)); 52 | } 53 | 54 | void Bitmap::clear_bit(int index) 55 | { 56 | char &bits = bitmap_[index / 8]; 57 | bits &= ~(1 << (index % 8)); 58 | } 59 | 60 | int Bitmap::next_unsetted_bit(int start) 61 | { 62 | int ret = -1; 63 | int start_in_byte = start % 8; 64 | for (int iter = start / 8, end = (size_ % 8 == 0 ? size_ / 8 : size_ / 8 + 1); iter <= end; iter++) { 65 | char byte = bitmap_[iter]; 66 | if (byte != -1) { 67 | int index_in_byte = find_first_zero(byte, start_in_byte); 68 | if (index_in_byte >= 0) { 69 | ret = iter * 8 + index_in_byte; 70 | break; 71 | } 72 | 73 | start_in_byte = 0; 74 | } 75 | } 76 | 77 | if (ret >= size_) { 78 | ret = -1; 79 | } 80 | return ret; 81 | } 82 | 83 | int Bitmap::next_setted_bit(int start) 84 | { 85 | int ret = -1; 86 | int start_in_byte = start % 8; 87 | for (int iter = start / 8, end = (size_ % 8 == 0 ? size_ / 8 : size_ / 8 + 1); iter <= end; iter++) { 88 | char byte = bitmap_[iter]; 89 | if (byte != 0x00) { 90 | int index_in_byte = find_first_setted(byte, start_in_byte); 91 | if (index_in_byte >= 0) { 92 | ret = iter * 8 + index_in_byte; 93 | break; 94 | } 95 | 96 | start_in_byte = 0; 97 | } 98 | } 99 | 100 | if (ret >= size_) { 101 | ret = -1; 102 | } 103 | return ret; 104 | } 105 | 106 | } // namespace common -------------------------------------------------------------------------------- /deps/common/lang/bitmap.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by wangyunlai on 2021/5/7. 13 | // 14 | 15 | #ifndef __COMMON_LANG_BITMAP_H__ 16 | #define __COMMON_LANG_BITMAP_H__ 17 | 18 | namespace common { 19 | 20 | class Bitmap { 21 | public: 22 | Bitmap(char *bitmap, int size); 23 | 24 | bool get_bit(int index); 25 | void set_bit(int index); 26 | void clear_bit(int index); 27 | 28 | int next_unsetted_bit(int start); 29 | int next_setted_bit(int start); 30 | 31 | private: 32 | char *bitmap_; 33 | int size_; 34 | }; 35 | 36 | } // namespace common 37 | 38 | #endif // __COMMON_LANG_BITMAP_H__ -------------------------------------------------------------------------------- /deps/common/lang/serializable.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #ifndef __COMMON_LANG_SERIALIZABLE_H__ 16 | #define __COMMON_LANG_SERIALIZABLE_H__ 17 | 18 | #include 19 | namespace common { 20 | 21 | /** 22 | * Through this type to determine object type 23 | */ 24 | enum { MESSAGE_BASIC = 100, MESSAGE_BASIC_REQUEST = 1000, MESSAGE_BASIC_RESPONSE = -1000 }; 25 | 26 | class Deserializable { 27 | public: 28 | /* 29 | * deserialize buffer to one object 30 | * @param[in]buffer, buffer to store the object serialized bytes 31 | * @return * object 32 | */ 33 | virtual void *deserialize(const char *buffer, int bufLen) = 0; 34 | }; 35 | 36 | class Serializable { 37 | public: 38 | /* 39 | * serialize this object to bytes 40 | * @param[in] buffer, buffer to store the object serialized bytes, 41 | * please make sure the buffer is enough 42 | * @param[in] bufferLen, buffer length 43 | * @return, used buffer length -- success, -1 means failed 44 | */ 45 | virtual int serialize(std::ostream &os) const = 0; 46 | 47 | /* 48 | * deserialize bytes to this object 49 | * @param[in] buffer buffer to store the object serialized bytes 50 | * @param[in] bufferLen buffer lenght 51 | * @return used buffer length -- success , -1 --failed 52 | */ 53 | virtual int deserialize(std::istream &is) = 0; 54 | 55 | /** 56 | * get serialize size 57 | * @return >0 -- success, -1 --failed 58 | */ 59 | virtual int get_serial_size() const = 0; 60 | 61 | /** 62 | * this function will generalize one output string 63 | */ 64 | virtual void to_string(std::string &output) const = 0; 65 | }; 66 | 67 | } // namespace common 68 | #endif /* __COMMON_LANG_SERIALIZABLE_H__ */ 69 | -------------------------------------------------------------------------------- /deps/common/math/md5.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #ifndef __COMMON_MATH_MD5_H__ 16 | #define __COMMON_MATH_MD5_H__ 17 | #include 18 | namespace common { 19 | 20 | typedef unsigned char *POINTER; 21 | typedef unsigned short int UINT2; 22 | typedef unsigned int UINT4; 23 | 24 | typedef struct { 25 | UINT4 state[4]; /* state (ABCD) */ 26 | UINT4 count[2]; /* number of bits, modulo 2^64 (lsb first) */ 27 | unsigned char buffer[64]; /* input buffer */ 28 | } MD5_CTX; 29 | 30 | #ifdef __cplusplus 31 | extern "C" { 32 | #endif 33 | 34 | /** md5 for string 35 | * parameters: 36 | * string: the string to md5 37 | * digest: store the md5 digest 38 | * return: 0 for success, != 0 fail 39 | */ 40 | int MD5String(char *string, unsigned char digest[16]); 41 | 42 | /** md5 for file 43 | * parameters: 44 | * filename: the filename whose content to md5 45 | * digest: store the md5 digest 46 | * return: 0 for success, != 0 fail 47 | */ 48 | int MD5File(char *filename, unsigned char digest[16]); 49 | 50 | /** md5 for buffer 51 | * parameters: 52 | * buffer: the buffer to md5 53 | * len: the buffer length 54 | * digest: store the md5 digest 55 | * return: 0 for success, != 0 fail 56 | */ 57 | int MD5Buffer(char *buffer, unsigned int len, unsigned char digest[16]); 58 | 59 | void MD5Init(MD5_CTX *); 60 | 61 | void MD5Update(MD5_CTX *, unsigned char *, unsigned int); 62 | 63 | void MD5Final(unsigned char[16], MD5_CTX *); 64 | 65 | #ifdef __cplusplus 66 | } 67 | #endif 68 | 69 | } // namespace common 70 | #endif //__COMMON_MATH_MD5_H__ 71 | -------------------------------------------------------------------------------- /deps/common/math/random_generator.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | 15 | #include 16 | 17 | #include "common/math/random_generator.h" 18 | 19 | namespace common { 20 | 21 | RandomGenerator::RandomGenerator() : randomData(std::chrono::system_clock::now().time_since_epoch().count()) 22 | {} 23 | 24 | RandomGenerator::~RandomGenerator() 25 | {} 26 | 27 | unsigned int RandomGenerator::next() 28 | { 29 | 30 | return randomData(); 31 | } 32 | 33 | unsigned int RandomGenerator::next(unsigned int range) 34 | { 35 | if (range > 0) { 36 | return next() % range; 37 | } else { 38 | return 0; 39 | } 40 | } 41 | 42 | } // namespace common -------------------------------------------------------------------------------- /deps/common/math/random_generator.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | #ifndef __COMMON_MATH_RANDOM_GENERATOR_H_ 15 | #define __COMMON_MATH_RANDOM_GENERATOR_H_ 16 | 17 | #include 18 | #include 19 | namespace common { 20 | 21 | #define DEFAULT_RANDOM_BUFF_SIZE 512 22 | 23 | class RandomGenerator { 24 | 25 | public: 26 | RandomGenerator(); 27 | virtual ~RandomGenerator(); 28 | 29 | public: 30 | unsigned int next(); 31 | unsigned int next(unsigned int range); 32 | 33 | private: 34 | // The GUN Extended TLS Version 35 | std::mt19937 randomData; 36 | }; 37 | 38 | } // namespace common 39 | 40 | #endif /* __COMMON_MATH_RANDOM_GENERATOR_H_ */ 41 | -------------------------------------------------------------------------------- /deps/common/math/regex.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | #include "common/math/regex.h" 20 | namespace common { 21 | 22 | int regex_match(const char *str_, const char *pat_) 23 | { 24 | regex_t reg; 25 | if (regcomp(®, pat_, REG_EXTENDED | REG_NOSUB)) 26 | return -1; 27 | 28 | int ret = regexec(®, str_, 0, NULL, 0); 29 | regfree(®); 30 | return ret; 31 | } 32 | 33 | } // namespace common -------------------------------------------------------------------------------- /deps/common/math/regex.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #ifndef __COMMON_MATH_REGEX_H__ 16 | #define __COMMON_MATH_REGEX_H__ 17 | namespace common { 18 | 19 | int regex_match(const char *str_, const char *pat_); 20 | 21 | } // namespace common 22 | #endif /* __COMMON_MATH_REGEX_H__ */ 23 | -------------------------------------------------------------------------------- /deps/common/metrics/console_reporter.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | 15 | #include "common/metrics/console_reporter.h" 16 | 17 | #include 18 | #include 19 | 20 | #include "common/metrics/metric.h" 21 | 22 | namespace common { 23 | 24 | ConsoleReporter *get_console_reporter() 25 | { 26 | static ConsoleReporter *instance = new ConsoleReporter(); 27 | 28 | return instance; 29 | } 30 | 31 | void ConsoleReporter::report(const std::string &tag, Metric *metric) 32 | { 33 | Snapshot *snapshot = metric->get_snapshot(); 34 | 35 | if (snapshot != NULL) { 36 | printf("%s:%s\n", tag.c_str(), snapshot->to_string().c_str()); 37 | } else { 38 | printf("There is no snapshot of %s metrics.", tag.c_str()); 39 | } 40 | } 41 | 42 | } // namespace common -------------------------------------------------------------------------------- /deps/common/metrics/console_reporter.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | 15 | #ifndef __COMMON_METRICS_CONSOLE_REPORTER_H__ 16 | #define __COMMON_METRICS_CONSOLE_REPORTER_H__ 17 | 18 | #include "common/metrics/reporter.h" 19 | 20 | namespace common { 21 | 22 | class ConsoleReporter : public Reporter { 23 | public: 24 | void report(const std::string &tag, Metric *metric); 25 | }; 26 | 27 | ConsoleReporter *get_console_reporter(); 28 | } // namespace common 29 | #endif //__COMMON_METRICS_CONSOLE_REPORTER_H__ 30 | -------------------------------------------------------------------------------- /deps/common/metrics/histogram_snapshot.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | 15 | #ifndef __COMMON_METRICS_HISTOGRAM_SNAPSHOT_H_ 16 | #define __COMMON_METRICS_HISTOGRAM_SNAPSHOT_H_ 17 | 18 | #include 19 | #include 20 | 21 | #include 22 | #include 23 | 24 | #include "common/metrics/snapshot.h" 25 | 26 | namespace common { 27 | 28 | class HistogramSnapShot : public Snapshot { 29 | public: 30 | HistogramSnapShot(); 31 | explicit HistogramSnapShot(const std::vector &collection); 32 | virtual ~HistogramSnapShot(); 33 | 34 | public: 35 | void set_collection(const std::vector &collection); 36 | 37 | /** 38 | * Returns the value at the given quantile 39 | * 40 | * @param quantile a given quantile, in {@code [0..1]} 41 | * @return the value in the distribute 42 | */ 43 | double get_value(double quantile); 44 | 45 | /** 46 | * Returns the size of collection in the snapshot 47 | */ 48 | size_t size() const; 49 | 50 | /** 51 | * Returns 50th_percentile. 52 | */ 53 | double get_median(); 54 | 55 | double get_75th(); 56 | double get_90th(); 57 | double get_95th(); 58 | double get_99th(); 59 | double get_999th(); 60 | 61 | double get_max(); 62 | double get_min(); 63 | double get_mean(); 64 | 65 | const std::vector &get_values(); 66 | 67 | std::string to_string(); 68 | 69 | protected: 70 | std::vector data_; 71 | }; 72 | 73 | } // namespace common 74 | 75 | #endif /* __COMMON_METRICS_HISTOGRAM_SNAPSHOT_H_ */ 76 | -------------------------------------------------------------------------------- /deps/common/metrics/log_reporter.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | 15 | #include "common/metrics/log_reporter.h" 16 | 17 | #include 18 | 19 | #include "common/metrics/metric.h" 20 | #include "common/log/log.h" 21 | 22 | namespace common { 23 | 24 | LogReporter *get_log_reporter() 25 | { 26 | static LogReporter *instance = new LogReporter(); 27 | 28 | return instance; 29 | } 30 | 31 | void LogReporter::report(const std::string &tag, Metric *metric) 32 | { 33 | Snapshot *snapshot = metric->get_snapshot(); 34 | 35 | if (snapshot != NULL) { 36 | LOG_INFO("%s:%s", tag.c_str(), snapshot->to_string().c_str()); 37 | } else { 38 | LOG_WARN("There is no snapshot of %s metrics.", tag.c_str()); 39 | } 40 | } 41 | 42 | } // namespace common -------------------------------------------------------------------------------- /deps/common/metrics/log_reporter.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | 15 | #ifndef __COMMON_METRICS_LOG_REPORTER_H__ 16 | #define __COMMON_METRICS_LOG_REPORTER_H__ 17 | 18 | #include "common/metrics/reporter.h" 19 | 20 | namespace common { 21 | 22 | class LogReporter : public Reporter { 23 | public: 24 | void report(const std::string &tag, Metric *metric); 25 | }; 26 | 27 | LogReporter *get_log_reporter(); 28 | } // namespace common 29 | #endif //__COMMON_METRICS_LOG_REPORTER_H__ 30 | -------------------------------------------------------------------------------- /deps/common/metrics/metric.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | 15 | #ifndef __COMMON_METRICS_METRIC_H__ 16 | #define __COMMON_METRICS_METRIC_H__ 17 | 18 | #include "common/metrics/snapshot.h" 19 | 20 | namespace common { 21 | 22 | class Metric { 23 | public: 24 | virtual void snapshot() = 0; 25 | 26 | virtual Snapshot *get_snapshot() 27 | { 28 | return snapshot_value_; 29 | } 30 | 31 | protected: 32 | Snapshot *snapshot_value_; 33 | }; 34 | 35 | } // namespace common 36 | #endif //__COMMON_METRICS_METRIC_H__ 37 | -------------------------------------------------------------------------------- /deps/common/metrics/metrics.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/19. 13 | // 14 | 15 | #ifndef __COMMON_METRICS_METRICS_H__ 16 | #define __COMMON_METRICS_METRICS_H__ 17 | 18 | #include "common/lang/string.h" 19 | #include "common/metrics/metric.h" 20 | #include "common/metrics/snapshot.h" 21 | #include "common/metrics/timer_snapshot.h" 22 | #include "common/metrics/uniform_reservoir.h" 23 | #include 24 | 25 | namespace common { 26 | 27 | class Gauge : public Metric { 28 | public: 29 | // user implement snapshot function 30 | void set_snapshot(Snapshot *value) 31 | { 32 | snapshot_value_ = value; 33 | } 34 | }; 35 | 36 | class Counter : public Metric { 37 | void set_snapshot(SnapshotBasic *value) 38 | { 39 | snapshot_value_ = value; 40 | } 41 | }; 42 | 43 | class Meter : public Metric { 44 | public: 45 | Meter(); 46 | virtual ~Meter(); 47 | 48 | void inc(long increase); 49 | void inc(); 50 | 51 | void snapshot(); 52 | 53 | protected: 54 | std::atomic value_; 55 | long snapshot_tick_; 56 | }; 57 | 58 | // SimpleTimer just get tps and meanvalue 59 | // time unit is ms 60 | class SimpleTimer : public Meter { 61 | public: 62 | virtual ~SimpleTimer(); 63 | 64 | void inc(long increase); 65 | 66 | void update(long one); 67 | 68 | void snapshot(); 69 | 70 | protected: 71 | std::atomic times_; 72 | }; 73 | 74 | // Histogram metric is complicated, in normal case , 75 | // please skip us histogram or Timer as more as possible 76 | // try use SimpleTimer to replace them. 77 | // if use histogram , please use sampling method. 78 | class Histogram : public UniformReservoir { 79 | public: 80 | Histogram(RandomGenerator &random); 81 | Histogram(RandomGenerator &random, size_t size); 82 | virtual ~Histogram(); 83 | 84 | void snapshot(); 85 | }; 86 | 87 | // timeunit is ms 88 | // Timer = Histogram + Meter 89 | class Timer : public UniformReservoir { 90 | public: 91 | Timer(RandomGenerator &random); 92 | Timer(RandomGenerator &random, size_t size); 93 | virtual ~Timer(); 94 | 95 | void snapshot(); 96 | void update(double ms); 97 | 98 | protected: 99 | std::atomic value_; 100 | long snapshot_tick_; 101 | }; 102 | // update ms 103 | class TimerStat { 104 | public: 105 | TimerStat(SimpleTimer &st_); 106 | 107 | ~TimerStat(); 108 | void start(); 109 | void end(); 110 | 111 | public: 112 | SimpleTimer &st_; 113 | long start_tick_; 114 | long end_tick_; 115 | }; 116 | 117 | } // namespace common 118 | #endif //__COMMON_METRICS_METRICS_H__ 119 | -------------------------------------------------------------------------------- /deps/common/metrics/metrics_registry.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | 15 | #include "common/metrics/metrics_registry.h" 16 | #include "common/log/log.h" 17 | 18 | namespace common { 19 | 20 | MetricsRegistry &get_metrics_registry() 21 | { 22 | static MetricsRegistry instance; 23 | 24 | return instance; 25 | } 26 | 27 | void MetricsRegistry::register_metric(const std::string &tag, Metric *metric) 28 | { 29 | std::map::iterator it = metrics.find(tag); 30 | if (it != metrics.end()) { 31 | LOG_WARN("%s has been registered!", tag.c_str()); 32 | return; 33 | } 34 | 35 | // metrics[tag] = metric; 36 | metrics.insert(std::pair(tag, metric)); 37 | LOG_INFO("Successfully register metric :%s", tag.c_str()); 38 | } 39 | 40 | void MetricsRegistry::unregister(const std::string &tag) 41 | { 42 | unsigned int num = metrics.erase(tag); 43 | if (num == 0) { 44 | LOG_WARN("There is no %s metric!", tag.c_str()); 45 | return; 46 | } 47 | LOG_INFO("Successfully remove metric of %s", tag.c_str()); 48 | } 49 | 50 | void MetricsRegistry::snapshot() 51 | { 52 | std::map::iterator it = metrics.begin(); 53 | for (; it != metrics.end(); it++) { 54 | it->second->snapshot(); 55 | } 56 | } 57 | 58 | void MetricsRegistry::report() 59 | { 60 | for (std::list::iterator reporterIt = reporters.begin(); reporterIt != reporters.end(); reporterIt++) { 61 | for (std::map::iterator it = metrics.begin(); it != metrics.end(); it++) { 62 | 63 | (*reporterIt)->report(it->first, it->second); 64 | } 65 | } 66 | } 67 | 68 | } // namespace common 69 | -------------------------------------------------------------------------------- /deps/common/metrics/metrics_registry.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | 15 | #ifndef __COMMON_METRICS_METRICS_REGISTRY_H__ 16 | #define __COMMON_METRICS_METRICS_REGISTRY_H__ 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #include "common/metrics/metric.h" 23 | #include "common/metrics/reporter.h" 24 | 25 | namespace common { 26 | 27 | class MetricsRegistry { 28 | public: 29 | MetricsRegistry(){}; 30 | virtual ~MetricsRegistry(){}; 31 | 32 | void register_metric(const std::string &tag, Metric *metric); 33 | void unregister(const std::string &tag); 34 | 35 | void snapshot(); 36 | 37 | void report(); 38 | 39 | void add_reporter(Reporter *reporter) 40 | { 41 | reporters.push_back(reporter); 42 | } 43 | 44 | protected: 45 | std::map metrics; 46 | std::list reporters; 47 | }; 48 | 49 | MetricsRegistry &get_metrics_registry(); 50 | } // namespace common 51 | #endif //__COMMON_METRICS_METRICS_REGISTRY_H__ 52 | -------------------------------------------------------------------------------- /deps/common/metrics/reporter.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/19. 13 | // 14 | 15 | #include "common/metrics/reporter.h" 16 | -------------------------------------------------------------------------------- /deps/common/metrics/reporter.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/19. 13 | // 14 | 15 | #ifndef __COMMON_METRICS_REPORTER_H__ 16 | #define __COMMON_METRICS_REPORTER_H__ 17 | 18 | #include 19 | #include "common/metrics/metric.h" 20 | 21 | namespace common { 22 | 23 | class Reporter { 24 | public: 25 | virtual void report(const std::string &tag, Metric *metric) = 0; 26 | }; 27 | } // namespace common 28 | #endif //__COMMON_METRICS_REPORTER_H__ 29 | -------------------------------------------------------------------------------- /deps/common/metrics/reservoir.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | 15 | #include "reservoir.h" 16 | 17 | using namespace common; 18 | 19 | Reservoir::Reservoir(RandomGenerator &random) : random(random) 20 | {} 21 | 22 | Reservoir::~Reservoir() 23 | {} 24 | 25 | size_t Reservoir::next(size_t range) 26 | { 27 | return random.next(range); 28 | } 29 | -------------------------------------------------------------------------------- /deps/common/metrics/reservoir.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/19. 13 | // 14 | 15 | #ifndef __COMMON_METRICS_RESERVOIR_H_ 16 | #define __COMMON_METRICS_RESERVOIR_H_ 17 | 18 | #include 19 | 20 | #include "common/math/random_generator.h" 21 | #include "common/metrics/metric.h" 22 | #include "common/metrics/snapshot.h" 23 | 24 | namespace common { 25 | 26 | class Reservoir : public Metric { 27 | public: 28 | Reservoir(RandomGenerator &random); 29 | virtual ~Reservoir(); 30 | 31 | public: 32 | virtual size_t size() = 0; 33 | virtual size_t get_count() = 0; 34 | 35 | virtual void update(double one) = 0; 36 | 37 | virtual void reset() = 0; 38 | 39 | protected: 40 | virtual size_t next(size_t range); 41 | 42 | private: 43 | RandomGenerator &random; 44 | }; 45 | 46 | } // namespace common 47 | 48 | #endif /* __COMMON_METRICS_RESERVOIR_H_ */ 49 | -------------------------------------------------------------------------------- /deps/common/metrics/sampler.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | 15 | #include "common/metrics/sampler.h" 16 | #include "common/log/log.h" 17 | 18 | #define RANGE_SIZE 100 19 | 20 | namespace common { 21 | 22 | Sampler *&get_sampler() 23 | { 24 | static Sampler *g_sampler = new Sampler(); 25 | 26 | return g_sampler; 27 | } 28 | 29 | Sampler::Sampler() : random_() 30 | {} 31 | 32 | Sampler::~Sampler() 33 | {} 34 | 35 | bool Sampler::sampling() 36 | { 37 | int v = random_.next(RANGE_SIZE); 38 | if (v <= ratio_num_) { 39 | return true; 40 | } else { 41 | return false; 42 | } 43 | } 44 | 45 | double Sampler::get_ratio() 46 | { 47 | return ratio_; 48 | } 49 | 50 | void Sampler::set_ratio(double ratio) 51 | { 52 | if (0 <= ratio && ratio <= 1) { 53 | this->ratio_ = ratio; 54 | ratio_num_ = ratio * RANGE_SIZE; 55 | } else { 56 | LOG_WARN("Invalid ratio :%lf", ratio); 57 | } 58 | } 59 | 60 | } // namespace common 61 | -------------------------------------------------------------------------------- /deps/common/metrics/sampler.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | 15 | #ifndef __COMMON_METRICS_SAMPLER_H__ 16 | #define __COMMON_METRICS_SAMPLER_H__ 17 | 18 | #include "common/math/random_generator.h" 19 | 20 | namespace common { 21 | 22 | /** 23 | * The most simple sample function 24 | */ 25 | class Sampler { 26 | public: 27 | Sampler(); 28 | virtual ~Sampler(); 29 | 30 | bool sampling(); 31 | 32 | void set_ratio(double ratio); 33 | double get_ratio(); 34 | 35 | private: 36 | double ratio_ = 1.0; 37 | int ratio_num_ = 1; 38 | RandomGenerator random_; 39 | }; 40 | 41 | Sampler *&get_sampler(); 42 | } // namespace common 43 | #endif //__COMMON_METRICS_SAMPLER_H__ 44 | -------------------------------------------------------------------------------- /deps/common/metrics/snapshot.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | 15 | #ifndef __COMMON_METRICS_SNAPSHOT_H__ 16 | #define __COMMON_METRICS_SNAPSHOT_H__ 17 | 18 | #include 19 | #include "common/lang/string.h" 20 | 21 | namespace common { 22 | 23 | class Snapshot { 24 | public: 25 | virtual ~Snapshot(){}; 26 | virtual std::string to_string() = 0; 27 | }; 28 | 29 | template 30 | class SnapshotBasic : public Snapshot { 31 | public: 32 | SnapshotBasic() 33 | : value(){ 34 | 35 | }; 36 | 37 | virtual ~SnapshotBasic() 38 | {} 39 | 40 | void setValue(T &input) 41 | { 42 | value = input; 43 | } 44 | 45 | std::string to_string() 46 | { 47 | std::string ret; 48 | val_to_str(value, ret); 49 | return ret; 50 | } 51 | 52 | private: 53 | T value; 54 | }; 55 | 56 | class SimplerTimerSnapshot : public Snapshot { 57 | public: 58 | SimplerTimerSnapshot() 59 | {} 60 | 61 | virtual ~SimplerTimerSnapshot() 62 | {} 63 | 64 | void setValue(double mean, double tps) 65 | { 66 | this->mean = mean; 67 | this->tps = tps; 68 | } 69 | 70 | std::string to_string() 71 | { 72 | std::stringstream oss; 73 | oss << "mean:" << mean << ",tps:" << tps; 74 | 75 | return oss.str(); 76 | } 77 | 78 | private: 79 | double mean = 1.0; 80 | double tps = 1.0; 81 | }; 82 | } // namespace common 83 | #endif //__COMMON_METRICS_SNAPSHOT_H__ 84 | -------------------------------------------------------------------------------- /deps/common/metrics/timer_snapshot.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | 15 | #include "common/metrics/timer_snapshot.h" 16 | #include 17 | 18 | namespace common { 19 | 20 | TimerSnapshot::TimerSnapshot() 21 | {} 22 | 23 | TimerSnapshot::~TimerSnapshot() 24 | {} 25 | 26 | double TimerSnapshot::get_tps() 27 | { 28 | return tps; 29 | } 30 | 31 | void TimerSnapshot::set_tps(double tps) 32 | { 33 | this->tps = tps; 34 | } 35 | 36 | std::string TimerSnapshot::to_string() 37 | { 38 | std::stringstream oss; 39 | 40 | oss << HistogramSnapShot::to_string() << ",tps:" << tps; 41 | 42 | return oss.str(); 43 | } 44 | } // namespace common -------------------------------------------------------------------------------- /deps/common/metrics/timer_snapshot.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | 15 | #ifndef __COMMON_METRICS_TIMER_SNAPSHOT_H__ 16 | #define __COMMON_METRICS_TIMER_SNAPSHOT_H__ 17 | 18 | #include "common/metrics/histogram_snapshot.h" 19 | 20 | namespace common { 21 | class TimerSnapshot : public HistogramSnapShot { 22 | public: 23 | TimerSnapshot(); 24 | virtual ~TimerSnapshot(); 25 | 26 | double get_tps(); 27 | void set_tps(double tps); 28 | 29 | std::string to_string(); 30 | 31 | protected: 32 | double tps = 1.0; 33 | }; 34 | } // namespace common 35 | #endif //__COMMON_METRICS_TIMER_SNAPSHOT_H__ 36 | -------------------------------------------------------------------------------- /deps/common/metrics/uniform_reservoir.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | 15 | #include "common/metrics/uniform_reservoir.h" 16 | 17 | #include 18 | 19 | #include "common/lang/mutex.h" 20 | #include "common/metrics/histogram_snapshot.h" 21 | 22 | namespace common { 23 | 24 | #define DEFAULT_SIZE 1023 25 | 26 | UniformReservoir::UniformReservoir(RandomGenerator &random) : Reservoir(random), counter(0) 27 | { 28 | pthread_mutexattr_t mutexatr; 29 | pthread_mutexattr_init(&mutexatr); 30 | pthread_mutexattr_settype(&mutexatr, PTHREAD_MUTEX_RECURSIVE); 31 | 32 | MUTEX_INIT(&mutex, &mutexatr); 33 | 34 | init(DEFAULT_SIZE); 35 | } 36 | 37 | UniformReservoir::UniformReservoir(RandomGenerator &random, size_t size) : Reservoir(random), counter(0) 38 | { 39 | 40 | pthread_mutexattr_t mutexatr; 41 | pthread_mutexattr_init(&mutexatr); 42 | pthread_mutexattr_settype(&mutexatr, PTHREAD_MUTEX_RECURSIVE); 43 | 44 | MUTEX_INIT(&mutex, &mutexatr); 45 | init(size); 46 | } 47 | 48 | UniformReservoir::~UniformReservoir() 49 | { 50 | if (snapshot_value_ == NULL) { 51 | delete snapshot_value_; 52 | snapshot_value_ = NULL; 53 | } 54 | } 55 | 56 | void UniformReservoir::init(size_t size) 57 | { 58 | MUTEX_LOCK(&mutex); 59 | counter = 0; 60 | data.resize(size); 61 | MUTEX_UNLOCK(&mutex); 62 | } 63 | 64 | size_t UniformReservoir::size() 65 | { 66 | MUTEX_LOCK(&mutex); 67 | size_t size = (counter < data.size()) ? counter : data.size(); 68 | MUTEX_UNLOCK(&mutex); 69 | return size; 70 | } 71 | 72 | size_t UniformReservoir::get_count() 73 | { 74 | MUTEX_LOCK(&mutex); 75 | size_t ret = counter; 76 | MUTEX_UNLOCK(&mutex); 77 | return ret; 78 | } 79 | 80 | void UniformReservoir::update(double value) 81 | { 82 | MUTEX_LOCK(&mutex); 83 | size_t count = ++counter; 84 | 85 | if (count < data.size()) { 86 | data[count] = (value); 87 | } else { 88 | size_t rcount = next(data.size()); 89 | data[rcount] = (value); 90 | } 91 | 92 | MUTEX_UNLOCK(&mutex); 93 | } 94 | 95 | void UniformReservoir::snapshot() 96 | { 97 | MUTEX_LOCK(&mutex); 98 | std::vector output = data; 99 | MUTEX_UNLOCK(&mutex); 100 | 101 | if (snapshot_value_ == NULL) { 102 | snapshot_value_ = new HistogramSnapShot(); 103 | } 104 | ((HistogramSnapShot *)snapshot_value_)->set_collection(output); 105 | } 106 | 107 | void UniformReservoir::reset() 108 | { 109 | 110 | MUTEX_LOCK(&mutex); 111 | counter = 0; 112 | data.clear(); 113 | 114 | // clear snapshot 115 | MUTEX_UNLOCK(&mutex); 116 | } 117 | 118 | } // namespace common -------------------------------------------------------------------------------- /deps/common/metrics/uniform_reservoir.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | #ifndef __COMMON_METRICS_UNIFORM_RESERVOIR_H_ 15 | #define __COMMON_METRICS_UNIFORM_RESERVOIR_H_ 16 | 17 | #include 18 | 19 | #include 20 | #include 21 | 22 | #include "common/metrics/reservoir.h" 23 | 24 | namespace common { 25 | 26 | /** 27 | * A random sampling reservoir of a stream of {@code long}s. Uses Vitter's 28 | * Algorithm R to produce a statistically representative sample. 29 | * 30 | * @see Random Sampling 31 | * with a Reservoir 32 | */ 33 | 34 | class UniformReservoir : public Reservoir { 35 | public: 36 | UniformReservoir(RandomGenerator &random); 37 | UniformReservoir(RandomGenerator &random, size_t size); 38 | virtual ~UniformReservoir(); 39 | 40 | public: 41 | size_t size(); // data buffer size 42 | size_t get_count(); // how many items have been insert? 43 | 44 | void update(double one); 45 | void snapshot(); 46 | 47 | void reset(); 48 | 49 | protected: 50 | void init(size_t size); 51 | 52 | protected: 53 | pthread_mutex_t mutex; 54 | size_t counter; // counter is likely to be bigger than data.size() 55 | std::vector data; 56 | RandomGenerator random; 57 | }; 58 | 59 | } // namespace common 60 | 61 | #endif /* __COMMON_METRICS_UNIFORM_RESERVOIR_H_ */ 62 | -------------------------------------------------------------------------------- /deps/common/mm/debug_new.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #ifndef __COMMON_MM_DEBUG_NEW_H__ 16 | #define __COMMON_MM_DEBUG_NEW_H__ 17 | 18 | #include 19 | #include 20 | namespace common { 21 | 22 | /* Prototypes */ 23 | bool check_leaks(); 24 | void *operator new(size_t size, const char *file, int line); 25 | void *operator new[](size_t size, const char *file, int line); 26 | #ifndef NO_PLACEMENT_DELETE 27 | void operator delete(void *pointer, const char *file, int line); 28 | void operator delete[](void *pointer, const char *file, int line); 29 | #endif // NO_PLACEMENT_DELETE 30 | void operator delete[](void *); // MSVC 6 requires this declaration 31 | 32 | /* Macros */ 33 | #ifndef DEBUG_NEW_NO_NEW_REDEFINITION 34 | #define new DEBUG_NEW 35 | #define DEBUG_NEW new (__FILE__, __LINE__) 36 | #define debug_new new 37 | #else 38 | #define debug_new new (__FILE__, __LINE__) 39 | #endif // DEBUG_NEW_NO_NEW_REDEFINITION 40 | #ifdef DEBUG_NEW_EMULATE_MALLOC 41 | 42 | #define malloc(s) ((void *)(debug_new char[s])) 43 | #define free(p) delete[](char *)(p) 44 | #endif // DEBUG_NEW_EMULATE_MALLOC 45 | 46 | /* Control flags */ 47 | extern bool new_verbose_flag; // default to false: no verbose information 48 | extern bool new_autocheck_flag; // default to true: call check_leaks() on exit 49 | 50 | } // namespace common 51 | #endif // __COMMON_MM_DEBUG_NEW_H__ 52 | -------------------------------------------------------------------------------- /deps/common/os/os.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010. 13 | // 14 | 15 | #include 16 | #include 17 | 18 | #include "common/defs.h" 19 | #include "common/os/os.h" 20 | #include "common/log/log.h" 21 | 22 | namespace common { 23 | // Don't care windows 24 | u32_t getCpuNum() 25 | { 26 | return std::thread::hardware_concurrency(); 27 | } 28 | 29 | #define MAX_STACK_SIZE 32 30 | 31 | void print_stacktrace() 32 | { 33 | int size = MAX_STACK_SIZE; 34 | void *array[MAX_STACK_SIZE]; 35 | int stack_num = backtrace(array, size); 36 | char **stacktrace = backtrace_symbols(array, stack_num); 37 | for (int i = 0; i < stack_num; ++i) { 38 | LOG_INFO("%d ----- %s\n", i, stacktrace[i]); 39 | } 40 | free(stacktrace); 41 | } 42 | 43 | } // namespace common -------------------------------------------------------------------------------- /deps/common/os/os.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #ifndef __COMMON_OS_OS_H__ 16 | #define __COMMON_OS_OS_H__ 17 | namespace common { 18 | 19 | u32_t getCpuNum(); 20 | 21 | void print_stacktrace(); 22 | 23 | } // namespace common 24 | #endif /* __COMMON_OS_OS_H__ */ 25 | -------------------------------------------------------------------------------- /deps/common/os/path.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/3/27. 13 | // 14 | 15 | #ifndef __COMMON_OS_PATH_H__ 16 | #define __COMMON_OS_PATH_H__ 17 | 18 | #include 19 | namespace common { 20 | 21 | /** 22 | * get file name from full path 23 | * example 24 | * input: /test/happy/ --> return : "" 25 | * input: /test/happy --> return : happy 26 | * input: test/happy --> return : happy 27 | * input: happy --> return : happy 28 | * input: "" --> return : "" 29 | */ 30 | std::string getFileName(const std::string &fullPath); 31 | void getFileName(const char *path, std::string &fileName); 32 | 33 | /** 34 | * get file path from full path 35 | * example 36 | * input: /test/happy --> return : /test 37 | * input: test/happy --> return : test 38 | * input: happy --> return : happy 39 | * input: "" --> return : "" 40 | */ 41 | std::string getFilePath(const std::string &fullPath); 42 | void getDirName(const char *path, std::string &parent); 43 | 44 | /** 45 | * Get absolute path 46 | * input: path 47 | * reutrn absolutely path 48 | */ 49 | std::string getAboslutPath(const char *path); 50 | 51 | /** 52 | * 判断给定目录是否是文件夹 53 | */ 54 | bool is_directory(const char *path); 55 | 56 | /** 57 | * 判断指定路径是否存在并且是文件夹 58 | * 如果不存在将会逐级创建 59 | * @return 创建失败,或者不是文件夹将会返回失败 60 | */ 61 | bool check_directory(std::string &path); 62 | 63 | /** 64 | * 列出指定文件夹下符合指定模式的所有文件 65 | * @param filter_pattern 示例 ^miniob.*bin$ 66 | * @return 成功返回找到的文件个数,否则返回-1 67 | */ 68 | int list_file(const char *path, const char *filter_pattern, std::vector &files); // io/io.h::getFileList 69 | 70 | } // namespace common 71 | #endif //__COMMON_OS_PATH_H__ 72 | -------------------------------------------------------------------------------- /deps/common/os/pidfile.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include "common/log/log.h" 26 | #include "common/os/pidfile.h" 27 | namespace common { 28 | 29 | std::string &getPidPath() 30 | { 31 | static std::string path; 32 | 33 | return path; 34 | } 35 | 36 | void setPidPath(const char *progName) 37 | { 38 | std::string &path = getPidPath(); 39 | 40 | if (progName != NULL) { 41 | path = std::string(_PATH_TMP) + progName + ".pid"; 42 | } else { 43 | path = ""; 44 | } 45 | } 46 | 47 | int writePidFile(const char *progName) 48 | { 49 | assert(progName); 50 | std::ofstream ostr; 51 | int rv = 1; 52 | 53 | setPidPath(progName); 54 | std::string path = getPidPath(); 55 | ostr.open(path.c_str(), std::ios::trunc); 56 | if (ostr.good()) { 57 | ostr << getpid() << std::endl; 58 | ostr.close(); 59 | rv = 0; 60 | } else { 61 | rv = errno; 62 | std::cerr << "error opening PID file " << path.c_str() << SYS_OUTPUT_ERROR << std::endl; 63 | } 64 | 65 | return rv; 66 | } 67 | 68 | void removePidFile(void) 69 | { 70 | std::string path = getPidPath(); 71 | if (!path.empty()) { 72 | unlink(path.c_str()); 73 | setPidPath(NULL); 74 | } 75 | return; 76 | } 77 | 78 | } // namespace common -------------------------------------------------------------------------------- /deps/common/os/pidfile.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #ifndef __COMMON_OS_PIDFILE_H__ 16 | #define __COMMON_OS_PIDFILE_H__ 17 | namespace common { 18 | 19 | //! Generates a PID file for the current component 20 | /** 21 | * Gets the process ID (PID) of the calling process and writes a file 22 | * dervied from the input argument containing that value in a system 23 | * standard directory, e.g. /var/run/progName.pid 24 | * 25 | * @param[in] programName as basis for file to write 26 | * @return 0 for success, error otherwise 27 | */ 28 | int writePidFile(const char *progName); 29 | 30 | //! Cleanup PID file for the current component 31 | /** 32 | * Removes the PID file for the current component 33 | * 34 | */ 35 | void removePidFile(void); 36 | 37 | std::string &getPidPath(); 38 | 39 | } // namespace common 40 | #endif // __COMMON_OS_PIDFILE_H__ 41 | -------------------------------------------------------------------------------- /deps/common/os/process.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #ifndef __COMMON_OS_PROCESS_H__ 16 | #define __COMMON_OS_PROCESS_H__ 17 | namespace common { 18 | 19 | //! Get process Name 20 | /** 21 | * @param[in] prog_full_name process full name with full path 22 | * @return process_name_ process name without directory path 23 | */ 24 | std::string get_process_name(const char *prog_full_name); 25 | //! Runs the service as a daemon 26 | /** 27 | * Backgrounds the calling service as a system daemon by detaching it from 28 | * the controlling terminal, closes stdin, and reopens stdout and stderr 29 | * to the files specified in the input parmaters. "/dev/null" is accepted as 30 | * a valid input, which will be equivalent to closing the respective stream. 31 | * Keeping the streams open but reopening them allows the streams of the 32 | * controling terminal to be closed, thus making it possible for the terminal 33 | * to exit normally while the service is backgrounded. The same file 34 | * could be used for reopening both stderr and stdout streams. 35 | * Creates a new session and sets the service process as the group parent. 36 | * 37 | * @param[in] std_out_file file to redirect stdout to (could be /dev/null) 38 | * @param[in] std_err_file file to redirect stderr to (could be /dev/null) 39 | * @return 0 if success, error code otherwise 40 | */ 41 | int daemonize_service(const char *std_out_file, const char *std_err_file); 42 | 43 | void sys_log_redirect(const char *std_out_file, const char *std_err_file); 44 | 45 | } // namespace common 46 | #endif //__COMMON_OS_PROCESS_H__ 47 | -------------------------------------------------------------------------------- /deps/common/os/process_param.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #include "process_param.h" 16 | #include 17 | namespace common { 18 | 19 | //! Global process config 20 | ProcessParam *&the_process_param() 21 | { 22 | static ProcessParam *process_cfg = new ProcessParam(); 23 | 24 | return process_cfg; 25 | } 26 | 27 | void ProcessParam::init_default(std::string &process_name) 28 | { 29 | assert(process_name.empty() == false); 30 | this->process_name_ = process_name; 31 | if (std_out_.empty()) { 32 | std_out_ = "../log/" + process_name + ".out"; 33 | } 34 | if (std_err_.empty()) { 35 | std_err_ = "../log/" + process_name + ".err"; 36 | } 37 | if (conf.empty()) { 38 | conf = "../etc/" + process_name + ".ini"; 39 | } 40 | 41 | demon = false; 42 | } 43 | 44 | } // namespace common -------------------------------------------------------------------------------- /deps/common/os/process_param.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #ifndef __COMMON_OS_PROCESS_PARAM_H__ 16 | #define __COMMON_OS_PROCESS_PARAM_H__ 17 | 18 | #include 19 | #include 20 | namespace common { 21 | 22 | class ProcessParam { 23 | 24 | public: 25 | ProcessParam() 26 | {} 27 | 28 | virtual ~ProcessParam() 29 | {} 30 | 31 | void init_default(std::string &process_name); 32 | 33 | const std::string &get_std_out() const 34 | { 35 | return std_out_; 36 | } 37 | 38 | void set_std_out(const std::string &std_out) 39 | { 40 | ProcessParam::std_out_ = std_out; 41 | } 42 | 43 | const std::string &get_std_err() const 44 | { 45 | return std_err_; 46 | } 47 | 48 | void set_std_err(const std::string &std_err) 49 | { 50 | ProcessParam::std_err_ = std_err; 51 | } 52 | 53 | const std::string &get_conf() const 54 | { 55 | return conf; 56 | } 57 | 58 | void set_conf(const std::string &conf) 59 | { 60 | ProcessParam::conf = conf; 61 | } 62 | 63 | const std::string &get_process_name() const 64 | { 65 | return process_name_; 66 | } 67 | 68 | void set_process_name(const std::string &processName) 69 | { 70 | ProcessParam::process_name_ = processName; 71 | } 72 | 73 | bool is_demon() const 74 | { 75 | return demon; 76 | } 77 | 78 | void set_demon(bool demon) 79 | { 80 | ProcessParam::demon = demon; 81 | } 82 | 83 | const std::vector &get_args() const 84 | { 85 | return args; 86 | } 87 | 88 | void set_args(const std::vector &args) 89 | { 90 | ProcessParam::args = args; 91 | } 92 | 93 | void set_server_port(int port) 94 | { 95 | server_port_ = port; 96 | } 97 | 98 | int get_server_port() const 99 | { 100 | return server_port_; 101 | } 102 | 103 | void set_unix_socket_path(const char *unix_socket_path) 104 | { 105 | unix_socket_path_ = unix_socket_path; 106 | } 107 | 108 | const std::string &get_unix_socket_path() const 109 | { 110 | return unix_socket_path_; 111 | } 112 | 113 | private: 114 | std::string std_out_; // The output file 115 | std::string std_err_; // The err output file 116 | std::string conf; // The configuration file 117 | std::string process_name_; // The process name 118 | bool demon = false; // whether demon or not 119 | std::vector args; // arguments 120 | int server_port_ = -1; // server port(if valid, will overwrite the port in the config file) 121 | std::string unix_socket_path_; 122 | }; 123 | 124 | ProcessParam *&the_process_param(); 125 | 126 | } // namespace common 127 | #endif //__COMMON_OS_PROCESS_PARAM_H__ 128 | -------------------------------------------------------------------------------- /deps/common/os/signal.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #include "common/os/signal.h" 16 | #include "common/log/log.h" 17 | #include "pthread.h" 18 | namespace common { 19 | 20 | void setSignalHandler(int sig, sighandler_t func) 21 | { 22 | struct sigaction newsa, oldsa; 23 | sigemptyset(&newsa.sa_mask); 24 | newsa.sa_flags = 0; 25 | newsa.sa_handler = func; 26 | int rc = sigaction(sig, &newsa, &oldsa); 27 | if (rc) { 28 | std::cerr << "Failed to set signal " << sig << SYS_OUTPUT_FILE_POS << SYS_OUTPUT_ERROR << std::endl; 29 | } 30 | } 31 | 32 | /* 33 | ** Set Singal handling Fucntion 34 | */ 35 | void setSignalHandler(sighandler_t func) 36 | { 37 | setSignalHandler(SIGQUIT, func); 38 | setSignalHandler(SIGINT, func); 39 | setSignalHandler(SIGHUP, func); 40 | setSignalHandler(SIGTERM, func); 41 | } 42 | 43 | void blockDefaultSignals(sigset_t *signal_set, sigset_t *old_set) 44 | { 45 | sigemptyset(signal_set); 46 | #ifndef DEBUG 47 | // SIGINT will effect our gdb debugging 48 | sigaddset(signal_set, SIGINT); 49 | #endif 50 | sigaddset(signal_set, SIGTERM); 51 | sigaddset(signal_set, SIGUSR1); 52 | pthread_sigmask(SIG_BLOCK, signal_set, old_set); 53 | } 54 | 55 | void unBlockDefaultSignals(sigset_t *signal_set, sigset_t *old_set) 56 | { 57 | sigemptyset(signal_set); 58 | #ifndef DEBUG 59 | sigaddset(signal_set, SIGINT); 60 | #endif 61 | sigaddset(signal_set, SIGTERM); 62 | sigaddset(signal_set, SIGUSR1); 63 | pthread_sigmask(SIG_UNBLOCK, signal_set, old_set); 64 | } 65 | 66 | void *waitForSignals(void *args) 67 | { 68 | LOG_INFO("Start thread to wait signals."); 69 | sigset_t *signal_set = (sigset_t *)args; 70 | int sig_number = -1; 71 | while (true) { 72 | errno = 0; 73 | int ret = sigwait(signal_set, &sig_number); 74 | LOG_INFO("sigwait return value: %d, %d \n", ret, sig_number); 75 | if (ret != 0) { 76 | char errstr[256]; 77 | strerror_r(errno, errstr, sizeof(errstr)); 78 | LOG_ERROR("error (%d) %s\n", errno, errstr); 79 | } 80 | } 81 | return NULL; 82 | } 83 | 84 | void startWaitForSignals(sigset_t *signal_set) 85 | { 86 | pthread_t pThread; 87 | pthread_attr_t pThreadAttrs; 88 | 89 | // create all threads as detached. We will not try to join them. 90 | pthread_attr_init(&pThreadAttrs); 91 | pthread_attr_setdetachstate(&pThreadAttrs, PTHREAD_CREATE_DETACHED); 92 | 93 | pthread_create(&pThread, &pThreadAttrs, waitForSignals, (void *)signal_set); 94 | } 95 | } // namespace common -------------------------------------------------------------------------------- /deps/common/os/signal.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #ifndef __COMMON_OS_SIGNAL_H__ 16 | #define __COMMON_OS_SIGNAL_H__ 17 | 18 | #include 19 | 20 | namespace common { 21 | 22 | //! Default function that blocks signals. 23 | /** 24 | * Now it blocks SIGINT, SIGTERM, and SIGUSR1 25 | */ 26 | void blockDefaultSignals(sigset_t *signal_set, sigset_t *old_set); 27 | //! Default function that unblocks signals. 28 | /** 29 | * It unblocks SIGINT, SIGTERM,and SIGUSR1. 30 | */ 31 | void unBlockDefaultSignals(sigset_t *signal_set, sigset_t *old_set); 32 | 33 | void *waitForSignals(sigset_t *signal_set); 34 | void startWaitForSignals(sigset_t *signal_set); 35 | 36 | // Set signal handling function 37 | /** 38 | * handler function 39 | */ 40 | typedef void (*sighandler_t)(int); 41 | void setSignalHandler(sighandler_t func); 42 | void setSignalHandler(int sig, sighandler_t func); 43 | 44 | } // namespace common 45 | #endif /* __COMMON_OS_SIGNAL_H__ */ 46 | -------------------------------------------------------------------------------- /deps/common/seda/callback.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | // Include Files 16 | #include "common/seda/callback.h" 17 | 18 | #include 19 | 20 | #include "common/log/log.h" 21 | #include "common/seda/stage.h" 22 | #include "common/seda/stage_event.h" 23 | namespace common { 24 | 25 | extern bool &get_event_history_flag(); 26 | 27 | /** 28 | * @author Longda 29 | * @date 3/27/07 30 | * 31 | * Implementation of CompletionCallback class. 32 | */ 33 | 34 | // Constructor 35 | CompletionCallback::CompletionCallback(Stage *trgt, CallbackContext *ctx) 36 | : target_stage_(trgt), context_(ctx), next_cb_(NULL), ev_hist_flag_(get_event_history_flag()) 37 | {} 38 | 39 | // Destructor 40 | CompletionCallback::~CompletionCallback() 41 | { 42 | if (context_) { 43 | delete context_; 44 | } 45 | if (next_cb_) { 46 | delete next_cb_; 47 | } 48 | } 49 | 50 | // Push onto a callback stack 51 | void CompletionCallback::push_callback(CompletionCallback *next) 52 | { 53 | ASSERT((!next_cb_), "%s", "cannot push a callback twice"); 54 | 55 | next_cb_ = next; 56 | } 57 | 58 | // Pop off of a callback stack 59 | CompletionCallback *CompletionCallback::pop_callback() 60 | { 61 | CompletionCallback *ret_val = next_cb_; 62 | 63 | next_cb_ = NULL; 64 | return ret_val; 65 | } 66 | 67 | // One event is complete 68 | void CompletionCallback::event_done(StageEvent *ev) 69 | { 70 | 71 | if (ev_hist_flag_) { 72 | ev->save_stage(target_stage_, StageEvent::CALLBACK_EV); 73 | } 74 | target_stage_->callback_event(ev, context_); 75 | } 76 | 77 | // Reschedule callback on target stage thread 78 | void CompletionCallback::event_reschedule(StageEvent *ev) 79 | { 80 | target_stage_->add_event(ev); 81 | } 82 | 83 | void CompletionCallback::event_timeout(StageEvent *ev) 84 | { 85 | LOG_DEBUG("to call event_timeout for stage %s", target_stage_->get_name()); 86 | if (ev_hist_flag_) { 87 | ev->save_stage(target_stage_, StageEvent::TIMEOUT_EV); 88 | } 89 | target_stage_->timeout_event(ev, context_); 90 | } 91 | 92 | } // namespace common -------------------------------------------------------------------------------- /deps/common/seda/example_stage.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #include "example_stage.h" 16 | 17 | #include 18 | #include 19 | 20 | #include "common/conf/ini.h" 21 | #include "common/io/io.h" 22 | #include "common/lang/string.h" 23 | #include "common/log/log.h" 24 | #include "common/seda/timer_stage.h" 25 | 26 | using namespace common; 27 | 28 | // Constructor 29 | ExampleStage::ExampleStage(const char *tag) : Stage(tag) 30 | {} 31 | 32 | // Destructor 33 | ExampleStage::~ExampleStage() 34 | {} 35 | 36 | // Parse properties, instantiate a stage object 37 | Stage *ExampleStage::make_stage(const std::string &tag) 38 | { 39 | ExampleStage *stage = new ExampleStage(tag.c_str()); 40 | if (stage == NULL) { 41 | LOG_ERROR("new ExampleStage failed"); 42 | return NULL; 43 | } 44 | stage->set_properties(); 45 | return stage; 46 | } 47 | 48 | // Set properties for this object set in stage specific properties 49 | bool ExampleStage::set_properties() 50 | { 51 | // std::string stageNameStr(stage_name_); 52 | // std::map section = g_properties()->get( 53 | // stageNameStr); 54 | // 55 | // std::map::iterator it; 56 | // 57 | // std::string key; 58 | 59 | return true; 60 | } 61 | 62 | // Initialize stage params and validate outputs 63 | bool ExampleStage::initialize() 64 | { 65 | LOG_TRACE("Enter"); 66 | 67 | // std::list::iterator stgp = next_stage_list_.begin(); 68 | // mTimerStage = *(stgp++); 69 | // mCommStage = *(stgp++); 70 | 71 | LOG_TRACE("Exit"); 72 | return true; 73 | } 74 | 75 | // Cleanup after disconnection 76 | void ExampleStage::cleanup() 77 | { 78 | LOG_TRACE("Enter"); 79 | 80 | LOG_TRACE("Exit"); 81 | } 82 | 83 | void ExampleStage::handle_event(StageEvent *event) 84 | { 85 | LOG_TRACE("Enter\n"); 86 | 87 | LOG_TRACE("Exit\n"); 88 | return; 89 | } 90 | 91 | void ExampleStage::callback_event(StageEvent *event, CallbackContext *context) 92 | { 93 | LOG_TRACE("Enter\n"); 94 | 95 | LOG_TRACE("Exit\n"); 96 | return; 97 | } 98 | -------------------------------------------------------------------------------- /deps/common/seda/example_stage.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #ifndef __COMMON_SEDA_EXAMPLE_STAGE_H__ 16 | #define __COMMON_SEDA_EXAMPLE_STAGE_H__ 17 | 18 | #include "common/seda/stage.h" 19 | 20 | namespace common { 21 | 22 | class ExampleStage : public Stage { 23 | public: 24 | ~ExampleStage(); 25 | static Stage *make_stage(const std::string &tag); 26 | 27 | protected: 28 | // common function 29 | ExampleStage(const char *tag); 30 | bool set_properties(); 31 | 32 | bool initialize(); 33 | void cleanup(); 34 | void handle_event(StageEvent *event); 35 | void callback_event(StageEvent *event, CallbackContext *context); 36 | }; 37 | } // namespace common 38 | #endif //__COMMON_SEDA_EXAMPLE_STAGE_H__ 39 | -------------------------------------------------------------------------------- /deps/common/seda/init.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #include "common/seda/init.h" 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | #include "common/io/io.h" 30 | #include "common/log/log.h" 31 | #include "common/time/datetime.h" 32 | #include "common/seda/kill_thread.h" 33 | #include "common/seda/seda_config.h" 34 | #include "common/seda/stage_factory.h" 35 | #include "common/seda/metrics_stage.h" 36 | #include "common/seda/thread_pool.h" 37 | #include "common/seda/timer_stage.h" 38 | namespace common { 39 | 40 | int init_seda(ProcessParam *process_cfg) 41 | { 42 | // Initialize the static data structures of threadpool 43 | Threadpool::create_pool_key(); 44 | 45 | // initialize class factory instances here 46 | static StageFactory kill_thread_factory("KillThreads", &KillThreadStage::make_stage); 47 | static StageFactory timer_factory("TimerStage", &TimerStage::make_stage); 48 | static StageFactory seda_stats_factory("MetricsStage", &MetricsStage::make_stage); 49 | 50 | // try to parse the seda configuration files 51 | SedaConfig *config = SedaConfig::get_instance(); 52 | SedaConfig::status_t config_stat; 53 | 54 | config_stat = config->parse(); 55 | if (config_stat != SedaConfig::SUCCESS) { 56 | LOG_ERROR("Error: unable to parse file %s", process_cfg->get_process_name().c_str()); 57 | return errno; 58 | } 59 | 60 | // Log a message to indicate that we are restarting, when looking 61 | // at a log we can see if mmon is restarting us because we keep 62 | // crashing. 63 | LOG_INFO("(Re)Starting State: Pid: %u Time: %s", (unsigned int)getpid(), DateTime::now().to_string_local().c_str()); 64 | LOG_INFO("The process Name is %s", process_cfg->get_process_name().c_str()); 65 | 66 | // try to initialize the seda configuration 67 | config_stat = config->init(); 68 | if (config_stat != SedaConfig::SUCCESS) { 69 | LOG_ERROR("SedaConfig: unable to initialize seda stages"); 70 | return errno; 71 | } 72 | 73 | get_seda_config() = config; 74 | 75 | return 0; 76 | } 77 | 78 | void cleanup_seda() 79 | { 80 | SedaConfig *seda_config = SedaConfig::get_instance(); 81 | delete seda_config; 82 | SedaConfig::get_instance() = NULL; 83 | } 84 | 85 | } // namespace common -------------------------------------------------------------------------------- /deps/common/seda/init.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #ifndef __COMMON_SEDA_INIT_H__ 16 | #define __COMMON_SEDA_INIT_H__ 17 | 18 | // Basic includes 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | 27 | #include "common/conf/ini.h" 28 | #include "common/defs.h" 29 | #include "common/os/process_param.h" 30 | namespace common { 31 | 32 | /** 33 | * start the seda process, do this will trigger all threads 34 | */ 35 | int init_seda(ProcessParam *process_cfg); 36 | 37 | void cleanup_seda(); 38 | 39 | } // namespace common 40 | #endif // __COMMON_SEDA_INIT_H__ 41 | -------------------------------------------------------------------------------- /deps/common/seda/kill_thread.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | // Include Files 15 | #include "common/seda/kill_thread.h" 16 | 17 | #include 18 | 19 | #include "common/seda/thread_pool.h" 20 | namespace common { 21 | 22 | /** 23 | * Notify the pool and kill the thread 24 | * @param[in] event Pointer to event that must be handled. 25 | * 26 | * @post Call never returns. Thread is killed. Pool is notified. 27 | */ 28 | void KillThreadStage::handle_event(StageEvent *event) 29 | { 30 | get_pool()->thread_kill(); 31 | event->done(); 32 | this->release_event(); 33 | pthread_exit(0); 34 | } 35 | 36 | /** 37 | * Process properties of the classes 38 | * @pre class members are uninitialized 39 | * @post initializing the class members 40 | * @return the class object 41 | */ 42 | Stage *KillThreadStage::make_stage(const std::string &tag) 43 | { 44 | return new KillThreadStage(tag.c_str()); 45 | } 46 | 47 | bool KillThreadStage::set_properties() 48 | { 49 | // nothing to do 50 | return true; 51 | } 52 | 53 | } // namespace common -------------------------------------------------------------------------------- /deps/common/seda/kill_thread.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #ifndef __COMMON_SEDA_KILL_THREAD_H__ 16 | #define __COMMON_SEDA_KILL_THREAD_H__ 17 | 18 | #include 19 | 20 | #include "common/defs.h" 21 | #include "common/seda/stage.h" 22 | namespace common { 23 | 24 | /** 25 | * @file 26 | * @author Longda 27 | * @date 3/16/07 28 | */ 29 | 30 | class Threadpool; 31 | 32 | /** 33 | * A Stage to kill threads in a thread pool 34 | * The KillThreadStage is scheduled on a thread pool whenever threads 35 | * need to be killed. Each event handled by the stage results in the 36 | * death of the thread. 37 | */ 38 | class KillThreadStage : public Stage { 39 | 40 | public: 41 | /** 42 | * parse properties, instantiate a summation stage object 43 | * @pre class members are uninitialized 44 | * @post initializing the class members 45 | * @return Stage instantiated object 46 | */ 47 | static Stage *make_stage(const std::string &tag); 48 | 49 | protected: 50 | /** 51 | * Constructor 52 | * @param[in] tag The label that identifies this stage. 53 | * 54 | * @pre tag is non-null and points to null-terminated string 55 | * @post event queue is empty 56 | * @post stage is not connected 57 | */ 58 | KillThreadStage(const char *tag) : Stage(tag) 59 | {} 60 | 61 | /** 62 | * Notify the pool and kill the thread 63 | * @param[in] event Pointer to event that must be handled. 64 | * 65 | * @post Call never returns. Thread is killed. Pool is notified. 66 | */ 67 | void handle_event(StageEvent *event); 68 | 69 | /** 70 | * Handle the callback 71 | * Nothing special for callbacks in this stage. 72 | */ 73 | void callback_event(StageEvent *event, CallbackContext *context) 74 | { 75 | return; 76 | } 77 | 78 | /** 79 | * Initialize stage params 80 | * Ignores next_stage_list_---there are no outputs for this stage. 81 | * 82 | * @pre Stage not connected 83 | * @return true 84 | */ 85 | bool initialize() 86 | { 87 | return true; 88 | } 89 | 90 | /** 91 | * set properties for this object 92 | * @pre class members are uninitialized 93 | * @post initializing the class members 94 | * @return Stage instantiated object 95 | */ 96 | bool set_properties(); 97 | 98 | friend class Threadpool; 99 | }; 100 | 101 | } // namespace common 102 | #endif // __COMMON_SEDA_KILL_THREAD_H__ 103 | -------------------------------------------------------------------------------- /deps/common/seda/metrics_report_event.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | 15 | #include "metrics_report_event.h" 16 | -------------------------------------------------------------------------------- /deps/common/seda/metrics_report_event.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/20. 13 | // 14 | 15 | #ifndef __COMMON_SEDA_METRICS_REPORT_EVENT_H__ 16 | #define __COMMON_SEDA_METRICS_REPORT_EVENT_H__ 17 | 18 | #include "common/seda/stage_event.h" 19 | 20 | namespace common { 21 | class MetricsReportEvent : public StageEvent { 22 | public: 23 | MetricsReportEvent(){ 24 | 25 | }; 26 | 27 | ~MetricsReportEvent(){ 28 | 29 | }; 30 | }; 31 | } // namespace common 32 | #endif //__COMMON_SEDA_METRICS_REPORT_EVENT_H__ 33 | -------------------------------------------------------------------------------- /deps/common/seda/metrics_stage.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #ifndef __COMMON_SEDA_METRICS_STAGE_H__ 16 | #define __COMMON_SEDA_METRICS_STAGE_H__ 17 | 18 | #include "common/seda/stage.h" 19 | 20 | namespace common { 21 | 22 | class MetricsStage : public Stage { 23 | public: 24 | ~MetricsStage(); 25 | static Stage *make_stage(const std::string &tag); 26 | 27 | protected: 28 | // common function 29 | MetricsStage(const char *tag); 30 | bool set_properties(); 31 | 32 | bool initialize(); 33 | void cleanup(); 34 | void handle_event(StageEvent *event); 35 | void callback_event(StageEvent *event, CallbackContext *context); 36 | 37 | protected: 38 | private: 39 | Stage *timer_stage_ = nullptr; 40 | // report metrics every @metric_report_interval_ seconds 41 | int metric_report_interval_ = 10; 42 | }; 43 | } // namespace common 44 | #endif //__COMMON_SEDA_METRICS_STAGE_H__ 45 | -------------------------------------------------------------------------------- /deps/common/seda/seda_defs.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/21. 13 | // 14 | 15 | #ifndef __COMMON_SEDA_SEDA_DEFS_H__ 16 | #define __COMMON_SEDA_SEDA_DEFS_H__ 17 | 18 | #define SEDA_BASE_NAME "SEDA_BASE" 19 | #define THREAD_POOLS_NAME "ThreadPools" 20 | #define STAGES "STAGES" 21 | 22 | #define EVENT_HISTORY "EventHistory" 23 | #define MAX_EVENT_HISTORY_NUM "MaxEventHistoryNum" 24 | 25 | #define COUNT "count" 26 | 27 | #define THREAD_POOL_ID "ThreadId" 28 | 29 | #define NEXT_STAGES "NextStages" 30 | #define DEFAULT_THREAD_POOL "DefaultThreads" 31 | #define METRCS_REPORT_INTERVAL "MetricsReportInterval" 32 | 33 | #endif //__COMMON_SEDA_SEDA_DEFS_H__ 34 | -------------------------------------------------------------------------------- /deps/common/seda/stage_factory.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #ifndef __COMMON_SEDA_STAGE_FACTORY_H__ 16 | #define __COMMON_SEDA_STAGE_FACTORY_H__ 17 | 18 | #include "common/seda/class_factory.h" 19 | #include "common/seda/stage.h" 20 | namespace common { 21 | 22 | class Stage; 23 | 24 | typedef ClassFactory StageFactory; 25 | 26 | } // namespace common 27 | #endif // __COMMON_SEDA_STAGE_FACTORY_H__ 28 | -------------------------------------------------------------------------------- /deps/common/time/timeout_info.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #include "common/time/timeout_info.h" 16 | 17 | #include 18 | namespace common { 19 | 20 | TimeoutInfo::TimeoutInfo(time_t deadLine) : deadline_(deadLine), is_timed_out_(false), ref_cnt_(0) 21 | { 22 | MUTEX_INIT(&mutex_, NULL); 23 | } 24 | 25 | TimeoutInfo::~TimeoutInfo() 26 | { 27 | // unlock mutex_ as we locked it before 'delete this' 28 | MUTEX_UNLOCK(&mutex_); 29 | 30 | MUTEX_DESTROY(&mutex_); 31 | } 32 | 33 | void TimeoutInfo::attach() 34 | { 35 | MUTEX_LOCK(&mutex_); 36 | ref_cnt_++; 37 | MUTEX_UNLOCK(&mutex_); 38 | } 39 | 40 | void TimeoutInfo::detach() 41 | { 42 | MUTEX_LOCK(&mutex_); 43 | if (0 == --ref_cnt_) { 44 | delete this; 45 | return; 46 | } 47 | MUTEX_UNLOCK(&mutex_); 48 | } 49 | 50 | bool TimeoutInfo::has_timed_out() 51 | { 52 | MUTEX_LOCK(&mutex_); 53 | bool ret = is_timed_out_; 54 | if (!is_timed_out_) { 55 | struct timeval tv; 56 | gettimeofday(&tv, NULL); 57 | 58 | ret = is_timed_out_ = (tv.tv_sec >= deadline_); 59 | } 60 | MUTEX_UNLOCK(&mutex_); 61 | 62 | return ret; 63 | } 64 | 65 | } // namespace common -------------------------------------------------------------------------------- /deps/common/time/timeout_info.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #ifndef __COMMON_TIME_TIMEOUT_INFO_H__ 16 | #define __COMMON_TIME_TIMEOUT_INFO_H__ 17 | 18 | #include 19 | 20 | #include "common/lang/mutex.h" 21 | namespace common { 22 | 23 | /** 24 | * Timeout info class used to judge if a certain deadline_ has reached or not. 25 | * It's good to use handle-body to automate the reference count 26 | * increase/decrease. However, explicit attach/detach interfaces 27 | * are used here to simplify the implementation. 28 | */ 29 | 30 | class TimeoutInfo { 31 | public: 32 | /** 33 | * Constructor 34 | * @param[in] deadline_ deadline_ of this timeout 35 | */ 36 | TimeoutInfo(time_t deadline_); 37 | 38 | // Increase ref count 39 | void attach(); 40 | 41 | // Decrease ref count 42 | void detach(); 43 | 44 | // Check if it has timed out 45 | bool has_timed_out(); 46 | 47 | private: 48 | // Forbid copy ctor and =() to support ref count 49 | 50 | // Copy constructor. 51 | TimeoutInfo(const TimeoutInfo &ti); 52 | 53 | // Assignment operator. 54 | TimeoutInfo &operator=(const TimeoutInfo &ti); 55 | 56 | protected: 57 | // Avoid heap-based \c TimeoutInfo 58 | // so it can easily associated with \c StageEvent 59 | 60 | // Destructor. 61 | ~TimeoutInfo(); 62 | 63 | private: 64 | time_t deadline_; // when should this be timed out 65 | 66 | // used to predict timeout if now + reservedTime > deadline_ 67 | // time_t reservedTime; 68 | 69 | bool is_timed_out_; // timeout flag 70 | 71 | int ref_cnt_; // reference count of this object 72 | pthread_mutex_t mutex_; // mutex_ to protect ref_cnt_ and flag 73 | }; 74 | 75 | } // namespace common 76 | #endif // __COMMON_TIME_TIMEOUT_INFO_H__ 77 | -------------------------------------------------------------------------------- /deps/common/version.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2010 13 | // 14 | 15 | #ifndef __COMMON_VERSION_H__ 16 | #define __COMMON_VERSION_H__ 17 | namespace common { 18 | 19 | #ifndef MAIJOR_VER 20 | #define MAIJOR_VER 1 21 | #endif 22 | 23 | #ifndef MINOR_VER 24 | #define MINOR_VER 0 25 | #endif 26 | 27 | #ifndef PATCH_VER 28 | #define PATCH_VER 0 29 | #endif 30 | 31 | #ifndef OTHER_VER 32 | #define OTHER_VER 1 33 | #endif 34 | 35 | #define STR1(R) #R 36 | #define STR2(R) STR1(R) 37 | 38 | #define VERSION_STR (STR2(MAIJOR_VER) "." STR2(MINOR_VER) "." STR2(PATCH_VER) "." STR2(OTHER_VER)) 39 | #define VERSION_NUM (MAIJOR_VER << 24 | MINOR_VER << 16 | PATCH_VER << 8 | OTHER_VER) 40 | 41 | } // namespace common 42 | #endif //__COMMON_VERSION_H__ 43 | -------------------------------------------------------------------------------- /docs/how_to_build.md: -------------------------------------------------------------------------------- 1 | # How to build 2 | 1. install cmake 3 | 4 | 5 | 6 | 2. build libevent 7 | 8 | ``` 9 | git submodule add https://github.com/libevent/libevent deps/libevent 10 | cd deps 11 | cd libevent 12 | git checkout release-2.1.12-stable 13 | mkdir build 14 | cd build 15 | cmake .. -DEVENT__DISABLE_OPENSSL=ON 16 | make 17 | sudo make install 18 | ``` 19 | 20 | 3. build google test 21 | ``` 22 | 23 | git submodule add https://github.com/google/googletest deps/googletest 24 | cd deps 25 | cd googletest 26 | mkdir build 27 | cd build 28 | cmake .. 29 | make 30 | sudo make install 31 | ``` 32 | 33 | 4. build jsoncpp 34 | ```shell 35 | 36 | git submodule add https://github.com/open-source-parsers/jsoncpp.git deps/jsoncpp 37 | cd deps 38 | cd jsoncpp 39 | mkdir build 40 | cd build 41 | cmake -DJSONCPP_WITH_TESTS=OFF -DJSONCPP_WITH_POST_BUILD_UNITTEST=OFF .. 42 | make 43 | sudo make install 44 | ``` 45 | 46 | 5. build miniob 47 | 48 | ```shell 49 | cd `project home` 50 | mkdir build 51 | cd build 52 | cmake .. 53 | make 54 | ``` 55 | -------------------------------------------------------------------------------- /etc/observer.ini: -------------------------------------------------------------------------------- 1 | # observer's configuration 2 | 3 | # log part 4 | [LOG] 5 | # log file's name, default is ${process_name_}.log 6 | LOG_FILE_NAME = observer.log 7 | # LOG_LEVEL_PANIC = 0, 8 | # LOG_LEVEL_ERR = 1, 9 | # LOG_LEVEL_WARN = 2, 10 | # LOG_LEVEL_INFO = 3, 11 | # LOG_LEVEL_DEBUG = 4, 12 | # LOG_LEVEL_TRACE = 5, 13 | # LOG_LEVEL_LAST 14 | # output log level, default is LOG_LEVEL_INFO 15 | LOG_FILE_LEVEL=3 16 | LOG_CONSOLE_LEVEL=1 17 | # the module's log will output whatever level used. 18 | #DefaultLogModules="server.cpp,client.cpp" 19 | 20 | # seda's configuration 21 | [SEDA_BASE] 22 | # record every event 23 | EventHistory=false 24 | # max history event's number, default is 100 25 | MaxEventHistoryNum=100 26 | # threadpools' name, it will contain the threadpool's section 27 | ThreadPools=SQLThreads,IOThreads,DefaultThreads 28 | # stage list 29 | STAGES=SessionStage,ExecuteStage,OptimizeStage,ParseStage,ResolveStage,\ 30 | PlanCacheStage,QueryCacheStage,DefaultStorageStage,MemStorageStage,\ 31 | TimerStage,MetricsStage 32 | 33 | [NET] 34 | CLIENT_ADDRESS=INADDR_ANY 35 | MAX_CONNECTION_NUM=8192 36 | PORT=6789 37 | 38 | [SQLThreads] 39 | # the thread number of this threadpool, 0 means cpu's cores. 40 | # if miss the setting of count, it will use cpu's core number; 41 | count=3 42 | #count=0 43 | 44 | [IOThreads] 45 | # the thread number of this threadpool, 0 means cpu's cores. 46 | # if miss the setting of count, it will use cpu's core number; 47 | count=3 48 | #count=0 49 | 50 | [DefaultThreads] 51 | # If Stage haven't set threadpool, it will use this threadpool 52 | # This threadpool is used for backend operation, such as timer, sedastats and so on. 53 | # the thread number of this threadpool, 0 means cpu's cores. 54 | # if miss the setting of count, it will use cpu's core number; 55 | count=3 56 | #count=0 57 | 58 | [SessionStage] 59 | ThreadId=SQLThreads 60 | NextStages=ResolveStage 61 | 62 | [ResolveStage] 63 | ThreadId=SQLThreads 64 | NextStages=QueryCacheStage 65 | 66 | [QueryCacheStage] 67 | ThreadId=SQLThreads 68 | NextStages=PlanCacheStage 69 | 70 | [PlanCacheStage] 71 | ThreadId=SQLThreads 72 | NextStages=ExecuteStage,ParseStage 73 | 74 | [ParseStage] 75 | ThreadId=SQLThreads 76 | NextStages=OptimizeStage 77 | 78 | [OptimizeStage] 79 | ThreadId=SQLThreads 80 | NextStages=ExecuteStage 81 | 82 | [ExecuteStage] 83 | ThreadId=SQLThreads 84 | NextStages=DefaultStorageStage,MemStorageStage 85 | 86 | [DefaultStorageStage] 87 | ThreadId=IOThreads 88 | BaseDir=./miniob 89 | SystemDb=sys 90 | 91 | [MemStorageStage] 92 | ThreadId=IOThreads 93 | 94 | [MetricsStage] 95 | NextStages=TimerStage 96 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | std::cout << "Hello, World!" << std::endl; 5 | return 0; 6 | } 7 | -------------------------------------------------------------------------------- /miniob/db/sys/table1.data: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dase314/db_impl_course/205142a51a59e2c308e3d693dfc91a1cc71916f3/miniob/db/sys/table1.data -------------------------------------------------------------------------------- /miniob/db/sys/table1.table: -------------------------------------------------------------------------------- 1 | { 2 | "fields" : 3 | [ 4 | { 5 | "len" : 4, 6 | "name" : "__trx", 7 | "offset" : 0, 8 | "type" : "ints", 9 | "visible" : false 10 | }, 11 | { 12 | "len" : 4, 13 | "name" : "c1", 14 | "offset" : 4, 15 | "type" : "ints", 16 | "visible" : true 17 | }, 18 | { 19 | "len" : 4, 20 | "name" : "c2", 21 | "offset" : 8, 22 | "type" : "ints", 23 | "visible" : true 24 | } 25 | ], 26 | "indexes" : null, 27 | "table_name" : "table1" 28 | } -------------------------------------------------------------------------------- /miniob/db/sys/table2.data: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dase314/db_impl_course/205142a51a59e2c308e3d693dfc91a1cc71916f3/miniob/db/sys/table2.data -------------------------------------------------------------------------------- /miniob/db/sys/table2.table: -------------------------------------------------------------------------------- 1 | { 2 | "fields" : 3 | [ 4 | { 5 | "len" : 4, 6 | "name" : "__trx", 7 | "offset" : 0, 8 | "type" : "ints", 9 | "visible" : false 10 | }, 11 | { 12 | "len" : 4, 13 | "name" : "c1", 14 | "offset" : 4, 15 | "type" : "ints", 16 | "visible" : true 17 | }, 18 | { 19 | "len" : 4, 20 | "name" : "c2", 21 | "offset" : 8, 22 | "type" : "ints", 23 | "visible" : true 24 | } 25 | ], 26 | "indexes" : null, 27 | "table_name" : "table2" 28 | } -------------------------------------------------------------------------------- /miniob/db/sys/table3.data: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dase314/db_impl_course/205142a51a59e2c308e3d693dfc91a1cc71916f3/miniob/db/sys/table3.data -------------------------------------------------------------------------------- /miniob/db/sys/table3.table: -------------------------------------------------------------------------------- 1 | { 2 | "fields" : 3 | [ 4 | { 5 | "len" : 4, 6 | "name" : "__trx", 7 | "offset" : 0, 8 | "type" : "ints", 9 | "visible" : false 10 | }, 11 | { 12 | "len" : 4, 13 | "name" : "c1", 14 | "offset" : 4, 15 | "type" : "ints", 16 | "visible" : true 17 | }, 18 | { 19 | "len" : 4, 20 | "name" : "c2", 21 | "offset" : 8, 22 | "type" : "ints", 23 | "visible" : true 24 | } 25 | ], 26 | "indexes" : null, 27 | "table_name" : "table3" 28 | } -------------------------------------------------------------------------------- /miniob/db/sys/table4.data: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dase314/db_impl_course/205142a51a59e2c308e3d693dfc91a1cc71916f3/miniob/db/sys/table4.data -------------------------------------------------------------------------------- /miniob/db/sys/table4.table: -------------------------------------------------------------------------------- 1 | { 2 | "fields" : 3 | [ 4 | { 5 | "len" : 4, 6 | "name" : "__trx", 7 | "offset" : 0, 8 | "type" : "ints", 9 | "visible" : false 10 | }, 11 | { 12 | "len" : 4, 13 | "name" : "c1", 14 | "offset" : 4, 15 | "type" : "ints", 16 | "visible" : true 17 | }, 18 | { 19 | "len" : 4, 20 | "name" : "c2", 21 | "offset" : 8, 22 | "type" : "ints", 23 | "visible" : true 24 | } 25 | ], 26 | "indexes" : null, 27 | "table_name" : "table4" 28 | } -------------------------------------------------------------------------------- /miniob/db/sys/table5.table: -------------------------------------------------------------------------------- 1 | { 2 | "fields" : 3 | [ 4 | { 5 | "len" : 4, 6 | "name" : "__trx", 7 | "offset" : 0, 8 | "type" : "ints", 9 | "visible" : false 10 | }, 11 | { 12 | "len" : 4, 13 | "name" : "c1", 14 | "offset" : 4, 15 | "type" : "ints", 16 | "visible" : true 17 | }, 18 | { 19 | "len" : 4, 20 | "name" : "c2", 21 | "offset" : 8, 22 | "type" : "ints", 23 | "visible" : true 24 | } 25 | ], 26 | "indexes" : null, 27 | "table_name" : "table5" 28 | } -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | PROJECT(miniob) 2 | MESSAGE("Begin to build " ${PROJECT_NAME}) 3 | MESSAGE(STATUS "This is PROJECT_BINARY_DIR dir " ${PROJECT_BINARY_DIR}) 4 | MESSAGE(STATUS "This is PROJECT_SOURCE_DIR dir " ${PROJECT_SOURCE_DIR}) 5 | 6 | 7 | ADD_SUBDIRECTORY(obclient) 8 | ADD_SUBDIRECTORY(observer) 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/obclient/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | PROJECT(obclient) 2 | MESSAGE("Begin to build " ${PROJECT_NAME}) 3 | MESSAGE(STATUS "This is PROJECT_BINARY_DIR dir " ${PROJECT_BINARY_DIR}) 4 | MESSAGE(STATUS "This is PROJECT_SOURCE_DIR dir " ${PROJECT_SOURCE_DIR}) 5 | 6 | 7 | #INCLUDE_DIRECTORIES([AFTER|BEFORE] [SYSTEM] dir1 dir2 ...) 8 | INCLUDE_DIRECTORIES(. ${PROJECT_SOURCE_DIR}/../../deps /usr/local/include SYSTEM) 9 | # 父cmake 设置的include_directories 和link_directories并不传导到子cmake里面 10 | #INCLUDE_DIRECTORIES(BEFORE ${CMAKE_INSTALL_PREFIX}/include) 11 | LINK_DIRECTORIES(/usr/local/lib ${PROJECT_BINARY_DIR}/../../lib) 12 | 13 | 14 | FILE(GLOB_RECURSE ALL_SRC *.cpp) 15 | FOREACH (F ${ALL_SRC}) 16 | 17 | SET(PRJ_SRC ${PRJ_SRC} ${F}) 18 | MESSAGE("Use " ${F}) 19 | 20 | ENDFOREACH (F) 21 | 22 | 23 | # 指定目标文件位置 24 | SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../../bin) 25 | MESSAGE("Binary directory:" ${EXECUTABLE_OUTPUT_PATH}) 26 | ADD_EXECUTABLE(${PROJECT_NAME} ${PRJ_SRC}) 27 | TARGET_LINK_LIBRARIES(${PROJECT_NAME} common pthread dl) 28 | 29 | 30 | # Target 必须在定义 ADD_EXECUTABLE 之后, programs 不受这个限制 31 | # TARGETS和PROGRAMS 的默认权限是OWNER_EXECUTE, GROUP_EXECUTE, 和WORLD_EXECUTE,即755权限, programs 都是处理脚步类 32 | # 类型分为RUNTIME/LIBRARY/ARCHIVE, prog 33 | INSTALL(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin) -------------------------------------------------------------------------------- /src/observer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | PROJECT(observer) 2 | MESSAGE("Begin to build " ${PROJECT_NAME}) 3 | MESSAGE(STATUS "This is PROJECT_BINARY_DIR dir " ${PROJECT_BINARY_DIR}) 4 | MESSAGE(STATUS "This is PROJECT_SOURCE_DIR dir " ${PROJECT_SOURCE_DIR}) 5 | 6 | 7 | #INCLUDE_DIRECTORIES([AFTER|BEFORE] [SYSTEM] dir1 dir2 ...) 8 | INCLUDE_DIRECTORIES(. ${PROJECT_SOURCE_DIR} ${PROJECT_SOURCE_DIR}/../../deps /usr/local/include SYSTEM) 9 | # 父cmake 设置的include_directories 和link_directories并不传导到子cmake里面 10 | #INCLUDE_DIRECTORIES(BEFORE ${CMAKE_INSTALL_PREFIX}/include) 11 | LINK_DIRECTORIES(/usr/local/lib ${PROJECT_BINARY_DIR}/../../lib) 12 | 13 | 14 | FILE(GLOB_RECURSE ALL_SRC *.cpp *.c) 15 | FILE(GLOB MAIN_SRC main.cpp) 16 | MESSAGE("MAIN SRC: " ${MAIN_SRC}) 17 | FOREACH (F ${ALL_SRC}) 18 | 19 | IF (NOT ${F} STREQUAL ${MAIN_SRC}) 20 | SET(LIB_SRC ${LIB_SRC} ${F}) 21 | ENDIF() 22 | 23 | MESSAGE("Use " ${F}) 24 | 25 | ENDFOREACH (F) 26 | 27 | SET(LIBRARIES common pthread dl event jsoncpp) 28 | 29 | # 指定目标文件位置 30 | SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../../bin) 31 | MESSAGE("Binary directory:" ${EXECUTABLE_OUTPUT_PATH}) 32 | ADD_EXECUTABLE(${PROJECT_NAME} ${ALL_SRC}) 33 | TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${LIBRARIES}) 34 | 35 | SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../../lib) 36 | MESSAGE("Archive directory:" ${LIBRARY_OUTPUT_PATH}) 37 | ADD_LIBRARY(${PROJECT_NAME}_static STATIC ${LIB_SRC}) 38 | SET_TARGET_PROPERTIES(${PROJECT_NAME}_static PROPERTIES OUTPUT_NAME ${PROJECT_NAME}) 39 | TARGET_LINK_LIBRARIES(${PROJECT_NAME}_static ${LIBRARIES}) 40 | 41 | 42 | # Target 必须在定义 ADD_EXECUTABLE 之后, programs 不受这个限制 43 | # TARGETS和PROGRAMS 的默认权限是OWNER_EXECUTE, GROUP_EXECUTE, 和WORLD_EXECUTE,即755权限, programs 都是处理脚本类 44 | # 类型分为RUNTIME/LIBRARY/ARCHIVE, prog 45 | INSTALL(TARGETS ${PROJECT_NAME} ${PROJECT_NAME}_static 46 | RUNTIME DESTINATION bin 47 | ARCHIVE DESTINATION lib) 48 | -------------------------------------------------------------------------------- /src/observer/event/execution_plan_event.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Wangyunlai on 2021/5/11. 13 | // 14 | 15 | #include "event/execution_plan_event.h" 16 | #include "event/sql_event.h" 17 | 18 | ExecutionPlanEvent::ExecutionPlanEvent(SQLStageEvent *sql_event, Query *sqls) : sql_event_(sql_event), sqls_(sqls) 19 | {} 20 | ExecutionPlanEvent::~ExecutionPlanEvent() 21 | { 22 | sql_event_ = nullptr; 23 | // if (sql_event_) { 24 | // sql_event_->doneImmediate(); 25 | // } 26 | 27 | query_destroy(sqls_); 28 | sqls_ = nullptr; 29 | } 30 | -------------------------------------------------------------------------------- /src/observer/event/execution_plan_event.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Wangyunlai on 2021/5/11. 13 | // 14 | 15 | #ifndef __OBSERVER_EVENT_EXECUTION_PLAN_EVENT_H__ 16 | #define __OBSERVER_EVENT_EXECUTION_PLAN_EVENT_H__ 17 | 18 | #include "common/seda/stage_event.h" 19 | #include "sql/parser/parse.h" 20 | 21 | class SQLStageEvent; 22 | 23 | class ExecutionPlanEvent : public common::StageEvent { 24 | public: 25 | ExecutionPlanEvent(SQLStageEvent *sql_event, Query *sqls); 26 | virtual ~ExecutionPlanEvent(); 27 | 28 | Query *sqls() const 29 | { 30 | return sqls_; 31 | } 32 | 33 | SQLStageEvent *sql_event() const 34 | { 35 | return sql_event_; 36 | } 37 | 38 | private: 39 | SQLStageEvent *sql_event_; 40 | Query *sqls_; 41 | }; 42 | 43 | #endif // __OBSERVER_EVENT_EXECUTION_PLAN_EVENT_H__ -------------------------------------------------------------------------------- /src/observer/event/session_event.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #include "session_event.h" 16 | 17 | SessionEvent::SessionEvent(ConnectionContext *client) : client_(client) 18 | {} 19 | 20 | SessionEvent::~SessionEvent() 21 | {} 22 | 23 | ConnectionContext *SessionEvent::get_client() const 24 | { 25 | return client_; 26 | } 27 | 28 | const char *SessionEvent::get_response() const 29 | { 30 | return response_.c_str(); 31 | } 32 | 33 | void SessionEvent::set_response(const char *response) 34 | { 35 | set_response(response, strlen(response)); 36 | } 37 | 38 | void SessionEvent::set_response(const char *response, int len) 39 | { 40 | response_.assign(response, len); 41 | } 42 | 43 | void SessionEvent::set_response(std::string &&response) 44 | { 45 | response_ = std::move(response); 46 | } 47 | 48 | int SessionEvent::get_response_len() const 49 | { 50 | return response_.size(); 51 | } 52 | 53 | char *SessionEvent::get_request_buf() 54 | { 55 | return client_->buf; 56 | } 57 | 58 | int SessionEvent::get_request_buf_len() 59 | { 60 | return SOCKET_BUFFER_SIZE; 61 | } -------------------------------------------------------------------------------- /src/observer/event/session_event.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #ifndef __OBSERVER_SESSION_SESSIONEVENT_H__ 16 | #define __OBSERVER_SESSION_SESSIONEVENT_H__ 17 | 18 | #include 19 | #include 20 | 21 | #include "common/seda/stage_event.h" 22 | #include "net/connection_context.h" 23 | 24 | class SessionEvent : public common::StageEvent { 25 | public: 26 | SessionEvent(ConnectionContext *client); 27 | virtual ~SessionEvent(); 28 | 29 | ConnectionContext *get_client() const; 30 | 31 | const char *get_response() const; 32 | void set_response(const char *response); 33 | void set_response(const char *response, int len); 34 | void set_response(std::string &&response); 35 | int get_response_len() const; 36 | char *get_request_buf(); 37 | int get_request_buf_len(); 38 | 39 | private: 40 | ConnectionContext *client_; 41 | 42 | std::string response_; 43 | }; 44 | 45 | #endif //__OBSERVER_SESSION_SESSIONEVENT_H__ 46 | -------------------------------------------------------------------------------- /src/observer/event/sql_event.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/14. 13 | // 14 | 15 | #include "event/sql_event.h" 16 | #include "event/session_event.h" 17 | 18 | SQLStageEvent::SQLStageEvent(SessionEvent *event, std::string &sql) : session_event_(event), sql_(sql) 19 | {} 20 | 21 | SQLStageEvent::~SQLStageEvent() noexcept 22 | { 23 | if (session_event_ != nullptr) { 24 | session_event_ = nullptr; 25 | // SessionEvent *session_event = session_event_; 26 | // session_event_ = nullptr; 27 | // session_event->doneImmediate(); 28 | } 29 | } -------------------------------------------------------------------------------- /src/observer/event/sql_event.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/14. 13 | // 14 | 15 | #ifndef __OBSERVER_SQL_EVENT_SQLEVENT_H__ 16 | #define __OBSERVER_SQL_EVENT_SQLEVENT_H__ 17 | 18 | #include "common/seda/stage_event.h" 19 | #include 20 | 21 | class SessionEvent; 22 | 23 | class SQLStageEvent : public common::StageEvent { 24 | public: 25 | SQLStageEvent(SessionEvent *event, std::string &sql); 26 | virtual ~SQLStageEvent() noexcept; 27 | 28 | const std::string &get_sql() const 29 | { 30 | return sql_; 31 | } 32 | 33 | SessionEvent *session_event() const 34 | { 35 | return session_event_; 36 | } 37 | 38 | private: 39 | SessionEvent *session_event_; 40 | std::string &sql_; 41 | // void *context_; 42 | }; 43 | 44 | #endif //__SRC_OBSERVER_SQL_EVENT_SQLEVENT_H__ 45 | -------------------------------------------------------------------------------- /src/observer/event/storage_event.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/14. 13 | // 14 | 15 | #include "event/storage_event.h" 16 | #include "event/execution_plan_event.h" 17 | 18 | StorageEvent::StorageEvent(ExecutionPlanEvent *exe_event) : exe_event_(exe_event) 19 | {} 20 | 21 | StorageEvent::~StorageEvent() 22 | { 23 | exe_event_ = nullptr; 24 | // if (exe_event_ != nullptr) { 25 | // ExecutionPlanEvent *exe_event = exe_event_; 26 | // exe_event->doneImmediate(); 27 | // } 28 | } -------------------------------------------------------------------------------- /src/observer/event/storage_event.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/14. 13 | // 14 | 15 | #ifndef __OBSERVER_SQL_EVENT_STORAGEEVENT_H__ 16 | #define __OBSERVER_SQL_EVENT_STORAGEEVENT_H__ 17 | 18 | #include "common/seda/stage_event.h" 19 | 20 | class ExecutionPlanEvent; 21 | 22 | class StorageEvent : public common::StageEvent { 23 | public: 24 | StorageEvent(ExecutionPlanEvent *exe_event); 25 | virtual ~StorageEvent(); 26 | 27 | ExecutionPlanEvent *exe_event() const 28 | { 29 | return exe_event_; 30 | } 31 | 32 | private: 33 | ExecutionPlanEvent *exe_event_; 34 | }; 35 | 36 | #endif //__OBSERVER_SQL_EVENT_STORAGEEVENT_H__ 37 | -------------------------------------------------------------------------------- /src/observer/ini_setting.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/14. 13 | // 14 | 15 | #ifndef __SRC_OBSERVER_INI_SETTING_H__ 16 | #define __SRC_OBSERVER_INI_SETTING_H__ 17 | 18 | //! this document is used for ini setting 19 | 20 | #define CLIENT_ADDRESS "CLIENT_ADDRESS" 21 | #define MAX_CONNECTION_NUM "MAX_CONNECTION_NUM" 22 | #define MAX_CONNECTION_NUM_DEFAULT 8192 23 | #define PORT "PORT" 24 | #define PORT_DEFAULT 16880 25 | 26 | #define SOCKET_BUFFER_SIZE 8192 27 | 28 | #define SESSION_STAGE_NAME "SessionStage" 29 | #endif //__SRC_OBSERVER_INI_SETTING_H__ 30 | -------------------------------------------------------------------------------- /src/observer/init.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/5/3. 13 | // 14 | 15 | #ifndef __OBSERVER_INIT_H__ 16 | #define __OBSERVER_INIT_H__ 17 | 18 | #include "common/os/process_param.h" 19 | #include "common/conf/ini.h" 20 | 21 | int init(common::ProcessParam *processParam); 22 | void cleanup(); 23 | 24 | #endif //__OBSERVER_INIT_H__ 25 | -------------------------------------------------------------------------------- /src/observer/net/connection_context.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #ifndef __SRC_OBSERVER_NET_CONNECTION_CONTEXT_H__ 16 | #define __SRC_OBSERVER_NET_CONNECTION_CONTEXT_H__ 17 | 18 | #include 19 | #include 20 | 21 | class Session; 22 | 23 | typedef struct _ConnectionContext { 24 | Session *session; 25 | int fd; 26 | struct event read_event; 27 | pthread_mutex_t mutex; 28 | char addr[24]; 29 | char buf[SOCKET_BUFFER_SIZE]; 30 | } ConnectionContext; 31 | 32 | #endif //__SRC_OBSERVER_NET_CONNECTION_CONTEXT_H__ 33 | -------------------------------------------------------------------------------- /src/observer/net/server.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/1. 13 | // 14 | 15 | #ifndef __OBSERVER_NET_SERVER_H__ 16 | #define __OBSERVER_NET_SERVER_H__ 17 | 18 | #include "common/defs.h" 19 | #include "common/metrics/metrics.h" 20 | #include "common/seda/stage.h" 21 | #include "net/connection_context.h" 22 | #include "net/server_param.h" 23 | 24 | class Server { 25 | public: 26 | Server(ServerParam input_server_param); 27 | ~Server(); 28 | 29 | public: 30 | static void init(); 31 | static int send(ConnectionContext *client, const char *buf, int data_len); 32 | 33 | public: 34 | int serve(); 35 | void shutdown(); 36 | 37 | private: 38 | static void accept(int fd, short ev, void *arg); 39 | // close connection 40 | static void close_connection(ConnectionContext *client_context); 41 | static void recv(int fd, short ev, void *arg); 42 | 43 | private: 44 | int set_non_block(int fd); 45 | int start(); 46 | int start_tcp_server(); 47 | int start_unix_socket_server(); 48 | 49 | private: 50 | bool started_; 51 | 52 | int server_socket_; 53 | struct event_base *event_base_; 54 | struct event *listen_ev_; 55 | 56 | ServerParam server_param_; 57 | 58 | static common::Stage *session_stage_; 59 | static common::SimpleTimer *read_socket_metric_; 60 | static common::SimpleTimer *write_socket_metric_; 61 | }; 62 | 63 | class Communicator { 64 | public: 65 | virtual ~Communicator() = default; 66 | virtual int init(const ServerParam &server_param) = 0; 67 | virtual int start() = 0; 68 | virtual int stop() = 0; 69 | }; 70 | 71 | #endif //__OBSERVER_NET_SERVER_H__ 72 | -------------------------------------------------------------------------------- /src/observer/net/server_param.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #ifndef __SRC_OBSERVER_NET_SERVER_PARAM_H__ 16 | #define __SRC_OBSERVER_NET_SERVER_PARAM_H__ 17 | 18 | #include 19 | 20 | class ServerParam { 21 | public: 22 | ServerParam(); 23 | 24 | ServerParam(const ServerParam &other) = default; 25 | ~ServerParam() = default; 26 | 27 | public: 28 | // accpet client's address, default is INADDR_ANY, means accept every address 29 | long listen_addr; 30 | 31 | int max_connection_num; 32 | // server listing port 33 | int port; 34 | 35 | std::string unix_socket_path; 36 | 37 | // 如果使用标准输入输出作为通信条件,就不再监听端口 38 | bool use_unix_socket = false; 39 | }; 40 | 41 | #endif //__SRC_OBSERVER_NET_SERVER_PARAM_H__ 42 | -------------------------------------------------------------------------------- /src/observer/session/session.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Wangyunlai on 2021/5/12. 13 | // 14 | 15 | #include "session/session.h" 16 | #include "storage/trx/trx.h" 17 | 18 | Session &Session::default_session() 19 | { 20 | static Session session; 21 | return session; 22 | } 23 | 24 | Session::Session(const Session &other) : current_db_(other.current_db_) 25 | {} 26 | 27 | Session::~Session() 28 | { 29 | delete trx_; 30 | trx_ = nullptr; 31 | } 32 | 33 | const std::string &Session::get_current_db() const 34 | { 35 | return current_db_; 36 | } 37 | void Session::set_current_db(const std::string &dbname) 38 | { 39 | current_db_ = dbname; 40 | } 41 | 42 | void Session::set_trx_multi_operation_mode(bool multi_operation_mode) 43 | { 44 | trx_multi_operation_mode_ = multi_operation_mode; 45 | } 46 | 47 | bool Session::is_trx_multi_operation_mode() const 48 | { 49 | return trx_multi_operation_mode_; 50 | } 51 | 52 | Trx *Session::current_trx() 53 | { 54 | if (trx_ == nullptr) { 55 | trx_ = new Trx; 56 | } 57 | return trx_; 58 | } 59 | -------------------------------------------------------------------------------- /src/observer/session/session.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Wangyunlai on 2021/5/12. 13 | // 14 | 15 | #ifndef __OBSERVER_SESSION_SESSION_H__ 16 | #define __OBSERVER_SESSION_SESSION_H__ 17 | 18 | #include 19 | 20 | class Trx; 21 | 22 | class Session { 23 | public: 24 | // static Session ¤t(); 25 | static Session &default_session(); 26 | 27 | public: 28 | Session() = default; 29 | ~Session(); 30 | 31 | Session(const Session &other); 32 | void operator=(Session &) = delete; 33 | 34 | const std::string &get_current_db() const; 35 | void set_current_db(const std::string &dbname); 36 | 37 | void set_trx_multi_operation_mode(bool multi_operation_mode); 38 | bool is_trx_multi_operation_mode() const; 39 | 40 | Trx *current_trx(); 41 | 42 | private: 43 | std::string current_db_; 44 | Trx *trx_ = nullptr; 45 | bool trx_multi_operation_mode_ = false; // 当前事务的模式,是否多语句模式. 单语句模式自动提交 46 | }; 47 | 48 | #endif // __OBSERVER_SESSION_SESSION_H__ -------------------------------------------------------------------------------- /src/observer/session/session_stage.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #ifndef __OBSERVER_SESSION_SESSIONSTAGE_H__ 16 | #define __OBSERVER_SESSION_SESSIONSTAGE_H__ 17 | 18 | #include "common/seda/stage.h" 19 | #include "net/connection_context.h" 20 | #include "common/metrics/metrics.h" 21 | 22 | /** 23 | * seda::stage使用说明: 24 | * 这里利用seda的线程池与调度。stage是一个事件处理的几个阶段。 25 | * 目前包括session,parse,execution和storage 26 | * 每个stage使用handleEvent函数处理任务,并且使用StageEvent::pushCallback注册回调函数。 27 | * 这时当调用StageEvent::done(Immediate)时,就会调用该事件注册的回调函数。 28 | */ 29 | class SessionStage : public common::Stage { 30 | public: 31 | ~SessionStage(); 32 | static Stage *make_stage(const std::string &tag); 33 | 34 | protected: 35 | // common function 36 | SessionStage(const char *tag); 37 | bool set_properties() override; 38 | 39 | bool initialize() override; 40 | void cleanup() override; 41 | void handle_event(common::StageEvent *event) override; 42 | void callback_event(common::StageEvent *event, common::CallbackContext *context) override; 43 | 44 | protected: 45 | void handle_input(common::StageEvent *event); 46 | 47 | void handle_request(common::StageEvent *event); 48 | 49 | private: 50 | Stage *resolve_stage_; 51 | common::SimpleTimer *sql_metric_; 52 | static const std::string SQL_METRIC_TAG; 53 | }; 54 | 55 | #endif //__OBSERVER_SESSION_SESSIONSTAGE_H__ 56 | -------------------------------------------------------------------------------- /src/observer/sql/executor/execute_stage.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #ifndef __OBSERVER_SQL_EXECUTE_STAGE_H__ 16 | #define __OBSERVER_SQL_EXECUTE_STAGE_H__ 17 | 18 | #include "common/seda/stage.h" 19 | #include "sql/parser/parse.h" 20 | #include "rc.h" 21 | 22 | class SessionEvent; 23 | 24 | class ExecuteStage : public common::Stage { 25 | public: 26 | ~ExecuteStage(); 27 | static Stage *make_stage(const std::string &tag); 28 | 29 | protected: 30 | // common function 31 | ExecuteStage(const char *tag); 32 | bool set_properties() override; 33 | 34 | bool initialize() override; 35 | void cleanup() override; 36 | void handle_event(common::StageEvent *event) override; 37 | void callback_event(common::StageEvent *event, common::CallbackContext *context) override; 38 | 39 | void handle_request(common::StageEvent *event); 40 | RC do_select(const char *db, Query *sql, SessionEvent *session_event); 41 | 42 | protected: 43 | private: 44 | Stage *default_storage_stage_ = nullptr; 45 | Stage *mem_storage_stage_ = nullptr; 46 | }; 47 | 48 | #endif //__OBSERVER_SQL_EXECUTE_STAGE_H__ 49 | -------------------------------------------------------------------------------- /src/observer/sql/executor/execution_node.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Meiyi & Wangyunlai on 2021/5/14. 13 | // 14 | 15 | #include "sql/executor/execution_node.h" 16 | #include "storage/common/table.h" 17 | #include "common/log/log.h" 18 | 19 | SelectExeNode::SelectExeNode() : table_(nullptr) 20 | {} 21 | 22 | SelectExeNode::~SelectExeNode() 23 | { 24 | for (DefaultConditionFilter *&filter : condition_filters_) { 25 | delete filter; 26 | } 27 | condition_filters_.clear(); 28 | } 29 | 30 | RC SelectExeNode::init( 31 | Trx *trx, Table *table, TupleSchema &&tuple_schema, std::vector &&condition_filters) 32 | { 33 | trx_ = trx; 34 | table_ = table; 35 | tuple_schema_ = tuple_schema; 36 | condition_filters_ = std::move(condition_filters); 37 | return RC::SUCCESS; 38 | } 39 | 40 | void record_reader(const char *data, void *context) 41 | { 42 | TupleRecordConverter *converter = (TupleRecordConverter *)context; 43 | converter->add_record(data); 44 | } 45 | RC SelectExeNode::execute(TupleSet &tuple_set) 46 | { 47 | CompositeConditionFilter condition_filter; 48 | condition_filter.init((const ConditionFilter **)condition_filters_.data(), condition_filters_.size()); 49 | 50 | tuple_set.clear(); 51 | tuple_set.set_schema(tuple_schema_); 52 | TupleRecordConverter converter(table_, tuple_set); 53 | return table_->scan_record(trx_, &condition_filter, -1, (void *)&converter, record_reader); 54 | } -------------------------------------------------------------------------------- /src/observer/sql/executor/execution_node.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Meiyi & Wangyunlai on 2021/5/13. 13 | // 14 | 15 | #ifndef __OBSERVER_SQL_EXECUTOR_EXECUTION_NODE_H_ 16 | #define __OBSERVER_SQL_EXECUTOR_EXECUTION_NODE_H_ 17 | 18 | #include 19 | #include "storage/common/condition_filter.h" 20 | #include "sql/executor/tuple.h" 21 | 22 | class Table; 23 | class Trx; 24 | 25 | class ExecutionNode { 26 | public: 27 | ExecutionNode() = default; 28 | virtual ~ExecutionNode() = default; 29 | 30 | virtual RC execute(TupleSet &tuple_set) = 0; 31 | }; 32 | 33 | class SelectExeNode : public ExecutionNode { 34 | public: 35 | SelectExeNode(); 36 | virtual ~SelectExeNode(); 37 | 38 | RC init( 39 | Trx *trx, Table *table, TupleSchema &&tuple_schema, std::vector &&condition_filters); 40 | 41 | RC execute(TupleSet &tuple_set) override; 42 | 43 | private: 44 | Trx *trx_ = nullptr; 45 | Table *table_; 46 | TupleSchema tuple_schema_; 47 | std::vector condition_filters_; 48 | }; 49 | 50 | #endif //__OBSERVER_SQL_EXECUTOR_EXECUTION_NODE_H_ 51 | -------------------------------------------------------------------------------- /src/observer/sql/executor/value.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | -------------------------------------------------------------------------------- /src/observer/sql/executor/value.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Meiyi & Wangyunlai on 2021/5/14. 13 | // 14 | 15 | #ifndef __OBSERVER_SQL_EXECUTOR_VALUE_H_ 16 | #define __OBSERVER_SQL_EXECUTOR_VALUE_H_ 17 | 18 | #include 19 | 20 | #include 21 | #include 22 | 23 | class TupleValue { 24 | public: 25 | TupleValue() = default; 26 | virtual ~TupleValue() = default; 27 | 28 | virtual void to_string(std::ostream &os) const = 0; 29 | virtual int compare(const TupleValue &other) const = 0; 30 | 31 | private: 32 | }; 33 | 34 | class IntValue : public TupleValue { 35 | public: 36 | explicit IntValue(int value) : value_(value) 37 | {} 38 | 39 | void to_string(std::ostream &os) const override 40 | { 41 | os << value_; 42 | } 43 | 44 | int compare(const TupleValue &other) const override 45 | { 46 | const IntValue &int_other = (const IntValue &)other; 47 | return value_ - int_other.value_; 48 | } 49 | 50 | private: 51 | int value_; 52 | }; 53 | 54 | class FloatValue : public TupleValue { 55 | public: 56 | explicit FloatValue(float value) : value_(value) 57 | {} 58 | 59 | void to_string(std::ostream &os) const override 60 | { 61 | os << value_; 62 | } 63 | 64 | int compare(const TupleValue &other) const override 65 | { 66 | const FloatValue &float_other = (const FloatValue &)other; 67 | float result = value_ - float_other.value_; 68 | if (result > 0) { // 浮点数没有考虑精度问题 69 | return 1; 70 | } 71 | if (result < 0) { 72 | return -1; 73 | } 74 | return 0; 75 | } 76 | 77 | private: 78 | float value_; 79 | }; 80 | 81 | class StringValue : public TupleValue { 82 | public: 83 | StringValue(const char *value, int len) : value_(value, len) 84 | {} 85 | explicit StringValue(const char *value) : value_(value) 86 | {} 87 | 88 | void to_string(std::ostream &os) const override 89 | { 90 | os << value_; 91 | } 92 | 93 | int compare(const TupleValue &other) const override 94 | { 95 | const StringValue &string_other = (const StringValue &)other; 96 | return strcmp(value_.c_str(), string_other.value_.c_str()); 97 | } 98 | 99 | private: 100 | std::string value_; 101 | }; 102 | 103 | #endif //__OBSERVER_SQL_EXECUTOR_VALUE_H_ 104 | -------------------------------------------------------------------------------- /src/observer/sql/optimizer/optimize_stage.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #include 16 | #include 17 | 18 | #include "optimize_stage.h" 19 | 20 | #include "common/conf/ini.h" 21 | #include "common/io/io.h" 22 | #include "common/lang/string.h" 23 | #include "common/log/log.h" 24 | #include "common/seda/timer_stage.h" 25 | 26 | using namespace common; 27 | 28 | //! Constructor 29 | OptimizeStage::OptimizeStage(const char *tag) : Stage(tag) 30 | {} 31 | 32 | //! Destructor 33 | OptimizeStage::~OptimizeStage() 34 | {} 35 | 36 | //! Parse properties, instantiate a stage object 37 | Stage *OptimizeStage::make_stage(const std::string &tag) 38 | { 39 | OptimizeStage *stage = new (std::nothrow) OptimizeStage(tag.c_str()); 40 | if (stage == nullptr) { 41 | LOG_ERROR("new OptimizeStage failed"); 42 | return nullptr; 43 | } 44 | stage->set_properties(); 45 | return stage; 46 | } 47 | 48 | //! Set properties for this object set in stage specific properties 49 | bool OptimizeStage::set_properties() 50 | { 51 | // std::string stageNameStr(stage_name_); 52 | // std::map section = g_properties()->get( 53 | // stageNameStr); 54 | // 55 | // std::map::iterator it; 56 | // 57 | // std::string key; 58 | 59 | return true; 60 | } 61 | 62 | //! Initialize stage params and validate outputs 63 | bool OptimizeStage::initialize() 64 | { 65 | LOG_TRACE("Enter"); 66 | 67 | std::list::iterator stgp = next_stage_list_.begin(); 68 | execute_stage = *(stgp++); 69 | 70 | LOG_TRACE("Exit"); 71 | return true; 72 | } 73 | 74 | //! Cleanup after disconnection 75 | void OptimizeStage::cleanup() 76 | { 77 | LOG_TRACE("Enter"); 78 | 79 | LOG_TRACE("Exit"); 80 | } 81 | 82 | void OptimizeStage::handle_event(StageEvent *event) 83 | { 84 | LOG_TRACE("Enter\n"); 85 | 86 | // optimize sql plan, here just pass the event to the next stage 87 | execute_stage->handle_event(event); 88 | 89 | LOG_TRACE("Exit\n"); 90 | return; 91 | } 92 | 93 | void OptimizeStage::callback_event(StageEvent *event, CallbackContext *context) 94 | { 95 | LOG_TRACE("Enter\n"); 96 | 97 | LOG_TRACE("Exit\n"); 98 | return; 99 | } 100 | -------------------------------------------------------------------------------- /src/observer/sql/optimizer/optimize_stage.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #ifndef __OBSERVER_SQL_OPTIMIZE_STAGE_H__ 16 | #define __OBSERVER_SQL_OPTIMIZE_STAGE_H__ 17 | 18 | #include "common/seda/stage.h" 19 | 20 | class OptimizeStage : public common::Stage { 21 | public: 22 | ~OptimizeStage(); 23 | static Stage *make_stage(const std::string &tag); 24 | 25 | protected: 26 | // common function 27 | OptimizeStage(const char *tag); 28 | bool set_properties(); 29 | 30 | bool initialize(); 31 | void cleanup(); 32 | void handle_event(common::StageEvent *event); 33 | void callback_event(common::StageEvent *event, common::CallbackContext *context); 34 | 35 | protected: 36 | private: 37 | Stage *execute_stage = nullptr; 38 | }; 39 | 40 | #endif //__OBSERVER_SQL_OPTIMIZE_STAGE_H__ 41 | -------------------------------------------------------------------------------- /src/observer/sql/parser/parse.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Meiyi 13 | // 14 | 15 | #ifndef __OBSERVER_SQL_PARSER_PARSE_H__ 16 | #define __OBSERVER_SQL_PARSER_PARSE_H__ 17 | 18 | #include "rc.h" 19 | #include "sql/parser/parse_defs.h" 20 | 21 | RC parse(const char *st, Query *sqln); 22 | 23 | #endif //__OBSERVER_SQL_PARSER_PARSE_H__ 24 | -------------------------------------------------------------------------------- /src/observer/sql/parser/parse_stage.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #ifndef __OBSERVER_SQL_PARSE_STAGE_H__ 16 | #define __OBSERVER_SQL_PARSE_STAGE_H__ 17 | 18 | #include "common/seda/stage.h" 19 | 20 | class ParseStage : public common::Stage { 21 | public: 22 | ~ParseStage(); 23 | static Stage *make_stage(const std::string &tag); 24 | 25 | protected: 26 | // common function 27 | ParseStage(const char *tag); 28 | bool set_properties(); 29 | 30 | bool initialize(); 31 | void cleanup(); 32 | void handle_event(common::StageEvent *event); 33 | void callback_event(common::StageEvent *event, common::CallbackContext *context); 34 | 35 | protected: 36 | common::StageEvent *handle_request(common::StageEvent *event); 37 | 38 | private: 39 | Stage *optimize_stage_ = nullptr; 40 | }; 41 | 42 | #endif //__OBSERVER_SQL_PARSE_STAGE_H__ 43 | -------------------------------------------------------------------------------- /src/observer/sql/parser/resolve_stage.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #include 16 | #include 17 | 18 | #include "resolve_stage.h" 19 | 20 | #include "common/conf/ini.h" 21 | #include "common/io/io.h" 22 | #include "common/lang/string.h" 23 | #include "common/log/log.h" 24 | #include "common/seda/timer_stage.h" 25 | #include "event/sql_event.h" 26 | 27 | using namespace common; 28 | 29 | //! Constructor 30 | ResolveStage::ResolveStage(const char *tag) : Stage(tag) 31 | {} 32 | 33 | //! Destructor 34 | ResolveStage::~ResolveStage() 35 | {} 36 | 37 | //! Parse properties, instantiate a stage object 38 | Stage *ResolveStage::make_stage(const std::string &tag) 39 | { 40 | ResolveStage *stage = new (std::nothrow) ResolveStage(tag.c_str()); 41 | if (stage == nullptr) { 42 | LOG_ERROR("new ResolveStage failed"); 43 | return nullptr; 44 | } 45 | stage->set_properties(); 46 | return stage; 47 | } 48 | 49 | //! Set properties for this object set in stage specific properties 50 | bool ResolveStage::set_properties() 51 | { 52 | // std::string stageNameStr(stage_name_); 53 | // std::map section = g_properties()->get( 54 | // stageNameStr); 55 | // 56 | // std::map::iterator it; 57 | // 58 | // std::string key; 59 | 60 | return true; 61 | } 62 | 63 | //! Initialize stage params and validate outputs 64 | bool ResolveStage::initialize() 65 | { 66 | LOG_TRACE("Enter"); 67 | 68 | std::list::iterator stgp = next_stage_list_.begin(); 69 | query_cache_stage = *(stgp++); 70 | 71 | LOG_TRACE("Exit"); 72 | return true; 73 | } 74 | 75 | //! Cleanup after disconnection 76 | void ResolveStage::cleanup() 77 | { 78 | LOG_TRACE("Enter"); 79 | 80 | LOG_TRACE("Exit"); 81 | } 82 | 83 | void ResolveStage::handle_event(StageEvent *event) 84 | { 85 | LOG_TRACE("Enter\n"); 86 | 87 | SQLStageEvent *sql_event = static_cast(event); 88 | 89 | // do nothing here 90 | query_cache_stage->handle_event(sql_event); 91 | 92 | LOG_TRACE("Exit\n"); 93 | return; 94 | } 95 | 96 | void ResolveStage::callback_event(StageEvent *event, CallbackContext *context) 97 | { 98 | LOG_TRACE("Enter\n"); 99 | 100 | LOG_TRACE("Exit\n"); 101 | return; 102 | } 103 | -------------------------------------------------------------------------------- /src/observer/sql/parser/resolve_stage.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #ifndef __OBSERVER_SQL_RESOLVE_STAGE_H__ 16 | #define __OBSERVER_SQL_RESOLVE_STAGE_H__ 17 | 18 | #include "common/seda/stage.h" 19 | 20 | class ResolveStage : public common::Stage { 21 | public: 22 | ~ResolveStage(); 23 | static Stage *make_stage(const std::string &tag); 24 | 25 | protected: 26 | // common function 27 | ResolveStage(const char *tag); 28 | bool set_properties(); 29 | 30 | bool initialize(); 31 | void cleanup(); 32 | void handle_event(common::StageEvent *event); 33 | void callback_event(common::StageEvent *event, common::CallbackContext *context); 34 | 35 | protected: 36 | private: 37 | Stage *query_cache_stage = nullptr; 38 | }; 39 | 40 | #endif //__OBSERVER_SQL_RESOLVE_STAGE_H__ 41 | -------------------------------------------------------------------------------- /src/observer/sql/plan_cache/plan_cache_stage.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #include 16 | #include 17 | 18 | #include "plan_cache_stage.h" 19 | 20 | #include "common/conf/ini.h" 21 | #include "common/io/io.h" 22 | #include "common/lang/string.h" 23 | #include "common/log/log.h" 24 | #include "common/seda/timer_stage.h" 25 | 26 | using namespace common; 27 | 28 | //! Constructor 29 | PlanCacheStage::PlanCacheStage(const char *tag) : Stage(tag) 30 | {} 31 | 32 | //! Destructor 33 | PlanCacheStage::~PlanCacheStage() 34 | {} 35 | 36 | //! Parse properties, instantiate a stage object 37 | Stage *PlanCacheStage::make_stage(const std::string &tag) 38 | { 39 | PlanCacheStage *stage = new (std::nothrow) PlanCacheStage(tag.c_str()); 40 | if (stage == nullptr) { 41 | LOG_ERROR("new PlanCacheStage failed"); 42 | return nullptr; 43 | } 44 | stage->set_properties(); 45 | return stage; 46 | } 47 | 48 | //! Set properties for this object set in stage specific properties 49 | bool PlanCacheStage::set_properties() 50 | { 51 | // std::string stageNameStr(stage_name_); 52 | // std::map section = g_properties()->get( 53 | // stageNameStr); 54 | // 55 | // std::map::iterator it; 56 | // 57 | // std::string key; 58 | 59 | return true; 60 | } 61 | 62 | //! Initialize stage params and validate outputs 63 | bool PlanCacheStage::initialize() 64 | { 65 | LOG_TRACE("Enter"); 66 | 67 | std::list::iterator stgp = next_stage_list_.begin(); 68 | execute_stage = *(stgp++); 69 | parse_stage = *(stgp++); 70 | 71 | LOG_TRACE("Exit"); 72 | return true; 73 | } 74 | 75 | //! Cleanup after disconnection 76 | void PlanCacheStage::cleanup() 77 | { 78 | LOG_TRACE("Enter"); 79 | 80 | LOG_TRACE("Exit"); 81 | } 82 | 83 | void PlanCacheStage::handle_event(StageEvent *event) 84 | { 85 | LOG_TRACE("Enter\n"); 86 | 87 | // Add callback to update plan cache 88 | /* 89 | CompletionCallback *cb = new (std::nothrow) CompletionCallback(this, nullptr); 90 | if (cb == nullptr) { 91 | LOG_ERROR("Failed to new callback for SQLStageEvent"); 92 | event->done_immediate(); 93 | return; 94 | } 95 | 96 | event->push_callback(cb); 97 | */ 98 | // do nothing here, pass the event to the next stage 99 | parse_stage->handle_event(event); 100 | 101 | LOG_TRACE("Exit\n"); 102 | return; 103 | } 104 | 105 | void PlanCacheStage::callback_event(StageEvent *event, CallbackContext *context) 106 | { 107 | LOG_TRACE("Enter\n"); 108 | 109 | // update execute plan here 110 | // event->done_immediate(); 111 | 112 | LOG_TRACE("Exit\n"); 113 | return; 114 | } 115 | -------------------------------------------------------------------------------- /src/observer/sql/plan_cache/plan_cache_stage.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #ifndef __OBSERVER_SQL_PLAN_CACHE_STAGE_H__ 16 | #define __OBSERVER_SQL_PLAN_CACHE_STAGE_H__ 17 | 18 | #include "common/seda/stage.h" 19 | 20 | class PlanCacheStage : public common::Stage { 21 | public: 22 | ~PlanCacheStage(); 23 | static Stage *make_stage(const std::string &tag); 24 | 25 | protected: 26 | // common function 27 | PlanCacheStage(const char *tag); 28 | bool set_properties(); 29 | 30 | bool initialize(); 31 | void cleanup(); 32 | void handle_event(common::StageEvent *event); 33 | void callback_event(common::StageEvent *event, common::CallbackContext *context); 34 | 35 | protected: 36 | private: 37 | Stage *parse_stage = nullptr; 38 | Stage *execute_stage = nullptr; 39 | }; 40 | 41 | #endif //__OBSERVER_SQL_PLAN_CACHE_STAGE_H__ 42 | -------------------------------------------------------------------------------- /src/observer/sql/query_cache/query_cache_stage.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #include 16 | #include 17 | 18 | #include "query_cache_stage.h" 19 | 20 | #include "common/conf/ini.h" 21 | #include "common/io/io.h" 22 | #include "common/lang/string.h" 23 | #include "common/log/log.h" 24 | #include "common/seda/timer_stage.h" 25 | 26 | using namespace common; 27 | 28 | //! Constructor 29 | QueryCacheStage::QueryCacheStage(const char *tag) : Stage(tag) 30 | {} 31 | 32 | //! Destructor 33 | QueryCacheStage::~QueryCacheStage() 34 | {} 35 | 36 | //! Parse properties, instantiate a stage object 37 | Stage *QueryCacheStage::make_stage(const std::string &tag) 38 | { 39 | QueryCacheStage *stage = new (std::nothrow) QueryCacheStage(tag.c_str()); 40 | if (stage == nullptr) { 41 | LOG_ERROR("new QueryCacheStage failed"); 42 | return nullptr; 43 | } 44 | stage->set_properties(); 45 | return stage; 46 | } 47 | 48 | //! Set properties for this object set in stage specific properties 49 | bool QueryCacheStage::set_properties() 50 | { 51 | // std::string stageNameStr(stage_name_); 52 | // std::map section = g_properties()->get( 53 | // stageNameStr); 54 | // 55 | // std::map::iterator it; 56 | // 57 | // std::string key; 58 | 59 | return true; 60 | } 61 | 62 | //! Initialize stage params and validate outputs 63 | bool QueryCacheStage::initialize() 64 | { 65 | LOG_TRACE("Enter"); 66 | 67 | std::list::iterator stgp = next_stage_list_.begin(); 68 | plan_cache_stage = *(stgp++); 69 | 70 | LOG_TRACE("Exit"); 71 | return true; 72 | } 73 | 74 | //! Cleanup after disconnection 75 | void QueryCacheStage::cleanup() 76 | { 77 | LOG_TRACE("Enter"); 78 | 79 | LOG_TRACE("Exit"); 80 | } 81 | 82 | void QueryCacheStage::handle_event(StageEvent *event) 83 | { 84 | LOG_TRACE("Enter\n"); 85 | 86 | // Add callback to update query cache 87 | /* 88 | CompletionCallback *cb = new (std::nothrow) CompletionCallback(this, nullptr); 89 | if (cb == nullptr) { 90 | LOG_ERROR("Failed to new callback for SQLStageEvent"); 91 | event->done_immediate(); 92 | return; 93 | } 94 | 95 | event->push_callback(cb); 96 | */ 97 | // do nothing here, pass the event to the next stage 98 | plan_cache_stage->handle_event(event); 99 | 100 | LOG_TRACE("Exit\n"); 101 | return; 102 | } 103 | 104 | void QueryCacheStage::callback_event(StageEvent *event, CallbackContext *context) 105 | { 106 | LOG_TRACE("Enter\n"); 107 | 108 | // update data to query cache here 109 | // event->done_immediate(); 110 | 111 | LOG_TRACE("Exit\n"); 112 | return; 113 | } 114 | -------------------------------------------------------------------------------- /src/observer/sql/query_cache/query_cache_stage.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #ifndef __OBSERVER_SQL_QUERY_CACHE_STAGE_H__ 16 | #define __OBSERVER_SQL_QUERY_CACHE_STAGE_H__ 17 | 18 | #include "common/seda/stage.h" 19 | 20 | class QueryCacheStage : public common::Stage { 21 | public: 22 | ~QueryCacheStage(); 23 | static Stage *make_stage(const std::string &tag); 24 | 25 | protected: 26 | // common function 27 | QueryCacheStage(const char *tag); 28 | bool set_properties(); 29 | 30 | bool initialize(); 31 | void cleanup(); 32 | void handle_event(common::StageEvent *event); 33 | void callback_event(common::StageEvent *event, common::CallbackContext *context); 34 | 35 | protected: 36 | private: 37 | Stage *plan_cache_stage = nullptr; 38 | }; 39 | 40 | #endif //__OBSERVER_SQL_QUERY_CACHE_STAGE_H__ 41 | -------------------------------------------------------------------------------- /src/observer/storage/common/bplus_tree_index.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Meiyi & wangyunlai.wyl on 2021/5/19. 13 | // 14 | 15 | #ifndef __OBSERVER_STORAGE_COMMON_BPLUS_TREE_INDEX_H_ 16 | #define __OBSERVER_STORAGE_COMMON_BPLUS_TREE_INDEX_H_ 17 | 18 | #include "storage/common/index.h" 19 | #include "storage/common/bplus_tree.h" 20 | 21 | class BplusTreeIndex : public Index { 22 | public: 23 | BplusTreeIndex() = default; 24 | virtual ~BplusTreeIndex() noexcept; 25 | 26 | RC create(const char *file_name, const IndexMeta &index_meta, const FieldMeta &field_meta); 27 | RC open(const char *file_name, const IndexMeta &index_meta, const FieldMeta &field_meta); 28 | RC close(); 29 | 30 | RC insert_entry(const char *record, const RID *rid) override; 31 | RC delete_entry(const char *record, const RID *rid) override; 32 | 33 | IndexScanner *create_scanner(CompOp comp_op, const char *value) override; 34 | 35 | RC sync() override; 36 | 37 | private: 38 | bool inited_ = false; 39 | BplusTreeHandler index_handler_; 40 | }; 41 | 42 | class BplusTreeIndexScanner : public IndexScanner { 43 | public: 44 | BplusTreeIndexScanner(BplusTreeScanner *tree_scanner); 45 | ~BplusTreeIndexScanner() noexcept override; 46 | 47 | RC next_entry(RID *rid) override; 48 | RC destroy() override; 49 | 50 | private: 51 | BplusTreeScanner *tree_scanner_; 52 | }; 53 | 54 | #endif //__OBSERVER_STORAGE_COMMON_BPLUS_TREE_INDEX_H_ 55 | -------------------------------------------------------------------------------- /src/observer/storage/common/condition_filter.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Meiyi & Wangyunlai on 2021/5/7. 13 | // 14 | 15 | #ifndef __OBSERVER_STORAGE_COMMON_CONDITION_FILTER_H_ 16 | #define __OBSERVER_STORAGE_COMMON_CONDITION_FILTER_H_ 17 | 18 | #include "rc.h" 19 | #include "sql/parser/parse.h" 20 | 21 | struct Record; 22 | class Table; 23 | 24 | struct ConDesc { 25 | bool is_attr; // 是否属性,false 表示是值 26 | int attr_length; // 如果是属性,表示属性值长度 27 | int attr_offset; // 如果是属性,表示在记录中的偏移量 28 | void *value; // 如果是值类型,这里记录值的数据 29 | }; 30 | 31 | class ConditionFilter { 32 | public: 33 | virtual ~ConditionFilter(); 34 | 35 | /** 36 | * Filter one record 37 | * @param rec 38 | * @return true means match condition, false means failed to match. 39 | */ 40 | virtual bool filter(const Record &rec) const = 0; 41 | }; 42 | 43 | class DefaultConditionFilter : public ConditionFilter { 44 | public: 45 | DefaultConditionFilter(); 46 | virtual ~DefaultConditionFilter(); 47 | 48 | RC init(const ConDesc &left, const ConDesc &right, AttrType attr_type, CompOp comp_op); 49 | RC init(Table &table, const Condition &condition); 50 | 51 | virtual bool filter(const Record &rec) const; 52 | 53 | public: 54 | const ConDesc &left() const 55 | { 56 | return left_; 57 | } 58 | 59 | const ConDesc &right() const 60 | { 61 | return right_; 62 | } 63 | 64 | CompOp comp_op() const 65 | { 66 | return comp_op_; 67 | } 68 | 69 | private: 70 | ConDesc left_; 71 | ConDesc right_; 72 | AttrType attr_type_ = UNDEFINED; 73 | CompOp comp_op_ = NO_OP; 74 | }; 75 | 76 | class CompositeConditionFilter : public ConditionFilter { 77 | public: 78 | CompositeConditionFilter() = default; 79 | virtual ~CompositeConditionFilter(); 80 | 81 | RC init(const ConditionFilter *filters[], int filter_num); 82 | RC init(Table &table, const Condition *conditions, int condition_num); 83 | virtual bool filter(const Record &rec) const; 84 | 85 | public: 86 | int filter_num() const 87 | { 88 | return filter_num_; 89 | } 90 | const ConditionFilter &filter(int index) const 91 | { 92 | return *filters_[index]; 93 | } 94 | 95 | private: 96 | RC init(const ConditionFilter *filters[], int filter_num, bool own_memory); 97 | 98 | private: 99 | const ConditionFilter **filters_ = nullptr; 100 | int filter_num_ = 0; 101 | bool memory_owner_ = false; // filters_的内存是否由自己来控制 102 | }; 103 | 104 | #endif // __OBSERVER_STORAGE_COMMON_CONDITION_FILTER_H_ -------------------------------------------------------------------------------- /src/observer/storage/common/db.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Meiyi & Longda & Wangyunlai on 2021/5/12. 13 | // 14 | 15 | #ifndef __OBSERVER_STORAGE_COMMON_DB_H__ 16 | #define __OBSERVER_STORAGE_COMMON_DB_H__ 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #include "rc.h" 23 | #include "sql/parser/parse_defs.h" 24 | 25 | class Table; 26 | 27 | class Db { 28 | public: 29 | Db() = default; 30 | ~Db(); 31 | 32 | RC init(const char *name, const char *dbpath); 33 | 34 | RC create_table(const char *table_name, int attribute_count, const AttrInfo *attributes); 35 | 36 | RC drop_table(const char* table_name); 37 | 38 | Table *find_table(const char *table_name) const; 39 | 40 | const char *name() const; 41 | 42 | void all_tables(std::vector &table_names) const; 43 | 44 | RC sync(); 45 | 46 | private: 47 | RC open_all_tables(); 48 | 49 | private: 50 | std::string name_; 51 | std::string path_; 52 | std::unordered_map opened_tables_; 53 | }; 54 | 55 | #endif // __OBSERVER_STORAGE_COMMON_DB_H__ -------------------------------------------------------------------------------- /src/observer/storage/common/field_meta.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Meiyi & Wangyunlai on 2021/5/12. 13 | // 14 | 15 | #ifndef __OBSERVER_STORAGE_COMMON_FIELD_META_H__ 16 | #define __OBSERVER_STORAGE_COMMON_FIELD_META_H__ 17 | 18 | #include 19 | 20 | #include "rc.h" 21 | #include "sql/parser/parse_defs.h" 22 | 23 | namespace Json { 24 | class Value; 25 | } // namespace Json 26 | 27 | // Take care of shallow copy 28 | class FieldMeta { 29 | public: 30 | FieldMeta(); 31 | ~FieldMeta() = default; 32 | 33 | RC init(const char *name, AttrType attr_type, int attr_offset, int attr_len, bool visible); 34 | 35 | public: 36 | const char *name() const; 37 | AttrType type() const; 38 | int offset() const; 39 | int len() const; 40 | bool visible() const; 41 | 42 | public: 43 | void desc(std::ostream &os) const; 44 | 45 | public: 46 | void to_json(Json::Value &json_value) const; 47 | static RC from_json(const Json::Value &json_value, FieldMeta &field); 48 | 49 | protected: 50 | std::string name_; 51 | AttrType attr_type_; 52 | int attr_offset_; 53 | int attr_len_; 54 | bool visible_; 55 | }; 56 | #endif // __OBSERVER_STORAGE_COMMON_FIELD_META_H__ -------------------------------------------------------------------------------- /src/observer/storage/common/index.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Meiyi & wangyunlai.wyl on 2021/5/19. 13 | // 14 | 15 | #include "storage/common/index.h" 16 | 17 | RC Index::init(const IndexMeta &index_meta, const FieldMeta &field_meta) 18 | { 19 | index_meta_ = index_meta; 20 | field_meta_ = field_meta; 21 | return RC::SUCCESS; 22 | } -------------------------------------------------------------------------------- /src/observer/storage/common/index.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Meiyi & Wangyunlai on 2021/5/11. 13 | // 14 | 15 | #ifndef __OBSERVER_STORAGE_COMMON_INDEX_H_ 16 | #define __OBSERVER_STORAGE_COMMON_INDEX_H_ 17 | 18 | #include 19 | #include 20 | 21 | #include "rc.h" 22 | #include "storage/common/index_meta.h" 23 | #include "storage/common/field_meta.h" 24 | #include "storage/common/record_manager.h" 25 | 26 | class IndexDataOperator { 27 | public: 28 | virtual ~IndexDataOperator() = default; 29 | virtual int compare(const void *data1, const void *data2) const = 0; 30 | virtual size_t hash(const void *data) const = 0; 31 | }; 32 | 33 | class IndexScanner; 34 | 35 | class Index { 36 | 37 | public: 38 | Index() = default; 39 | virtual ~Index() = default; 40 | 41 | const IndexMeta &index_meta() const 42 | { 43 | return index_meta_; 44 | } 45 | 46 | virtual RC insert_entry(const char *record, const RID *rid) = 0; 47 | virtual RC delete_entry(const char *record, const RID *rid) = 0; 48 | 49 | virtual IndexScanner *create_scanner(CompOp comp_op, const char *value) = 0; 50 | 51 | virtual RC sync() = 0; 52 | 53 | protected: 54 | RC init(const IndexMeta &index_meta, const FieldMeta &field_meta); 55 | 56 | protected: 57 | IndexMeta index_meta_; 58 | FieldMeta field_meta_; /// 当前实现仅考虑一个字段的索引 59 | }; 60 | 61 | class IndexScanner { 62 | public: 63 | IndexScanner() = default; 64 | virtual ~IndexScanner() = default; 65 | 66 | virtual RC next_entry(RID *rid) = 0; 67 | virtual RC destroy() = 0; 68 | }; 69 | 70 | #endif // __OBSERVER_STORAGE_COMMON_INDEX_H_ -------------------------------------------------------------------------------- /src/observer/storage/common/index_meta.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Meiyi & Wangyunlai.wyl on 2021/5/18. 13 | // 14 | 15 | #include "storage/common/index_meta.h" 16 | #include "storage/common/field_meta.h" 17 | #include "storage/common/table_meta.h" 18 | #include "common/lang/string.h" 19 | #include "common/log/log.h" 20 | #include "rc.h" 21 | #include "json/json.h" 22 | 23 | const static Json::StaticString FIELD_NAME("name"); 24 | const static Json::StaticString FIELD_FIELD_NAME("field_name"); 25 | 26 | RC IndexMeta::init(const char *name, const FieldMeta &field) 27 | { 28 | if (common::is_blank(name)) { 29 | LOG_ERROR("Failed to init index, name is empty."); 30 | return RC::INVALID_ARGUMENT; 31 | } 32 | 33 | name_ = name; 34 | field_ = field.name(); 35 | return RC::SUCCESS; 36 | } 37 | 38 | void IndexMeta::to_json(Json::Value &json_value) const 39 | { 40 | json_value[FIELD_NAME] = name_; 41 | json_value[FIELD_FIELD_NAME] = field_; 42 | } 43 | 44 | RC IndexMeta::from_json(const TableMeta &table, const Json::Value &json_value, IndexMeta &index) 45 | { 46 | const Json::Value &name_value = json_value[FIELD_NAME]; 47 | const Json::Value &field_value = json_value[FIELD_FIELD_NAME]; 48 | if (!name_value.isString()) { 49 | LOG_ERROR("Index name is not a string. json value=%s", name_value.toStyledString().c_str()); 50 | return RC::GENERIC_ERROR; 51 | } 52 | 53 | if (!field_value.isString()) { 54 | LOG_ERROR("Field name of index [%s] is not a string. json value=%s", 55 | name_value.asCString(), 56 | field_value.toStyledString().c_str()); 57 | return RC::GENERIC_ERROR; 58 | } 59 | 60 | const FieldMeta *field = table.field(field_value.asCString()); 61 | if (nullptr == field) { 62 | LOG_ERROR("Deserialize index [%s]: no such field: %s", name_value.asCString(), field_value.asCString()); 63 | return RC::SCHEMA_FIELD_MISSING; 64 | } 65 | 66 | return index.init(name_value.asCString(), *field); 67 | } 68 | 69 | const char *IndexMeta::name() const 70 | { 71 | return name_.c_str(); 72 | } 73 | 74 | const char *IndexMeta::field() const 75 | { 76 | return field_.c_str(); 77 | } 78 | 79 | void IndexMeta::desc(std::ostream &os) const 80 | { 81 | os << "index name=" << name_ << ", field=" << field_; 82 | } -------------------------------------------------------------------------------- /src/observer/storage/common/index_meta.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Meiyi & Wangyunlai on 2021/5/12. 13 | // 14 | 15 | #ifndef __OBSERVER_STORAGE_COMMON_INDEX_META_H__ 16 | #define __OBSERVER_STORAGE_COMMON_INDEX_META_H__ 17 | 18 | #include 19 | #include "rc.h" 20 | 21 | class TableMeta; 22 | class FieldMeta; 23 | 24 | namespace Json { 25 | class Value; 26 | } // namespace Json 27 | 28 | class IndexMeta { 29 | public: 30 | IndexMeta() = default; 31 | 32 | RC init(const char *name, const FieldMeta &field); 33 | 34 | public: 35 | const char *name() const; 36 | const char *field() const; 37 | 38 | void desc(std::ostream &os) const; 39 | 40 | public: 41 | void to_json(Json::Value &json_value) const; 42 | static RC from_json(const TableMeta &table, const Json::Value &json_value, IndexMeta &index); 43 | 44 | protected: 45 | std::string name_; // index's name 46 | std::string field_; // field's name 47 | }; 48 | #endif // __OBSERVER_STORAGE_COMMON_INDEX_META_H__ -------------------------------------------------------------------------------- /src/observer/storage/common/meta_util.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // Created by Meiyi & wangyunlai.wyl on 2021/5/18. 12 | // 13 | 14 | #include "common/defs.h" 15 | #include "storage/common/meta_util.h" 16 | 17 | std::string table_meta_file(const char *base_dir, const char *table_name) 18 | { 19 | return std::string(base_dir) + common::FILE_PATH_SPLIT_STR + table_name + TABLE_META_SUFFIX; 20 | } 21 | std::string table_data_file(const char *base_dir, const char *table_name) 22 | { 23 | return std::string(base_dir) + common::FILE_PATH_SPLIT_STR + table_name + TABLE_DATA_SUFFIX; 24 | } 25 | 26 | std::string table_index_file(const char *base_dir, const char *table_name, const char *index_name) 27 | { 28 | return std::string(base_dir) + common::FILE_PATH_SPLIT_STR + table_name + "-" + index_name + TABLE_INDEX_SUFFIX; 29 | } 30 | -------------------------------------------------------------------------------- /src/observer/storage/common/meta_util.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // Created by Meiyi & wangyunlai.wyl on 2021/5/18. 12 | // 13 | 14 | #ifndef __OBSERVER_STORAGE_COMMON_META_UTIL_H_ 15 | #define __OBSERVER_STORAGE_COMMON_META_UTIL_H_ 16 | 17 | #include 18 | 19 | static const char *TABLE_META_SUFFIX = ".table"; 20 | static const char *TABLE_META_FILE_PATTERN = ".*\\.table$"; 21 | static const char *TABLE_DATA_SUFFIX = ".data"; 22 | static const char *TABLE_INDEX_SUFFIX = ".index"; 23 | 24 | std::string table_meta_file(const char *base_dir, const char *table_name); 25 | std::string table_data_file(const char *base_dir, const char *table_name); 26 | std::string table_index_file(const char *base_dir, const char *table_name, const char *index_name); 27 | 28 | #endif //__OBSERVER_STORAGE_COMMON_META_UTIL_H_ 29 | -------------------------------------------------------------------------------- /src/observer/storage/common/table_meta.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Meiyi & Wangyunlai on 2021/5/12. 13 | // 14 | 15 | #ifndef __OBSERVER_STORAGE_COMMON_TABLE_META_H__ 16 | #define __OBSERVER_STORAGE_COMMON_TABLE_META_H__ 17 | 18 | #include 19 | #include 20 | 21 | #include "rc.h" 22 | #include "storage/common/field_meta.h" 23 | #include "storage/common/index_meta.h" 24 | #include "common/lang/serializable.h" 25 | 26 | class TableMeta : public common::Serializable { 27 | public: 28 | TableMeta() = default; 29 | ~TableMeta() = default; 30 | 31 | TableMeta(const TableMeta &other); 32 | 33 | void swap(TableMeta &other) noexcept; 34 | 35 | RC init(const char *name, int field_num, const AttrInfo attributes[]); 36 | 37 | RC add_index(const IndexMeta &index); 38 | 39 | public: 40 | const char *name() const; 41 | const FieldMeta *trx_field() const; 42 | const FieldMeta *field(int index) const; 43 | const FieldMeta *field(const char *name) const; 44 | const FieldMeta *find_field_by_offset(int offset) const; 45 | int field_num() const; 46 | int sys_field_num() const; 47 | 48 | const IndexMeta *index(const char *name) const; 49 | const IndexMeta *find_index_by_field(const char *field) const; 50 | const IndexMeta *index(int i) const; 51 | int index_num() const; 52 | 53 | int record_size() const; 54 | 55 | public: 56 | int serialize(std::ostream &os) const override; 57 | int deserialize(std::istream &is) override; 58 | int get_serial_size() const override; 59 | void to_string(std::string &output) const override; 60 | void desc(std::ostream &os) const; 61 | 62 | protected: 63 | static RC init_sys_fields(); 64 | 65 | protected: 66 | std::string name_; 67 | std::vector fields_; // 包含sys_fields 68 | std::vector indexes_; 69 | 70 | int record_size_ = 0; 71 | 72 | //@@@ TODO why used static variable? 73 | static std::vector sys_fields_; 74 | }; 75 | 76 | #endif // __OBSERVER_STORAGE_COMMON_TABLE_META_H__ -------------------------------------------------------------------------------- /src/observer/storage/default/default_storage_stage.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Meiyi & Longda on 2021/4/13. 13 | // 14 | 15 | #ifndef __OBSERVER_STORAGE_DEFAULT_STORAGE_STAGE_H__ 16 | #define __OBSERVER_STORAGE_DEFAULT_STORAGE_STAGE_H__ 17 | 18 | #include "common/seda/stage.h" 19 | #include "common/metrics/metrics.h" 20 | 21 | class DefaultHandler; 22 | 23 | class DefaultStorageStage : public common::Stage { 24 | public: 25 | ~DefaultStorageStage(); 26 | static Stage *make_stage(const std::string &tag); 27 | 28 | protected: 29 | // common function 30 | DefaultStorageStage(const char *tag); 31 | bool set_properties() override; 32 | 33 | bool initialize() override; 34 | void cleanup() override; 35 | void handle_event(common::StageEvent *event) override; 36 | void callback_event(common::StageEvent *event, common::CallbackContext *context) override; 37 | 38 | private: 39 | std::string load_data(const char *db_name, const char *table_name, const char *file_name); 40 | 41 | protected: 42 | common::SimpleTimer *query_metric_ = nullptr; 43 | static const std::string QUERY_METRIC_TAG; 44 | 45 | private: 46 | DefaultHandler *handler_; 47 | }; 48 | 49 | #endif //__OBSERVER_STORAGE_DEFAULT_STORAGE_STAGE_H__ 50 | -------------------------------------------------------------------------------- /src/observer/storage/mem/mem_storage_stage.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #include 16 | #include 17 | 18 | #include "mem_storage_stage.h" 19 | 20 | #include "common/conf/ini.h" 21 | #include "common/io/io.h" 22 | #include "common/lang/string.h" 23 | #include "common/log/log.h" 24 | #include "common/seda/timer_stage.h" 25 | #include "common/metrics/metrics_registry.h" 26 | 27 | using namespace common; 28 | 29 | //! Constructor 30 | MemStorageStage::MemStorageStage(const char *tag) : Stage(tag) 31 | {} 32 | 33 | //! Destructor 34 | MemStorageStage::~MemStorageStage() 35 | {} 36 | 37 | //! Parse properties, instantiate a stage object 38 | Stage *MemStorageStage::make_stage(const std::string &tag) 39 | { 40 | MemStorageStage *stage = new (std::nothrow) MemStorageStage(tag.c_str()); 41 | if (stage == nullptr) { 42 | LOG_ERROR("new MemStorageStage failed"); 43 | return nullptr; 44 | } 45 | stage->set_properties(); 46 | return stage; 47 | } 48 | 49 | //! Set properties for this object set in stage specific properties 50 | bool MemStorageStage::set_properties() 51 | { 52 | // std::string stageNameStr(stage_name_); 53 | // std::map section = g_properties()->get( 54 | // stageNameStr); 55 | // 56 | // std::map::iterator it; 57 | // 58 | // std::string key; 59 | 60 | return true; 61 | } 62 | 63 | //! Initialize stage params and validate outputs 64 | bool MemStorageStage::initialize() 65 | { 66 | LOG_TRACE("Enter"); 67 | 68 | LOG_TRACE("Exit"); 69 | return true; 70 | } 71 | 72 | //! Cleanup after disconnection 73 | void MemStorageStage::cleanup() 74 | { 75 | LOG_TRACE("Enter"); 76 | 77 | LOG_TRACE("Exit"); 78 | } 79 | 80 | void MemStorageStage::handle_event(StageEvent *event) 81 | { 82 | LOG_TRACE("Enter\n"); 83 | TimerStat timerStat(*queryMetric); 84 | 85 | event->done_immediate(); 86 | 87 | LOG_TRACE("Exit\n"); 88 | return; 89 | } 90 | 91 | void MemStorageStage::callback_event(StageEvent *event, CallbackContext *context) 92 | { 93 | LOG_TRACE("Enter\n"); 94 | 95 | LOG_TRACE("Exit\n"); 96 | return; 97 | } 98 | -------------------------------------------------------------------------------- /src/observer/storage/mem/mem_storage_stage.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/13. 13 | // 14 | 15 | #ifndef __OBSERVER_STORAGE_MEM_STORAGE_STAGE_H__ 16 | #define __OBSERVER_STORAGE_MEM_STORAGE_STAGE_H__ 17 | 18 | #include "common/seda/stage.h" 19 | #include "common/metrics/metrics.h" 20 | 21 | class MemStorageStage : public common::Stage { 22 | public: 23 | ~MemStorageStage(); 24 | static Stage *make_stage(const std::string &tag); 25 | 26 | protected: 27 | // common function 28 | MemStorageStage(const char *tag); 29 | bool set_properties(); 30 | 31 | bool initialize(); 32 | void cleanup(); 33 | void handle_event(common::StageEvent *event); 34 | void callback_event(common::StageEvent *event, common::CallbackContext *context); 35 | 36 | protected: 37 | common::SimpleTimer *queryMetric = nullptr; 38 | static const std::string QUERY_METRIC_TAG; 39 | 40 | private: 41 | }; 42 | 43 | #endif //__OBSERVER_STORAGE_MEM_STORAGE_STAGE_H__ 44 | -------------------------------------------------------------------------------- /test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | PROJECT(test) 2 | MESSAGE("Begin to build " ${PROJECT_NAME}) 3 | MESSAGE(STATUS "This is PROJECT_BINARY_DIR dir " ${PROJECT_BINARY_DIR}) 4 | MESSAGE(STATUS "This is PROJECT_SOURCE_DIR dir " ${PROJECT_SOURCE_DIR}) 5 | 6 | 7 | # 可以获取父cmake的变量 8 | MESSAGE("${CMAKE_COMMON_FLAGS}") 9 | 10 | 11 | #INCLUDE_DIRECTORIES([AFTER|BEFORE] [SYSTEM] dir1 dir2 ...) 12 | INCLUDE_DIRECTORIES(. ${PROJECT_SOURCE_DIR}/../deps /usr/local/include SYSTEM) 13 | # 父cmake 设置的include_directories 和link_directories并不传导到子cmake里面 14 | #INCLUDE_DIRECTORIES(BEFORE ${CMAKE_INSTALL_PREFIX}/include) 15 | LINK_DIRECTORIES(/usr/local/lib ${PROJECT_BINARY_DIR}/../lib) 16 | 17 | 18 | IF (DEFINED ENV{LD_LIBRARY_PATH}) 19 | SET(LD_LIBRARY_PATH_STR $ENV{LD_LIBRARY_PATH}) 20 | #separate_arguments(LD_LIBRARY_PATH_STR) #只能处理空行 21 | string(REPLACE ":" ";" LD_LIBRARY_PATH_LIST ${LD_LIBRARY_PATH_STR}) 22 | MESSAGE(" Add LD_LIBRARY_PATH to -L flags " ${LD_LIBRARY_PATH_LIST}) 23 | LINK_DIRECTORIES(${LD_LIBRARY_PATH_LIST}) 24 | ELSE () 25 | LINK_DIRECTORIES(/usr/local/lib) 26 | ENDIF () 27 | 28 | 29 | 30 | #get_filename_component( FileName 31 | # PATH|ABSOLUTE|NAME|EXT|NAME_WE|REALPATH 32 | # [CACHE]) 33 | FILE(GLOB_RECURSE ALL_SRC *.cpp) 34 | # AUX_SOURCE_DIRECTORY 类似功能 35 | FOREACH (F ${ALL_SRC}) 36 | get_filename_component(prjName ${F} NAME_WE) 37 | MESSAGE("Build ${prjName} according to ${F}") 38 | ADD_EXECUTABLE(${prjName} ${F}) 39 | TARGET_LINK_LIBRARIES(${prjName} common pthread dl observer_static) 40 | 41 | ENDFOREACH (F) 42 | 43 | -------------------------------------------------------------------------------- /unitest/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | PROJECT(unitest) 2 | MESSAGE("Begin to build " ${PROJECT_NAME}) 3 | MESSAGE(STATUS "This is PROJECT_BINARY_DIR dir " ${PROJECT_BINARY_DIR}) 4 | MESSAGE(STATUS "This is PROJECT_SOURCE_DIR dir " ${PROJECT_SOURCE_DIR}) 5 | 6 | 7 | # 可以获取父cmake的变量 8 | MESSAGE("${CMAKE_COMMON_FLAGS}") 9 | 10 | 11 | #INCLUDE_DIRECTORIES([AFTER|BEFORE] [SYSTEM] dir1 dir2 ...) 12 | INCLUDE_DIRECTORIES(. ${PROJECT_SOURCE_DIR}/../deps ${PROJECT_SOURCE_DIR}/../src/observer /usr/local/include SYSTEM) 13 | # 父cmake 设置的include_directories 和link_directories并不传导到子cmake里面 14 | #INCLUDE_DIRECTORIES(BEFORE ${CMAKE_INSTALL_PREFIX}/include) 15 | LINK_DIRECTORIES(/usr/local/lib /usr/local/lib64 ${PROJECT_BINARY_DIR}/../lib) 16 | 17 | 18 | IF (DEFINED ENV{LD_LIBRARY_PATH}) 19 | SET(LD_LIBRARY_PATH_STR $ENV{LD_LIBRARY_PATH}) 20 | #separate_arguments(LD_LIBRARY_PATH_STR) #只能处理空行 21 | string(REPLACE ":" ";" LD_LIBRARY_PATH_LIST ${LD_LIBRARY_PATH_STR}) 22 | MESSAGE(" Add LD_LIBRARY_PATH to -L flags " ${LD_LIBRARY_PATH_LIST}) 23 | LINK_DIRECTORIES(${LD_LIBRARY_PATH_LIST}) 24 | ELSE () 25 | LINK_DIRECTORIES(/usr/local/lib) 26 | ENDIF () 27 | 28 | 29 | find_package(GTest CONFIG REQUIRED) 30 | 31 | 32 | enable_testing() 33 | include(GoogleTest) 34 | #get_filename_component( FileName 35 | # PATH|ABSOLUTE|NAME|EXT|NAME_WE|REALPATH 36 | # [CACHE]) 37 | FILE(GLOB_RECURSE ALL_SRC *.cpp) 38 | # AUX_SOURCE_DIRECTORY 类似功能 39 | FOREACH (F ${ALL_SRC}) 40 | get_filename_component(prjName ${F} NAME_WE) 41 | MESSAGE("Build ${prjName} according to ${F}") 42 | ADD_EXECUTABLE(${prjName} ${F}) 43 | # 不是所有的单测都需要链接observer_static 44 | TARGET_LINK_LIBRARIES(${prjName} common pthread dl gtest gtest_main observer_static) 45 | gtest_discover_tests(${prjName}) 46 | ENDFOREACH (F) 47 | 48 | -------------------------------------------------------------------------------- /unitest/bitmap_test.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by wangyunlai.wyl on 2021 13 | // 14 | 15 | #include 16 | 17 | #include "gtest/gtest.h" 18 | #include "common/lang/bitmap.h" 19 | #include 20 | 21 | using namespace common; 22 | 23 | TEST(test_bitmap, test_bitmap) 24 | { 25 | char buf1[1]; 26 | memset(buf1, 0, sizeof(buf1)); 27 | Bitmap bitmap(buf1, 8); 28 | 29 | for (int i = 0; i < 8; i++) { 30 | ASSERT_EQ(bitmap.get_bit(i), false); 31 | } 32 | 33 | ASSERT_EQ(0, bitmap.next_unsetted_bit(0)); 34 | ASSERT_EQ(-1, bitmap.next_setted_bit(0)); 35 | 36 | for (int i = 0; i < 8; i++) { 37 | bitmap.set_bit(i); 38 | ASSERT_EQ(bitmap.get_bit(i), true); 39 | } 40 | ASSERT_EQ(-1, bitmap.next_unsetted_bit(0)); 41 | ASSERT_EQ(0, bitmap.next_setted_bit(0)); 42 | 43 | for (int i = 0; i < 8; i++) { 44 | bitmap.clear_bit(i); 45 | } 46 | for (int i = 0; i < 8; i++) { 47 | ASSERT_EQ(bitmap.get_bit(i), false); 48 | } 49 | 50 | char buf3[3]; 51 | memset(buf3, 0, sizeof(buf3)); 52 | 53 | Bitmap bitmap3(buf3, 22); 54 | ASSERT_EQ(0, bitmap3.next_unsetted_bit(0)); 55 | ASSERT_EQ(21, bitmap3.next_unsetted_bit(21)); 56 | ASSERT_EQ(-1, bitmap3.next_unsetted_bit(22)); 57 | 58 | buf3[0] = -1; 59 | buf3[1] = -1; 60 | buf3[2] = -1; 61 | ASSERT_EQ(0, bitmap3.next_setted_bit(0)); 62 | ASSERT_EQ(21, bitmap3.next_setted_bit(21)); 63 | ASSERT_EQ(-1, bitmap3.next_setted_bit(22)); 64 | 65 | buf3[1] = 0; 66 | ASSERT_EQ(8, bitmap3.next_unsetted_bit(0)); 67 | ASSERT_EQ(16, bitmap3.next_setted_bit(8)); 68 | } 69 | 70 | int main(int argc, char **argv) 71 | { 72 | // 分析gtest程序的命令行参数 73 | testing::InitGoogleTest(&argc, argv); 74 | 75 | // 调用RUN_ALL_TESTS()运行所有测试用例 76 | // main函数返回RUN_ALL_TESTS()的运行结果 77 | return RUN_ALL_TESTS(); 78 | } -------------------------------------------------------------------------------- /unitest/bp_manager_test.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by wangyunlai.wyl on 2021 13 | // 14 | 15 | #include "storage/default/disk_buffer_pool.h" 16 | #include "gtest/gtest.h" 17 | 18 | void test_get(BPManager &bp_manager) 19 | { 20 | Frame *frame1 = bp_manager.alloc(); 21 | ASSERT_NE(frame1, nullptr); 22 | 23 | frame1->file_desc = 0; 24 | frame1->page.page_num = 1; 25 | 26 | ASSERT_EQ(frame1, bp_manager.get(0, 1)); 27 | 28 | Frame *frame2 = bp_manager.alloc(); 29 | ASSERT_NE(frame2, nullptr); 30 | frame2->file_desc = 0; 31 | frame2->page.page_num = 2; 32 | 33 | ASSERT_EQ(frame1, bp_manager.get(0, 1)); 34 | 35 | Frame *frame3 = bp_manager.alloc(); 36 | ASSERT_NE(frame3, nullptr); 37 | frame3->file_desc = 0; 38 | frame3->page.page_num = 3; 39 | 40 | frame2 = bp_manager.get(0, 2); 41 | ASSERT_NE(frame2, nullptr); 42 | 43 | Frame *frame4 = bp_manager.alloc(); 44 | frame4->file_desc = 0; 45 | frame4->page.page_num = 4; 46 | 47 | bp_manager.free(frame1); 48 | frame1 = bp_manager.get(0, 1); 49 | ASSERT_EQ(frame1, nullptr); 50 | 51 | ASSERT_EQ(frame3, bp_manager.get(0, 3)); 52 | 53 | ASSERT_EQ(frame4, bp_manager.get(0, 4)); 54 | 55 | bp_manager.free(frame2); 56 | bp_manager.free(frame3); 57 | bp_manager.free(frame4); 58 | 59 | ASSERT_EQ(nullptr, bp_manager.get(0, 2)); 60 | ASSERT_EQ(nullptr, bp_manager.get(0, 3)); 61 | ASSERT_EQ(nullptr, bp_manager.get(0, 4)); 62 | } 63 | 64 | void test_alloc(BPManager &bp_manager) 65 | { 66 | int size = bp_manager.get_size(); 67 | 68 | std::list used_list; 69 | 70 | for (int i = 0; i < size; i++) { 71 | Frame *item = bp_manager.alloc(); 72 | ASSERT_NE(item, nullptr); 73 | used_list.push_back(item); 74 | } 75 | 76 | ASSERT_EQ(used_list.size(), bp_manager.get_used_num()); 77 | 78 | for (int i = 0; i < size; i++) { 79 | Frame *item = bp_manager.alloc(); 80 | 81 | ASSERT_EQ(item, nullptr); 82 | } 83 | 84 | for (int i = 0; i < size * 10; i++) { 85 | if (i % 2 == 0) { 86 | Frame *item = used_list.front(); 87 | used_list.pop_front(); 88 | 89 | bp_manager.free(item); 90 | } else { 91 | Frame *item = bp_manager.alloc(); 92 | used_list.push_back(item); 93 | } 94 | 95 | ASSERT_EQ(used_list.size(), bp_manager.get_used_num()); 96 | } 97 | } 98 | 99 | TEST(test_bp_manager, test_bp_manager_simple_lru) 100 | { 101 | BPManager bp_manager("Test"); 102 | bp_manager.init(false, 2); 103 | 104 | test_get(bp_manager); 105 | 106 | test_alloc(bp_manager); 107 | 108 | bp_manager.cleanup(); 109 | } 110 | 111 | int main(int argc, char **argv) 112 | { 113 | 114 | // 分析gtest程序的命令行参数 115 | testing::InitGoogleTest(&argc, argv); 116 | 117 | // 调用RUN_ALL_TESTS()运行所有测试用例 118 | // main函数返回RUN_ALL_TESTS()的运行结果 119 | return RUN_ALL_TESTS(); 120 | } -------------------------------------------------------------------------------- /unitest/log_test.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021 13 | // 14 | 15 | #include "log_test.h" 16 | 17 | #include "gtest/gtest.h" 18 | 19 | #include "common/log/log.h" 20 | 21 | using namespace common; 22 | 23 | LogTest::LogTest() 24 | { 25 | // Auto-generated constructor stub 26 | } 27 | 28 | LogTest::~LogTest() 29 | { 30 | // Auto-generated destructor stub 31 | } 32 | 33 | int LogTest::init(const std::string &logFile) 34 | { 35 | 36 | LoggerFactory::init_default(logFile); 37 | 38 | g_log->set_rotate_type(LOG_ROTATE_BYSIZE); 39 | 40 | return 0; 41 | } 42 | 43 | void *LogTest::log_loop(void *param) 44 | { 45 | int index = *(int *)param; 46 | int i = 0; 47 | while (i < 100) { 48 | i++; 49 | LOG_INFO("index:%d --> %d", index, i); 50 | } 51 | 52 | return NULL; 53 | } 54 | 55 | void checkRotate() 56 | { 57 | LogTest test; 58 | 59 | test.init(); 60 | ASSERT_EQ(g_log->get_rotate_type(), LOG_ROTATE_BYSIZE); 61 | 62 | int index = 30; 63 | test.log_loop(&index); 64 | } 65 | 66 | TEST(checkRotateTest, CheckRoateTest) 67 | { 68 | checkRotate(); 69 | } 70 | 71 | void testEnableTest() 72 | { 73 | LogTest test; 74 | 75 | test.init(); 76 | 77 | ASSERT_EQ(g_log->check_output(LOG_LEVEL_PANIC, __FILE__), true); 78 | ASSERT_EQ(g_log->check_output(LOG_LEVEL_ERR, __FILE__), true); 79 | ASSERT_EQ(g_log->check_output(LOG_LEVEL_WARN, __FILE__), true); 80 | ASSERT_EQ(g_log->check_output(LOG_LEVEL_INFO, __FILE__), true); 81 | ASSERT_EQ(g_log->check_output(LOG_LEVEL_DEBUG, __FILE__), false); 82 | ASSERT_EQ(g_log->check_output(LOG_LEVEL_TRACE, __FILE__), false); 83 | ASSERT_EQ(g_log->check_output(LOG_LEVEL_LAST, __FILE__), false); 84 | 85 | g_log->set_default_module(__FILE__); 86 | 87 | ASSERT_EQ(g_log->check_output(LOG_LEVEL_PANIC, __FILE__), true); 88 | ASSERT_EQ(g_log->check_output(LOG_LEVEL_ERR, __FILE__), true); 89 | ASSERT_EQ(g_log->check_output(LOG_LEVEL_WARN, __FILE__), true); 90 | ASSERT_EQ(g_log->check_output(LOG_LEVEL_INFO, __FILE__), true); 91 | ASSERT_EQ(g_log->check_output(LOG_LEVEL_DEBUG, __FILE__), true); 92 | ASSERT_EQ(g_log->check_output(LOG_LEVEL_TRACE, __FILE__), true); 93 | ASSERT_EQ(g_log->check_output(LOG_LEVEL_LAST, __FILE__), true); 94 | } 95 | 96 | TEST(testEnableTest, CheckEnableTest) 97 | { 98 | testEnableTest(); 99 | } 100 | 101 | int main(int argc, char **argv) 102 | { 103 | 104 | // 分析gtest程序的命令行参数 105 | testing::InitGoogleTest(&argc, argv); 106 | 107 | // 调用RUN_ALL_TESTS()运行所有测试用例 108 | // main函数返回RUN_ALL_TESTS()的运行结果 109 | return RUN_ALL_TESTS(); 110 | } 111 | -------------------------------------------------------------------------------- /unitest/log_test.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021 13 | // 14 | 15 | #ifndef CTESTLOG_H_ 16 | #define CTESTLOG_H_ 17 | 18 | #include 19 | 20 | /* 21 | * 22 | */ 23 | class LogTest { 24 | public: 25 | LogTest(); 26 | virtual ~LogTest(); 27 | 28 | int init(const std::string &logFile = "test.log"); 29 | 30 | void *log_loop(void *param); 31 | }; 32 | 33 | #endif /* CTESTLOG_H_ */ 34 | -------------------------------------------------------------------------------- /unitest/md5_test.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021 13 | // 14 | 15 | #include 16 | 17 | #include "common/math/md5.h" 18 | #include "md5_test.h" 19 | 20 | using namespace common; 21 | 22 | Md5Test::Md5Test() 23 | { 24 | // Auto-generated constructor stub 25 | } 26 | 27 | Md5Test::~Md5Test() 28 | { 29 | // Auto-generated destructor stub 30 | } 31 | 32 | void Md5Test::string() 33 | { 34 | char buf[512] = "/home/fastdfs/longda"; 35 | unsigned char digest[16] = {0}; 36 | MD5String(buf, digest); 37 | for (int i = 0; i < 16; i++) { 38 | printf("%d: %02x %d\n", i, digest[i], digest[i]); 39 | } 40 | } 41 | 42 | int main(int argc, char **argv) 43 | { 44 | Md5Test test; 45 | test.string(); 46 | 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /unitest/md5_test.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021 13 | // 14 | 15 | #ifndef CTESTMD5_H_ 16 | #define CTESTMD5_H_ 17 | 18 | /* 19 | * 20 | */ 21 | class Md5Test { 22 | public: 23 | Md5Test(); 24 | virtual ~Md5Test(); 25 | 26 | void string(); 27 | }; 28 | 29 | #endif /* CTESTMD5_H_ */ 30 | -------------------------------------------------------------------------------- /unitest/mem_pool_test.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by longda on 2022 13 | // 14 | 15 | #include 16 | #include 17 | #include "common/mm/mem_pool.h" 18 | #include "gtest/gtest.h" 19 | 20 | using namespace common; 21 | 22 | TEST(test_mem_pool_item, test_mem_pool_item_basic) 23 | { 24 | MemPoolItem mem_pool_item("test"); 25 | 26 | const int item_num_per_pool = 128; 27 | mem_pool_item.init(32, true, 1, item_num_per_pool); 28 | std::list used_list; 29 | 30 | int alloc_num = 1000; 31 | 32 | for (int i = 0; i < alloc_num; i++) { 33 | void *item = mem_pool_item.alloc(); 34 | used_list.push_back(item); 35 | } 36 | 37 | std::cout << mem_pool_item.to_string() << std::endl; 38 | 39 | int pool_size = ((alloc_num + item_num_per_pool - 1) / item_num_per_pool) * item_num_per_pool; 40 | ASSERT_EQ(alloc_num, mem_pool_item.get_used_num()); 41 | ASSERT_EQ(pool_size, mem_pool_item.get_size()); 42 | ASSERT_EQ(item_num_per_pool, mem_pool_item.get_item_num_per_pool()); 43 | ASSERT_EQ(32, mem_pool_item.get_item_size()); 44 | 45 | int free_num = item_num_per_pool * 3; 46 | for (int i = 0; i < free_num; i++) { 47 | auto item = used_list.front(); 48 | used_list.pop_front(); 49 | 50 | char *check = (char *)item + 10; 51 | mem_pool_item.free(item); 52 | mem_pool_item.free(check); 53 | } 54 | 55 | std::cout << mem_pool_item.to_string() << std::endl; 56 | ASSERT_EQ(used_list.size(), mem_pool_item.get_used_num()); 57 | ASSERT_EQ(pool_size, mem_pool_item.get_size()); 58 | } 59 | 60 | int main(int argc, char **argv) 61 | { 62 | 63 | // 分析gtest程序的命令行参数 64 | testing::InitGoogleTest(&argc, argv); 65 | 66 | // 调用RUN_ALL_TESTS()运行所有测试用例 67 | // main函数返回RUN_ALL_TESTS()的运行结果 68 | return RUN_ALL_TESTS(); 69 | } -------------------------------------------------------------------------------- /unitest/path_test.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021 13 | // 14 | 15 | int main(int argc, char **argv) 16 | { 17 | return 0; 18 | } -------------------------------------------------------------------------------- /unitest/pidfile_test.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/4/16. 13 | // 14 | #include 15 | 16 | #include "gtest/gtest.h" 17 | 18 | #include "common/os/pidfile.h" 19 | #include "common/io/io.h" 20 | #include "common/lang/string.h" 21 | 22 | using namespace common; 23 | 24 | int main() 25 | { 26 | long long pid = (long long)getpid(); 27 | 28 | const char *programName = "test"; 29 | writePidFile(programName); 30 | 31 | std::string pidFile = getPidPath(); 32 | 33 | char buf[1024] = {0}; 34 | char *p = buf; 35 | size_t size = 0; 36 | readFromFile(pidFile, p, size); 37 | 38 | std::string temp(p); 39 | long long target = 0; 40 | str_to_val(temp, target); 41 | 42 | EXPECT_EQ(pid, target); 43 | } -------------------------------------------------------------------------------- /unitest/rc_test.cpp: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021/5/3. 13 | // 14 | #include "rc.h" 15 | #include 16 | 17 | int main(int argc, char **argv) 18 | { 19 | 20 | std::cout << rc2SimpleStr(status) << std::endl; 21 | } -------------------------------------------------------------------------------- /unitest/thread_test.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2021 Xie Meiyi(xiemeiyi@hust.edu.cn) and OceanBase and/or its affiliates. All rights reserved. 2 | miniob is licensed under Mulan PSL v2. 3 | You can use this software according to the terms and conditions of the Mulan PSL v2. 4 | You may obtain a copy of Mulan PSL v2 at: 5 | http://license.coscl.org.cn/MulanPSL2 6 | THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 7 | EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 8 | MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 9 | See the Mulan PSL v2 for more details. */ 10 | 11 | // 12 | // Created by Longda on 2021 13 | // 14 | 15 | #ifndef CTESTTHREAD_H_ 16 | #define CTESTTHREAD_H_ 17 | 18 | #include "common/lang/mutex.h" 19 | 20 | /* 21 | * 22 | */ 23 | class ThreadTest { 24 | public: 25 | ThreadTest(); 26 | virtual ~ThreadTest(); 27 | 28 | int create(); 29 | 30 | int startTestCond(); 31 | int startTestDeadLock(); 32 | 33 | static void *testCond(void *param); 34 | static void *testDeadLock(void *param); 35 | 36 | private: 37 | int param[10]; 38 | 39 | pthread_mutex_t mutex; 40 | pthread_cond_t cond; 41 | 42 | pthread_mutex_t dead_mutex1; 43 | pthread_mutex_t dead_mutex2; 44 | }; 45 | 46 | #endif /* CTESTTHREAD_H_ */ 47 | --------------------------------------------------------------------------------